/*
    Initialize Events
	-----------------------------------------------------------*/
    $(document).ready(function(e){
  		$('a[href^="http://"], a[rel="directions"], a[rel="external"], a[rev="external"]').live('click',external_link);

  		$('a[name="ammenity"]').live('click',ammenities);
  		$('#location_ammenities a[rel="close"]').live('click',ammenities);

  		$('a[rel="apply"]').live('click',job_application);
  		$('a[rel="browser"]').live('click',dead_browser);

        $('a.checkbox').live('click',form_checkbox_foci);
        $('a[type="radio"]').live('click',form_radio_foci);
        $('a[type="select"] select').live('click',{'mouse':'click'},form_select_foci).live('change',{'mouse':'change'},form_select_foci).live('blur',{'mouse':'blur'},form_select_foci);


		$('form input[name="customer_phone"]').live('blur',{ 'type' : 'phone' },form_coerce_value);

    	$('form#deli_order a[rel="process"]').live('click',form_submission);
    	$('form#key_card_program_subscription a[rel="process"]').live('click',form_submission);
    	$('form#sandwich_order a[rel="process"]').live('click',form_submission);



        $('form select').css({'opacity':'0','-ms-filter':'progid:DXImageTransform.Microsoft.Alpha(Opacity=0)','filter':'alpha(opacity=0)'}).change().click();
        
        
        if($('#slideshow li').length						    > 1){
        
        	$slideshow_interval                               	= setInterval('slideshow()',4500);
        
        }
    });





/* 
	Site Functions
	-----------------------------------------------------------*/
    
    
    	/* Dead Browser
		-------------------------------------------------------*/
    	function dead_browser(e){    		
    		$.post(
				'/functions/user.functions.php?function=user_dead_browser',
				function(e){
					$('#site_alert').fade_away();
				}
			);
    	
    	
    		return false;
    	}
    	
    	
		/* External Link
		-------------------------------------------------------*/
    	function external_link(e){
    		var $link											= $(this);
    	
    		window.open($link.attr('href'),$link.attr('rel'),"toolbar=0,location=1,directories=0,status=1,menubar=1,scrollbars=1,resizable=1,width=900,height=600");
    	
    	
    		return false;
    	}
    	
    	
    	
    	

/* 
	User Functions
	-----------------------------------------------------------*/
	
	
		/* Ammenities
		-------------------------------------------------------*/
		function ammenities(e){
			var $button											= $(this);
		
		
			if($button.attr('rel')							   == "close"){
			
				$('#location_ammenities').removeClass('current');
			
			} else {
		
				$('#location_ammenities').load(
					"/functions/user.functions.php?function=user_paladin&paladin=location_ammenities",
					{ 
						ammenity									: $button.attr('rel')
					},
					function(data){
						$('#location_ammenities').addClass('current');
					}
				);
				
			}
		
		
			return false;
		}
		
		
		
		/* Job Application
		-------------------------------------------------------*/
		function job_application(e){
			var $button											= $(this);
		
		
			if($button.parents('ul').length						> 0){
			
				var $description								= $button.siblings("p").text();
				var $title										= $button.siblings("h5").text();
			
			} else {
			
				var $description								= $button.siblings("h4").text();
				var $title										= $button.siblings("h2").text();
			
			}
			
			
			var $body											= "Thank you for your interest in the " + $title + " job opportunity at New England Farms. %0D%0DPlease Attach your Resume. We will contact you after we have reviewed your resume.";
			var $subject										= "Job Application &ndash;" + $title;	
			
			$body										   	   += "%0D%0D%0D%0D%0D%0D========================================%0D%0D%0D";
			$body										   	   += $title + "%0D%0D";
			$body										   	   += $description;
			$body										   	   += "%0D%0D%0D========================================%0D%0D%0D%0D";
			
			
			location.href 										= "mailto:career_opportunities@ne-farms.com,laurasmith@ne-farms.com?subject="+ $title +"&body="+ $body;
			
			
			return false;
		}
		
		
		
		/* Slideshow
		-------------------------------------------------------*/
		function slideshow(e){
			var $current        								= $('#slideshow li.current');

        	if($current.length 								   == 0){ 

        		$current 										= $('#slideshow li:last'); 

        	}

        	var $next 											= $current.next().length ? $current.next() : $('#slideshow li:first');


        	$next.hide();
        	$current.fade_out(1000,function(event){ $next.addClass('current'); });
        	$next.fade_in(1000,function(event){ $current.removeClass('current'); });
		}
	
	
	
	
