/*
//
// Emerio Media AS
// by Øyvind Heggernes
//
// Januar 2012
//
*/

$(document).ready(function() {
	/*
	var $winwidth = $(window).width();
	//alert($winheight);
	$("img.img_bg").attr({ width: $winwidth });
	*/
	/*
	var $winheight = $("img.img_bg").height();
	alert($winheight);
	$(".left").attr({ height: $winheight });
	*/
	/*
	$(window).bind("resize", function(){
			var $winwidth = $(window).width();
			$("img.img_bg").attr({
							width: $winwidth
			});
	 });
	 */
	$("#bookingHeader").click(
		function() {
			$("#booking").fadeOut();
			$("#overlay").fadeOut();
		}
	);
	
	$("#BookingDato").datepicker({
		minDate: new Date,
		monthNames: ['Januar','Februar','Mars','April','Mai','Juni','Juli','August','September','Oktober','November','Desember'],
		dayNamesMin: ['Sø', 'Ma', 'Ti', 'On', 'To', 'Fr', 'Lø'],
		firstDay: 1,
		dateFormat: 'yy-mm-dd'
	});
	$("#BookingFra, #BookingTil").datetimepicker({
		timeOnly: true,
		showTime: false,
		stepMinute: 15,
		timeOnlyTitle: 'Velg tid',
		hourText: 'Time',
		minuteText: 'Minutt',
		currentText: 'Nå',
		closeText: 'Lagre'
		
	});
	
	$("#bookingKnapp").click(function () {
		var sh = $(window).height();
		var sb = $(window).width();
		var bh = $('#booking').height();
		var bb = $('#booking').width();
		//alert(sh + ' ' + bh);
		var top = ((sh-bh)/2);
		var left = ((sb-bb)/2);
		$('#overlay').fadeIn(function() {
			$('#booking').css({'top': top+'px', 'left': left+'px'});
			$('#booking').fadeIn();
		});
	});
});

$(window).load(function() {    

	var theWindow        = $(window),
			$bg              = $("#bg"),
			aspectRatio      = $bg.width() / $bg.height();

	function resizeBg() {
		if ( (theWindow.width() / theWindow.height()) < aspectRatio ) {
			$bg
				.removeClass()
				.addClass('bgheight');
		} else {
			$bg
				.removeClass()
				.addClass('bgwidth');
		}
		//$bg.delay(500).fadeIn('slow');
		//alert('asdf');
		/*
		if(window.location == 'http://sudost.emerio.no/') {
			$bg.fadeIn('slow');
		}
		else {
			$bg.css({display: 'block'});
		}
		*/
		$bg.fadeIn('fast');
	}

	theWindow.resize(function() {
					mainHeights();
	}).trigger("resize");
	
	theWindow.resize(function() {
					//resizeBg();
	}).trigger("resize");
	
	// Autonav - Magic Line //
	var $el, leftPos, newWidth,
			$mainNav = $("#sub_nav");

	$mainNav.append("<li id='magic-line'></li>");
	var $magicLine = $("#magic-line");

	$magicLine
			.width($(".current_page_item").width()-30)
			.css("left", $(".current_page_item a").position().left)
			.data("origLeft", $magicLine.position().left)
			.data("origWidth", $magicLine.width());

	/*
	$("#sub_nav li a span").hover(function() {
			$el = $(this);
			leftPos = $el.position().left;
			newWidth = $el.parent().width();
			$magicLine.stop().animate({
					left: leftPos,
					width: newWidth
			});
	}, function() {
			$magicLine.stop().animate({
					left: $magicLine.data("origLeft"),
					width: $magicLine.data("origWidth")
			});
	});
	*/
	
	
	// Scrolling //
	mCustomScrollbars();

	
	// Adjust heights //
	
});

function mainHeights() {
	var h = $(".content").height();
	var mvs_h = h-200;
	$("#mcs_container").height(mvs_h);
}

function mCustomScrollbars() {
	$("#mcs_container").mCustomScrollbar("vertical",400,"",1.0,100,"yes","yes",20); 
}

/* function to fix the -10000 pixel limit of jquery.animate */
$.fx.prototype.cur = function(){
    if ( this.elem[this.prop] != null && (!this.elem.style || this.elem.style[this.prop] == null) ) {
      return this.elem[ this.prop ];
    }
    var r = parseFloat( jQuery.css( this.elem, this.prop ) );
    return typeof r == 'undefined' ? 0 : r;
}

/* function to load new content dynamically */
function LoadNewContent(id,file){
	$("#"+id+" .customScrollBox .content").load(file,function(){
		mCustomScrollbars();
	});
}

function sendBooking() {
	$("#booking_res").html('<img src="/grafikk/loading.gif" width="16" height="11" />');
	$.post("/inc/ajax/sendBooking.php", $("#booking_form").serialize(), function(data) { 
		$("#booking_res").html(data);
	});
}
