// JavaScript Document
var main_link; 			// inside gallery
var main_img_link;		// inside gallery
var root_img_link;		// root

var xmlDoc;		// xml document
var gTag; 		// gallery Tag
var gLength; 	// gallery Tag Length
var fTag; 		// folder Tag
var fLength; 	// folder Tag Length
var iTag; 		// image Tag
var iLength; 	// image Tag Length
var old_image_id;
//------------------------------------Assign XMLHttpRequestObject--------------------------//
function GetXmlHttpObject()
{
	var xmlHttp=null;
	try
	{
	  	// Firefox, Opera 8.0+, Safari
	  	xmlHttp=new XMLHttpRequest();
	}
	catch (e)
	{
		// Internet Explorer
	  	try
		{
			xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
		}
	  	catch (e)
		{
			xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
		}
	}
	return xmlHttp;
}
//------------------------------------Assign XMLHttpRequestObject--------------------------//

function tr_show(id)	// Show specific TR
{
	try
	{
		document.getElementById(id).style.display='table-row';			
	}
	catch(eIE)
	{
		document.getElementById(id).style.display='block';						
	}
}

function show(id)	// Show specific DIV
{
	document.getElementById(id).style.display='block';
}

function hide(id)	// Hide specific DIV,TR
{
	document.getElementById(id).style.display='none';
}

function show_visible(id)	// Show specific DIV
{
	document.getElementById(id).style.visibility='visible';
}

function hide_visible(id)	// Hide specific DIV,TR
{
	document.getElementById(id).style.visibility='hidden';
}

//------------------------------------Get XML Data--------------------------//
function getXmlData()
{
	//alert('main_link = '+main_link);
	getXmlData_object=GetXmlHttpObject();
	getXmlData_object.open("GET",main_link+"/Gallery.xml",false);
	getXmlData_object.send(null);	
	xmlDoc=getXmlData_object.responseXML;
	
	gTag = xmlDoc.getElementsByTagName('Gallery');	
	//alert('gTag = '+gTag);
	gLength=gTag.length;
	//alert('gLength = '+gLength);
	
	fTag=gTag[0].getElementsByTagName("Gallery_Folder");
	//alert('fTag = '+fTag);
	fLength=fTag.length;
}

