var IE7 = false;	

init();

function init(){
	if (navigator.appVersion.indexOf("Mac")!=-1) {
		$('body').addClass('mac');
	} else {
		$('body').addClass('realpc');
	}	
	if (navigator.appVersion.indexOf("MSIE 7")!=-1) {
		IE7 = true;	
	}
}

/*************************     SLIDER   *******************************/

if(document.getElementById("ats")){
	var aL = 1;
	var tP = document.getElementById("ats").value;
}

function gotoPanel(id){
	if(id != aL){
		$('#sliderContent').animate({left: -(960 * (id-1))}, 400);		
		
		document.getElementById("b" + id).className = "a";
		document.getElementById("b" + aL).className = "";
		
		aL = id;
		if(cP == 1){			
			document.getElementById("bLeft").className += " d";	
			document.getElementById("bRight").className = "bNav";
		}else if(cP == tP){			
			document.getElementById("bLeft").className = "bNav";
			document.getElementById("bRight").className += " d";	
		}else{			
			document.getElementById("bLeft").className = "bNav";
			document.getElementById("bRight").className = "bNav";
		}
		
		Cufon.refresh();
	}	
}


function bL(){
	if(aL != 1){
		id = aL-1;
		gotoPanel(id);
		
	}
}
function bR(){
	if(aL != tP){
		id = aL+1;
		gotoPanel(id);
	}
}



/*************************   DISTRIBUTION  ***********************************/

$(function() {  
	$("#s").jqDropDown({
		optionChanged: function(){ sC();}	,
		placeholder: '#x' 
	});
}); 

function sC(){
	var x= document.getElementById("x").innerHTML;
	/*
	var y= document.getElementsByTagName("option");	
	var c= y[x].value;	
	*/
	if(x != "Select a country"){
		changeCountry(x);
	}
}

function changeCountry(c){		
	var u = window.location.href + "";
	u = u.split("/");
	u = u.length;
	
	if(u == 4){
		window.location += "/c/" + c;
	}else{
		window.location = "../c/" + c;
	}
}

function iM(){
	if(document.getElementById("map")){
		if (GBrowserIsCompatible()) {			
			
			var u = window.location.href + "";
			u = u.split("/");
			l = u.length;			
			
			var map = new GMap2(document.getElementById("map"));
			map.setMapType(G_NORMAL_MAP );
			map.addControl(new GSmallMapControl());
			
			if(l == 4){		
				var dist = document.getElementById("distData").value;				
				dist = dist.split(";");
				
				for(i=0;i<dist.length-1;i++){					
					gC(dist[i],i,map);				
				}
				
			}else{
				getCountry(u[5],1,map);		
			}
			
			if(l == 4){
				map.setCenter(new GLatLng(36.031332,12.304688), 2);
			}else{			
				shops = eC("shop");			
				for(i=0;i<shops.length;i++){	
					getShops(shops[i].value,i,map);				
				}
			}		
			
		}
	}
}

var xhr = new Array(); // ARRAY OF XML-HTTP REQUESTS

