(function ($) {
$.fn.vAlign = function() {
	return this.each(function(i){
	var h = $(this).height();
	var oh = $(this).outerHeight();
	var mt = (h + (oh - h)) / 2;	
	$(this).css("margin-top", "-" + mt + "px");	
	$(this).css("top", "50%");
	$(this).css("position", "absolute");	
	});	
};
})(jQuery);

(function ($) {
$.fn.hAlign = function() {
	return this.each(function(i){
	var w = $(this).width();
	var ow = $(this).outerWidth();	
	var ml = (w + (ow - w)) / 2;
	$(this).css("margin-left", "-" + ml + "px");
	$(this).css("left", "50%");
	$(this).css("position", "absolute");
	return $(this);
	});
};
})(jQuery);

(function ($) {
	$.fn.centerAlign = function() {
		return this.each(function(i){
			var w = $(this).width();
			var ow = $(this).outerWidth();	
			var ml = (w + (ow - w)) / 2;
			var h = $(this).height();
			var oh = $(this).outerHeight();
			var mt = (h + (oh - h)) / 2;

			$(this).css("margin-left", "-" + ml + "px");
			$(this).css("left", "50%");
			$(this).css("margin-top", "-" + mt + "px");	
			$(this).css("top", "50%");
			$(this).css("position", "absolute");

			$(this).css({width: w + "px", height: h + "px"});
			
			return $(this);
		});
	};
})(jQuery);

$(window).load(function(){

	contentHeight = $("#content #jscrollpane").wrapInner("<div class='inner'>").find(".inner").height() + 40;
	maxHeight = $('body').height() - 310;

	//console.log(contentHeight, maxHeight, $('body').height());

	if(maxHeight < contentHeight)
	{
		contentHeight = maxHeight;
	}

	$("#content #jscrollpane > div").children().unwrap();
	
	$("#content").animate({height: contentHeight}, 200);
	
	$("#jscrollpane").animate({height: contentHeight - 55}, 200, function(){
		if(maxHeight == contentHeight)
		{
			var settings = {
				showArrows: false,
				autoReinitialise: true
			};
			$('#jscrollpane').jScrollPane(settings);
		}
	}).data('maxHeight', maxHeight);
});

var slideShowInterval;

