/*
Webshark Javascript Extensions
	CORE METHODS
		Version: 1.0

		History: 
		1.0 @ G-Lex
			- Első verzió
*/

function LoadJS( jsFile ) {

    head = document.getElementsByTagName('head').item(0);
    script=document.createElement('script');
    script.src= jsFile;
    script.type='text/javascript';
    script.defer=true;
    void(head.appendChild(script));
}

var Class = {
  create: function() {
    return function() {
      this.initialize.apply(this, arguments);
    }
  }
}

var $break    = new Object();
var $continue = new Object();

Function.prototype.bind = function() {
  var __method = this, args = $A(arguments), object = args.shift();
  return function() {
    return __method.apply(object, args.concat($A(arguments)));
  }
}

var $A = Array.from = function(iterable) {
  if (!iterable) return [];
  if (iterable.toArray) {
    return iterable.toArray();
  } else {
    var results = [];
    for (var i = 0, length = iterable.length; i < length; i++)
      results.push(iterable[i]);
    return results;
  }
}

var Try = {
  these: function() {
    var returnValue;

    for (var i = 0, length = arguments.length; i < length; i++) {
      var lambda = arguments[i];
      try {
        returnValue = lambda();
        break;
      } catch (e) {}
    }

    return returnValue;
  }
}

Object.extend = function(destination, source) {
  for (var property in source) {
    destination[property] = source[property];
  }
  return destination;
}

function getObj( name ) {
	if ( typeof name == 'string' ) {
	    return document.getElementById( name );    
	} else {
	    return name;
	}
}

//Összes objektum
Object.extend(Object, {
  keys: function(object) {
    var keys = [];
    for (var property in object)
      keys.push(property);
    return keys;
  },

  clone: function(object) {
    return Object.extend({}, object);
  }
});

var wsElements = new Object();

wsElements = {


	//Méretek lekérdezése
	getDimensions: function( element ) {
		var obj = getObj(element);

		var els = obj.style;
		var originalDisplay = els.display;
		
		els.display = 'block';
		
		var originalWidth = obj.offsetWidth;
		var originalHeight = obj.offsetHeight;

		var borderW = wsStyles.getStyle( element, 'border-left-width' ).toInt() + wsStyles.getStyle( element, 'border-right-width' ).toInt();
		var borderH = wsStyles.getStyle( element, 'border-top-width' ).toInt() + wsStyles.getStyle( element, 'border-bottom-width' ).toInt();

		var marginW = wsStyles.getStyle( element, 'margin-left' ).toInt() + wsStyles.getStyle( element, 'margin-right' ).toInt();
		var marginH = wsStyles.getStyle( element, 'margin-top' ).toInt() + wsStyles.getStyle( element, 'margin-bottom' ).toInt();

		var fullWidth = obj.clientWidth + borderW + marginW;
		var fullHeight = obj.clientHeight + borderH + marginH;

		var iWidth = originalWidth - borderW;
		var iHeight = originalHeight - borderH;

		els.display = originalDisplay;
		
		return {width: iWidth, height: iHeight, fullWidth: fullWidth, fullHeight: fullHeight};
	},

	//Pontos absolút pozíció
	getPos: function( name ) {

		obj = getObj( name );
		var curleft = curtop = 0;
		curleft = obj.offsetLeft + wsStyles.getStyle( obj, 'margin-left').toInt();
		curtop	= obj.offsetTop + wsStyles.getStyle( obj, 'margin-top' ).toInt();

		if ( obj.offsetParent ) {
			while (obj = obj.offsetParent) {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			}
		}

		return {left:curleft, top:curtop};
	},

	//Abszolút pozícionálás
	absolutize: function(element) {
		var obj = getObj(element);

		if (obj.style.position == 'absolute') return;

		var offsets		= this.getPos(element);
		var dimensions	= this.getDimensions(element);		

		var top     = offsets['top'];
		var left    = offsets['left'];
		var width   = dimensions.width;
		var height  = dimensions.height;

		obj._originalLeft   = left - parseFloat(obj.style.left  || 0);
		obj._originalTop    = top  - parseFloat(obj.style.top || 0);
		obj._originalWidth  = obj.style.width;
		obj._originalHeight = obj.style.height;

		obj.style.position	= 'absolute';
		obj.style.top		= top + 'px';
		obj.style.left		= left + 'px';
		obj.style.width		= width + 'px';
		obj.style.height	= height + 'px';
		obj.style.margin	= '0';
		obj.style.padding	= '0';

	},

	alignTo: function( element, toElement, options ) {
	  var defaultOptions = { left: true, top: true, width: true, height: true }
	  Object.extend( defaultOptions, options );

	  wsElements.absolutize( element );

	  var pos = wsElements.getPos( toElement );
	  var dimensions = wsElements.getDimensions( toElement );


	  if ( defaultOptions.width )
		  wsStyles.setStyle( element, 'width', dimensions.width+'px' );			
	  if ( defaultOptions.height )
		  wsStyles.setStyle( element, 'height', dimensions.height+'px' );
	  if ( defaultOptions.top )
		 wsStyles.setStyle( element, 'top', pos.top+'px' );
	  if ( defaultOptions.left )
			wsStyles.setStyle( element, 'left', pos.left+'px' );

	},


	getElementsByClass: function (searchClass, tag, node) {      
	   var returnArray = [];
	   if ( typeof node == 'undefined' ) { node = document; }
	   if ( tag === null )  { tag = '*'; }
	   var els = node.getElementsByTagName(tag);
	   var pattern = new RegExp('(^|\\s)'+searchClass+'(\\s|$)');
	   for (var i = 0; i < els.length; i++) {
		  if ( pattern.test(els[i].className) ) {
			 returnArray.push(els[i]);
		  }
	   }
	   return returnArray;
	}

}

