/*
 XOO - XANIDO OOP JS
 Timothy Anido 2006/2007
 
 Inspired by (and may contain traces of) prototype.js and mootools.
 Provides useful shortcuts and powerful extensions to javascript
 
 VERSION   0.2.5

*/

var Class = function(proto){
	var klass = function(){
		return (arguments[0] !== null && this.initialize && typeof this.initialize == 'function')
		? this.initialize.apply(this, arguments) : this;
	};
	$extend(klass, this);
	klass.prototype = proto;
	klass.constructor = Class;
	return klass;
};

Class.prototype = {
	extend: function(properties){
		var proto = new this(null);
		for (var property in properties){
			var pp = proto[property];
			proto[property] = Class.Merge(pp, properties[property]);
		}
		return new Class(proto);
	}
};
Class.Merge = function(previous, current){
	if (previous && previous != current){
		var type = typeof(current);
		if (type != typeof(previous)) return current;
		switch(type){
			case 'function':
				var merged = function(){
					this.parent = arguments.callee.parent;
					return current.apply(this, arguments);
				};
				merged.parent = previous;
				return merged;
			case 'object': return $merge(previous, current);
		}
	}
	return current;
};

var $ = function( el ) {
	if (!el) return null;
	if (el.htmlElement) return (el);
	var type = typeof el;
	if (type == 'string'){
		el = document.getElementById(el);
		type = (el) ? 'element' : false;
	}
	if( type == 'object' && obj.nodeType == 1 ) type = 'element';
	if (type != 'element') return null;
	if (el.htmlElement) return( el );
	$extend(el, ElementExtension.prototype);
	el.htmlElement = function(){};
	return( el );
};

var $time = function( ) { return new Date( ).getTime( ) };
var $extend = function( ) {
	var args = arguments;
	if (!args[1]) args = [this, args[0]];
	for (var property in args[1]) args[0][property] = args[1][property];
	return args[0];
};
var $implement = function( ) {
	var args = arguments;
	if (!args[1]) args = [this, args[0]];
	for (var property in args[1]) args[0].prototype[property] = args[1][property];
	return args[0];
};
var $merge = function( ) {
	var args = arguments;
	for( var key in args[1] ) args[0][key] = args[1][key];
	return args[0];
};
var $empty = function( v ) {
	return ( v == '' ) ? true : false;
}
var $hi = function( ) {
	return (arguments[0] > arguments[1]) ? arguments[0] : arguments[1];
}
var $lo = function( ) {
	return (arguments[0] < arguments[1]) ? arguments[0] : arguments[1];
}

var __ext = function( ) {
	for(var i=1; i<arguments.length; i++){
		arguments[i].extend = (arguments[0]=='implement') ? $implement : $extend;
	}
};
__ext( 'implement',Array,Function,String,Number );
__ext( 'extend',window );


var ElementExtension = new Class( {
	getPosition: function( ) {
		obj = this;
		var X=0,Y=0;
		while(obj.offsetParent!=null) {
			X += obj.offsetParent.offsetLeft;
			Y += obj.offsetParent.offsetTop;
			obj = obj.offsetParent;
		}
		return{ 'X': X + this.offsetLeft, 'Y': Y+this.offsetTop };
	},
	wrapIt: function( ) {
		if( this.wrapper ) return this.wrapper;
		this.wrapper = document.createElement( 'div' );
		this.parentNode.insertBefore( this.wrapper, this );
		this.wrapper.appendChild( this );
		this.wrapper.style.overflow = "hidden";
		this.wrapper.margin = this.margin;
		this.margin = "0";
		this.wrapper.padding = "0";
	},
	slide: function( props ) {
		//var id = this.getAttribute('id');
		return new Tween( $merge(props, { 'element': this }) ).start( );
	},
	animate: function( props ) {
		return this.slide( props );
	},
	show: function( ) { this.style.display = 'block'; },
	hide: function( ) {	this.style.display = 'none'; }
});


function $class(className, tag, elm){
	var testClass = new RegExp("(^|\\\\s)" + className + "(\\\\s|$)");
	var tag = tag || "*";
	var elm = elm || document;
	var elements = (tag == "*" && elm.all)? elm.all : elm.getElementsByTagName(tag);
	var returnElements = [];
	var current;
	var length = elements.length;
	for(var i=0; i<length; i++){
		current = elements[i];
		if(testClass.test(current.className)){
			returnElements.push(current);
		}
	}
	return returnElements;
}

var classHide = function( className )
{
	
}

