// When DOM is loaded
$(document).ready(function() {
	
/* 	Navigation Submenu Popup */
	$('ul.mainmenu li').bind('mouseenter', function() {
  		
  		$(this).find(".submenu").show();
	});

	$('ul.mainmenu li').bind('mouseleave', function() {
  		
  		$(this).find(".submenu").hide();
	});


/* 	Product Preview Overlay */
	$('.thumbnail').bind('click', function() {
	
		var thumbnailSource;
		var previewSource;
		var element;
		var previewText;
		
		element = $(this).get(0);
  		thumbnailSource = element.getAttribute("src");
  		
  		indexNo = $.inArray(thumbnailSource, filenameArray);
  		
  		showPreview(filenameArray[indexNo], altTextArray[indexNo]);
	});
	
	$('#close-preview').bind('click', function() {
	
		$('#overlay').hide();
	});
	
	$('#prev-image').bind('click', function() {
	
		showPrevImage();
	});
	
	$('#next-image').bind('click', function() {
	
		showNextImage();
	});
	
	$(window).keydown(function(event) {
	
		if ($('#overlay').length && $('#overlay').css('display') != 'none') {
	
			switch (event.keyCode) {
			
	/* 			left key */
				case 37:
				
					showPrevImage();
					break;
			
	/* 			right key */
				case 39:
				
					showNextImage();
					break;
	
	/* 			Esc button		 */
				case 27:
				
					$('#overlay').hide();
					break;
			}
			
		} else if ($('#overlay-contact').css('display') != 'none') {
		
/* 			Esc button */
			if (event.keyCode == 27) {
				$('#overlay-contact').hide();
			}
		}
	});
	
	
/* 	Send recipe overlay */
	$('.send-recipe').bind('click', function() {
	
		showContactForm();
	});
	
	
/* 	Close contact form overlay */
	$('#close-contact-form').bind('click', function() {
	
		$('#overlay-contact').hide();
	});
	
	
/* 	Show print window */
	$('.print').bind('click', function() {
	
		window.print();
	});
	
	browserWindowSize();
	
/* 	#aligner was hidden to prevent shitty loading effects */
	$('#aligner').show();
});

/* ******************************** */
	function allowAccess(lang) {
	
		createCookie("accessAllowed", lang);
		$('#verification-overlay').hide();
	}

	function createCookie(name,value,days) {	
	    if (days) {
	        var date = new Date();
	        date.setTime(date.getTime()+(days*24*60*60*1000));
	        var expires = "; expires="+date.toGMTString();
	    }
	    else var expires = "";
	    document.cookie = name+"="+value+expires+"; path=/";
	}
	
	function readCookie(name) {
	    var nameEQ = name + "=";
	    var ca = document.cookie.split(';');
	    for(var i=0;i < ca.length;i++) {
	        var c = ca[i];
	        while (c.charAt(0)==' ') c = c.substring(1,c.length);
	        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	    }
	    return null;
	}
	
	function eraseCookie(name) {
	    createCookie(name,"",-1);
	}
  		
	function showPreview(thumbnailSource, previewText) {
  		
		var element;	
  		var previewSource = thumbnailSource.replace("thumbnails/", "");
		
		// To position the overlay in center of page
		$('#overlay').css("top", $('#wrapper').height() / 2 + $('#header').height());
		
		// To position the overlay in center of page
		$('#overlay').css("left", $('#aligner').width() / 2);
		
		// Set source of the big preview file on <img> element
		element = $('#preview-image').get(0);
		element.setAttribute("src", previewSource);
		
		// Show alt text from thumbnail image in <p>
		$('#preview-text').html(previewText);
		
		// Show preview overlay
		$('#overlay').show();
	
	}
	
	function showPrevImage() {
	
		if (indexNo > 0) {
			indexNo--;
		}
	
		showPreview(filenameArray[indexNo], altTextArray[indexNo]);
	}
	
	function showNextImage() {
	
		if (indexNo < (filenameArray.length - 1)) {
			indexNo++;
		}
	
		showPreview(filenameArray[indexNo], altTextArray[indexNo]);
	}
	
	function showContactForm() {
		
		// To position the overlay in center of page
		$('#overlay-contact').css("top", $('#wrapper').height() / 2 + $('#header').height());
		
		// To position the overlay in center of page
		$('#overlay-contact').css("left", $('#aligner').width() / 2);
		
		// Show preview overlay
		$('#overlay-contact').show();
	}

$(window).resize(browserWindowSize);
