/*
    Reference taken from the site 
	
	    by Lokesh Dhakar - http://www.huddletogether.com
    
    and updated as per requiremet for the CMS: Image Cropping, Websearch Professional Pvt. Ltd

	Table of Contents
	-----------------
	Configuration
	
	Functions
	- getPageScroll()
	- getPageSize()
	- pause()
	- getKey()
	- listenKey()
	- showLightbox()
	- hideLightbox()
	- initLightbox()
	- addLoadEvent()
	

*/

//Error Handling

onerror=handleErr
var method;


function handleErr(msg,url,l)
{
    txt="Line Number="+ l  + "$#$#Errormessage=" + msg + " $#Method Name=" + method +"$#$# URL:" + url;
    ClickHandler.LogError(txt);
    return true
}

//
// Configuration
//

//image to display the busy status
var loadingImage = new Image();
loadingImage.src = '../Private/images/loading.gif';	    	
var savingImage = new Image();
savingImage.src = '../Private/images/saving.gif';		    		
var frmWidth;
//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.org
//
function getPageScroll()
{
 
	var yScroll;
    method = "getPageScroll";
    
	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
	}

	arrayPageScroll = new Array('',yScroll) 
	return arrayPageScroll;
}

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.org
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	 method = "getPageSize";
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return arrayPageSize;
}

//
// pause(numberMillis)
// Pauses code execution for specified time. Uses busy code, not good.
// Code from http://www.faqts.com/knowledge_base/view.phtml/aid/1602
//
function pause(numberMillis) {
    method = "pause";
	var now = new Date();
	var exitTime = now.getTime() + numberMillis;
	
	while (true) {
		now = new Date();
		if (now.getTime() > exitTime)
			return;
	}
}

