var homepage = {};

$(function(){
	$(".global_popup .closebutton a").click(function(){
		$(".global_popup").fadeOut();
	});
});


homepage.init = function(){
	$(".home-div .inner > a").bind("mouseenter focus",function(jEvent){
		$(this).next().show();
	});
	$(".home-div .inner").bind("mouseleave",function(jEvent){
		$(this).find(".itext").fadeOut();
	});
	
	TT1 = setTimeout(chooseRandomBox,randomInt(2,8)*1000);
};

function chooseRandomBox(){

	var randBox = $(".home-div .pbox:not(.center)"); // all product boxes
	var index = randomInt(0,randBox.length-1);
	randBox = $(randBox[index]);
	
	swapRandomHomeBox(randBox);
	
	TT1 = setTimeout(chooseRandomBox,randomInt(2,8)*1000);
	
}

function randomInt(min,max){
	return min + Math.floor( Math.random() * (1+max-min) );
}

function swapRandomHomeBox(box){
	var dataStr = 'action=getNewProduct&';
	
	box = $(box);
	
	dataStr += 'swap='+box.attr('alt')+'&';
	
	dataStr += $("#products_form").serialize();
	
	jQuery.ajax({
		data:dataStr,
		type: 'GET',
		url: "/ajax.products.php",
		success:function(obj){
			if(obj.error){
				alert(obj.error);
			} else {
				// console.dir(obj);
				var old_id = box.attr("alt");
				$("#products_form input[value="+old_id+"]").val(obj.id);
				
				box.fadeOut(500,function(){
					box.attr("alt",obj.id).css("background-color",obj.color);
					box.find("a[href]").attr("href",obj.url);
					box.find("img").attr("src",obj.image);
					box.find(".itext h2 a").text(obj.name);
					box.find(".itext p span").text(obj.description);
					box.fadeIn(500);
				});
				
				
			}
		}
	});
	
}

function removeCartItem(obj){
	
	if(!confirm("Really remove this item from your cart?")){
		return false;
	}
	$(obj).parent().parent().find(".qty input").val('0');
	$("#cart_form").submit();
	
	return false;
	
}

function headerEmailSignup(){

	if(jQuery){
		jQuery.ajax({
			data:$("#signup_form").serialize(),
			dataType:'json',
			type: 'GET',
			url: "/ajax.newsletter.php",
			success:function(obj){
				if(obj.error){
					alert(obj.error);
				} else if(obj.message){
					fancyAlert(obj.message);
					// $("#signup_form").fadeOut();
				}
			}
		});
	}

	return false;
}

function promptNewsletterSignup(){
	if(jQuery){
		jQuery.ajax({
			data: 'prompt=1',
			type: 'GET',
			url: "/ajax.newsletter.php",
			success:function(htm){
				fancyAlert(htm);
			}
		});
	}

	return false;
}

function fancyAlert(msg){
	$(".global_popup .inner").html(msg);
	centerDiv(".global_popup");

}

/* Center an absolute Div vertically within the view port.
Should be horizontally centered by way of CSS and margin:0 auto; */
function centerDiv(oDiv,desired_top_margin){

	var element = $(oDiv);
	if(!element) return false;

	var dims = {};
	dims.width = element.width();
	dims.height = element.height();

	var top_offset = (window.pageYOffset) ? window.pageYOffset :  document.body.scrollTop;

	if(document.documentElement.scrollTop){
		// Supported in IE7 and Firefox
		top_offset = document.documentElement.scrollTop;
	}

	var w = 0;	var h = 0;

	if( typeof( window.innerWidth ) == 'number' ) {
		//Non-IE
		w = window.innerWidth;
		h = window.innerHeight;
	} else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
		//IE 6+ in 'standards compliant mode'
		w = document.documentElement.clientWidth;
		h = document.documentElement.clientHeight;
	} else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
		//IE 4 compatible
		w = document.body.clientWidth;
		h = document.body.clientHeight;
	}

	x_pos = Math.round((w * 0.5 - dims.width / 2 ));
	y_pos = top_offset + Math.round( h * 0.5 - dims.height / 2 ) - 40;
	if(desired_top_margin){
		y_pos = 0 + top_offset + desired_top_margin;
	}

	element.css({top:y_pos, left:x_pos});
	element.fadeIn();

	return false;
}

