function addItem(name)
{	
	
	if($("#free3").length > 0)
	{
		var free3 = $("#free3").attr("value");
	}
	if($("#free4").length > 0)
	{
		var free4 = $("#free4").attr("value");
	}
	if($("#free4sizehat").length > 0)
	{
		var free4sizehat = $("#free4sizehat").attr("value");
	}
	if($("#free4size").length > 0)
	{
		var free4size = $("#free4size").attr("value");
	}
	
	var selectedItem = $("#" + name + "_current").val();
		$.ajax({
				   type: "POST",
				   url: "/_additem.html",
				   async: false,
				   cache: false,
				   data: "item=" + selectedItem,
				   success: function(msg)
				   {
						$("#mystacklist").html(msg);
				   } // end success
			 }); //  end ajax call
	if($("#free3").length > 0)
	{
		$("#free3 option[value = " + free3 + "]").attr("selected", "selected");
	}
	if($("#free4").length > 0)
	{
		$("#free4 option[value = " + free4 + "]").attr("selected", "selected");

		if(free4 == "-2")
		{
			$("#free4sizehat").show();
			$("#free4sizehat option[value = " + free4sizehat + "]").attr("selected", "selected");
		}
		if(free4 == "-1")
		{
			$("#free4size").show();
			$("#free4size option[value = " + free4size + "]").attr("selected", "selected");
		}
	}
}

function removeItem(name)
{	

	$.ajax({
			   type: "POST",
			   url: "/_removeitem.html",
			   data: "item=" + name,
			   async: false,
			   cache: false,
			   success: function(msg)
			   { 
					//used to update the mystack DIV
				 	addItem("0");
			   }
	});
}

function updatePrice(product_name, id)
{
	
	$.ajax({
			   type: "POST",
			   url: "/_getPrice.html",
			   data: "pid=" + id,
			   async: false,
			   cache: false,
			   success: function(msg)
			   { 
					var output = msg.split('|');
			 		$("#display_price_" + product_name).html(output[0]);
			 		$("#display_member_price_" + product_name).html(output[1]);
			   }
	});
}

function useFilter()
{
	var selectedItem = $("#displayMenu").val();
	
	$.ajax({
			   type: "POST",
			   url: "/_itemslisting.html",
			   data: "cat=" + selectedItem,
			   async: false,
			   cache: false,
			   success: function(msg)
			   { 
					$("#product_listing_div").html(msg);
			   }
	});
}

$(document).ready(function(){
						   
	useFilter();
	addItem("0");
	getStackTemplates("1");
	$(".add").tooltip();
	
	$(".dropdown_item")
	.mouseover(function(){$(this).css("background-color", "#aaaaaa")})
	.mouseout(function(){$(this).css("background-color", "#333333")});
 });

function updateItem(name, id)
{
	var size_flavor = $("#" + id + "_item").text();
	$("#" + name + "_current").val(id);
	$("#" + name + "Select").val(size_flavor);
	$("#" + name + "_dropdiv").hide();
	
	updatePrice(name, id);
}


function getStackTemplates(tab)
{
	$.ajax({
			   type: "POST",
			   url: "/_stacktabs.html",
			   async: false,
			   cache: false,
			   data: "sid=" + tab,
			   success: function(msg)
			   {
					$("#prebuilt_tabs").html(msg);
			   } // end success
		 }); //  end ajax call
	
	$.ajax({
			   type: "POST",
			   url: "/_templatestacks.html",
			   async: false,
			   cache: false,
			   data: "sid=" + tab,
			   success: function(msg1)
			   {
					$("#prebuilt_flavors").html(msg1);
			   } // end success
		 }); //  end ajax call
}

