
var SalumeCommon = {};
SalumeCommon.selected; 
SalumeCommon.sending = false;
SalumeCommon.deeplinkPost;

SalumeCommon.setupForm = function () {
	
	var validator = $('#newletterForm').validate({
		submitHandler: function (form) {
		var emailString = $('#email').val(); 
		$('#email').val("Sending...");
		SalumeCommon.sending = true;
			$.post("/wp-content/themes/salume/custom-php/store-address.php", {email:emailString, ajax:true}, function(data){
            	if(data.indexOf('Error:') != -1){
            		$('#email').addClass("error");
            		if(data.indexOf('already subscribed') != -1){
            			$('#email').val("Duplicate email");
            		}else {
            			$('#email').val("Wrong format");
            		}
            	}else{
            		$('#email').removeClass("error");
            		$('#email').val("Signup successful!");
            	}
            	SalumeCommon.sending = false;
            });
		},

		rules: {
			email: {required: true, email:true}
		},

		messages: {
			email: ''
		},

		onfocusout: false,

		invalidHandler: function(form, validator) {
		      var errors = validator.numberOfInvalids();
		      if (errors) {
		    	  $('#email').val('Wrong format');
		      }else{
		    	  $('#email').val('Sending...');
		      }
		}

	});
	
	$('#btn').bind('mouseup', function(){
		if(SalumeCommon.sending == false){
			$('#newletterForm').submit(function() {
				return false;
			});			
		}
    });
    
    //bg image
  	//var FullscreenrOptions = {  width: 1280, height: 800, bgID: '#bgimg' };
	// This will activate the full screen background!
	//jQuery.fn.fullscreenr(FullscreenrOptions);
	
}

//check for ajax deeplink 
SalumeCommon.checkForAjaxState = function(){
	var addressUrl = new String(window.location);
	var pos = addressUrl.indexOf("#");
	if(pos != -1){
		SalumeCommon.deeplinkPost = addressUrl.slice(pos+1);
		//find a tag in menu
		var aTag = $('a[href="/paninipedia/'+SalumeCommon.deeplinkPost+'"]'); 
		//if not found reset the menu
		if(aTag.attr('href') != undefined){
			$('.page-post').css('visibility', 'hidden');
			//$('.Scrollbar-Track').css('visibility', 'hidden');
			//$('.Scrollbar-Handle').css('visibility', 'hidden');
			//check if its ingredients / panini link
			if(aTag.parent().parent().hasClass('menu_subnav')){
				SalumeCommon.loadIngredientPage(aTag);
			}else{
				SalumeCommon.loadPaniniPage(aTag);
			}
		}else {
			//SalumeCommon.setAjaxState('');
		}
	}else{	
	//check for page deeplink and perform redirect to ajax page if so. 
	pos = addressUrl.indexOf("/paninipedia/");
	if(pos != -1){
		var rest = addressUrl.substr(pos+13);  //lastIndexOf("/");
		if(rest.length > 0 ){
			if(rest.lastIndexOf("/") != -1){
				rest = rest.substr(0, rest.length -1);
			}
			$('.page-post').css('visibility', 'hidden');
			//$('.Scrollbar-Track').css('visibility', 'hidden');
			//$('.Scrollbar-Handle').css('visibility', 'hidden');
			window.location = "/paninipedia/#"+rest;

		}
	}
	}
	
	/*
	Ugly hack
	*/
	var newsPos = addressUrl.indexOf("/news/");
	if(newsPos != -1){
		$.backstretch("/wp-content/themes/salume/img/Latest-news.jpg", {speed: 150});
	}

	
}

//update addressbar with ajax state
SalumeCommon.setAjaxState = function(postSlug) {
	if("#"+postSlug != window.location.hash){
		window.location.hash = postSlug;
	}
}

SalumeCommon.setupMenu = function() {
	//----------------
  	// AJAX Accordion menu
  	//----------------
  	
  	//enable accordion menu
  	$('#paninipedia-menu-top').initMenu();
  	
  	//calculate height of submenu foldout.  
  	$('#paninipedia-menu-top li li').mouseover(function(){
  		var size = 20*$(this).find('ul li').size() + 20;
  		$(this).find('ul').css('height', size+'px');
  	});
  	
  	
  	
	//add event listener on individual ingredients. 
  	$('#paninipedia-menu-top ul ul').children('li').mouseup(function(){
  		var link = $(this).find('a');
  		var href = link.attr('href');
  		SalumeCommon.selected = $(this).parent().parent().find('a:first');  		
  		var slug = href.substr(href.lastIndexOf('/')+1);
  		SalumeCommon.setAjaxState(slug);
  	});
  	
  	//add event listener on individual paninis. 
  	$('.panini-container').children('li').mouseup(function(){
  		var link = $(this).find('a');
  		var href = link.attr('href');
  		var slug = href.substr(href.lastIndexOf('/')+1);
  		SalumeCommon.setAjaxState(slug);
  	});
  	
  	//hide scrollbar when animating menu
  	//this is also done from menu.js on animation
  	$('.menu-ingredients').mousedown(function(){
  		$('#scrollbar-container-menu').css('visibility', 'hidden');
  		$('#scrollbar-container-menu .Scrollbar-Handle').css('visibility', 'hidden');
  	});
  	
  	$('#paninipedia-menu-top > ul').remove();
  	
}

