﻿/*  Prototype Wretch Javascript Framework, based on prototype.js and scriptaculous.js - http://www.wretch.cc/
  
	- 2007.02.05
	
	Utility Methods
	- $B(); // equivalent: Element.build();
	
	Element Methods
	- Element.setOpacity(element,value);
	- Element.elementInfo(element);
	- Element.appendTo(element,parentElement);
	- Element.appendToBody(element);
	- Element.within(element,wrapElement);
	- Element.getElementsByHTML(element,html[,index]);
	- Element.build(tagName[,{[content:object or text[, style:{styles}]]element attributes ..}])

	Position
	- Position.absolutePosition(element);
	- Position.getPageScroll();
	- Position.getPageSize();

	Object
	- Object.isDefined(object);
	- Object.isUndefined(object);
	
	Event
	- Event.keyDown(event);
----------------------------------------------------------------- */

var extendedElementMethods={
	setOpacity:function(element, value)
	{
	// setOpacity()
	// Core code from - scriptaculous.js
		element= $(element);  
		if (value == 1){
			element.setStyle({ opacity: 
				(/Gecko/.test(navigator.userAgent) && !/Konqueror|Safari|KHTML/.test(navigator.userAgent)) ? 
				0.999999 : 1.0 });
			if(/MSIE/.test(navigator.userAgent) && !window.opera)  
				element.setStyle({filter: Element.getStyle(element,'filter').replace(/alpha\([^\)]*\)/gi,'')});  
		} else {  
			if(value < 0.00001) value = 0;  
			element.setStyle({opacity: value});
			if(/MSIE/.test(navigator.userAgent) && !window.opera)  
				element.setStyle(
					{ filter: element.getStyle('filter').replace(/alpha\([^\)]*\)/gi,'') +
							'alpha(opacity='+value*100+')' });  
		}
		return element;
	},
	elementInfo:function(element)
	{
		element=$(element);
		var para={};
		para.tagName=element.tagName.toLowerCase();
		para.id=element.getAttribute('id');
		para.className=Element.classNames(element).toString();
		if(!para.id)para.id='';
		if(!para.className)para.className='';
		return para;
	},
	appendTo:function(element,parentElement)
	{
		$(parentElement).appendChild($(element));
		return element;
	},
	appendToBody:function(element)
	{
		return Element.appendTo(element,document.getElementsByTagName('body')[0]);
	},
	within:function(element,wrapElement)
	{
		wrapElement=$(wrapElement);
		var pos=Position.absolutePosition(element);
		return Position.within(wrapElement,pos.left,pos.top) ||
		Position.within(wrapElement,pos.left+pos.width,pos.top) ||
		Position.within(wrapElement,pos.left+pos.width,pos.top+pos.height) ||
		Position.within(wrapElement,pos.left,pos.top+pos.height);
	},
	getElementsByHTML:function(element,html,index)
	{
		return document.getElementsByHTML(html,index,element);
	}
};
Object.extend(Element,extendedElementMethods);
Element.addMethods(extendedElementMethods);

document.getElementsByHTML=function(html,index,parentElement)
{
	var m=new RegExp("<([^ ]*) *([^=]*)=\"?([^ /]*)\" */?>").exec(html);
	var tags=($(parentElement)||document.body).getElementsByTagName(m[1]);
	if(tags.length==0)return null;
	if(m[2]=="class")m[2]=document.all&&!window.opera?"className":"class";
	for(var i=0,elements=[];i<tags.length;i++){
		match=!(m[2]=="class"||m[2]=="className")?false:Element.hasClassName(tags[i],m[3])?true:false;
		if($(tags[i]).readAttribute(m[2])==m[3]||match){
			if(elements.length==index)return Element.extend(tags[i]);
			elements.push(Element.extend(tags[i]));}}
	if(elements.length==0)return null;
	if(Object.isUndefined(index))return elements;
	if(elements.length<=Math.abs(index))index=(index<0)?0:(elements.length-1);
	else if(index<0)index=elements.length+index;
	return elements[index];
};

var $B=Element.build=function(tagName,options)
{
	options=Object.extend({},options);
	if(document.all&&!window.opera&&options.ie)return document.createElement(options.ie);
	else{
		if(!options.style)options.style={};
		var newObj=document.createElement(tagName);
		for(key in options){
			if(options[key]){
				if(key=='content'){
					if(typeof options[key]=='string'||!options[key].length)options[key]=[options[key]];
					for(var i=0;i<options[key].length;i++){
						if(typeof options[key][i]=='string')options[key][i]=document.createTextNode(options[key][i]);
						newObj.appendChild(options[key][i]);}}
				else if(key=='style')
					Element.setStyle(newObj,options[key]);
				else if(key=='className')
					Element.addClassName(newObj,options[key]);
				else if(key=='htmlFor'){
					if(document.all&&!window.opera)newObj.setAttribute('htmlFor',options[key]);
					else newObj.setAttribute('for',options[key]);}
				else if(key=='ie')continue;
				else newObj.setAttribute(key,options[key]);}}
		return Element.extend(newObj);}
}

Object.extend(Object,{
	isDefined:function(object) {
    return typeof(object)!="undefined"&&object!=null;
  },
	isUndefined:function(object) {
    return !Object.isDefined(object);
  }
});

// Positin extend
Object.extend(Position,{
  absolutePosition:function(element)
	{
    element=$(element);
		Position.prepare();
		var cumulative=Position.cumulativeOffset(element);
		var scroll=element.up()?Position.realOffset(element.up()):Position.realOffset(element);
		return Object.extend(Object.extend({},element.getDimensions()),{left:cumulative[0]-scroll[0]+Position.deltaX,top: cumulative[1]-scroll[1]+Position.deltaY});
	},
	getPageScroll:function()
	{
		// getPageScroll()
		// Returns array with x,y page scroll values.
		// Core code from - quirksmode.org
		var yScroll,xScroll;
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
			xScroll = self.pageXOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
			xScroll = document.documentElement.scrollLeft;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
			xScroll = document.body.scrollLeft;
		}
		var para=
		{
			x: xScroll,
			y: yScroll
		}
		return para;
	},
	getPageSize:function()
	{
		// getPageSize()
		// Returns array with page width, height and window width, height
		// Core code from - quirksmode.org
		// Edit for Firefox by pHaez
		var xScroll, yScroll;
		
		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;
		}
		arrayPageSize = {
			width:        pageWidth,
			height:       pageHeight,
			windowWidth:  windowWidth,
			windowHeight: windowHeight
		};
		return arrayPageSize;
	}
});

// Event extend
Object.extend(Event,{
	keyDown:function(event)
	{
		return event.charCode||event.keyCode||-1;
	}
});
