// Global window.open options
var windowOptions = 'directories=no;fullscreen=no,location=no,menubar=no,resizable=yes,status=yes,titlebar=yes,toolbar=no,height=770,width=615';

function newWindow() {
    // test links with rel of external
    var $external = $('a[rel="external"]').newWindow();
    var numTargetBlanks = 0;
    $external.each(function() {
        if (this.target = "_blank") numTargetBlanks++;
    });
}

// Window used on product details page
function popRecipeWindow() {
    $('a.recipe-pop').popup({ width: 420, height: 600 });
}

// Window used on product details page
function popProductWindow() {
    $('a.product-pop').popup({ width: 1010, height: 600 });
}

// Window used on coupons page
function popCouponWindow()
{
	$('.print-now-button').popup({ width: 540, height: 600 });
}

// Triggers drop down menus on hover events
function dropDownMenus()
{
    $('.dropdown-trigger').mouseenter(function()
	{
		$(this).children('.dropdown').show();
	}).mouseleave(function()
	{
		$(this).children('.dropdown').hide();
	})
}

// Global pop-up button
function popUpButton()
{
	$('.print-now-btn, .pop-up-btn').popup({ width: 608, height: 750 });
	$('.pop-up-btn-big-win').popup({ width: 850, height: 600 });
}

// Handles when a user interacts with the rating system
function callProductRatings(type){
	$('.star-product').rating({ 
		focus: function(value, link){ 
				var tip = $('span.rating-link'); 
				tip[0].data = tip[0].data || tip.html(); 
				tip.html(link.title || 'value: '+value); 
				}, 
		blur: function(value, link){ 
				var tip = $('span.rating-link'); 
				$('span.rating-link').html(tip[0].data || ''); 
			},
		// Handles when a user saves a rating
		callback: function(value, link){ 
			var productid = parseInt($('input#product-id').val());
			var rating = '';
			rating = $.toJSON(rating);
			
			// Send the rating off for vlidation
			$.ajax({
			    url: '/products/RateProduct/' + productid + '/' + value,
			    type: 'POST',
			    data: rating,
			    dataType: 'JSON',
			    contentType: "application/json; charset=utf-8",
			    success: handleRatedProduct
			});
		} 
	});
	
	$('span.star-rating-control').hide();
	$('span.rating-link > a').click(function(e){
		e.preventDefault();
		$('div.rating-average').hide();
		$('span.star-rating-control').show();
	});
}

// Handles when a user has submitted a rating
function handleRatedProduct(data)
{
	var returnObj = $.evalJSON(data);
	
	var totalRatings = returnObj.Result['ResultPackets'][0];
	
	if(returnObj.Result['ReturnCode'] == 1)
	{
		$('.star-product').rating('disable');
		$('span.rating-link').hide();
		$('span#thank-you-hidden').show();
		if(totalRatings == 1)
		{
			$('span.rating-title').html(totalRatings + " RATING " + "|");
		}
		else
		{
			$('span.rating-title').html(totalRatings + " RATINGS " + "|");
		}
		
	}
} 

function callRecipeRatings(type){
	$('.star-recipe').rating({ 
		focus: function(value, link){ 
				var tip = $('span.rating-link'); 
				tip[0].data = tip[0].data || tip.html(); 
				tip.html(link.title || 'value: '+value); 
				}, 
		blur: function(value, link){ 
				var tip = $('span.rating-link'); 
				$('span.rating-link').html(tip[0].data || ''); 
			},
		callback: function(value, link){ 
			var recipeid = parseInt($('input#recipe-id').val());
			var rating = '';
			rating = $.toJSON(rating);
			
			$.ajax({
			    url: '/products/RateRecipe/' + recipeid + '/' + value,
			    type: 'POST',
			    data: rating,
			    dataType: 'JSON',
			    contentType: "application/json; charset=utf-8",
			    success: handleRatedRecipe
			});
		} 
	});
	
	$('span.star-rating-control').hide();
	$('span.rating-link > a').click(function(e){
		e.preventDefault();
		$('div.rating-average').hide();
		$('span.star-rating-control').show();
	});
}

function handleRatedRecipe(data)
{
	var returnObj = $.evalJSON(data);
	
	var totalRatings = returnObj.Result['ResultPackets'][0];
	
	if(returnObj.Result['ReturnCode'] == 1)
	{
		$('.star-recipe').rating('disable');
		$('span.rating-link').hide();
		$('span#thank-you-hidden').show();
		if(totalRatings == 1)
		{
			$('span.rating-title').html(totalRatings + " RATING " + "|");
		}
		else
		{
			$('span.rating-title').html(totalRatings + " RATINGS " + "|");
		}
		
	}
} 

$(document).ready(function()
{
	dropDownMenus();
	newWindow();
	popRecipeWindow();
	popProductWindow();
	popCouponWindow();
	callProductRatings();
	callRecipeRatings();
	popUpButton();
});