var fadeinto;
window.onresize=resizePageFilter;


function filterDisplayDiv(article_number) {
	
	loadProductInfo(article_number);
	document.getElementById('productInformationWrapper').style.top=getScrollPosition()+50+'px';
	fadeInDiv('productInformationWrapper');
	displayDiv('pageFilter');

	resizePageFilter();
	//alertSize();
	
}
function getScrollPosition() {
	return window.pageYOffset || document.documentElement.scrollTop || 0;
	//alert(window.pageYOffset || document.documentElement.scrollTop || 0);
} 

function getPageHeight() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
  return myHeight;
}
function resizePageFilter() {
	var pageSize2 = getPageSizeWithScroll();
	var pageSize = getPageHeight();
	var pageSize3 = document.getElementById('productInformationWrapper').offsetHeight;
	//alert(pageSize3);
	//alert('pageSize=' +pageSize+ '\r\n pageSize3=' +pageSize2+ '\r\n pageSize3=' +pageSize3)
	if(pageSize2 > pageSize){
		pageSize=pageSize2;
		//alert('pageSize=' +pageSize+ '\r\n pageSize2=' +pageSize2)
	}
	if(pageSize3 > pageSize){
		pageSize=pageSize3;
		//alert('pageSize=' +pageSize+ '\r\n pageSize3=' +pageSize3)
	}
	//alert(pageSize[0]  + "x" + pageSize[1])
	document.getElementById('pageFilter').style.height=pageSize + "px";
	//getPageSizeWithScroll();
} 
function filterHideDiv() {
	
	fadeOutDiv('productInformationWrapper');
	hideDiv('pageFilter');
	
} 

function displayDiv(id) {
	
	document.getElementById(id).style.display="block";
	
} 
function hideDiv(id) {
	document.getElementById(id).style.display="none";
	document.getElementById(id).style.height="1px";
} 

function fadeInDiv(id) {
	
	document.getElementById(id).style.display="none";
	fadeIn(0,id);
	//fadeinto=setTimeout("fadeIn(" + 0 + ",'" + id + "')",10);
} 

function fadeOutDiv(id) {
	
	fadeoutto=setTimeout("fadeOut(" + 100 + ",'" + id + "')",10);
	
} 

function fadeIn(opacity, id) { 
	opacity+=25;
	
	var os = document.getElementById(id).style; 
	
	os.opacity = (opacity / 100); 
	os.MozOpacity = (opacity / 100); 
	os.KhtmlOpacity = (opacity / 100); 
	os.filter = "alpha(opacity=" + opacity + ")";
	os.display="block";
	
	clearTimeout(fadeinto);
	if(opacity<100){
		fadeinto=setTimeout("fadeIn(" + opacity + ",'" + id + "')",10);
	}
	else {
		resizePageFilter(); // FF
	}
	
}

function fadeOut(opacity, id) { 
	opacity-=25;
	
	var os = document.getElementById(id).style; 
	os.opacity = (opacity / 100); 
	os.MozOpacity = (opacity / 100); 
	os.KhtmlOpacity = (opacity / 100); 
	os.filter = "alpha(opacity=" + opacity + ")";
	
	clearTimeout(fadeoutto);
	
	if(opacity>0)	fadeoutto=setTimeout("fadeOut(" + opacity + ",'" + id + "')",10);	
	else		os.display="none";
	 	
	
} 

function getPageSizeWithScroll(){    
	if (window.innerHeight && window.scrollMaxY) {
		// Firefox         
		yWithScroll = window.innerHeight + window.scrollMaxY;         
		xWithScroll = window.innerWidth + window.scrollMaxX;   
		 
	} 
	else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac         
		yWithScroll = document.body.scrollHeight;         
		xWithScroll = document.body.scrollWidth;  
		
	} 
	else { // works in Explorer 6 Strict, Mozilla (not FF) and Safari         
		yWithScroll = document.body.scrollHeight;


		xWithScroll = document.body.offsetWidth;   
		//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );   
	}     
	//arrayPageSizeWithScroll = new Array(xWithScroll,yWithScroll);     
	//alert( 'The height is ' + yWithScroll + ' and the width is ' + xWithScroll );   
	
	return yWithScroll; 
} 

