function printProps(obj, objName) {
	var output = "";
	for (var prop in obj) {
		output += objName + "." + prop + " = " + obj[prop] + "\n";
	}
	return output;
}

function hideHelp() {
	var sWidth = LT_sWidth(GetCookie(LT_LiveTimingCookieKey));
	$$(".helpContainer").addClass("hide");
	$("f1app").width = sWidth;
	$("f1app").style.width = sWidth;
	$("f1app").removeClass(LT_iJaveMode == "small" || LT_sMode == "inline" ? "standardPopup" : "largePopup");
}

function showHelp() {
	var sWidth = LT_sWidth(GetCookie(LT_LiveTimingCookieKey));
	
	if (LT_iJaveMode == null) {
        LT_iJaveMode = "small";
    }

	//Set the 'hide' class to the previously clicked elements corresponding item. This will make sure that the object is visible after clicking COMMENTRY element.
	prevTab = $$(".liveTimingMenu li a.active");
	if($(prevTab[0].innerHTML.toLowerCase())){
		$(prevTab[0].innerHTML.toLowerCase()).addClass("hide");
	}
	
	obj = ($(this.innerHTML.toLowerCase()) || $("error"))
	$$(".liveTimingMenu li a").removeClass("active");
	this.addClass("active");

	if (obj == null) {
		hideHelp();
		return;
	}

	$("sliderArea").style.display = "";

	if (obj.hasClass("hide")) {
		$$(".helpContent").addClass("hide");
		$$(".helpContainer").removeClass("hide");
	
		//App Width
		//$("f1app").width = (LT_iJaveMode == "small" || LT_sMode == "inline" ? 450 : 643);
		//$("f1app").style.width = (LT_iJaveMode == "small" || LT_sMode == "inline" ? 450 : 643);
		$("f1app").addClass(LT_iJaveMode == "small" || LT_sMode == "inline" ? "standardPopup" : "largePopup");
	
		//Help Content Width
		$$(".helpInner").setStyle("width", (LT_iJaveMode == "small" || LT_sMode == "inline" ? 255 : 355));
		$$(".helpContent").setStyle("width", (LT_iJaveMode == "small" || LT_sMode == "inline" ? 227 : 327));
	
		//Help Content Height
		$$(".helpContent").setStyle("height", (LT_iJaveMode == "small" || LT_sMode == "inline" ? 420 : 551));

		if (!obj.mySlide) {
			setupSlider(obj, null, (LT_iJaveMode == "small" || LT_sMode == "inline" ? 432 : 564))
	
	        /* mousewheel scroll */
	        obj.addEvents({
	            'wheelup': function(e) {
					e = new Event(e).stop();
					obj.mySlide.step = obj.mySlide.step - scrollSpeed;
					obj.mySlide.set(obj.mySlide.step);
	            },

	            'wheeldown': function(e) {
					e = new Event(e).stop();
					obj.mySlide.step = obj.mySlide.step + scrollSpeed;
					obj.mySlide.set(obj.mySlide.step);
	            }
	        });
	    }

		obj.toggleClass("hide");
		obj.scrollTo(0, 0);
		obj.mySlide.step = 0;
		obj.mySlide.set(obj.mySlide.step);
	}
	/*
	Commented this code as it was hiding the corresponding content when clicked twice.
	else{				
			$$(".helpContainer").addClass("hide");
			$$(".liveTimingMenu li a").removeClass("active");

			$$("firstItem").addClass("active");
			
			$("f1app").width=sWidth;
			$("f1app").style.width=sWidth;
	}*/
	
	//Remove scroller if needed
	if (obj.scrollHeight <= divHeight) {
		$("sliderArea").style.display = "none";
		//$$(".helpContent").setStyle("width", (LT_sMode == "inline" || LT_iJaveMode == "small" ? 242 : 332));
	} else {
		$("sliderArea").removeClass("hide");
		obj.removeClass("wider");
	}
	return false;
}

