var i = 0;
var attempts = 18; //60*3/10
var timer;
var shouldbeclosed = true;
jQuery.noConflict();
jQuery(document).ready(function(){
	var newdiv2 = document.createElement("div");
	newdiv2.id = "blackoverlay";
	newdiv2.style.position = "fixed";
	newdiv2.style.width = "0%";
	newdiv2.style.height = "0%";
	newdiv2.style.background = "black";
	newdiv2.style.opacity = "0.0";
	newdiv2.style.filter = "alpha(opacity=0)";
	newdiv2.style.zIndex = "100";
	 
	var newdiv = document.createElement("div");
	newdiv.id = "overlayinfo";
	newdiv.style.position = "fixed";
	newdiv.style.width = "0px";
	newdiv.style.height = "0px";
	newdiv.style.left = "50%";
	newdiv.style.top = "50%";
	newdiv.style.background = "#111111";
	newdiv.style.marginLeft = "0px";
	newdiv.style.color = "white";
	newdiv.style.textAlign = "center";
	newdiv.style.zIndex = "120";
	newdiv.style.opacity = "0.0";
	newdiv.style.filter = "alpha(opacity=0)";
	
	jQuery(document.body).prepend(newdiv);
	jQuery(document.body).prepend(newdiv2);
});

function FastCheckOut(productid)
{
	clearTimeout(timer)
	i = 0;
	shouldbeclosed = false;
	CreateOverlayBox('<div class="fastbuy_form"><div <div class="fastbuy_head">Please Enter Your Mobile Phone Number:</div><input id="fastbuy_mobile" class="fastbuy_mobile_input" name="fastbuy_mobile" type="text"><input class="fastbuy_submit" name="" type="button" value="Fast Buy!" onclick="OnSubmitFastBuy('+productid+')"><a href="#" class="closebutton" onclick="CloseBoxes();">[X]</a></div>', 340, 140, true);
}
function CreateOverlayBox(content, width, height, border)
{	
	jQuery("#blackoverlay").fadeTo(-1, 0.0);
	jQuery("#overlayinfo").fadeTo(-1, 0.0);
	jQuery("#blackoverlay").css({"width" : "100%", "height" : "100%"});
	jQuery("#overlayinfo").css({"width" : width+"px", "height" : height, "margin-left" : width/2*-1+"px"});
	if(border)
		jQuery("#overlayinfo").addClass("colorborder");
	jQuery("#overlayinfo").html(content);
	jQuery("#overlayinfo").css({"margin-top" : jQuery("#overlayinfo").height()/2*-1+"px"});
	jQuery(".linktext").hover(function () {jQuery(this).addClass("linkhovertext");}, function () {jQuery(this).removeClass("linkhovertext");});
	jQuery(".buttoncolor").hover(function () {jQuery(this).addClass("buttoncolorhover");}, function () {jQuery(this).removeClass("buttoncolorhover");});
	jQuery("#blackoverlay").fadeTo(1000, 0.65);
	jQuery("#overlayinfo").fadeTo(1000, 1.0);
}
function OnSubmitFastBuy(productid)
{
	var mobilenumber = jQuery("#fastbuy_mobile").val();
	jQuery("#overlayinfo").html('<div class="fastbuy_form" style="line-height:120px;"><div id="fastbuy-response">Waiting....</div><a href="#" class="closebutton" onclick="CloseBoxes();">[X]</a></div>');
	StartFastBuyAjax(productid, mobilenumber)
}
function StartFastBuyAjax(productid, mobilenumber)
{		
	jQuery.ajax({
	type: "POST",
	url: "/store/fastbuy/"+productid,
	data: "mobile="+mobilenumber,
	success: function(id){FastCheck(id)}
	});
}
function FastCheck(id)
{
	if(shouldbeclosed)
		return;
	if(i == attempts+1)
	{
		SetTimedOut();
		return;
	}
	else
	{
		i++;
	}
	
	jQuery.ajax({
	type: "GET", 
	url: "/store/fastcheck/"+id,
	success: function(html)
	{
		if(html != 0)
		{
			jQuery("#fastbuy-response").html(html);
		}
		else
		{
			timer = setTimeout("FastCheck("+id+")",10000)
		}
	}
	});
}
function SetTimedOut()
{
	shouldbeclosed = true;
	clearTimeout(timer)
	jQuery("#fastbuy-response").html('Session Timed Out Please Try Again');
}
function CloseBoxes()
{
	shouldbeclosed = true;
	clearTimeout(timer)
		
	jQuery("#blackoverlay").fadeTo(500, 0.0, function() {jQuery("#blackoverlay").css({"width" : "0%", "height" : "0%"});});
	jQuery("#overlayinfo").fadeTo(500, 0.0, function() {jQuery("#overlayinfo").css({"width" : "0%", "height" : "0%"});jQuery("#overlayinfo").html("");});
}
