/*=====================
prenaud@riester.com
November 2011
=====================*/


$(document).ready(function(){

	if ($('body').is('.home')){
		homepageSlider();
	} else {
		pageOptions();
	}; //if-else home

	if ($('img.vignette').length){
		vignetteHelper();
	}

	if ($('.contentproper.twosplit').length){
		setTimeout(function() {
			adjustColumns();
		}, 1000);
		$(window).resize(function(){
		setTimeout(function() {
			adjustColumns();
		}, 1000);
		}); //window.resize
	}

	menuToggle();
	formHelper();
	listHelper();

	printHelper();

	emailHelper();
	
    if ($.browser.msie) {
        ieImages();
    }

	
}); //document.ready


/*==( ^ Homepage Slider )======================================================*/

function homepageSlider(){
	setSliderSizes();
	$(window).resize(function(){
		setSliderSizes();
	}); //window resize
	$('.hero').append('<div class="leftright"><a href="#" class="goleft">Previous</a><a href="#" class="goright">Next</a></div>');
	$('#worldcontainer').prepend('<ul class="panelnav"></ul>');
	$('.panelcontainer').children('li').each(function(i){
	$('.panelnav').append('<li><a href="#">Panel '+i+'</a></li>');
	}); //each
	
	$('.panelcontainer').children('li:first-child').addClass('active');
	$('.panelnav').children('li:first-child').addClass('active');
	
	setSliderClicks();

	getLoopy();

}; //homepageslider

function getLoopy(){
	looper = setInterval(function(){
		$('.goright').click();
	}, 5000); //looper
}; //getLoopy

function setSliderSizes(){
	$('.panelcontainer').children('.panel').css({'width': $(window).width()});
}; //setSliderSizes

function setSliderClicks(){

	$('.goright').click(function(){
		headRightward();
		return false;
	}); //click

	$('.goleft').click(function(){
		headLeftward();
		return false;
	}); //click
	
	
	$('.panelnav').children('li').click(function(){
    	if ($(this).not('.active')) {
        	clearInterval(looper);
        	whichdidiclick = $('.panelnav').children('li').index($(this));
        	$('.panelnav').children('li.active').removeClass('active');
        	$(this).addClass('active');
        	$('.panelcontainer').children('li.active').removeClass('active');
        	$('.panelcontainer').children('li').eq(whichdidiclick).addClass('active');
        	$('.panelcontainer').stop().animate({'left': -$('.panelcontainer').children('li.active').position().left}, {duration: 1500, easing: 'easeOutExpo'});
        	getLoopy();
    	}; // if not active
    	return false;
	}); //click


}; //setSliderClicks

function headRightward() {
		clearInterval(looper);
		whichisactive = $('.panelcontainer').children('li').index($('.active'));
		howmanyarethere = $('.panelcontainer').children('li').length-1;
		if (whichisactive == howmanyarethere) {
			$('.panelcontainer').children('li.active').removeClass('active').siblings('li').eq(0).addClass('active');
			$('.panelnav').children('li.active').removeClass('active').siblings('li').eq(0).addClass('active');
		} else {
			$('.panelcontainer').children('li.active').removeClass('active').next('li').addClass('active');
			$('.panelnav').children('li.active').removeClass('active').next('li').addClass('active');
		}; //if active is last child
		whereami = $('.panelcontainer').children('li').index($('.active'));
		$('.panelcontainer').stop().animate({'left': -$('.panelcontainer').children('li').eq(whereami).position().left}, {duration: 1500, easing: 'easeOutExpo'});
		getLoopy();
}; //headRightward

function headLeftward() {
		clearInterval(looper);
		whichisactive = $('.panelcontainer').children('li').index($('.active'));
		if (whichisactive == 0) {
			$('.panelcontainer').children('li.active').removeClass('active').siblings('li:last-child').addClass('active');
			$('.panelnav').children('li.active').removeClass('active').siblings('li:last-child').addClass('active');
		} else {
			$('.panelcontainer').children('li.active').removeClass('active').prev('li').addClass('active');
			$('.panelnav').children('li.active').removeClass('active').prev('li').addClass('active');
		}; //if active is last child
		whereami = $('.panelcontainer').children('li').index($('.active'));
		$('.panelcontainer').stop().animate({'left': -$('.panelcontainer').children('li').eq(whereami).position().left}, {duration: 1500, easing: 'easeOutExpo'});
		getLoopy();
}; //headRightward



/*==( ^ Forcing Equal-Height Columns )======================================================*/

