var table_id; /*an object which hold the image source variables*/
var j=0;    /*index for the loop*/  
var str_id; 
var holdtags;
var LastTdElement ; 
var LastFontElement;
var lastImg = ""  ;
var up_image=new Image(); 
var down_image=new Image();                                                     

var image_id;                                                                                      /*hold the identification value of an image*/
var str_name;                                                                                     /*hold the string image name after cutting by substring method*/
var image_name;                                                                             /*hold the location of the image in monovalent way*/
var i=0;        
var images_tage;
var last_image_id ;
var LastButtenWasPress ; 

var subList = new Array(); 
var lastTypId ;
var lastTypeId ; 
var oNode ; 
var TypeID = "" ;
var fixGalleryStr = "" ; 

function fixGallery(TypeId){
	fixGalleryStr = fixGalleryStr + TypeId ; 
	collObjects = document.getElementsByTagName("TD");
	for(var j=0 ; j < collObjects.length; j++){
	
		if (collObjects[j].id.indexOf("ptr_") > -1   ){
			title = collObjects[j].innerText ; 
			Linkobj = eval("document.all.A_" + collObjects[j].id );
			temp = title ;
			if (temp.length > 20) {
			
			if( Linkobj.innerHTML.indexOf(".gif") > -1)
				_length = 20 ; 
			else
				_length = 17 ; 
			
			temp = temp.substring( 3  , _length );
			
			Linkobj.innerHTML  = temp + "..." ; 
			Linkobj.title = title ; 
			}
		}	
	}
}


var lastinitStr= ""

function init(initStr , from ){



if(lastinitStr.indexOf(initStr) == -1  || lastinitStr == ""){

lastinitStr = initStr ;

itemId = -1 ;  TypeID= -1 ; colId = -1 ; catId = -1 ; 
	
	if(initStr){
		params = initStr ;
	}
	else{
		params = location.href;
		if(params.indexOf('?') > -1 ){
			 params = params.substring(params.indexOf('?')+1 ,params.length );
		}
		else{
		params="";
		}
	}
	
	fieldValuePairs = params.split("&");
	var catId = -1 ;
	for (var i=0 ; i < fieldValuePairs.length ;  i++ ){
		
		if (fieldValuePairs[i].indexOf("itemId")> -1){
			itemId = fieldValuePairs[i].substring(fieldValuePairs[i].indexOf('=')+1 ,fieldValuePairs[i].length );
			window.scrollTo(0,0);
		}
		else if((fieldValuePairs[i].indexOf("typeId") > -1)  || fieldValuePairs[i].indexOf("typeID") > -1){
			TypeID = fieldValuePairs[i].substring(fieldValuePairs[i].indexOf('=')+1 ,fieldValuePairs[i].length );
			
		}
		else if(fieldValuePairs[i].indexOf("colId") > -1 ){
			colId = fieldValuePairs[i].substring(fieldValuePairs[i].indexOf('=')+1 ,fieldValuePairs[i].length );
			
		}
		else if (fieldValuePairs[i].indexOf("catId") > -1){
			catId = fieldValuePairs[i].substring(fieldValuePairs[i].indexOf('=')+1 ,fieldValuePairs[i].length );
		}
	}
	
	
	
	
	if((params.indexOf("typeId") == -1)  && (params.indexOf("typeID") == -1)){
		TypeID = 1 ; 
	}
	
	
	if (((catId == -1) && (colId == -1) && (itemId == -1)) || (params.indexOf('most_popular') > -1) || params == "")
	{
			if (params == ""){
				TypeID=1;
			}
			if(lastImg){
				eval("document.all." + lastImg  + ".style.backgroundColor = '#EFEBF7'");
				eval("document.all.A_" + lastImg  + ".style.color = '#31309C'");
				lastImg="";
			}
			
			if(LastTdElement)
			{
				LastTdElement.bgColor = "EFEBF7";
				LastFontElement.style.color = "31309C";
				LastTdElement = null
			}	
			MostPopular.style.backgroundColor = '31309C';
			document.all['MostPopular'].innerHTML = "<font id='MostPopularFont' style='color:#EFEBF7' > Most Popular</font>";
			lastTypeId = "" ; 
	}
	
	
	if ( TypeID == ""  ){
		TypeID = 1 ;
	}
	
	
	if (itemId != -1 && colId == -1 ){
	}
	else
	{
			
		changeMenu (TypeID , eval('document.all.bar_' +  TypeID   ) );
			
		if (catId == -1 || catId ==  -1 ){	
		}
		else{
				if (TypeID == 1){
				dHide(catId);
			}
			cPopLink(TypeID);
			
			if ( colId !=  -1 ){
				imgId =  "ptr_" +  colId ;
				mC(imgId);
			
				if (catId != -1 ){
						TdElement = document.getElementById("td_" + catId) ;	
						FontElement = document.getElementById("A_td_" + catId) ; 
						TdElement.bgColor = "EFEBF7" ;
						FontElement.style.color= "31309C" ;
						LastTdElement = TdElement ;
						LastFontElement = FontElement ; 
						
				}
			}
			else if (colId ==  -1){
				
				if(lastImg){
					eval("document.all." + lastImg  + ".style.backgroundColor = '#EFEBF7'");
					eval("document.all.A_" + lastImg  + ".style.color = '#31309C'");
				}
				lastImg = "" ; 
			}
		}
	}
	}
}


