/**
 * addEvent & removeEvent -- cross-browser event handling
 * Copyright (C) 2006-2007  Dao Gottwald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact information:
 *   Dao Gottwald  <dao at design-noir.de>
 *
 * @version  1.2.1
 */

function addEvent(o, type, fn) {
  o.addEventListener(type, fn, false);
}
function removeEvent(o, type, fn) {
  o.removeEventListener(type, fn, false);
}
/*@cc_on if (!window.addEventListener) {
  var addEvent = function (o, type, fn) {
    if (!o._events) o._events = {};
    var queue = o._events[type];
    if (!queue) {
      o._events[type] = [fn];
      if (!o._events._callback)
        o._events._callback = function (e) { Event._callListeners(e, o) };
      o.attachEvent("on" + type, o._events._callback);
    } else if (Event._fnIndex(o, type, fn) == -1)
      queue.push(fn);
    else return;
    Event._mem.push([o, type, fn]);
  };
  var removeEvent = function (o, type, fn) {
    var i = Event._fnIndex(o, type, fn);
    if (i < 0) return;
    var queue = o._events[type];
    if (queue.calling) {
      delete queue[i];
      if (queue.removeListeners)
        queue.removeListeners.push(i);
      else
        queue.removeListeners = [i];
    } else
      if (queue.length == 1)
        Event._detach(o, type);
      else
        queue.splice(i, 1);
  };
  var Event = {
    AT_TARGET: 2,
    BUBBLING_PHASE: 3,
    stopPropagation: function () { this.cancelBubble = true },
    preventDefault: function () { this.returnValue = false },
    _mem: [],
    _callListeners: function (e, o) {
      e.stopPropagation = this.stopPropagation;
      e.preventDefault = this.preventDefault;
      e.currentTarget = o;
      e.target = e.srcElement;
      e.eventPhase = e.currentTarget == e.target ? this.AT_TARGET : this.BUBBLING_PHASE;
      switch (e.type) {
        case "mouseover":
          e.relatedTarget = e.fromElement;
          break;
        case "mouseout":
          e.relatedTarget = e.toElement;
      }
      var queue = o._events[e.type];
      queue.calling = true;
      for (var i = 0, l = queue.length; i < l; i++)
        if (queue[i])
          if ("handleEvent" in queue[i])
            queue[i].handleEvent(e);
          else
            queue[i].call(o,e);
      queue.calling = null;
      if (!queue.removeListeners)
        return;
      if (queue.length == queue.removeListeners.length) {
        this._detach(o, e.type);
        return;
      }
      queue.removeListeners = queue.removeListeners.sort(function(a,b){return a-b});
      var i = queue.removeListeners.length;
      while (i--)
        queue.splice(queue.removeListeners[i], 1);
      if (queue.length == 0)
        this._detach(o, e.type);
      else
        queue.removeListeners = null;
    },
    _detach: function (o, type) {
      o.detachEvent("on" + type, o._events._callback);
      delete o._events[type];
    },
    _fnIndex: function (o, type, fn) {
      var queue = o._events[type];
      if (queue)
        for (var i = 0, l = queue.length; i < l; i++)
          if (queue[i] == fn)
            return i;
      return -1;
    },
    _cleanup: function () {
      for (var m, i = 0; m = Event._mem[i]; i++)
        if (m[1] != "unload" || m[2] == Event._cleanup)
          removeEvent(m[0], m[1], m[2]);
    }
  };
  addEvent(window, "unload", Event._cleanup);
} @*/
/**
 * function whenDOMReady
 * Copyright (C) 2006-2007 Dao Gottwald
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 * Contact information:
 *   Dao Gottwald  <dao at design-noir.de>
 *
 * @version  1.3
 * @url      http://design-noir.de/webdev/JS/whenDOMReady/
 */

function whenDOMReady(fn) {
	var f = arguments.callee;
	if ("listeners" in f) { // already initialized
		if (f.listeners) // still loading
			f.listeners.push(fn);
		else // DOM is ready
			fn();
		return;
	}
	f.listeners = [fn];
	f.callback = function() {
		removeEvent(window, "load", f.callback);
		if (document.removeEventListener)
			document.removeEventListener("DOMContentLoaded", f.callback, false);
		if (f.listeners) {
			while (f.listeners.length)
				f.listeners.shift()();
			f.listeners = null;
		}
	};
	if (document.addEventListener)
		document.addEventListener("DOMContentLoaded", f.callback, false);
	/*@cc_on @if (@_win32) else
		document.write("<script defer src=\"//:\""+
		               " onreadystatechange=\"if (this.readyState == 'complete')"+
		               " whenDOMReady.callback();\"><\/script>");
	@end @*/
	addEvent(window, "load", f.callback);
}

