$(document).ready(function(){
	
	// jQuery SWFObject
	$('.tt-flash').flash({swf:'/media/tt-flash.02052010.swf', width:643, height:275});
	$('.tt-flash-review').flash({swf:'/media/tt-flash.02052010.swf', width:643, height:275});
	$('.bbt-flash').flash({swf:'/media/bbt-flash.02032010.swf', width:643, height:275});
	$('.bbt-flash-review').flash({swf:'/media/bbt-flash.02032010.swf', width:643, height:275});
	$('.ti-flash').flash({swf:'/media/ti-flash.03032009.swf', width:643, height:275});
	$('#home-items').flash({swf:'/media/region-map.05122009.swf', width:383, height:265});
	$('#home-items-review').flash({swf:'/media/region-map.05122009.swf', width:383, height:265});
	
	// Open new window without target="_blank" - use <a href="#" class="external">
	$(".external").attr("target","_blank");

	
	// Catering to IE6
	$("#landing .grid-2:last-child").addClass("last-child")
	$("#landing .grid-2:first-child").addClass("first-child")
	$("li:last-child").addClass("last-child"); // Adds a class="lastchild" to the last <li> in a list
	$("ol.striped li:nth-child(odd)").addClass("oddrow"); // Adds class="oddrow" to odd rows in every <ol class="striped">
	$("input[@type=submit]").addClass("submit"); // Adds class="submit" to <input type="submit" .../>
	$("input[@type=password], input[@type=text]").addClass("text"); // Adds class="text" to text and password inputs

	$("input[@type=password], input[@type=text], textarea").focus(function() {
        $(this).addClass("focus"); 
                //focus class provides a hook to alert the user 
    });
    
    $("input[@type=password], input[@type=text], textarea").blur(function(){
        if ($(this).find(".focus")) {
            $(this).removeClass("focus");
        }
    });
	
	// Drop shadow
	//$("#favorite, #subnav").dropShadow({left: -2, top: 2, blur: 1, opacity: 0.5});

	// Chef form field highlighting
	$('#chefform form').highlight('div', 'highlight', 'mousedown')
	
	// Places the <label> from <form id="searchform"> inside the text input and makes it show/hide on blur/focus
	$("#searchform label, #recipesearchform label").each(function() {
		var label = $(this);
		var input = $('#' + label.attr('for'));
		var initial = label.hide().text().replace(':', '');
		input.focus(function() {
			input.css('color', '#2b2e27');
			if (input.val() == initial) {
				input.val('');
			}
		}).blur(function() {
			if (input.val() == '') {
				input.val(initial).css('color', '#2b2e27');
			}
		}).css('color', '#2b2e27').val(initial);
	});
	
	// Hide <div id="othercuisine" />
	$("#field_id_91").css("display","none");
		
	// Add onclick handler to checkbox id="57"
	$("#57").click(function(){

		if ($("#57").is(":checked")) {
			$("#field_id_91").fadeIn("slow");
		}
		else {
			$("#field_id_91").fadeOut("slow");
		}
	});

	// Add onclick handler to checkbox id="recipepicture"
	$("#pictureperm").click(function(){

		if ($("#pictureperm").is(":checked")) {
			$("#recipepicture").fadeIn("slow").animate({ backgroundColor: "#ccffbb" }, "slow");
		}
		else {
			$("#recipepicture").fadeOut("slow");
		}
	});

	// Tabbed interface
	$('#chefform > ul').tabs();
	
	var $tabs = $('#chefform').tabs(); // first tab selected

	$('#tab1next, #tab3prev').click(function() {
		$tabs.tabs('select', 1); // switch to second tab
		return false;
	});
	
	$('#tab2prev').click(function() {
		$tabs.tabs('select', 0); // switch to first tab
		return false;
	});
	
	$('#tab2next').click(function() {
		$tabs.tabs('select', 2); // switch to third tab
		return false;
	});

	// Select all checkbox
	$("#selectall").click(function()				
	{
		var checked_status = this.checked;
		$("input[@name^=category]:not(:last)").each(function() // Select all checkboxes where the name attribute starts with "category", except the last one ("Other")
		{
			this.checked = checked_status;
		});
	});
	
	// Add Item functionality (see below for the functions)
	addItem();
	addPrepItem();
	
	// jQuery Validation plugin
	var container = $('div.formerror');
	// validate the form when it is submitted
	var validator = $("#entryform").validate({
		errorContainer: container,
		errorLabelContainer: $("ol", container),
		wrapper: 'li',
		meta: "validate"
});
	
	$(".cancel").click(function() {
		validator.resetForm();
	});
	
	
	// Initialize Shadowbox
	Shadowbox.init();
	
}); // End of main jQuery function



// Custom functions for add-step & add-step-prep
function addItem(custom) {
  
  $("#add-step li").hide();

  $("#add-step").each(function(){
    var step = $(this).find("li");
    for (i=0;i<step.length;i++) {
      var defaultSteps = step[i];
      if((i<=3)){
        $(defaultSteps).show();
      }
    };
  });

   $("#add-step p.button a").click( function() { 
   $("#add-step li:hidden:first").fadeIn("slow");
   return false;
  });
}

function addPrepItem(custom) {
  
  $("#add-step-prep li").hide();

  $("#add-step-prep").each(function(){
    var step = $(this).find("li");
    for (i=0;i<step.length;i++) {
      var defaultSteps = step[i];
      if((i<=3)){
        $(defaultSteps).show();
      }
    };
  });

   $("#add-step-prep p.button a").click( function() { 
   $("#add-step-prep li:hidden:first").fadeIn("slow");
   return false;
  });
}


var newwindow;
function poptastic(url)
{
	newwindow=window.open(url,'name','height=500,width=300');
	if (window.focus) {newwindow.focus()}
}
