window.onload = (function(){

			ctylanguage =  readCookie("country_link");
			ctyname = readCookie("country_name");
			if (ctylanguage!= null)
			{
			    var linkquery = document.location.href;
			    //alert(linkquery.indexOf( "http://www.flash8magic.com/" + ctylanguage.toLowerCase()));
			    if (linkquery.indexOf( "http://www.flash8magic.com/" + ctylanguage.toLowerCase()) >= 0 ||  linkquery.indexOf( "http://flash8magic.com/" + ctylanguage.toLowerCase()) >= 0 )
			    {

				    divObj = document.getElementById("selectedCountry");
				    divObj.innerHTML = ctyname;
			    }
			    else 
			    {
				    switchCountry(ctylanguage);
			    }

			}
            
// load countries
$.ajax({
	type: "GET",
	url: "res/countries.xml",
	dataType: "xml",
	success: function(xml) 
	{

		var countrylanguage =  readCookie("country_link");
		if (countrylanguage!=null)
		{
			if (countrylanguage == 'us' || countrylanguage == 'en')
			{
				countrylanguage = 'en2';
			}
		}
		else
		{
			countrylanguage = 'en2';
		}

		
		$(xml).find('country').each(function(){
			var cName = $(this).find(countrylanguage).text();
			var cLink = $(this).find('link').text();
			$('<li><a href="#" onclick="setCountry(\''+cName+'\',\'' +cLink+'\');">'+cName+'</a></li>').appendTo('#CountryList');
		});
		$('#CountryList').wrapInner('<ul></ul>');
		reinitialiseScrollPane();
	}
	
	}	
	);
	

	function readCookie(name) 
	{
		var nameEQ = name + "=";
		var ca = document.cookie.split(';');
		for(var i=0;i < ca.length;i++) 
		{
			var c = ca[i];
			while (c.charAt(0)==' ') c = c.substring(1,c.length);
			if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
		}
		return null;
	}


	
	//----------------------------------------------------
	// country selector				  
	var cSel = document.getElementById("CountrySelector");
	cSel.onmouseout = (function(){
		displayObject1('CountryBox','hide');
	});
	cSel.onmouseover = (function(){
		displayObject1('CountryBox','show');
	});
	var cBox = document.getElementById("CountryBox");
	cBox.onmouseout = (function(){
		displayObject1('CountryBox','hide');
	});
	cBox.onmouseover = (function(){
		displayObject1('CountryBox','show');
	});
	//----------------------------------------------------
	setMenu();

});

function highlightMenuItem(itemId){
	document.getElementById("mainmenu").className = 'sel'+itemId;
}

function setMenu(){
	document.getElementById("hand").className = 'sel'+activePage+'pos';
	if(activePage>0){
		document.getElementById("mm"+activePage).className = "selected";
	}
}

// ----------------------------------------------------------------------------
function setCountry(cName,cLink) {
	displayObject1('CountryBox','hide');
	var selectorObj = document.getElementById("selectedCountry");
	selectorObj.innerHTML = cName;

	//Write cookie
	expires = "";
	document.cookie = "country_name="+cName+expires+"; path=/";
	document.cookie = "country_link="+cLink+expires+"; path=/";
	//Redirect
	switchCountry(cLink);
}

function switchCountry(cLink) 
{
	var siteLink = document.location.href;

	if (siteLink.indexOf( "http://www.flash8magic.com/") >= 0 )
	{
		window.location = "http://www.flash8magic.com/" + cLink.toLowerCase();
	}

	if (siteLink.indexOf( "http://flash8magic.com/" ) >= 0 )
	{
		window.location = "http://flash8magic.com/" + cLink.toLowerCase();
	}
}
// ----------------------------------------------------------------------------

function selectSorter(sorterId){
	var sourceObj = document.getElementById("sorter"+sorterId);
	var selObj = document.getElementById("selectedSorter");
	selObj.innerHTML = sourceObj.innerHTML;
}

