var popupStatus = 0;
function loadPopup(){
	if(popupStatus==0){
		$("#pop_bg").css({
			"opacity": "0.7"
		});
		$("#pop_bg").fadeIn("slow");
		$("#pop").fadeIn("slow", function(){
			popupStatus = 1;
		});
		
	}
}
function disablePopup(){
	if(popupStatus==1){
		$("#pop_bg").fadeOut("slow");
		$("#pop").fadeOut("slow", function(){
			popupStatus = 0;
			$("#pop").html("");
		});
	}
}
function LoadProduct(id){
	var pop_data = DoAjax('product.php?product_id='+id);
	GetId('pop').innerHTML = pop_data;
	
	
}
function GetPos(id){
	var i = GetId(id);
	var iReturnValue = 0;
	while(i != null ) {
		iReturnValue += i.offsetTop;
		i = i.offsetParent;
	}
	return iReturnValue;
}
	
	function PostAdd(p_id){
		var c = DoAjax('product.php?action=success&product_id='+p_id);	
		GetId('pop').innerHTML = c;

		$(document).ready(function(){
			$("#pop").animate({width: "600px", top: "+=120px", left: "+=150px",  height: "240px"}, 1500 );
		});
		
		
		
	}


$(document).ready(function(){
	$(".product").click(function(){
		var cur_string = $(this).attr("id");
		var product_id = cur_string.replace('p_','');
		var i = GetPos(cur_string);
		
		$("#pop").css({
			"width": "900px",
			"height": "550px"
		});
		centerPopup(i);
		loadPopup();
		var r = DoAjax('product.php?product_id='+product_id);
		GetId('pop').innerHTML = r;
	});
	$(".product_new").click(function(){
		var cur_string = $(this).attr("id");
		var product_id = cur_string.replace('p_','');
		var i = GetPos(cur_string);
		
		$("#pop").css({
			"width": "900px",
			"height": "550px"
		});
		centerPopup(i);
		loadPopup();
		var r = DoAjax('new.product.php?product_id='+product_id);
		GetId('pop').innerHTML = r;
		
	});
	$(".product_test").click(function(){
		var cur_string = $(this).attr("id");
		var product_id = cur_string.replace('p_','');
		var i = GetPos(cur_string);
		
		$("#pop").css({
			"width": "900px",
			"height": "550px"
		});
		centerPopup(i);
		loadPopup();
		var r = DoAjax('test.product.php?product_id='+product_id);
		GetId('pop').innerHTML = r;
	});
	$(".pop_login").click(function(){
		//centering with css
		//alert('hohohoho');
		$("#pop").css({
			"width": "520px",
			"height": "260px"
		});
		centerPopup(200);
		var login = DoAjax('login.php');
		//alert(cart);
		GetId('pop').innerHTML = '<div id="pop_content">'+login+'</div><a href="javascript:disablePopup()" title="Close" id="close"><img src="/gfx/i/close.png" width="20" height="20" alt="Close" title="Close" /></a>';
		//load popup
		loadPopup();
		ShowLogin();
	});	
	$(".pop_contact").click(function(){
		$("#pop").css({
			"width":"600px",
			"height":"700px"	
		});
		centerPopup(100);
		//load popup
		loadPopup();
		ShowContact();
	});
	$('.thumb').live('click', function() {
	
		var thumb_src = $(this).attr("src");
		var thumb_id = $(this).attr("id");
		var big_src  = thumb_src.replace('_80.jpg','.jpg');
		/*
		$("#big").fadeOut(500, function () {
			GetId('big').src = big_src;
			$("#big").fadeIn('slow');
		*/
		$('#photo').html('');
		$('#photo').iviewer({
			src: big_src,
			zoom_max : 100,
			zoom_min : 50,
			initCallback: function(){
				iviewer = this;
			}
		});
	});
	$(".panah_atas").live('click', function() {
		var ct = parseInt($("#curtop").val());
		var mt = parseInt($("#maxth").val());
		var pt = parseInt($("#postop").val());
		if (ct==0) {
			$("#thumbs").animate({ "top":"120px" });
			$(".thumb_cont").animate({ "top":"0px" });
			$("#curtop").val("3");
			$("#postop").val(0);
		} else if (ct<mt) {
			pt = pt - 95;
			ct = ct + 1;
			$(".thumb_cont").animate({ "top":pt +"px" });
			$("#postop").val(""+ pt +"");
			$("#curtop").val(ct);
		} else if (ct==mt) {
			$(".thumb_cont").animate({ "top":"0px" });
			$("#curtop").val("3");
			$("#postop").val(0);
		}
	});
	$(".panah_bawah").live('click', function() {
		var ct = parseInt($("#curtop").val());
		var mt = parseInt($("#maxth").val());
		var pt = parseInt($("#postop").val());
		if (ct<=3) {
			pt = (mt-3) * 95;
			$(".thumb_cont").animate({ "top":"-"+ pt +"px" });
			$("#curtop").val(mt);
			$("#postop").val(pt*-1);
		} else {
			pt = pt + 95;
			ct = ct - 1;
			$(".thumb_cont").animate({ "top":pt +"px" });
			$("#postop").val(""+ pt +"");
			$("#curtop").val(ct);
		}
		//$("#thumbs").animate({ "top":"450px" });
	});
	$(".link_hide").live('click', function(){
		$("#thumbs").animate({ "top":"450px" }, function(){
			$("#postop").val("0");
			$("#curtop").val("0");
		});
	});
		
				
	//CLOSING POPUP
	//Click the x event!
	
	//Click out event!
	$("#pop_bg").click(function(){
		disablePopup();
	});
	$("#close").click(function(){
		disablePopup();
	});	
	//Press Escape event!
	$(document).keypress(function(e){
		if(e.keyCode==27 && popupStatus==1){
			disablePopup();
		}
	});

});

