var cur_rollover = '';
var cur_e = '';
var cur_t = '';


// javascript functions used throughout the site
$(document).ready(function()
{
	$.ifixpng('/images/pixel.gif');
	
	$('.png').ifixpng();
	
	$('.bg_box').ifixpng();

	$('img[@src$=.png]').ifixpng(); // apply IE6 transparent PNG fix
	
	// product (pop up) in product range listing
	if($("#shelf_wrap").html() != undefined){
		shelf_apply_hover();
	}
	
	// product (pop up) in product-detail page & recipe-detail page
	if($("#related_products").html() != undefined){
		sliding_bar_apply_hover();
	}
	
	// pop up - tv debut
	if($(".tv_cbox").html() != undefined){
		$('.tv_cbox').cBox({
			width:405, 
			height:377, 
			callback:function(){
				if($.browser.msie && $.browser.version < 7)
				{
					$('.png').ifixpng();
				}
				
				swfobject.embedSWF("/flv/FLVPlayer_Progressive.swf", "flv", "350", "241", "9.0.0", "", {skinName:"/flv/Clear_Skin_1", streamName:"/flv/tv_debut", autoPlay: true, autoRewind: false}, {wmode:"opaque"}, {});
			}
		});
	}
	
	// pop up - email friend
	if($(".cbox").html() != undefined){
		$('.cbox').cBox({
			width:450, 
			height:450, 
			callback:function(){
				if($.browser.msie && $.browser.version < 7)
				{
					$('.png').ifixpng();
				}
			}
		});
	}
	
	// pop up - nutrition
	if($(".nutrition_cbox").html() != undefined){
		$('.nutrition_cbox').cBox({
			width:669, 
			height:551, 
			callback:function(){
				if($.browser.msie && $.browser.version < 7)
				{
					$('.png').ifixpng();
				}
			}
		});
	}
});

// jCarouselLite - sliding bar
$(function() {
	
	if($(".prod_jCarouselLite").html() != undefined){
		$(".prod_jCarouselLite").jCarouselLite({
			btnNext: ".p_next",
			btnPrev: ".p_prev",
			start: 0,
			visible: 6,
			scroll: 3,
			speed: 1000,
			circular: false
		});
	}
	
	if($(".recipe_jCarouselLite").html() != undefined){
		$(".recipe_jCarouselLite").jCarouselLite({
			btnNext: ".r_next",
			btnPrev: ".r_prev",
			start: 0,
			visible: 2,
			scroll: 2,
			speed: 1000,
			circular: false
		});
	}
});

// pre-submit callback 
function showRequest(formData, jqForm, options) 
{ 
	$('.status').html('');
	
    return true; 
} 

// post-submit callback 
function showResponse(responseJSON)  
{ 
	var response = eval(responseJSON); 
	
	if (response.status == 1)
	{
		for (var i = 0; i < response.message.length; i++)
		{
			$('#' + response.message[i].id).siblings('span').html( response.message[i].msg ); // print error message
		}
	}
	else
	{
		$('.processing').attr('class','status successful').html(response.message);
		post_submit(response);
	}
}

function slideTopSection() {
	
	if($("#top_section").css("top") == '0px') {
		$("#top_section").animate({"top":"-18px"}, '2000', function(){});
	} else {
		$("#top_section").animate({"top":"0px"}, '2000', function(){});
	}
}

function clearField(id) {
	$('#'+id).val('');
}

/*$(window).bind('load', function() {
	$('.bigImage').css('left', '0px');
});*/


/********************* pop up functions *********************/
function shelf_apply_hover(){
	$("#shelf_wrap a.popup_require").bind("mouseover", function(e){
		cur_rollover = $(this).children('p').html();
		cur_e = e;
		
		cur_t = setTimeout('show_ajax_div($("#product_popup"), cur_rollover, cur_e)', 500);
	});
	$("#shelf_wrap a.popup_require").mousemove(function(e){
		if($("#product_popup").html() != '') {
			move_ajax_div($("#product_popup"), e);
		}
	});
	$("#shelf_wrap a.popup_require").mouseout(function(e){
		clearTimeout(cur_t);
		hide_div($("#product_popup"));
	});
}

function sliding_bar_apply_hover(){
	$("#related_products ul li.popup_require").bind("mouseover", function(e){
		cur_rollover = $(this).children('p').html();
		cur_e = e;
		
		cur_t = setTimeout('show_ajax_div($("#product_popup"), cur_rollover, cur_e)', 500);
	});
	$("#related_products ul li.popup_require").mousemove(function(e){
		if($("#product_popup").html() != '') {
			move_ajax_div($("#product_popup"), e);
		}
	});
	$("#related_products ul li.popup_require").mouseout(function(e){
		clearTimeout(cur_t);
		hide_div($("#product_popup"));
	});
}

function show_ajax_div(popup, link, pos){
	popup.html('<img src="/images/loading.gif" width="16" height="16" alt="loading..." />');
	
	$.ajax({
		type:'POST',
		url: link,
		data:'',
		success:function(html){
			popup.html(html);
		}
	});
	
	
	move_ajax_div(popup, pos)
}

function move_ajax_div(popup, pos){
	var w = $(window).width();
	var h = $(window).height();
	
	if(pos.pageX >= w/2) {
		popup.css('left', pos.pageX-460+'px');
	} else {
		popup.css('left', pos.pageX+10+'px');
	}
	
	if(pos.screenY >= h/2) {
		popup.css('top', pos.pageY-250+'px');
	} else {
		popup.css('top', pos.pageY+30+'px');
	}
}

function hide_div(popup){
	popup.html('');
	popup.css('left', '-1000px');
}