function getImageInfo(gallery_id,image_id,check_type)
{
	//alert('gallery_id = '+gallery_id);
	//alert('image_id = '+image_id);
	
	for(f=0;f<fLength;++f)
	{
		if( fTag[f].getAttribute("Directory_Id") == gallery_id )
		{	
			//alert('yes1');
			iTag=fTag[f].getElementsByTagName("Image");
			iLength=iTag.length;
			
			for(i=0;i<iLength;++i)
			{
				if( iTag[i].getAttribute("Image_Id") == image_id )
				{
					//alert('yes2');
					
					// get image info
					width=iTag[i].getAttribute('Width');
					height=iTag[i].getAttribute('Height');
					full_path= root_img_link+"/" + gTag[0].getAttribute('Default_Directory') + "/" + fTag[f].getAttribute('Folder') + "/" + iTag[i].getAttribute('Source');
					
					target_width=530;
                    target_height=354;
					img_info=getThumb2(width,height,target_width,target_height);
					//alert('img_info = '+img_info);
					//alert('full_path = '+full_path);
					
					document.getElementById("img_src").src='';
					document.getElementById("img_src").src=full_path;
					//alert('width = '+document.getElementById("img_src").width);
					document.getElementById("img_src").width=img_info[0];
					document.getElementById("img_src").height=img_info[1];
					
					//alert('document.getElementById("img_src").src = '+document.getElementById("img_src").src);
										
					document.getElementById("img_title").innerHTML=iTag[i].getElementsByTagName('Title')[0].firstChild.nodeValue;
					
					cdata_val='';
					len=0;
					
					// code for IE
					/*if (window.ActiveXObject)
					{
						alert('ie, description = '+iTag[i].getElementsByTagName('Description')[0].firstChild.nodeValue);
						//cdata_val=iTag[i].getElementsByTagName('Description')[0].childNodes[0].nodeValue;
					}
					// code for Mozilla, Firefox, Opera, etc.
					else if (document.implementation && document.implementation.createDocument)
					{
						alert('mozila, description = '+iTag[i].getElementsByTagName('Description')[0].firstChild.nodeValue);
						//cdata_val=iTag[i].getElementsByTagName('Description')[0].childNodes[1].nodeValue;
					}*/
					cdata_val=iTag[i].getElementsByTagName('Description')[0].firstChild.nodeValue;
					// Image Description Whole
					//document.getElementById("td_img_desc").innerHTML=cdata_val+'<img src="../SiteImages/spacer.gif" height="1" width="10" /><span class="Read_More"><a href="javascript:hide(\'tr_img_desc\')">Close</a></span>';
					document.getElementById("div_img_desc").innerHTML=cdata_val+'<img src="'+root_img_link+'/SiteImages/spacer.gif" height="1" width="10" /><span class="Read_More"><a href="#more" name="close" onclick="hide(\'div_img_desc\');show(\'img_desc\');">Close</a></span>';
					
					// Striptags
					show_some=strip_tags(cdata_val);
					
					// Stripslashes
					show_some=stripslashes(show_some);
					
					// Strlen
					len=strlen(show_some);
					
					//alert('len = '+len);
					
					if(len>163)
					{
						show_some=(substr(show_some, 0, 162))+"...";
						//read_more='<img src="../SiteImages/spacer.gif" height="1" width="10" /><span class="Read_More"><a href="javascript:tr_show(\'tr_img_desc\')">READ MORE >></a></span>';
						read_more='<img src="'+root_img_link+'/SiteImages/spacer.gif" height="1" width="10" /><span class="Read_More"><a href="#close" name="more" onclick="hide(\'img_desc\');show(\'div_img_desc\');">READ MORE >></a></span>';
					}
					else
					{
						read_more='';
					}
						
					//alert('show_some = '+show_some);
						
					document.getElementById("img_desc").innerHTML=show_some+read_more;
					
					document.getElementById("camera_info").innerHTML='';
					
					if(iTag[i].getElementsByTagName('Date')[0].firstChild!=null)
						document.getElementById("camera_info").innerHTML += 'Taken on: '+iTag[i].getElementsByTagName('Date')[0].firstChild.nodeValue;
					else if(iTag[i].getAttribute('Date_Created')!=null)
						document.getElementById("camera_info").innerHTML += 'Added: '+iTag[i].getAttribute('Date_Created');
						
					if(iTag[i].getElementsByTagName('Exposure')[0].firstChild!=null)
						document.getElementById("camera_info").innerHTML += ' '+iTag[i].getElementsByTagName('Exposure')[0].firstChild.nodeValue;
						
					if(iTag[i].getElementsByTagName('Flash')[0].firstChild!=null)
						document.getElementById("camera_info").innerHTML += ', '+iTag[i].getElementsByTagName('Flash')[0].firstChild.nodeValue;
						
					if(iTag[i].getElementsByTagName('Camera')[0].firstChild!=null)
						document.getElementById("camera_info").innerHTML += ' '+iTag[i].getElementsByTagName('Camera')[0].firstChild.nodeValue;
					//document.getElementById("camera_info").innerHTML='Taken on: '+iTag[i].getElementsByTagName('Date')[0].firstChild.nodeValue+' '+iTag[i].getElementsByTagName('Exposure')[0].firstChild.nodeValue+', '+iTag[i].getElementsByTagName('Flash')[0].firstChild.nodeValue+' '+iTag[i].getElementsByTagName('Camera')[0].firstChild.nodeValue;
					//alert(old_image_id);
					
					// deselect class for old one 
					document.getElementById("thumb_top_left"+old_image_id).src=main_img_link+'/Style2/thumb_top_left.gif';
					document.getElementById("thumb_top_right"+old_image_id).src=main_img_link+'/Style2/thumb_top_right.gif';
					document.getElementById("thumb_bottom_left"+old_image_id).src=main_img_link+'/Style2/thumb_bottom_left.gif';
					document.getElementById("thumb_bottom_right"+old_image_id).src=main_img_link+'/Style2/thumb_bottom_right.gif';
					document.getElementById("Thumb_Top"+old_image_id).className='Thumb_Top';
					document.getElementById("Thumb_Left"+old_image_id).className='Thumb_Left';
					document.getElementById("Thumb_Right"+old_image_id).className='Thumb_Right';
					document.getElementById("Thumb_Bottom"+old_image_id).className='Thumb_Bottom';
					
					// select class for new one
					document.getElementById("thumb_top_left"+image_id).src=main_img_link+'/Style2/thumb_top_left_selected.gif';
					document.getElementById("thumb_top_right"+image_id).src=main_img_link+'/Style2/thumb_top_right_selected.gif';
					document.getElementById("thumb_bottom_left"+image_id).src=main_img_link+'/Style2/thumb_bottom_left_selected.gif';
					document.getElementById("thumb_bottom_right"+image_id).src=main_img_link+'/Style2/thumb_bottom_right_selected.gif';
					document.getElementById("Thumb_Top"+image_id).className='Thumb_Top_Selected';
					document.getElementById("Thumb_Left"+image_id).className='Thumb_Left_Selected';
					document.getElementById("Thumb_Right"+image_id).className='Thumb_Right_Selected';
					document.getElementById("Thumb_Bottom"+image_id).className='Thumb_Bottom_Selected';
					
					if( check_type=='btn_next' || check_type=='btn_prev' )
					{
						txt='';
						
						if(i!=0)	// Show Previous Button
							txt=txt+'<a href="javascript:getImageInfo(\''+gallery_id+'\',\''+iTag[i-1].getAttribute('Image_Id')+'\',\'btn_prev\')"><img src="'+main_img_link+'/Style2/Previous.gif" alt="Previous" hspace="10" vspace="10" /></a>';
						else
							txt=txt+'<img src="'+root_img_link+'/SiteImages/spacer.gif" hspace="10" vspace="10" height="14" width="22" />';
						
						// Show Play Button
						txt=txt+'<img class="Play_Pause" id="pauseBtn" onclick="doPlay();" src="'+main_img_link+'/Style2/Play.gif" alt="Play" hspace="10" vspace="10" />';
						
						if((i+1)!=iLength)	// Show Next Button
							txt=txt+'<a href="javascript:getImageInfo(\''+gallery_id+'\',\''+iTag[i+1].getAttribute('Image_Id')+'\',\'btn_next\')"><img src="'+main_img_link+'/Style2/Next.gif" alt="Next" hspace="10" vspace="10" /></a>';
						else
							txt=txt+'<img src="'+root_img_link+'/SiteImages/spacer.gif" hspace="10" vspace="10" height="14" width="22" />';
						
						document.getElementById("next_prev").innerHTML=txt;
					}
					// insert new image id
					old_image_id=image_id;
					return;
				}
				//alert(i);
			}
		}
		//alert(f);
	}
}

