$(function() {
	
	$("#sign_act").click(function(){
		$("#sign_act").addClass("sign_act");
		$("#sign_box").show();
		/*var linkW = $(this).width() / 2;
		var loginW = $("#sign_box").width() / 2;
		$(".case").css("left", (linkW - loginW - 4));*/
		return false;
	});
	
	$(".login").bind("mouseleave",function(){
		setTimeout(loginClose,400);
	});
	
    /* Wire up login form input fields to submit form when keyboard enter key is pressed */
    $('.loginform input').keydown(function(e){
        if (e.keyCode == 13) {
            $(this).parents('form').submit();
            return false;
        }
    });
	
	$("#flag_current").click(function(){
		$("#flag_dropdown").show();
		return false;
	});
	
	$("#flag_dropdown, .flag").bind("mouseleave",function(){
		setTimeout(flagClose,400);
	});
	
	$("#flag_dropdown ul li a").click(function(){
		var src = $(this).find("img").attr("src");
		$("#flag_current_img").attr("src", src);
		setTimeout(flagClose,1);
	});

	$(".get_started").click(function(e){
		if(e.target.id !="link1" && e.target.id !="link2"){
			$(".get_content").toggle();
			$(".flag_bottom span").hide();
		}
	});

	var get_started_W = $(".get_started").width();
	$(".get_content").css("width", get_started_W);
	
	$(".get_started").mouseover(function(e){
		$(".get_content").show();
		$(".flag_bottom span").hide();
		return false;
	});

	$(".get_content").mouseout(function(e){
		$(".get_content").hide();
		return false;
	});
	
	Cufon.replace('.mainhead h1', { fontFamily: 'Gotham-XLight', fontSize: '30px', letterSpacing: '-2px' });
	Cufon.replace('.leader h4', { fontFamily: 'GothamBook', letterSpacing: '-1px' });
	Cufon.replace('.leader h3', { fontFamily: 'GothamMedium', letterSpacing: '-1px'  });
	
	$(".mainhead h1").show();
	
	$(".popup-list li a.link").click(function(){
		$(".popup").hide()
		var thislist = $(this).parent();
		var popuptext = thislist.find(".text").html();
		var linktext = $(this).find("span").text();
		var position = thislist.position();
		var top = position.top;
		var left = position.left;
		$(".opened").find("span").html(linktext);
		$(".popup").css("left",left+"px");
		$(".popup").css("top",(top+38)+"px");
		$(".popup").show().find(".bg").html(popuptext);		
		$(".opened .close").click(function(){
			$(".popup").hide();
			return false;
		});
		return false;
	});
	
	$(".popup").hover(function(){
	},function(){
		$(this).find(".opened span, .bg").html("");
		$(".popup").hide(0);	
	});
	
	if ($.browser.msie && $.browser.version.substr(0,1)<7) {
		$(".headlink").hover(function(){
			$(this).addClass("hovered");
		},function(){
			$(this).removeClass("hovered");
		});
	}
	
	// dropdown navigation
	$(".navcontent ul:first li:has(ul)").each(function(){ $(this).find('a:first').addClass('dropdownlink'); }); // add class to dropdown link
	$(".navcontent ul:first li:has(ul)").each(function(){ $(this).find('li:last').addClass('lastlink'); }); // add class to last dropdown list
	/*$(".navcontent ul:first li:has(ul)").hover(function(){
		$(this).find('ul:first').css({visibility: "visible",display: "none"}).show(1).parent().find('.dropdownlink').addClass('opened');
	},function(){
		$(this).find('ul:first').css({visibility: "hidden"}).parent().find('.dropdownlink').removeClass('opened');
	});*/
	
    $(".navcontent ul:first li:has(ul) a").hover(function(){
		$(this).parent().find('ul:first').css({visibility: "visible",display: "none"}).show(1).parent().find('.dropdownlink').addClass('opened');
	},function(){
	});
	
	$(".navcontent ul:first li:has(ul) a").click(function(){
		$(this).parent().find('ul:first').css({visibility: "visible",display: "none"}).show(1).parent().find('.dropdownlink').addClass('opened');
	},function(){
	});
	
	$(".navcontent ul:first li:has(ul)").hover(function(){
	},function(){
		$(this).find('ul:first').css({visibility: "hidden"}).parent().find('.dropdownlink').removeClass('opened');
	});
	
	$(".navcontent ul:first li:has(ul)").click(function(){
	},function(){
		$(this).find('ul:first').css({visibility: "hidden"}).parent().find('.dropdownlink').removeClass('opened');
	});
	
	if($("div.imagecontent").length){
		$("div.topleft div.content, div.topright div.content, div.bottomleft div.content, div.bottomright div.content").css("height", "auto");
		equalHeight($("div.image_text_top_same_height"));
		equalHeight($("div.image_text_bottom_same_height"));
		var image_text_top_same_height = $("div.image_text_top_same_height").height();
		$(".imagecontent .centertext").css("top", (image_text_top_same_height - 79) + 18);
	}

});

window.onload = function(){

	if(document.images) {
		// logo-rotator exist, playit
		if($('.logo-rotator').length != 0) {
			var playLogoRotator =  setInterval( "logoRotator()", 2000 );
			
			$(".logo-rotator").hover(function() {
				clearInterval(playLogoRotator);
			},
			function() {
				playLogoRotator =  setInterval( "logoRotator()", 2000 );
			});
		}

	}
}

function loginClose(){
	$("#sign_act").removeClass("sign_act");
	$("#sign_box").hide();
}

function flagClose(){
	$("#flag_dropdown").hide();
}

// LogoRotator
function logoRotator() {
    var $active = $('.logo-rotator img.active');
    if ( $active.length == 0 ) $active = $('.logo-rotator img:last');
    var $next =  $active.next().length ? $active.next() : $('.logo-rotator img:first');
    $active.addClass('last-active');
    $next.css({opacity: 0.0})
	    .addClass('active')
		.animate({opacity: 1.0}, 1000, function() {
		$active.removeClass('active last-active');
	});
}
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}


