
Cufon.replace('#navigation li a', { fontFamily: 'Carnivalee', hover : true });

function setHeight() {
	var wh = $(window).height() - ($('#header').height()+$('#footer').height());
	
	var max = wh - 40;
	$('#main .col, #main2 .col').css('height', '');
	$('#main .col, #main2 .col').each(function (index) {
		max = Math.max($(this).height(), max);
	});
	$('#main .col, #main2 .col').css('height', max);
	
	$('#main, #main2').height(wh);
};

/*Resize background and center*/
(function($) {
  $.fn.fullBg = function(){
    var bgImg = $(this); // $('.bg')
 
    function resizeImg() {
    	var img = {width: 0, height: 0, left:0};
    	
		var imgwidth = bgImg.width();
		var imgheight = bgImg.height();
		
		var winwidth = $(window).width();
		
		//$('#main .col').height(winheight);
		
		setHeight();
		
		$(document).resize(function() {
			setHeight();
		});
		
		
		$('#main, #main2').jScrollPane();
		
		var winheight = $(window).height() - ($('#header').height()+$('#footer').height());
		
		var widthratio = winwidth / imgwidth;
		var heightratio = winheight / imgheight;
		
		var widthdiff = heightratio * imgwidth;
		var heightdiff = widthratio * imgheight;
		
		if(heightdiff>winheight) {
			/*bgImg.css({
				width: winwidth+'px',
				height: heightdiff+'px'
			});*/
			img.width = winwidth;
			img.height = heightdiff;
		} else {
			/*bgImg.css({
				width: widthdiff+'px',
				height: winheight+'px'
			});*/
			img.width = widthdiff;
			img.height = winheight;
		}
		// center
        if (img.width > winwidth) {
        	img.left = -((img.width-winwidth)/2);
        }
        $('.bg').css(img);
	}
    resizeImg();
    $(window).resize(function() {
      resizeImg();
    });
  };
})(jQuery);

/*Fade background*/
(function( $ ){
	$.fn.bgFade = function(options) {
	
	//var elem = this; // $('.bg')
	var timer;
	var index = 0;
	
	var s = $.extend({
		speedIn: 500, 
		speedOut: 500
	}, options || {});
	
	start();
	function start() {
		timer = setInterval(function () {
			stop();
			$('#overlay').animate({opacity: 1}, s.speedIn, function () {
				$('.bg').eq(index).hide();
				if (index == $('.bg').length - 1) index = -1;
				$('.bg').eq(++index).show();
				$('#overlay').animate({opacity: 0}, s.speedOut, function () {
					start();
				});
			});
		}, 5000);
	}
	
	function stop() {
		clearInterval(timer);
	}
	
	return this;
	};
})( jQuery );

/*Image preload*/
(function( $ ){
	$.fn.imagePreload = function(options) {
	
	var container = this;
	
	var s = $.extend({
		url: null
	}, options || {});
	
	var index = 0;
	
	if (s.url != null && s.url.length > 0) start();
	
	function start() {
		$.ajax({url: s.url[index],
			success: function (data, textStatus, jqXHR) {
				
				AddImage();
			},
			error: function (jqXHR, textStatus, errorThrown) {
				
				if ($.browser.msie && jqXHR.status == 200) // IE hack
					AddImage();
			}
		});
	}
	
	function AddImage() {
		container.append('<img src="'+s.url[index++]+'" class="bg"></img>');
		if (index < s.url.length) {
			start();
		} else {
			index = -1; // all done
		}
		$('.bg').fullBg();
	}
	
	return this;
	};
})( jQuery );

$(window).load(function() {
	$('.bg').fullBg();
});

$(function () {
	
	var path = window.location.pathname;
	
	if (path.substring(path.lastIndexOf('/') + 1) == 'index.html' || path.substring(path.lastIndexOf('/')) == '/') { // fade images only on index.html
		$('body').imagePreload({
			url: ['css/images/intro/image2.jpg',
			      'css/images/intro/image11.jpg',  
				  'css/images/intro/image3.jpg', 
				  'css/images/intro/image4.jpg', 
				  'css/images/intro/image5.jpg',
				  'css/images/intro/image6.jpg',
				  'css/images/intro/image7.jpg',
				  'css/images/intro/image8.jpg',
				  'css/images/intro/image9.jpg',
				  'css/images/intro/image10.jpg'
				  ]
		});
		$('.bg:eq(0)').show();
		$('.bg').bgFade();
	} else {
		$('.bg').fullBg();
		$('.bg:eq(0)').show();
	}
	
	//
	$('.blink')
		.focus(function(){
			if( $(this).val() == $(this).attr('title') ) {
				$(this).val('');
			}
		})
		.blur(function(){
			if( $(this).val() == '' ) {
				$(this).val( $(this).attr('title') );
			}
		});
});
