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;	
	}
}

/*************************     BRANDBOX SLIDER   *******************************/


var cP = 1;
if($('#brandbox').length){
	var tP = $('#pc').value;
}


function gotoPanel(id){
	if(id != cP){
		$('#sliderContent').animate({left: -(795 * (id-1))}, 400);		
		
		document.getElementById("b" + id).className = "a";
		document.getElementById("b" + cP).className = "";
		
		cP = parseInt(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(cP != 1){
		id = cP-1;
		gotoPanel(id);
		
	}
}
function bR(){
	if(cP != tP){
		id = cP+1;
		gotoPanel(id);
	}
}

/*************************   ABOUT SLIDER   *******************************/


if(document.getElementById("tP")){
	var cP = 1;
	var tP = document.getElementById("tP").value;
	
	function mD(id){
		if(id != cP){
			$('#detailHolder').animate({left: -(785 * (id-1))}, 400);		
			$('#history').animate({left: -(128 * (id-1))}, 400);		
			
			if(IE7 == false){
				$('#d' + id).animate({opacity: 1}, 250);	
				$('#d' + cP).animate({opacity: 0}, 250);	
			}
			
			document.getElementById("h" + id).className = "a";
			document.getElementById("h" + cP).className = "";
					
			cP = id;
			if(cP == 1){			
				document.getElementById("mLeft").className += " d";	
				document.getElementById("mRight").className = "mNav";
			}else if(cP == tP){			
				document.getElementById("mLeft").className = "mNav";
				document.getElementById("mRight").className += " d";	
			}else{			
				document.getElementById("mLeft").className = "mNav";
				document.getElementById("mRight").className = "mNav";
			}		
			
			Cufon.refresh();
		}	
	}
	function mL(){
		if(cP != 1){
			id = cP-1;
			mD(id);
			
		}
	}
	function mR(){
		if(cP != tP){
			id = cP+1;
			mD(id);
		}
	}
}

/*********************     COLLECTION SLIDER   *******************************/

if(document.getElementById("slider")){
	
	var cC = cCI();    /* COLLECTION COUNT */
	var fP = 1;    /* FIRST PRODUCT */
	var a = false; /* IS ANIMATING ? */
	
}

function sR(){ /* SLIDE RIGHT */
	if(a == false){			
		if(fP != cC-2 && cC > 3){			
			a = true;
			if(cC - (fP+2) >= 3){
				s = 3;	
			}else{
				s = cC - (fP+2);
			}
			for(i = 1; i <= cC;i++){
				mL = parseInt(gS(document.getElementById('p' + i), "left"));	
				pW = parseInt(gS(document.getElementById('p' + i), "width"));	
				$('#p' + i).delay((100*(i-1)) - ((fP-3)*100)).animate({left: mL - (pW*s)}, 100*(s+2));
			}
			var t = setTimeout ( function(){a = false;}, (100*(cC-1))+200 );
			fP = fP + s;			
			if(fP == cC-2){
				document.getElementById("slideRight").className += " d";				
			}
			document.getElementById("slideLeft").className = "slideNav";
			
		}
	}
}
function sL(){ /* SLIDE LEFT */
	if(a == false){
		if(fP != 1){
			a = true;
			if((fP-1)%3 != 0){
				if(fP >3){
					if(fP%2 == 0){
						s = 2;
					}else{
						s = 1;
					}
				}else{
					s = fP -1;
				}
			}else{
				s = 3;				
			}	
			//alert("fP: " + fP + "   ;   s: " + s);
			for(i = 1; i <= cC;i++){
				mL = parseInt(gS(document.getElementById('p' + i), "left"));	
				pW = parseInt(gS(document.getElementById('p' + i), "width"));	
				$('#p' + i).delay(100*(fP-i+2)).animate({left: mL + (pW*s)}, 100*(s+2));	
			}
			var t = setTimeout ( function(){a = false;}, (100*(cC-1))+200 );
			fP = fP - s;
			if(fP == 1){
				document.getElementById("slideLeft").className += " d";
			}
			document.getElementById("slideRight").className = "slideNav";
			
		}
	}
}

function cCI(){  /* COUNT COLLECTION ITEMS */
	cC = 0;
	arr = document.getElementById("slider").childNodes;	
	for(var i=0;i<arr.length;i++){
		if(arr[i] != "[object Text]"){cC++;}
	}	
	return(cC-2);
}


/*********************   PRODUCT DETAIL FUNCTIONS  *******************************/

if(document.getElementById("cP")){
	prevId = 0;
	cP(document.getElementById("cP").value);
	prevId = document.getElementById("cP").value; /* ID OF CURRENTLY DISPLAYED PRODUCT */
	
	first = true;
	
	if (window.addEventListener){			
		window.addEventListener('load', sN, false);
		window.addEventListener('resize', sN, false);	
	}else{
		window.onload = document.onload =  sN;
		window.onresize = document.onresize = sN;
	}
}

function sN(){  /* SHOW/HIDE NAV */
	if(navigator.appName == "Microsoft Internet Explorer"){
		var v = navigator.appVersion;
		ioH = v.indexOf("(");
		v = v.substr(0, ioH - 1);	
		if(v == "5.0"){
			wW = window.innerWidth;
		}else{
			wW = document.body.clientWidth;
		}
	}else{
		wW = window.innerWidth;
	}
	if(parseInt(wW) <= 1100){
		if(first == true){
			first = false;
			document.getElementById("minNav").style.display = "block";
			document.getElementById("maxNav").style.display = "none";
		}else{
			$('#minNav').fadeIn();
			$('#maxNav').fadeOut();
		}
	}else{
		if(first == true){
			first = false;
			document.getElementById("minNav").style.display = "none";
			document.getElementById("maxNav").style.display = "block";		
		}else{
			$('#maxNav').fadeIn();
			$('#minNav').fadeOut();
		}
	}
	
}

function showP(id){  /* SCROLL TO PRODUCT DETAIL */
	$('html,body').animate({scrollTop: 870}, 450);
	cP(id);	
}

function cP(id){ /* SHOW PRODUCT DETAIL */
	if(prevId != id){
		$('#pD' +prevId).fadeOut();
		$('#pD' +id).fadeIn();
		prevId = parseInt(id);
		
		if(prevId == 1){
			document.getElementById("goLeft").className += " d";
			document.getElementById("goRight").className = "goNav";
			document.getElementById("gL").className += " d";
			document.getElementById("gR").className = "goNav";
		}else if(prevId == cC){
			document.getElementById("goLeft").className = "goNav";
			document.getElementById("goRight").className += " d";
			document.getElementById("gL").className = "goNav";
			document.getElementById("gR").className += " d";
		}else{
			document.getElementById("goLeft").className = "goNav";
			document.getElementById("goRight").className = "goNav";
			document.getElementById("gL").className = "goNav";
			document.getElementById("gR").className = "goNav";
		}
	}
}

function b(){	/*  BACK TO COLLECTIONS  */
	$('html,body').animate({scrollTop: 100}, 300);
}

function c(){	/*  BACK TO COLLECTIONS  */
	$('html,body').animate({scrollTop: 455}, 300);
}


function gL(){  /*  NAVIGATE BETWEEN PRODUCTS (  go left ) */
	if(prevId != 1){
		cP(parseInt(prevId) - 1);	
	}
}
function gR(){ /*  NAVIGATE BETWEEN PRODUCTS (  go right ) */	
	if(prevId != cC){
		cP(parseInt(prevId) + 1);	
	}
}


/*********************   NEWS  *******************************/

function t(){ /* SCROLL TO TOP */
	$('html,body').animate({scrollTop: 0}, 300);
}

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

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

function sC(){
	var x= document.getElementById("x").innerHTML;
	if(x != "Select a country"){
		changeCountry(x);
	}
}
*/

$("#s").jqDropDown({
    optionChanged: function(elem){sC();},
    placeholder: '#x' ,
    effect: 'fade', effectSpeed: 300
});

function sC(elem){
    var selectedVal = $('ul.ddOptionList').find('a.selected').text();
	if(selectedVal != "Select a country"){
		changeCountry(selectedVal.toLowerCase());
	}
    $('#value').html(selectedVal);
}

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", "http://rodania.com/inc/functions/getCor.php?a=" + 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", "http://rodania.com/inc/functions/getCor.php?a=" + 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", "http://rodania.com/inc/functions/getCor.php?a=" + 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 = "http://rodania.com/images/layout/blue-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;
}

