// Global PodcastPeople functions 
// Created by Andrew Okonetchnikov (andrew@wildbit.com)
// Wildbit LLC (http://wildbit.com)
// Ver 1.1

/* PROTOTYPE's onDOMReady Extension */

Object.extend(Event, {
  _domReady : function() {
    if (arguments.callee.done) return;
    arguments.callee.done = true;

    if (this._timer)  clearInterval(this._timer);
    
    this._readyCallbacks.each(function(f) { f() });
    this._readyCallbacks = null;
},
  onDOMReady : function(f) {
    if (!this._readyCallbacks) {
      var domReady = this._domReady.bind(this);
      
      if (document.addEventListener)
        document.addEventListener("DOMContentLoaded", domReady, false);
        
        /*@cc_on @*/
        /*@if (@_win32)
            document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
            document.getElementById("__ie_onload").onreadystatechange = function() {
                if (this.readyState == "complete") domReady(); 
            };
        /*@end @*/
        
        if (/WebKit/i.test(navigator.userAgent)) { 
          this._timer = setInterval(function() {
            if (/loaded|complete/.test(document.readyState)) domReady(); 
          }, 10);
        }
        
        Event.observe(window, 'load', domReady);
        Event._readyCallbacks =  [];
    }
    Event._readyCallbacks.push(f);
  }
});

/* Global.js functions */

Event.onDOMReady( initPage );

function initPage() {
	var subscribeDD = new Dropdown("subscribe_ctrl", "subscribe_container");
	initSWFObjects();
}

// SWFObject
var swfobjs = [];
function registerSWFObject(swfobj,domel) {
	$(domel).style.visibility = "hidden";
	swfobjs.push({obj:swfobj,el:domel});
}

function initSWFObjects() {
	swfobjs.each( function( swfobj ){
		swfobj.obj.write(swfobj.el);
		$(swfobj.el).style.visibility = "visible";
	});
}


var Dropdown = Class.create();

Dropdown.prototype = {
	initialize: function(controller, element, options) {
		this.controller = $(controller);
		this.element = $(element);
		this.setOptions(options);
		
		if(!this.controller || !this.element)
			return false;
		
		this.isVisible = false;
		
		this.hideWithTimeout = this.hideWithTimeout.bindAsEventListener(this);
		this.cancelHiding = this.cancelHiding.bindAsEventListener(this);
		
		this.options.posX = this.controller.offsetLeft;
		this.options.posY = this.controller.offsetTop + this.controller.getHeight() - 3;
		
		if (navigator.appVersion.match(/\bMSIE\b/))
		{
			this.options.posY += 8;
			this.options.posX += 6;
		}
			
		
		Event.observe( this.controller, "click", this._clickHandler.bindAsEventListener(this), false );
	},
	
	setOptions: function(options) {
		this.options = {
			closeTimeout: 500 	// 1,5 sec before auto close
		};
		Object.extend(this.options, options || {});
	},
	
	toggle: function() {
		if(this.isVisible == false)
			this.show();
		else
			this.hide();
	},
	
	show: function() {
		if(!this.isVisible) {
			var offset = Position.cumulativeOffset(this.controller);
			this.element.style.position = "absolute";
			this.element.style.left = offset[0] + "px";
			this.element.style.top = offset[1] + Element.getDimensions(this.controller).height + "px";
			this.element.style.width = Element.getDimensions(this.element).width + "px";
			this.element.style.height = Element.getDimensions(this.element).height + "px";
			if (/Konqueror|Safari|KHTML/.test(navigator.userAgent)) { // Safari/WebKit browsers position fix
				this.element.style.top = offset[1] - 15 + "px";
			}
			this.controller.addClassName("dd_selected");
			Effect.SlideDown(this.element, {duration: 0.25, afterFinish: this._setVisibilityTrue.bind(this) });
			
			Event.observe( this.controller, "mouseout", this.hideWithTimeout, false);
			Event.observe( this.element, "mouseout", this.hideWithTimeout, false);
			
			Event.observe( this.controller, "mouseover", this.cancelHiding, false);
			Event.observe( this.element, "mouseover", this.cancelHiding, false);
		}
	},
	
	hide: function() {
		if(this.isVisible) {
			
			this.controller.removeClassName("dd_selected");
			
			Effect.SlideUp(this.element, {duration: 0.15 });
			
			Event.stopObserving( this.controller, "mouseout", this.hideWithTimeout, false);
			Event.stopObserving( this.element, "mouseout", this.hideWithTimeout, false);
			
			Event.stopObserving( this.controller, "mouseover", this.cancelHiding, false);
			Event.stopObserving( this.element, "mouseover", this.cancelHiding, false);
			
			this.isVisible = false;
			delete this.timeoutID;
		}
	},
	
	hideWithTimeout: function() {
		this.timeoutID = window.setTimeout(this.hide.bind(this), this.options.closeTimeout);
	},
	
	cancelHiding: function() {
		if(typeof this.timeoutID == "number") {
			window.clearTimeout(this.timeoutID);
			delete this.timeoutID;
		}
	},
	
	hideByClick: function() {
	},
	
	_setVisibilityTrue: function() {
		this.isVisible = true;
	},
	
	_clickHandler: function(e) {
		this.toggle();
		Event.element(e).blur();
		Event.stop(e);
	}
};
