﻿/***************************************************************************
 * Copyright (c) 2006 Michael Herndon(http://www.pimpmysite.com)
 *
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 * 1)Overrides and extentions of objects.
 *		**Opacity functionality borrowed from scriptaculous**
 *		  Copyright (c) 2005 Thomas Fuchs (http://script.aculo.us, http://mir.aculo.us)
 * 2)Vivus Javascript Core
 ***************************************************************************/
 
 // String Extensions
 String.isNullOrEmpty = function(value){
	if(value == 'undefined' || value == null || value == "")
		return true;
	return false;
 }
 // Window Extensions
window.moveOnEvent = function(e, el, offsetX, offsetY) {
    el.style.top=(Event.pointerY(e)+offsetY)+"px";
    el.style.left=(Event.pointerX(e)-offsetX)+"px";
}
window.getHeight = function() {
	if(window.innerHeight) return window.innerHeight;
	if(document.documentElement) return document.documentElement.clientHeight; 
	if(document.body) return document.body.clientHeight;
}
window.getWidth = function() {
	if(window.innerWidth) return window.innerWidth;
	if(document.documentElement) return document.documentElement.clientWidth;
	if(document.body) return document.body.clientWidth;
}

document.head = document.getElementsByTagName('head').item(0);

// Element Extensions
Element.Methods.setLocation =	
	function(element, t, l){
		Element.setStyle( $(element), {
			top: t + 'px',
			left: l + 'px'
		});
	}
Element.Methods.center = 
	function(element) {
		var dim = Element.getDimensions(element);
		Position.prepare();
		Element.Methods.setLocation(
			$(element),
			((window.getHeight() - dim.height) /2 + Position.deltaY),
			((window.getWidth() - dim.width) /2 +  Position.deltaX)	
		);
	}

Element.Methods.getOpacity = function(element){  
  var opacity;
  if (opacity = Element.getStyle(element, 'opacity'))  
    return parseFloat(opacity);  
  if (opacity = (Element.getStyle(element, 'filter') || '').match(/alpha\(opacity=(.*)\)/))  
    if(opacity[1]) return parseFloat(opacity[1]) / 100;  
  return 1.0;  
}

Element.Methods.setOpacity = function(element, value){  
  element= $(element);  
  if (value == 1){
    Element.setStyle(element, { opacity: 
      (/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 
      0.999999 : null });
    if(/MSIE/.test(navigator.userAgent))  
      Element.setStyle(element, {filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'')});  
  } else {  
    if(value < 0.00001) value = 0;  
    Element.setStyle(element, {opacity: value});
    if(/MSIE/.test(navigator.userAgent))  
     Element.setStyle(element, 
       { filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'') +
                 'alpha(opacity='+value*100+')' });  
  }
}  
 
Element.Methods.getInlineOpacity = function(element){  
  return $(element).style.opacity || '';
}
// Apply Extentions to Element. 
Object.extend(Element, Element.Methods);
Element.addMethods();


// Event Extensions
Event.getKey = function(e){
	if(!e) e = window.event;
	if(e)
		return ((e.keyCode)? e.keyCode : e.which);
	return null;
}

var Selection = {
	getCurrent: function(){
		if(window.getSelection) return window.getSelection();
		if(document.selection) return document.selection.createRange().text;
		if(document.getSelection) return document.getSelection();
		return '';
	},
	getStart: function(control) {
		if(control.selectionStart || control.selectionStart == "0")
			return control.selectionStart;
		else if(Vivus.Browser.isIE)
			return Math.abs(document.selection.createRange().moveStart("character", -1000000));
		return 0;
	},
	getEnd: function(control) {
		if(control.selectionEnd || control.selectionEnd == "0")
			return control.selectionEnd;
		else if(Vivus.Browser.isIE)
			return Math.abs(document.selection.createRange().moveEnd("character", -1000000));
		return 0
	},
	setCurrent: function (control, start, end) {
		 if (document.selection) { // IE Support
			  control.focus();
			  var sel = document.selection.createRange();	  
			  sel.moveStart ('character', -control.value.length);
			  sel.moveEnd ('character', -control.value.length);
			  if(end != null)
				sel.moveEnd ('character', end);
			  else
				sel.moveEnd ('character', start);
			  sel.moveStart ('character', start);
			  sel.select();
		 } else if(control.selectionStart || control.selectionStart == '0') {
			  control.selectionStart = start;
			  if(end != null)
					control.selectionEnd = end;
			  else
					control.selectionEnd = start;
			  control.focus();
		}
	}
}
 
/*******************************************************************************/ 
// Vivus Core Object 