function loadProductInfo(article_number) {
			
	var xmlHttpA=null; // Defines that xmlHttp is a new variable.
	// Try to get the right object for different browser
	try {
		// Firefox, Opera 8.0+, Safari
		xmlHttpA = new XMLHttpRequest(); // xmlHttp is now a XMLHttpRequest.
	} 
	catch (e) {
		// Internet Explorer
		try {
			xmlHttpA=new ActiveXObject("Msxml2.XMLHTTP");
		} 
		catch (e) {
			xmlHttpA=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	xmlHttpA.onreadystatechange = function() {
		if (xmlHttpA.readyState == 4)
		try { // In some instance, status cannot be retrieve and will produce an error (ex: Port is not responsive)
			//alert(xmlHttpA.status);
			if (xmlHttpA.status == 200) {
				//Set the main HTML of the body to the info provided by the AJAX Request
				//document.getElementById("ajax_output").innerHTML = xmlHttp.responseText;
				
				document.getElementById('image').innerHTML = "<img style=\"\" src=\"/files/products/" +getAjaxResponse(xmlHttpA.responseText,"productid")+ "/imageProductInformation.jpg\" >";
				document.getElementById('description').innerHTML = getAjaxResponse(xmlHttpA.responseText,"description");
				document.getElementById('product').innerHTML = getAjaxResponse(xmlHttpA.responseText,"product");
				document.getElementById('ingredients').innerHTML = getAjaxResponse(xmlHttpA.responseText,"ingredients").replace(/\r\n/g,"<br>");
				
				document.getElementById('productLabelTitle').innerHTML = getAjaxResponse(xmlHttpA.responseText,"product");
				document.getElementById('productLabelIngredients').innerHTML = getAjaxResponse(xmlHttpA.responseText,"ingredients").replace(/\r\n/g,", ");
				
				//document.getElementById('image').innerHTML = "<span style=\"background-image:url(/files/products/" +getAjaxResponse(xmlHttpA.responseText,"productid")+ "/imageProductInformation.jpg)\" ></span>";
				
				
				//if(xmlHttpA.responseText.indexOf("success") != -1) {
					//document.getElementById(rowID).innerText=printRecord(name,15);
					
				//}
			}
		//alert(xmlHttpA.status);
		
		} 
		catch (e) {
			//document.getElementById("ajax_output").innerHTML = "Error on Ajax return call : " + e.description;
			alert("Error on Ajax return call 1: " + e.description);
			//¤ return "Error on Ajax return call : " + e.description;
			//alert(5);
		
		}
	}
	xmlHttpA.open("get", "loadProductInfo.aspx?article_number=" + article_number);
	xmlHttpA.send(null); // Since there is no supplied form, null takes its place as a new form.
}

function getAjaxResponse(responseText, varName) {
		
	//[VAR productid={82}][VAR product={Rågbulle}][VAR description={}][VAR ingredients={}][VAR imageProductInformation={}]
	
	var found = false;
	var start;
	var end;
	
	var startString = "[VAR " +varName+ "={";
	var endString = "}]";
	
	start = responseText.indexOf(startString);  	
	if (start != -1){
		start += startString.length;
		end = responseText.indexOf(endString,start)
		responseText = responseText.substring(start,end);
		
		found = true;	
	}
	
	if(found)	return responseText;
	else		return "";
}

function printProductInfo(type) {
	document.getElementById('productInformationWrapper').style.top='50px';
	/*
	if(type = 'productInformation'){
		document.getElementById('productLabel').style.display='none';
		print();
		document.getElementById('productLabel').style.display='block';
		
	}
	else if(type = 'productLabel'){
		document.getElementById('productInformationSheet').style.display='none';
		print();
		document.getElementById('productInformationSheet').style.display='block';
	}
	*/
	print();
	document.getElementById('productInformationWrapper').style.top=getScrollPosition()+50+'px';
}
function printDiv(ID){
  var content=document.getElementById(ID).innerHTML
  var pwin=window.open('','print_content','width=100,height=100');

  pwin.document.open();
  pwin.document.write('<html><head><link rel="stylesheet" type="text/css" media="print" href="/css/productInformationPrint.css"></head><body onload="window.print()">'+content+'</body></html>');
  pwin.document.close();
 
  setTimeout(function(){pwin.close();},1000);

}