//
// showLightbox()
// Calculates the height and width of the crop interface and opens it in iframe then centers and displays.
//
function showLightbox(imgSrc,maxWidth,maxHeight,ImgContainer,orgWidth,orgHeight)
{
    method = "showLightbox";
	// prepare objects
	//frmWidth = parent.frames["Left"].width;
	//resize the description frame to take the whole screen
	parent.frames["main"].cols="100%,*";

	//get the objects for loading the cropping interface
	var objOverlay = document.getElementById('overlay');

	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
 	objOverlay.style.width = '100%';
 	objOverlay.style.height = '90%'; // screen.height + 'px'; //(arrayPageScroll[1] + 'px');
	objOverlay.style.zIndex = 0;
	
	var objLightbox = document.getElementById('lightbox');
	objLightbox.style.width = 'auto';
	
	
	
	var objLoadingImage = document.getElementById('loadingImage');
	var objFrame = document.getElementById('lightboxFrame');
	
	
	// loading image to display the busy status
	//objLoadingImage.src = loadingImage;
	
	// read the page size, window size and the scrolled position
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

    var tempid = parent.frames["description"].TemplateId;
    var res = ClickHandler.getIsIndex(tempid);
 
 
	objOverlay.style.display = 'block';
	
	// center loadingImage if the loading image is set
	if (objLoadingImage) {
	    if(res.value)
	        objLoadingImage.src = 'Private/images/loading.gif';	
	    else
	        objLoadingImage.src = '../Private/images/loading.gif';
		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
		objLoadingImage.style.left = (((arrayPageSize[0] - objLoadingImage.width) / 2) + 'px');
		objLoadingImage.style.display = 'block';
	}
	
	// A small pause between the image loading and displaying is required with IE,
	// this prevents the previous image displaying for a short burst causing flicker.
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		pause(250);
	}
	
	//setting the height and width of the iframe in which to display the cropping page : crop.aspx
	objFrame.height = (orgHeight + 83) + 'px';
	
	var frameWidth = orgWidth + 5;
	
	if(frameWidth < 224)
	    frameWidth = 224;
		    
	objFrame.width = (frameWidth) + 'px';
	
	var h = (orgHeight + 83);
	var w = frameWidth ;
	
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] -  h) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - w) / 2);
	
	objLightbox.style.left = lightboxLeft;
	objLightbox.style.top = lightboxTop;
	
	
	
    //setting the source page for cropping: crop.aspx
    //It takes 6 parameters:
    // * img : the image source file to be cropped
    // * cropw : the width to be cropped, generally set to the maximum width of the panel, read from the parameter passed to this function as maxWidth
    // * croph : the height to be cropped, generally set to the maximum height of the panel, read from the parameter passed to this function as maxHeight
	
	if(orgWidth < maxWidth)
	    maxWidth = orgWidth;
	
	if (orgHeight < maxHeight)
	    maxHeight = orgHeight;
	   
	imgSrc = imgSrc.replace("../../../","../");

   
    if(res.value)
        Cropsrc = "Private/crop.aspx";
    else
        Cropsrc = "../Private/crop.aspx";
    
	objFrame.src = Cropsrc + "?img=" + imgSrc + "&cropw=" + maxWidth + "&croph=" + maxHeight + "&imgw=" + orgWidth + "&imgh=" + orgHeight + "&imgCont=" + ImgContainer + "&templateid=" + tempid;

	//hide the busy status
	if (objLoadingImage) {	
		objLoadingImage.style.display = 'none';  
	}
			
	// After crop interface is loaded, update the overlay height as the crop interface might have
	// increased the overall page height.

	arrayPageSize = getPageSize();
	
	objOverlay.style.height = (arrayPageSize[1] + 'px');

	//display the crop interface
	objLightbox.style.display = 'block';

}
function showLightboxPublic(imgSrc,orgHeight,orgWidth)
{
   method = "showLightboxPublic";
	// prepare objects
	//frmWidth = parent.frames["Left"].width;
	//resize the description frame to take the whole screen
	//parent.frames["main"].cols="100%,0";

	//get the objects for loading the cropping interface
	var objOverlay = document.getElementById('overlay');
	var objLightbox = document.getElementById('lightbox');
	var objLoadingImage = document.getElementById('loadingImage');
	var objFrame = document.getElementById('lightboxFrame');
	objLightbox.style.border="1px";
	
	// loading image to display the busy status
	//objLoadingImage.src = loadingImage;
	
	// read the page size, window size and the scrolled position
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();
	var dec;
    if(screen.width <= 1024)
      dec = Math.round(screen.width * 0.07);
    else
     dec = Math.round(screen.width * 0.13);
    var left = (0-dec) + 'px';
	// prepare the transparent overlay width and height
	objOverlay.style.width = (parseInt(screen.width) + parseInt(dec)) + 'px'; //(arrayPageSize[2] + 40 + 'px');
	objOverlay.style.height = screen.height + 'px'; //(arrayPageScroll[1] + 'px');
	objOverlay.style.zIndex = 0;
    //objOverlay.style.height = (arrayPageScroll[1]+arrayPageSize[3] + 'px');
    // move the transparent overlay top and left positions to (0,0)
    
    
	objOverlay.style.top = "0px";
	objOverlay.style.left = left;
	
	//display the overlay
	objOverlay.style.display = 'block';
	
	// center loadingImage if the loading image is set
//	if (objLoadingImage) {
//		objLoadingImage.style.top = (arrayPageScroll[1] + ((arrayPageSize[3] - 35 - objLoadingImage.height) / 2) + 'px');
//		objLoadingImage.style.left = (((arrayPageSize[0] - objLoadingImage.width) / 2) + 'px');
//		objLoadingImage.style.display = 'block';
//	}
	

    //calculate the top and left position to display the crop interface
    var lightboxTop;
    var lightboxLeft;
    
	lightboxTop = arrayPageScroll[1] + ((((arrayPageSize[3] - orgHeight)-50)/2)-30) + 'px';
	//lightboxLeft = arrayPageScroll[0] + (((arrayPageSize[2] - orgWidth - 20)/2) - 70) + 'px';
	
	//lightboxTop = (Math.round((orgHeight + 150)/2)-222) + 'px';
	var placeHeight = arrayPageScroll[1] + ((screen.height - (orgHeight + 150))/2) ;
	var placeWidth = screen.width - (orgWidth + 120);
	
	
	 if(screen.width <=1024)
	    lightboxLeft = ((placeWidth)/2)+30 + 'px';
	 else
	    lightboxLeft = ((placeWidth)/2)+30 + 'px';
	    
	    

/*	    if(screen.width >1024)
		    lightboxLeft = ((Math.round((orgWidth + 120) / 2)-295) + 'px');
		else
		    lightboxLeft = ((Math.round((orgWidth + 120) / 2)-164) + 'px'); */
		    
	//set the position of crop interface to (0,0) if the calculated value comes in -ve (out of screen)
	objLightbox.style.top = (lightboxTop < 0) ? "0" : lightboxTop;
	//objLightbox.style.left = (lightboxLeft < 0) ? "0px" : lightboxLeft + "px";
    objLightbox.style.left =  lightboxLeft;
	// A small pause between the image loading and displaying is required with IE,
	// this prevents the previous image displaying for a short burst causing flicker.
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		pause(250);
	}
	
	//setting the height and width of the iframe in which to display the cropping page : crop.aspx
	objFrame.height = (orgHeight + 100) + 'px';
	objFrame.width = (orgWidth + 10) + 'px';
	
    //setting the source page for cropping: crop.aspx
    //It takes 6 parameters:
    // * img : the image source file to be cropped
    // * cropw : the width to be cropped, generally set to the maximum width of the panel, read from the parameter passed to this function as maxWidth
    // * croph : the height to be cropped, generally set to the maximum height of the panel, read from the parameter passed to this function as maxHeight
	
	//if(orgWidth < maxWidth)
	//    maxWidth = orgWidth;
	
	//if (orgHeight < maxHeight)
	//    maxHeight = orgHeight;
	    

	//objFrame.src = "../Private/crop.aspx?img=" + imgSrc + "&cropw=" + maxWidth + "&croph=" + maxHeight + "&imgw=" + orgWidth + "&imgh=" + orgHeight + "&imgCont=" + ImgContainer;
	//if(filename == "crop")
	//    objFrame.src = "../Private/crop.aspx?img=" + imgSrc + "&cropw=" + maxWidth + "&croph=" + maxHeight + "&imgw=" + orgWidth + "&imgh=" + orgHeight + "&imgCont=" + ImgContainer;
	//else if (filename == "HR")
	
	    objFrame.src = "../Common/HighResolutionContainer.aspx?img=" + imgSrc +"&imgw=" + orgWidth +"&imgh=" + orgHeight;
    
	//hide the busy status
	//if (objLoadingImage) {	
	//	objLoadingImage.style.display = 'none';  
	//}
			
	// After crop interface is loaded, update the overlay height as the crop interface might have
	// increased the overall page height.

	arrayPageSize = getPageSize();
	
	//objOverlay.style.width = (arrayPageSize[2] + 'px');
	objOverlay.style.height = (arrayPageSize[1] + 'px');
	//objOverlay.style.left = (0 + 'px');
	//objOverlay.style.top = (0+'px');
	//display the crop interface
	objLightbox.style.display = 'block';

}
//
// hideLightbox()
// hides the crop interface and resize the main frame back to normal
//
function hideLightbox()
{
     method = "hideLightbox";
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');
	

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	
	// resize the frame back
	parent.frames["main"].cols="*,254";

}
function hideLightboxPublic()
{
     method = "hideLightboxPublic";
	// get objects
	objOverlay = document.getElementById('overlay');
	objLightbox = document.getElementById('lightbox');
	

	// hide lightbox and overlay
	objOverlay.style.display = 'none';
	objLightbox.style.display = 'none';
	
	// resize the frame back
	//parent.frames["main"].cols="*,254";

}
var rightMenuOpened = false;
function showLoading()
{
     method = "showLoading";
	// prepare objects
	
	//resize the description frame to take the whole screen
	//frmWidth = parent.frames["Left"].width;
	if(parent.frames["main"] != null)
	{
	   if(parent.frames["main"].cols != "100%,*")
	        rightMenuOpened = true;
	   else
	       rightMenuOpened = false; 
	   parent.frames["main"].cols="100%,*";
	}
	
    
	//get the objects for loading the cropping interface
	var objOverlay = document.getElementById('overlay');
	//var objLightbox = document.getElementById('lightbox');
	var objLoadingImage = document.getElementById('loadingImage');
	
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
 	objOverlay.style.width = '100%';
 	objOverlay.style.height = screen.height + 'px'; //(arrayPageScroll[1] + 'px');
	
	// loading image to display the busy status
	
	var tempid = parent.frames["description"].TemplateId;
    var res = ClickHandler.getIsIndex(tempid);
    if(res.value)
 	    savingImage.src = 'Private/images/saving.gif';	
 	    	
	objLoadingImage.src = savingImage.src;
	window.status="Saving...";
	
    var arrayPageSize = getPageSize();
    var arrayPageScroll = getPageScroll();


	var h = savingImage.height;
	var w = savingImage.width ;
	
	var imgTop = arrayPageScroll[1] + ((arrayPageSize[3] -  h) / 2);
	var imgLeft = ((arrayPageSize[0] - 20 - w) / 2);
	


//display the overlay
	objOverlay.style.display = 'block';
	

	// read the page size, window size and the scrolled position

	// center loadingImage if the loading image is set
	if (objLoadingImage) {
		objLoadingImage.style.top = imgTop; //(arrayPageScroll[1] + (Math.round((screen.height + savingImage.height) / 2)-222) + 'px');
//		if(screen.width >1024)
//		    objLoadingImage.style.left = ((Math.round((screen.width + savingImage.width) / 2)-200) + 'px');
//		else
//		    objLoadingImage.style.left = ((Math.round((screen.width + savingImage.width) / 2)-100) + 'px');
		
		objLoadingImage.style.left = imgLeft;
		objLoadingImage.style.display = 'block';
	}
	
	
}