function ShowContact(){
	var contact = DoAjax('contact.php');
	//alert(contact);
	GetId('contact').innerHTML = contact;
	
	
}

function ShowLogin(){
	var login = DoAjax('login.php');
	//alert(cart);
	GetId('pop_content').innerHTML = login;
	
	
}

function centerPopup(top){
	//request data for centering
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight = $("#pop").height();
	var popupWidth = $("#pop").width();
	
	//centering
	if($.browser.safari){ bodyelem = $("body"); } else{ bodyelem = $("html,body"); } 
	var vscroll = bodyelem.scrollTop()+50;
	//alert(vscroll);
	var l = windowWidth/2-popupWidth/2-10;
	//alert(vscroll+' ==== '+l);
	$("#pop").css({
		"position": "absolute",
		"top": vscroll+'px',
		"left": l+'px'
	});
	//only need force for IE6
	$("#pop_bg").css({
		"height": windowHeight+'px'
	});
	
}

function clickedWithinProduct(p_product_id) {
	if(popupStatus==1){
		$("#pop_bg").fadeOut("slow");
		$("#pop").fadeOut("slow", function(){
			popupStatus = 0;
			$("#pop").html("");
			var cur_string = "p_"+ p_product_id;
			var product_id = cur_string.replace('p_','');
			var i = GetPos(cur_string);
			
			$("#pop").css({
				"width": "900px",
				"height": "550px"
			});
			//centerPopup(i);
				var windowWidth = document.documentElement.clientWidth;
				var windowHeight = document.documentElement.clientHeight;
				var popupHeight = $("#pop").height();
				var popupWidth = $("#pop").width();
				
				//centering
				if($.browser.safari){ bodyelem = $("body"); } else{ bodyelem = $("html,body"); } 
				var vscroll = bodyelem.scrollTop()+50;
				//alert(vscroll);
				var l = windowWidth/2-popupWidth/2-10;
				//alert(vscroll+' ==== '+l);
				$("#pop").css({
					"position": "absolute",
					"top": vscroll+'px',
					"left": l+'px'
				});
				//only need force for IE6
				$("#pop_bg").css({
					"height": windowHeight+'px'
				});
			//end centerPopup(i);
			//loadPopup();
				if(popupStatus==0){
					$("#pop_bg").css({
						"opacity": "0.7"
					});
					$("#pop_bg").fadeIn("slow");
					$("#pop").fadeIn("slow", function(){
						popupStatus = 1;
						var r = DoAjax('new.product.php?product_id='+product_id);
						GetId('pop').innerHTML = r;
					});
					
				}
			//end loadPopup();
		});
	}
	
	
}