function initTimingsHelpNav() {
	helpButtons = $$(".liveTimingMenu li a");
	for (x = 0; x < helpButtons.length; x++) {
	helpButtons[x].onclick = showHelp;
	}
}

function writeApplet() {
	var sCookie = GetCookie(LT_LiveTimingCookieKey);
	var sWidth = LT_sWidth(sCookie);
	var sHeight = LT_sHeight(sCookie);
	var sJavaSize;
	
	if (LT_iJaveMode == null) LT_iJaveMode = "inline";
	
	if (LT_sMode == "popup") {
	    sJavaSize = (LT_iJaveMode != "bigpopup" ? "inline" : LT_iJaveMode);
	} else {
	    sJavaSize = "inline";
	}
	
	if(LT_IsArchive == 0)	{
	    LT_CircuitName = UpdateLiveTiming();
	}	
	
	sContent = "";
	iContent = "<iframe width='" + sWidth + "' height='" + sHeight + "' frameborder='0'  vspace='0'  hspace='0'  marginwidth='0' marginheight='0' scrolling='no' id='f1app' style='border:0px;padding:0px;z-index:-200;width:" + sWidth + ";height:" + sHeight + ";'>";
	sContent += "<object\n";
	sContent += "classid='clsid:CAFEEFAC-0016-0000-0000-ABCDEFFEDCBA'\n";
	sContent += "codebase='http://java.sun.com/update/1.6.0/jinstall-6u24-windows-i586.cab#Version=1,6,0,0'\n";
	sContent += "WIDTH='" + sWidth + "' HEIGHT='" + sHeight + "' NAME='f1app' VIEWASTEXT>\n";
	sContent += "<PARAM NAME='CODE' VALUE='uk/co/aspectgroup/f1app/f1app.class'>\n";
	sContent += "<PARAM NAME='CODEBASE' VALUE='http://live-timing.formula1.com/java" + LT_iVersion + "/'>\n";
	sContent += "<PARAM NAME='ARCHIVE' VALUE='f1app.jar'>\n";
	sContent += "<PARAM NAME='NAME' VALUE='f1app'>\n";
	sContent += "<PARAM NAME='season' VALUE='" + LT_iSeasonYear + "'>\n";
	sContent += "<PARAM NAME='type' VALUE='application/x-java-applet;version=1.6'>\n";
	sContent += "<PARAM NAME='scriptable' VALUE='false'>\n";
	sContent += "<PARAM NAME='keyframe' VALUE='" + LT_KeyFrame + "'>\n";
	sContent += "<PARAM NAME='textcolour' VALUE='1a1a1a'>\n";
	sContent += "<PARAM NAME='bgcolour' VALUE='dfe2e3'>\n";
	sContent += "<PARAM NAME='streaming' VALUE='" + (LT_IsArchive == 1 ? 0 : 1) + "'>\n";
	sContent += "<param value='transparent' name='wmode' />\n";
	sContent += "<PARAM NAME='language' VALUE='1'>\n";
	sContent += "<PARAM NAME='size' VALUE='" + sJavaSize + "'>\n";
	sContent += "<PARAM NAME='circuit' VALUE='/circuit/" + LT_CircuitName + ".gif'>\n";
	sContent += "<PARAM NAME='user' value='" + LT_UserCookie + "'>\n";
	sContent += "<comment>\n";
	sContent += "<embed\n";
	sContent += "type='application/x-java-applet;version=1.6'\n";
	sContent += "CODE='uk/co/aspectgroup/f1app/f1app.class'\n";
	sContent += "JAVA_CODEBASE='http://live-timing.formula1.com/java" + LT_iVersion + "/'\n";
	sContent += "ARCHIVE='f1app.jar'\n";
	sContent += "NAME='f1app'\n";
	sContent += "season='" + LT_iSeasonYear + "'\n";
	sContent += "wmmode='transparent'\n";
	sContent += "WIDTH='" + sWidth + "'\n";
	sContent += "HEIGHT='" + sHeight + "'\n";
	sContent += "scriptable='false'\n";
	sContent += "keyframe='" + LT_KeyFrame + "'\n";
	sContent += "textcolour='1a1a1a'\n";
	sContent += "bgcolour='dfe2e3'\n";
	sContent += "streaming='" + (LT_IsArchive == 1 ? 0 : 1) + "'\n";
	sContent += "language='1'\n";
	sContent += "size='" + sJavaSize + "'\n";
	sContent += "circuit='/circuit/" + LT_CircuitName + ".gif'\n";
	sContent += "user='" + LT_UserCookie + "'\n";
	sContent += "pluginspage='http://java.sun.com/products/plugin/index.html#download'>\n";
	sContent += "<noembed>Java is required to view this page.</noembed>\n";
	sContent += "</embed>\n";
	sContent += "</comment></object>\n";
	iContent += "</iframe>\n";
	
	$('liveTimingsApplet').innerHTML = iContent;
	$('liveTimingsApplet').style.height = sHeight;
	
    var tFrame = $('f1app');
    var doc = tFrame.contentDocument;
    if (doc == undefined || doc == null)
        doc = tFrame.contentWindow.document;
    doc.open();
    doc.write(sContent);
    doc.close();
}