function hide(){

	if (lastImg != "" ){
	eval("document.all." + lastImg  + ".style.backgroundColor = '#EFEBF7'");
		eval("document.all.A_" + lastImg  + ".style.color = '#31309C'");
	lastImg = "" ; }
	if(LastTdElement){
		id = LastTdElement.id.substring (LastTdElement.id.indexOf("_") + 1 , LastTdElement.id.length);
		MostPopular.removeChild(oNode);
		MostPopular.innerHTML = ("<font id='MostPopularFont' style='color:EFEBF7' >&nbsp;Most Popular</font>"); 
		MostPopular.style.backgroundColor  = "31309C";
		if(TypeID == 1){
		eval("document.all.subtable_" + id + ".style.display = 'none'");}
		LastTdElement.bgColor = "EFEBF7" ;
		LastFontElement.style.color= "31309C" ; 
	}
	else{
		MostPopular.innerHTML = ("<font id='MostPopularFont' style='color:EFEBF7' >&nbsp;Most Popular</font>"); 
		MostPopular.style.backgroundColor  = "31309C";
	}
	LastTdElement= null ;
	lastTypeId= "" ; 
	
}

function dHide(table_id){
    holdtags=document.all.tags("TR");
	TdElement = document.getElementById("td_" + table_id) ;			
	FontElement = document.getElementById("A_td_" + table_id) ; 
	if(LastTdElement)
	{
		if (TdElement.id != LastTdElement.id){
			LastTdElement.bgColor = "EFEBF7";
			LastFontElement.style.color = "31309C";
		}
	}	
	
	if (TdElement.bgColor != "31309C"){	
		TdElement.bgColor = "31309C" ;
		FontElement.style.color= "EFEBF7" ; 
	}
	else{
		TdElement.bgColor = "EFEBF7" ;
		FontElement.style.color= "31309C" ; 
	}			
	for(j=0; j< holdtags.length; j++){
		if(holdtags[j].id.substring(0,holdtags[j].id.lastIndexOf('_'))=='subtable'){
		str_id=holdtags[j].id.substring(0,holdtags[j].id.lastIndexOf('_'));
		if(holdtags[j].id!=str_id+'_'+table_id){
				holdtags[j].style.display='none';
		}
							                    
		if(holdtags[j].id==str_id+'_'+table_id){
			if(holdtags[j].style.display=='none'){
				holdtags[j].style.display='';
			}
			else
			{
				holdtags[j].style.display='none';
			}
		}
	} 
	window.focus();
	}
	
	LastTdElement = TdElement ;
	LastFontElement = FontElement ;
}
/* Set of function which change the category images onMouseOver and OnMouseOut */
function changeMenu (TypeID , obj )
{
	
	if(TypeID != lastTypId ){

	MenyMoreGif.style.display = 'inline';
		if (TypeID == 2) {
			manyMore.style.display = 'inline';
			manyMoreLink.href = "http://www.incredimail.com/english/LearnHow_add_A.html" ; 
			MenyMoreGif.src = "http://www2.incredimail.com/english/images/banners/home_letter_small.gif";
		MenyMoreGifLink.href = "http://www.incredimail.com/english/lc/download_Splash.html";
		}
		else if(TypeID == 4){
			manyMore.style.display = 'inline';
			manyMoreLink.href = "http://www.incredimail.com/english/LearnHow_add_S.html" ; 
			MenyMoreGif.src = "http://www2.incredimail.com/english/images/gallery/backg_sound_big.gif";
			MenyMoreGifLink.href = "http://www.incredimail.com/english/faq/add_backgroundSound.html";
		}
		else if (TypeID == 5){
			manyMore.src = "http://www2.incredimail.com/english/images/banners/home_letter_small.gif";
			MenyMoreGifLink.href = "http://www.incredimail.com/english/lc/download_Splash.html";
		}
		else{
			
			manyMore.style.display = 'inline';
			manyMoreLink.href = "http://www.incredimail.com/english/web_ring.html" ; 
			MenyMoreGif.src = "http://www2.incredimail.com/english/images/banners/home_letter_small.gif";
			MenyMoreGifLink.href = "http://www.incredimail.com/english/lc/download_Splash.html";
		}
 		lastTypId = 	TypeID ;
	}

		

	if( LastButtenWasPress != obj.name  )
	{
		MostPopular.style.backgroundColor = '31309C';
		MostPopular.innerHTML = ("<font id='MostPopularFont' style='color:#EFEBF7' >&nbsp;Most Popular</font>"); 
		LastButtenWasPress = obj.name ;
		for ( i=1 ; i < 7; i++)
		{
			if( eval ("document.images['bar_" + i + "']" ))
			{
				ImgObj = eval ("bar_"  + i  );
				oldSrc = ImgObj.src  ;
				if (TypeID == i )
				{
					indexOfGif = oldSrc.lastIndexOf(".gif");
					newSrc = oldSrc.substring (0 , indexOfGif) + "_chosen.gif" 
					ImgObj.src = newSrc   ;
				}
				else
				{
					indexOfGif = oldSrc.lastIndexOf("_chosen.gif");
					if(indexOfGif > -1 )
					{
				  		newSrc = oldSrc.substring (0 , indexOfGif) + ".gif" 
						ImgObj.src = newSrc   ;
					}
				}
			}
		}
	}
	for ( i=0 ; i < subList.length ; i++)
	{
		
		if( TypeID > 1 )
		{
			eval("document.all.categoryTitle_" + subList[i]  + ".style.display = 'none'");	
		}
		else{
			if(subList[i] > 6  ){
				eval("document.all.categoryTitle_" + subList[i]  + ".style.display = 'inline'");
			}
		}
		if( TypeID ==  subList[i] ){
			eval("document.all.subtable_" + subList[i] + ".style.display = 'inline'");
		}else{
			
			eval("document.all.subtable_" + subList[i] + ".style.display = 'none'");
		}
	}
	

	//GalleryFrame.location.href = "http://www.incredimail.com/english/gallery_frame.asp?most_popular=1&typeId=" +  TypeID ; 
}


