$(function(){
	$('#login').toggle(function(event) {
		//$('#loginForm').css('display','block');
		$('#loginForm').show();
	}, function(event){
		//$('#loginForm').css('display','none');
		$('#loginForm').hide();
	});
	$('#nav li.nav-main').simpleAccordian();
	$('a.category').click(function(){
		setCookie('accordianMenu', false);
	});
	
	$('.more').hide();
	$('a.expand').toggle(function(){
		$(this).text('- collapse');
		$(this).parents('.recipe').find('.more').slideDown();
		
	}, function(){
		$(this).text('+ expand');
		$(this).parents('.recipe').find('.more').slideUp();
	});
	
	$('.printRecipe').click(function() {
		$(this).parent().parent().parent().parent().jqprint();
	});
	
	$('#search-ingredient').change(function(){
		var val = $(this).val();
		if(val != "") {
			$('#search-keywords').val(val).hide();
		} else {
			$('#search-keywords').val('').show();
		}
	});

	$('a.popup, a[rel="_blank"]"').popUp();	

});

jQuery.fn.popUp = function(options)
{
	var settings = {
		name: 'newWindow',
		width: 500,
		height: 600,
		left: false,
		top: false,
		scrollbars: 'yes',
		resizeable: 'no',
		statusbar: 'no',
		menubar: 'no',
		toolbar: 'no'
	}

    if(options) {
        jQuery.extend(settings, options);
    };

	this.each(function(){
		$(this).click(function(){
			var rel = $(this).attr('rel');
			var href = $(this).attr('href');
			if(!rel){
				window.open(href, settings.name);
			} else {
				var rel_split = rel.split('|');
				var width = rel_split[0] ? rel_split[0] : settings.width;
				var height = rel_split[1] ? rel_split[1] : settings.height;
				var scrollbars = rel_split[2] ? rel_split[2] : settings.scrollbars;
				var leftPos = settings.left ? settings.left : (screen.width-width)/2;
				var topPos = settings.right ? settings.right : (screen.height-height)/2;
				
				var config = 'width='+ width +',height='+ height +', \
							left='+ leftPos +',top='+ topPos +', \
							scrollbars='+ scrollbars +', \
							resizable='+ settings.resizeable +', \
							statusbar='+ settings.statusbar +', \
							menubar='+ settings.menubar +', \
							toolbar='+ settings.toolbar;
					
				window.open(href,settings.name, config);
			}
			return false;
		});
	});
}


jQuery.fn.simpleAccordian = function(options)
{
	var settings = {}

    if(options) {
        jQuery.extend(settings, options);
    };

	var self = $(this);

	$(this).each(function(i){
		var el = $(this);
		var ul = el.find('ul');
		$(ul).wrap('<div class="group"></div>'); 		
		var group = el.find('.group').hide();
				
		el.find('a').not('.group a').click(function(){
			self.find('.group').slideUp('fast');
			if( group.is(':hidden') ) {
				group.slideDown('fast');
				setCookie('accordianMenu', i);
			}
			return false;
		});
		
		if( getCookie('accordianMenu') == i) {
			group.show();
		}
	});	
}

var getCookie = function(name){  
		var re=new RegExp(name+"=[^;]+", "i"); //construct RE to search for target name/value pair
		if (document.cookie.match(re)) //if cookie found
			return document.cookie.match(re)[0].split("=")[1]; //return its value
		return null;
};

var setCookie = function(name, value){
	document.cookie = name + "=" + value + "; path=/";
};

jQuery.fn.perfectForm = function(options)
{
	var settings = {
    	overlap: true				
    }

    if(options) {
        jQuery.extend(settings, options);
    };

	this.each(function(){
		var input = $(this).next('input');
		var label = $(this);
		
		// first check all inputs for values, then hide it's label if it has a value
		if( settings.overlap ) {
			label.addClass('overlap');
		}
		
		if( settings.overlap && input.val() != '' ) {
			label.hide();
		}
		
		label.click(function(){
			label.hide();
			input.focus();
		}, function(){
			if( input.val() == '' ) {
				label.show();
			}
		});
		
		// then add a focus event to all inputs to hide their labels when focused on, 
		// and remain hidden on blur if a value is entered
		input.focus(function(){
			label.hide();
		}).blur(function(){
			if( $(this).val() == '' ) {
				label.show();
			}
		});
		
		// do the same thing for elements added to the DOM after page load. jQuery 1.3 has the
		// 'live' method, but doesn't support focus or blur yet, so use livequery for now.
		input.livequery('focus', function(){
			label.hide();
		}).livequery('blur', function(){
			if( $(this).val() == '' ) {
				label.show();
			}
		});
	});
}		

jQuery.fn.autoWidth = function(options) 
{
    var settings = {
    	minWidth   : false,
        limitWidth  : false,
        ignore	: ''
    }

    if(options) {
        jQuery.extend(settings, options);
    };

    var maxWidth = 0;

	this.not(settings.ignore).each(function(){
        if ($(this).width() > maxWidth){
        	if(settings.limitWidth && maxWidth >= settings.limitWidth) {
        		maxWidth = settings.limitWidth;
        	} 
        	else if(settings.minWidth && maxWidth <= settings.limitWidth)
        	{
        		maxWidth = settings.minWidth;
        	} 
        	else 
        	{
        		maxWidth = $(this).width();
        	}
        }
	});	 

	this.not(settings.ignore).width(maxWidth);
}