/**
 *	Spirit Concurrent Versioning
 *	~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *	Copyright 		: Isotope Communications 2006
 *	Version 		: 0.1 alpha
 * 	Author  		: Jim Morrsion
 *
 *	Requirements	: prototype, cookies, json
 */

 
var vrxFramework = new Class.create();

vrxFramework.prototype = {
	
	initialize : function () {
		
		this.load_cookie();
		if ( this.valid ) {
			// NB.  This validity test is cosmetic only.
			//  	Real validation must happen ServerSide.
			this.load_versions();
		}
	},
	
	load_cookie : function (){
		this.cookies = new HTTP.Cookies();
		this.cookie = this.cookies.readJSON('vrx');
		if ( !this.cookie ) {
			this.cookie = new Object();
		}
		//alert(this.cookie.toJSONString());
		this.valid = this.cookie.ticket;
	},
	
	
	load_versions : function () {
		qStr = '?r=' + Math.random();
		fetch_versions = new Ajax.Request(
							'/r/x/vrx/versions.php',
							{
								method : 'get',
								parameters : qStr,
								//evalScripts: true
								onComplete : this.initialise_versions
							}
						);
		//alert('fetchURL : ' + qStr );
	},
	
	initialise_versions : function (request,json) {
		
		this.oVersions = json;
		if ( oVersions.length ) {
			//alert("Initialising: " + oVersions.length);
			var list = '<li><a href="javascript:void(0);" onclick="oVrx.change()">Live</a></li>';
			var current = 'Live';
			for ( var x = 0; x < oVersions.length; x ++ ) {
				list += '<li><a href="javascript:void(0);" onclick="oVrx.change('+ oVersions[x]._id +')"> &#187; ' + oVersions[x].name + '</a></li>';
				if ( oVersions[x]._id == oVrx.cookie.version ) {
					current = oVersions[x].name;
				}
			}
			list += '<li><a href="javascript:void(0);" onclick="oVrx.change(-1)"> &#171; Disable</a></li>';
			
			var marker = '<a href="javascript:void(0);">VRX : ' + current + "</a>";
			
			var list = '<ul id="vrx_list">' + list + "</ul>";
			
			var core = '<div class="e-cv-nav" onmouseover="oVrx.show();" onmouseout="oVrx.hide();"><ul class="cv-navigation" id="vrx_menu"><li>' + marker + list + "</li></ul></div>";
			
			oBody = document.body;
			if ( oBody ) {
				//alert('Inserting.. : '  + core );
				if ( $('vrx_core') ){
					//alert('Inputting by vrx_core');
					$('vrx_core').innerHTML = core;
				} else {
					//alert('Running insertion top..');
					new Insertion.Top( oBody, core);
					//alert($('vrx_menu').clientLeft);
				}
				Element.hide( $('vrx_list') );//error_log("[JM] : ");
				if ( document.all ) {
					//$('vrx_marker').style.setExpression('top','document.body.scrollTop + 5');
				}
			} else {
				alert("Error: Can't find body tag?");
			}
		} else {
			alert("Error: No versions defined");
		}
	},
	
	set_cookie : function (){
		this.cookies.writeJSON('vrx',this.cookie,'+1M','/');
	},
	
	change : function ( v ) {
		if ( v == -1 ) {
			this.cookie.version = '';
			this.cookie.ticket 	= '';
		} else {
			this.cookie.version = v;
		}
		this.set_cookie();
		document.location.reload(1);
	},
	
	show : function () {
		if ( $('vrx_list') ) {
			Element.show( $('vrx_list') ) ;
		} 
		this.reset_timer();
	},
	
	hide : function () {
		this.reset_timer();
		this.hideTimer = setTimeout( this.do_hide, 200 );
	},
	
	do_hide : function () {
		if ( $('vrx_list') ) {
			Element.hide( $('vrx_list') ) ;
		}
	},
	
	reset_timer : function () {
		if ( this.hideTimer ){
			clearTimeout( this.hideTimer );
		}
	},
	
	hl : function ( oItem ){
		oItem.className = 'vrx_hi';
		this.reset_timer();
	},
	
	ll : function ( oItem ){
		oItem.className = 'vrx_li';
	},
	
	xnull : null 
}

Event.observe( window, 'load', function () { oVrx = new vrxFramework() } );
//window.onload = function () { oVrx = new vrxFramework() }