function ShowHelpForSession(iSessionID)
{
    ShowHelpForSession(LT_iSessionTypeID);
}

function ShowHelpForSession(iSessionID)
{
    //Hide all
    var itemsToHide = $$("#ltnHelp li");
    for (i = 0; i < itemsToHide.length; i++) {
        itemsToHide[i].setStyle("display", "none");
    }

    var itemsToShow;
    itemsToShow = $$(".donotremoveItem");
	for (i = 0; i < itemsToShow.length; i++) {
        itemsToShow[i].setStyle("display", "");
    }

    itemsToShow = $$(".session_" + iSessionID);
	for (i = 0; i < itemsToShow.length; i++) {
        itemsToShow[i].setStyle("display", "");
    }
}

function UpdateLiveTiming()
{
    var n, s, i, x;
    var sSessionTitle, iCurrentSessionID, sSeasonTitle;
    var sCircuitName;

	x = 0;
    n = new Date;

    sSessionTitle = aLiveTimingSessions[0].SessionName;
    sSeasonTitle = aLiveTimingSessions[0].SeasonName;
    sCircuitName = aLiveTimingSessions[0].CircuitName;
    
	for (i = 0; i < aLiveTimingSessions.length; i++)
	{
        s = new Date(aLiveTimingSessions[i].start);
        if ( aLiveTimingSessions[i].SessionTypeID > 0 )
            s.setMinutes(s.getMinutes() - 3000); // Change titles 5 minutes early.

        if (s < n) {
			sSessionTitle = aLiveTimingSessions[i].SessionName;
            sSeasonTitle = aLiveTimingSessions[i].SeasonName;
			LT_iSessionID = aLiveTimingSessions[i].SessionID;
			sCircuitName = aLiveTimingSessions[i].CircuitName;
            if (aLiveTimingSessions[i].SessionTypeID > 0)
			{
			    LT_iSessionTypeID = aLiveTimingSessions[i].SessionTypeID;
			}
		}
		else
		{
			//get time for next update
            x = s.getTime() - n.getTime(); // update at the start of the next session

            if (x <= 0)
			{
                x = 1;
            }

            break;
        }
    }

    ShowHelpForSession(LT_iSessionTypeID);
    document.getElementById("LiveTimingSession").childNodes[0].nodeValue = "(" + sSessionTitle + ")";
    document.getElementById("LiveTimingSeason").childNodes[0].nodeValue = sSeasonTitle;

    if (x > 0) {
        setTimeout("UpdateLiveTiming()", Math.min(x, 5 * 60 * 1000));
    }
    return sCircuitName;
}

window.addEvent('domready', initTimingsHelpNav);
window.addEvent('domready', writeApplet);
