/*
Created By: Chris Campbell
Website: http://particletree.com
Date: 2/1/2006

Inspired by the videobox implementation found at http://www.huddletogether.com/projects/videobox/
*/

/*-------------------------------GLOBAL VARIABLES------------------------------------*/

var detect = navigator.userAgent.toLowerCase();
var OS,browser,version,total,thestring;

/*-----------------------------------------------------------------------------------------------*/

//Browser detect script origionally created by Peter Paul Koch at http://www.quirksmode.org/

function XgetBrowserInfo() {
	if (XcheckIt('konqueror')) {
		browser = "Konqueror";
		OS = "Linux";
	}
	else if (XcheckIt('safari')) browser 	= "Safari"
	else if (XcheckIt('omniweb')) browser 	= "OmniWeb"
	else if (XcheckIt('opera')) browser 		= "Opera"
	else if (XcheckIt('webtv')) browser 		= "WebTV";
	else if (XcheckIt('icab')) browser 		= "iCab"
	else if (XcheckIt('msie')) browser 		= "Internet Explorer"
	else if (!XcheckIt('compatible')) {
		browser = "Netscape Navigator"
		version = detect.charAt(8);
	}
	else browser = "An unknown browser";

	if (!version) version = detect.charAt(place + thestring.length);

	if (!OS) {
		if (XcheckIt('linux')) OS 		= "Linux";
		else if (XcheckIt('x11')) OS 	= "Unix";
		else if (XcheckIt('mac')) OS 	= "Mac"
		else if (XcheckIt('win')) OS 	= "Windows"
		else OS 								= "an unknown operating system";
	}
}

function XcheckIt(string) {
	place = detect.indexOf(string) + 1;
	thestring = string;
	return place;
}

/*-----------------------------------------------------------------------------------------------*/

Event.observe(window, 'load', initialize, false);
Event.observe(window, 'load', XgetBrowserInfo, false);
Event.observe(window, 'unload', Event.unloadCache, false);

var videobox = Class.create();

videobox.prototype = {

	yPos : 0,
	xPos : 0,

	initialize: function(ctrl) {
		this.content = ctrl.href;
		Event.observe(ctrl, 'click', this.Xactivate.bindAsEventListener(this), false);
		ctrl.onclick = function(){return false;};
	},
	
	// Turn everything on - mainly the IE fixes
	Xactivate: function(){
		if (browser == 'Internet Explorer'){
			this.XgetScroll();
			this.XprepareIE('100%', 'hidden');
			this.XsetScroll(0,0);
			this.XhideSelects('hidden');
		}
		this.displayVideobox("block");
	},
	
	// Ie requires height to 100% and overflow hidden or else you can scroll down past the videobox
	XprepareIE: function(height, overflow){
		bod = document.getElementsByTagName('body')[0];
		bod.style.height = height;
		bod.style.overflow = overflow;
  
		htm = document.getElementsByTagName('html')[0];
		htm.style.height = height;
		htm.style.overflow = overflow; 
	},
	
	// In IE, select elements hover on top of the videobox
	XhideSelects: function(visibility){
		selects = document.getElementsByTagName('select');
		for(i = 0; i < selects.length; i++) {
			selects[i].style.visibility = visibility;
		}
	},
	
	// Taken from videobox implementation found at http://www.huddletogether.com/projects/videobox/
	XgetScroll: function(){
		if (self.pageYOffset) {
			this.yPos = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){
			this.yPos = document.documentElement.scrollTop; 
		} else if (document.body) {
			this.yPos = document.body.scrollTop;
		}
	},
	
	XsetScroll: function(x, y){
		window.scrollTo(x, y); 
	},
	
	displayVideobox: function(display){
		$('overlay').style.display = display;
		$('videobox').style.display = display;
		if(display != 'none') this.XloadInfo();
	},
	
	// Begin Ajax request based off of the href of the clicked linked
	XloadInfo: function() {
		var myAjax = new Ajax.Request(
        this.content,
        {method: 'post', parameters: "", onComplete: this.XprocessInfo.bindAsEventListener(this)}
		);
		
	},
	
	// Display Ajax response
	XprocessInfo: function(response){
		info = "<div id='vbContent'>" + response.responseText + "</div>";
		new Insertion.Before($('vbLoadMessage'), info)
		$('videobox').className = "done";	
		this.Xactions();			
	},
	
	// Search through new links within the videobox, and attach click event
	Xactions: function(){
		vbActions = document.getElementsByClassName('vbAction');

		for(i = 0; i < vbActions.length; i++) {
			Event.observe(vbActions[i], 'click', this[vbActions[i].rel].bindAsEventListener(this), false);
			vbActions[i].onclick = function(){return false;};
		}

	},
	
	// Example of creating your own functionality once videobox is initiated
	Xinsert: function(e){
	   link = Event.element(e).parentNode;
	   Element.remove($('vbContent'));
	 
	   var myAjax = new Ajax.Request(
			  link.href,
			  {method: 'post', parameters: "", onComplete: this.XprocessInfo.bindAsEventListener(this)}
	   );
	 
	},
	
	// Example of creating your own functionality once videobox is initiated
	Xdeactivate: function(){
		Element.remove($('vbContent'));
		
		if (browser == "Internet Explorer"){
			this.XsetScroll(0,this.yPos);
			this.XprepareIE("auto", "auto");
			this.XhideSelects("visible");
		}
		
		this.displayVideobox("none");
	}
}

/*-----------------------------------------------------------------------------------------------*/

// Onload, make all links that need to trigger a videobox active
function initialize(){
	addVideoboxMarkup();
	vbox = document.getElementsByClassName('vbOn');
	for(i = 0; i < vbox.length; i++) {
		valid = new videobox(vbox[i]);
	}
}

// Add in markup necessary to make this work. Basically two divs:
// Overlay holds the shadow
// Videobox is the centered square that the content is put into.
function addVideoboxMarkup() {
	bod 				= document.getElementsByTagName('body')[0];
	overlay 			= document.createElement('div');
	overlay.id		= 'overlay';
	vb					= document.createElement('div');
	vb.id				= 'videobox';
	vb.className 	= 'loading';
	vb.innerHTML	= '<div id="vbLoadMessage">' +
//						  '<p>Loading</p>' +
						  '</div>';
	bod.appendChild(overlay);
	bod.appendChild(vb);
}