function adjustColumns(){

//  $('.vignetted').css({'height': $('.vignetted').height()+$('.vignetted').find('img.vignette').height()});
//	$('.vignetted').css({'paddingBottom': $('.vignetted').find('img.vignette').height()});

	if ($(window).width() > 992){
		$('img.vignette').css({'width': '217px'});
		minorheight = $('.contentproper .minor').height();
		majorheight = $('.contentproper .major').height();
		if (minorheight > majorheight) {
			$('.contentproper .major').children('.inner').css({'min-height': minorheight-30});
			$('.contentproper .major').css({'min-height': minorheight});
		} else if (minorheight < majorheight) {
			$('.contentproper .minor').children('.inner').css({'min-height': majorheight-20});
		}; //ifelse
		if ($('img.vignette').length){
			vignettebottom = $('img.vignette').position().top;
			vignetteheight = $('img.vignette').height();
			vignettebottom = vignettebottom+vignetteheight;
			minorinnerheight = $('.minor .inner').height()+20;
			thedifference = minorinnerheight-vignettebottom;
			$('img.vignette').css({'bottom': -thedifference});
		};
	} else {
		$('.contentproper .major').children('.inner').css({'min-height': '0px'});
		$('.contentproper .minor').children('.inner').css({'min-height': '0px'});
		$('img.vignette').css({'width': $('.minor').children('.inner').width()+20});
		$('img.vignette').css({'bottom': '-11px'});
	}; //if window.width
	
	
}; //adjustColumns




/*==( ^ Fancybox - lightbox on menu, etc. )======================================================*/

function menuToggle(){
	$('li.menutoggle').children('a').fancybox({
		'overlayColor'		: '#000'
	}); //.fancybox()
}; //menuToggle


/*==( ^ Page options - print and email links added to non-home pages )======================================================*/

function pageOptions(){
    $('.major').children('.inner').children('.innerinner').prepend('<div class="pageoptions clearfix"><a href="#" class="print">Print</a></div>');

if ($('#emailForm').length){
	$('.pageoptions').append('<a href="#emailForm" class="email">Email</a>');
}; //if emailForm exists

}; //pageOptions



/*==( ^ Focus/Blur helper for form input fields )======================================================*/

function formHelper(){
    $("input[type=text]").focus(function() {
        if( this.value == this.defaultValue ) {
            this.value = "";
        }
    }).blur(function() {
        if( !this.value.length ) {
            this.value = this.defaultValue;
        }
    }); //focusblur
}; //formHelper


/*==( ^ List helper style-additions: post-render )======================================================*/

function listHelper(){
    $('.list').children('li').children('a').children('img').parent('a').siblings('h5').css({'paddingTop': '30px'});

    if ($('body').is('.recipes')) {
        $('ul.list').children('li:odd').find('img').removeClass('tiltleft').addClass('tiltright');
        $('ul.list').children('li:even').find('img').removeClass('tiltright').addClass('tiltleft');
    }; //if recipes
    
    
}; //listHelper


/*==( ^ Vignette Helper )======================================================*/

function vignetteHelper(){
	$('img.vignette').prev().addClass('prevignette');
	$('.minor .inner').addClass('vignetted');
}; //vignetteHelper


function printHelper() {
	$('a.print').click(function(){
		window.print();
	}); //click
}; //printHelper


/*==( ^ Email Helper )======================================================*/



function emailHelper(){

thecontent = $('#emailForm');
	$('.pageoptions .email').fancybox({
		'overlayColor'		: '#000',
		'content'			: thecontent
	}); //.fancybox()
	
	$('.emailClose').bind('click', function() {
		$('#emailOverlay').hide();
		return false;
	});
	
	$('#email-product-info').submit(function() {
		var formdata = $('#email-product-info').serialize();
		$.ajax({
			type: "POST",
			url: "/products/email/",
			data: formdata,
			success: function(msg){
				if(msg == 1){
					$("#message").html('<span class="thankyou">Thank you! An email has been successfully sent.</span>');			
				} else {
					$("#message").html(msg);
				}
			}
		});
		return false;
	});
	$('#email-recipe-info').submit(function() {
		var formdata = $('#email-recipe-info').serialize();
		$.ajax({
			type: "POST",
			url: "/recipes/email/",
			data: formdata,
			success: function(msg){
				if(msg == 1){
					$("#message").html('<span class="thankyou">Thank you! An email has been successfully sent.</span>');			
				} else {
					$("#message").html(msg);
				}
			}
		});
		return false;
	});
}; //emailHelper



/*==( ^ IE Images )======================================================*/

function ieImages(){
    $('.major').find('img').each(function(){
        $(this).css({'height': 'auto', 'width': 'auto'});
    }); //each img
}; //vignetteHelper