function selectFilter(filterId){
	var sourceObj = document.getElementById("filter"+filterId);
	var selObj = document.getElementById("selectedFilter");
	selObj.innerHTML = sourceObj.innerHTML;
}


// ----------------------------------------------------------------------------
function displayObject(elemId,action) {
  var object = document.getElementById(elemId);
  if(action == "show" ) {
    object.style.display = "";
  }
  else {
    object.style.display = "none";
  }
}

function displayObject1(elemId,action) {
  var object = document.getElementById(elemId);
  if(action == "show" ) {
    object.style.visibility = "visible";
  }
  else {
    object.style.visibility = "hidden";
  }
}

function setMenuSelection(elemId,action){
  var object = document.getElementById(elemId);
  if(action == "select" ) {
    object.className = 'selected';
  }
  else {
    object.className = 'none';
  }
}

function displayBlock(sourceText){
	var temp = new Array();
	temp = sourceText.split("/");
	var blockName = temp[0];
	var selectedItem = temp[1];
	var maxItems = temp[2];
	for(var i=1;i<=maxItems;i++){
		displayObject(blockName+i+"data","hide");
		setMenuSelection(blockName+i+"link","unselect");
	}
	displayObject(blockName+selectedItem+"data","show");
	setMenuSelection(blockName+selectedItem+"link","select");
    clearInterval(myInterval);
    myInterval = setInterval('slideSwitch(\'' + selectedItem + '\')', 5000);
}

// ---------------------------------------------------------------------------
var $next = null;
var myInterval = null;

