var DDSPEED = 2;  //Higher the slower. 100/11(DDSPEED) = 9px height change per interval
var DDTIMER = 3;  //How often the interval is called in Milliseconds

function imageH(img, state) {

	if (state == 1) {
		if ( img.src.indexOf('H.') < 0 ) {
			var pos = img.src.lastIndexOf('.');
			img.src = img.src.substr(0, pos) + 'H' + img.src.substr(pos);
		}
	} else {
		img.src = img.src.replace('H.','.');		
	}
}

function changeImage(imgName, newImage) {
	document.getElementById(imgName).src = newImage;
}

function getImageName(imageName,parameters) {
	var insertPos = imageName.lastIndexOf('/') + 1;
	imageName = imageName.substr(0,insertPos) + bkblanguage.substring(0,2).toUpperCase() + "-" + imageName.substr(insertPos);

	if (!parameters)
		parameters="";
	
	document.write("<img src='" + imageName + "' " + parameters + " >");
}

function showDiv(divName) {
	var divHandle = document.getElementById(divName);
	divHandle.style.visibility = "visible";
	divHandle.style.display = "";
}

function hideDiv(divName) {
	var divHandle = document.getElementById(divName);
	divHandle.style.visibility = "hidden";
	divHandle.style.display = "none";	
}

function hideDivPause(divName) {
	setTimeout('hideDiv("'+divName+'")',1000);
}
	
function showTab(tab, totalTabs) {
	for( x=1; x<=totalTabs; x++ ) {
		tmpItem = document.getElementById("divTab" + x);
		if (x == tab ) {
			tmpItem.style.visibility = "visible";
			tmpItem.style.display = "";
			imageH(document.getElementById("tab" + x),1);
		} else {
			tmpItem.style.visibility = "hidden";
			tmpItem.style.display = "none";
			imageH(document.getElementById("tab" + x),0);
		}			
	}
}

// main function to handle the mouse events //
function ddMenu(id,d){
  var h = document.getElementById(id + '-ddheader');
  var hTxt = document.getElementById(bkblanguage + '_' + id + '-ddheaderTxt');
  var c = document.getElementById(id + '-ddcontent');
  clearInterval(c.timer);
  if(d == 1){
    clearTimeout(h.timer);
    if(c.maxh && c.maxh <= c.offsetHeight){return}
    else if(!c.maxh){
      c.style.display = 'block';
      c.style.height = 'auto';
      c.maxh = c.offsetHeight;
      c.style.height = '0px';
    }
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }else{
    //h.timer = setTimeout(function(){ddCollapse(c)},200);  //Time to stall the menu from closing
    h.timer = setTimeout(function(){c.style.height=0;hTxt.style.backgroundColor='';hTxt.style.color='#555555';},200);  //Time to stall the menu from closing
  }
 hTxt.style.backgroundColor='#555555';
 hTxt.style.color='#ffffff';
}

// collapse the menu //
function ddCollapse(c){
  //c.timer = setInterval(function(){ddSlide(c,-1)},1);
}

// cancel the collapse if a user rolls over the dropdown //
function cancelHide(id){
  var h = document.getElementById(id + '-ddheader');
  var c = document.getElementById(id + '-ddcontent');
  clearTimeout(h.timer);
  clearInterval(c.timer);
  if(c.offsetHeight < c.maxh){
    c.timer = setInterval(function(){ddSlide(c,1)},DDTIMER);
  }
}

// incrementally expand/contract the dropdown and change the opacity //
function ddSlide(c,d){

  var currh = c.offsetHeight;
  var dist;
  
  if(d == 1){
    dist = (Math.round((c.maxh - currh) / DDSPEED));
  }else{
    if ( currh > 0 ) {
		dist = 1;
    } else {
		dist = (Math.round(currh / DDSPEED));
	}
  }
  if(dist <= 1 && d == 1){
    dist = 1;
  }
  
  c.style.height = currh + (dist * d) + 'px';
  c.style.opacity = currh / c.maxh;
  c.style.filter = 'alpha(opacity=' + (currh * 100 / c.maxh) + ')';
		window.status = currh;

  if((currh < 2 && d != 1) || (currh > (c.maxh - 2) && d == 1)){
    clearInterval(c.timer);
  }
}