var XooUI = new Class({

	options: {
		'title': 'Are You Positive?',
		'cancelLabel': 'Cancel',
		'continueLabel': 'Delete'
	},

	initialize: function( )	{
		this.overlay = document.createElement( 'div');
		this.overlay.setAttribute('id', 'overlay');
		document.getElementsByTagName('body').item(0).insertBefore( this.overlay, document.getElementsByTagName('body').item(0).firstChild );
	},
	
	dialog: function( options ) {
		this.options = $merge( this.options, options );
		$class('xooui_title')[0].innerHTML = this.options.title;
		$class('xooui_msg')[0].innerHTML = this.options.msg;
		this.showOverlay( );
		$('dialog').show( );
		$('dialog').style.top = "-80px";
		$('dialog').style.left = window.dimensions.windowWidth/2 - $('dialog').offsetWidth/2 + 'px';
		$('dialog').animate( { 'property': 'top', 'start': 0, 'end': window.dimensions.windowHeight/2 - 1.5*$('dialog').offsetHeight } );
		$('xooui_cancel').onclick = this.hideDialog.bind(XooUI);
		$('xooui_continue').onclick = this.continueDialog.bind(XooUI);
	},
	
	hideDialog: function( ) {
		this.hideOverlay( );
		$('dialog').hide( );
	},
	
	continueDialog: function( ) {
		document.location.href="processor.dll?form=end_job&job_id="+this.options.id;
	},
	
	hideOverlay: function( ) {
		this.overlay.style.display = "none";
	},
	
	showOverlay: function( ) {
		dims = window.getPageDimensions( );
		this.overlay.style.height = dims.pageHeight +"px";
		this.overlay.style.display = "block";
	}
});

var Tween = new Class({
	
	props: new Array(),
	clock: 0,
	
	initialize: function( props ){
		this.props = $merge({
			'duration': 1,
			'fps': 40,
			'onComplete': function(){},
			'onFrame': function(){},
			'onStart': function(){},
			'onStop': function(){},
			'element': '',
			'property': '',
			'start': '',
			'end': 200,
			'transition': easing.easeOut,
			'unit': 'px'
		}, props);
		
		this.interval = Math.round(1000/this.props.fps);
		if( browser.IE ) this.props.duration *= .75;
		this.el = $( this.props.element );
		this.val = this.props.start;
	},
	
	start: function( ) {
		this.animating = true;
		this.time = this.frame.repeat(this.interval,this);
		this.fireEvent('onStart');
		return this;
	},
	stop: function( ) {
		this.animating = false;
		clearInterval( this.time );
	},
	pause: function( ) {
		if( this.animating ) {
			this.animating = false;
			clearInterval( this.time );
		} else {
			this.animating = true;
			this.time = this.frame.repeat(this.props.interval,this);
		}
	},
	tick: function( ) {
		
	},
	frame: function( ) {
		this.clock++;
		this.val = this.props.transition( this.clock * this.interval / (this.props.duration*1000) );
		this.el.style[this.props.property] = ((this.props.end - this.props.start) * this.val + this.props.start )+ this.props.unit;
		this.fireEvent('onFrame');
		if( this.clock * this.interval >= this.props.duration*1000 ){
			this.complete( );
		}
	},
	fforward: function( ) {

	},
	rewind: function( ) {
		var flip = this.props.start;
		this.props.start = this.props.end;
		this.props.end = flip;
		this.clock = 0;
		this.start();
	},
	complete: function( ) {
		clearInterval( this.time );
		this.animating = false;
		this.fireEvent('onComplete','onStop');
	},
	
	fireEvent: function( ) {
		for( var j=0; j<arguments.length; j++ ) {
			handlers = ( typeof this.props[arguments[j]] != 'object' ) ? [this.props[arguments[j]]] : this.props[arguments[j]];
			for( var i=0; i<handlers.length; i++ ) {
				handlers[i].call(this);
			}
		}
	},
	addHandler: function( evt, handler ) {
		if( typeof this.props[evt] != 'object' ) [this.props[evt]];
		this.props[evt].push( handler );
	},
	clearHandler: function( evt ) {
		this.props[evt] = '';
	}
});

var ColorTween = Tween.extend({
	rgb: ['r','g','b'],
	initialize: function( props ) {
		this.parent( props );
		this.props.property = this.props.property || 'backgroundColor';
		this.props.start = new Color( this.props.start );
		this.props.end = new Color( this.props.end );
	},
	start: function( ) {
		this.animating = true;
		this.val = new Color( this.props.start.base );
		this.time = this.frame.repeat(this.interval,this);
		this.fireEvent('onStart');
		return this;
	},
	frame: function( ) {
		this.clock++;
		for( c in this.rgb ){
			var delta = this.props.transition( this.clock * this.interval / (this.props.duration*1000) );
			this.val[this.rgb[c]]( (this.props.end.components()[this.rgb[c]] - this.props.start.components()[this.rgb[c]] ) * delta + this.props.start.components()[this.rgb[c]] );
		}
		this.el.style[this.props.property] = this.val.html;
		this.fireEvent('onFrame');
		if( this.clock * this.interval == this.props.duration*1000 ){
			this.complete( );
		}
	}
});