$(document).ready(function() {
	
    function resizeImg() {
	  $('#background-image img').stop(true,true).each(function(){
	  bgImage = $(this);
      var imgwidth = bgImage.width(),
		   imgheight = bgImage.height(),
		   winwidth = $('body').width(),
		   winheight = $('body').height() - 180,
		   widthratio = winwidth / imgwidth,
		   heightratio = winheight / imgheight,
		   widthdiff = heightratio * imgwidth,
		   heightdiff = widthratio * imgheight;
		   topDiff = (winheight - heightdiff) + 130;
		   leftDiff = (winwidth - widthdiff);
		
		 $(this).parent().css({
			width: winwidth,
			height: winheight + 130
		 });
		 
		 
		 
	  bgImage.css({
		marginTop: 0,
		marginLeft: 0
	  });
      if(heightdiff>winheight) {
        bgImage.css({
          width: winwidth+'px',
          height: heightdiff+'px',
		  marginTop: topDiff
        });
      } else {
        bgImage.css({
          width: widthdiff+'px',
          height: winheight+'px',
		  marginTop: '130px',
		  marginLeft: leftDiff
        });
      }
		});
		
		oldMaxHeight = $("#jscrollpane").data('maxHeight');

		if(oldMaxHeight)
		{
			contentLeft = 229 + ($('body').width() - 229)/2;
			maxHeight = $('body').height() - 310;
			maxHeightDiff = maxHeight - oldMaxHeight
			if(maxHeightDiff > 0)
			{
				maxHeightDiff = "+=" + parseInt(maxHeightDiff);
			} else {
				maxHeightDiff = "-=" + parseInt(-maxHeightDiff);
			}

			//console.log(maxHeightDiff);

			$("#content").animate({left: contentLeft, height: maxHeightDiff}, 200);
			$("#jscrollpane").animate({height: maxHeightDiff}, 200).data('maxHeight', maxHeight);;
		}
    }
	
    resizeImg();
	
    $(window).resize(function() {
      resizeImg();
    });
	
	nextImageAlpha = function(){
		
		thisImage = $("#background-image img").last();
				
		rnd = Math.random();
		ow = thisImage.width();
		oh =  thisImage.height();
		ol = thisImage.css('left');
		ot = thisImage.css('top');
		ratio = oh / ow;
		nh =  String(Math.round(200*ratio));
		nt = String(Math.round(200*ratio/2));
		thisImage.animate(
		{
			opacity: 0,
			width: '+=200',
			height: '+=' + nh,
			left: '-=100',
			top: '-=' + nt
		},
		2000,
		function(){
			$(this).parent().prepend($(this));
			$(this).css({opacity: 1, width: ow, height: oh, left: ol, top: ot});
		});
	};
	
	nextImageSlide = function(){
		
		thisImage = $("#background-image img").last();
		newImage = thisImage.prev();
	
		rnd = Math.random();
		ow = thisImage.width();
		oh =  thisImage.height();
		ol = thisImage.css('left');
		
		//console.log(ol,ow);
		
		newImage
		.stop(true,true)
		.css('left',parseInt(ol) + parseInt(ow))
		.animate(
		{
			left: ol
		},
		1000,
		'easeInCubic');
		
		thisImage
		.stop(true,true)
		.animate(
		{
			left: '-=' + ow
		},
		1000,
		'easeInCubic',
		function(){
			$(this).parent().prepend($(this));
			$(this).css({left: ol});
		});
	};
	
	//slideShowInterval = setInterval(nextImageAlpha, 6000);
	
	nextBanner = function(){
		var position = $("#banner a.inner").css('background-position');
		switch(position)
		{
			case '50% 0%':
				$("#banner a.inner").css('background-position','50% -280px');
				break;
			case '50% -280px':
				$("#banner a.inner").css('background-position','50% -560px');
				break;
			case '50% -560px':
				$("#banner a.inner").css('background-position','50% 0%');
				break;
		}
	};
	
	var bannerInterval = setInterval(nextBanner, 2000);
	
	$("#side-menu-1").click(function(event){
		event.preventDefault();
		hs.htmlExpand(
		this,
		{ 
			objectType: 'iframe',
			width: 440,
			height: 500,
			allowSizeReduction: false,
			wrapperClassName: 'draggable-header no-footer',
			preserveContent: true,
			objectLoadTime: 'after'
		});
	});
	
	$("#phone").parent().click(function(event){
		event.preventDefault();
		hs.htmlExpand(
		this,
		{ 
			objectType: 'iframe',
			width: 440,
			height: 500,
			allowSizeReduction: false,
			wrapperClassName: 'draggable-header no-footer',
			preserveContent: true,
			objectLoadTime: 'after'
		});
	});
	
	$("#side-menu-5").fancybox({
		zoomSpeedIn: 0,
		zoomSpeedOut:0,
		width: 720,
		height: 437,
		type: 'iframe',
		padding: 0,
		margin: 0,
		onStart: function(){ clearInterval(slideShowInterval); }
	});
	
	$("#banner .inner").fancybox({
		zoomSpeedIn: 0,
		zoomSpeedOut:0,
		width: 900,
		height: 436,
		type: 'iframe',
		padding: 0,
		margin: 0
	});
	
	$("a[href$='.jpg']").fancybox({
		'zoomSpeedIn': 300,
		'zoomSpeedOut': 300,
		'enableEscapeButton': true,
		padding: 0,
		margin: 0,
		autoScale: true,
		autoDimensions: true
	});
	
	$("#banner a.close, #content a.close").click(function(event){
		event.preventDefault();
		$(this).parent().fadeOut();
		
		if($(this).parent().attr('id') == 'banner')
		{
			clearInterval(bannerInterval);
		}
	});
	
	$("#content ul.pdf-list li a span").centerAlign();
});