function cPopLink(typeId)
{

	
	if (lastTypeId != typeId)
	{
		lastTypeId = typeId  ;
		if(typeId == 0  )
		{
			MostPopular.style.backgroundColor = '31309C';
			document.all['MostPopular'].innerHTML = "<font id='MostPopularFont' style='color:#EFEBF7' >Most Popular</font>";
			hide();
		}
		else
		{
			oChild=MostPopular.children(0);	
    		MostPopular.removeChild(oChild);
			oNode = document.createElement("a");
			oNode.setAttribute("href", "gallery_frame.asp?most_popular=1&typeId=" + typeId );
			oNode.setAttribute("target", "gf");
			oNode.attachEvent("onclick", hide );
			oNode.innerText="";
			oNode.innerText="Most Popular";
			MostPopular.innerText = " " ; 
			MostPopular.appendChild(oNode);
			MostPopular.style.backgroundColor = '#EFEBF7';
		}
	}
}


function mC(imgId){
	if(LastTdElement)
			{
				LastTdElement.bgColor = "EFEBF7";
				LastFontElement.style.color = "31309C";
				LastTdElement = null
			}	
	
	if ((imgId != lastImg) ||  (lastImg == "")){
		eval("document.all." + imgId  + ".style.backgroundColor = '#31309C'");
		eval("document.all.A_" + imgId  + ".style.color = '#EFEBF7'");
	if(lastImg){
		eval("document.all." + lastImg  + ".style.backgroundColor = '#EFEBF7'");
		eval("document.all.A_" + lastImg  + ".style.color = '#31309C'");
	}
	lastImg = imgId ;
	}
		
}