var OVERLAY_TOP = 3;
var OVERLAY_BOTTOM = 10;

// Variable to hold array of itemIds that should be reported when loging event
var compids = '';

// Global variable keeping track of current context (search, ad, offer, compare)
//var context = 'search';
var cardtype = "search";
var cardtypeconstant=-1;
var cardtypeid=cardtypeconstant;


var popups = new Array();

if (typeof YAHOO != 'undefined') {

	var $D = YAHOO.util.Dom?YAHOO.util.Dom:null;
	var $E = YAHOO.util.Event?YAHOO.util.Event:null;
	var $A = YAHOO.util.Anim?YAHOO.util.Anim:null;
}
var $ = $D?$D.get:document.getElementById;

if ($E)
	$E.on(window, 'resize', sizeOverlay);

function renderOverlay()
{
	var html = '<div id="overlay" onclick="closeOverlay()"></div>\
	<div id="overlaywin">\
		<div id="overlayhead">\
			<a id="overlayclose" href="javascript:closeOverlay()">Close</a>\
			<a id="overlayprint" href="Print">Print</a>\
			<h1>&nbsp;</h1>\
		</div>\
		<div id="overlaybody"></div>\
	</div>';
	
	document.write(html);

}
function openOverlay(title, url, html, printFunction, width, top, dontDim, contentClass ) 
{
	sizeOverlay();
	setOverlayTitle(title);
	
	//Render content
	var overlayBody = $('overlaybody');
	overlayBody.style.height = 'auto';


	if (html != "" && html != null && html != true) {
       overlayBody.innerHTML = (contentClass?'<div class="'+contentClass+'">':'')+html+(contentClass?'</div>':'');
	} else if (url && html == true) {
		var callbackOverlay = {
			success: showOverlay,
			argument: [title, url, printFunction, width, top, dontDim, contentClass]
		};

   		YAHOO.util.Connect.asyncRequest('GET', url, callbackOverlay);
   		
   		overlayBody.innerHTML = '<div style="height:1px">&nbsp;</div><div class="loading"></div>';
   		
	} else if (url) {
		overlayBody.innerHTML = '<iframe id="overlayframe" name="overlayframe" src="'+url+'" frameborder="0" style="height:1px"></iframe><div class="loading"></div>';
	} else {
		return;
	}
	
	// Should we show print or not
	var printElem = $('overlayprint');
	if (printElem) {
		if (printFunction){
            printElem.style.display='block';
			printElem.href = 'javascript:'+printFunction+'()';
        } else {
            printElem.style.display='none';
		}
	}
	
	// Set width
	if (width)
		$('overlaywin').style.width = (width)+'px';
	else
		$('overlaywin').style.width = '';
	
	if (!dontDim)
		$('overlay').style.display = 'block';
		
	$('overlaywin').style.display = 'block';
	
	positionOverlayWin(top);
	
	var fadeAnim = new $A('overlaywin', { opacity: { to: 1 } }, 1, YAHOO.util.Easing.easeOut);
	fadeAnim.animate(); 
}

function showOverlay( res )
{
	openOverlay(res.argument[0], res.argument[1], res.responseText, res.argument[2], res.argument[3], res.argument[4], res.argument[5], res.argument[6]);
}


function positionOverlayWin(top)
{
	var win = $('overlaywin');
	win.style.top = document.body.scrollTop+document.documentElement.scrollTop+(top?top:OVERLAY_TOP)+'px';
	win.style.left = ((($('bodyBgOverlay').offsetWidth-win.offsetWidth)/2)+2)+'px';
}

function sizeOverlay()
{
	var width = (document.body.scrollLeft+document.documentElement.scrollLeft+$D.getViewportWidth());
	var height = (document.body.scrollTop+document.documentElement.scrollTop+$D.getViewportHeight());
if($('bodyBgOverlay'))
{
	var docWidth = $('bodyBgOverlay').offsetWidth+15;
	var docHeight = $('bodyBgOverlay').offsetHeight+15;

if($('overlay'))
{
	//alert((width < docWidth?docWidth:width)+'px');
	$('overlay').style.width = (width < docWidth?docWidth:width)+'px';
	$('overlay').style.height = (height < docHeight?docHeight:height)+'px';
}
}
}

function setOverlayTitle( title ) 
{
	var headers = $D.getElementsBy(function(){return true;}, 'h1', $('overlayhead'));

	if (headers)
 		headers[0].innerHTML = title;
}

function printOverlayFrame()
{
	logEvent(null, 'print');
	
	// Calls print function of the document in the iframe window (has to be in same domain to avoid security problems)
	var frame = $('overlayframe');
	if (frame) {
		if (frame.contentWindow.printThisWindow) {
			frame.contentWindow.printThisWindow();
		} else {
			frame.contentWindow.focus();
			frame.contentWindow.print();
		}
	}
}

function printThisWindow()
{
	window.focus();
	window.print();
}

function closeOverlay(pid)
{	
	// Hide the overlay
	var fadeAnim = new $A('overlaywin', { opacity: { to: 0 } }, 0.1, YAHOO.util.Easing.easeOut);
	fadeAnim.onComplete.subscribe(hideOverlay);
	fadeAnim.animate();
	if(pid) {
	//	document.location.href=UrlArr['module_register_url']+"search/propertydetails/"+pid;
		document.location.href=UrlArr['module_register_url']+"upload/uploadimages/"+pid;
	}
	setTimeout("refreshcalendar()",2000);
}

function hideOverlay()
{
	$('overlaybody').innerHTML = "";
	$('overlaybody').style.height = 'auto';
	$('overlay').style.display = 'none';
	$('overlaywin').style.display = 'none';
}

function hideOverlayWindow(pid)
{
	if (parent && parent != window && parent.showContent)
		parent.closeOverlay(pid);
}

function setOverlayHeight( newHeight )
{
	// Adding one pixel removes vertical scrollbars from Firefox
	newHeight++;
	
	if (parent && parent != window && parent.setOverlayHeight) {
		parent.setOverlayHeight(newHeight);
	} else if ($('overlaybody')) {
		var totalOffset = OVERLAY_TOP+OVERLAY_BOTTOM+$('overlayhead').offsetHeight;
		if (totalOffset+newHeight > $D.getViewportHeight())
			newHeight = $D.getViewportHeight()-totalOffset;
		
		var sizeAnim = new $A('overlaybody', { height: { to: newHeight } }, 0.2);
		sizeAnim.animate();
		
		if (!window.XmlHttpRequest && $('overlayframe'))
			$('overlayframe').style.height = newHeight+'px';
	}
}

function contentLoaded()
{
	if (parent && parent != window && parent.showContent)
		{
		//parent.showContent('800');		
		parent.showContent($('bodydata').offsetHeight);                 
		}
}

function showContent( height )
{
	var loader = $D.getElementsByClassName('loading', 'div', 'overlaybody')[0];
	if (loader) {
		$('overlayframe').style.height = '10%';
		loader.style.display = 'none';
	}
	setOverlayHeight(height+10);
}
function reloadparent()
{
	opener.reload();
	
}
function refreshcalendar()
{
         try
         { 
if(document.getElementById('currentMonth'))
        {
	var id=document.getElementById('user_id').value;
        var startDay=document.getElementById('startday').value;
	var callbackHandler={success:handleLoadDisplay,argument:{startDay:"null"}};

	var target="/cakedev/calendar/getdateinmonth/"+id;
	YAHOO.util.Connect.setForm(document.getElementById("frmcalendar"));
	var request=YAHOO.util.Connect.asyncRequest('POST',target,callbackHandler);
        }	
}
catch(e)
{

}
}
