// JavaScript Document
$(document).ready(function(){

	// language selector
	// add to all languages flag and set selected lang
	$('#lang_selector p').each(function(){
		var lang = $(this).attr('class');
		var imgUrl = 'url(images/flags/'+lang+'.gif)';
		$(this).css({backgroundImage: imgUrl});
		// this IF should be removed, if you print selected-language first by server-side script, else use class="selected" on A tag
		if($(this).find('a').hasClass('selected')){
			var item = $(this).html();
			$(this).parent().prepend('<p class="'+lang+'" style="background-image:'+imgUrl+'">'+item+'</p>');
			$(this).remove();
		}
	});
	// show/hide list of langs
	$('#lang_selector').click(function(){
		if($(this).height() == '18'){
			var langs = $(this).children().size();
			var newHeight = langs*19;
			$(this).stop().animate({height: newHeight+'px'}, {duration: 500});
		}else{
			$(this).stop().animate({height: '18px'}, {duration: 500});
		}
	});
	
	// inputs with predefined text
	$('.dark_input').each(function(){
		var val = $(this).attr('title');
		$(this).val(val);
		$(this).focus(function(){if($(this).val() == val)$(this).val('');}).blur(function(){if($(this).val() == '')$(this).val(val);});
	});
	
	// fix main_menu width
	var width = $('#header menu').width();
	var childs = $('#header menu').children().size();
	if(width > 664){
		var diff = width-664;
		var padding = 18-diff/(childs*2);
		// must be rouded because all browsers except firefox not support decimals
		padding = Math.round(padding);
		$('#header menu li a').css({padding: '0px '+padding+'px'});
		// fix if OS is mac osx (widder font)
		if(navigator.userAgent.toLowerCase().indexOf("mac")!=-1)$("#header menu li:last-child a").css({padding: '0px '+(padding+1)+'px 0px '+(padding-1)+'px'});
	}
	if(width < 664){
		var diff = 664-width;
		var padding = 18+diff/(childs*2);
		padding = Math.round(padding);
		$('#header menu li a').css({padding: '0px '+padding+'px'});
	}
	
	// fix content width depends on sidebar(is absolute because IE)
	var sidebar_h = $('#sidebar').height() - 40;
	var content_h = $('#content').height();
	if(sidebar_h > content_h)$('#content').css({height: sidebar_h+'px'});
	
	// open A tags with class external in new windows - replace unsemantic target="_blank"
	$('a.external').click(function() { window.open($(this).attr('href')); return false; });
	
	// breadcrumb last element
	$(".breadcrumb li:last-child").attr('class','last');
	
	// table styling
	$("table th:last-child").css("border", "none");
	var i = 1;
	col = '';
	$("table th").each(function(){
		if($(this).html() == '')$(this).attr('class','blank');
		if($(this).hasClass('highlighted'))col = i;
		i++;
	});
	if(col){
		$('table tbody tr').each(function(){
			var x = 1;
			$(this).find('td').each(function(){
				if(col == x)$(this).attr('class','highlighted');
				x++;
			});
		});
	}
	
	// IE6 fixs
	if(getInternetExplorerVersion()==6){
		// fix hover effect on input buttons
		$('input.button').hover(function(){
			$(this).css({backgroundImage:'url(images/design/button_hover.png)'});
		},function(){
			$(this).css({backgroundImage:'url(images/design/button.png)'});
		});
		// fix sidebar position - this ugly hack I must use because I want to maintain the proper structure and semantic of document
		if($('#content').hasClass('home')){
			var margin = $('#content').height() + 374;
			$('#sidebar').css({marginTop: '-'+margin+'px'});
		}
	}
});

function getInternetExplorerVersion()
// Returns the version of Internet Explorer or a -1
// (indicating the use of another browser).
{
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer')
  {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}

