$(document).ready(function() {
	
	$(function() {
		  setInterval(update, 100);
	});
	
	function update() {
		if($('#mcs_container').length != 0) {
			$("#mcs_container").slideDown('300');
			mCustomScrollbars();
		}
		else {
			$(".inner").slideDown('500');
		}
	}
	
	function mCustomScrollbars(){
		/* 
		malihu custom scrollbar function parameters: 
		1) scroll type (values: "vertical" or "horizontal")
		2) scroll easing amount (0 for no easing) 
		3) scroll easing type 
		4) extra bottom scrolling space for vertical scroll type only (minimum value: 1)
		5) scrollbar height/width adjustment (values: "auto" or "fixed")
		6) mouse-wheel support (values: "yes" or "no")
		7) scrolling via buttons support (values: "yes" or "no")
		8) buttons scrolling speed (values: 1-20, 1 being the slowest)
		*/
		$("#mcs_container").mCustomScrollbar("vertical",300,"easeOutCirc",1.05,"auto","yes","yes",15); 
	} 

	/* 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();
		});
	}
	
	
	
	$('#bg_img1').click(function(event) {
		$('#page_wrapper').css("background-image", "url(images/bg1.jpg)");
		$('#bg_img1').css('background-position', 'top');
		$('#bg_img2').css('background-position', 'bottom');
		$('#bg_img3').css('background-position', 'bottom');
	});
	$('#bg_img2').click(function(event) {
		$('#page_wrapper').css("background-image", "url(images/bg2.jpg)");
		$('#bg_img1').css('background-position', 'bottom');
		$('#bg_img2').css('background-position', 'top');
		$('#bg_img3').css('background-position', 'bottom');
	});
	$('#bg_img3').click(function(event) {
		$('#page_wrapper').css("background-image", "url(images/bg3.jpg)");
		$('#bg_img1').css('background-position', 'bottom');
		$('#bg_img2').css('background-position', 'bottom');
		$('#bg_img3').css('background-position', 'top');
	});
	
	
	function setSize() {
		var Hsize = $(window).height() - 220;
		var inner = $('.inner').height() + 75;
		if( inner > 643){
			//alert('less');
			var minHeight = inner + 90;
			if(minHeight > Hsize) {
				$('#page_wrapper').css('height', minHeight + "px");
			}
			else {
				$('#page_wrapper').css('height', Hsize + "px");
			}
		}
		else {
			$('#page_wrapper').css('height', Hsize + "px");
		}
		
	}

	setSize();
	
	$(window).resize(function() {
		setSize();
	});
	
});
