/* create a timeOut function in jQuery */
    jQuery.fn.idle = function(time){
        return this.each(function(){
            var i = $(this);
            i.queue(function(){
                setTimeout(function(){
                i.dequeue();
                }, time);
            });
        });
    };
/**
 * jQuery Plugin Toggle Fade v1.0
 * Requires jQuery 1.2.3 (Not tested with earlier versions).
 * Copyright (c) 2008 Gregorio Magini [gmagini at gmail dot com] 
 * 
 *	@param: Object Array. Arguments need to be in object notation.
 *	Returns: jQuery.
 *	Options:	
 *		speedIn: Sets the speed of the fadeIn effect. Default: "normal".
 *    speedOut: Sets the speed of the fadeOut effect. Default: same as speedIn.
 *
 *	Examples: 
 *    
 *    speedIn and speedOut both "normal":
 *		$("#toggle-link").toggleFade();
 *
 *    speedIn and speedOut both "fast":
 *		$("#toggle-link").toggleFade({ speedIn : "fast");
 *
 *    different settings for speedIn and speedOut:
 *		$("#toggle-link").toggleFade({ speedIn : 800, speedOut : 150 });
 *
 */

(function($) {
  $.fn.toggleFade = function(settings)
  {
  	settings = jQuery.extend(
  		{
        speedIn: "normal",
        speedOut: settings.speedIn
  		}, settings
  	);
  	return this.each(function()
  	{
  	  var isHidden = jQuery(this).is(":hidden");
      jQuery(this)[ isHidden ? "fadeIn" : "fadeOut" ]( isHidden ? settings.speedIn : settings.speedOut);
    });
  };
})(jQuery);
// Removes leading whitespaces
function LTrim( value ) {
	
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");
	
}

// Removes ending whitespaces
function RTrim( value ) {
	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");
	
}

// Removes leading and ending whitespaces
function trim( value ) {
	
	return LTrim(RTrim(value));
	
}
/* 
   Simple JQuery Collapsing menu.
   HTML structure to use:

   <ul id="menu">
     <li><a href="#">Sub menu heading</a>
     <ul>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       ...
       ...
     </ul>
     <li><a href="#">Sub menu heading</a>
     <ul>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       <li><a href="http://site.com/">Link</a></li>
       ...
       ...
     </ul>
     ...
     ...
   </ul>

Copyright 2007 by Marco van Hylckama Vlieg

web: http://www.i-marco.nl/weblog/
email: marco@i-marco.nl

Free for non-commercial use
*/


function initMenu() {
  // recipe filters
  $('.recipeFilterNav').hide();
  $('#recipeFilters img').click(
    function() {
        $(this).next().slideToggle('normal');	
      }
    );
	$("#recipeFilters img").toggle(
	  function () {
		this.src = this.src.replace("_off","_on");
	  },
	  function () {
		this.src = this.src.replace("_on","_off");
	  }
);
	
  }
$(document).ready(function() {initMenu();});

function initMenuExpando() {
  // recipe filters
  $('.expandoArea').hide();
  $('.expandoBtn').click(
    function() {
        $(this).next().slideToggle('normal');	
      }
    );
	$(".expandoBtn").toggle(
	  function () {
		this.src = this.src.replace("_off","_on");
	  },
	  function () {
		this.src = this.src.replace("_on","_off");
	  }
);
  //products
    $('.expandoAreaProd').hide();
  $('.expandoProdBtn').click(
    function() {
        $(this).next().slideToggle('normal');	
      }
    );
	$(".expandoProdBtn").toggle(
	  function () {
		this.src = this.src.replace("_off","_on");
	  },
	  function () {
		this.src = this.src.replace("_on","_off");
	  }
);
	
  }
$(document).ready(function() {initMenuExpando();});


