var SW = window.SW ||
{};

if (!SW.Sha)
{ SW.Sha = {}; }

SW.Sha.serverID = (function()
{
	var serverControls = {};

	function registerServerControlID(serverId, clientId)
	{
		serverControls[serverId] = clientId;
	}

	return {
		serverControls: serverControls,
		registerServerControlID: registerServerControlID
	};
})();

SW.Sha.FormCheck = (function($)
{
	$(document).ready(function()
	{

		//Blink text (all browsers)
		setInterval(function()
		{
			var blink = $(".blink").each(function()
			{
				var state = $(this).css("visibility") == "hidden" ? "visible" : "hidden";
				$(this).css("visibility", state);
			})
		}, 500);

		//delay for alert few seconds top allow image load
		if ($('.main_image_container')) {
			window.setTimeout(function showMainIamges()
			{
				$('.main_image_container').removeClass("hidden");
			}, 7000);
		}

		$('#submitform').click(function()
		{

			var tn = $('#title').val();
			var fn = $('#firstname').val();
			var ln = $('#lastname').val() + " " + $('#lastname2').val();
			var c = $('#country').val();
			var e = $('#email').val();
			var tel = $('#telephone').val();
			var q = $('#query').val();
			var t = $('#type').val();

			if (fn.length > 0 && ln.length > 0 && e.length > 0) {
				//check email
				var filter = /^.+@.+\..{2,3}$/
				if (filter.test(e)) { //do nothing
				}
				else {
					alert("Please input a valid email address!")
					return false;
				}
				if ($("#terms_checkbox").size() > 0) {
					if (!$("#terms_checkbox").is(":checked")) {
						return false;
					}
				}
				var location = "/sendmail.aspx?title=" + tn + "&firstname=" + fn + "&lastname=" + ln + "&country=" + c + "&email=" + e + "&telephone=" + tel + "&query=" + q + "&type=" + t;
				window.location.href = "http://" + window.location.host + location;
				return false;
			}
			else {
				alert("Please give a Firtsname, Lastname and Email.");
				return false;
			}
		});

		//image download function
		var documentheight = $(window).height();

		$(".page_overlay").height(documentheight);

		//image download function
		var documentheight = $(document).height();
		$(".page_overlay").height(documentheight);

		$("#imagedownload a ").click(function(e)
		{
			var width = 0;
			var height = 0;
			$("#highres_image, .page_overlay ").css("visibility", "visible");
			var src = $(this).prev().children("img").attr("src");
			var element = src.split('/').pop();
			var loaction = "http://cms5.jhadmin.net/pictures/highresolution/" + element;
			$(".path").val(loaction);
			var img = new Image();
			$(img).load(function()
			{
				$(this).hide();
				$('#highres_image').removeClass('loading');
				$('#highres_image div').append(this);
				$("#highres_image div, #highres_image h2").css("visibility", "visible");
				$("#highres_image .dimension").html(element + " (" + $("#highres_image img").width() + " x " + $("#highres_image img").height() + ")");

				width = Math.round($("#highres_image img").width() / 3.5);
				height = Math.round($("#highres_image img").height() / 3.5);
				$("#highres_image img").height(height);
				$("#highres_image img, #highres_image h2").width(width);
				$(this).fadeIn();
			}).error(function()
			{
				// notify the user that the image could not be loaded
			}).attr('src', loaction);
			e.preventDefault();
		});

		$("#highres_image h2 #close_btn").click(function()
		{
			$(".page_overlay, #highres_image h2, #highres_image, #highres_image div").css("visibility", "hidden");
			$('#highres_image').addClass('loading');
			$('#highres_image div').empty();
		});
	});
})(jQuery);