var Color = Class({
	initialize: function( hex ){
		this.base = hex;
		this.html = '#' + hex;
		hex = '0x' + hex;
		this.r( hex >> 16 );
		this.g( hex >> 8 & 0xFF );
		this.b( hex & 0xFF );
	},
	r: function( r ){
		if( r ) this.rc = r;
		this.recompile();
		return this.rc;
	},
	g: function( g ){
		if( g ) this.gc = g;
		this.recompile();
		return this.gc;
	},
	b: function( b ){
		if( b ) this.bc = b;
		this.recompile();
		return this.bc;
	},
	recompile: function(){
		this.base = this.rc << 16 | this.gc << 8 | this.bc;
		this.hex = '0x' + this.base.toString(16);
		this.html = '#' + this.base.toString(16);
		return this.base;
	},
	components: function() {
		return {'r':this.rc, 'g':this.gc, 'b':this.bc}
	}
});


Function.extend({
	repeat: function( interval, bind, args ){
		var fn = this;
		var f = function( ){
			return fn.apply( bind || fn, args || [] );
		}
		return setInterval( f, interval );
	},
	bind: function(object) {
		var m = this;
		return function() {
			return m.apply(object, arguments);
		}
	}	
});

Array.extend({
	each: function( func, arg ){
		for( i=0; i<this.length;i++ ) {
			func( this[i] );
		}
	}
});

String.extend({
	alert: function( ) {
		alert(this);
	},
	obfuscate: function( ) {
		var s = '';
		for(var i=0; i<this.length;i++)
		{
			c = this.charCodeAt(i);
			if( Math.random( ) < 0.5 )
				c = 'x'+ c.toString(16);
			s += '&#'+ c + ';';
		}
		return( s )
	},
	reverse: function( ) {
		var r = '';
		for( letter in this ){
			if(parseInt(letter)==letter) r = this.charAt(letter) + r;
		}
		return( r );
	},
	write: function( ) {
		document.write( this );
	}
});


window.extend({
	addEvent: function( event, handler ) {
		if( event == 'domready'){
			if ( browser.FF || browser.NS || browser.OP ) {
				document.addEventListener("DOMContentLoaded", handler, false);
			}
			if ( browser.IE ) {	
				document.write("<scr" + "ipt id=__ieinit defer=true " + "src=//:><\/script>");
				var script = document.getElementById("__ieinit");
				script.onreadystatechange = function() {
					if ( this.readyState != "complete" ) return;
					this.parentNode.removeChild( this );
					handler();
				};
			}	
		}
		if (window.addEventListener) {
		  window.addEventListener(event, handler, false);
		} else if (window.attachEvent) {
		  window.attachEvent('on' + event, handler);
		}
	},
	
	getPageDimensions: function( ) {
		var xScroll, yScroll;
		var dimensions;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}

		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
		
		this.dimensions = { 'pageWidth': pageWidth, 'pageHeight': pageHeight, 'windowWidth': windowWidth, 'windowHeight': windowHeight } ;
		return this.dimensions;
		
	},
	getPageScroll: function( ) {

		var yScroll;

		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}

		return { 'x': 0, 'y': yScroll };
	}
});

var Element = new Class({

	el: null,
	
	initialize: function( type, attributes ) {
		el = document.createElement( type );
		if( attributes )
		{
			for( attribute in attributes )
			{
				el.setAttribute( attribute, attributes[attribute] );
			}
		}
		return el;
	}
	
});

var Easing = new Class({
	easeIn: function( p )
	{
		return Math.pow( p, 3 );
	},
	easeOut: function ( p ) {
		return 1 - Math.pow( 1-p, 3 );
	}
});
easing = new Easing( );


var Browser = new Class({
	initialize: function( ) {
		this.IE = ( document.all && document.getElementById && !window.opera );
		this.FF = (!document.all && document.getElementById && !window.opera );
		this.OP = ( document.all && document.getElementById && window.opera  );
		this.NS = ( navigator.userAgent.indexOf('Netscape6/') != -1 ) || false;
	}
});
browser = new Browser( );

window.addEvent( 'domready', function( ) {
	window.getPageDimensions( );
});
window.addEvent( 'resize',  function( ) {
	window.getPageDimensions( );
});


// COOKIES
function createCookie(name,value,days) {
	if (days) {
		var date = new Date();
		date.setTime(date.getTime()+(days*24*60*60*1000));
		var expires = "; expires="+date.toGMTString();
	}
	else var expires = "";
	document.cookie = name+"="+value+expires+"; path=/";
}
function readCookie(name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}
function eraseCookie(name) {
	createCookie(name,"",-1);
}
