$(document).ready(function(){
	
	// **************************************************************************************************************************
	// GENERAL ACTIONS
	
	// external links
	$("a[rel='external']").click(function() {
		return !window.open($(this).attr("href"));
	});
	$('.hide-with-js').hide(); // use this class to hide the message "please open this link in new window"
	
	// fade in messages
	$('.successMsg').hide();
	$('.successMsg').fadeIn(1000);
	$('.errorMsg div').hide();
	$('.errorMsg div').fadeIn(1000);
	$('.warningMsg').hide();
	$('.warningMsg').fadeIn(1000);
	
	// non-clickable links
	$(".return-false").click( function() {
		return false;
	});
	
	$(".but-close-dialog").click(function() {
		$.unblockUI();
	});
	
	// JUMP MENU
	$("select.jump-menu").change(function(x){
		var url = $("option:selected", this).attr("title");
		if (url.length) {
			window.location.href = url;
		}
	});
	
	$("select.per-page-selector").change(function(x){
		var id = $("option:selected", this).attr("title");
		$("#form-display-filter" + id).submit();
		
	});
	
	
	// clear inputs
	$("#projects input, #projects textarea, #contact-form input, #contact-form textarea").each(function(i){
		var inputValue = $(this).val();
		var inputTitle = $(this).attr("title");
		
		if (inputValue=="" || inputValue==inputTitle) {
			$(this).focus(function(){ 
				if ($(this).val()==inputValue) {
					$(this).val("");
				}
			});
			$(this).blur(function(){ 
				if ($(this).val()=="") {
					$(this).val(inputValue);
				}
			});
		}
		
	});
	
	
	// Header error message
	$(".errorMsg-overlay, .errorMsg-after").hide();
	$(".errorMsg-overlay, .errorMsg-after").fadeIn(300, function(){ 
		var elem = $(this);
		setTimeout(function(){
			$(elem).fadeOut(300);
		}, 1200);
	});
	
	// END GENERAL ACTIONS
	// **************************************************************************************************************************
	
	
	
	// Equal heights wrapper-left, wrapper-right
	//setTimeout(calculateHeights, 0);
	
	// Font replacement
	replaceFonts();
	
	
	/* SUBMENU */
	var menuElem = "#main-menu";
	var submenuElem = ".submenu";

	$(menuElem + " a[rel='main-menu']").hover(
		function(){
			$(menuElem + " " + submenuElem).hide();
			$(this).parent().find(submenuElem).show();
			//$(lnk).addClass("active");
			
			// animate
			/*var initialTop = $(this).parent().find(submenuElem).css("top");
			$(this).parent().find(submenuElem).css("top", "40px");
			$(this).parent().find(submenuElem).animate({ 
				top: initialTop
			}, 300);*/
			
			// width
			var linkWidth = $(this).innerWidth();
			var submenuWidth = $(this).parent().find(submenuElem).innerWidth();
			/*if (submenuWidth<linkWidth) {
				$(this).parent().find(submenuElem).css("width", linkWidth+"px");
			}*/
			//alert(linkWidth+", "+submenuWidth)
			
			// center horizontaly
			var leftMargin = parseInt((linkWidth - submenuWidth)/2) + 5;
			$(this).parent().find(submenuElem).css("left", leftMargin+"px");
			//alert(leftMargin)
		},
		function(){
		}
	);
	var revertClass = true; // if the current main-menu item is active we do not revert
	$(menuElem + " " + submenuElem).hover(
		function(){
			var lnk = $(this).parent().find("a").get(0);
			if ($(lnk).hasClass("active")) {
				revertClass = false;
			} else {
				//alert("active");
				//$(lnk).addClass("active");
				$(lnk).addClass("hover");
			}
		},
		function(){
			//alert(revertClass)
			var lnk = $(this).parent().find("a").get(0);
			if (revertClass==true) {
				//$(lnk).removeClass("active");
				$(lnk).removeClass("hover");
			} else {
				
			}
			$(this).hide();
		}
	);
	$(menuElem).hover(
		function(){ },
		function(){
			$(menuElem + " " + submenuElem).hide();
		}
	);
	
	
	// Services  - Offer
	
	$(".offer-wrapper").hide();
	
	$("#services .general-table a.offer").click( function() {
		var id = $(this).attr('id');
		var nr = id.substr(9);
		
		var currentValue = $("span", "#but-offer"+nr).text();
		//alert(currentValue);
		var newValue = $("#but-offer"+nr).attr("rel");
		
		if (currentValue=="See offer") {
			var id1 = $(this).parent().parent().attr('class');
			//alert(lnk);
			$("."+id1+" .column-first").css({ "color": "#dc4589" });
			$("."+id1+" .column-second em").css({ "color": "#dc4589" });
		} else {
			
		}
		
		$("span", "#but-offer"+nr).text(newValue);
		$("#but-offer"+nr).attr("rel", currentValue);
		
		$("#offer-wrapper"+nr).animate({ height: "toggle", opacity: "toggle"}, "fast" );
		
		return false;
	});
	
	$("#services .general-table tr").hover( 
		function() {
			var id = $(this).attr('class');
			//alert(id);
			$("."+id+" .column-first").css({ "color": "#dc4589" });
			$("."+id + " " + ".column-second em").css({ "color": "#dc4589" });
		},
		function(){
			var id = $(this).attr('class');
			$("."+id+" .column-first").css({ "color": "#000000" });
			$("."+id + " " + ".column-second em").css({ "color": "#606163" });
		}
	);
	
	
	//Services Appointment
	$("#appointment_location").change(function(){
		var val= $("#appointment_location").val();
		//alert(val);
		if (val > 0){
			$("#content-app").css({"display": "block"});
		}
		else{
			$("#content-app").css({"display": "none"});
		}
	});
	
	//submit appointment
	$("#submit-appointment").click( function () {
		var url = $("#appointment_location option:selected").attr("title");
		if (url.length) {
			 $('form').get(0).setAttribute('action', url);
		}
		
	} );
	
	//Blow to Go Contact	
	$("#phone-blow").hide();
	$("#email-blow").hide();
	
	$("#blow_to_go_contact_0").click(function(){
		var checked = $(this).attr('checked');
		//alert(checked);
		if (checked == true){
			$("#phone-blow").show();
		}
		else {
			$("#phone-blow").hide();
		}
	});
	
	$("#blow_to_go_contact_1").click(function(){
		var checked = $(this).attr('checked');
		//alert(checked);
		if (checked == true){
			$("#email-blow").show();
		}
		else {
			$("#email-blow").hide();
		}
	});
	$("input[name='blow_to_go[contact][]']").ready(function(){
		var nr_checked = $("input[name='blow_to_go[contact][]']:checked").length;
		var val = $("input[name='blow_to_go[contact][]']:checked").val();
		//alert(nr_checked);
		if (nr_checked == 2)
		{
			$("#phone-blow").show();
			$("#email-blow").show();
		}
		else
		{
			if (val == 0)
			{
				$("#phone-blow").show();
				$("#email-blow").hide();
			}
			else if (val == 1)
			{
				$("#phone-blow").hide();
				$("#email-blow").show();
			}
		}
		
	});
	
	
	
	
});


// Font replacement
function replaceFonts() {
	Cufon.replace('#main-menu a[rel="main-menu"], h1, h3:not(.nocufon), h2:not(.nocufon), a.link-pinker span', { fontFamily: 'Helvetica Neue LT Std', hover: true, hoverables: { a: true, span: true , button: true } });
	
}

/*
function calculateHeights() {
	
	var minHeight = 580;
	var heightLeft = $("#wrapper-left").height();
	var heightRight = $("#wrapper-right").height() ;
	//alert(heightLeft);
	//alert(heightRight);
	if (heightRight < minHeight){
		heightRight = minHeight;
	}
	if (heightRight > heightLeft){
		$("#wrapper-left").height(heightRight );
	}
	
}*/