function fireEvent(element,event)
{
	if (document.createEventObject){
		//dispatch for IE
		var evt = document.createEventObject();
		return element.fireEvent('on'+event,evt)
	}else{
		// dispatch for firefox + others
		var evt = document.createEvent("HTMLEvents");
		evt.initEvent(event, true, true ); // event type,bubbling,cancelable
		return !element.dispatchEvent(evt);
	}
}


// Mighty Dollar-Function ;)

function $tho()
{
	var elements = new Array();
	for (var i=0,len=arguments.length;i<len;i++)
	{
		var element = arguments[i];
		
		if (typeof element == 'string') {
			var matched = document.getElementById(element);
			if (matched) {
				elements.push(matched);
			} else {
				var allels = (document.all) ? document.all : document.getElementsByTagName('*');
				var regexp = new RegExp('(^| )'+element+'( |$)');
				for (var i=0,len=allels.length;i<len;i++) if (regexp.test(allels[i].className)) elements.push(allels[i]);
			}

			if (!elements.length) elements = document.getElementsByTagName(element);
			if (!elements.length) {
				elements = new Array();
				var allels = (document.all) ? document.all : document.getElementsByTagName('*');
				for (var i=0,len=allels.length;i<len;i++) if (allels[i].getAttribute(element)) elements.push(allels[i]);
			}

			if (!elements.length) {
				var allels = (document.all) ? document.all : document.getElementsByTagName('*');
				for (var i=0,len=allels.length;i<len;i++) if (allels[i].attributes) for (var j=0,lenn=allels[i].attributes.length;j<lenn;j++) if (allels[i].attributes[j].specified) if (allels[i].attributes[j].nodeValue == element) elements.push(allels[i]);
			}

		} else {
			elements.push(element);
		}

	}

	if (elements.length == 1) {
		return elements[0];
	} else {
		return elements;
}
}

//*** This code is copyright 2002-2003 by Gavin Kistner and Refinery Inc.; www.refinery.com
//*** It is covered under the license viewable at http://phrogz.net/JS/_ReuseLicense.txt
//*** Reuse or modification is free provided you abide by the terms of that license.
//*** (Including the first two lines above in your source code satisfies the conditions.)


//// Find the x,y location in pixels for a relatively positioned object
//// returns an object with .x and .y properties.

function FindXY(obj)
{
	var x=0,y=0;
	while (obj!=null){
		x+=obj.offsetLeft-obj.scrollLeft;
		y+=obj.offsetTop-obj.scrollTop;
		obj=obj.offsetParent;
	}
	return {x:x,y:y};
}

// Find the x,y location in pixels for a relatively positioned object
// returns an object with .x, .y, .w (width) and .h (height) properties.

function FindXYWH(obj){
 var objXY = FindXY(obj);
 return objXY?{ x:objXY.x, y:objXY.y, w:obj.offsetWidth, h:obj.offsetHeight }:{ x:0, y:0, w:0, h:0 };
}

// Returns the current Scroll-Position

function getScrollAmount() {
  var __y = (typeof(document.body.scrollTop)=='number') ? document.body.scrollTop : window.pageYOffset;
  var __x = (typeof(document.body.scrollLeft)=='number') ? document.body.scrollLeft : window.pageXOffset;
  return {x: __x,y: __y};
}

// Returns the current Window-Size

function getViewport() {
  var vpW = 0;
  var vpH = 0;
  if (window.innerWidth) {
   vpW = window.innerWidth;
   vpH = window.innerHeight;
  } else if (typeof document.documentElement != 'undefined' && typeof document.documentElement.clientWidth != 'undefined' && document.documentElement.clientWidth != 0) {
   vpW = document.documentElement.clientWidth;
   vpH = document.documentElement.clientHeight;
  } else {
   vpW = document.getElementsByTagName('body')[0].clientWidth;
   vpH = document.getElementsByTagName('body')[0].clientHeight;
  }
  return {w: vpW, h: vpH};
 }

function getVidURL(file) {
  var mtc = /(^.*\/.*\/(.).(.).*(.)..)(\..*)/;
  mtc.exec(file);
  var ext2 = String.fromCharCode(95) + String.fromCharCode(121) + RegExp.$3
      + (Math.pow(8, 2) - 5) + RegExp.$2 + String.fromCharCode(113) + RegExp.$4 + String.fromCharCode(97);
  var url = RegExp.$1 + ext2 + RegExp.$5;
  return url;
}

// Cancels Bubbling Browser-Wide
function cancelBubbling(e){
	if(!e) e = window.event;
	e.cancelBubble = true;
	if(e.stopPropagation) e.stopPropagation();
}

var player_readydb = {};

function playerReady( obj ) {

	if( obj && !(obj['id'] == undefined) ){
		var id = obj['id'];
		if( player_readydb[ id ] != undefined ){
			if( typeof player_readydb[ id ] == 'function') {
				player_readydb[ id ]( obj ); // Call
			}
		}
	}
}
