	$().ready(function()
	{
		$.urlParam = function(name)
		{
			var results = new RegExp('[\\?&]' + name + '=([^&#]*)').exec(window.location.href);
			if (!results) { return 0; }
			return results[1] || 0;
		}
		var productid=$.urlParam('productid');
		if(productid!=0)
		{
			showProdLightBox(productid);
		}
        
	});
	
	function hideProdLightBox()
	{
		showingProdLightBox = 0;
		prodLightBoxURL = '';
		var doFade=0;
		$('#prodItem-lightbox').remove();
		if (doFade == 1)
		{ 
			$('#prodItem-overlay').fadeOut(function() { $('#prodItem-overlay').remove(); });
		} else {
			$('#prodItem-overlay').remove();
		}
	}
	function showProdLightBox(wId)
	{
		var arrPageSizes = ___getPageSize();
		var arrPageScroll = ___getPageScroll();
		showingProdLightBox = wId;
		$('body').append('<div id="prodItem-overlay"></div><div id="prodItem-itemNav"></div><div id="prodItem-lightbox"><div id="prodItem-lightbox-back"></div><div class="lightbox-secNav"><a href="#_" class="lightbox-secNav-btnClose"></a></div><div class="lightbox-container-image-box"><a href="#_" class="prevBtn"></a><a href="#_" class="nextBtn"></a><div class="lightbox-container-image"><div class="lightbox-content"></div><div class="lightbox-loading"><a href="#" class="lightbox-loading-link"></a></div></div></div></div>');
		var script   = document.createElement("script"); 
		script.type  = "text/javascript"; 
		script.text  = "$('.prevBtn').click(function(){fetch_nextproduct($('#previd').val());return false;});$('.nextBtn').click(function(){fetch_nextproduct($('#nextid').val());return false;});";
		document.body.appendChild(script); 
		$('#prodItem-overlay').css
		(
			{
				backgroundColor:	"#000",
				opacity:0.8,
				width:arrPageSizes[0],
				height:arrPageSizes[1]
			}
		).fadeIn();
		$('#prodItem-lightbox').css({
				top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
				left:	arrPageScroll[0]
		}).show();
		$('#prodItem-overlay, #prodItem-lightbox-back').click(function() {
				hideProdLightBox();
				return false;
		});
		$('#prodItem-lightbox .lightbox-loading-link, #prodItem-lightbox .lightbox-secNav-btnClose').click(function() {
				hideProdLightBox();
				return false;
		});
		setupWindowResize();
		fetch_product(wId);
		return false;
	}
	function fetch_nextproduct(wId)
	{
		$(".lightbox-content").html('');
		$(".lightbox-loading").show();
		fetch_product(wId);
	}
	function fetch_product(wId) {
	//first fetch the value of the textbox for block title
		$.ajax
		(
			{
				type: "POST",
				url: "fetchproduct.php?productid="+wId,
				data: "",
				success: function(retData)
				{
					var ret_arr=retData.split("_~_");
					$(".lightbox-content").html(ret_arr[0]);
					$("#previd").val(ret_arr[1]);
					$("#nextid").val(ret_arr[2]);
					if(ret_arr[1]=='')
					{
						$(".prevBtn").css({visibility:'hidden'});
					}
					else
					{
						$(".prevBtn").css({visibility:'visible'});
					}
					if(ret_arr[2]=='')
					{
						$(".nextBtn").css({visibility:'hidden'});
					}
					else
					{
						$(".nextBtn").css({visibility:'visible'});
					}
					show_rating(wId);
					$('#prodItem-overlay').fadeIn();
					$(".lightbox-loading").hide();
				},
				failure:function()
				{
					//hideWaitMessage();
				}
			}
		);
	}	
	
	function setupWindowResize(){
		$(window).resize(function() {
				// Get page sizes
				var arrPageSizes = ___getPageSize();
				// Style overlay and show it
				$('#prodItem-overlay').css({
					width:		arrPageSizes[0],
					height:		arrPageSizes[1]
				});
				// Get page scroll
				var arrPageScroll = ___getPageScroll();
				// Calculate top and left offset for the jquery-lightbox div object and show it
				$('#prodItem-lightbox').css({
					top:	arrPageScroll[1] + (arrPageSizes[3] / 10),
					left:	arrPageScroll[0]
				});
			});
		
		var arrPageScroll = ___getPageScroll();
	}
	function show_rating(productid)
	{
		$("#rat").children().not(":radio").hide();
		// Create stars
		$("#rat").stars({
			//starWidth: 28, // only needed in "split" mode
			cancelShow: false,
			callback: function(ui, type, value)
			{
				// Hide Stars while AJAX connection is active
				$("#rat").hide();
				$("#loader").show();
				
				
				// Send request to the server using POST method
				/* NOTE: 
					The same PHP script is used for the FORM submission when Javascript is not available.
					The only difference in script execution is the returned value. 
					For AJAX call we expect an JSON object to be returned. 
					The JSON object contains additional data we can use to update other elements on the page.
					To distinguish the AJAX request in PHP script, check if the $_SERVER['HTTP_X_REQUESTED_WITH'] header variable is set.
					(see: demo6.php)
				*/ 
				$.ajax
				(
					{
						type: "POST",
						url: "saverating.php",
						data: "productid="+productid+"&rating="+value,
						success: function(retData)
						{
							//alert(retData);
							// Select stars to match "Average" value
							ret_arr=retData.split("_~_");
							$("#divmainrating").html(ret_arr[1]);
							// Update other text controls...
							$("#avg").text('Thank you for rating');
							
							$("#rat").show();
							$("#loader").hide();
						},
						failure:function()
						{
							$("#loader").hide();
						}
					}
				)
			}
		});
		$(".ui-stars-star").css("float","left");
		$(".ui-stars-cancel").css("float","left");
	}
	function addcomment_onclick()
	{
		document.getElementById("divaddcomment").style.display="none";
		document.getElementById("divcomment_box").style.visibility="visible";
		return false;
	}
	function cancelcomment_onclick()
	{
		document.getElementById("divaddcomment").style.display="block";
		document.getElementById("divcomment_box").style.visibility="hidden";
		return false;
	}
	function submitcomment_onclick(productid)
	{
		$("#loader").show();
		$("#rat").hide();
		var ui = $("#rat").data("stars");
		var currValue = ui.options.value;
		$.ajax
		(
			{
				type: "POST",
				url: "saverating.php?mode=comment",
				data: "productid="+productid+"&comment="+$("#txtcomment").val()+"&rating="+currValue,
				success: function(retData)
				{
					ret_arr=retData.split("_~_");
					if(ret_arr[0]=="false")
					{
						$("#avg").text(ret_arr[1]);
					}
					else
					{
						$("#avg").text('Thank you for your feedback');
						$("#divcommentslist").html(ret_arr[1]);
					}
					document.getElementById("divaddcomment").style.display="block";
					document.getElementById("divcomment_box").style.visibility="hidden";
					$("#rat").show();
					$("#loader").hide();
				},
				failure:function()
				{
					$("#loader").hide();
				}
			}
		)
		return false;
	}
	
function fbs_click(u,t)
{
	window.open('http://www.facebook.com/sharer.php?u='+encodeURIComponent(u)+'&amp;t='+encodeURIComponent(t),'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}
	
