// JavaScript Document

jQuery.noConflict();
$j = jQuery;
$j(document).ready(function()
{
	
	//$j("#demo img[title]").tooltip();
	noFunctionTip = "<div class='tiptextstyle'><span><strong>ATTENZIONE.</strong></span><br >"+
				"La WhishList non &egrave; attualmente disponibile, la funzionalit&agrave; &egrave; solamente dimostrativa.<br />"+
				"Non verranno salvate le preferenze ed eseguite le procedure di acquisto se richieste tramite la pressione del tasto &quot;Carrello&quot;.<br />"+
				"Renderemo operativa la WishList appena possibile.</div>";
	
	tip = "<div class='tiptextstyle'><span>WishList</span><br >"+
				"Potete aggiungere i prodotti alla WishList facendo click su simbolo <img src='../mainimg/add.png' /><br />"+
				"La WishList verr&agrave; memorizzata nel Vostro PC e pronta per un successivo utilizzo nei Vostri acquisti.</div>";
				
	$j('#infoWL').attr('title', noFunctionTip);
	$j("#infoWL").tooltip({ effect: 'slide'});

});
						
var Timer = function()
{        
    // Property: Frequency of elapse event of the timer in millisecond
    this.Interval = 1000;
    
    // Property: Whether the timer is enable or not
    this.Enable = new Boolean(false);
    
    // Event: Timer tick
    this.Tick;
    
    // Member variable: Hold interval id of the timer
    var timerId = 0;
    
    // Member variable: Hold instance of this class
    var thisObject;
    
    // Function: Start the timer
    this.Start = function()
    {
        this.Enable = new Boolean(true);

        thisObject = this;
        if (thisObject.Enable)
        {
            thisObject.timerId = setInterval(
            function()
            {
                thisObject.Tick(); 
            }, thisObject.Interval);
        }
    };
    
    // Function: Stops the timer
    this.Stop = function()
    {            
        thisObject.Enable = new Boolean(false);
        clearInterval(thisObject.timerId);
    };

};


//var aPages = new Array();
/*aPages[0] = "http://www.freelandtime.com";
aPages[1] = "http://www.freelandtime.com/wellness.html";
aPages[2] = "http://www.freelandtime.com/micoterapia.html";
aPages[3] = "http://www.freelandtime.com/listini/listino.html";
*/

function addPage(){
	var pagename = document.getElementById('addRemovePage').innerHTML;
	aPages.push(pagename);
}

function removePage(){
	var pagename = document.getElementById('addRemovePage').innerHTML;
	for(var i=0; i<aPages.length; i++){
		if(aPages[i].toLowerCase() == pagename){
			aPages.splice(i, 1);
			break;
		}
	}
}



//var tim = setInterval ('callPage()', 5000);
var nItem = -1;

var showtime = true;
var currentTime = "";

var ttime = 5;
var subtime = 0;
var elap = 5;

var countLimit = 10;
var lastPageCount = 10;

var obj = new Timer();
obj.Interval = 100;
obj.Tick = timer_tick;

function switchtime(select) 
{ 
	var index; 

  for(index=0; index<select.options.length; index++) 
    if(select.options[index].selected) 
      { 
        if(select.options[index].value!="") 
					document.getElementById('myTime').innerHTML = "Current time is: " + select.options[index].title;
					obj.Stop();
					elap = select.options[index].value;
					ttime = elap;
					obj.Start();
        break; 
      } 
}

function switchCountLimit(select){
	var count; 
  for(count=0; count<select.options.length; count++) 
    if(select.options[count].selected) 
    { 
	    if(select.options[count].value!="") 
				countLimit = select.options[count].value;
				lastPageCount = countLimit;
				document.getElementById('countpage').innerHTML = countLimit;
	      break; 
	} 
}

function startInterval(){
	if(aPages.length > 0){
	timer_tick();
	obj.Start();
	}
}

function stopInterval(){
	obj.Stop();
}

function timer_tick()
{
	if(countLimit >= 0){
		document.getElementById('countpage').innerHTML = countLimit;
		if (ttime == elap) 
		{
			nItem = nItem+1;
			if(nItem >= aPages.length){ nItem = 0; }
			var url = aPages[nItem];
			
			var legend = document.getElementById('myLegend');
			legend.innerHTML = "item n.: " + (nItem+1) + " - address: " + url;
		
			// carica solo le pagine iniziali (index.html)
			//loadIframe('ifrm', url);
			// carica le pagine iniziali e i link
			changeIframeSrc('ifrm', url);
			ttime = 0;
			countLimit -= 1;
		}
	}else{
		document.getElementById("elapsed").innerHTML = 0;
		obj.Stop();
		countLimit = lastPageCount;
		ttime = elap;
	}
	subtime  = subtime + 1;
	if(showtime ?	(document.getElementById("elapsed").innerHTML = ttime + ":" + subtime) : (document.getElementById("elapsed").innerHTML = "") );
	if (subtime == 10) 
	{
		ttime += 1;
		subtime = 0;
	}
}

function ctr(cbox){
	if(cbox.checked ? showtime = true : showtime = false);
}

/* Free code from dyn-web.com */

// Two choices for loading new pages into the iframe 
function loadIframe(iframeName, url) {
    if ( window.frames[iframeName] ) {
        window.frames[iframeName].location = url;   
        return false;
    }
    return true;
}

function changeIframeSrc(id, url) {
    if (!document.getElementById) return;
    var el = document.getElementById(id);
    if (el && el.src) {
        el.src = url;
        return false;
    }
    return true;
}