$(document).ready(function(){
	// top nav rollover
	$(".rollover").hover(
	 function()
	 {
	  this.src = this.src.replace("_off","_on");
	 },
	 function()
	 {
	  this.src = this.src.replace("_on","_off")	;
	 }
	);
	// messagebox 
	if(document.getElementById("messageBox")){
		// fade out
		$('#messageBox').idle(7000).fadeOut('fast');
	}
	
	if (document.getElementById("slides")){
		function onAfter() { 
			$('#slideText').html(this.title); 
		}
		// home slider rotation
		$('#slides').cycle({ 
			fx:    'fade', 
			timeout:  5000,
			pager: '#slideNav',
			slideExpr: 'img',
			after: onAfter
		
		});
		
	}
	
	// gcc carousel
	if (document.getElementById("gccChickens")){
		// portfolio carousel
		$('#mycarousel').jcarousel({
			auto: 0,
			scroll: 5
			//wrap: 'circular'
		});
	}
	//gcc login
	if(document.getElementById("gccChickens")){
		// login form
		$(".gccloginLink").fancybox({
			scrolling 		: 'no',
			titleShow		: false,
			width			: 	300,
			height			:	200,
			overlayOpacity	:	.7,
			onComplete		:	function() {
										document.gccLoginForm.GCC_Email.focus();
									}
		});
		// invalid code
		$("#invalidCodeLink").fancybox({
			scrolling 		: 'no',
			titleShow		: false,
			width			: 	554,
			height			:	240,
			overlayOpacity	:	.7
		});
	}
	// login link for coupon request
	$('#loginLink').click(
    function() {
        $("#couponRequestLogin").slideToggle('normal');	
      }
    );
	// top nav search
	if(document.getElementById("searchField")){
		$('#searchField').focus(    function() {
			$('#searchField').css('fontStyle','normal');	
      	}
   	 );
	 $('#searchField').blur(    function() {
			$('#searchField').css('fontStyle','italic');	
      	}
   	 );
	}
	//recipe search
	if(document.getElementById("recipeSearchField")){
		$('#recipeSearchField').focus(    function() {
			$('#recipeSearchField').css('fontStyle','normal');	
      	}
   	 );
	 $('#recipeSearchField').blur(    function() {
			$('#recipeSearchField').css('fontStyle','italic');	
      	}
   	 );
	}
	// on actions for recipe fiilters
	if(document.getElementById("btnDishOn")){
		
		$('#btnDishOn').src = $('#btnDishOn').attr("src") .replace("_off","_on");
        $('#btnDishOn').next().slideToggle('normal');	
	}
	if(document.getElementById("btnTimeOn")){
		
		$('#btnTimeOn').src = $('#btnTimeOn').attr("src") .replace("_off","_on");
        $('#btnTimeOn').next().slideToggle('normal');	
	}
	if(document.getElementById("btnMethodOn")){
		
		$('#btnMethodOn').src = $('#btnMethodOn').attr("src") .replace("_off","_on");
        $('#btnMethodOn').next().slideToggle('normal');	
	}
	if(document.getElementById("btnNutroOn")){
		
		$('#btnNutroOn').src = $('#btnNutroOn').attr("src") .replace("_off","_on");
        $('#btnNutroOn').next().slideToggle('normal');	
	}
	if(document.getElementById("btnCuisineOn")){
		
		$('#btnCuisineOn').src = $('#btnCuisineOn').attr("src") .replace("_off","_on");
        $('#btnCuisineOn').next().slideToggle('normal');	
	}
	if(document.getElementById("btnProdTypeOn")){
		
		$('#btnProdTypeOn').src = $('#btnProdTypeOn').attr("src") .replace("_off","_on");
        $('#btnProdTypeOn').next().slideToggle('normal');	
	}
	
	//related product rollovers
	if(document.getElementById("recipeRelProds")){
		var orgSrc = $("#relRecipeProdImg").attr("src");
		$(".relRecipeProdItem").hover(
			 function()
			 {
				 var whichDiv = $(this).attr("id");
				 whichDiv = whichDiv.replace("Item","Img");
				 var imgSrc = $("#"+whichDiv).attr("src");
				 $("#relRecipeProdImg").attr("src",imgSrc);
			 },
			 function()
			 {
			 	// var whichDiv = $(this).attr("id");
				// whichDiv = whichDiv.replace("Link","Img");
				//  var imgSrc = $("#"+whichDiv).attr("src");
				 $("#relRecipeProdImg").attr("src",orgSrc);
			 }
			);

	}
	// gcc images
	if (document.getElementById("gccEntryWrapper")){
		$("a[href$=.jpg],a[href$=.png],a[href$=.gif]").fancybox();
	
	}
	// gcc ticker
	if (document.getElementById("gccTicker")){
		$("ul#gccTicker").liScroll({travelocity: 0.15});
	}
	
	//popups
	$("a.iframe").fancybox({
			scrolling 		: 'yes',
			titleShow		: false,
			width			: 	640,
			height			:	600,
			overlayOpacity	:	.7,
			type		:	'iframe'
		});
	$("a.iframeCon").fancybox({
			scrolling 		: 'no',
			titleShow		: false,
			width			: 	610,
			height			:	490,
			overlayOpacity	:	.7,
			type		:	'iframe'
		});
	// recipe email form
	if(document.getElementById("recipeEmailLink")){
		$("#recipeEmailLink").fancybox({
			scrolling 		: 'no',
			titleShow		: false,
			width			: 	500,
			height			:	500,
			overlayOpacity	:	.7,
			onComplete		:	function() {
										document.emailRecipeForm.yourName.focus();
									}
		});
	}
	// recipe print
	if(document.getElementById("recipePrintLink")){
		$("#recipePrintLink").fancybox({
			scrolling 		: 'yes',
			titleShow		: false,
			width			: 	640,
			height			:	600,
			overlayOpacity	:	.7,
			type		:	'iframe'
		});
	}
	// coupon mail form
	if(document.getElementById("couponMailLink1")){
		$("#couponMailLink1").fancybox({
			scrolling 		: 'no',
			titleShow		: false,
			width			: 	500,
			height			:	500,
			overlayOpacity	:	.7,
			onComplete		:	function() {
										document.couponMailForm1.First_Name.focus();
									}
		});
	}
	if(document.getElementById("couponMailLink2")){
		$("#couponMailLink2").fancybox({
			scrolling 		: 'no',
			titleShow		: false,
			width			: 	500,
			height			:	500,
			overlayOpacity	:	.7,
			onComplete		:	function() {
										document.couponMailForm3.First_Name.focus();
									}
		});
	}
	if(document.getElementById("couponMailLink3")){
		$("#couponMailLink3").fancybox({
			scrolling 		: 'no',
			titleShow		: false,
			width			: 	500,
			height			:	500,
			overlayOpacity	:	.7,
			onComplete		:	function() {
										document.couponMailForm3.First_Name.focus();
									}
		});
	}
	if(document.getElementById("couponMailLink4")){
		$("#couponMailLink4").fancybox({
			scrolling 		: 'no',
			titleShow		: false,
			width			: 	500,
			height			:	500,
			overlayOpacity	:	.7,
			onComplete		:	function() {
										document.couponMailForm4.First_Name.focus();
									}
		});
	}
	// auto complete for main search
	if(document.getElementById("searchField")){
            $("#searchField").autocomplete({
                source: "http://216.17.90.210/site_search_post.cfm",
                minLength: 2,
                select: function(event, ui) {
					$('#searchURL').val(ui.item.id);
					var theURL = document.searchForm.searchURL.value;
					document.location.href = theURL;
                    ;
                }
            });
	}
	
	
	// auto complete for recipes
	if(document.getElementById("recipeSearchField")){
            $("#recipeSearchField").autocomplete({
                source: "recipe_search_post.cfm",
                minLength: 2,
                select: function(event, ui) {
					$('#Recipe_ID').val(ui.item.id);
					$('#recipeSearchField').val("boogo");

					var theID = document.recipeSearchForm.Recipe_ID.value;
					document.location.href='recipe.cfm?Recipe_ID=' + theID;


             }  
            });
	}
	
	
	
	/**
	 * Character Counter for inputs and text areas
	 */
	$('.word_count').each(function(){
		// get current number of characters
		var length = $(this).val().length;
		// get current number of words
		var length = $(this).val().split(/\b[\s,\.-:;]*/).length;
		// update characters
		$(this).parent().find('.counter').html( length + ' characters');
		// bind on key up event
		$(this).keyup(function(){
			// get new length of characters
			var new_length = $(this).val().length;
			// get new length of words
			//var new_length = $(this).val().split(/\b[\s,\.-:;]*/).length;
			// update
			$(this).parent().find('.counter').html( new_length + ' characters');
		});
	});
	
	if(document.getElementById("User_Name_J")){
		$('#User_Name_J').focus();
	}
	
	// contact page
	if(document.getElementById("contactForm99")){
		
		// consumer questions
		var consumer = {
			"" :	"Select a specific question from the drop-down",
			"0" : 	"I'd like to know where I can buy your products.",
			"1" : "I have a concern/complaint about one of your products.",
			"2" : "I have a question about one of your products.",
			"3" : "I want to share something positive about your products.",
			"4" : "I want to know what the date code on the package means.",
			"5" : "I have a question about cooking chicken.",
			"6" : "I am looking for a chicken recipe.",
			"7" : "I have a question about how you raise your chickens.",
			"8" : "I have a question about what you feed and/or give your chickens.",
			"9" : "I have a comment or question about your advertising, a promotion or an offer.",
			"15" : "None of the above."
		};
		var employee = {
			"" :	"Select a specific question from the drop-down",
			"10" : "I am looking for a job."
		};
		
		var donation = {
			"" :	"Select a specific question from the drop-down",
			"11" : "I would like to request a donation from your company."
		};
		
		var media = {
			"" :	"Select a specific question from the drop-down",
			"12" : "I am with the media/blogger and would like to talk to someone."
		};
		
		var partner = {
			"" :	"Select a specific question from the drop-down",
			"13" : "I am interested in selling or distributing your products.",
			"14" : "I am interested in raising chickens for your company."
		};
		
		var current = {
			"" :	"Select a specific question from the drop-down",
			"16" : "I am a current business partner."
		};
		
		// not really used but added just in case they do in the future
		var other = {
			"" :	"Select a specific question from the drop-down",
			"15" : "I have some other issue."
		};
		
		// product type dropdown
		$(".productTypeSelect").change(function() {
			var prodCode = $(this).val();
			if(prodCode == ""){
				// hide all items
				$(".prodTypeItem").fadeOut(50);
			}
			else{
				// hide all items, display the "all" items and only those for the selected product
				$(".prodTypeItem").fadeOut("fast");
				$(".all").fadeIn("fast");
				$("."+prodCode).fadeIn("fast");
				// change thumbnail and link to large image
				var imgSrc = "images/" + prodCode + "_contact_us_large.jpg";
				var imgSrcT = "images/" + prodCode + "_contact_us_thumb.jpg";
				$(".prodTypeLg").attr('href',imgSrc);
				$(".prodTypeThumb").attr('src',imgSrcT);
			}
		});
		
		
		$("#youAre").change(function() {
			// hide everything
			$(".contactItem").fadeOut('fast');
			$(".prodTypeItem").fadeOut("fast");
			$("#specificQuestionWrapper").fadeOut('fast');
			// which item selected
			var youAreNum = $("#youAre").val();
			var select = $('#specificQuestion');
			var options = select.attr('options');
			var goDirect = false;
			$('option', select).remove();
			if(youAreNum == 1){
				newOptions = consumer;
			}
			else if(youAreNum == 2){
				newOptions = employee;
				goDirect = true;
				var qNum = 10;
			}
			else if(youAreNum == 3){
				newOptions = donation;
				goDirect = true;
				var qNum = 11;
			}
			else if(youAreNum == 4){
				newOptions = media;
				goDirect = true;
				var qNum = 12;
			}
			else if(youAreNum == 5){
				newOptions = partner;
			}
			else if(youAreNum == 7){
				newOptions = current;
				goDirect = true;
				var qNum = 16;
			}
			else if(youAreNum == 6){
				newOptions = other;
				goDirect = true;
				var qNum = 15;
			}
			// build the option list
			$.each(newOptions, function(val, text) {
				$('#specificQuestion').append(
					$('<option></option>').val(val).html(text)
					
				);
				//options[options.length] = new Option(text, val);
			});
			// set the option without a value as selected
			$("#specificQuestion option[value='']").attr("selected","selected");
			// display it
			//$("#youAre").fadeOut('fast');
			if(!goDirect){
				$("#specificQuestionWrapper").fadeIn('slow');
			}
			else{
				// is there an interim step
				var interimDiv2 = "interim" + qNum;
				var interimDiv = "#interim" + qNum;
				var contactDiv = "#contact" + qNum;
				var btnID = "#button" + qNum;
				if(document.getElementById(interimDiv2)){
					$(interimDiv).fadeIn('fast');
					// is there a button clicked
					$(btnID).click(function() {
							$(interimDiv).fadeOut(50);
							$(contactDiv).fadeIn('slow');
							$(".email_condReq").fadeIn('fast');
							$("#btnSubmitGeneric").fadeIn('slow');
					});
				}
				else {
					$(contactDiv).fadeIn('slow');
					$("#btnSubmitGeneric").fadeIn('slow');
				}
			}
		});
		
		
		// select a question, hide all the elements
		$("#specificQuestion").change(function() {
			var questionNum = $("#specificQuestion").val();
			var interimDiv = "#interim" + questionNum;
			var interimDiv2 = "interim" + questionNum;
			var contactDiv = "#contact" + questionNum;
			var btnID = "#button" + questionNum;
			
			$(".contactItem").fadeOut(50);
			$("#btnSubmit").fadeOut(50);
			//alert("ho");
			$(".productTypeSelect option[value='']").attr("selected", true);
			$(".prodTypeItem").fadeOut(50);
			// is there an interim div, if so display it otherwise go direct to form
			if(document.getElementById(interimDiv2)){
				
				$(interimDiv).fadeIn('fast');
				$(btnID).click(function() {
						$(interimDiv).fadeOut(50);
						$(contactDiv).fadeIn('slow');
						// if a grower request mail address is required
						
						if(questionNum == 14){
							$(".usmail_condReq").fadeIn('fast');
						}
						else{
							$(".email_condReq").fadeIn('fast');
						}
						$("#btnSubmitGeneric").fadeIn('slow');
				});
			}
			else {
				$(contactDiv).fadeIn('slow');
						$("#btnSubmitGeneric").fadeIn('slow');
						//alert(questionNum);
						// if a grower request mail address is required
						if(questionNum == 14){
							$(".usmail_condReq").fadeIn('fast');
						}
						else{
							$(".email_condReq").fadeIn('fast');
						}
			}
		});	
			
		
		$(".contactOption").click(function() {
				$(".condReq").fadeOut(50);
				var formNum = $(this).attr("id");
				formNumArry = formNum.split("_");
				formNum = formNumArry[1];
				var contactMeth = $(this).val();
				//alert(formNum+ " " + contactMeth);
				// hide/show required asteisk
				$("."+ contactMeth + "_condReq").fadeIn('fast');
				//$(".condReq").fadeOut(50);
				//$(".email_condReq").fadeIn('fast');
		});
		
		
		
		// which contact method
		//$("#preferredContact").click(function() {
		//		var form = $('input[type=image]').form;
		//		alert($(form).attr('name'));

		//		$(".condReq").fadeOut(50);
		//		$(".email_condReq").fadeIn('fast');
		//});
		//$("#preferredContact1").click(function() {
		//		$(".condReq").fadeOut(50);
		//		$(".phone_condReq").fadeIn('fast');
		//});
		//$("#preferredContact2").click(function() {
		//		$(".condReq").fadeOut(50);
		//		$(".usmail_condReq").fadeIn('fast');
		//});
	}
	

});