var vivus = Class.create();
vivus.prototype = {
	debug: true,
	version: "0.1.0.0",
	windowLoaded: true,
	path: 'ScriptLibrary/Vivus/Debug/',
	registry: new Array(),
	initialize: function() {
		this.registry = new Array();
		this.registry["callbacks"] = new Array();
		this.registry["scripts"] = new Array();
		this.registry["scripts"]["unloaded"] =  new Array();
		if(typeof document != 'undefined' && 
			typeof document.getElementsByTagName != 'undefined' && 
			document.body != null) {
				this.onWindowLoaded();	
		}else{
			Event.observe(window, 'load', 
			this.onWindowLoaded.bindAsEventListener(this));
		}
		
	},
	setUsingPath: function(path){
		this.path = path;
	},
	using: function(scriptName){
		this.addReference(this.path + scriptName + '.js');
	},
	addReference: function(scriptFile){
			
		  this.registry["scripts"]["unloaded"].push(scriptFile);
	},
	loadScripts: function(callback){
		this.scriptCallback  = callback;
		this.registry["scripts"]["unloaded"].reverse();
		this.onLoadingScripts();
	},
	onLoadingScripts: function(){
		if(this.registry["scripts"]["unloaded"]["current"]){
			var current = this.registry["scripts"]["unloaded"]["current"];
			if((current.readyState != 'loaded') && (current.readyState != 'complete'))
				return;
			else{
				if(!Vivus.Browser.isIE)
					current.onload = null;
				else
					current.onreadystatechange = null;
				current = null;
			}
		}
		if(this.registry["scripts"]["unloaded"].length){
			var script = document.createElement('script');
			this.registry["scripts"]["unloaded"]["current"] = script;
			if(!Vivus.Browser.isIE){
				script.readyState = 'loaded';
				script.onload = this.onLoadingScripts.bind(this);
			}else
				script.onreadystatechange = this.onLoadingScripts.bind(this);
			script.type = 'text/javascript';
			var x =  this.registry["scripts"]["unloaded"].pop();
			script.src = x;
			document.head.appendChild(script);
			this.registry["scripts"][script.src] = script;
			
			if(this.registry["scripts"]["unloaded"].length == 1 && ! /MSIE/.test(navigator.userAgent))
				this.onLoadingScripts();
			return;
		}
		if(this.scriptCallback){
			this.registry["scripts"]["unloaded"] = new Array();
			
			this.scriptCallback.call();
		}
		this.onInitalizingFinished();
	},
	onInitalizingStarted: function(id, className,  style, content, opacity){
		if(id){
			this.div = $(id);
			if(className)
				this.div.setAttribute("class", className);
			if(style)
				Element.setStyle(this.div, style);
			this.div.center();
			if(content)
				this.div.innerHTML = content;
			if(opacity)
				Element.setOpacity(this.div, opacity);
		}
	},
	onWindowLoaded: function(){
		this.windowLoaded = true;
	},
	onInitalizingFinished: function() {
		if(this.div)
			this.div.hide();
	},
	printData: function(message) {
		if(this.data) 
			this.data.innerHTML = this.data.innerHTML + message + "<br />";
	},
	getSelection: function() {
		if(window.getSelection) return window.getSelection();
		if(document.selection) return document.selection.createRange().text;
		if(document.getSelection) return document.getSelection();
		return '';
	}	
}



/* instaniate objects */
var Vivus = new vivus(); 
Vivus.UI = new Object();

var vivusBrowser = Class.create();
vivusBrowser.prototype = {
	initialize: function(){
		this.isIE = false; 
		this.isMozilla = false; 
		this.isFF = false;
		this.isOpera = false; 
		this.isSafari = false; 
		this.isKDE = false;
		switch(true){
			case /MSIE/.test(navigator.userAgent):
				this.isIE = true; break;
			case /Firefox/.test(navigator.userAgent):
				this.isFF = true; break;
			case /Opera/.test(navigator.userAgent):
				this.isOpera = true; break;
			case /Safari/.test(navigator.userAgent):
				this.isSafari =true; break;
			case /isKDE/.test(navigator.userAgent):
				this.isKDE = true; break;
		}
	}
	
}
Vivus.Browser = new vivusBrowser();



Vivus.UI.Size = Class.create();
Vivus.UI.Size.prototype = {
	isEmpty: (this.width == 0 && this.height == 0),
	initialize: function(width, height){
		this.width = width;
		this.height = height;
	}
}


/* throws the error only on debug  */
function $log(message){
	if(Vivus.debug && Vivus.data)
		Vivus.printData(message);
	else if(Vivus.debug)
		alert(message);
}

function $throw(e) {
	$log("Error:" + e.Message);
}
function NewThumbnailWindow(image, h, w)
		{
			window.open(image, 'fullview', "width=" + w + ",height=" + h + "","" );
		}