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?gameID=" + 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)
{
	$(".fpEmbTitle .icon").attr("src", "images/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", "images/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)
{
	$(".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));
				}
				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.cbcv.com/ptvFlash/unifiedplayer/fibavod/data/xml/config/unified_config-vod.xml", 
			resourceLocalePath:"http://www.cbcv.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.cbcv.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);
}

function fp2SwitchArticle(linkID, position, id, summary, reload)
{
	$(".fp2TabA").removeClass("fp2TabA").addClass("fp2Tab");
	$("#fp2Tab_" + id).removeClass("fp2Tab").addClass("fp2TabA");
	$('#frontPage2Links').html(summary);

	if (reload){
		$("#fpLoading2").show();
		$("#fpLoading2").css("opacity", "0.4");
		$.ajax({
			url: "getRawModule.asp?id=" + linkID + "&aTab=" + position + "&sTab=-1",
			type: "GET",
			dataType: "html",
			timeout: 10000,
			success: function(html) {
				$("#fpLoading2").hide();
				$("#fp2Container").html($(html).find("#fp2Container").html());
			}
		});
	}
}

function fp2Scroll(id, direction)
{
	var scrollTo = $("#fp2ArtScroll_" + id).scrollLeft() + 250 * direction;
	var scrollWidth = $("#fp2ArtScroll_" + id)[0].scrollWidth;
	var width = $("#fp2ArtScroll_" + id).width();
	if (scrollTo < 0) scrollTo = 0;
	if (scrollTo > scrollWidth - width) scrollTo = scrollWidth - width;
	$("#fp2ArtScroll_" + id).stop(true, true).animate({scrollLeft: scrollTo}, 300);
	return false;
}

// Competition menu functionality
function toggleCompMenu()
{	
	$('#divAllCompetitions').css( {"none" : $('#allCompMenu').offset().left - $('#divAllCompetitions').width() + $('#allCompMenu').width() + "px", "top" : $('#allCompMenu').offset().top + $('#allCompMenu').height() + 1 + "px" } );
	$('#allCompMenu').toggleClass('active');
	$('#divAllCompetitions').toggle();
	if ($('#divAllCompetitions').is(':visible')){
		$('#divAllCompetitions').animate({"height": 200});
		$('#btnAllCompetitions').attr("src", "images/ico-arrow-up.png");
	}
	else {
		$('#divAllCompetitions').height(0);
		$('#btnAllCompetitions').attr("src", "images/ico-arrow-down.png");
	}
}



$(document).ready(function() {
	$("#secondLevelNavigation").find(".menuItem, .menuItemA").mouseenter(function(){
		$(".thirdLevelNavigation").stop(true, true).hide();
		var id = this.id.substring(15);
		if ($("#content3rdMenu_" + id).length > 0){
			$("#content3rdMenu_" + id).css({ "left": $(this).offset().left + 8 + "px", "top": $(this).offset().top + $(this).height() + "px" });
			$("#content3rdMenu_" + id).show("fast");
		}	
	});
	
	$("#secondLevelNavigation").find(".menuItem, .menuItemA").mouseleave(function(e){
		$(".thirdLevelNavigation").stop(true, true).hide();
		if (e.pageY < $(this).offset().top + $(this).height()){
			$(".fourthLevelNavigation").hide();
		}
	});
	
	$(".thirdLevelNavigation").mouseleave(function(e){
		$(".thirdLevelNavigation").stop(true, true).hide();
		$(".fourthLevelNavigation").hide();
	});
	
	$(".thirdLevelNavigation").find(".menu").mouseenter(function(){
		var id = this.id.substring(15);
		var parent = $("#content3rdItem_" + id).parent();
		$(".fourthLevelNavigation").stop(true, true).hide();
		if ($("#content4thMenu_" + id).length > 0){
			$("#content4thMenu_" + id).show("fast");
		}
	});
});

// Banner functionality
var Banners = new Array();
var bannerIndex = 0;

$(document).ready(function() {
	bannerIndex = 0;
	if (Banners.length > 1) nextBanner();
});

function nextBanner()
{
	if (++bannerIndex >= Banners.length) bannerIndex = 0;
	
	$("#bannerLink").animate({"marginTop": "100px"}, "slow", "", function(){
		$("#bannerImage").attr("src", "files/" + Banners[bannerIndex][0]);
		$("#bannerLink").attr("href", Banners[bannerIndex][1]);
		$("#bannerImage").attr("alt", Banners[bannerIndex][2]);
		$("#bannerLink").css({"marginTop": "-100px"});
		$("#bannerLink").animate({"marginTop": "0px"}, "slow");
	});
	setTimeout(function(){ nextBanner() }, 15000);
}

// 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());
}