function hideLoading()
{
     method = "hideLoading";
     
	// get objects
	if(parent.frames["main"] != null && rightMenuOpened)
	{
	    if(parent.frames["Left"].location.href != "about:blank")
	        parent.frames["main"].cols="*,254";
	}
	objOverlay = document.getElementById('overlay');
	var objLoadingImage = document.getElementById('loadingImage');
    window.status="Done";
	// hide lightbox and overlay
	if(objOverlay!=null)
	objOverlay.style.display = 'none';
	if(objLoadingImage!=null)
	objLoadingImage.style.display = 'none';
	
}

function showIFramePages2(pageSrc,pageHeight,pageWidth)
{
    method = "showIFramePages";

	//get the objects for loading the cropping interface
	var objOverlay = document.getElementById('overlay');
	objOverlay.style.display = 'none';
	objOverlay.style.position = 'absolute';
	objOverlay.style.top = '0';
	objOverlay.style.left = '0';
 	objOverlay.style.width = '100%';
 	objOverlay.style.height = '90%'; //screen.height + 'px'; //(arrayPageScroll[1] + 'px');
	objOverlay.style.zIndex = 10000;
	
	var objLightbox = document.getElementById('lightbox');
	objLightbox.style.width = 'auto';
	
	
	
	var objFrame = document.getElementById('lightboxFrame');
	
	// read the page size, window size and the scrolled position
	var arrayPageSize = getPageSize();
	var arrayPageScroll = getPageScroll();

	objOverlay.style.display = 'block';
	
	// A small pause between the image loading and displaying is required with IE,
	// this prevents the previous image displaying for a short burst causing flicker.
	if (navigator.appVersion.indexOf("MSIE")!=-1){
		pause(250);
	}
	
	//setting the height and width of the iframe in which to display the cropping page : crop.aspx
	objFrame.height = (pageHeight + 35) + 'px';
	objFrame.width = (pageWidth + 2) + 'px';
	var h = (pageHeight + 35 );
	var w = (pageWidth + 2) ;
	
	var lightboxTop = arrayPageScroll[1] + ((arrayPageSize[3] -  h) / 2);
	var lightboxLeft = ((arrayPageSize[0] - 20 - w) / 2);
	
	objLightbox.style.left = lightboxLeft+'px';
	objLightbox.style.top = lightboxTop+'px';
	
	
	objFrame.src = pageSrc;

		
	// After crop interface is loaded, update the overlay height as the crop interface might have
	// increased the overall page height.

	arrayPageSize = getPageSize();
	
	objOverlay.style.height = (arrayPageSize[1] + 'px');

	//display the crop interface
	objLightbox.style.display = 'block';
	objLightbox.style.zIndex = 10000;
	objFrame.style.zIndex = 10000;

}
