function examineForm(formName, fieldName){
  var form = document.forms[formName];
  if (form.elements[fieldName].value.length > 0){
    form.submit();
  }
}
function dsp_photo(url, w, h){
  var winphoto=window.open(url, 'photo', 'width='+w+',height='+h+',status=yes,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes,screenX=0,screenY=0');
  winphoto.focus();
}
function open_live(game_number, micrositescope){
	window.open("http://live.fibaeurope.com/www/Game.aspx?game_number=" + game_number, "LiveGames", "width=1067,height=725,status=no,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=yes", true);
}

// Embedded page functions 
function switchTabs(id)
{
	if (id != ''){
		curID = id;
		var tabBox = document.getElementById('emb_tabs');
		for (var i=0; i<tabBox.childNodes.length; i++){
			var tab = tabBox.childNodes[i];
			tab.className = 'tab_item';
		}
		tabBox = document.getElementById('tab_'+id);
		tabBox.className = 'tab_item_act';
	
		var divBox = document.getElementById('emb_divs');
		for (var i=0; i<divBox.childNodes.length; i++){
			var div = divBox.childNodes[i];
			div.style.display = 'none';
		}
		divBox = document.getElementById('emb_'+id);
		divBox.style.display = 'block';
		window.location.hash = id;
	}
}

function embExpand(id, prefix)
{
	$(".fpEmbTitle .icon").attr("src", prefix + "/ico-plus.png");
	if ($("#fpEmbCont_" + id).is(":visible")){
		$("#fpEmbCont_" + id).find(".fpOtherInner").animate({"height" : "0px"}, function(){
			$("#fpEmbCont_" + id).hide();
		});
	}
	else {
		$(".fpEmbCont").hide();
		$("#fpEmbCont_" + id).find(".fpOtherInner").css({"height": 0});
		var count = $(".fpEmbCont").length;
		var availableHeight = $("#fpEmbCont_" + id).parent().height();
		$("#fpEmbCont_" + id).show();
		$("#fpEmbCont_" + id).find(".fpOtherInner").animate({"height" : availableHeight - count*24 - 2*21})
		$("#fpEmbTitle_" + id).find(".icon").attr("src", prefix + "/ico-minus.png");
	}
}
	
// Front page functions
function fpSwitchLeft(linkID, position, id, reload)
{
	$(".fpl_tab_enabled").hide();
	$(".fpl_tab_disabled").show();
	$("#tab_" + id + "_disabled").hide();
	$("#tab_" + id + "_enabled").show();
	clearTimeout(fpAnimation);
	if (reload){
		$("#fpLoadingA").css("opacity", "0.4");
		$("#fpLoadingA").show();
		$.ajax({
			url: "getRawModule.asp?id=" + linkID + "&aTab=" + position + "&sTab=-1",
			type: "GET",
			dataType: "html",
			timeout: 10000,
			success: function(html) {
				$("#fpLoadingA").hide();
				$("#articlesContainer").html($(html).find("#articlesContainer").html());
				var artID = $("#fpArt_" + id).find("td.fpArticleTab:eq(0)").attr("id").substr(13);
				fpSwitchArticle(artID, true);
				$(".fpVideoWrapper").each(function(){
					fpAddVideo($(this).attr("id").substr(15), $(this).attr("title"));
				});
			}
		});
	}
	else {
		var artID = $("#fpArt_" + id).find("td.fpArticleTab:eq(0)").attr("id").substr(13);
		fpSwitchArticle(artID, true);		
	}
}

function fpSwitchRight(linkID, position, id, reload, prefix)
{
	$(".fpr_tab_enabled").hide();
	$(".fpr_tab_disabled").show();
	$("#tab_" + id + "_disabled").hide();
	$("#tab_" + id + "_enabled").show();
	if (reload){
		$("#fpLoadingS").css("opacity", "0.4");
		$("#fpLoadingS").show();
		$.ajax({
			url: "getRawModule.asp?id=" + linkID + "&aTab=-1&sTab=" + position,
			type: "GET",
			dataType: "html",
			timeout: 10000,
			success: function(html) {
				$("#fpLoadingS").hide();
				$("#otherContainer").html($(html).find("#otherContainer").html());
				// Detect and initiate scoreboards and embedded pages in the loaded html
				if ($("#otherContainer").find(".fpEmbCont").length > 0){
					embExpand($(".fpEmbCont:eq(0)").attr("id").substr(10), prefix);
				}
				if ($("#otherContainer").find(".sbBox").length > 0){
					$("#otherContainer").find(".sbBox").each(function(){
						initScoreboard($(this).attr("id"));
					});
				}
			}
		});
	}
}