function gC(a,i,map){
	d = a.split("%");	
	c = d[1];
	a = d[0];
	a = urlencode(a);	
	
	if (window.XMLHttpRequest)
	{
		xhr[i]=new XMLHttpRequest();
	}
	else
	{
		xhr[i]=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xhr[i].onreadystatechange=function(){ ;
		if (xhr[i].readyState==4 && xhr[i].status==200)
		{
			if(xhr[i].responseText != ""){
				c = xhr[i].responseText;
				aP(c,i,map);
				
				var u = window.location.href + "";
				u = u.split("/");
				l = u.length;
				
				if(l > 4){
					cor = c.split(",");
					map.setCenter(new GLatLng(cor[0], cor[1]), 7);
					document.getElementById("map").style.display = "block";
				}				
			}
		}
	}	
	xhr[i].open("GET", "#" + a + "&c=" + c,true);
	xhr[i].send();	
}

var xh = new Array(); // ARRAY OF XML-HTTP REQUESTS

function getShops(a,i,map){
	
	d = a.split("%");	
	c = d[1];
	id = d[2];
	a = d[0];
	a = urlencode(a);	
	
	
	
	if (window.XMLHttpRequest)
	{
		xh[i]=new XMLHttpRequest();
	}
	else
	{
		xh[i]=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xh[i].onreadystatechange=function(){ ;
		if (xh[i].readyState==4 && xh[i].status==200)
		{
			if(xh[i].responseText != ""){
				c = xh[i].responseText;
				aS(c,map);						
			}
		}
	}	
	xh[i].open("GET", "#" + a + "&c=" + c + "&id=" + id,true);
	xh[i].send();	
}


var xc = new Array(); // ARRAY OF XML-HTTP REQUESTS

function getCountry(a,i,map){		
	
	if (window.XMLHttpRequest)
	{
		xc[i]=new XMLHttpRequest();
	}
	else
	{
		xc[i]=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	xc[i].onreadystatechange=function(){ ;
		if (xc[i].readyState==4 && xc[i].status==200)
		{
			if(xc[i].responseText != ""){
				c = xc[i].responseText;
				cor = c.split(",");
				var locatie = new GLatLng(cor[0], cor[1]);				
				var marker = new GMarker(locatie);							
				map.addOverlay(marker);	
				GEvent.addListener(marker, "click", function() {
					changeCountry(cor[2]);
				});		
				
				var u = window.location.href + "";
				u = u.split("/");
				l = u.length;
				
				if(l > 4){
					map.setCenter(new GLatLng(cor[0], cor[1]), 7);
				}			
			}
		}
	}	
	xc[i].open("GET", "#" + a + "&c=" + a,true);
	xc[i].send();	
}


function aP(cor,i, map){
	cor = cor.split(",");
	if(cor[0] != "0" && cor[1] != "0"){
		var locatie = new GLatLng(cor[0], cor[1]);				
		var marker = new GMarker(locatie);							
		map.addOverlay(marker);	
		GEvent.addListener(marker, "click", function() {
			changeCountry(cor[2]);
		});
	}else{
		 getCountry(cor[2],i,map);
	}
}


function aS(cor, map){
	cor = cor.split(",");
	var locatie = new GLatLng(cor[0], cor[1]);			
	n = document.getElementById("shop" + cor[3]).innerHTML
	
	var m = createMarker(locatie,n); 
	map.addOverlay(m);	
}


function createMarker(latlng,n) { 
	
	var icon_blue = new GIcon(G_DEFAULT_ICON);
	icon_blue.iconSize = new GSize(32, 32);	
	icon_blue.image = "red-dot.png";
	
	marker = new GMarker(latlng,icon_blue); 
	GEvent.addListener(marker, "mouseover", function() { 
		showInfo(n);
	}); 
	
	return marker;
} 

function showInfo(html){
	document.getElementById("showInfo").style.display = "block";
	document.getElementById("showInfo").innerHTML = html;
}

prevID = 1;

function doNav(id){
	if(id != prevID){
		document.getElementById("block" + prevID).style.display = "none";
		document.getElementById("block" + id).style.display = "block";
		
		document.getElementById("n" + prevID).className = "";
		document.getElementById("n" + id).className = "active";
		prevID = id;
	}
}

/*************************   DOWNLOADS  ***********************************/

function o(id){	
	gH = parseInt(gS(document.getElementById(id), "height"));
	if(gH == 0){
		 doo(id)
	}else{
		 doc(id)
	}
}
function doo(id){
	h = document.getElementById("c" + id).value;
	document.getElementById("a" + id).className = "active cat";
	$('#' + id).animate({height: h * 30}, 300);
}
function doc(id){
	document.getElementById("a" + id).className = "cat";
	$('#' + id).animate({height: 0}, 300);
}


/*********************   GENERAL FUNCTIONS  *******************************/

function urlencode (str) {    
    str = (str+'').toString();
    return encodeURIComponent(str).replace(/%20/g, '+').replace(/,/g, '%2C');
}

function gS(oElm, strCssRule){  /* GETSTYLE */
	var strValue = "";
	if(document.defaultView && document.defaultView.getComputedStyle){
		strValue = document.defaultView.getComputedStyle(oElm, "").getPropertyValue(strCssRule);
	}
	else if(oElm.currentStyle){	
		strCssRule = strCssRule.replace(/\-(\w)/g, function (strMatch, p1){
			return p1.toUpperCase();
		});
		strValue = oElm.currentStyle[strCssRule];
	}
	if(strValue == "auto" || strValue == "100%"){
		if(strCssRule == "width"){
			strValue = oElm.offsetWidth;	
		}
		if(strCssRule == "height"){
			strValue = oElm.offsetHeight;
		}
	}
	return strValue;
}

function p(u){	 /* CREATE POP-UP */
    w = window.open(u, "Share on Facebook", "location=0,status=0,scrollbars=0, width=575,height=380");    
}

function eC(needle) {
	var my_array = document.getElementsByTagName("*");
	var retvalue = new Array();
	var i;
	var j;
	for (i=0,j=0;i<my_array.length;i++) {
		var c = " " + my_array[i].className + " ";
		if (c.indexOf(" " + needle + " ") != -1) retvalue[j++] = my_array[i];
	}
	return retvalue;
}

