var op = 0.6;
var dur = 0.4;


var Watermark = Class.create();

Watermark.prototype = {
	
	initialize: function() {	

		var bod = document.getElementsByTagName("body").item(0);
		
		var wm = document.createElement("div");
		wm.setAttribute('id','watermark');
		wm.style.display = 'none';
		wm.onclick = function() { myWatermark.end(); }
		bod.appendChild(wm);

		var stripeDiv = document.createElement("div");
		stripeDiv.setAttribute('id','stripe');
		stripeDiv.style.display = 'none';
		stripeDiv.onclick = function() { myWatermark.end(); }
		bod.appendChild(stripeDiv);

		var stripeImg = document.createElement("img");
		stripeImg.setAttribute('src','/images/stripe.gif');
		stripeDiv.appendChild(stripeImg);

	},

	start: function() {	
		var ps = getPageSize();
		if(readCookie('watermark')==null) {
			hideSelectBoxes();
			hideFlash();
			var ps = getPageSize();
			if(ps[1]<$('stripe').getHeight())
				ps[1]=$('stripe').getHeight();
			$('watermark').style.width = ps[0] + "px";
			$('watermark').style.height = ps[1]+ "px";
			
			$('stripe').style.left = ((ps[0]/2)-($('stripe').getWidth()/2))+'px';
			
			new Effect.Appear('watermark', { duration: dur, from: 0.0, to: op, queue: 'front' });
			new Effect.Appear('stripe', { duration: dur, from: 0.0, to: op, queue: 'end'  });		
			createCookie('watermark', true, 30);
		} else {	
			new Effect.BlindDown('offerbar', { queue:'front'});		
			if(readCookie('name')==null){
				hideSelectBoxes();
				hideFlash();
				setTimeout(showBubble, 1000);
			} else {
				setTimeout(showSelectBoxes, 2000);
				setTimeout(showFlash, 2000);
			}
		}

	},
	end: function() {
		new Effect.Fade('watermark', { duration: dur, queue:'front'});
		new Effect.Fade('stripe', { duration: dur, queue:'front'});		
		new Effect.BlindDown('offerbar', { queue:'end'});		
		if(readCookie('name')==null){
			setTimeout(showBubble, 2000);
		} else {
			setTimeout(showSelectBoxes, 2000);
			setTimeout(showFlash, 2000);
		}
	}
}

function closeBubble(){
	$('bubblepop').style.display = 'none';								
	setTimeout(showSelectBoxes, 1000);
	setTimeout(showFlash, 1000);
}
function showBubble(){
	var ps = getPageSize();
	$('bubblepop').style.left = ((ps[0]/2)-400)+'px';
	$('bubblepop').style.display = 'block';									
}
// -----------------------------------------------------------------------------------

//
// getPageScroll()
// Returns array with x,y page scroll values.
// Core code from - quirksmode.com
//
function getPageScroll(){

	var xScroll, yScroll;

	if (self.pageYOffset) {
		yScroll = self.pageYOffset;
		xScroll = self.pageXOffset;
	} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
		yScroll = document.documentElement.scrollTop;
		xScroll = document.documentElement.scrollLeft;
	} else if (document.body) {// all other Explorers
		yScroll = document.body.scrollTop;
		xScroll = document.body.scrollLeft;	
	}

	arrayPageScroll = new Array(xScroll,yScroll) 
	return arrayPageScroll;
}

// -----------------------------------------------------------------------------------

//
// getPageSize()
// Returns array with page width, height and window width, height
// Core code from - quirksmode.com
// Edit for Firefox by pHaez
//
function getPageSize(){
	
	var xScroll, yScroll;
	
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		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
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			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;
	}	
	
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}

	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}

	ps = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
	return ps;
}

// ---------------------------------------------------

function showSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideSelectBoxes(){
	var selects = document.getElementsByTagName("select");
	for (i = 0; i != selects.length; i++) {
		selects[i].style.visibility = "hidden";
	}
}

// ---------------------------------------------------

function showFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "visible";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "visible";
	}
}

// ---------------------------------------------------

function hideFlash(){
	var flashObjects = document.getElementsByTagName("object");
	for (i = 0; i < flashObjects.length; i++) {
		flashObjects[i].style.visibility = "hidden";
	}

	var flashEmbeds = document.getElementsByTagName("embed");
	for (i = 0; i < flashEmbeds.length; i++) {
		flashEmbeds[i].style.visibility = "hidden";
	}

}


// ---------------------------------------------------



function getQueryVariable(variable) {
  
  var query = window.location.search.substring(1);
  var vars = query.split("&");
  for (var i=0;i<vars.length;i++) {

	var pair = vars[i].split("=");

	if (pair[0].toLowerCase() == variable) {
	  return pair[1];
    }
  }
  return null; 
}


function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function eraseCookie(name) {
	createCookie(name,"",-1);
}

var company_name = "Your Logo Here";

if(getQueryVariable('name')!=null||readCookie('name')!=null){
	if(getQueryVariable('name')!=null){
		company_name = unescape(getQueryVariable('name')).
		replace(/&/g,'&amp;').                                         
    replace(/>/g,'&gt;').                                           
    replace(/</g,'&lt;').                                           
    replace(/"/g,'&quot;');
		createCookie('name', company_name, 30);
	} else if(readCookie('name')!=null) {
		company_name = readCookie('name').
		replace(/&/g,'&amp;').                                         
    replace(/>/g,'&gt;').                                           
    replace(/</g,'&lt;').                                           
    replace(/"/g,'&quot;');
	}	
}


function getPageTitle(){
	return company_name;
}


function initWatermark() { 
	myWatermark = new Watermark(); 
	myWatermark.start(); 
	
}
Event.observe(window, 'load', initWatermark, false);
