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
var animationIndex = 0;
var animationTimeout = 0;

function fpAnimate()
{
	animationIndex++;
	if (animationIndex >= $("#fpArt_" + activeFP + " > div").length) animationIndex = 0;
	fpUpdate(1);
	animationTimeout = setTimeout(fpAnimate, 7000);
}

function fpStopAnimation()
{
	clearTimeout(animationTimeout);
}

function fpUpdate(direction)
{
	$(".fpVideoWrapper").hide();
	$(".overlay").show();
	$(".videoicon").hide();
	$(".videoiconV").hide();
	var n = $("#fpArt_" + activeFP + " > div").length;
	var a1 = animationIndex;
	var a2 = a1 + 1 < n ? a1 + 1 : 0;
	var a3 = a2 + 1 < n ? a2 + 1 : 0;
	
	var fp = $("#fpArt_" + activeFP);
	//fp.find(".title").hide();
	if (direction > 0){
		fp.find("#fp" + a3).css({left: 1000});
		fp.find(".fp1").removeClass("fp1").addClass("fpD").css({left: 1000});
	}
	else {
		fp.find("#fp" + a1).css({left: -200});
		fp.find(".fp3").removeClass("fp3").addClass("fpD").css({left: 1000});
	}
	fp.find(".arrowLeft").hide();
	fp.find(".arrowRight").hide();
		
	fp.find("#fp" + a1).animate({ top: 60, left: 3, width: 147, height: 316 }, 500, function() {
		//$(this).find(".title").css({"top": 0, "margin-top": 0}).show();
  });
	fp.find("#fp" + a1).find(".overlay").animate({ opacity: 0.7 });
	
	fp.find("#fp" + a2).animate({ top: 15, left: 152, width: 650, height: 360 }, 500, function() {
		//$(this).find(".title").css({"margin-top": 360}).show().animate({marginTop: 260}, 200);
		//$(this).find(".title").css({"top": 360, "margin-top": 360}).show().animate({marginTop: 260, top: 260}, 200);
		fp.find(".arrowLeft").fadeIn();
		fp.find(".arrowRight").fadeIn();
  });
	fp.find("#fp" + a2).find(".overlay").animate({ opacity: 0.0 });

	fp.find("#fp" + a3).animate({ top: 60, left: 808, width: 147, height: 316 }, 500, function() {
		//$(this).find(".title").css({"top": 0, "margin-top": 0}).show();
  });
	fp.find("#fp" + a3).find(".overlay").animate({ opacity: 0.7 });
	
	fp.find("#fp" + a1).removeClass("fpD fp1 fp2 fp3").addClass("fp1");
	fp.find("#fp" + a2).removeClass("fpD fp1 fp2 fp3").addClass("fp2");
	fp.find("#fp" + a3).removeClass("fpD fp1 fp2 fp3").addClass("fp3");
	$(".fp2").find(".videoicon").show();
	$(".fp2").find(".videoiconV").show();
}

function fpSwitchTabs(linkID, position, id, reload)
{
	clearTimeout(animationTimeout);
	$(".fpReiterA").removeClass("fpReiterA").addClass("fpReiter");
	$("#fpReiter_" + id).removeClass("fpReiter").addClass("fpReiterA");
	if (reload){
		$("#fpLoading").css("opacity", "0.4");
		$("#fpLoading").show();
		$.ajax({
			url: "getRawModule.asp?id=" + linkID + "&aTab=" + position + "&sTab=-1",
			type: "GET",
			dataType: "html",
			timeout: 10000,
			success: function(data) {
				$("#fpLoading").fadeOut();
				$("#fp").html($(data).find("#fp").html());
				$(".fpVideoWrapper").each(function(){
					fpAddVideo($(this).attr("id").substr(15), $(this).attr("title"));
				});
				activeFP = id;
				animationIndex = $("#fpArt_" + id + " > div").length - 2;
				fpAnimate();
			}
		});
	}
	else {
		activeFP = id;
		animationIndex = $("#fpArt_" + id + " > div").length - 2;
		fpAnimate();
	}
}

function fpScroll(direction)
{
	clearTimeout(animationTimeout);
	animationIndex += direction;
	var n = $("#fpArt_" + activeFP + " > div").length;
	if (animationIndex >= n) animationIndex = 0;
	if (animationIndex < 0) animationIndex = n-1;
	fpUpdate(direction);
}

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)
{
	$("#fpVideoWrapper_"+id).parent().find(".overlay").hide();
	fpStopAnimation();
	$("#fpVideoIcon_"+id).hide();
	$("#fpVideo_"+id).css("visibility", "visible");
	$("#fpVideoWrapper_"+id).show();
}