SalumeCommon.setupScrollbarSidebar = function () {
	var scrollerMenu  = new jsScroller(document.getElementById("scroller-menu"), 145, 203, 'panini-container');
  	var scrollbarMenu = new jsScrollbar(document.getElementById("scrollbar-container-menu"), scrollerMenu, true);
}

SalumeCommon.setupScrollbarEntry = function () {
	var scrollerEntry  = new jsScroller(document.getElementById("scroller-entry"), 400, 200, 'entry');
  	var scrollbarEntry = new jsScrollbar(document.getElementById("Scrollbar-Container"), scrollerEntry, true);
}

SalumeCommon.setupScrollbarPaninipedia = function () {
  	var scroller  = new jsScroller(document.getElementById("scroller"), 400, 200, 'paninipedia-entry');
  	var scrollbar = new jsScrollbar(document.getElementById("Scrollbar-Container"), scroller, true);
}

//events attached after HTML snippet is loaded
SalumeCommon.attachRelatedIngredientsAjaxEvents = function () {
	$('#ingredients').find('a').mouseup(function(){
		SalumeCommon.loadIngredientPage($(this));
  	});  	
}

//events attached after HTML snippet is loaded
SalumeCommon.attachRelatedPaniniAjaxEvents = function () {
	$('#ingredients').find('a').mouseup(function(){
		SalumeCommon.loadPaniniPage($(this));
	});
}

SalumeCommon.loadIngredientPage = function(ajaxLink) {
  		var href = $(ajaxLink).attr('href');
  		$.post(href+"/?isAjax=true", function(data){
  			$("#ingredients").html("");
  			$(".page-post").html($(data).find('.page-post').html());
  			$("#ingredients").html($(data).find('#ingredients').html());
  			$('.page-post').css('visibility', 'visible');

  			$('.menu-item-ingredients').click();
  			
  			SalumeCommon.setupScrollbarPaninipedia();
  			SalumeCommon.attachRelatedPaniniAjaxEvents();
  			
  			if(SalumeCommon.selected != null){
  				SalumeCommon.selected.removeClass('selected');
  			}
  			
  			var aTag = $('a[href="'+href+'"]'); 
  			var parentTag = aTag.parent().parent().parent().find('a:first');
  			parentTag.addClass('selected');
   			SalumeCommon.selected = parentTag; 
  		
  			var slug = href.substr(href.lastIndexOf('/')+1);
  			//SalumeCommon.setAjaxState(slug);
  			$("#backstretch img").remove();
  			$.backstretch($("#imageUrl").val(), {speed: 150});
            
            document.title = $('#headerTitle').val() + " | Salumè";

            addthis.button(".addthis_button_compact", {}, {url: "http://salumenewyork.com/paninipedia/"+slug, title: document.title});
  		});
}

SalumeCommon.loadPaniniPage = function(ajaxLink) {
	var href = $(ajaxLink).attr('href');
  		$.post(href+"/?isAjax=true", function(data){
  			$("#ingredients").html("");
  			//set data
  			$(".page-post").html($(data).find('.page-post').html());
  			$("#ingredients").html($(data).find('#ingredients').html());
  			$('.page-post').css('visibility', 'visible');
  			
  			//open menu
  			$('.menu-item-panini').click();
  			
  			SalumeCommon.setupScrollbarPaninipedia();
  			SalumeCommon.attachRelatedIngredientsAjaxEvents();
  			
  			if(SalumeCommon.selected != null){
  				SalumeCommon.selected.removeClass('selected');
  			}
  			
  			//find loaded a tag
  			var aTag = $('a[href="'+href+'"]'); 
  			aTag.addClass('selected');
  			SalumeCommon.selected = aTag;
  			
  			//update addressbar
  			var slug = href.substr(href.lastIndexOf('/')+1);
  			//SalumeCommon.setAjaxState(slug);
  			$("#backstretch img").remove();
  			$.backstretch($("#imageUrl").val(), {speed: 150});
            
            document.title = $('#headerTitle').val() + " | Salumè";
            
            addthis.button(".addthis_button_compact", {}, {url: "http://salumenewyork.com/paninipedia/"+slug, title: document.title});
  			
  		});
}

//DOM-Ready Event Handler _____________________________________________________

$(document).ready(function () {
	setBackground();
	SalumeCommon.checkForAjaxState();

	SalumeCommon.setupForm();
	SalumeCommon.setupMenu();
	
	if($('#Scrollbar-Container')){
		var page = top.location.href;
		var exist = (page.lastIndexOf('paninipedia'));
	
		if(exist != -1){
			SalumeCommon.setupScrollbarPaninipedia();
			SalumeCommon.setupScrollbarSidebar();
		}else{
			SalumeCommon.setupScrollbarEntry();
		}
	}
	
	$(window).hashchange(function() {
		SalumeCommon.checkForAjaxState(); 
	});
	
});


