/* IE6 flicker fix
-------------------------------------------------- */
try { document.execCommand("BackgroundImageCache", false, true); } catch(err){}
$.browser.msie6 = $.browser.msie && /MSIE 6\.0/i.test(window.navigator.userAgent) && !/MSIE 7\.0/i.test(window.navigator.userAgent);
var menu01timer;
$.fn.tagName = function(){
	if(1 === this.length){
		return this[0].tagName.toLowerCase();
	} else{
		var tagNames = [];
		this.each(function(i, el){
			tagNames[i] = el.tagName.toLowerCase();
		});
		return tagNames;
	}
};

/* Tabs switcher */
var frontInterval
var currentTab = 0;
var prevTab = 0;
var frontAnimating = false;
var frontHover = false;
var slideTime = 5000

function showTab(ref,set,targ){
	if(frontAnimating==false){
		prevTab = currentTab	
		clearInterval(frontInterval)
		frontInterval = 0
		frontAnimating = true;
		$('.'+set+' > LI > A').each(function(i,a){
			$(a).removeClass('active');		
		});
		$(ref).addClass('active');
		
		t = $('.'+targ)
		i = $('.'+set+' > LI ').index($(ref).parent())
		currentTab = i;
		heightShift = parseInt($("LI:eq("+i+")",t).css("top"))
		h = $("LI",t).height();
		$("LI",t).each(function(index){
			targetheight = parseInt($(this).css("top"))-heightShift;
			$(this).css("top",targetheight)
			if(frontHover==false && frontInterval==0){								
				frontInterval = setInterval ( "rotateEvents()", slideTime );
			} 
			frontAnimating = false;	
			newTop = parseInt($(this).css("top"));
			if(newTop<0){
				newOffset = ($("LI",t).length-1)*h+newTop+h;
				
				$(this).css("top",newOffset)
			}				
		});
		return false;
	}
}

function animateTab(ref,set,targ){
	clearInterval(frontInterval)
	frontInterval = 0
	frontAnimating = true;
	$('.'+set+' > LI > A').each(function(i,a){
		$(a).removeClass('active');		
	});
	$("A",ref).addClass('active');
	
	t = $('.'+targ)
	i = $('.'+set+' > LI ').index(ref)
	currentTab = i;
	h = $("LI",t).height();
	$("LI",t).each(function(index){
		targetheight = parseInt($(this).css("top"))-h
		$(this).animate({top:targetheight},700,'linear',function(){
			if(frontHover==false && frontInterval==0){								
				frontInterval = setInterval ( "rotateEvents()", slideTime );
			} 
			frontAnimating = false;	
			if(parseInt($(this).css("top"))<0){
				$(this).css("top",($("LI",t).length-1)*h)
			}
		});
	});
	return false;
};

function rotateEvents(){
	prevTab = currentTab
	currentTab++;
	
	if(currentTab+1>$(".navi > LI").length)
	{
		currentTab = 0;
	}
	animateTab($(".navi > LI:eq("+currentTab+")"),"navi","elements");
}


/* Language dropdown */
function toggleLang(ref){
	$('#languages').toggleClass('hidden');
	if(!$('#languages').hasClass('hidden')){
		$(document).bind('mouseup', toggleLang);
		$('#languages')[0].style.left = ($(ref).offset().left-1) + 'px';
		$('#languages')[0].style.top = $(ref).offset().top + ref.offsetHeight + 'px';
	} else {
		$(document).unbind('mouseup', toggleLang);
	}
}

$(document).ready(function() {
	$(".temp").click(function(){
		$(this).val("")
			.unbind("click")
			.removeClass("temp");		
	})
	.focus(function(){
		$(this).val("")
			.unbind("click")
			.removeClass("temp");		
	});
	if($("#front-rotator").length!=0){
		$("#front-rotator .elements LI").each(function(index){
			$(this).css("top",index*$(this).height());
		});
		frontInterval = setInterval ( "rotateEvents()", slideTime );
		$("#front-rotator").mouseover(function(){
			clearInterval(frontInterval)
			frontInterval = 0
			frontHover = true;
		})
		.mouseleave(function(){
			frontHover = false;
			if(frontAnimating==false && frontInterval==0){								
				frontInterval = setInterval ( "rotateEvents()", slideTime );
			}
		})
	}
	// menu01 functions
	$('.menu01 LI:has(UL) > A').addClass('haschildren').bind('mouseenter',function(){
		$t = $(this);
		$top  = parseInt($t.parent().css('top'));
		$left  =  parseInt($t.parent().css('left'));
		$h  = $t.parent().outerHeight();
		$w  = $t.parents('UL').eq(0).outerWidth()+1
		$offset  = $t.parent().offset().left+$t.width();
		$docw = $(window).width();
		
		if($left+$w>$docw){ }
		if( $t.parents('UL').length == 1 ){
			$t.addClass('open').parents('LI').eq(0).find('UL').eq(0).css({ display: 'block', left: $left + 'px', top: $top+$h + 'px', marginTop: 5 + 'px' });
		} else {
			$left = $t.parents('UL').eq(0).outerWidth() + 1;
			if($offset+$w>$docw || parseInt($t.parent().parent().css("left"))<0){ $left -=$w+$t.outerWidth()+1; }
			if($offset+$w>$docw) { $left-=1; }
			$t.addClass('open').parents('LI').eq(0).find('UL').eq(0).css({ display: 'block', left: $left + 'px',top: -3 + 'px' });
			if($offset+$w>$docw) { 
				$t.addClass('open').parents('LI').eq(0).find('UL').eq(0).css({width: 140+"px"});
			}
		}
	});
	$('.menu01 A')
		.bind('mouseenter',function(){
			window.clearTimeout(menu01timer);
			$('.menu01 LI:has(A.open)').not($(this).parents('LI')).each(function(){
				$(this).find('UL').hide();
				$(this).find('A.open').removeClass('open');
			});
		})
		.bind('mouseleave',function(){
			window.clearTimeout(menu01timer);
			menu01timer = window.setTimeout(function(){
				$('.menu01 LI UL').hide();
				$('.menu01 A.open').removeClass('open');
			},500);
		})
	;
	$("UL.twocol").each(function(){
		var children = $(this).children("LI");
		var len = children.length;
		var i = 0;
		while(i<len){
			var maxHeight=0;
			for(var j = i; j<i+2; j++){
				var currHeight = children.eq(j).height();
				if(currHeight>maxHeight) maxHeight=currHeight;				
			}
			for(var j = i; j<i+2; j++){
				 children.eq(j).css("height",maxHeight+"px");
			}
			i+=2;
		}
	})
});




