function setDispStyle(id, style)
{
    document.getElementById(id).style.display = style;
}
function g(c) {return window.document.getElementById(c);}

function favorite(c, id)
{
    var cid = "lnkfav_" + id;
    setDispStyle(cid, "none");
    loadXMLDoc('/support/handler/TenantSearch.ashx?FAVP=' + id + "&FAVC=" + cid, loadFavorite, null);
}

function loadFavorite()
{
if (xmlhttp.readyState==4)
  {// 4 = "loaded"
  if (xmlhttp.status==200)
    {// 200 = "OK"
        if (xmlhttp.responseText == "")
        {
            alert("Unable to process favorite");
        }
        else
        {
            var r = xmlhttp.responseText.substr(0,1);
            var cid = xmlhttp.responseText.substr(2);
            var c=g(cid);
            setDispStyle(cid, "block");

            if (r == "A")
            {
                c.innerHTML = "Add to My Favorites"
            }
            else
            {
                c.innerHTML = "Remove from My Favorites"
            }
        }
    }
  else
    {
    alert("Problem retrieving data:" + xmlhttp.statusText);
    }
  }
}

function loadXMLDoc(url, callback, packet)
{
    xmlhttp=null;
    if (window.XMLHttpRequest)
    {// code for Firefox, Opera, IE7, etc.
        xmlhttp=new XMLHttpRequest();
    }
    else if (window.ActiveXObject)
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (xmlhttp!=null)
    {
        xmlhttp.onreadystatechange=callback;
        if (packet == null)
            xmlhttp.open("GET",url,true);
        else
            xmlhttp.open("POST",url,true);
            
        xmlhttp.send(packet);
    }
    else
    {
        alert("Your browser does not support XMLHTTP.");
    }
}


//--------------------------------------------------------------------------------------------------
// Copyright © 2005.  All rights reserved.
//
// Author: Epic River LLC.
//---------------------------------------------------------------------------------------------------
// :::::::::::::::::::::::::
// ::: Initialize Page ::::
// :::::::::::::::::::::::::

// |||||||||||||||||||||||||||||||||||||||||||
// | Set Position, Dimensions And Color HERE  |
// |||||||||||||||||||||||||||||||||||||||||||

// Image Viewer Dimensions settings (pixels):
	var PagePositionLEFT		= 300; 			// Set position of Image viewer from LEFT of page
	var PagePositionTOP			= 150; 			// Set position of Image viewer from TOP of page
	
	var InterFaceWidth 			= 300; //630			// Set [Overall] WIDTH |||| minimum length=300
	var ViewingAreaHeight		= 70; 			// Set [Viewing area] HEIGHT |||| minimum length=10

	var OverALLBorder			= 'on'; 			// Set [Over All] Border 'on' or '' (blank) for off

// Image Viewer Colors [example: 'red' or '#FF0000']
	var OverALLBorderColor		= 'gray'; 		// Set color of [Over All] Border

	var ControlsBGColor			= 'white'; 		// Set color of [Control Area] Background |||| use '' = no color 333333'
	var ControlsFontColor		= 'black'; 		// Set color of [Control Area] Font
	
	var ViewAreaBGColor		= 'white';		// Set color of [Viewing Area] Background |||| use '' = no color
	
	var ImageBorderColor		= 'white'; 		// Set color of [Image] Border
	
// Image Alignment
	var ImageValignment		= 'bottom'; 		// Set verticle alignment within viewer (options: top, bottom. middle)
	var DestImg = '';

function InitializePage(ImageArray, DestinationIMGTagName)
{
// Install Image Viewer HTML file name holderInput element
if (ImageArray!=null)
{
    DestImg = DestinationIMGTagName;
    for (l=0; l<ImageArray.length; l++)
	{
	    TDElement=document.createElement("TD");
	    TDElement.innerHTML = "<IMG border='0' src='" + ImageArray[l] + "' height='" + (ViewingAreaHeight - 28) + "px' title='" + getQueryVariable(ImageArray[l], "imageCaption") + "'>"
	    document.getElementById('ImageContainer').appendChild(TDElement)		
	}
}
else
{
    alert('No thumbnails to display');
    document.getElementById('interface').style.visibility='hidden';
}

setStyles();
ContainerElement=document.getElementById('ImageContainer')

setIDs();
attachEventhandlers();

//Select First Image

//IMGCol=document.getElementById('ViewingArea').getElementsByTagName('IMG');
//if (IMGCol!=null)
//	{for (im=0; im<1; im++)
//		{
//		IMGCol.item(im).style.borderColor='black';
//		}
//	}
}
/*Modified by Shailesh Kumar Pandey for Bug #: RML-3953 on 07-Oct-2008.*/
function setStyles()
{

// set z-index
if(null != document.getElementById('ViewingArea'))
{
document.getElementById('ViewingArea').style.zIndex=1;
// set Image Scroller DIVs height Dimensions
document.getElementById('ViewingArea').style.height=ViewingAreaHeight+"px";
document.getElementById('ViewingArea').style.width=InterFaceWidth+"px";

// Set Viewer Page position

// image Viewer Color Scheme
document.getElementById('ViewingArea').style.backgroundColor=ViewAreaBGColor;

if(OverALLBorder=='on'){
document.getElementById('ViewingArea').style.borderStyle='solid';
document.getElementById('ViewingArea').style.borderWidth="1px";
document.getElementById('ViewingArea').style.borderColor=OverALLBorderColor;}

// Image Styles
IMGCol=document.getElementById('ViewingArea').getElementsByTagName('IMG');
if (IMGCol!=null)
	{for (im=0; im<IMGCol.length; im++)
		{
		IMGCol.item(im).style.borderStyle='solid';
		IMGCol.item(im).style.borderWidth="1px";
		IMGCol.item(im).style.borderColor=ImageBorderColor;
		}
	}

// Table Cell Styles
TDCol=document.getElementById('ViewingArea').getElementsByTagName('TD');
if (TDCol!=null)
	{for (td=0; td<TDCol.length; td++)
		{TDCol.item(td).style.verticalAlign=ImageValignment;}}
 }
}


