
// set location for wizard
var wizardDir = '/js/webWizard';
var wizardIMG = 'owlyWizard.png';


var webWizard = {
	
	init: function(callback) {
		
		if ( webWizard.initiated != true) {
		
			var callback = callback || function() {} ;

			WWIncludes = new Array( 
				wizardDir+'/webWizard.css',
				wizardDir+'/jquery.hoverIntent.minified.js',
				wizardDir+'/jquery.bt.js'
			);
		
		
			if(jQ.browser.msie) {
				//WWIncludes.push(wizardDir+'/jquery.bgiframe.min.js');
				//WWIncludes.push(wizardDir+'/excanvas.js');
			}
		
		
			new depLoad(WWIncludes, callback, true);
			
			webWizard.focusAttempts = 0;
			
			webWizard.initiated = true;
		
		}
		
	},
	
	
	setMessage: function(message, noButton) {
				
		var noButton = noButton || false;
		
		if(webWizard.agenda) {
			if(webWizard.agenda[webWizard.currentStep]) {
				if(noButton != true && typeof(webWizard.agenda[webWizard.currentStep].bind_click) == 'undefined') {
					message += '<br /> <a class="wizard_button" onclick="webWizard.nextStep()">OK</a>';
		
				} else if (typeof(webWizard.agenda[webWizard.currentStep].bind_click) != 'undefined') { 
				
					webWizard.hijackClick(webWizard.agenda[webWizard.currentStep].bind_click);

				}
			}
		}
		
		
		jQ('.wizardGuy').btOff().bt(message, {
			padding: 25,
			width: 250,
			spikeLength: 20,
			spikeGirth: 20,
			cornerRadius: 20,
			fill: 'rgba(255, 255, 255, .8)',
			strokeWidth: 3,
			strokeStyle: '#CC0'
		})
		.btOn();
		
	},
	
	
	nextStep: function() {
		
		webWizard.currentStep++;
		
		if(typeof( webWizard.agenda[webWizard.currentStep] ) != 'undefined') {
			
			if (typeof( webWizard.agenda[webWizard.currentStep].page ) != 'undefined') {
				if( document.location.pathname != webWizard.agenda[webWizard.currentStep].page ) {
					document.location = webWizard.agenda[webWizard.currentStep].page + '#WebWizard~' + webWizard.currentAgenda + '~' + ( webWizard.currentStep );
				}
			}
		
			if(typeof(webWizard.agenda[webWizard.currentStep].element) == 'undefined') {
				webWizard.currentStep++;
			}
		
			webWizard.setFocus(webWizard.agenda[webWizard.currentStep].element, webWizard.agenda[webWizard.currentStep].message);
		
			
		} else {
			webWizard.setMessage('<h1 style="text-align:center;">BYE!</h1>', true);
			setTimeout("webWizard.fadeOut(1200)", 1000);
		}
		
	},
		
	
	fadeIn: function(speed, callback) {
		jQ('.disableOverlay').css('opacity', '0.0').fadeTo((speed/2), 0.6);
		jQ('.wizardGuy').css('opacity', '0.0').fadeTo(speed, 1, function() {
			callback();
		});
		
	},
	
	
	fadeOut: function(speed, callback) {
		
		var callback = callback || function(){};
		
		if(jQ('#wizardContainer').length > 0) {
			
			jQ('.wizardGuy').btOff().fadeOut(speed/2);
			
			jQ('.disableOverlay').fadeOut(speed, function() { 
				jQ('#wizardContainer').remove();
				jQ('.wizardGuy').remove();
				callbackCounter(this.parentNode, 4, callback);
			});
			
		} else {
			callback();
		}
		
	},
	
	
	setFocus: function(e, message) {
	
		if (webWizard.focusAttempts > 20) {
			webWizard.setMessage('I could not find what I was looking for.');
			return false;
		}
	
		if (jQ(e).length == 0 || jQ(e+':visible').length == 0) {
			setTimeout('webWizard.setFocus("'+e+'","'+message+'")', 250);
			webWizard.focusAttempts++;
			cb.console('WebWizard Check Again.');
			return;
		}
		
		if( webWizard.initiated ) {
		
			webWizard.fadeOut(500, function() {
		
				// Do the math to get locations for all the divs
				docWidth = jQ(document).width();
				docHeight = jQ(document).height();
				
				webWizard.focus.offset = jQ(e).offset()
		
				webWizard.focus.height = jQ(e).height() + parseInt(jQ(e).css("padding-top")) + parseInt(jQ(e).css("padding-bottom")) + parseInt(jQ(e).css("border-top-width")) + parseInt(jQ(e).css("border-bottom-width"));
				webWizard.focus.width = jQ(e).width() + parseInt(jQ(e).css("padding-left")) + parseInt(jQ(e).css("padding-right")) + parseInt(jQ(e).css("border-left-width")) + parseInt(jQ(e).css("border-right-width"));
		
				webWizard.focus.offset.bottom = webWizard.focus.offset.top + webWizard.focus.height;
				webWizard.focus.offset.right = webWizard.focus.offset.left + webWizard.focus.width;
		
				// Setup the HTML for the disable overlay divs
				overlay1 = '<div class="disableOverlay" style="width: 100% ; height:' + webWizard.focus.offset.top  + 'px; left: 0px; top: 0px "></div> ';
				overlay2 = '<div class="disableOverlay" style="width: ' + webWizard.focus.offset.left + 'px ; height:' + webWizard.focus.height + 'px; left: 0px; top: ' + webWizard.focus.offset.top + 'px; "></div> ';
				overlay3 = '<div class="disableOverlay" style="width: 100% ; height:' + ( docHeight - webWizard.focus.offset.bottom ) + 'px; left: 0px; top: ' + webWizard.focus.offset.bottom  + 'px; "></div> ';
				overlay4 = '<div class="disableOverlay" style="width: ' + ( docWidth - webWizard.focus.offset.right ) + 'px ; height:' + webWizard.focus.height + 'px; left: ' + webWizard.focus.offset.right + 'px; top: ' + webWizard.focus.offset.top + 'px; "></div> ';
		
				// Check to see if wizard is off screen to left or right... If both then center him
				if ( (webWizard.focus.offset.right + 250) > docWidth && (webWizard.focus.offset.left - 250) < 0 ) {
					webWizard.guy.left = webWizard.focus.offset.right - ( (webWizard.focus.width / 2) + 100 );
				} else if ( (webWizard.focus.offset.right + 250) > docWidth ) {
					webWizard.guy.left = webWizard.focus.offset.left - 225;
				} else  {
					webWizard.guy.left = webWizard.focus.offset.right + 25;
				}
			
				// Check to see if wizard should be on top or bottom
				if ( (webWizard.focus.offset.bottom + 200) > docHeight ) {
					webWizard.guy.top = webWizard.focus.offset.top - 225;
				} else {
					webWizard.guy.top = webWizard.focus.offset.bottom + 25;
				}
				
				// Setup the wizard guy image and append him
				webWizard.wizardImg = '<img class="wizardGuy" style="cursor:pointer; position: absolute; z-index:9999; left: ' + webWizard.guy.left + 'px; top: ' + webWizard.guy.top + 'px;" src="'+wizardDir+'/'+wizardIMG+'" onclick="webWizard.nextStep();" />';
				jQ(document.body).append(webWizard.wizardImg);
				
				// Append the disable overlay divs
				jQ(document.body).append('<div id="wizardContainer"></div>');
				jQ('#wizardContainer').append(overlay1);
				jQ('#wizardContainer').append(overlay2);
				jQ('#wizardContainer').append(overlay3);
				jQ('#wizardContainer').append(overlay4);
				
				webWizard.fadeIn(1500, function() { webWizard.setMessage(message) } );
		
			});
		
		} else {
			webWizard.e = e;
			webWizard.init( function() { webWizard.setFocus(webWizard.e, message); } );
		}
		
		webWizard.focusAttempts = 0;
		
	},
	
	
	hijackClick: function(selector) {
		
		//webWizard.hold_click = jQ(selector).get(0).onclick;
		jQ(selector).unbind('click');
		
		jQ(selector).click( function() {
			
			jQ(this).unbind('click');			
			webWizard.nextStep();
			if (webWizard.agenda[webWizard.currentStep-1].hijack_click) return false;
			//jQ(this).get(0).onclick = hold_click;
			//jQ(this).get(0).onclick = null;
			
		});
		
	},
	
	
	getAgenda: function(agendaTitle) {
	
		if(typeof(agendaTitle) != 'undefined' && agendaTitle != '') {
			webWizard.currentAgenda = agendaTitle;
			
			jQ.getJSON( wizardDir + '/agendas/' + agendaTitle + '.json', function(data) {
				webWizard.setAgenda(data);
			});
		} else {
			jQ.getJSON( wizardDir + '/', function(data) {
				webWizard.setFocus(document.body, data);
			});
		}
		
	},
	
	
	setAgenda: function(agenda) {
		
		webWizard.agenda = agenda;	
		webWizard.nextStep();
		
	},
	
	
	checkAgenda: function() {
		
		if (document.location.hash.match('WebWizard') && document.location.hash.split('~').length > 1) {
			
			webWizard.currentStep = (parseInt(document.location.hash.split('~')[2]) - 1);
			webWizard.getAgenda(document.location.hash.split('~')[1]);
		
		} else {
			webWizard.currentStep = 0;
		}
		
	},
	
	
	focus: {},
	
	guy: {}, 
	
	currentAgenda: 0,
	
	currentStep: 0
	
}

jQ(document).ready( function() {
	
	webWizard.checkAgenda();

});

