// convert rel="external" to blank-page-links
$(function(){$('a[href][rel*=external]').each(function(i){this.target = "_blank";});});

//submenu bg manipulation
$(function(){
	if($('#submenu ul li').length>1)
	$('#submenu ul li:last a').css({'background':'none'});
});

//bread manipulation
$(function(){
	if($('#bread ul li').length>1){
		var lastBreadHtml = $('#bread ul li:last').html().replace(/»/,"");
		$('#bread ul li:last').html(lastBreadHtml);
	}
});

//title manipulation
$(function(){
	var colors = Array('yellow','green','red','blue');
	var key = 0;
	$('.item-container h1').each(function(){
		var color = colors[key];
		var span = $(this).children('span');
		var spanWidth = span.width()+25;
		var spanHeight = span.height()+2;
		$(span).after('<img src="/images/bg-title-'+color+'.png" height="'+spanHeight+'" width="'+spanWidth+'"+ />');
		
		key++;
		if(key>colors.length-1) key = 0;
	});
});

//make right minimum equal to left - content height for bottom fitting;
$(function(){
	var leftHeight = $('#left').height();
	var rightHeight = $('#right').height();
	
	if(leftHeight>rightHeight)
		$('#right').height(leftHeight);
		
	//contentheight
	var contentHeight = $('#content').height();
	var top = Math.ceil(contentHeight/282);
	$('#right').height(top*282);
});

// intro make right minimum equal to left - content height for bottom fitting;
$(function(){
	var leftHeight = $('#intro-text-left').height();
	var rightHeight = $('#intro-text-right').height();
	//log(leftHeight);
	if(leftHeight>rightHeight){
		$('#intro-text-right').height(leftHeight);
	}else{
		$('#intro-text-left').height(rightHeight);
	}
});


function log(msg){
	if(window.console)
	window.console.log(msg);
}


// Background Image
$(window).load(function(){ displayBackgroundImage(false); });
$(window).resize( function() { displayBackgroundImage(true); });


function winWidth() {
	if (self.innerWidth)
		return self.innerWidth;
	else if (document.documentElement &&
document.documentElement.clientWidth)
		return document.documentElement.clientWidth;
	else if (document.body)
		return document.body.clientWidth;
}

function winHeight() {
	if (self.innerHeight)
		return self.innerHeight;
	else if (document.documentElement &&
document.documentElement.clientHeight)
		return document.documentElement.clientHeight;
	else if (document.body)
		return document.body.clientHeight;
}

function displayBackgroundImage(animate) {
	imgWidth = $("#bgImage img").width();
	imgHeight = $("#bgImage img").height();
	
	var windowHeight = winHeight();
	var windowWidth = winWidth();
	
	if (winWidth() && winHeight()) {
	
		var diffY = windowWidth/imgWidth;
		var diffX = windowHeight/imgHeight;
		var marge = 1.03;
		
		if(diffY>diffX){
			newWidth 	= imgWidth*diffY*marge;
			newHeight = imgHeight*diffY*marge;
		}else{
			newWidth 	= imgWidth*diffX*marge;
			newHeight = imgHeight*diffX*marge;
		}
		
		var marginTop = 	(windowHeight-newHeight)/2;
		var marginLeft = 	(windowWidth-newWidth)/2;

		if(animate) {
			$("#bgImage img").stop();
			$("#bgImage img").animate({
				'width':newWidth,
				'height': newHeight,
				'marginTop': marginTop,
				'marginLeft': marginLeft
			});
		}else{
			$("#bgImage img").width(newWidth);
			$("#bgImage img").height(newHeight);
			$("#bgImage img").css({'marginTop':marginTop, 'marginLeft':marginLeft});
		}
	}
}