var fpAnimation;
function fpSwitchArticle(id, animate)
{
	$(".fpArticleTabA").removeClass("fpArticleTabA").addClass("fpArticleTab");
	$("#fpArticleTab_" + id).removeClass("fpArticleTab").addClass("fpArticleTabA");
	$(".fpVideoWrapper").hide();
	$(".fpArtBig").hide();
	$("#fpArtBig_"+id).fadeIn();
	
	if (animate){
		var nextTabID = $("#fpArticleTab_" + id).next("td").next("td").attr("id");
		if (!nextTabID){
			nextTabID = $("#fpArticleTab_" + id).siblings(":eq(0)").attr("id");
		}
		fpAnimation = setTimeout(function(){ fpSwitchArticle(nextTabID.substr(13), true) }, 5000);
	}
	else {
		clearTimeout(fpAnimation);
	}
}

function fpAddVideo(id, clip_id)
{
	if (swfobject.hasFlashPlayerVersion("10")) {
		var params = { 
			wmode:"transparent", 
			allowScriptAccess:"always", 
			bgcolor: "#ffffff", 
			allowFullScreen:"true"
		};
		var flashVars = {
			configXML: "http://www.fibatv.com/ptvFlash/unifiedplayer/fibavod/data/xml/config/unified_config-vod.xml", 
			resourceLocalePath:"http://www.fibatv.com/ptvFlash/unifiedplayer/aljazeera/assets/resources/", 
			setPlayerType:"vod", 
			locale:"en_GB",
			disableControls:"false", 
			siteId:"12805",
			useInitialSplashScreen:"false",
			useMetaSizing:"true",
			autoPlay:"false",
			clipId: clip_id
		};
		swfobject.switchOffAutoHideShow();
		swfobject.embedSWF("http://www.fibatv.com/ptvFlash/unifiedplayer/fibavod/UnifiedPlayer.swf", "fpVideo_"+id, "656", "369", "10", false, flashVars, params, {})
	}
	swfobject.registerObject("fpVideo_"+id, "10.0.0", "/ptvFlash/SWFObject/SWFObject2_2/expressInstall.swf");
}

function fpShowVideo(id)
{
	clearTimeout(fpAnimation);
	$("#fpArtBig_"+id).hide();
	$("#fpVideo_"+id).css("visibility", "visible");
	$("#fpVideoWrapper_"+id).show();
}

function scroll(id, direction)
{
	var scrollTo = $("#fpOtherInner_" + id).scrollTop() + 250 * direction;
	var scrollHeight = $("#fpOtherInner_" + id)[0].scrollHeight;
	var height = $("#fpOtherInner_" + id).height();
	if (scrollTo < 0) scrollTo = 0;
	if (scrollTo > scrollHeight - height) scrollTo = scrollHeight - height;
	$("#fpOtherInner_" + id).animate({scrollTop: scrollTo}, 300);
}

// Scoreboard functions
function swapBoxParent(linkID, idElement) {
  $(".parentBox"+linkID).hide();
  $("#"+idElement).show();
  $("#"+idElement+" ul.tabNavigationSB a").filter(':first').click();
}

function initScoreboard(linkID) {
  var tabContainers = $('#'+linkID).find('div.tabSB > div');
  tabContainers.hide().filter(':first').show();
        
  $('#'+linkID).find('div.tabSB ul.tabNavigationSB a').click(function () {
		$("#"+$(this).attr("rel")+" > div").hide();
        
    tabContainers.filter(this.hash).show();
    $("#"+$(this).attr("rel")+" ul.tabNavigationSB a").removeClass('selected');
    $(this).addClass('selected');
     
    return false;
  }).filter(':first').click();
  swapBoxParent(linkID, $("#selectForm"+linkID).val());
}
