/**
 * common.js
 *	-Common Javascript functions
 **/
  //Most functions require jQuery
  
  //Create a cookie to tell us user has JS
		var tday = new Date();
		
		tday.setTime(tday.getTime() + 1800000); //expires in a 1/2 hour
		//Set the cookie to expire in a minute
		//That's enough time for HPP to use it, but if user disables js it'll disappear
		document.cookie="FRENCH_js=1;path=/;expires="+tday.toGMTString();
  
	$(document).ready(function(){
		if($('#recipesearch_submit_button').length>0){
			$('#recipesearch_submit_submit').css('display','none');
			$('#recipesearch_submit_button').css('display','');
		}
		
		//display print recipe link
		
		$('#search-toggle').click(function(e){
			e.preventDefault();
			$('#recipe-search-box-condensed').hide();
			$('#recipe-search-box').show();
		});
		
		$('.close-search').click(function(){
			$('#recipe-search-box').hide();
			$('#recipe-search-box-condensed').show();
		});
		

		/* nutrition info links */
		$('#nutrition-link, .nutrition-link').click( function(e) {
			e.preventDefault();
			$('#modalTemplate #modalContentArea').html($('#nutrition-popup').clone());
			$('#modalTemplate').modal({
				opacity:48,
				overlayClose: false,
				zIndex: 2000
			});
		});
		
		$('#product-info-link').click ( function(e) {
			e.preventDefault();
			$('#modalTemplate #modalContentArea').html($('#product-info-popup').clone());
			$('#modalTemplate').modal({
				opacity:48,
				overlayClose: false,
				zIndex: 2000
			});
		});
		
		$('.watch-the-video-a').click( function(e) {
			e.preventDefault();
			$.get($(this).attr('href'), function(data) {
				$('#modalTemplate #modalContentArea').html(data);
				$('#modalTemplate').modal({
					opacity:48,
					overlayClose: false,
					zIndex: 2000
				});
			});
		});
		
		
		$('.watch-happy-starts-here').colorbox({
			iframe:true,
			title:false,
			fixedWidth:'600',
			fixedHeight:'500',
			transition:'none'
		});
		$('.watch-happy-starts-here').click( function(e) {
			e.preventDefault();
			$('#colorbox').ready(function(){
				$('#colorbox').addClass('inner-flash');
				$('#colorbox').css('background','none');
				$('#colorbox').css('width','683px');
			});
			return false;
		});
		
		$('.watch-standard-video').colorbox({
			iframe:true,
			title:false,
			fixedWidth:'600',
			fixedHeight:'500',
			transition:'none'
		});
		$('.watch-standard-video').click( function(e) {
			e.preventDefault();
			$('#colorbox').ready(function(){
				$('#colorbox').addClass('inner-flash');
				$('#colorbox').addClass('standard');
				$('#colorbox').css('background','none');
				$('#colorbox').css('width','488px');
			});
			return false;
		});
		
		$('#why-popup-link').click( function(e) {
			e.preventDefault();
			$('#modalTemplate #modalContentArea').html($('#why-popup-content').clone());
			$('#modalTemplate').modal({
				opacity:48,
				overlayClose: false,
				zIndex: 2000
			});	
		});
		
		$('#why-popup-link2').click( function(e) {
			e.preventDefault();
			$('#modalTemplate #modalContentArea').html($('#why-popup-content2').clone());
			$('#modalTemplate').modal({
				opacity:48,
				overlayClose: false,
				zIndex: 2000
			});	
		});
		
		if($('#top-search-field').length>0){
			$('#top-search-field #keywords').keypress(function(e){
				//if enter is pressed, submit form
				if(e.which == 13) $('#top-search-field button').click();
			});	
		}

		
		/* recipe search box clearing */
		$('.keywords-enter').focus( function() {
			if ($(this).val() == 'Mustard Chicken...') {
				$(this).val('');
			}
		});
		$('.keywords-enter').blur( function() {
			if ($(this).val() == '') {
				$(this).val('Mustard Chicken...');
			}
		});
		
		/* print links */
		$('.print-link').click ( function() {
			window.print();
			pageTracker._trackPageview(window.location + '?print=1');
		});
		
		$('.requires-login').click( function(e) {
			e.preventDefault();
			var action = $(this).attr('title');
			var queryString = $(this).attr('href').split('?')[1];
			$('#modalTemplate #modalContentArea').html($('#modal-content-login').clone());
			$('#modalTemplate').modal({
				opacity:48,
				overlayClose: false,
				zIndex: 2000,
				onShow : function() {
					$('#simplemodal-container #login-popup-action').html(action);
					$('#simplemodal-container #login-link').attr('href', '/my-frenchs/login?' + queryString);
					$('#simplemodal-container #create-account-link').attr('href', '/my-frenchs/create-an-account?' + queryString);
				}
			});
		});
		
		/* requires login modal on main nav 'Coupons' */
		$('#top-nav-promotions a').click (function(e) {
			if (!is_logged_in) {
				e.preventDefault();
				var action = "View Coupons";
				var queryString = $(this).attr('href').split('?')[1];
				$('#modalTemplate #modalContentArea').html($('#modal-content-login').clone());
				$('#modalTemplate').modal({
					opacity:48,
					overlayClose: false,
					zIndex: 2000,
					onShow : function() {
						$('#simplemodal-container #login-popup-action').html(action);
						$('#simplemodal-container #login-link').attr('href', '/my-frenchs/login?' + queryString);
						$('#simplemodal-container #create-account-link').attr('href', '/my-frenchs/create-an-account?' + queryString);
					}
				});
			}
		});

		
	});
	
 
 /**
  * _close(); 	closes the sendto friend popup (triggered after e-mails sent)
  **/
  function RequiresLogin_close(no_wait){
	if(no_wait) $('#modalClose').click();
	else setTimeout("$('#modalClose').click();",2000);
  }
  
  
function openVideoModal(recipe) {
	$.get('/recipes/video-modal?r='+recipe, function(data){
		$('#modalTemplate #modalContentArea').html(data);
		$('#modalTemplate').modal({
			opacity:48,
			overlayClose: false,
			zIndex: 2000
		});
	});
}
