// include css
var e = document.createElement("link");
e.setAttribute("rel","stylesheet");
e.setAttribute("href","http://libs.zaftr.com/hints/jq_hints3.css");
try {
        document.getElementsByTagName('head')[0].appendChild(e);
} catch(z) {
        document.body.appendChild(e);
}

var hcnt = '0';
 
function makehints() {

	var e = document.createElement('div');
	e.setAttribute('id','h_current');
	e.setAttribute('style','display:none');
	document.body.appendChild(e);
	$('#h_current').html('0');
 
	var hms = $(".hint");
 
	var h_wt = '0';
	var h_has_steps = '0';
 
	for (i = 0; i < hms.length; i++) {
 
		theid = hms[i]['id'];
 
		var hc = document.getElementById(theid).className;
 
		$('#'+theid).bind("mouseover",function() {
			show_hint(this,hcnt,'_mo');
		});
		$('#'+theid).bind("mouseout",function() {
			close_hint(hcnt);
		});
 
		hcnt++;

		if (hc.indexOf("h_walkthrough") != -1) {
			h_wt = '1';
		}
		if (hc.indexOf("h_step") != -1) {
			h_has_steps = '1';
		} 

	}

	if (h_wt == '1' && h_has_steps == '1') {
		pgrowl('text','<b>Hints Walkthrough</b><p>A hints walkthrough is available for this page<p><a href=javascript:show_wt()>Show walkthrough</a><p><a href=javascript:pg_close_all()>close</a>','pg_noclose');
	}
 
}
 
function show_multi_hint(hnull,hcnt,hid) {
 
	var what = $('#'+hid);
	show_hint(what,hcnt,'');
 
}
 
function show_hint(what,hcnt,info) {

	if ($('#h_current').html() == '0') {
 
		$('#h_current').html(''+hcnt+'');

		if (info != '_mo') {
			what = document.getElementById(info);
		} else {
			close_hint(hcnt);
		}
	 
		// get the additional classes for this hint
		var hc = what.className;
	
		var thehint = '';
		if (hc.indexOf("h_ajax") == -1) {
			thehint = what.title;
			do_show_hint(what,hcnt,info,thehint);
		} else {
			parts = hc.split("h_ajax:");
			h_bits = parts[1].split(" ");
			h_ajax = h_bits[0];
			// get the content from the ajax call
			$.post(h_ajax,  ''  ,function(aResult) {
				thehint = aResult;
				do_show_hint(what,hcnt,info,thehint);
			});
		}
 
	}
}

