/*
	The Deepend Website

	Copyright (C) 2006 Michael Diolosa (michael@deepend.com),
	                   Deepend New York, Inc.,
                           490 Broadway Floor 5,
                           New York, NY, 10012 

	This program is free software; you can redistribute it and/or modify
	it under the terms of the GNU General Public License as published by
	the Free Software Foundation; either version 2 of the License, or
	(at your option) any later version.
	
	This program is distributed in the hope that it will be useful,
	but WITHOUT ANY WARRANTY; without even the implied warranty of
	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
	GNU General Public License for more details.

	You should have received a copy of the GNU General Public License
	along with this program; if not, write to the Free Software
	Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
*/

var agt = navigator.userAgent.toLowerCase();
var is_ie_win = (((agt.indexOf("msie") != -1) && (agt.indexOf("opera") == -1)) && ((agt.indexOf("win") !=-1) || (agt.indexOf("16bit") != -1)));

window.getHeight = function ()
{
	if (window.innerHeight != null)
		return window.innerHeight;
	else
		return document.body.clientHeight;
}

window.getWidth = function ()
{
	if (window.innerWidth != null)
		return window.innerWidth;
	else
		return document.body.clientWidth;
}

/* CLASS EventManager */
function EventManager () {}

EventManager.addEventListener = function (obj, eventType, eventHandler, capture) {
        if (obj.addEventListener) {
                obj.addEventListener (eventType, eventHandler, capture);
        } else if (obj.attachEvent) {
                obj.attachEvent ("on" + eventType, eventHandler);
        } else {
                eval ("obj.on" + eventType + " = " + eventHandler);
        }
}

EventManager.removeEventListener = function (obj, eventType, eventHandler, capture) {
        if (obj.removeEventListener) {
                obj.removeEventListener (eventType, eventHandler, capture);
        } else if (obj.detachEvent) {
                obj.detachEvent ("on" + eventType, eventHandler);
        } else {
                eval ("obj.on" + eventType + " = null");
        }
}

/* CLASS ImageManager */
function ImageManager () {}

ImageManager._images = new Array ();

ImageManager.registerImage = function (name, source)
{
        if (name == null || name == "")
                throw "You must specify an image name.";
                
        if (source == null || source == "")
                throw "You must specify an image source.";
        
        ImageManager._images [name] = new Image ();
        ImageManager._images [name].src = source;
}
		
ImageManager.changeAlphaImageSource = function (imageObject, destName)
{
	if (is_ie_win && !ImageManager.isIESeven)
		ImageManager.changeFilterImageSource (imageObject, destName);
	else
		ImageManager.changeImageSource (imageObject, destName);
}
	
ImageManager.changeImageSource = function (imageObject, destName)
{
        if (imageObject == null || imageObject == "")
                throw "You must specify an image object.";
                
        if (destName == null || destName == "")
                throw "You must specify a destination image source.";
                
        imageObject.src = ImageManager._images [destName].src;
}

ImageManager.changeFilterImageSource = function (imageObject, destName)
{
        if (imageObject == null || imageObject == "")
                throw "You must specify an image id.";
                
        if (destName == null || destName == "")
                throw "You must specify a destination image source.";
        
        imageObject.filters [0].src = ImageManager._images [destName].src;
}

/* CLASS OpacityManager */
function OpacityManager () {}

OpacityManager.setOpacity = function (ele, opacity)
{
	if (typeof ele.filters != "undefined" && ele.filters != null) {
		ele.filters ["alpha"].opacity = opacity * 100;
	} else {
		if (typeof ele.style.opacity != "undefined")
			ele.style.opacity = opacity;
		
		if (typeof ele.style.MozOpacity != "undefined")
			ele.style.MozOpacity = opacity;
	}
}

/* CLASS CenterStage */
function CenterStage (bgName, clientAreaName, windowName, imgWidth, imgHeight, winWidth, winHeight)
{
	this._imageWidth = imgWidth;
	this._imageHeight = imgHeight;
	this._windowWidth = winWidth;
	this._windowHeight = winHeight;
	this._bgPhotoDiv = document.getElementById(bgName);
	this._bgPhoto = this._bgPhotoDiv.getElementsByTagName ("img") [0];
	this._stretcher = document.getElementById(clientAreaName);
	this._win = document.getElementById(windowName);
	
	EventManager.addEventListener(window, "resize", new Function("if (CenterStage.stage != null) CenterStage.stage.scaleImage ()"), false);
}

CenterStage.prototype.scaleImage = function ()
{
	var iHeight = window.getHeight();
	var iWidth = window.getWidth();
	
	var newHeight = 0;
	var newWidth = 0;
	
	var differenceHeight = iHeight / this._imageHeight;
	var differenceWidth = iWidth / this._imageWidth;
	
	if (differenceWidth < differenceHeight) {
		newHeight = iHeight;
		newWidth = (iHeight * this._imageWidth) / this._imageHeight;
	} else {
		newHeight = (iWidth * this._imageHeight) / this._imageWidth;
		newWidth = iWidth;
	}
	
	this._bgPhoto.style.height = Math.ceil (newHeight) + "px";
	this._bgPhoto.style.width = Math.ceil (newWidth) + "px";

	this._bgPhotoDiv.style.height = Math.ceil (iHeight) + "px";
	this._bgPhotoDiv.style.width = Math.ceil (iWidth) + "px";
	this._stretcher.style.height = Math.ceil (iHeight) + "px";
	this._stretcher.style.width = Math.ceil (iWidth) + "px";
	
	var newLeft = Math.ceil ((iWidth - this._windowWidth) / 2);
	var newTop = Math.ceil ((iHeight - this._windowHeight) / 2);
	
	if (newLeft < 0)
		newLeft = 0;
		
	if (newTop < 0)
		newTop = 0;
		
	this._win.style.left = newLeft + "px";
	this._win.style.top = newTop + "px";
}

CenterStage.printFlash = function(url, width, height)
{
    document.write("<object type=\"application/x-shockwave-flash\" data=\"" + url + "\" width=\"" + width + "\" height=\"" + height + "\">");
    document.write("<param name=\"movie\" value=\"" + url + "\" />");
    document.write("<param name=\"quality\" value=\"high\" />");
    document.write("<param name=\"menu\" value=\"false\" />");
    document.write("<param name=\"scale\" value=\"noscale\" />");
    document.write("<param name=\"bgcolor\" value=\"#666\" />");
    document.write("</object>");
}