/* 
	Form Functions
	-----------------------------------------------------------*/
    	
    	
    	/* Form Checkbox Foci
		-------------------------------------------------------*/
    	function form_checkbox_foci(e){
    		if(!$(this).hasClass('disabled')){	
			    if($(this).hasClass('checked')){
			    	$(this).children('input').val('');
			    	$(this).removeClass('checked');
			    } else {
			    	$(this).children('input').val($(this).attr('data-value'));
			    	$(this).addClass('checked');
			    }
			}


  			return false;
    	}
    	
    	
    	
    	/* Form Coerce
		-------------------------------------------------------*/
    	function form_coerce_value(e){
    		$field								   		 	   	= $(this);
    		$type								   		 	   	= e.data.type;
    		$value								   		 	   	= $field.val();
    		

    		if($value							   		 	   != ''){
    			$.post(
					'/functions/user.functions.php?function=user_form_coercion',
					{
						type					   		 	   	: $type,
						value 					   		 	   	: $value
					},
					function(data){
						$field.val(data[$type]);
					},
					"json"
				);
    		}
    	}
    	
    	
    	
    	/* Form Radio Foci
		-------------------------------------------------------*/
    	function form_radio_foci(e){
    	
    		if(!$(this).hasClass('disabled')){
    		
			    if(!$(this).hasClass('checked')){
			    
			    	var $input									= $(this).parents('ol:eq(0)').siblings('input');
			    
			    		    	
			    	$input.val($(this).attr('rel'));
			    	$(this).parents('ol:eq(0)').find('a[type="radio"]').removeClass('checked');
			    	$(this).addClass('checked');
			    	
			    	
			    	if($(this).hasClass('other')){
			    	
			    		$input.val('');
			    		$(this).parents('ol:eq(0)').append('<input class="other" type="text" name="'+ $input.attr('name') +'" value=""/>');
			    		
			    	} else {
			    	
			    		$(this).parent().siblings('input.other').hide().remove();
			    		
			    	}
			    }
			    
			}


  			return false;
    	}
    	
    	
    	
    	/* Form Select Foci
		-------------------------------------------------------*/
    	function form_select_foci(e){
    		if(!$(this).parent().hasClass('disabled')){
    			if(e.data.mouse		   		       	   		   == 'click'){
    			
					$.browser.msie					   		    ? $(this).change() : '';
					
    			} else {
    			
    				var $name	   					   		    = $(this).attr('name');
    				var $label	   					   		    = $(this).parent().parent().siblings('label');
					var $value	   					   		    = $(this).val();
					
					var $empty						   			= ($value == 'Select '+$label.attr('title'));
					
										
					if($(this).parent().hasClass('datetime')){
					
					    var $class							= $(this).attr('class');
					    var $input							= $(this).siblings('input[type="hidden"]:eq(0)');
					    var $select	   					   	= $(this).parent().children('em.' + $class);
					    

    				    $value				  		   	   == '' ? $select.text($(this).find('option[value=""]').text()) : $select.text($(this).find('option[value="'+ $value +'"]').text());
    				    
    				    $input.val($(this).parent().parent().find('select:eq(0)').val() + ' ' + $(this).parent().parent().find('select:eq(1)').val() + ':00:00');

					} else {
					
					    var $select	   					   	= $(this).parent().children('em');						
					    
					    
    				    $value				  		   	   == '' ? $select.text($(this).find('option[value=""]').text()) : $select.text($(this).find('option[value="'+ $value +'"]').text());
    				
    				}
    				
    			}   
    		} 		
		}
		
		
		
		
		
    	/* Form Submission
		-------------------------------------------------------*/
		function form_submission(e){
			var $button							   			   	= $(this);

			var $form							   			   	= $button.parents('form:eq(0)');
			var $form_antihack					   			   	= $form.find('input[name="form_antihack"]').val();
			var $form_data						   			   	= $form.serialize();
			var $form_errors									= 0;
			var $form_id						   			   	= $form.find('input[name="form_id"]').val();
			var $form_key						   			   	= $form.find('input[name="form_key"]').val();
			var $form_message					   			   	= "";
			var $form_name						   			   	= $form.attr('id');
			var $form_relay						   			   	= '';

			var $form_fieldset									= $form.children('fieldset');
			var $form_message									= $form.children('#form_message');
			var $form_processing								= $form.children('#form_processing');


			$button.hide();

			$form_fieldset.fade_out();
			$form_message.fade_away();
			
			$form.find(':input').blur();

			setTimeout(function(wait){
				$form_processing.fade_in();
			},500);


			if(!$form_antihack && $form_data && $form_id && $form_key){

				$.post(
					'/functions/user.functions.php?function=user_form_submission',
					{
						form_antihack			   			   	: $form_antihack,
						form_data	 			   			   	: $form_data,
						form_genesis			   			   	: 'Form [ '+ $form_name.toUpperCase() +' ] |' + location.href,
						form_id		 			   			   	: $form_id,
						form_key	 			   			   	: $form_key,
						form_name	 			   			   	: $form_name
					},
					function(data){
						setTimeout(function(wait){
							if(data.decree		   			   == 'pass'){
							
								$form_processing.fade_out();
								
							
								switch($form_name){
								
									case 'sandwich_order'		:	$button.addClass('addition');
									
																	$form.find('#section-1_choose_your_bread a[type="radio"]:eq(0)').click();
																	$form.find('#section-2_choose_your_fillings div a[type="radio"]:eq(0)').click();
																	$form.find('#section-3_choose_your_toppings a[type="checkbox"].checked').click();
																	$form.find('#section-make_it_a_combo a[type="checkbox"].checked').click();
																	$form.find('#section-salads a[type="checkbox"].checked').click();
															
																	$button.addClass('addition').show();
															
																	break;


									default 					:	$form.find('.section div input').attr('disabled','disabled');
																	$form.find('.section div a[type="checkbox"]').addClass('disabled');
																	$form.find('.section div a[type="radio"]').addClass('disabled');
																	$form.find('.section div a[type="select"]').addClass('disabled');

																	$form_processing.fade_out();
									
																	break;
								
								}
							
							} else {
							
								$button.show();
								$form_processing.fade_out();

								$form_fieldset.fade_in();
								
							}


							setTimeout(function(wait){
								$form_fieldset.before('<div id="form_message"><em class="'+ data.decree +'">'+ data.header +'</em><div>'+ data.message +'</div></div>'+ (($form_relay != undefined) ? $form_relay : '')).fade_in();	
							},500);								
						},2000);
					},"json"
				);
				
			} else {
			
				setTimeout(function(){
					$button.show();
					$form_processing.fade_away();

					setTimeout(function(wait){
						$form_fieldset.before('<div id="form_message"><em class="fail">Processing Failed.</em><div><p>Submission suspected as potential Spam Threat. If you feel this is a mistake, refresh &amp; re-submit.</p></div></div>').fade_in();					
					},500);
				},2000);
				
			}
			
			
			return false;
		}
    	
    	
    	
    	
    	
