var globalfunc = {
	renderLocation: function(){

		if (document.location.href.split("?").length > 1){
			var param2 = document.location.href.split("?")[1];
			if (param2.indexOf('s') == 0){
				alert("Your message has been sent! Thanks for contacting us!");
			}
			else if (param2.indexOf('ns') == 0){
				alert("Your message couldn't be sent. Please try again later.");
			}
		}
		if (document.location.href.split("#").length > 1){
			var param = document.location.href.split("#")[1];
			if (param.indexOf('aboutus') == 0){
				globalfunc.showContent("aboutus", true);
			}
			else if (param.indexOf('services') == 0){
				globalfunc.showContent("services", true);
			}
			else if (param.indexOf('clients') == 0){
				globalfunc.showContent("clients", true);
			}
			else if (param.indexOf('contact') == 0){
				globalfunc.showContent("contact", true);
			}
		}
	},
	validateEmail: function() {
		var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
		var address = document.forms['contactform'].elements['email'].value;
		return reg.test(address);
	},
	trimText : function(textValue){
		return textValue.replace(/^\s+|\s+$/g,"");
	},
	submitValidation : function(){
		var submitForm = true;
		$("form .requestedElem").each(function(){
			if (globalfunc.trimText(this.value) == "" || this.value == this.getAttribute("defvalue")){
				submitForm = false;
			}
		});
		if (!submitForm)
			alert("Please fill al the required fields");
		else{
			submitForm = globalfunc.validateEmail();
			if (!submitForm)
				alert("Please provide a valid email address");
		}
		return submitForm;
	},
	saveFormDefaults: function(){
		$("form input").each(function(){
			var defValue = this.getAttribute("value");
			this.setAttribute("defvalue", defValue);
		});
		$("form select").each(function(){
			var defValue = $(this).find("option:selected")[0].getAttribute("value");
			this.setAttribute("defvalue", defValue);
		});
		$("form textarea").each(function(){
			var defValue = this.value;
			this.setAttribute("defvalue", defValue);
		});
	},
	setFormEvents: function(){
		this.saveFormDefaults();
		$("form input,form textarea").each(function(){
			$(this).focus(function(){
				if (globalfunc.trimText(this.value) == this.getAttribute("defvalue")){
					this.value = "";
				}
			});
			$(this).blur(function(){
				if(globalfunc.trimText(this.value) == ""){
					this.value = this.getAttribute("defvalue");
				}
			});
		});
	},
	setupServices: function() {	
		$("div.services div.serviceItem").each(function(){
			var type = $(this).find(".header h4")[0].textContent;
			/*var desc = $(this).find("p.desc");
			$($(this).find("a.more")[0]).click(function(){
				if(desc.is(':visible') ) {
					desc.hide("slow");
					$(this).text("+more");
				}
				else {
					desc.show("slow");
					$(this).text("-less");
				}
			});*/
			$($(this).find("a.request")[0]).click(function(){
				globalfunc.showContent("contact", true);
				var servicetype = 0;
				if (type.indexOf("Staff Argumentation") == 0){
					servicetype = "1";
				}
				else if (type.indexOf("Website Development") == 0){
					servicetype = "4";
				}
				else if (type.indexOf("Mobile Applications") == 0){
					servicetype = "2";
				}
				else if (type.indexOf("Custom Software") == 0){
					servicetype = "5";
				}
				else if (type.indexOf("Website Design") == 0){
					servicetype = "3";
				}
				else if (type.indexOf("Consulting") == 0){
					servicetype = "6";
				}
				else if (type.indexOf("Maintenance & Support") == 0){
					servicetype = "8";
				}
				else if (type.indexOf("eCommerce") == 0){
					servicetype = "7";
				}
				$("select#service")[0].value = servicetype;
			});

		});
	},
	// The function to show the main content by layer
	showContent: function(layer, animate) {

		$("#background").show();
		$("#footer").hide();
		$("#announcement").hide();

		$("#content").html($("#" + layer + "_html").comments());

		$("#nav").children().each(function() {
			if ($(this).attr("id").search(layer) == -1) {
				$(this).addClass("inactive");
			} else {
				$(this).removeClass("inactive");
			}
		});

		//Starting the pikachoose image slider in case About us
		if (layer.indexOf('aboutus') == 0){
				$("#pikame").PikaChoose({
				auto_play:true,
				show_caption:false,
				show_prev_next:false,
				transition: [0]
			});
		}
		else if (layer.indexOf('services') == 0){
			globalfunc.setupServices();
		}

		if (animate == true) {
			$('html,body').animate({scrollTop: $("#menu").offset().top}, 200, "swing");
		}

		$("a#backToTop").each(function(){
			$(this).click(function(){
				if (animate == true) {
					$('html,body').animate({scrollTop: $("#top").offset().top}, 200, "swing");
				}
			});
		});

		$("a#backToMenu").each(function(){
			$(this).click(function(){
				if (animate == true) {
					$('html,body').animate({scrollTop: $("#menu").offset().top}, 200, "swing");
				}
			});
		});

		// setUpHover();
		setupFullScreen();
		setupImageViewers();
		globalfunc.setupHovers();

		// Load analytics tracking
		setTimeout(function() {
			$("#tracker").attr("src", "/track/" + layer + "/")
		}, 500);

		window.location.hash = layer;

	},
	setupHovers: function() {	
		$(".hoverable").hover(
			function() {$(this).addClass("hover")},
			function() {$(this).removeClass("hover")}
		);

		$(".gallery .thumbnail").hover(
			function() {$(this).children("img.fade").stop().fadeTo(200, 1)},
			function() {$(this).children("img.fade").stop().fadeTo(50, 0)}
		);

		$(".profile").hover(
			function() {$('img.fade', this).stop().fadeTo(200, 1)},
			function() {$('img.fade', this).stop().fadeTo(50, 0)}
		);
		
	}
};



$(document).ready(function() {
	
	// ====================================
	// = Content showing for main buttons =
	// ====================================

	// Set up the on click functions for the different menus
	$("#nav").children().click(function() {
		globalfunc.showContent($(this).attr("id").split("_")[1], true);
	});
	
	// If there is a location has we open the layer with the content and scroll to it,
	// but only if we're not coming from the same url, because then we'd like to end up
	// at the scroll position we were.
	if (window.location.hash && document.referrer != window.location) {
		globalfunc.showContent(window.location.hash.replace("#", ""), false);
	}
	
	// =========================
	// = Preloading of content =
	// =========================

/*	preloadContentDelay = 1000;

	setTimeout(function() {

		preloadNode = $("<div></div>");

		preloadNode.css({
			"position": "absolute", 
			"left": "-100000px", 
			"top": "-100000px", 
			"height": "1px", 
			"width": "1px",
			"overflow": "hidden"})

		preloadNode.append($("#aboutus_html").comments())
		preloadNode.append($("#services_html").comments())
		preloadNode.append($("#clients_html").comments())
		preloadNode.append($("#contact_html").comments())

		$("body").append(preloadNode);

		// setUpHover();

	}, preloadContentDelay);
*/
	//rendering the location
	globalfunc.renderLocation();
	
	// =========================
	// = Main page menu hovers =
	// =========================
	
	globalfunc.setupHovers();
	
	// ===============================
	// = Setting up Services Content =
	// ===============================
});