function slideSwitch(id) {
    $next = null;
    var $active = $('#slide' + id + 'show IMG.active');
    if ( $active.length == 0 ) $active = $('#slide' + id + 'show IMG:last');
    // use this to pull the images in the order they appear in the markup
    $next =  $active.next().length ? $active.next()
        : $('#slide'+id+'show IMG:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0}).addClass('active')
        .animate({opacity: 1.0}, 1000, null, function(id) {
            $active.removeClass('active last-active');
        });
}

$(function(id) {
     myInterval = setInterval( "slideSwitch(1)", 5000 );
});


// xmlHttp will store the reference to the XMLHttpRequest object
  var xmlHttp;
  // try to instantiate the native XMLHttpRequest object
  try
  {
    // create an XMLHttpRequest object
    xmlHttp = new XMLHttpRequest();
  }
  catch(e)
  {
    // assume IE6 or older
    try
    {
      xmlHttp = new ActiveXObject("Microsoft.XMLHttp");
    }
    catch(e) { }
  }
  // return the created object or display an error message
  if (!xmlHttp)
    alert("Error creating the XMLHttpRequest object.");

    
    function SendRating(productID, vote)
    {
        // call the server to execute the server side operation
        xmlHttp.open("GET", "/vote.php?pid=" + productID + "&v=" + vote, true);
        xmlHttp.onreadystatechange = handleRequestStateChange;
        xmlHttp.send(null);
    }

    function SendPWD()
    {
        var p = document.getElementById("password");
        var c = document.getElementById("remember");
        
        if(c.checked == true)
        {
            expires = "";
            document.cookie = "password_q="+p.value+expires+"; path=/";
        }
        
        
        // call the server to execute the server side operation
        xmlHttp.open("GET", "/pwd.php?p=" + p.value + "&c=" + (c.checked == true ? 1 : 0), true);
        xmlHttp.onreadystatechange = handleRequestStateChangePWD;
        xmlHttp.send(null);
    }

    function DownloadFile(id)
    {
        // call the server to execute the server side operation
        xmlHttp.open("GET", "/download.php?pid=" + id, true);
        xmlHttp.onreadystatechange = dldFile;
        xmlHttp.send(null);
    }


    function DownloadFileG(id)
    {
        // call the server to execute the server side operation
        xmlHttp.open("GET", "/download.php?pid=" + id+"&isg=1", true);
        xmlHttp.onreadystatechange = dldFileG;
        xmlHttp.send(null);
    }
    
    function dldFile()
    {
        
        // continue if the process is completed
      if (xmlHttp.readyState == 4) 
      {
        // continue only if HTTP status is "OK"
        if (xmlHttp.status == 200) 
        {
          try
          {
            // retrieve the response
            response = xmlHttp.responseText;
            if (response != "x")
            {
                var url='/download/'+response;    
                window.location.href = url;
                //window.open(url,'_blank'); 
            }
            else
            {
                errordiv =  document.getElementById("error"); 
                errordiv.style.display = "";
            }
          }
          catch(e)
          {
            // display error message
            alert("Could not download, please try again!" +e);
          }
        } 
        else
        {
          // display status message
          alert("Could not download, please try again!\nThere was a problem retrieving the data:\n" + 
                xmlHttp.statusText);
        }
      }
    }
    
    
    function dldFileG()
    {
        // continue if the process is completed
      if (xmlHttp.readyState == 4) 
      {
        // continue only if HTTP status is "OK"
        if (xmlHttp.status == 200) 
        {
          try
          {
            // retrieve the response
            response = xmlHttp.responseText;
            if (response != "x")
            {
                var url='/download/groups/'+response;    
                window.location.href = url;
                //window.open(url,'_blank'); 
            }
            else
            {
                errordiv =  document.getElementById("error"); 
                errordiv.style.display = "";
            }
          }
          catch(e)
          {
            // display error message
            alert("Could not download, please try again!" +e);
          }
        } 
        else
        {
          // display status message
          alert("Could not download, please try again!\nThere was a problem retrieving the data:\n" + 
                xmlHttp.statusText);
        }
      }
    }
    
    // function executed when the state of the request changes
    function handleRequestStateChangePWD() 
    {
      // continue if the process is completed
      if (xmlHttp.readyState == 4) 
      {
        // continue only if HTTP status is "OK"
        if (xmlHttp.status == 200) 
        {
          try
          {
            // retrieve the response
            response = xmlHttp.responseText;
            //alert(response);
            if (response == "ok")
            {
                displayObject("passwordlayer","hide");
                for(i=1; i<=itemsSize;i++)
                {
                    displayObject("item"+i+"exclusive","hide");
                    displayObject("item"+i+"getpass","hide");
                    displayObject("item"+i+"download","show");                    
                }
            }
            else
            {
                errordiv =  document.getElementById("error"); 
                errordiv.style.display = "";
            }
          }
          catch(e)
          {
            // display error message
            alert("Password not computed, please try again!" +e);
          }
        } 
        else
        {
          // display status message
          alert("Password not computed, please try again!\nThere was a problem retrieving the data:\n" + 
                xmlHttp.status);
        }
      }
    }
    
    // function executed when the state of the request changes
    function handleRequestStateChange() 
    {
      // continue if the process is completed
      if (xmlHttp.readyState == 4) 
      {
        // continue only if HTTP status is "OK"
        if (xmlHttp.status == 200) 
        {
          try
          {
            // retrieve the response
            response = xmlHttp.responseText;
            var arr =  response.split("/");
            
            var s1 = document.getElementById("s1_" + arr[0]);
            s1.className = "";
            if (1<=arr[1])
            {
                s1.className = "sel";
            }
            
            var s2 = document.getElementById("s2_" + arr[0]);
            s2.className = "";
            if (2<=arr[1])
            {
                s2.className = "sel";
            }
            
            var s3 = document.getElementById("s3_" + arr[0]);
            s3.className = "";
            if (3<=arr[1])
            {
                s3.className = "sel";
            }
            
            var s4 = document.getElementById("s4_" + arr[0]);
            s4.className = "";
            if (4<=arr[1])
            {
                s4.className = "sel";
            }
            
            var s5 = document.getElementById("s5_" + arr[0]);
            s5.className = "";
            if (5<=arr[1])
            {
                s5.className = "sel";
            }
            
          }
          catch(e)
          {
            // display error message
            alert("Vote not registered, please try again!");
          }
        } 
        else
        {
          // display status message
          alert("Vote not registered, please try again!\nThere was a problem retrieving the data:\n" + 
                xmlHttp.statusText);
        }
      }
    }