function getFreeExtras()
{
	centerDiv("#ajax_div");
	var list = '';
	if($("#free0").length > 0)
	{
		list += $("#free0").attr("value");
	}
	if($("#free1").length > 0)
	{
		list += "-" + $("#free1").attr("value");
	}
	if($("#free2").length > 0)
	{
		list += "-" + $("#free2").attr("value");
	}
	if($("#free3").length > 0)
	{
		if($("#free3").val() == 0)
		{
			$("#fadebg").show(); 
			$("#ajax_div").html('<div id="ajaxDivTitle" ><label>My Stack</label><button id="ajaxDivClose" class="form_btn" onClick="closeOptions();">Close</button></div><div id="mystack_message"><h2>Please select your free trial kit under the Free Extras section</h2></div>');
			$("#ajax_div").show();
			return false;
		}
		else
		{
			list += "-"+ $("#free3").val();
		}
	}
	if($("#free4").length > 0)
	{
		if($("#free4").val() == '0')
		{
			$("#fadebg").show(); 
			$("#ajax_div").html('<div id="ajaxDivTitle" ><label>My Stack</label><button id="ajaxDivClose" class="form_btn" onClick="closeOptions();">Close</button></div><div id="mystack_message"><h2>Please select your free items under the Free Extras section</h2></div>');
			$("#ajax_div").show();
			return false;
		}
		else if($("#free4").val() == '-1')
		{
			if($("#free4size").val() == 0)
			{
				$("#fadebg").show(); 
				$("#ajax_div").html('<div id="ajaxDivTitle" ><label>My Stack</label><button id="ajaxDivClose" class="form_btn" onClick="closeOptions();">Close</button></div><div id="mystack_message"><h2>Please select your shirt size under the Free Extras section</h2></div>');
				$("#ajax_div").show();
				return false;
			}
			else
			{
				list += "-"+ $("#free4size").val();
			}
		}
		else if($("#free4").val() == '-2')
		{
			if($("#free4sizehat").val() == 0)
			{
				$("#fadebg").show(); 
				$("#ajax_div").html('<div id="ajaxDivTitle" ><label>My Stack</label><button id="ajaxDivClose" class="form_btn" onClick="closeOptions();">Close</button></div><div id="mystack_message"><h2>Please select your hat size under the Free Extras section</h2></div>');
				$("#ajax_div").show();
				return false;
			}
			else
			{
				list += "-"+ $("#free4sizehat").val();
			}
		}
		else
		{
			list += "-"+ $("#free4").val();
		}
	}
	
	if(list == '')
	{
		return true;
	}
	return list;
}

function postForm()
{
	var list;

	list = getFreeExtras();

	if(list != false)
	{
	
		$("#fadebg").show(); 
		$("#ajax_div").html('<img src="/templates/default/en/img/loading_sm.gif"><br /><br />Loading may take up to 60 second to process.')
		$("#ajax_div").show();
		
			$.ajax({
			   type: "POST",
			   url: "/_postPPC.html",
			   data: "items=" + list,
			   async: false,
			   cache: false,
			   success: function(msg)
			   { 
				  if(msg.search('You') != -1 || msg.search('Under') != -1)
				  {
					var output = '<div id="ajaxDivTitle" ><label>My Stack</label><button id="ajaxDivClose" class="form_btn" onClick="closeOptions();">Close</button></div><div id="mystack_message"><h2>' + msg + '</h2></div>'; 
					$("#ajax_div").html(output);
					centerDiv("#ajax_div");
					$("#ajax_div").show();
				  }
				  else
				  {
					$("#mystackform").html('<input type="hidden" name="ppc_items" id="ppc_items" value="' + escape(msg) + '" />');
					document.getElementById('mystackform').submit();
				  }
			   }
			}); 
	}
}

function checkSize()
{
	if($("#free4").val() == "-1")
	{
		$("#free4sizehat").hide();
		$("#free4size").show();
	}
	else if($("#free4").val() == "-2")
	{
		$("#free4size").hide();
		$("#free4sizehat").show();
	}
	else
	{
		$("#free4sizehat").hide();
		$("#free4size").hide();
	}
}

function showProductInfo(product)
{	
	$("#fadebg").show(); 
	
	$.ajax({
		   type: "POST",
		   url: "/_learnmore.html",
		   data: "product=" + escape(product),
		   async: false,
		   cache: false,
		   success: function(msg)
		   {
			   $("#ajax_div").html(msg);
		   }
		}); 
	centerDiv("#ajax_div");
	$("#ajax_div").show();
}

function addCurrentStack()
{
	var counter = 0;
	var items = '';
	
	$("#fadebg").hide(); 
	$("#ajax_div").hide();
	$(".flavor").each(function() {
		items += $(this).val() + '-';
    });
	
	$.ajax({
		   type: "POST",
		   async: false,
		   cache: false,
		   url: "/_addSession.html",
		   data: "items=" + items,
		   success: function(msg)
		   {
			   window.location = ('signup_1.html')
		   }
	 });	
}

