// SpryAlwaysVisible.js - version 0.1 - Spry Pre-Release 1.6.1
var Spry;
if (!Spry) Spry = {};
if (!Spry.Widget) Spry.Widget = {};

Spry.Widget.BrowserSniff = function(){
	var b = navigator.appName.toString();
	var up = navigator.platform.toString();
	var ua = navigator.userAgent.toString();
	this.mozilla = this.ie = this.opera = this.safari = false;
	var re_opera = /Opera.([0-9\.]*)/i;
	var re_msie = /MSIE.([0-9\.]*)/i;
	var re_gecko = /gecko/i;
	var re_safari = /(applewebkit|safari)\/([\d\.]*)/i;
	var r = false;
	if ( (r = ua.match(re_opera))) {
		this.opera = true;
		this.version = parseFloat(r[1]);
	} else if ( (r = ua.match(re_msie))) {
		this.ie = true;
		this.version = parseFloat(r[1]);
	} else if ( (r = ua.match(re_safari))) {
		this.safari = true;
		if(parseFloat(r[2]) < 420)
			this.version = 2;
		else
			this.version = 3;		
	} else if (ua.match(re_gecko)) {
		var re_gecko_version = /rv:\s*([0-9\.]+)/i;
		r = ua.match(re_gecko_version);
		this.mozilla = true;
		this.version = parseFloat(r[1]);
	}
	this.windows = this.mac = this.linux = false;
	this.Platform = ua.match(/windows/i) ? "windows" :
					(ua.match(/linux/i) ? "linux" :
					(ua.match(/mac/i) ? "mac" :
					ua.match(/unix/i)? "unix" : "unknown"));
	this[this.Platform] = true;
	this.v = this.version;

	if (this.safari && this.mac && this.mozilla) {
		this.mozilla = false;
	}
};
Spry.is = new Spry.Widget.BrowserSniff();
Spry.Widget.AlwaysVisible = function(avc_element, options){
	options = Spry.Widget.Utils.firstValid(options, {});
	this.init(avc_element, options);
};
Spry.Widget.AlwaysVisible.prototype.init = function(avc_element, options){
	var Utils = Spry.Widget.Utils;
	this.avcElement = Utils.getElement(avc_element);

	if (typeof this.avcElement == 'undefined' || !this.avcElement){
		this.showError('The element "' + avc_element + '" do not exists in the page');
		return false;
	}
	Utils.setOptions(this, options);
	if(Spry.is.ie && Spry.is.version == '6'){
		this.avcElement.style.position = 'absolute';
	}else{
		this.avcElement.style.position = 'fixed';
	}
	this.avcElement.style.zindex = "400";
	var bodyElement = (Spry.is.safari) ? document.body : document.documentElement;
	var self=this;
	var bodySizeListener = Spry.Widget.Utils.addEventListener;
	bodySizeListener(window, 'load', function(e) {self.setPosition();return true;}, false);
	bodySizeListener(window, 'resize', function(e) {self.setPosition();return true;}, false);
	if(Spry.is.ie && Spry.is.version == '6'){
		bodySizeListener(window, 'scroll', function(e) {self.setPosition();return true;}, false);
	}
};

Spry.Widget.AlwaysVisible.prototype.setPosition = function(){
	var bodyElement = (Spry.is.safari) ? document.body : document.documentElement;
	var winWidth = (Spry.is.ie)?bodyElement.clientWidth:(bodyElement.scrollWidth-bodyElement.scrollLeft);
	var winHeight = (Spry.is.ie)?bodyElement.clientHeight:window.innerHeight;
	var scrollPosy = (Spry.is.ie && Spry.is.version == '6')?bodyElement.scrollTop:0;
	var scrollPosx = (Spry.is.ie && Spry.is.version == '6')?bodyElement.scrollLeft:0;
	switch(this.hPosition){
		case "left":
		this.avcElement.style.left = scrollPosx+'px';
		break;
		case "right":
		this.avcElement.style.left = scrollPosx+(winWidth-this.avcElement.offsetWidth)+'px';
		break;
		case "center":
		this.avcElement.style.left = scrollPosx+(winWidth/2-this.avcElement.offsetWidth/2)+'px';
		break;
	}
	switch(this.vPosition){
		case "top":
		this.avcElement.style.top = scrollPosy+'px';
		break;
		case "bottom":
		this.avcElement.style.top = scrollPosy+(winHeight-this.avcElement.offsetHeight)+'px';
		break;
		case "middle":
		this.avcElement.style.top = scrollPosy+(winHeight/2-this.avcElement.offsetHeight/2)+'px';
		break;
	}
}

Spry.Widget.AlwaysVisible.addLoadListener = function(handler){
	if (typeof window.addEventListener != 'undefined')
		window.addEventListener('load', handler, false);
	else if (typeof document.addEventListener != 'undefined')
		document.addEventListener('load', handler, false);
	else if (typeof window.attachEvent != 'undefined')
		window.attachEvent('onload', handler);
};

Spry.Widget.AlwaysVisible.prototype.createIframeLayer = function(avc){
	if (typeof this.iframeLayer == 'undefined'){
		var layer = document.createElement('iframe');
		layer.tabIndex = '-1';
		layer.src = 'javascript:"";';
		layer.scrolling = 'no';
		layer.frameBorder = '0';
		layer.className = 'iframeAlwaysVisible';
		avc.parentNode.appendChild(layer);
		this.iframeLayer = layer;
	}
	this.iframeLayer.style.left = avc.offsetLeft + 'px';
	this.iframeLayer.style.top = avc.offsetTop + 'px';
	this.iframeLayer.style.width = avc.offsetWidth + 'px';
	this.iframeLayer.style.height = avc.offsetHeight + 'px';
	this.iframeLayer.style.display = 'block';
};
Spry.Widget.AlwaysVisible.prototype.removeIframeLayer =  function(avc){
	if (this.iframeLayer)
		this.iframeLayer.style.display = 'none';
};


Spry.Widget.AlwaysVisible.prototype.showError = function(msg){
	alert('Spry.Widget.AlwaysVisible ERR: ' + msg);
};

if (!Spry.Widget.Utils)	Spry.Widget.Utils = {};
Spry.Widget.Utils.setOptions = function(obj, optionsObj, ignoreUndefinedProps){
	if (!optionsObj)
		return;
	for (var optionName in optionsObj)
	{
		if (ignoreUndefinedProps && optionsObj[optionName] == undefined)
			continue;
		obj[optionName] = optionsObj[optionName];
	}
};
Spry.Widget.Utils.getElement = function(ele){
	if (ele && typeof ele == "string")
		return document.getElementById(ele);
	return ele;
};
Spry.Widget.Utils.firstValid = function(){
	var ret = null;
	var a = Spry.Widget.Utils.firstValid;
	for(var i=0; i< a.arguments.length; i++)
	{
		if (typeof(a.arguments[i]) != 'undefined')
		{
			ret = a.arguments[i];
			break;
		}
	}
	return ret;
};
Spry.Widget.Utils.addEventListener = function(element, eventType, handler, capture){
	try{
		if (element.addEventListener)
			element.addEventListener(eventType, handler, capture);
		else if (element.attachEvent)
			element.attachEvent("on" + eventType, handler);
	}
	catch (e) {}
};
