Page = {
	init: function(){
	  	Page.setStyles();
		
		Util.externalLinks();
		
		Page.setNavigationHover();
	},
	setStyles: function() {
	
	},
	
	setNavigationHover: function (){
		$('#site #header ul#nav li a').hover(
			function()
			{
				if($(this).attr("name") == "safety")
				{
					return;
				}
				
				$('#sub_' + $(this).attr("name")).css("display", "block");
			}, 
			function()
			{
				$('#sub_' + $(this).attr("name")).css("display", "none");
			}
		);
		
		$('.subnavigation').hover(
			function()
			{
				if($(this).attr("name") == "safety")
				{
					return;
				}
				
				$('#sub_' + $(this).attr("name")).css("display", "block");
			}, 
			function()
			{
				$('#sub_' + $(this).attr("name")).css("display", "none");
			}
		);
	}
};


Gallery= {
	init: function(zoom) {
		$(".galleries .gallery").each(function() {
			var gallery= $(this).attr("id");
			$(".image", $(this)).click(function() {
				$('#' + zoom).attr("src", $(this).attr("id"));
				$('#' + zoom).attr("width", $(this).attr("width"));
				$('#' + zoom).attr("height", $(this).attr("height"));
			});
		});
	}
}

Search = {

	
	init: function()
	{
		/*var t, timeout = 500;
		var searchForm = "#search";
		
		 $(searchForm).blur(function(){
		 	//$('#suggestions').fadeOut();
		 });
		
		$(searchForm).keyup(function()
		{
			if(t)
			{
				clearTimeout(t);
				t = setTimeout(function(){Search.lookup(encodeURIComponent($(searchForm).val()))}, timeout);
			}
			else
			{
				t = setTimeout(function(){Search.lookup(encodeURIComponent($(searchForm).val()))}, timeout);
			}
		});*/
	},
	
	
	lookup: function(inputString) 
	{
		if(inputString.length == 0) {
			$('#suggestions').fadeOut(); // Hide the suggestions box
		} 
		else if(inputString.length >= 3)
		{
			$.post("/search/" + inputString, function(data) { // Do an AJAX call
				$('#suggestions').fadeIn(); // Show the suggestions box
				$('#suggestions').html(data); // Fill the suggestions box
			});
		}
	}
};

Jobs = {
	init: function()
	{

		$('.more').click(function(){
			$(this).hide();
			$(this).parent().find(".less").show();
			$(this).parent().find(".less").css("display", "block");
			
		
			$(this).parent().find('#fulltext').show(500);				
		});
		
		$('.less').click(function(){
			$(this).hide();
			$(this).parent().find(".more").show();
			
			$(this).parent().find('#fulltext').hide(500);
		});
	}
};

Store = 
{
	init: function(){
		$('.toform').click(function(){
			$('.contact_form_div').hide();
			$('#' + $(this).attr("name")).show(500);
		});
	}
};

Util= {
	getWindowSize: function(){
		var xScroll, yScroll;

		if (window.innerHeight && window.scrollMaxY) {
			xScroll = window.innerWidth + window.scrollMaxX;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight) {
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else if(document.body) { 
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}

		var windowWidth, windowHeight;
		if (self.innerHeight) {
			if(document.documentElement.clientWidth){
				windowWidth = document.documentElement.clientWidth;
			} else {
				windowWidth = self.innerWidth;
			}
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) {
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) {
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}

		if(yScroll < windowHeight) {
			pageHeight = windowHeight;
		} else {
			pageHeight = yScroll;
		}

		if(xScroll < windowWidth) {
			pageWidth = xScroll;
		} else {
			pageWidth = windowWidth;
		}
		var arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
		return arrayPageSize;
	},
	externalLinks: function(){
		 $("a").each(function(){
		 	if(		 	  
		 	   $(this).attr("href") != "" &&	
		 	   $(this).attr("href").indexOf(window.location.hostname) < 0 &&
			   $(this).attr("href").indexOf("http://") >= 0 	
			   ||  $(this).attr("rel") == "external"   
		 	 ){
		 		$(this).attr("target", "_blank");
		 	 }
		 });
	},
    validateForm: function(id) {    	
		var result= true;				
		$("#"+id + " input, #"+id + " textarea, #"+id + " input.checkbox, #"+id + " .select").each(function() {
			if($(this).attr("class") && $(this).attr("class").match("required")) {
			    $(this).val($(this).val().replace(/^\s+|\s+$/g, ""));
				var itemresult= Util.validate($(this).attr("id"));
				result= result && itemresult;
			}
		});		
		if(!result){
			$("#error .message").css("visibility", "visible");
		}
		return result;		
	},
	validate: function(id) {
		var result= true;
		var obj= $("#"+id);		
		var commands= $("#"+id).attr("class").split(" ");
		for(var i=0; i<commands.length; i++) {
			var command= commands[i].replace(/[0-9]/g, "");			
			switch(command) {
				case("minlength"):
					var length= parseInt(commands[i].replace(/minlength/g, ""));
					result= result && (obj.val().length>=length);
					break;
				case("maxlength"):
					var length= parseInt(commands[i]);
					result= result && (obj.val().length<=length);
					break;
				case("email"):
					result = result && ( obj.get(0).value.match(/\S@\S.\S{2,}/)!=null );
					break;
				case("checkbox"):
					if( $("#"+id+":checked").length <= 0 ){
						result = result && false;
						$("#"+id).parent().children(".checkbox .info").addClass("error");
					}					
					break;
				case("select"):
					result = result && !( $("#"+id).val() == "---" );					
					break;
				default:
					result= result && (obj.val().length!=0);
					break;
			}
		}						
		if(!result) {
			obj.addClass("error");
		} else {
			obj.removeClass("error");			
		}
		return result;
	},
	
	pricerangeChanged: function(sender)
	{
		window.location.href=$(sender).find(":selected").attr("value");
	},
	
	limitChanged: function(sender)
	{
		window.location.href=$(sender).find(":selected").attr("value");
	}
};