function showDetails()
{	
	$("#fadebg").show(); 
	
	$.ajax({
		   type: "POST",
		   url: "/_getDetails.html",
		   async: false,
		   cache: false,
		   success: function(msg)
		   {
			   $("#ajax_div").html(msg);
		   }
		});
	centerDiv("#ajax_div");
	$("#ajax_div").show();
}

function showOptions()
{	
	$("#fadebg").show();
	centerDiv("#ajax_div");
	$("#ajax_div").show();
}
function closeOptions()
{
	$("#fadebg").hide(); 
	centerDiv("#ajax_div");
	$("#ajax_div").hide();
}

function showDiv(div_id)
{
	
	$(".dropdown_div").each(function() {
		$(this).hide();
		$("#" + div_id).parent().css("margin-left","0px");
		$(this).parent().css("z-index","2");
	});

	$("#" + div_id).parent().css("margin-left","7px");
	$("#" + div_id).parent().css("z-index","100");
	$("#" + div_id).show();
}

function hideDiv(div_id)
{
	$("#" + div_id).hide();
	$("#" + div_id).parent().css("margin-left","0px");
	$("#" + div_id).parent().css("z-index","2");
}

//////////////////////////////////////////////////

function centerDiv(thisDiv)
{
	var availHeight;
	var availWidth;
	
	if(typeof(window.innerWidth) == 'number')
	{
		availHeight = window.innerHeight;
		availWidth = window.innerWidth;
	}
	else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
	{
		availHeight = document.documentElement.clientHeight;
		availWidth = document.documentElement.clientWidth;
	}
	else if(document.body && (document.body.clientWidth || document.body.clientHeight))
	{
		availHeight = document.body.clientHeight;
		availWidth = document.body.clientWidth;
	}

	var offsetX = 0;
	var offsetY = 0;
	//IE
	if(!window.pageYOffset)
	{
		//strict mode
		if(!(document.documentElement.scrollTop == 0))
		{
			offsetY = document.documentElement.scrollTop;
			offsetX = document.documentElement.scrollLeft;
		}
		//quirks mode
		else
		{
			offsetY = document.body.scrollTop;
			offsetX = document.body.scrollLeft;
		}
	}
	//w3c
	else
	{
		offsetX = window.pageXOffset;
		offsetY = window.pageYOffset;
	}

	var divWidth = $("#" + thisDiv).width();
	var divHeight = $("#" + thisDiv).height();
	var divleft = ((availWidth - divWidth)/2) + offsetX;
	var divtop = ((availHeight - divHeight)/2) + offsetY;
	$("#" + thisDiv).css({"position" : "absolute", "top" : divtop + "px", "left" : divleft + "px", "display" : "block"});
}

///////////////////////////////////////

window.onscroll = function()
{
    if( window.XMLHttpRequest ) 
	{
		var availHeight;
		var availWidth;
		
		if(typeof(window.innerWidth) == 'number')
		{
			availHeight = window.innerHeight;
			availWidth = window.innerWidth;
		}
		else if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight))
		{
			availHeight = document.documentElement.clientHeight;
			availWidth = document.documentElement.clientWidth;
		}
		else if(document.body && (document.body.clientWidth || document.body.clientHeight))
		{
			availHeight = document.body.clientHeight;
			availWidth = document.body.clientWidth;
		}
		//alert(availHeight);
        if (document.documentElement.scrollTop > 192 || self.pageYOffset > 192)
		{
			if(document.documentElement.scrollTop > availHeight ||  self.pageYOffset > availHeight)
			{
				$('#mystacknav').css('position','relative');
				$('#mystacknav').css('top', '192px');
			}
			else
			{
				$('#mystacknav').css('position','fixed');
				$('#mystacknav').css('top','0px');
			}
        } 
		else if(document.documentElement.scrollTop < 192 || self.pageYOffset < 192)
		{
            $('#mystacknav').css('position','absolute');
            $('#mystacknav').css('top','192px');
        }

		if (document.documentElement.scrollTop > 250 || self.pageYOffset > 250)
		{
			$('#ajax_div').css('position','fixed');
			$('#ajax_div').css('top','100px');
        }
		else if(document.documentElement.scrollTop < 250 || self.pageYOffset < 250)
		{
            $('#ajax_div').css('position','absolute');
            $('#ajax_div').css('top','250px');
        }
    }
}