Object.extend(Array.prototype, {
  extended: true,

  _each: function(iterator) {
    for (var i = 0, length = this.length; i < length; i++)
      iterator(this[i]);
  },


  each: function(iterator) {
    var index = 0;
    try {
      this._each(function(value) {
        try {
          iterator(value, index++);
        } catch (e) {
          if (e != $continue) throw e;
        }
      });
    } catch (e) {
      if (e != $break) throw e;
    }
    return this;
  }


});


Object.extend(String.prototype, {

  camelize: function() {
    var parts = this.split('-'), len = parts.length;
    if (len == 1) return parts[0];

    var camelized = this.charAt(0) == '-'
      ? parts[0].charAt(0).toUpperCase() + parts[0].substring(1)
      : parts[0];

    for (var i = 1; i < len; i++)
      camelized += parts[i].charAt(0).toUpperCase() + parts[i].substring(1);

    return camelized;
  },

  toArray: function( splitter ) {
    return this.split(splitter);
  },

  toInt: function() {
      if ( isNaN(parseInt( this )) ) {
          return 0;
      } else {
          return parseInt( this );
      }
  }
});

var wsStyles = new Object();

wsStyles = {
	
	quadraticItems: new Array( 'top', 'right', 'bottom', 'left' ),

	_isQuadratic: function( name ) {
		
		var isLeft		= name.search( '-left' ) !== -1;
		var isRight		= name.search( '-right' ) !== -1;
		var isTop		= name.search( '-top' ) !== -1;
		var isBottom	= name.search( '-bottom' ) !== -1;

		return isLeft || isRight || isTop || isBottom;
	},

	setOpacity: function( element, val ) {
		obj = getObj( element );
		obj.style.opacity = (val / 100);
		obj.style.MozOpacity = (val / 100);
		obj.style.KhtmlOpacity = (val / 100);
		obj.style.filter = "alpha(opacity=" + val + ")"; 
	},
	
	getOpacity: function( element ) {

		var opacity = this.getStyle( element, 'opacity' ) * 100;
		var mozOpacity = this.getStyle( element, '-moz-opacity' ) * 100;
		var KhtmlOpacity = this.getStyle( element, '-khtml-opacity' ) * 100;
		var ieOpacity = ((this.getStyle( element, 'filter' ) || '') .toLowerCase().match( /alpha\(opacity=(.*)\)/ ) || new Array() ).last();

		return opacity || mozOpacity || KhtmlOpacity || ieOpacity;
	},

	getFromClass: function( element, styleProp ) {
		var obj = getObj( element );

		if ( obj.currentStyle ) {
			var result = obj.currentStyle[styleProp.camelize()];
		} else if ( window.getComputedStyle ) {
			var result = document.defaultView.getComputedStyle( obj, null ).getPropertyValue( styleProp );

		}

		return result;
	},

	getFromStyle: function( element, styleProp ) {
		return getObj( element ).style[styleProp.camelize()];
	},

	getStyle: function( element, styleProp ) {
		var styleResult = this.getFromStyle( element, styleProp );
		var classResult = this.getFromClass( element, styleProp );		

		if ( styleResult == '' && classResult == '' && this._isQuadratic( styleProp ) ) {
			var styleResultQ = this.getFromStyle( element, styleProp.gsub( /-.*?$/ , '' ) );
			var classResultQ = this.getFromClass( element, styleProp.gsub( /-.*?$/ , '' ) );		

			var quadraticResult = styleResultQ !== '' ? styleResultQ : classResultQ;

			if ( quadraticResult !== '' ) {
			    var resultArr = quadraticResult.toArray(' ');
				
				if ( resultArr.length == 1 ) {
				    return quadraticResult;
				}
				return resultArr[ this.quadraticItems.indexOf( styleProp.gsub( /^[a-z]*?-/ , '' ) ) ];
			}
		}

		if ( styleResult !== '' ) {
		    return styleResult;
		} else {
		    return classResult;
		}
	},

	setStyle: function( element, styleProp, value ) {

		if ( styleProp == 'opacity' ) {
		    this.setOpacity( element, value );
		} else {
		    getObj( element ).style[styleProp.camelize()] = value;
		}

	}

}