/* site.js */

// Initializing cufon
Cufon.replace('h2, h3, .contacts-form label, .fancy-close, .pagination strong, .heading h5, .faq dt, .emphasize, .duration', { fontFamily: 'Trajan Pro' });
Cufon.replace('#nav li a, #sidemenu li a, .contacts-list li, h4, .blog h4, .blog a, .contacts-form .submit, .button2', { fontFamily: 'Trajan Pro', hover: 'true' });
Cufon.replace('#nav ul li a', { fontFamily: 'Trajan Pro', hover: 'true', textShadow: '-2px 1px 1px #000'});
Cufon.replace('#langs li a, #footer p', { fontFamily: 'Bell MT', hover: 'true' });
Cufon.replace('#payoff p', { fontFamily: 'Bell MT', textShadow: '-2px 1px 1px #000'});
Cufon.replace('.button', { fontFamily: 'Trajan Pro', textShadow: '-2px 1px 1px #000'});

var base_url = window.location.host;

$(window).resize(function() {
	// Fix the background
	bg_resize();

});

$(document).ready(function() {

	// Add first / last classes
	$('ul').each(function(){
		$('li:last', this).addClass('last');
		$('li:first', this).addClass('first');
	});

	$('ul#nav li ul li.top').next().addClass('first');
	$('ul#nav li.current ul li:first').next().addClass('first');

	$('.column p:last-child').addClass('last');
	$('#personal_data tr td:nth-child(odd)').addClass('heading');
	
	// load background
	bg_load();

	// Submeniu centering
	var submenuParent = $("#nav > li a").not("#nav li ul a");
	var submenu;
	var submenuWidth = 111;
	var submenuParentWidth;
	var submenuPositionLeft;

	submenuParent.each(function(index) {

		// Getting position value
		submenuParentWidth = $(this).width();
		submenuPositionLeft = (submenuWidth - submenuParentWidth) / 2;

		// If submenu position is negative, setting it to positive
		if(submenuPositionLeft > 0) {
			submenuPositionLeft = -submenuPositionLeft;
		}

		// Getting submenu
		submenu = $(this).next();

		// Setting new position
		submenu.css({ 'left' : submenuPositionLeft});

	});

	// Open all external links and PDF in new window
	$("a[href^='http:']:not([href*='" + base_url + "']), a[href$='.pdf']:not([href*='" + base_url + "']), a[href$='.pdf']").live('click', function() {
		$(this).attr('target','_blank');
	});

	// Enable go back link
	$('.go-back').click(function() {

		history.back();
		return false;

	});

	// Logo Fade
	$('#logo a').hover(function(){
		if($.browser.msie) {
			// hide if IE
			$('span',this).hide();
		} else {
			// fade if not IE
			$('span',this).stop(true,true).fadeOut(250);
		}
	}, function() {
		if($.browser.msie) {
			$('span',this).show();
		} else {
			$('span',this).fadeIn(250);
		}
	})
});

function bg_load() {
	// load bg
	// Set random background
	var bg_image = bg_images[Math.floor(Math.random()*bg_images.length)];
		
	// add image to bg if empty
	if($('#bg img').length == 0) {
		$('#bg').append('<img />');
	}
	$('#bg img').attr('src', bg_image);

	// Preload background image
	var cache = [];
	
	var cacheImage = document.createElement('img');
	cacheImage.src = $('#bg img').attr('src');
	cache.push(cacheImage);
	
	// Reveal background
	$('#bg img').load(function() {
		// fade in
		$('#bg').fadeIn(1500);
		// resize bg
		bg_resize();
	});
	
	// Still is not loaded?
	setTimeout(function() {
			// resize first
		$('#bg').fadeIn(1500);
		bg_resize();

		}, 2500);
}

function bg_resize() {
	//
	// resize bg to fit the window

	// set background height = window height
	$('#bg').height($(window).height());
	//
	$('#bg img').each(function(){
		var ratio = $(this).width()/$(this).height();
	    // if bg width is smaller than window width
		if($(this).width() < $(window).width()) {
			$(this).width($(window).width());
			$(this).height($(this).width()/ratio)
		}
	    // if bg height is smaller than window height
		if($(this).height() < $(window).height()) {
			$(this).height($(window).height());
			$(this).width($(this).height()*ratio)
		}
	    // position to center of the screen
		$(this).css({
			'margin-left':'-'+($(this).width()-$(window).width())/2+'px',
			'margin-top':'-'+($(this).height()-$(window).height())/2+'px'
		})
		// make sure cont has same height as bg
		$('#cont').height($('#bg').height());
	});
	
}

/* End of site.js */