/* 
    Object Extensions
	-----------------------------------------------------------*/
    (function($){

		
		/* Exists
		-------------------------------------------------------*/
    	$.fn.exists                 				= function(){ return ($(this).length > 0); };
    	
    	
    	/* Fade Away
		-------------------------------------------------------*/
    	$.fn.fade_away 								= function(speed,callback){
            return this.each(function(e){
                $(this).fadeOut(speed,function(e){
                    $.browser.msie     				? $(this).get(0).style.removeAttribute('filter') : '';
                    typeof(eval(callback))     	   == 'function' ? eval(callback)() : '';
                    
                    $(this).remove();
                });
            });            
    	};
    	
    	
    	/* Fade In
		-------------------------------------------------------*/
    	$.fn.fade_in 								= function(speed,callback){
            return this.each(function(e){  
                $(this).fadeIn(speed,function(e){
                    $.browser.msie     				? $(this).get(0).style.removeAttribute('filter') : '';
                    typeof(eval(callback)) 	   	   == 'function' ? eval(callback)() : '';
                });
    		});
    	};
    	
    	
    	/* Fade Out
		-------------------------------------------------------*/
    	$.fn.fade_out 								= function(speed,callback){
            return this.each(function(e){
                $(this).fadeOut(speed,function(e){
                    $.browser.msie     				? $(this).get(0).style.removeAttribute('filter') : '';
                    typeof(eval(callback))     	   == 'function' ? eval(callback)() : '';
                });
            });
    	};
    	
    	    
    	/* Has Attribute
		-------------------------------------------------------*/
		$.fn.hasAttr                				= function(attribute){ return (($(this).attr(attribute) !== undefined) && ($(this).attr(attribute).length > 0)); }
        	
    	
    	/* Set Class
		-------------------------------------------------------*/
    	$.fn.set_class              				= function(new_class){ return this.each(function(e){ $(this).attr('class',new_class); }); }


		/* Tag
		-------------------------------------------------------*/
    	$.fn.tag 									= function(lower){ return lower ? this.get(0).tagName.toLowerCase() : this.get(0).tagName; }
    	

	})($);
