var currentTime, ctime, ctimes;
var curmenu = "";
var oldcurmenu = curmenu;
$().ready(function() {
    refreshBackgroundPosition();
	
    $(".con_header .bg").css({ opacity: 0.5 });
    $(".con_header .bg").animate({ top: 0 }, 'slow', function() {
        $(".con_header .con").fadeIn('normal');
        $(".con_header .btn").fadeIn('normal');
    });

    refreshPNGBackground();
    ctime = $("#Header1_hid_countdown").val();
	if(ctime<=0){
		$("#countdown_inner").html("<img src='http://www.singtelrace.com/i/txt_see-you-in-march-2010.gif' hspace='28' />");
	}else{
		ctimes = MillisecondsToDuration(ctime).split("");
		
		$("#countdown_inner").html('<div class="no' + ctimes[0] + '"></div><div class="no' + ctimes[1] + '"></div><div class="no' + ctimes[2] + '"></div><div class="no_days"></div><div class="no' + ctimes[3] + ' first"></div><div class="no' + ctimes[4] + '"></div><div class="no_hours"></div><div class="no' + ctimes[5] + ' first"></div><div class="no' + ctimes[6] + '"></div><div class="no_minutes"></div><div class="no' + ctimes[7] + ' first"></div><div class="no' + ctimes[8] + '" style="width:10px;"></div><div class="no_secs"></div>');

		MillisecondsToDuration(ctime);
		var mytimer = setInterval("mytimer()", 1000);
	}

    

	if($("#header_login").size()>0){
		$("#header_btnlogin").hide();
	}else{
		$("#header_btnlogin").show();
		$("#header_signup").css({left:745});
		$("#header_btnlogin").css({left:835});
	}

    //MENU

    $("a#m_home,a#m_whats-on,a#m_grid-girls,a#m_the-ultimate-drive,a#m_contests,a#m_special-deals,a#m_singtel-pit-stop,a#m_gallery,a#m_downloads").hover(function() {
        var cid = $(this).get(0).id;
        //if (!is_submenushown) {

        // }
        curmenu = $(this).get(0).id;
        if (oldcurmenu != curmenu) {
            $(".submenu").fadeOut('fast', function() { });
        }

        oldcurmenu = curmenu;


        if (cid == "m_grid-girls" || cid == "m_contests" || cid == "m_the-ultimate-drive" || cid == "m_singtel-pit-stop" || cid == "m_special-deals" || cid == "m_downloads") {
            $(this).parent().find(".submenu").fadeIn("normal", function() { });
        }
    }, function() {
        curmenu = $(this).get(0).id;
    });

	var int_o= -1;
	
	$(".submenu").each(function(){	
		$(this).hover(function(){
		curmenu = $(this).prev().get(0).id;
		},function(){
			curmenu="";
			clearTimeout(int_o);
			int_o = setTimeout("checkMenu()", 200);
		});
		$(this).prev().hover(function() {
		}, function() {
			curmenu = "";
			
			clearTimeout(int_o);
			int_o = setTimeout("checkMenu()", 200);
		});
	});
});
$(window).unload( function () {
	clearInterval(intbg);
});
function checkMenu() {
	$(".submenu").each(function(){
		if ($(this).prev().get(0).id == curmenu) {
			
		} else {
			$(this).fadeOut('fast', function() { });
		}
	});

}
function mytimer(){
	if(ctime<=0){
		$("#countdown_inner").html("<img src='i/raceday.gif' hspace='28' />");
	}else{
		ctime = ctime - 1000;
		ctimes = MillisecondsToDuration(ctime).split("");

		if (currentTime) currentTime(ctime);
		var tgt = document.getElementById("countdown_inner") ;
		if(tgt){
			tgt.innerHTML=('<div class="no' + ctimes[0] + '"></div><div class="no' + ctimes[1] + '"></div><div class="no' + ctimes[2] + '"></div><div class="no_days"></div><div class="no' + ctimes[3] + ' first"></div><div class="no' + ctimes[4] + '"></div><div class="no_hours"></div><div class="no' + ctimes[5] + ' first"></div><div class="no' + ctimes[6] + '"></div><div class="no_minutes"></div><div class="no' + ctimes[7] + ' first"></div><div class="no' + ctimes[8] + '" style="width:10px;"></div><div class="no_secs"></div>');
	   }
	}
}
var counter=0;

clearInterval(intbg);
var intbg= setInterval("refreshBackgroundPosition()", 1000);
function refreshBackgroundPosition(off) {
	counter++;
	if(counter>120){
		clearInterval(intbg);		
	}

    var $ft = 0
	if($("#footer").size()>0) $ft= $("#footer").offset().top;

    if ($ft < 150) setTimeout("refreshBackgroundPosition()", 1000);
    //var $dft = $(document).height();
    //var $cft = $(".container").height() + 300;
    //var $fft = ($dft - $cft);
    var $ctt = 0
	if($(".container").size()>0) $ctt=$(".container").offset().top + $(".container").height() - $("#contentbg").height();

    if (off) {
        $ft += off;
        $ctt += off;
    }

    
    $("body").css({ backgroundPosition: "0 " + $ft + "px" });
    //$("#footerbg").css({ top: $ft, height: $fft + "px" });
    
	if($("body.the-ultimate-drive").size()>0){
		$("#contentbg").css({ top: 220+ "px" });
	}else if($("body.gg-on-tour").size()>0){
		$("#contentbg").css({ top: 651+ "px" });
	}else{
		$("#contentbg").css({ top: $ctt+ "px" });
	}
}
function MillisecondsToDuration(n) {
		var hms = "";
		var dtm = new Date();
		dtm.setTime(n);
		var d = "000" + Math.floor(n / 86400000);
		var h = "0" + dtm.getUTCHours();
		var m = "0" + dtm.getMinutes();
		var s = "0" + dtm.getSeconds();

		var cs = "0" + Math.round(dtm.getMilliseconds() / 10);
		hms = d.substr(d.length-3) + h.substr(h.length-2) + m.substr(m.length-2);
		hms += s.substr(s.length-2) /*+ "." + cs.substr(cs.length-2)*/;
		return hms;
}

$(window).resize(function() {
    //    refreshBackgroundPosition();
    refreshPNGBackground();
});
function refreshPNGBackground() {
    if ($(window).width() <= 960) {
        $("span.fixbg").show();
    } else {    
        $("span.fixbg").hide();
    }
}