// JavaScript Document

var Popover = Class.create();

Popover.prototype = {
	initialize: function(options) {
		this.options = Object.extend({
			imageURL : "",
			destinationURL : "",
			cookieDuration : 168, // in hours, defaults to one week
			cookiePath : "",
			scrimOpacity : 0.75,
			scrimColor : "#000000"
				 }, options || {});
		if (this.cookie() === false) {
			this.create();
		}
	},
	//create the markup
	create: function () {
		this.bodyElement = $$("body");
		this.zMax();
		this.scrim = new Element("div",{style : 'position:fixed; z-index:'+(this.z+1)+'; opacity:0; display:none; background-color:'+this.options.scrimColor+';'});
		this.scrim.onclick = function (){this.hidePopover()}.bind(this);
		this.ad = new Element("div",{style : 'position:fixed; z-index:'+(this.z+2)+'; opacity:0; display:none; visibility:hidden;'});
		this.imageLink = new Element("a",{href:this.options.destinationURL});
		this.image = new Element("img");
		this.image.onload = function () {this.build()}.bind(this);
		this.image.src = this.options.imageURL;
	},
	// find the maximum z-index value that already exists on the page
	zMax: function() {
		var elements = $$("*");
		this.z = 0;
		for (var i=0;i < elements.length;i++) {
			if (this.z < elements[i].style.zIndex) {
				this.z = elements[i].style.zIndex;
			}
		}
	},
	
	build: function() {
		// insert the markup
		this.imageLink.insert({bottom:this.image});
		this.ad.insert({bottom:this.imageLink});
		this.bodyElement[0].insert({bottom:this.ad});
		this.bodyElement[0].insert({bottom:this.scrim});
		this.detectIE();
		this.getPageSize();
		this.ad.style.display = "block";
		this.dimensions = this.ad.getDimensions();
		this.ad.style.display = "none";
		this.ad.style.visibility = "visible";
		this.showPopover();
	},
	// check for, and create the cookie
	cookie: function() {
		var cookie = new Cookie(window.document, 'rivista_popover', this.options.cookieDuration, this.options.cookiePath);
		if (cookie.load() === true) {
			return true;
		}
		else {
			cookie.visited = "true";
			cookie.store();
			return false;
		}
	},
	// get the page dimensions
	getPageSize: function() {
		if (window.innerHeight && window.scrollMaxY) {	
			this.xScroll = document.body.scrollWidth;
			this.yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			this.xScroll = document.body.scrollWidth;
			this.yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			this.xScroll = document.body.offsetWidth;
			this.yScroll = document.body.offsetHeight;
		}
		
		if (self.innerHeight) {	// all except Explorer
			this.windowWidth = self.innerWidth;
			this.windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			this.windowWidth = document.documentElement.clientWidth;
			this.windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			this.windowWidth = document.body.clientWidth;
			this.windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(this.yScroll < this.windowHeight){
			this.pageHeight = this.windowHeight;
		} else { 
			this.pageHeight = this.yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(this.xScroll < this.windowWidth){	
			this.pageWidth = this.windowWidth;
		} else {
			this.pageWidth = this.xScroll;
		}
	},
	// show the popover
	showPopover: function() {
		this.scrim.style.height = (this.pageHeight + 'px');
		this.scrim.style.width = (this.pageWidth + 'px');
		this.scrim.style.left = ('0px');
		this.scrim.style.top = ('0px');
		this.ad.style.left = ((Math.round(this.pageWidth/2)-(Math.round(this.dimensions.width/2))) + 'px');
		this.ad.style.top = ((Math.round(this.windowHeight/2)-(Math.round(this.dimensions.height/2))) + 'px');
		if (this.ie === true) {
			 Event.observe(window, 'scroll', this.IEFixed.bind(this));
		}
		var a = new Effect.Appear(this.scrim, { duration: 0.5, from: 0, to: this.options.scrimOpacity });
		var b = new Effect.Appear(this.ad, { duration: 0.5, from: 0, to: 1 });
	},
	// hide the popover
	hidePopover: function() {
		if (this.ie === true) {
			  Event.stopObserving(window, 'scroll', this.IEFixed.bind(this));
		}
		var c = new Effect.Fade(this.scrim, { duration: 0.5, from: this.options.scrimOpacity, to: 0 });
		var d = new Effect.Fade(this.ad, { duration: 0.5, from: 1, to: 0 });
	},
	// detect the hated IE6
	detectIE: function() {
		if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)){ //test for MSIE x.x;
			var ieversion=new Number(RegExp.$1) // capture x.x portion and store as a number
			if (ieversion>=6 && ieversion<7) {
				this.ad.style.position = "absolute";
				this.scrim.style.position = "absolute";
				this.ie = true;
			}
			else {
				this.ie = false;
			}
		}
	},
	// keep the popover centered in IE6
	IEFixed : function(){
        var scroll = document.documentElement.scrollTop;
        var s_top = scroll + Math.round((document.viewport.getDimensions().height - (this.ad.offsetHeight || 0)) / 2);
        this.ad.style.top = s_top + 'px';
    }
}


