
var TimeToShow = 40;
var elAd; // non-Null after the initial wait period. Holds reference to "flyad" DOM element
var EnterSpeed = 20;
var ExitSpeed = 100;

addLoadEvent(SetStartupTimer);

//---------------------------------------------------------------------

function SetStartupTimer() {

	window.setTimeout('ShowFlyAd()', TimeToShow * 1000);

}

//---------------------------------------------------------------------

function ShowFlyAd() {
			
	// Get Ad object
	elAd = document.getElementById("flyad");
	if (!elAd) { return false; } // If we don't have it, exit (for older browsers)

	// Check if we've already shown it
	if (getCookie("FlyAd20071004Shown") != "") {
		return false;
	}

	// Measure page and window
	var PageSize = getPageSize();
	var ScrollPos = getScrollingPosition();
	
	// Initial position and display
	elAd.style.top = (-parseInt(elAd.style.height)) + "px";	
	elAd.style.left = ((PageSize[2] - parseInt(elAd.style.width) ) / 2 ) + "px";	
	elAd.style.display = "";
	
	// Hook onResize event to reposition ad
	window.onresize = FlyAdOnWindowScroll;
	window.onscroll = FlyAdOnWindowScroll;
	
	
	// Set variables in the Ad
	elAd.DesiredTop = (PageSize[3] - parseInt(elAd.style.height) ) / 2 + ScrollPos[1];	
	elAd.Retired = false;
	elAd.MovingTimer = setInterval('MoveAd()', 30);


	// Set cookie for the Ad not to show again
	setCookie("FlyAd20071004Shown", "true", true);
		
}

//---------------------------------------------------------------------

function FlyAdClose() {

	if (!elAd) { return false; }	

	// Measure page and window
	var PageSize = getPageSize();

	elAd.DesiredLeft = PageSize[2];	
	elAd.Retired = true;
	elAd.MovingTimer = setInterval('MoveAd()', 30);

}

//---------------------------------------------------------------------

function MoveAd() {

	if (!elAd) { return false; }	

	
	if (elAd.Retired != true) {
	
		// Move Down
		var curTop = parseInt(elAd.style.top);		
		
		if (curTop < elAd.DesiredTop) {
			elAd.style.top = (curTop + EnterSpeed) + "px";
		} else {
			if (curTop > (elAd.DesiredTop + EnterSpeed + 10)) {
				elAd.style.top = (curTop - EnterSpeed) + "px";
			} else {
				clearInterval(elAd.MovingTimer);
				elAd.MovingTimer = null;
			}
		}
		
	} else { // elAd.Retired
		// Move Right
		var curLeft = parseInt(elAd.style.left);
		
		if (curLeft < elAd.DesiredLeft) {
			elAd.style.left = (curLeft + ExitSpeed) + "px";
		} else {
			clearInterval(elAd.MovingTimer);
			elAd.MovingTimer = null;
			elAd.style.display = "none";
		}				
		
	}

}

//---------------------------------------------------------------------

function FlyAdOnWindowScroll() {

	if (!elAd) { return false; }	
	if (elAd.Retired) { return false; }

	
	var PageSize = getPageSize();
	var ScrollPos = getScrollingPosition();
	
	// Center horizontally
	elAd.style.left = (((PageSize[2] - parseInt(elAd.style.width) ) / 2 ) + ScrollPos[0]) + "px";
	
	elAd.DesiredTop = (PageSize[3] - parseInt(elAd.style.height) ) / 2 + ScrollPos[1];	
	
	// If not currently moving, restart
	if (elAd.MovingTimer == null) {
		elAd.MovingTimer = setInterval('MoveAd()', 30);
	}
		
}

//========================================================================================
//========================================================================================
//========================================================================================


function addLoadEvent(func)
{
	var oldonload = window.onload;
	if (typeof window.onload != 'function'){
    	window.onload = func;
	} else {
		window.onload = function(){
			oldonload();
			func();
		};
	}

}

//========================================================================================
//																		Cookies
//========================================================================================
function getCookie(cookieName) {
	var cookies = document.cookie.split(';');
	for (var i=0; i < cookies.length; i++) {
		var cookieParts = cookies[i].split("=");
		if (trim(cookieParts[0]) == cookieName) {
			if (cookieParts[1]) {
				return trim(cookieParts[1]);
			}
		}
	}
	return false;
}

function setCookie(cookieName, cookieValue, persistent) {

	var strCookie = cookieName  + "=" + cookieValue;
	
	if (persistent == true) {
		var datExpires = new Date();
		datExpires.setTime(datExpires.getTime() + 10 * 365 * (24 * 60 * 60 * 1000));  // 10 years
		strCookie += "; expires=" + datExpires.toGMTString();
	}
	
	strCookie += "; path=/";
		
	document.cookie = strCookie;
}

//------------------------------------------------------------------------------



//------------------------------------------------------------------------------

function getElementSize(obj) {
	return [obj.offsetWidth,obj.offSetHeight];
}

//------------------------------------------------------------------------------

function getWindowSize() {

	if (typeof window.innerHeight != "undefined") {
		return [window.clientWidth,window.innerHeight];
	} else {
		return [document.documentElement.clientWidth, document.documentElement.clientHeight]; // IE 6
	}
	
}

//------------------------------------------------------------------------------
// 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;
	
	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;
}

//------------------------------------------------------------------------------

function getScrollingPosition()
{
 var position = [0, 0];

 if (typeof window.pageYOffset != 'undefined')
 {
   position = [
       window.pageXOffset,
       window.pageYOffset
   ];
 }

 else if (typeof document.documentElement.scrollTop
     != 'undefined' && document.documentElement.scrollTop > 0)
 {
   position = [
       document.documentElement.scrollLeft,
       document.documentElement.scrollTop
   ];
 }

 else if (typeof document.body.scrollTop != 'undefined')
 {
   position = [
       document.body.scrollLeft,
       document.body.scrollTop
   ];
 }

 return position;
}


//========================================================================================
//                        Functions for trimming and padding strings
//========================================================================================
  
// Removes leading whitespaces
function LTrim( value ) {
	var re = /\s*((\S+\s*)*)/;
	return value.replace(re, "$1");	
}

// Removes ending whitespaces
function RTrim( value ) {	
	var re = /((\s*\S+)*)\s*/;
	return value.replace(re, "$1");	
}

// Removes leading and ending whitespaces
function trim( value ) {	
	return LTrim(RTrim(value));	
}