
// 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() {
    
    
    //li.dropdown-trigger:hover div.dropdown { left: -6px; }
    $('.dropdown-trigger').mouseenter(function() {
         $(this).children('.dropdown').css('left', '-6px');
        var classStr = $(this).children('a').attr('class').split(" ")[0];
        $(this).children('a').addClass(classStr + "-hover");
    }).mouseleave(function() {
        $(this).children('.dropdown').css('left', '-1000em');
        var classStr = $(this).children('a').attr('class').split(" ")[0];
        $(this).children('a').removeClass(classStr + "-hover");
        //$(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 + '?csrf=' + CSRF,
			    type: 'POST',
			    data: rating,
			    dataType: 'json',
			    contentType: "application/json; charset=utf-8",
			    success: function(data, textStatus, jqXHR) { handleRatedProduct (data) }
			});
		} 
	});
	
	$('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 totalRatings = data.Result['ResultPackets'][0];
	
	if(data.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 + '?csrf=' + CSRF,
			    type: 'POST',
			    data: rating,
			    dataType: 'json',
			    contentType: "application/json; charset=utf-8",
			    success: function(data, textStatus, jqXHR) { handleRatedRecipe (data) }
			});
		} 
	});
	
	$('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 totalRatings = data.Result['ResultPackets'][0];
	
	if(data.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 " + "|");
		}
		
	}
}

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');
    });
    */
  }
  function mainNavIcons() {
      $(".sitenav ul.sublist li").each(function(i) {
          if ($(this).hasClass('fiber')) {
              $(this).children("a").append("&nbsp;<img  src='/_res/i/navigation/special-k-fiber-small.png' />");
          }
          if ($(this).hasClass('new')) {
              $(this).children("a").append("&nbsp;<img style='margin-right:-10px' src='/_res/i/navigation/special-k-new-product-small.png' />");
          }

      })
  }


$(document).ready(function() {
    dropDownMenus();
    newWindow();
    popRecipeWindow();
    popProductWindow();
    popCouponWindow();
    callProductRatings();
    callRecipeRatings();
    popUpButton();
    searchBox();
    mainNavIcons();
    $("div.search-result:odd").addClass('odd');
});
