function getScrollXY() {
  var scrOfX = 0, scrOfY = 0;
  if( typeof( window.pageYOffset ) == 'number' ) {
    //Netscape compliant
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    //DOM compliant
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    //IE6 standards compliant mode
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }
  return [ scrOfX, scrOfY ];
}

function alertSize() {
  var myWidth = 0, myHeight = 0;
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
  //window.alert( 'Width = ' + myWidth );
  //window.alert( 'Height = ' + myHeight );
  return [myWidth , myHeight ];
}


function myScreen() {
		this.f_clientWidth = function () {
			return this.f_filterResults (
				window.innerWidth ? window.innerWidth : 0,
				document.documentElement ? document.documentElement.clientWidth : 0,
				document.body ? document.body.clientWidth : 0
			);
		}
		this.f_clientHeight = function() {
			return this.f_filterResults (
				window.innerHeight ? window.innerHeight : 0,
				document.documentElement ? document.documentElement.clientHeight : 0,
				document.body ? document.body.clientHeight : 0
			);
		}
		this.f_scrollLeft = function () {
			return this.f_filterResults (
				window.pageXOffset ? window.pageXOffset : 0,
				document.documentElement ? document.documentElement.scrollLeft : 0,
				document.body ? document.body.scrollLeft : 0
			);
		}
		this.f_scrollTop = function() {
			return this.f_filterResults (
				window.pageYOffset ? window.pageYOffset : 0,
				document.documentElement ? document.documentElement.scrollTop : 0,
				document.body ? document.body.scrollTop : 0
			);
		}
		this.f_filterResults = function (n_win, n_docel, n_body) {
			var n_result = n_win ? n_win : 0;
			if (n_docel && (!n_result || (n_result > n_docel)))
				n_result = n_docel;
			return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
		}
}

function StatsIncrement(listingId, linkId)
{
    // Mozilla/Safari
    if (window.XMLHttpRequest) {
    	http  = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject) {
    	http  = new ActiveXObject("Microsoft.XMLHTTP");
    }
    http.open("GET", "./stats_increment.php?listing_id="+listingId+"&link_id="+linkId);
    //http.onreadystatechange=function() {
    //	  if(http.readyState == 4) {
    //	    alert(http.responseText);
    //	  }
    //}
    http.send(null);
}

function dhtmlWindowClose(id)
{
	var alpha_start = 80;
	var alpha_timerRunning;
	var alpha_timerID;
	var alpha_increment=16;
	var alpha_secs=0.5;
	var alpha_delay =(alpha_secs*1000)/(alpha_start/alpha_increment);

	var myoverlay = document.getElementById(id+'_dhtml_Overlay');
	var mywindow = document.getElementById(id+'_dhtml_window');
	var myiframe = document.getElementById(id+'_dhtml_iframe');
	var myflash = document.getElementById(id+'_dhtml_flash');
	var mycaption = document.getElementById(id+'_caption');
	var mybottom = document.getElementById(id+'_dhtml_bottom');

	mywindow.style.display = 'none';
	mywindow.style.visibility = 'hidden';
	myflash.style.display = 'none';


	alphaTimerRemove();		

	function alphaTimerRemove()
	{
		if (alpha_start<0)
	    {
			if(alpha_timerRunning)
		        clearTimeout(alpha_timerID)
			myoverlay.style.visibility = 'hidden';
		    alpha_timerRunning = false
		    
	    }
	    else
	    {
			myoverlay.style.opacity = (alpha_start/100);	
			myoverlay.style.filter = 'alpha(opacity='+alpha_start+')';
			alpha_start -= alpha_increment;
	    	self.status = alpha_secs;
	        alpha_timerRunning = true;
	        alpha_timerID = setTimeout(function(){alphaTimerRemove();}, alpha_delay);
	    }

	} 
		
}

