// JavaScript Document

var siteURL                  = "";
var imagePath                = siteURL + "/images";
var ratingClickCallback      = null;
var ie6WindowsResizeEnabled  = false;

$(function() {
	createInputStars();
	
	// Navigation Menu Highlight Function
	$('div.nav-menu-item').mouseover(function()
	{
		var img = $(this).find('img');
		var src = img.attr('src').replace(/\/menu\//, '/menu-hl/');
		img.attr('src', src);
	});
	
	$('div.nav-menu-item').mouseout(function()
	{
		var img = $(this).find('img');
		var src = img.attr('src').replace(/\/menu-hl\//, '/menu/');
		img.attr('src', src);
	});
	
	// Login Box Functions
	$('.toggle-login-box').click(function()
	{
		$('#login-box').toggle();
		return false;
	});
	
	$('#login-box .close-button').click(function()
	{
		$('#login-box').hide();
	});
	
	
	// Search Box Functions
	$('#nav-search-text-input').focus(function()
	{
		if ($(this).val() == 'Search') $(this).val('');
	});
	
	$('#nav-search-text-input').blur(function()
	{
		if ($(this).val() == '') $(this).val('Search');
	});
	
	// Restores recipe_nav_box to last position
	select_cat_box = getCookie('recipe_nav_box');
	if (select_cat_box == 'occasion')
	{
		showRecipesByOccasion(true);
	}
	else if (select_cat_box == 'favorite')
	{
		showMyFavoriteRecipes(true);
	}
	

});

function isIE6()
{
	return jQuery.browser.msie && jQuery.browser.version.substr(0,1) == "6";
}

function blockerIE6Resize()
{
	$('div.blocker').css("width", $(window).width() + $(window).scrollLeft());
	$('div.blocker').css("height",$(window).height() + $(window).scrollTop());	
}

function enableIE6BlockerResize()
{
	if ( ! ie6WindowsResizeEnabled)
	{
		$(window).resize(function(){
			blockerIE6Resize();
		});
	
		$(window).scroll(function(){
			blockerIE6Resize();
		});
		
		ie6WindowsResizeEnabled = true;
	}
	blockerIE6Resize();
}

function showBlocker()
{
	$("div#rap").before("<div class=\"blocker\"></div>");
	
	if (isIE6()) enableIE6BlockerResize();
	
	$('div.blocker').click(function()
	{
		hideBlocker();
	});
}

function hideBlocker()
{
	$('div.blocker').remove();
	hideVideoPlayer();
}

function loadVideo(title, path)
{
	showBlocker();
	showVideoPlayer();
	
	$('#video-overlay-320-240 .title').html(title);
	
	var params   = {};
	params.menu  = "false";
	
	var flashvars = {};
	flashvars.video_url = siteURL + path;
	
	swfobject.embedSWF(siteURL + "/swf/video-player/shell.swf", "video-player-swf", "480", "406", "9.0.0", "", flashvars, params);
	
}

function showVideoPlayer()
{
	$("div#rap").before("<div id=\"video-overlay-320-240\"><div class=\"background png32t\"></div><div class=\"title\"></div><div class=\"close\"><img class=\"png32t\" src=\"" + imagePath + "/common/video-player/close-button.png\" width=\"47\" height=\"11\" alt=\"close\" /></div><div id=\"video-player-swf\"></div></div>");
	$("#video-overlay-320-240 .close").click(function() {hideVideoPlayer();});
	
	$(window).resize(function(){
		positionVideoPlayer();
	});

	$(window).scroll(function(){
		positionVideoPlayer();
	});
	
	positionVideoPlayer();
}

function positionVideoPlayer()
{
	var xpos = $(window).width()  - $("#video-overlay-320-240").width();
	var ypos = $(window).height() - $("#video-overlay-320-240").height();
	
	
	if (xpos < 0) xpos = 0;
	if (ypos < 0) ypos = 0;
	
	
	xpos = xpos / 2 + $(window).scrollLeft();
	ypos = ypos / 2 + $(window).scrollTop();
	
	
	$("#video-overlay-320-240").css("left", xpos);
	$("#video-overlay-320-240").css("top",  ypos);
}

function hideVideoPlayer()
{
	$('div.blocker').remove();
	$("#video-overlay-320-240").remove();
}


function h1imgswap(id, name, src, width, height)
{
	$(id).replaceWith('<img class="png32t" src="' + src + '" width="' + width + '" height="' + height + '" alt="' + name + '" />');
}


// Regular Expressions
var regexEmail      = /^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/i;
var regexProperName = /^([A-Za-z]| |-|\'|,|\.){1,30}$/i;
var regexZipCode    = /^([a-z0-9]| |-){5,10}$/i;

function createSlug(str)
{
	var result = str.toLowerCase();
	result     = result.replace(/ |_|\.|,|\/|&/g, '-');
	result     = result.replace(/[^-a-z0-9]/g, '');
	result     = result.replace(/-+/g, '-');
	result     = result.replace(/^-|-$/g, '');
	return result;
}

function number_alpha_numeric_chars(str)
{
	var result = str.replace(/[^a-z0-9]/ig, '');
	return result.length;
}

/* Validation Functions */

function validProperName(name)
{
	return regexProperName.test(name);
}

function validZipCode(zipcode)
{
	return regexZipCode.test(zipcode);
}

function validEmail(email)
{
	return regexEmail.test(email);
}

function validEmails(emails)
{
	var email_list   = emails.split(',');
	var num_emails   = email_list.length;
	var emails_found = 0;
	
	for (var i = 0; i < num_emails; i++)
	{
		email = $.trim(email_list[i]);
		if (email.length == 0) continue;
		
		if ( ! validEmail(email) ) return false;
		emails_found++;
	}
	return emails_found > 0;
}

function cleanEmailsList(emails)
{
	var email_list = emails.replace(/\n|\s/g, '');
	email_list     = email_list.replace(/,+/g, ', ');
	email_list     = email_list.replace(/^,\s|,\s$/g, '');
	return email_list;
}

function formatErrorMessage(errors)
{
	var msg = 'Please fix the following errors: <ul>';
	for (i in errors)
	{
		msg += '<li>' + errors[i] + '</li>';
	}
	return msg + '</ul>';
}


/* Login box functions */

function getURL()
{
	var url = window.location.toString();
	return url;
}

function setCookie(name, value)
{
	var expireDate = new Date();
	expireDate.setDate(expireDate.getDate() + 365);
	
	document.cookie = name + "=" + escape(value) + ";expires=" + expireDate.toGMTString() + "; path=/";
}

function getCookie(name)
{
	var index = document.cookie.indexOf(name + "=");
	if (index < 0) return "";
	
	
	var start = index + name.length + 1;
	var end   = document.cookie.indexOf(";", start);
	if (end < 0) end = document.cookie.length;
	
	return unescape(document.cookie.substring(start, end));
}

/* Recipe Category sidebar functions */

function showRecipesByType(instant)
{
	var animateTime = 500;
	if (instant) animateTime = 0;
	
	setActiveRecipeCat("recipe-cat-recipes-by-type", animateTime);
	if ( ! instant) setCookie('recipe_nav_box', 'type');
	
	$("#recipe-cat-recipes-by-occasion").animate({marginTop: 153}, animateTime);
	$("#recipe-cat-my-fav-recipes").animate({marginTop: 183}, animateTime);
}

function showRecipesByOccasion(instant)
{
	var animateTime = 500;
	if (instant) animateTime = 0;
	
	setActiveRecipeCat("recipe-cat-recipes-by-occasion", animateTime);
	if ( ! instant) setCookie('recipe_nav_box', 'occasion');
	
	$("#recipe-cat-recipes-by-occasion").animate({marginTop: 30}, animateTime);
	$("#recipe-cat-my-fav-recipes").animate({marginTop: 183}, animateTime);
}

function showMyFavoriteRecipes(instant)
{
	var animateTime = 500;
	if (instant) animateTime = 0;
	
	setActiveRecipeCat("recipe-cat-my-fav-recipes", animateTime);
	if ( ! instant) setCookie('recipe_nav_box', 'favorite');
	
	$("#recipe-cat-recipes-by-occasion").animate({marginTop: 30}, animateTime);
	$("#recipe-cat-my-fav-recipes").animate({marginTop: 60}, animateTime);
}

function setActiveRecipeCat(id, animateTime)
{
	var recipeCat;
	var recipeCats = new Array("recipe-cat-recipes-by-type", "recipe-cat-recipes-by-occasion", "recipe-cat-my-fav-recipes");
	
	var slideDownTime = animateTime + 50;
	var slideUpTime   = animateTime - 50;
	
	if (animateTime < 50)
	{
		slideDownTime = 0;
		slideUpTime   = 0;
	}
	
	for (var i in recipeCats)
	{
		recipeCat = "#" + recipeCats[i];
		if (recipeCats[i] == id)
		{
			$(recipeCat).find(".recipe-cat-button").attr("src", imagePath + "/recipe-cat-box/button-collapse.png");
			$(recipeCat).children(".recipe-cat-content").slideDown(slideDownTime);
		}
		else
		{
			$(recipeCat).find(".recipe-cat-button").attr("src", imagePath + "/recipe-cat-box/button-expand.png");
			$(recipeCat).children(".recipe-cat-content").slideUp(slideUpTime);
		}
	}
}

/* User Review Star Ratings */

function createInputStars(callback)
{
	ratingClickCallback = callback;
	
	$("span.star").addClass('star-default').click(starClick);
	
	$("span.star input").each(function() {
		if ($(this).attr('checked'))
		{
			var name      = $(this).attr('name');
			var curValue  = this.id.split("-")[1];
			
			starsRefresh(name, curValue);
		}
	});
}

function starClick()
{
	var inputElem = $(this).find('input').get(0);
	var name      = $(inputElem).attr('name');
	var newValue  = parseInt(inputElem.id.split("-")[1]);
	
	if (newValue < 1 || newValue > 5) return;
	
	$(inputElem).attr('checked', true);
	starsRefresh(name, newValue);
	
	if (ratingClickCallback)
	{
		ratingClickCallback(newValue);
	}
}

function starsRefresh(name, newValue)
{	
	$("input:radio[name='" + name + "']").each(function() {
		var val = parseInt(this.id.split("-")[1]);
		if (val <= newValue)
		{
			$(this).parents("span.star").addClass("star-selected");
		}
		else
		{
			$(this).parents("span.star").removeClass("star-selected");
		}
	});
}