function do_show_hint(what,hcnt,info,thehint) {
 
	// get the additional classes for this hint
	var hc = what.className;
 
	// create the new element for title
	var e = document.createElement("div");
	var hint_title = 'hint_title_'+hcnt;
	e.setAttribute("id",hint_title);
	e.innerHTML = thehint;
	document.body.appendChild(e);
	$('#'+hint_title).css({"display": "none"});
	// empty the title on the hint element
	what.title = '';
 
	var theid = what.id;
 
	// create a new element saying which hint we are on
	var e = document.createElement("div");
	var hint_id = 'hint_id_'+hcnt;
	e.setAttribute("id",hint_id);
	e.innerHTML = theid;
	document.body.appendChild(e);
	$('#'+hint_id).css({"display": "none"});
 
	// create a new element for the current element bgcolor
	var e = document.createElement("div");
	var hint_bg = 'hint_bg_'+hcnt;
	e.setAttribute("id",hint_bg);
	e.innerHTML = $('#'+theid).css("background-color");
	document.body.appendChild(e);
	$('#'+hint_bg).css({"display": "none"});
 
	var hpos = $('#'+theid).offset();
	var hx = hpos.left;
	var hy = hpos.top;
	var hwidth = $('#'+theid).outerWidth();
	var hheight = $('#'+theid).outerHeight();
 
	// additional css effects for the calling element
	if (hc.indexOf("h_hand") != -1) {
		$('#'+theid).css({ "cursor": "pointer" });
	}
 
	var h_width = '120';
	var h_height = '';
 
	// size the hint
	if (hc.indexOf("h_hintwidth") != -1) {
		parts = hc.split("h_hintwidth:");
		h_bits = parts[1].split(" ");
		h_width = h_bits[0];
	}
	if (hc.indexOf("h_hintheight") != -1) {
		parts = hc.split("h_hintheight:");
		h_bits = parts[1].split(" ");
		h_height = h_bits[0];
	}
 
	// possible additional positioning
	var h_def_top = '';
	var h_def_left = '';
	var h_ft = '';
	if (hc.indexOf("h_defined_top") != -1) {
		parts = hc.split("h_defined_top:");
		h_bits = parts[1].split(" ");
		h_def_top = h_bits[0]*1;
	}
	if (hc.indexOf("h_defined_left") != -1) {
		parts = hc.split("h_defined_left:");
		h_bits = parts[1].split(" ");
		h_def_left = h_bits[0]*1;
	}
	// finetuning on the hook?
	if (hc.indexOf("h_finetune") != -1) {
		parts = hc.split("h_finetune:");
		h_bits = parts[1].split(" ");
		h_ft = h_bits[0];
	}
 
	// create the new element for the hint
	var e = document.createElement("div");
	var hint = 'hint_'+hcnt;
	e.setAttribute("id",hint);
	e.innerHTML = thehint;
	document.body.appendChild(e);
	$('#'+hint).css({"display": "none"});
	document.getElementById(hint).className = 'hinty';
 
	var hook_size = 0;
 
	if (hc.indexOf("h_nohook") == -1) {
 
		// create the new element for the hint hook
		var e = document.createElement("div");
		var hint_hook = 'hint_hook_'+hcnt;
		e.setAttribute("id",hint_hook);
		document.body.appendChild(e);
		$('#'+hint_hook).css({"display": "none"});
		hook_size = 10;
 
	} 
 
	// size the hint hook
	$('#'+hint).css({"width": h_width+"px"});
	if (h_height != '') {
		$('#'+hint).css({"height": h_height+"px"});
	}
 
	// additional position calcs
	var hint_w = $('#'+hint).outerWidth();
	var hint_h = $('#'+hint).outerHeight();
	var ft_x = '0';
	var ft_y = '0';
	var si_x = 0;
	var si_y = 0;

	// get the hint background color
	var hintbg = $('#'+hint).css('background-color');
	if (hc.indexOf("h_hookbg") != -1) {
		parts = hc.split("h_hookbg:");
		h_bits = parts[1].split(" ");
		hintbg = h_bits[0];	
	}
 
	// compass positioning the hook?
	var hookpos = '';
	if (hc.indexOf("h_hookpos") != -1) {
		parts = hc.split("h_hookpos:");
		h_bits = parts[1].split(" ");
		hookpos = h_bits[0];	
	}

	// make position of the hint and the hook
	if (hc.indexOf("h_right") != -1) {
		hwx = hwidth + hx + hook_size;
		hwy = hheight/2 + hy - (hint_h/2);
		hh_wx = hwx - (hook_size);
		hh_wy = hwy + (hint_h/2) - (hook_size/2);
		if (hookpos == 'NW') {
			hwy = hwy + (hint_h/4);
			hh_wy = hwy + (hint_h/4) - (hook_size/2);
		}
		if (hookpos == 'SW') {
			hwy = hwy - (hint_h/4);
			var hcalc = hint_h/4;
			hh_wy = hwy + (hcalc*3) - (hook_size/2);
		}

		hook_class = 'right';
		si_x = 10;
		$('#'+hint_hook).css({'border-right':'10px solid '+hintbg+''});

	}
	if (hc.indexOf("h_below") != -1) {
		hwx = hx + (hwidth/2 - hint_w/2);
		hwy = hheight + hy + hook_size;
		hh_wx = hwx + (hint_w/2) - (hook_size/2);
		hh_wy = hwy - (hook_size);
		if (hookpos == 'NW') {
			hwx = hwx + (hint_w/4);
			hh_wx = hwx + (hint_w/4) - (hook_size/2);
		}
		if (hookpos == 'NE') {
			hwx = hwx - (hint_w/4);
			var hcalc = hint_w/4;
			hh_wx = hwx + (hcalc*3) - (hook_size/2);
		}
		hook_class = 'below';
		si_y = 10;
		$('#'+hint_hook).css({'border-bottom':'10px solid '+hintbg+''});
	}
	if (hc.indexOf("h_left") != -1) {
		hwx = hx - hint_w - hook_size;
		hwy = hheight/2 + hy - (hint_h/2);
		hh_wx = hx - (hook_size);
		hh_wy = hwy + (hint_h/2) - (hook_size/2);
		if (hookpos == 'NE') {
			hwy = hwy + (hint_h/4);
			hh_wy = hwy + (hint_h/4) - (hook_size/2);
		}
		if (hookpos == 'SE') {
			hwy = hwy - (hint_h/4);
			var hcalc = hint_h/4;
			hh_wy = hwy + (hcalc*3) - (hook_size/2);
		}

		hook_class = 'left';
		si_x = -10;
		$('#'+hint_hook).css({'border-left':'10px solid '+hintbg+''});
	}
	if (hc.indexOf("h_above") != -1) {
		hwx = hx + (hwidth/2 - hint_w/2);
		hwy = hy - hint_h - hook_size;
		hh_wx = hwx + (hint_w/2) - (hook_size/2);
		hh_wy = hwy + hint_h;
		if (hookpos == 'SW') {
			hwx = hwx + (hint_w/4);
			hh_wx = hwx + (hint_w/4) - (hook_size/2);
		}
		if (hookpos == 'SE') {
			hwx = hwx - (hint_w/4);
			var hcalc = hint_w/4;
			hh_wx = hwx + (hcalc*3) - (hook_size/2);
		}
		hook_class = 'above';
		si_y = -10;
		$('#'+hint_hook).css({'border-top':'10px solid '+hintbg+''});
	}
 
	// finetune
	if (h_ft == 'top') {
		ft_y = hint_h/4;
		hwy = hwy + ft_y;
	}
	if (h_ft == 'bottom') {
		ft_y = hint_h/4;
		hwy = hwy - ft_y;
	}
	if (h_ft == 'left') {
		ft_x = hint_w/4;
		hwx = hwx + ft_x;
	}
	if (h_ft == 'right') {
		ft_x = hint_w/4;
		hwx = hwx - ft_x;
	}
 
 
	if (hc.indexOf("h_nohook") == -1) {
		// assign the hook class
		document.getElementById(hint_hook).className = 'hooky h_hook_'+hook_class;
	}
 
	// any additional positioning
	if (h_def_top != '') {
		hwy = hwy + h_def_top;
		hh_wy = hh_wy + h_def_top;
	}
	if (h_def_left != '') {
		hwx = hwx + h_def_left;
		hh_wx = hh_wx + h_def_left;
	}
 
	// IE 6 boxmodel fixes for above and below
	h_br = navigator.userAgent;
	if (h_br.indexOf("MSIE 6") != -1) {
		if (hook_class == 'above' || hook_class == 'below') {
			hh_wy = hh_wy - 3;
		}
	}
 
	// highlight the element if we need to
	if (hc.indexOf("h_highlight") != -1) {
		$('#'+theid).css({"background-color":"#fff"});
	}

	// position the hint
	$('#'+hint).css({"top": hwy+"px", "left": hwx+"px"});
 
	if (hc.indexOf("h_nohook") == -1) {
		// position the hook
		$('#'+hint_hook).css({"top": hh_wy+"px", "left": hh_wx+"px"});
	}
 
	//
	//// allow h_fadein and h_slidein
	//

	var h_in = '0';

	// fade in
	if (hc.indexOf("h_fadein") != -1) {

		$('#'+hint).fadeIn("slow");
		if (hc.indexOf("h_nohook") == -1) {
			$('#'+hint_hook).fadeIn("slow");
		}
		h_in = '1';
	} 

	// slide in
	if (hc.indexOf("h_slidein") != -1) {

		var hsip_x = hwx+si_x;
		var hsip_y = hwy+si_y;

		var hhsip_x = hh_wx+si_x;
		var hhsip_y = hh_wy+si_y;


		// reposition the hook and hint
		$('#'+hint).css({"top": hsip_y+"px", "left": hsip_x+"px"});
	 
		if (hc.indexOf("h_nohook") == -1) {
			// position the hook
			$('#'+hint_hook).css({"top": hhsip_y+"px", "left": hhsip_x+"px"});
		}

		if (hc.indexOf("h_solid") != -1) {
			var h_o = 1;
			if (hc.indexOf("h_nohook") == -1) {
				var hh_o = 1;
			}
		} else {
			var h_o = $('#'+hint).css('opacity');
			if (hc.indexOf("h_nohook") == -1) {
				var hh_o = $('#'+hint_hook).css('opacity');
			}
		}

		$('#'+hint).css('display', 'block');
		$('#'+hint).animate({ opacity: 0 }, 0);

		if (hc.indexOf("h_nohook") == -1) {
			$('#'+hint_hook).css('display', 'block');
			$('#'+hint_hook).animate({ opacity: 0 }, 0);
		}

		// slide in
		$('#'+hint).animate({"top": hwy+"px", "left": hwx+"px","opacity":h_o});
	 
		if (hc.indexOf("h_nohook") == -1) {
			// position the hook
			$('#'+hint_hook).animate({"top": hh_wy+"px", "left": hh_wx+"px","opacity":hh_o});
		}


		h_in = '1';
	} 

	if (h_in == '0') {
		$('#'+hint).css({"display": "block"});
		if (hc.indexOf("h_nohook") == -1) {
			$('#'+hint_hook).css({"display": "block"});
		}
 	} 
 
 
}