function dhtmlWindow(id,source,caption,type)
{
	//alert(id+"  "+source+"  "+caption+"  "+type);
	var that = this;
	var scrollingstyle;
	var scrollingatt;
	this.caption = caption;
	this.source = source;
	this.windowWidth = 620;
	this.windowHeight = 530;
	this.scrolling = 'no';
	this.content = '';
	var id = id;
	var type = type;

	
	if(type=="div")
	{
		document.write('<div>');
		document.write('<div id="'+id+'_dhtml_Overlay" style="position: fixed; 	z-index: 9998; 	left: 0; 	top: 0; 	width: 100%; 	height: 100%; 	background-color: #000; 	cursor: pointer; 	visibility: hidden; 	opacity: 0.1; filter: alpha(opacity=10); -moz-opacity: 0.1;"></div>');
		document.write('<div id="'+id+'_dhtml_window" align="center" style = "position: absolute; 	z-index: 9999; 	overflow: hidden; 	background-color: #68802B; 	width:620px; 	height:530px; 	display:none;"	>');
		document.write('<div id="'+id+'_dhtml_flash" style = "background-color: #68802B; 	width:0px; 	height:0px;	margin-left:10px; margin-top:10px;	display:none;"	></div>');
		document.write('<div id="'+id+'_dhtml_iframe" class = "dhtmliframe" marginwidth="0" marginheight="0" vspace="0" hspace="0" style="background-color: #68802B; 	width:600px; 	height:470px; 	margin-left:0px; 	margin-top:10px; 	border: 0px; ">');
		document.write(this.content);
		document.write('</div>');
		document.write('<div id="'+id+'_dhtml_bottom"  style="width:100px; 	float:right; 	margin-top:0px;"><a id="'+id+'_dhtmlCloseLink" style="display: block; 	float: right; 	width: 66px;	height: 22px;	background: transparent url(styles/closelabel.gif) no-repeat center;	margin-right: 10px;	outline: none;" href="javascript:void(0);" onClick="javascript: dhtmlWindowClose(\''+id+'\');"></a></div>');
		document.write('<div id="'+id+'_caption" style="margin-top:5px; color:white;	font-size:12px;	display: block;	float: left;	width: 400px;	height: 22px;	margin-left: 10px;	outline: none;	font-weight: bold;"></div>');
		document.write('</div>');
		document.write('</div>');
	}
	else
	{
		document.write('<div>');
		document.write('<div id="'+id+'_dhtml_Overlay" style="position: fixed; 	z-index: 9998; 	left: 0; 	top: 0; 	width: 100%; 	height: 100%; 	background-color: #000; 	cursor: pointer; 	visibility: hidden; 	opacity: 0.1; filter: alpha(opacity=10); -moz-opacity: 0.1;"></div>');
		document.write('<div id="'+id+'_dhtml_window" align="center" style = "position: absolute; 	z-index: 9999; 	overflow: hidden; 	background-color: #68802B; 	width:620px; 	height:530px; 	display:none;"	>');
		document.write('<div id="'+id+'_dhtml_flash" style = "background-color: #68802B; 	width:0px; 	height:0px;	margin-left:10px; margin-top:10px;	display:none;"	></div>');
		document.write('<iframe id="'+id+'_dhtml_iframe" class = "dhtmliframe" scrolling="vertical" noresize="noresize" frameborder="0" marginwidth="0" marginheight="0" vspace="0" hspace="0" style="background-color: #68802B; 	width:600px; 	height:470px; 	margin-left:0px; 	margin-top:10px; 	border: 0px; "> </iframe>');
		document.write('<div id="'+id+'_dhtml_bottom"  style="width:100px; 	float:right; 	margin-top:0px;"><a id="'+id+'_dhtmlCloseLink" style="display: block; 	float: right; 	width: 66px;	height: 22px;	background: transparent url(styles/closelabel.gif) no-repeat center;	margin-right: 10px;	outline: none;" href="javascript:void(0);" onClick="javascript: dhtmlWindowClose(\''+id+'\');"></a></div>');
		document.write('<div id="'+id+'_caption" style="margin-top:5px; color:white;	font-size:12px;	display: block;	float: left;	width: 400px;	height: 22px;	margin-left: 10px;	outline: none;	font-weight: bold;"></div>');
		document.write('</div>');
		document.write('</div>');
	}
	
	var myoverlay = document.getElementById(id+'_dhtml_Overlay');
	var mywindow = document.getElementById(id+'_dhtml_window');
	var myiframe = document.getElementById(id+'_dhtml_iframe');
	var myflash = document.getElementById(id+'_dhtml_flash');
	var mycaption = document.getElementById(id+'_caption');
	var mybottom = document.getElementById(id+'_dhtml_bottom');
	var myscreen = new myScreen();
	
	
	
	
	/*****************************************************/
	//		Overlay Timer Variables
	
	var finalOpacity = 80;
	var alpha_secs = 0.5
	var alpha_timerID = null
	var alpha_timerRunning = false
	var alpha_delay = 10
	var alpha_start =0;
	var alpha_final = finalOpacity;
	var alpha_increment = 16;
	alpha_delay =(alpha_secs*1000)/((alpha_final-alpha_start)/alpha_increment);

	/*****************************************************/
	//		Height and width Timer Variables
	
	var scrollPos = getScrollXY();
	var winSize = alertSize();
	
	var scrollLeft = scrollPos[0];	//myscreen.f_scrollLeft();
	var scrollTop = scrollPos[1];	//myscreen.f_scrollTop();
	var clientWidth = winSize[0];	//myscreen.f_clientWidth();
	var clientHeight = winSize[1];	//myscreen.f_clientHeight();

	var startWindowWidth = 200;
	var startWindowHeight = 200;
	var windowWidth = this.windowWidth;
	var windowHeight = this.windowHeight-50;
	var scrolling;
	var windowHeight2 = this.windowHeight;
	var onlyHeight2 = (windowHeight2-windowHeight)/2;
	var addLeft = 0;
	var addTop = 0;
	var loop = 5; 
	var loop2 = 5;
	var windowWidthAdd = parseInt((windowWidth-startWindowWidth)/loop);
	var windowHeightAdd = parseInt((windowHeight-startWindowHeight)/loop)
	var windowHeight2Add = parseInt((windowHeight2-windowHeight)/loop2)
	var currentWidth = startWindowWidth;
	var currentHeight = startWindowHeight;

	var height_secs = 0.5;
	var height_timerID = null;
	var height_timerRunning = false;
	var height_delay = 10;
	var height2_timerID = null;
	var height2_timerRunning = false;
	var height2_delay = 10;
	var width_secs = 0.5;
	var width_timerID = null;
	var width_timerRunning = false;
	var width_delay = 10;

	height_delay =(height_secs*1000)/((windowHeight-startWindowHeight)/windowHeightAdd);
	height2_delay =(height_secs*1000)/((windowHeight2-windowHeight)/windowHeight2Add);
	width_delay =(width_secs*1000)/((windowWidth-startWindowWidth)/windowWidthAdd);
	

	function showAnimated ()
	{
		var iframewidth;
		var iframeheight;
		scrollLeft = myscreen.f_scrollLeft();
		scrollTop = myscreen.f_scrollTop();

		myoverlay.style.visibility = 'visible';
		mywindow.style.display = 'block';
		mywindow.style.visibility = 'visible';
		
		myiframe.style.display  = 'none';
		iframewidth = windowWidth-20;
		iframeheight = windowHeight2-60;
		myiframe.style.width = iframewidth+'px';
		myiframe.style.height = iframeheight+'px';
		mywindow.style.width = startWindowWidth + 'px';
		mywindow.style.height = startWindowHeight + 'px';
		mywindow.style.left = getWindowLeft(scrollLeft,clientWidth,200,addLeft) + 'px';
		mywindow.style.top = getWindowTop(scrollTop,clientHeight,200,addTop) + 'px';
		mybottom.style.display='none';
		mycaption.style.display='none';
		
		alpha_start =0;
		alpha_final = finalOpacity;

		alphaTimer();

		currentHeight = startWindowHeight;
		heightTimer();

		if(scrolling=='vertical')
			{ myiframe.style.overflowX = 'hidden';
			  myiframe.style.overflowY = 'visible';
				}
		else if(scrolling=='horizental')
			{ myiframe.style.overflowY = 'hidden';
			  myiframe.style.overflowX = 'visible';}
		else if(scrolling=='both')
			{ myiframe.style.overflow = 'visible';}
		else if(scrolling=='no')
			{ myiframe.style.overflow = 'hidden';}

		if(scrolling=='vertical')
			{myiframe.scrolling='no'; }
		else if(scrolling=='horizental')
			{myiframe.scrolling='no'; }
		else if(scrolling=='both')
			{myiframe.scrolling='yes'; }
		else if(scrolling=='no')
			{myiframe.scrolling='no'; }
		
	}
	function getWindowLeft(scrollLeft,clientWidth,windowWidth,addLeft)
	{
		return scrollLeft+ parseInt((clientWidth-windowWidth)/2) + addLeft;
	}
	function getWindowTop(scrollTop,clientHeight,windowHeight,addTop)
	{
		return scrollTop+ parseInt((clientHeight-windowHeight)/2) + addTop;
	}


	function alphaTimer()
	{
		if (alpha_start>=alpha_final)
	    {
			if(alpha_timerRunning)
		        clearTimeout(alpha_timerID)
		    myoverlay.style.opacity = (alpha_final/100);	
			myoverlay.style.filter = 'alpha(opacity='+alpha_final+')';
		    alpha_timerRunning = false
		    return null;
	    }
	    else
	    {
			myoverlay.style.opacity = (alpha_start/100);	
			myoverlay.style.filter = 'alpha(opacity='+alpha_start+')';
			alpha_start += alpha_increment;
	    	self.status = alpha_secs;
	        alpha_timerRunning = true;
	        alpha_timerID = self.setTimeout(function(){alphaTimer();}, alpha_delay);
	    }

	} 

	function alphaTimerRemove()
	{
		if (alpha_start<0)
	    {
			if(alpha_timerRunning)
		        clearTimeout(alpha_timerID)
			myoverlay.style.visibility = 'hidden';
		    alpha_timerRunning = false
		    
	    }
	    else
	    {
			myoverlay.style.opacity = (alpha_start/100);	
			myoverlay.style.filter = 'alpha(opacity='+alpha_start+')';
			alpha_start -= alpha_increment;
	    	self.status = alpha_secs;
	        alpha_timerRunning = true;
	        alpha_timerID = setTimeout(function(){alphaTimerRemove();}, alpha_delay);
	    }

	} 
	

	function heightTimer ()
	{
		if (currentHeight>=windowHeight)
	    {
			if(height_timerRunning)
		        clearTimeout(height_timerID)
			mywindow.style.height = windowHeight+'px';
			mywindow.style.top = (getWindowTop(scrollTop,clientHeight,windowHeight,addTop)-onlyHeight2)+'px';
			currentWidth = startWindowWidth;
			widthTimer();
			height_timerRunning = false
		    
	    }
	    else
	    {
			mywindow.style.height = currentHeight+'px';
			mywindow.style.top = getWindowTop(scrollTop,clientHeight,currentHeight,addTop)+'px';
			currentHeight = currentHeight + windowHeightAdd;
	    	self.status = height_secs;
	    	height_timerRunning = true;
	    	height_timerID = setTimeout(function(){heightTimer();}, height_delay);
	    }

	} 

	function widthTimer ()
	{
		if (currentWidth>=windowWidth)
	    {
			if(width_timerRunning)
		        clearTimeout(width_timerID)
			mywindow.style.width = windowWidth+'px';
			mywindow.style.left = getWindowLeft(scrollLeft,clientWidth,windowWidth,addLeft)+'px';
			myiframe.style.display  = 'block';
			height2Timer();
			width_timerRunning = false
		    
	    }
	    else
	    {
			mywindow.style.width = currentWidth+'px';
			mywindow.style.left = getWindowLeft(scrollLeft,clientWidth,currentWidth,addLeft)+'px';
			currentWidth = currentWidth + windowWidthAdd;
	    	self.status = width_secs;
	    	width_timerRunning = true;
	    	width_timerID = setTimeout(function(){widthTimer();}, width_delay);
	    }

	} 

	function height2Timer ()
	{
		if (currentHeight>=windowHeight2)
	    {
			if(height2_timerRunning)
		        clearTimeout(height2_timerID)
			mywindow.style.height = windowHeight2+'px';
			mybottom.style.display='block';
			mycaption.style.display='block';					
			myflash.style.display='block';
			height2_timerRunning = false
		    
	    }
	    else
	    {
			mywindow.style.height = currentHeight+'px';
			currentHeight = currentHeight + windowHeight2Add;
	    	self.status = height_secs;
	    	height2_timerRunning = true;
	    	height2_timerID = setTimeout(function(){height2Timer();}, height2_delay);
	    }

	} 	

	this.show = function()
	{
		
		windowWidth = this.windowWidth;
		windowHeight = this.windowHeight-50;
		windowHeight2 = this.windowHeight;
		scrolling = this.scrolling;
		//type = this.type;
		//alert(type)
		if(type=="flash")
		{
			embed = "<div><object width='"+(windowWidth-20)+"' height='"+(windowHeight-10)+"'><param name='movie' value='"+ this.source +"'><embed src='"+ this.source +"' width='"+(windowWidth-20)+"' height='"+(windowHeight-10)+"'></embed></object></div>";
			myflash.innerHTML = embed;
		}
		else if(type=="image")
		{
			embed = "<div><img height='"+(windowHeight-10)+"' width='"+(windowWidth-20)+"' src='"+ this.source +"' alt='Promo' style='cursor: -moz-zoom-in;'/></div>";
			myflash.innerHTML = embed;
		}
		else if(type=="div")
		{
			//alert(myiframe);
			myiframe.innerHTML = this.content;
		}
		else
			myiframe.src = this.source;

		mycaption.innerHTML = this.caption;
		showAnimated();
	};
	this.close = function()
	{
		alphaTimerRemove();
		mywindow.style.display = 'none';
		mywindow.style.visibility = 'hidden';
	};
	
	this.resize = function(URL,width,height)
	{
		mywindow.style.display = 'none';
		mywindow.style.visibility = 'hidden';
		myiframe.src = URL;
		mywindow.style.width = width+'px';
		mywindow.style.left = getWindowLeft(scrollLeft,clientWidth,width,addLeft)+'px';
		currentWidth = width;
		mywindow.style.height = height+'px';
		mywindow.style.top = getWindowTop(scrollTop,clientHeight,height,addTop)+'px';
		currentHeight = height;
		myiframe.style.width = (width-20)+'px';
		myiframe.style.height = (height-60)+'px';
		mywindow.style.display = 'block';
		mywindow.style.visibility = 'visible';
	};
	
	
	
}
