﻿var AllImages = new Object;
var count=0;
var CurrentImage=0;
var rTimer;
var OpicityFilter;

function initImage(imageId) {
	image = document.getElementById(imageId);
	setOpacity(image, 0);
	fadeIn(imageId,0);
}
function fadeIn(objId,opacity) {
	if (document.getElementById) {
		obj = document.getElementById(objId);
		if (opacity <= 100) {
			setOpacity(obj, opacity);
			opacity += 5;
			OpicityFilter= setTimeout("fadeIn('"+objId+"',"+opacity+")", 50);
		}
	}
}
function setOpacity(obj, opacity) {
	opacity = (opacity == 100)?99.999:opacity;
	// IE/Win
	obj.style.filter = "alpha(opacity:"+opacity+")";
	// Safari<1.2, Konqueror
	obj.style.KHTMLOpacity = opacity/100;
	// Older Mozilla and Firefox
	obj.style.MozOpacity = opacity/100;
	// Safari 1.2, newer Firefox and Mozilla, CSS3
	obj.style.opacity = opacity/100;
}


//////////////////////////////////////////////////////

function SetArrayValues(ImageURL,Description,Date,Title,URL,IsMainItem)
{


	if((IsMainItem=='Yes')&&(count<4))
		AddItem(ImageURL,Description,Date,Title,URL);
    
    //if it's not a main item, then there should be at least one main item before it
    else if((count==1)&&(IsMainItem=='No'))
		AddItem(ImageURL,Description,Date,Title,URL);

}
///////////////////////////////////////////////////////
function AddItem(ImageURL,Description,Date,Title,URL)
{

				if(count==0)
				{
				var NumbersHTML='<div id="Num3" style="display:none;" onclick="showdescription(\'3\')" class="NumberOff">4</div>'+
					'<div id="Num2" style="display:none;" onclick="showdescription(\'2\')" class="NumberOff">3</div>'+
					'<div id="Num1" style="display:none;" onclick="showdescription(\'1\')" class="NumberOff">2</div>'+
					'<div id="Num0" style="display:none;" onclick="showdescription(\'0\')" class="NumberOn">1</div>';
					
				document.getElementById('MainNews').innerHTML='<div class="MainNewsArea">'+
				'<div class="MainNews_mn_img" id="thephoto">'+
				'<div class="MainNews_img_brdr">'+
				'<div id="player" align="center"> </div>'+			
				'</div>'+
				'</div>'+
				'<div class="MainNewsDesc right4">'+
				'<div id="divTitle" class="MainNews_ttl">'+
				'</div>'+
				'<div id="divDate" class="MainNews_date">'+
				'</div>'+
				'<div id="divdesc" class="MainNewsDesc_txt">'+            
				'</div>'+
				'</div>'+
				'<div class="NumbersArea">'+NumbersHTML
				+
				'</div>'+
				'</div>';
				}
				
				var MyImage = new Object;
				
           		MyImage.Description = Description;
           		MyImage.Date = Date;
           		MyImage.Title = Title;
           		MyImage.URL = URL;
                MyImage.Image= ImageURL;
                
            	AllImages[count] = MyImage; 
            	document.getElementById('Num'+count).style.display="block";
				count++;

}

///////////////////////////////////////////////////////////
//Handles the display of the data and calling the timer to loop again:
function showdescription(Index)
{

		//alert("count= "+count+", Current Image= "+ CurrentImage+", Index= "+Index);
		var Site=GetCurrentSite();
		var Link;
		
		if(Site=="English")
			Link='Read Full Story';
		else
			Link=' إقرأ القصة كاملة';
		if(rTimer);
        clearTimeout(rTimer);

		if(OpicityFilter)
		clearTimeout(OpicityFilter);
		
		if(CurrentImage!=Index)
      		 CurrentImage=Index;
        if(AllImages[Index]!=null)
        {
        var description= AllImages[Index].Description;
        var date= AllImages[Index].Date;
        var Title= AllImages[Index].Title;
        var URL= AllImages[Index].URL;
        var Image= AllImages[Index].Image;
	
      	document.getElementById('thephoto').style.backgroundImage='url(\''+Image+'\')';
        //initImage('thephoto');
        fadeIn('thephoto',0);
		document.getElementById('divTitle').innerHTML ='<a href='+ URL + '>' + Title + '</a>';
		
		document.getElementById('divdesc').innerHTML = description + '<a href='+URL+'>'+Link+'</a>';
		document.getElementById('divDate').innerHTML =date;
		FindElementContainsClass('NumberOn','div').className='NumberOff';
		document.getElementById('Num'+Index).className='NumberOn';
		
		CurrentImage++;	
		 }
		 else
		    CurrentImage=0;

		 Timer();
		
}
///////////////////////////////////////////////////////////

function Timer(){
				
	if(rTimer)
        clearTimeout(rTimer);
    rTimer = setTimeout('showdescription(CurrentImage)', 10000);
}
///////////////////////////////////////////////////////////////
function GetCurrentSite()
{
	var Location= document.location.href;
	var Site;

	if (Location.toLowerCase().search("english")!=-1)
		Site="English";
	else
		Site="Arabic";
		
	return Site;
}
