var activeform;

$j(document).ajaxStart(function() { loader(true); });
$j(document).ajaxStop(function() { loader(false); });

function resizestage() {
	var container = $j("#container");
	var colspace = 15;
	
	h = $j(window).height() - $j("#footer").height() - 1;
	w = (container.width() - $j("#left").width()) - colspace;
	
	$j(".subcontainer").height(h);
	$j("#right").width(w);
	
	if (h > 495) {
		$j("body").removeClass("scroll");	
	} else {
		$j("body").addClass("scroll");	
	}
	
	/*
	if (h > 495) {
		$j("body").removeClass("scroll");
		
		$j("#right").height(h);
		
		var share_height = $j(".share").outerHeight();
		var main_height = (container.height()-31-share_height)
		
		$j("#main").css({
			height: main_height+"px",
			'margin-top': ""+share_height+"px"
		});
		
	} else {
		$j("body").addClass("scroll");
	}*/
}

$j(window).load(function() {
	$j(".slideshow").fadeIn("fast");
	
	if ($j(".gallerysubcontainer .gallery li").size() > 0) {
		$j(".gallerysubcontainer").mouseslide({
			height:'245px', //Element height
			widthExpand:true, //Auto Expand Width
			mirror:false, //Create Mirror
			mirrorOpacity:.3 //30% Mirror Opacity
		});
	}
	
	loader(false);
});

$j(window).resize(function() { 
	resizestage(); 
});

$j(document).ready(function() {
	
	loader(true);

	if ((getUrlVars()["page"] == "rooms") && (getUrlVars()["records"] == "roomsearch") || (getUrlVars()["records"] == "roomdetails")) {
		$j("form#form1").clone().appendTo(".page .fleft. .padd").attr("id","form2")
		$j(".page form#form1 input.date").attr("name","checkin2").attr("id","checkin2")
	}
	
	$j(".date").datepicker({
			defaultDate: "+1",
			dateFormat: "dd/mm/yy",
			minDate: 0
		});
	
	if ($j.browser.webkit) {
    	$j("body").addClass("webkit");
	}
	
	resizestage();
	
	if ($j("body").hasClass("home")) {
		$j('.slideshow').before('<div class="cyclenav">').cycle({
			fx: 'fade',
			slideExpr: 'li',
			timeout: 5000
		});
	
		$j(".carousel").jCarouselLite({
			btnNext: ".down",
			btnPrev: ".up",
			vertical: true,
			circular: false,
			mouseWheel: true
		});
	}
	
	if (getUrlVars()["page"] == "contact") { $j("form#contact").appendTo(".scroll"); }
	if (getUrlVars()["records"] != "") { $j("div#products").appendTo(".scroll"); }	
	if ($j(".scroll").size() > 0) { $j('.scroll').jScrollPane({ verticalGutter: 30 }); }
	

	$j(".slideshow img").each(function() {
		a_width = $j(".subcontainer > #right").width();
		a_height = $j(".subcontainer > #right").height() - $j(".share").outerHeight();
		
		src_prefix = "timthumb.php?src=";
		src_suffix = "&w="+a_width+"&h="+a_height+"&q=100&cz=1"
		
		src = src_prefix + $j(this).attr("src") + src_suffix;
		
		$j(this).attr("src", src).width(a_width).height(a_height);
		
		//console.log(src);
		//console.log("Width: "+a_width+"");
		//console.log("Height: "+a_height+"");
	});
	
	$j(".lg .active").append("<ul>");
	$j(".lg li:not(.active)").appendTo(".lg .active ul");
	
	$j("ul.nav, ul.lg, ul.btns").superfish({ autoArrows:  false, dropShadows: false }); 

	var walloptions = { beforeSubmit: form_validate, success: form_success }; 
	
	function form_validate(formData, jqForm) { 
		activeform = jqForm[0].id;
		return $j("#"+jqForm[0].id+"").xvalidate();
	}

	function form_success(responseText) { 
		fo = $j("form#"+activeform+"");
		message("Form Response", responseText);
		
		fo.resetForm();
		fo.clearForm();
	}
	
	$j('.ajax').ajaxForm(walloptions);
	
	$j("ul.gallery a, .thumbs a").colorbox({maxHeight: '90%', maxWidth: '90%', opacity: '0.9'});
	
	parselinks();	
	
	/* Form Theme */
	
	$j("form.theme .row select").each(function() {
		thisclass = $j(this).attr("class");
		$j(this).wrap('<div class="element select clearfix '+thisclass+'"></div>');
		$j(this).before('<span class="sel"></span>');
		$j(this).before('<span class="icon"></span>');
	});
	
	$j("form.theme .row input").each(function() {
		thisclass = $j(this).attr("class");
		$j(this).wrap('<div class="element textbox clearfix '+thisclass+'"></div>');
		$j(this).before('<span class="icon"></span>');
	});
	
	$j("form.theme .element select").change(function() {
		sel = $j(this).find("option:selected").text();
		$j(this).parent().find("span.sel").html(sel);		
	});
	
	$j("form.theme .element select").trigger("change");
	/* End of Form Theme */
	
	if (!$j.browser.msie) {
		$j(".element, button, .corner").corner("4px");
	}
});

function getUrlVars() {
	var vars = [], hash;
	var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
	for(var i = 0; i < hashes.length; i++) {
		hash = hashes[i].split('=');
		vars.push(hash[0]);
		vars[hash[0]] = hash[1];
	}
	return vars;
}

function parselinks() {
	$j('[icon^="ui-icon"]:not(:has(span))').each(function() { 
		$j(this).html('<span class="ui-icon '+$j(this).attr("icon")+'"></span><span class="text">'+$j(this).html()+'</span>');
		$j(this).addClass("myicon");
	});
}

function loader(x) { if (x) { $j(".loading").fadeIn("fast"); } else { $j(".loading").fadeOut("fast"); }}