function getThumb1(width, height, target)  // WHERE Height & Width Same
{
	//takes the larger size of the width and height and applies the formula accordingly...this is so this script will work  dynamically with any size image
	if (width > height) 
		percentage = (target / width);
	else 
		percentage = (target / height);

	//gets the new value and applies the percentage, then rounds the value
	width = round(width * percentage);
	height = round(height * percentage);

	//returns the new sizes in html image tag format...this is so you can plug this function inside an image tag and just get the
	//return "width=\"width\" height=\"height\"";
	
	img_info= new Array ();
	img_info[0]= width;
	img_info[1]= height;
	//alert('img_info = '+img_info);
	return (img_info);
}

function getThumb2(width, height,target_width,target_height)	// WHERE Height & Width Different
{
	if( (height <=target_height)&&(width <=target_width) )
	{
		new_width = width;
		new_height = height;
	}
	else if( (height <target_height)&&(width >target_width) )
	{
		new_width = target_width;
		new_height = parseInt((target_width* (height/width)));
	}
	else if( (height >target_height)&&(width <target_width) )
	{
		new_height =target_height;
		new_width = parseInt((target_height*(width/height)));
	}
	else if ( (height >target_height)&&(width >target_width) )
	{
		RH=height/target_height;
		RW=width/target_width;

		if(RH>RW)
		{
			new_height =target_height;
			new_width = parseInt((target_height*(width/height)));
		}
		else if(RH<RW)
		{
			new_width = target_width;
			new_height = parseInt((target_width* (height/width)));
		}
		else
		{
			new_width = target_width;
			new_height = target_height;
		}
	}
	
	//return "width=\"new_width\" height=\"new_height\"";
	img_info= new Array ();
	img_info[0]= new_width;
	img_info[1]= new_height;
	//alert('img_info = '+img_info);
	return (img_info);
}