/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;
var xPos,yPos;
/*-----------------------------------------------------------------------------------------------*/

function getBrowserInfo() {
	if (checkIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (checkIt('safari')) browser 	= "Safari"
	else if (checkIt('omniweb')) browser 	= "OmniWeb"
	else if (checkIt('opera')) browser 		= "Opera"
	else if (checkIt('webtv')) browser 		= "WebTV";
	else if (checkIt('icab')) browser 		= "iCab"
	else if (checkIt('msie')) browser 		= "Internet Explorer"
	else if (!checkIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (checkIt('linux')) OS 		= "Linux";
		else if (checkIt('x11')) OS 	= "Unix";
		else if (checkIt('mac')) OS 	= "Mac"
		else if (checkIt('win')) OS 	= "Windows"
		else OS 						= "an unknown operating system";
	}
}

function checkIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}


function getScroll(){
debugger;
		if (self.pageYOffset) {
			yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			yPos = document.body.scrollTop;
		}
}

function prepareIE(height, overflow){
		var bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		var htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
}

function setScroll(x, y){
		window.scrollTo(x, y); 
}

function hideSelects(visibility){
		var selects = new Array();
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
}

function activate(){
	
	if(browser == 'Internet Explorer')
	{
		getScroll();
		prepareIE('100%', 'hidden');
		setScroll(0,0);
		hideSelects('hidden');
	}
}

function deactivate()
{
	if (browser == "Internet Explorer")
		{
			hideSelects("visible");
			prepareIE("auto", "auto");
			setScroll(0,yPos);
		}
}

var timerStatus;

function close()
{
	if($("#lbcontainer iframe").length != 0)
	{
		$("#lbcontainer").empty();
	}
	$("#overlay").fadeOut("fast");
	$("#bottomclose").hide();
	$("#topclose").hide();
	$("#lightbox").hide();
	deactivate();
	
	// ClearTimeout for any recursive thread
	if(timerStatus)
		clearTimeout(timerStatus);
}

function checkForClose()
{  
	if(window.location.hash == "#close_child")     
	{
		close();
		window.location.hash="";
		window.location.reload();
	}     
	else     
	{       
		timerStatus = setTimeout(checkForClose, 1000);     
	} 
}

function callToShowLightBox(ref){
	// Comment Date [By Roshan] : 16 March 2011
		// Due to IE's onload issue for the dynamic Iframe, we need to put the "onload" event 
		// in its tag itself and so Approach: Two is being used instead on Approach : 0ne
		
		// Approach: One
		/*$('<iframe />',{
						name:'myFrame',
						id	:'myFrame',     
						src	: this.href,
						frameborder: 0,
						width : '100%',
						height: '100%',
						onload:	'fixFunction()'
					}
		).appendTo("#lbcontainer");
		*/
		
		// Approach : Two
		$('<iframe name="myFrame" id="myFrame" src="' + ref.href + '" frameborder=0 width="100%" height="100%" onload="postProcess();" />').appendTo("#lbcontainer");
		
		// Execute On Load Complete of Iframe
		document.getElementById('myFrame').postProcess = function(){
		
			// For https request, making configurable by using pooling through hash
			if(this.src.substring(0,5)=="https")
			{
				// Initially Hide Both Close Strip
				$("#topclose").hide();
				$("#bottomclose").hide();
				
				// Get the Hash Data from URL: (It need to be set by child page running on https for parent location)
				var currentHash = window.location.hash;
				var valueSet = currentHash.split("@");
				
				if(valueSet != null && valueSet != "")
				{
					//valueSet[1] contains configurable text
					if(valueSet[0]!="null")
					{
						$("#topclose a").html(valueSet[0].substr(1));
						$("#bottomclose a").html(valueSet[0].substr(1));
					}
					//valueSet[0] contains configurable location
					if(valueSet[1]!="null")
					{
						// For Top
						if(valueSet[1].charAt(0) == "T")
						{
							switch(valueSet[1].charAt(1))
							{
								case "L":
									$('#topclose').css('text-align','left');
									break;
								case "C":
									$('#topclose').css('text-align','center');
									break;
								case "R":
									parent.$('#topclose').css('text-align','right');
								break;
							}
							$("#topclose").show();
						}
						else
						{
							switch(valueSet[1].charAt(1))
							{
								case "L":
									$('#bottomclose').css('text-align','left');
									break;
								case "C":
									$('#bottomclose').css('text-align','center');
									break;
								case "R":
									$('#bottomclose').css('text-align','right');
									break;
							}
							$("#bottomclose").show();
						}
					}
					window.location.hash="";
					setTimeout(checkForClose, 5000);
				}
			}
			//Process only when there is no customization for "close" (default: hidden) has been executed by iframe content 
			if($("#topclose").is(":hidden") && $("#bottomclose").is(":hidden"))
			{	
				$("#topclose a").html("CLOSE");
				$('#topclose').css('text-align','center');
				$("#topclose").show();
			}
		} 
		
		$("#overlay").css("opacity", "0.8");
		$("#overlay").fadeIn("fast");
		
		//Start: This need to set dynamically
		//$("#bottomclose").show();
		//End: This need to set dynamically
		
		activate();
		$("#lightbox").show();
}
	
jQuery(document).ready(function($) {
	getBrowserInfo();
	// Initialize popup element if not exist
	if($("#lightbox").length == 0 )
	{
		var strHTML = "<div id=\"overlay\"></div><div id=\"lightbox\"><div id=\"lbContent\"><div id=\"lbcontainer\"></div></div></div>";
		$("body").append(strHTML);

		$("#lbContent").prepend("<div id='topclose' style='text-align:center;display:none'><a href='#' class='closePopup'>Close Window</a></div>");
		$("#lbContent").append("<div id='bottomclose' style='text-align:center;display:none'><a href='#' class='closePopup'>Close Window</a></div>");
	}
	
	$(".lbOn").click(function(){
		callToShowLightBox(this);		
		return false;	
	});	
	
	/* Closing Popup */

	$(".closePopup").click(function(){
		close();
		return false;
	});
	
})