// ::::::::::::::::::::::::
// ::: Event Handlers ::
// ::::::::::::::::::::::::

function onclickHandler(e)
{
// Browser compatibility code
	var targ;
	if (!e){var e = window.event;}
	
	if (e.target)
		{	targ = e.target;
			var xpos=(e.pageX); var ypos=(e.pageY);}
	
	else if (e.srcElement)
		{	var xpos=(event.x);	var ypos=(event.y);
			targ = e.srcElement;}
	showImage(targ);
}
function showImage(targ)
{
// Strip file name from image src
	var spath=targ.getAttribute('src');
	spath=spath.replace('thumbnail','preview');
	setStyles();
    targ.style.borderColor='black';

	
	//Modified by Shailesh Kumar Pandey for bug#:RML-3195 on 19-Aug-2008
	if(spath.search(/&watermark=true/i) < 0)
	{
	    document.getElementById(DestImg).src = spath + '&watermark=true';
	}
	else
	{
	    document.getElementById(DestImg).src = spath;
	}
	//Added by Shailesh Kumar Pandey for bug#:RML-3377 on 01-Sep-2008
	if(spath.search(/&imageCaption=/i) >= 0)
	{
	    document.getElementById(DestImg).title = document.getElementById(getQueryVariable(spath, "imageCaption")).innerHTML;
	    document.getElementById(lblCaption).style.display = '';
	    document.getElementById(lblCaption).innerHTML = document.getElementById(getQueryVariable(spath, "imageCaption")).innerHTML;
	}
	else
	{
	    document.getElementById(DestImg).title = "none";
	    document.getElementById(lblCaption).style.display = '';
	}
	scale_load(DestImg);
}
//Added by Shailesh Kumar Pandey for bug#:RML-3377 on 01-Sep-2008
function getQueryVariable(UrlPath,variable) {
  //var query = window.location.search.substring(1);//alert(window.location.search.substring(1));
  var query = UrlPath.substring(1);
  var vars = query.split("&");//alert(vars[0]);
  for (var i=0;i<vars.length;i++) {
    var pair = vars[i].split("=");//alert(pair);
    if (pair[0].toLowerCase() == variable.toLowerCase()) {
      return pair[1];
      
    }
  } 
  return "";
}

// Attach event handlers to all images within container
/*Modified by Shailesh Kumar Pandey for Bug #: RML-3953 on 07-Oct-2008.*/
function attachEventhandlers()
{
ContainerElement=document.getElementById('ImageContainer')
if(null != ContainerElement)
{
TDCol=ContainerElement.getElementsByTagName('TD');

if (TDCol!=null)
	{for (l=0; l<TDCol.length; l++)
		{
		IMGCol=TDCol.item(l).getElementsByTagName("IMG");
		IMGCol.item(0).style.cursor="pointer"
		IMGCol.item(0).setAttribute('id',"Image"+l)
		IMGCol.item(0).onclick=onclickHandler;
		}
	}
 }
}

// Set ID's for all table cells
/*Modified by Shailesh Kumar Pandey for Bug #: RML-3953 on 07-Oct-2008.*/
function setIDs()
{
ContainerElement=document.getElementById('ImageContainer')
if(null != ContainerElement)
{
TDCol=ContainerElement.getElementsByTagName('TD');
if (TDCol!=null)
	{	for (i=0; i<TDCol.length; i++)
	    {
			TDCol.item(i).setAttribute('id',i);
	    }
	}
 }
}

// :::::::::::::::::::::::::
// ::: Scroll Functions ::
// :::::::::::::::::::::::::

function moveLeft()
{
IMGCol=document.getElementById('ViewingArea').getElementsByTagName('IMG');
var thumb = 0;
if (IMGCol!=null)
	{
	for (im=0; im<IMGCol.length; im++)
		{
		    if (IMGCol.item(im).style.borderColor.indexOf('black') > -1)
		    {
		        thumb = im;
		    }
		}
	}
	thumb--;
	if (thumb < 0)
	    thumb = IMGCol.length -1;
	IMGCol.item(thumb).focus();
	document.getElementById('ViewingArea').scrollLeft = IMGCol.item(thumb).offsetParent.offsetLeft;
	showImage(IMGCol.item(thumb));
}

function moveRight()
{
IMGCol=document.getElementById('ViewingArea').getElementsByTagName('IMG');
var thumb = -1;
if (IMGCol!=null)
	{
	for (im=0; im<IMGCol.length; im++)
		{
		    if (IMGCol.item(im).style.borderColor.indexOf('black') > -1)
		    {
		        thumb = im;
		    }
		}
	}
	thumb++;
	if (thumb >= IMGCol.length)
	    thumb = 0;
	IMGCol.item(thumb).focus();
	document.getElementById('ViewingArea').scrollLeft = IMGCol.item(thumb).offsetParent.offsetLeft;
	showImage(IMGCol.item(thumb));
}