function close_hint(thecnt) {
 
	if (document.getElementById('hint_'+thecnt)) {
 
		var oldhint = document.getElementById('hint_'+thecnt);
		document.body.removeChild(oldhint);
 
		if (document.getElementById('hint_hook_'+thecnt)) {
			var oldhint_hook = document.getElementById('hint_hook_'+thecnt);
			document.body.removeChild(oldhint_hook);
		}
 
		var oldhint_id = document.getElementById('hint_id_'+thecnt);
		document.body.removeChild(oldhint_id);
 
		var oldhint_bg = document.getElementById('hint_bg_'+thecnt);
		$('#'+oldhint_id.innerHTML).css({ "background-color": oldhint_bg.innerHTML});
		document.body.removeChild(oldhint_bg);
 
		var hc = document.getElementById(oldhint_id.innerHTML).className;
 
		var oldhint_title = document.getElementById('hint_title_'+thecnt);
		if (hc.indexOf("h_ajax") == -1) {
			document.getElementById(oldhint_id.innerHTML).title = oldhint_title.innerHTML;	
		}
		document.body.removeChild(oldhint_title);

		$('#h_current').html('0')
 
	}
 
}

// hints walkthrough functions
//
// requires jq_pgrowls js and css

var wt_cnt = '0';
var wt_timer = null;