// Adds multiple items to the shopping cart. This is used by all product pages
function getItemsToAdd(divNum) {
	var x = document.getElementById('divTab'+divNum).getElementsByTagName('div');	
	var itemsToAdd = new Array();
	var n = 0;

	for (var i=0;i<x.length;i++) {								//Cycle through the Div
		if ( x[i].id.substr(0,18) == 'relatedItemOptions' ) {
			var y = x[i].getElementsByTagName('input');			//Cycle through the input fields of divs relatedItemOptions
			for (var j=0;j<y.length;j++) {
				if (y[j].id == 'addItem') {
					if (y[j].checked == true) {					//If checkbox found with name 'addItem' and checked display value
						var submitStr = 'c=446037&n=1&buyid='+y[j].value;
						var z = x[i].getElementsByTagName('select');	// Cycle through drop down items
						for (var k=0;k<z.length;k++) {
							if (z[k].value == '') {
								alert("Please review the options for your selected Accessories.");
								return;
							} else {
								submitStr = submitStr + '&' + z[k].id + '=' + z[k].value;
							}
						}
						itemsToAdd[n] = submitStr;
						n+=1;
					}
				}
			}
		} else if ( x[i].id.substr(0,10) == 'addProduct' ) {
			var qty = document.getElementById('qty').value;
			var buyId = document.getElementById('productId').value;
			if ( qty > 0 ) {
				var z = x[i].getElementsByTagName('select');	// Cycle through drop down items
				var submitStr = 'c=446037&n=1&buyid='+buyId +'&qty='+qty;
				
				for (var k=0;k<z.length;k++) {
					if (z[k].value == '') {
						alert("Please review the options for the item(s) you are trying to add to the cart.");
						return;
					} else {
						submitStr = submitStr + '&' + z[k].id + '=' + z[k].value;
					}
				}
				itemsToAdd[n] = submitStr;
				n+=1;				
			} else {
				alert("Please enter a quantity.");
				return;
			}
		}
	}
	
	for (var i=0;i<n;i++) {
		postRequest("http://www.bestkiteboarding.com/app/site/backend/additemtocart.nl", itemsToAdd[i]);
	}
	document.location.href = "http://www.bestkiteboarding.com/s.nl?sc=3";
}


var xmlhttp;
function loadXMLDoc(url) {
	xmlhttp=null;
	if (window.XMLHttpRequest) { 						// code for Firefox, Opera, IE7, etc.
	  xmlhttp=new XMLHttpRequest();
	} else if (window.ActiveXObject) {					// code for IE6, IE5
	  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	
	if (xmlhttp!=null) {
	  xmlhttp.onreadystatechange=state_Change;
	  xmlhttp.open("GET",url,true);
	  xmlhttp.send(null);
	} else {
	  alert("Your browser does not support XMLHTTP.");
	}
}

function state_Change() {
	if (xmlhttp.readyState==4) {						// 4 = "loaded"
		if (xmlhttp.status==200) {						// 200 = "OK"
			var closeButton = '<div align="right" width="100%"><a onclick="javascript:document.getElementById(\'contentBox\').style.display=\'none\';" style="cursor:pointer;">Close X</a></div><br>';
			document.getElementById('contentBox').innerHTML=closeButton + xmlhttp.responseText + closeButton;
		} else {
			alert("Problem retrieving data:" + xmlhttp.statusText);
		}
	}
}
			
function showContentBox(url,w,h) {
	
	var newdiv = document.getElementById('contentBox');
	
	if (!newdiv) {
		newdiv = document.createElement('div');
		newdiv.setAttribute('id','contentBox');			
	}	
	
	newdiv.style.visibility = "visible";
	newdiv.style.display = "block";
	newdiv.style.top = "140px";
	newdiv.style.left = "50%";
	newdiv.style.marginLeft = (-1*w/2) + "px";
	newdiv.style.width = w + "px";
	newdiv.style.height = h + "px";
	newdiv.style.position = "absolute";
	newdiv.style.background = "#FFFFFF";
	newdiv.style.zIndex = "100";
	newdiv.style.padding = "2px";
	newdiv.style.border = "1px solid #555555";
	newdiv.style.overflow = "auto";

	document.body.appendChild(newdiv);		
	loadXMLDoc(url);
}	
