// JavaScript Document

function GetXmlHttpObject(){
	var xmlHttp=null;
	try{
		// Firefox, Opera 8.0+, Safari
		xmlHttp=new XMLHttpRequest();
	}
	catch (e){
	// Internet Explorer
		try{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch (e){
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}

function remove_item(e){
	document.getElementById(e).value = 0;
	document.update_form.submit();
}

function hideAllPops(){
	if(document.getElementById("checkout_option")){
			document.getElementById("checkout_option").style.display = "none";
	}
	if(document.getElementById("wholesale_login_pop")){
			document.getElementById("wholesale_login_pop").style.display = "none";
	}
	if(document.getElementById("video_pop")){
			document.getElementById("video_pop").style.display = "none";
	}
}

function show_cart_popup(){
	if(document.getElementById("checkout_option")){
			show_info("checkout_option", null, null, 100);
	}
}

function show_wholesaler(){
	
}

function show_video(vid, title){
	//clear child nodes...
	$("#video_pop").empty();
	
	$("#video_pop").append("<div id=\"video_pop_video\"></div><h2 id=\"vid_title\">" + title + "</h2>");

	vid = 'http://c0561032.cdn.cloudfiles.rackspacecloud.com/' + vid;
//	alert(vid);
	var flashvars = {
		videoURL:vid,
		autoPlay:true,
		scaleMode:"noScale",
		startPhotoSource:"assets/videos/training.gif"
	};
	var params = {allowFullScreen:"true",autoPlay:true};
	var attributes = {};
	params.allowscriptaccess = "always"; 
	swfobject.embedSWF("assets/media/train_player.swf", "video_pop_video", "480", "240", "9.0.115.0", "assets/media/expressInstall.swf", flashvars, params, attributes);
	
	show_info('video_pop', 520, null, null, 'video_pop_video' );
}

function add_to_cart(product_id){
	var quantity = document.getElementById('input_quantity').value;
	if(quantity!=""){
		send_to_cart(product_id, quantity);	
	}

}

function send_to_cart(product_id, quantity){
	xmlHttp=GetXmlHttpObject();
	if (xmlHttp==null){
	  alert ("Your browser does not support AJAX!");
	  return;
	} 
	var url="ajax_add_to_cart.php";
	var params="pid=" + product_id;
	params=params+"&q="+quantity;
	params=params+"&cid="+cart_id;
	params=params+"&sid="+Math.random(); //used to force no cache of URL

	url = url + "?" + params;

	xmlHttp.onreadystatechange=onCartResponse;
	xmlHttp.open("GET",url,true);
	xmlHttp.send(params);
}

function onCartResponse(){
	if (xmlHttp.readyState==4){ 
			var e = document.getElementById('header_cart_count');
			cart_count = xmlHttp.responseText * 1;
			if(e==null){
				var d= document.getElementById('cart_count');
				d.innerHTML = '<a href="cart.php"><span id="header_cart_count"></span> in your cart <strong>Checkout</strong></a>';	
				
				e = document.getElementById('header_cart_count');
			}
			
			if(cart_count==1){
				e.innerHTML = cart_count + " item";	
			}else{
				e.innerHTML = cart_count + " items";	
			}	

			show_cart_popup();
	}
}

function close_info(){
	$("#frameContent embed").remove();
	$("#frameContent object").remove();

	showFlash();

	$("#frame").fadeOut("fast")
	$("#cover").hide();  

}

function show_wholesaler_login(){
	show_info('wholesale_login_pop');	
}

function show_info(elemID, frameWidth, frameHeight, padTop, excludeFlash){
	//define height and width of popup frame
	hideFlash(excludeFlash);
	hideAllPops();
	
	if(frameWidth==null)
		var frameWidth = 660;

	var close_left = frameWidth - 14;
	if(frameHeight==null)
		var frameHeight = 468;

	if(padTop==null)
		var padTop = 0;

	$("#" + elemID).show();

	// stretch overlay to fill page and fade in
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	$("#cover").css("width", arrayPageSize[0]+arrayPageScroll[0]+"px");
	$("#cover").css("height", arrayPageSize[1]+arrayPageScroll[1]+'px');
	
	$("#closeButton").css("left", close_left + 'px');

	var boxTop = arrayPageScroll[1] + 30 + padTop;
	var boxLeft = arrayPageScroll[0] + Math.round(arrayPageSize[0] / 2);
	if(boxLeft > frameWidth/2){ boxLeft -= Math.round(frameWidth/2);}

	$("#frame").css("top", boxTop + 'px');
	$("#frame").css("left", boxLeft + 'px');
	$("#frame").css("width", frameWidth + 'px');

	$("#cover").show();
	$("#frame").show();

	//frame.style.visibility = 'visible';
}


// -----------------------------------------------------------------------------------
//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
//	console.log(self.innerWidth);
//	console.log(document.documentElement.clientWidth);

	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

//	console.log("xScroll " + xScroll)
//	console.log("windowWidth " + windowWidth)

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
//	console.log("pageWidth " + pageWidth)

	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}


// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){
	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// ---------------------------------------------------

function showFlash(){
	$("object").css("visibility", "visible");
	$("embed").css("visibility", "visible");
}

// ---------------------------------------------------

function hideFlash(except){
	$("object:not(#"+except+")").css("visibility", "hidden");
	$("embed:not(#"+except+")").css("visibility", "hidden");
}