// run the walkthrough
function show_wt() {

	wt_cnt = '0';

	wt_lock();

	// create the hints that can be stepped through
	var e = document.createElement("div");
	e.setAttribute("id","wt_steps");
	document.body.appendChild(e);
	$('#wt_steps').css({'display':'none'});
	$('#wt_steps').html('');

	var hms = $(".hint");

	var steps = ''; 

	for (i = 0; i < hms.length; i++) {
 
		theid = hms[i]['id'];

		var wthc = document.getElementById(theid).className;

		if (wthc.indexOf("h_step") != -1) {
			steps += ''+theid+' ';
		}

	}

	if (steps != '') {
		wt_running();
		$('#wt_steps').html(steps);
		run_wt();
	}

}

function run_wt() {

	close_open_wt();

	var steps = $('#wt_steps').html();
	var bits = steps.split(" ");
	var thishint = bits[wt_cnt];

	if (thishint != '') {

		// show this hint
		show_hint(document.getElementById(thishint),wt_cnt,'_mo');
	
		/* calculate the time the box will be open for */
		var title_length = document.getElementById(thishint).title;
		var hint_delay = title_length.length*40;
		if (hint_delay < 2000) {
			hint_delay = 2000;
		}
		
		wt_timer = setTimeout("run_wt()",hint_delay);
	
		wt_cnt++;

	} else {

		end_wt();

	}

}

function continue_wt() {

	wt_lock();
	wt_running();
	run_wt();

}

function wt_running() {

	pgrowl('text','<b>Hints Walkthrough</b><p>The hints walkthrough is running<p><a href=javascript:pause_wt()>Pause walkthrough</a><p><a href=javascript:end_wt()>Cancel</a>','pg_noclose');

}

function pause_wt() {

	clear_wt_lock();
	close_open_wt();
	clearTimeout(wt_timer);

	pgrowl('text','<b>Hints Walkthrough</b><p>The hints walkthrough is paused<p><a href=javascript:continue_wt()>Continue walkthrough</a><p><a href=javascript:end_wt()>Cancel</a>','pg_noclose');

}

function close_open_wt() {

	var check_wt = wt_cnt - 1;

	if (document.getElementById('hint_'+check_wt)) {
		close_hint(check_wt);
	}

}

function wt_lock() {

	// lock the page
	var e = document.createElement("div");
	e.setAttribute("id","wt_lock");
	document.body.appendChild(e);
	$('#wt_lock').css({'display':'block', 'position':'fixed','top':'0px','left':'0px','width':'100%','height':'100%','z-index':'8995'});
	$('#wt_lock').css({'cursor':'wait'});

}

function clear_wt_lock() {

	if (document.getElementById('wt_lock')) {
		var e = document.getElementById('wt_lock');
		document.body.removeChild(e);
	}

}

function end_wt() {

	close_open_wt();
	clearTimeout(wt_timer);

	if (document.getElementById('wt_steps')) {
		var e = document.getElementById('wt_steps');
		document.body.removeChild(e);
	}

	pgrowl('text','<b>Hints Walkthrough</b><p>The hints walkthrough has finished<p><a href=javascript:show_wt()>Show walkthrough again</a><p><a href=javascript:pg_close_all()>close</a>','pg_noclose');

	clear_wt_lock();

}


