//Rotating images variables
var_imgamt = 0;
var_imgflag = 1;
var_imgcur = 1;
var_imginterval = 0;
// Common functions //

//check version
getIEVersionNumber = function() {
    var ua = navigator.userAgent;
    var MSIEOffset = ua.indexOf("MSIE ");
    if (MSIEOffset == -1) {
        return 999; //so as not to confuse the version no.
    } else {
        return parseFloat(ua.substring(MSIEOffset + 5, ua.indexOf(";", MSIEOffset)));
    }
}

//Navigation
navigationAddDivider = function(clip,str) {
	var clipli = clip.size();
	for(var i=1;i<clipli;i++){
		clip.eq(i-1).after(str);
	}
}

//rotating images
setupRotatingImages = function(){
	if($('#fp_imagecontainer').size()>0){
		var_imgamt = $('.fp_imagepanel').size();

		if(var_imgamt > 1) {
			var butappend = '<div id="fp_imagebuttoncontainer">';
			for(var i=1;i<=var_imgamt;i++){
				butappend += '<div class="fp_imagebutton"><!-- button --></div>'
			}
			butappend += '</div>';
			$('#fp_imagecontainer').append(butappend);
			
			
			
			$('.fp_imagebutton').click(function(){
				//var_imgflag = 0;
				$('.fp_imagebutton').removeClass('butactive');
				$(this).addClass('butactive');
				var pos = $(this).index();
				var_imgcur = pos+1;
				$('.fp_imagepanel').hide();
				$('.fp_imagepanel').eq(pos).fadeIn();
			});
			var_imgflag = 1;
			var_imgcur = 1;
			var_imginterval = setTimeout(timernp,6000);
			$('.fp_imagebutton').eq(0).addClass('butactive');
			$('.fp_imagepanel').eq(0).fadeIn();
			
			//version check
			var isIE = getIEVersionNumber();
			if(isIE == 6){
				//fix imagebuttons
				//DD_belatedPNG.fix('.fp_imagebutton');
			}
			
		} else if(var_imgamt == 1) {
			$('.fp_imagepanel').eq(0).fadeIn();
		}
	}
}
timernp = function(){
	clearTimeout(var_imginterval);
	var newpos = var_imgcur+1;
	if(newpos > var_imgamt) {
		newpos = 1;
	}
	$('.fp_imagebutton').removeClass('butactive');
	$('.fp_imagebutton').eq(newpos-1).addClass('butactive');
	$('.fp_imagepanel').hide();
	$('.fp_imagepanel').eq(newpos-1).fadeIn();
	var_imgcur = newpos;
	if(var_imgflag == 1) {
		var_imginterval = setTimeout(timernp,6000);
	}
}

$(function() {
	//Add dividers to footer navigation
	navigationAddDivider($('#footer_navigation li'),'<li> | </li>');
	
	//Cufon
	Cufon.replace("h1",{fontFamily:'Futura Bk',hover:true});
	Cufon.replace("h3",{fontFamily:'Futura Bk',hover:true});
	
	
	//Navigation
	if($('#navigation').size()>0){
	
		$('.menu_1lvl').hover(function(){
			$(this).children('ul').show();
			$(this).children('.but_top').addClass('active');
			$(this).children('.but_top').stop(true).animate({
				backgroundPosition: '0px 0px'
			}, 500, function() {
					//
			});
		},function(){
			$(this).children('ul').hide();
			$(this).children('.but_top').removeClass('active');
			$(this).children('.but_top').stop(true).animate({
				backgroundPosition: '0px -500px'
			}, 500, function() {
					//
			});
		});
	}
	
	//Grids (auto padding)
	if($('.grid5').size()>0){
		$('.grid5').each(function(){	
			$(this).children('.gridpanel').each(function(){
				var cur = $(this).index();
				if(cur%5 != 0){
					$(this).addClass('grid5_marleft');
				}
			});
		});
	}
	if($('.grid3').size()>0){
		$('.grid3').each(function(){	
			$(this).children('.gridpanel').each(function(){
				var cur = $(this).index();
				if(cur%3 != 0){
					$(this).addClass('grid3_marleft');
				}
			});
		});
	}
	
	
	
	//Fix PNG images & replacement of <hr>
	var isIE = getIEVersionNumber();
	if(isIE == 6){
		DD_belatedPNG.fix('.but_top');
		DD_belatedPNG.fix('.arrow_grey');
		DD_belatedPNG.fix('.img_missionzero');
	}
	if(isIE <= 7) {
		$("hr").replaceWith('<div class="hr"><!-- divider --></div>');
	}
	
	
	//Country selector
	$('#country_selector select').change(function(){
		window.open($(this).val());
	});
	
	
	//Frontpage rotating images
	setupRotatingImages();
	
	

});

$(window).load(function(){
	//height fixing for dual columns
	if($('.col_left').size()>0 && $('.col_right').size()>0){
		var lht = $('.col_left').height();
		var rht = $('.col_right').height();
		if(lht >= rht){
			$('.col_right').css('height',lht);
		} else {
			$('.col_left').css('height',rht);
		}
	}
});
