/*-------------------------------------------------------------------

	glue - adobe.store revision
	
	$Id: glue.js,v 1.1 2009/12/18 19:20:11 dgasior Exp $
	
	This is old code needed to keep the iframemanager and 
	animation JS working in the new templates.

-------------------------------------------------------------------*/


/*-------------------------------------------------------------------

	PNG Image Support
	
-------------------------------------------------------------------*/

PNG = function (id,src,altsrc) {
	var png = document.createElement('img');
	png.setAttribute('id',id);
	
	if (browser.ua.indexOf('msie 5.0') != -1) {
		// WinIE 5.0 neither supports PNG images nor image opacity filters
		// if an alternative source is provided use this and continue
		if (altsrc != null) png.src = altsrc;
		else return;
	
	} else if ((browser.appN.indexOf('microsoft') != -1) && (browser.ua.indexOf('mac') == -1)) {
		// WinIE 5.5+ support
		png.src = '/images/alpha/blank.gif';
		png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='"+src+"',sizingMethod='scale');";
		
	} else {
		png.src = src;
	}
	
	return png;
}	


/*-------------------------------------------------------------------

	Event Registration
	
-------------------------------------------------------------------*/

function defineEventHandler (element,eventName,handler,capture) {
	try {
		if (document.addEventListener) {
			// W3C Event model
			element.addEventListener(eventName,handler,capture);
			
		} else if (document.attachEvent) {
			// Internet Explorer Event model
			eventName = 'on'+eventName;
			element.attachEvent(eventName,handler);
		}
		
	} catch (ex) {
		return;
	}
}


function removeEventHandler (element,eventName,handler,capture) {
	try {
		if (document.removeEventListener) {
			// W3C Event model
			element.removeEventListener(eventName,handler,capture);
			
		} else if (document.detachEvent) {
			// Internet Explorer Event model
			element.detachEvent('on'+eventName,handler);
		}	

	} catch (ex) {
		return;
	}
}


/*-------------------------------------------------------------------

	Browser Detection
	
-------------------------------------------------------------------*/

BrowserDescription = function () {
	this.appN = navigator.appName.toLowerCase();
	this.appV = parseInt(navigator.appVersion);
	this.ua = navigator.userAgent.toLowerCase();
	this.plt = navigator.platform.toLowerCase();
	//the preferred OS language in IE or the language version of the browser for non IE
	this.lang = (navigator.language || navigator.userLanguage).substring(0,2); 
	// reset appV string incorrect user agent
	if (this.ua.indexOf('opera/7') != -1 || this.ua.indexOf('opera 7') != -1) {
		this.appV = 7;
	}
	this.ax = window.ActiveXObject != "undefined";
	this.isSafari = (this.ua.indexOf('safari') != -1); //should remove this
	
	if(this.ua.indexOf('safari') != -1) 
	{
		var wk = 'applewebkit/';
		var kitpos = this.ua.indexOf(wk);
		if(kitpos == -1) return null;
		var kit = this.ua.substring(kitpos+wk.length);
		kit = kit.substring(0,kit.indexOf(" "));
		this.kitV = parseInt(kit);
	}
	return this;
}

var browser = new BrowserDescription();


/*-------------------------------------------------------------------

	Element Coordinates
	
	Hacking back in JS from old templates to get iframemanager
	JS to work.

-------------------------------------------------------------------*/

Dimensions = function (w,h) { 
	this.width = w || 0;
	this.height = h || 0;
	return this;
}


XYCoords = function (x,y) { 
	this.x = x || 0;
	this.y = y || 0;	
	return this;
}


BoxCoords = function (x1,y1,x2,y2) {
	this.x1 = x1 || 0;
	this.y1 = y1 || 0;
	this.x2 = x2 || 0;
	this.y2 = y2 || 0;
	return this;
}


function getEventCoords (e) {
	var coords = new XYCoords();
	
	if (e.pageX && e.pageY) {
		// W3C model
		coords.x = e.pageX;
		coords.y = e.pageY;
		
	} else if (e.clientX && e.clientY) {
		// Internet Explorer model
		coords.x = e.clientX + document.body.scrollLeft;
		coords.y = e.clientY + document.body.scrollTop;
	}
	
	return coords;
}

/*this function always calculates from the offsetParent 
	IE 6 standards mode calculates values for offsetTop and offsetLeft values 
	from the BODY element NOT from the offsetParent */
	
function getElementBoxCoords(domElement) {
	this.element = domElement;
	this.calculated_offset;
	
	this.calcOffsetFrom = function (element,from,reset) {
		if (reset != false) this.calculated_offset = 0;
		
		if (element != null)	{
			switch (from) {
				case 'top': 
					this.calculated_offset += element.offsetTop;
					break;
				case 'left': 
					this.calculated_offset += element.offsetLeft;
					break;
			}
			
			if ((element.offsetParent == document.body) || (element.offsetParent.tagName == ('HTML'||'BODY'))) {				
				return this.calculated_offset;
			} else {
				return this.calcOffsetFrom(element.offsetParent,from,false);
			}
		}
	}
	
	/*IE6 compatible mode offsetWidth and offsetHeight values include border
		IE6 standards mode, Netscape, MacIE these values includes padding + border 
		Safari, Opera6, Opera7, Mozilla ...? */
	this.w = this.element.offsetWidth;
	this.h = this.element.offsetHeight;

	var coords = new BoxCoords();
	
	coords.x1 = this.calcOffsetFrom(this.element,'left',true);
	coords.y1 = this.calcOffsetFrom(this.element,'top',true);
	coords.x2 = coords.x1 + this.w;
	coords.y2 = coords.y1 + this.h;
	
	return coords;
}

// extension to getElementBoxCoords function 
// accepts element id instead of element object
function getElementBoxCoordsById(elementID) {
	var element = document.getElementById(elementID);
	if (element != null) {
		return getElementBoxCoords(element);
	}
}

function getWindowDimensions () {
	var width = (window.innerWidth) ? window.innerWidth : document.body.clientWidth;
	var height = (window.innerHeight) ? window.innerHeight : document.body.clientHeight;
	return new Dimensions(width,height);
}

function getContentDimensions () {
	// take the greater of scroll and offset dimension
	var width = Math.max(document.body.offsetWidth,document.body.scrollWidth);
	var height = Math.max(document.body.offsetHeight,document.body.scrollHeight);
	return new Dimensions(width,height);
}

function getScrollPosition () {
	var scrollPosition = new XYCoords();
	
	if (window.scrollX && window.scrollY) {
		// Netscape implementation
		scrollPosition.x = window.scrollX;
		scrollPosition.y = window.scrollY;
	
	} else {
		// Internet Explorer		
		var docBody = document.body;
		
		var parent_scrollLeft = (docBody.parentNode.scrollLeft) ? docBody.parentNode.scrollLeft : 0;
		var parent_scrollTop = (docBody.parentNode.scrollTop) ? docBody.parentNode.scrollTop : 0;
		
		scrollPosition.x = Math.max(docBody.scrollLeft,parent_scrollLeft);
		scrollPosition.y = Math.max(docBody.scrollTop,parent_scrollTop);
	}
	
	return scrollPosition;
}