SW.Sha.PageSlides = (function($)
{

	var slideshowEngine;
	var config = {
		quoteFadeInterval: 6000,
		quoteFadeSpeed: 500
	};

	function init()
	{
		var currentLanguage = $("#current_language").val();
		if (currentLanguage == null || currentLanguage == undefined || currentLanguage == "") {
			currentLanguage = "en";
		}
		$(".links .checkoutDate").datepicker({
			dateFormat: 'yy/mm/dd',
			regional: currentLanguage
		});

		$("x.log-container ul .information ").fadeTo("slow", 0.33, togglefade);
		function togglefade()
		{
			$("x.log-container ul .information ").fadeTo("slow", 0.99);
			$("x.log-container ul .information ").fadeTo("slow", 0.33, togglefade);
		}
		//picture slider	
		$('#slide-images').cycle({
			fx: 'fade',
			speed: 500,
			timeout: 4000,
			next: '#next',
			prev: '#prev',
			pause: 1
		});
		//add active class to the li of the selected link
		$("#first_navigation li a.active ").parent().addClass("active");

		//show arrow for anly those with third navigation / open close navigation commented this out in the next line
		//$("#second_navigation li .thirdlevel_nav").siblings('.togglebtn').show();
		$("#second_navigation .secondnavigation-link.active").siblings('.togglebtn').show();

		//hides the toggle arrow on the first link on eacg page which are for sections
		$("#second_navigation ul li div:first").hide();


		//by default all third vav is hidden via css - the first statement is to show third nav when on second level and the second statement  is to show the third nav of the current if you are on a third level page only
		$("#second_navigation .secondnavigation-link.active").next().show();
		$("#second_navigation ul .thirdlevel_nav .active").parent().parent().parent().show();


		$('#second_navigation li .togglebtn').click(function(e)
		{
			$(this).siblings('.thirdlevel_nav').toggle("slow");
		});

		$("#second_navigation li .togglebtn").toggle(function()
		{
			$(this).addClass("active");
		}, function()
		{
			$(this).removeClass("active");
		});

		//show/hide display board what am I looking for
		//$("#logo .show-board").click(function(e){
		//	$('.displayboardpanel .displayboard').show();
		//	 e.preventDefault();
		//});

		$(".displayboardpanel .displayboard .close").click(function(e)
		{
			$('.displayboardpanel .displayboard').hide();
			e.preventDefault();
		});
		//video
		$(".slideshow-nav #video").click(function(e)
		{
			$('.main_image_container .video_container').show();
			e.preventDefault();
		});

		$(".main_image_container .video_container .close").click(function()
		{
			$('.main_image_container .video_container').hide();
		});

		//tab  control
		var tabsize = $(".itemlist > li").size();
		if (tabsize > 1) {
			$(".itemlist .clickable").next(".description").hide();
			$('.itemlist .clickable').click(function()
			{
				$(this).next(".description").toggle();
			});

			$(".itemlist .clickable").toggle(function()
			{
				$(this).addClass("active");
			}, function()
			{
				$(this).removeClass("active");
			});
		}


		//hide the cycle navigation buttons if there is only one image
		var imagenumber = $("#slide-images > img").size();
		if (imagenumber == 1) {
			$(".slidesnow-nav-container").hide();
		}

		var cancelhideGallery = window.setTimeout(function hideGallery()
		{
			$("#main-images .imageoverlay,  #quotations").show("slide", { direction: "down" }, 1000);
		}, 4000);

		// slide engine
		slideshowEngine = new imageslideshowEngine();
		slideshowEngine.images = $('#main-images > img ').get();
		slideshowEngine.init(config.quoteFadeInterval, config.quoteFadeSpeed);

		slideshowEngine = new imageslideshowEngine();
		slideshowEngine.images = $('#quotations > div').get();
		slideshowEngine.init(config.quoteFadeInterval, config.quoteFadeSpeed);

		slideshowEngine = new imageslideshowEngine();
		slideshowEngine.images = $('#main-images .imagecontainer > img').get();
		slideshowEngine.init(config.quoteFadeInterval, config.quoteFadeSpeed);

		slideshowEngine = new imageslideshowEngine();
		slideshowEngine.images = $('#main-images > div.caption').get();
		slideshowEngine.init(config.quoteFadeInterval, config.quoteFadeSpeed);
	}

	return {
		init: init
	};

})(jQuery);
jQuery(SW.Sha.PageSlides.init);