// Navigation stuff
$(document).ready(function() {
	$("#thirdLevelNavigation").find(".menuItem, .menuItemA").mouseenter(function(){
		$(".fourthLevelNavigation").stop(true, true).hide();
		var id = this.id.substring(15);
		if ($("#content4thMenu_" + id).length > 0){
			$("#content4thMenu_" + id).css({ "left": "8px", "top": "30px" });
			$("#content4thMenu_" + id).show("fast");
		}	
	});
	
	$("#thirdLevelNavigation").find(".menuItem, .menuItemA").mouseleave(function(e){
		$(".fourthLevelNavigation").stop(true, true).hide();
		if (e.pageY < $(this).offset().top + $(this).height()){
			$(".fifthLevelNavigation").hide();
		}
	});
	
	$(".fourthLevelNavigation").mouseleave(function(e){
		$(".fourthLevelNavigation").stop(true, true).hide();
		$(".fifthLevelNavigation").hide();
	});
	
	$(".fourthLevelNavigation").find(".menu").mouseenter(function(){
		var id = this.id.substring(15);
		var parent = $("#content4thItem_" + id).parent();
		$(".fifthLevelNavigation").stop(true, true).hide();
		if ($("#content5thMenu_" + id).length > 0){
			$("#content5thMenu_" + id).show("fast");
		}
	});
});

// 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());
}

// Banner functionality
var Banners = new Array();
var bannerIndex = 0;

$(document).ready(function() {
	bannerIndex = 0;
	if (Banners.length > 1) setTimeout(function(){ nextBanner() }, 1500);
});

function nextBanner()
{
	if (++bannerIndex >= Banners.length) bannerIndex = 0;
	$("#bannerImage").animate({"top": "100px"}, "slow", "", function(){
		$("#bannerImage").attr("src", "files/" + Banners[bannerIndex][0]);
		$("#bannerLink").attr("href", Banners[bannerIndex][1]);
		$("#bannerImage").attr("alt", Banners[bannerIndex][2]);
		$("#bannerImage").css({"top": "-100px"});
		$("#bannerImage").animate({"top": "0px"}, "slow");
	});
	setTimeout(function(){ nextBanner() }, 15000);
}

// Quotes module scripts
function quotesObject(moduleID){
	this.quotesCount = 0;
	this.quotesArr = new Array();
	this.id = moduleID;
	this.currentIndex = 0;

	this.addArticle = function(pHeadline, pSubhead, pPreview, pDate) {
		var article = new function(){
			this.headline = pHeadline;
			this.subhead  = pSubhead;
			this.preview  = pPreview;
			this.date     = pDate;
		}
		this.quotesArr[this.quotesCount] = article;
		this.quotesCount++;
	}

	this.test = function(){
		alert(this.id);
	}

	this.switchQuote = function(){
		document.getElementById('quotetext' + this.id).innerHTML = this.quotesArr[this.currentIndex].preview;
		document.getElementById('quotedate' + this.id).innerHTML = this.quotesArr[this.currentIndex].date;
		var str = this.quotesArr[this.currentIndex].headline;
		if (this.quotesArr[this.currentIndex].subhead != ''){
			str = str + ' (' + this.quotesArr[this.currentIndex].subhead + ')';
		}
		document.getElementById('quotewho' + this.id).innerHTML = str;
	}

	this.nextQuote = function(){
		this.currentIndex++;
		if (this.currentIndex >= this.quotesCount){
			this.currentIndex = 0;
		}
		this.switchQuote();
		return false;
	}

	this.prevQuote = function(){
		this.currentIndex--;
		if (this.currentIndex < 0){
			this.currentIndex = this.quotesCount-1;
		}
		this.switchQuote();
		return false;
	}
}
var quotes = new Array();

// FP ticker
function fpTicker(position){
	var currentTicker = $("#ticker .tickerItem:eq(" + position + ")");
	position++;
	if (position >= $("#ticker .tickerItem").length) position = 0;
	var nextTicker = $("#ticker .tickerItem:eq(" + position + ")");
	var width = 0 - currentTicker.width() - 10; 
	currentTicker.animate({"right": width}, "slow", "", function(){
		nextTicker.animate({"right": 10}, "slow");
	});
	setTimeout(function() { fpTicker(position) }, 2000);
}
