// 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: '/content/specialk/links/recipeRating.update',
		    type: 'GET',
		    data: value,
		    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)
{
	data = data.trim();
	//alert(data);
	var returnObj = $.evalJSON(data);	
	var totalRatings = returnObj.ResultCount;
	var ratingText = returnObj.Rating;
	var ratingsText = returnObj.Ratings;
	if(returnObj.ReturnCode == 1)
	{
		$('.star-recipe').rating('disable');
		$('span.rating-link').hide();
		$('span#thank-you-hidden').show();
		if(totalRatings == 1)
		{
			$('span.rating-title').html(totalRatings + " " + ratingText + " |");
		}
		else
		{
			$('span.rating-title').html(totalRatings + " " + ratingsText + " |");
		}
		setRatingCookie(returnObj.PageTitle, returnObj.ResultRating);
		//$('div.current-average').style("width","10%");
	}
}
function setRatingCookie(name, value) {
	var exdate=new Date();
	var cookieName = "ratingCookie"+name;
	exdate.setDate(exdate.getDate()+5);
	document.cookie = cookieName+"="+value+"; expires="+exdate.toUTCString()+"; path=/content";

}

function searchBox() {
    $('.site-search').mouseenter(function() {
        $(this).children('#cse').addClass('active');
    }).mouseleave(function() {
    $(this).children('#cse').removeClass('active');
    })

    $(".gs-result a").live("click",function(e){
      e.preventDefault();
      window.location = this.href;
      });

      $('#q').bind('focus', function() {
          $(this).val('');
      });

      $('#magnify').bind('click', function() {
          $('#cse-search-box').submit();
          return false;
      });
    /* google js is overwritting this.
    $('input.gsc-input').focus(function() {
        alert("focus");
        $('div#cse').addClass('active');
    });
    */
}


$(document).ready(function() {
    //dropDownMenus();
    newWindow();
    popRecipeWindow();
    popProductWindow();
    popCouponWindow();
    callProductRatings();
    callRecipeRatings();
    popUpButton();
    searchBox();
	$("div.search-result:odd").addClass('odd');
});
