﻿/* This script based on:
 *
 * Thickbox 2.1 - jQuery plugin for displaying content in a box above the page
 * 
 * Copyright (c) 2006, 2007 Cody Lindley (http://www.codylindley.com)
 *
 * Licensed under the MIT License:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Modified by Trumba Corporation in 2007.
 */

function ImagePath(path) {
	if (typeof(swapRoot) == "undefined")
		path = path.replace('images', '../images');
	return path;
}

var exArrowClosed = new Image(); exArrowClosed.src = ImagePath("images/expand_arrow_closed.gif");
var exArrowOpen = new Image(); exArrowOpen.src = ImagePath("images/expand_arrow_open.gif");

window.onload = AP_init;

function AP_init() {
	$("a.about_pop_link").click(function(event) {
		event.preventDefault();
		this.blur();
		AP_show(event, this.href);
	});
	
	$("a.HP_link").click(function(event) {
		event.preventDefault();
		this.blur();
		HP_toggle(this, this.href);
	});
}

function HP_toggle(link, url) {
	var queryString = url.match(/\#HP\?(.+)/)[1];
	var params = AP_parseQuery( queryString );
	var hpID = "#" + params["id"];
	
	$(hpID).slideToggle("fast", function() {
		var h2 = $(link).parent()[0];
		var eImg = $(h2).find("img")[0];
		if (eImg.src == exArrowClosed.src)
			eImg.src = exArrowOpen.src;
		else
			eImg.src = exArrowClosed.src;
	});
}

function AP_show(e, url) {
	AP_remove();
	
	var queryString = url.match(/\#TB_inline\?(.+)/)[1];
	var params = AP_parseQuery( queryString );
	var boxID = "#" + params["inlineID"];
	
	// Append a shim iframe. Append overlay to handle click hide event.
	if ( !$("#AP_overlay").length ) {
		$("body").append("<div id='AP_overlay'></div>");
		$("#AP_overlay").click(AP_remove);
	}
	
	AP_overlaySize();
	
	$(boxID).show("fast", function() {
		$("a.pop_close_link").click(AP_remove);
		
		$("a.EX_link").click(function(event) {
			event.preventDefault();
			this.blur();
			EX_show(this, this.href);
		});
		this.parentNode.style.zIndex = "100";
	});
	
	// If there's a hide parameter on the url. Hide that id.
	var hideID = "#" + params["hide"];
	$(hideID).hide("fast");
}

function EX_show(link, url) {
	var queryString = url.match(/\#EX\?(.+)/)[1];
	var params = AP_parseQuery( queryString );
	var exID = "#" + params["id"];
	
	$(exID).slideToggle("fast", function() {
		var h2 = $(link).parent()[0];
		var eImg = $(h2).find("img")[0];
		if (eImg.src == exArrowClosed.src)
			eImg.src = exArrowOpen.src;
		else
			eImg.src = exArrowClosed.src;
	});
}

function AP_remove() {
	$("#AP_overlay").unbind("click");
	$("a.EX_link").unbind("click");
	var url = $(".about_pop").hide("fast",function(){$("#AP_overlay").remove();});
	
	$(".about_spud").css({"z-index":"20"});
	$(".spud_button").css({"z-index":"20"});
	
	// HACK: make sure the view spud button is showing.
	$("#view_chooser_img").show("fast");
	
	return false;
}

function AP_overlaySize(){
	if (window.innerHeight && window.scrollMaxY || window.innerWidth && window.scrollMaxX) {	
		yScroll = window.innerHeight + window.scrollMaxY;
		xScroll = window.innerWidth + window.scrollMaxX;
		var deff = document.documentElement;
		var wff = (deff&&deff.clientWidth) || document.body.clientWidth || window.innerWidth || self.innerWidth;
		var hff = (deff&&deff.clientHeight) || document.body.clientHeight || window.innerHeight || self.innerHeight;
		xScroll -= (window.innerWidth - wff);
		yScroll -= (window.innerHeight - hff);
	} else if (document.body.scrollHeight > document.body.offsetHeight || document.body.scrollWidth > document.body.offsetWidth){ // all but Explorer Mac
		yScroll = document.body.scrollHeight;
		xScroll = document.body.scrollWidth;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		yScroll = document.body.offsetHeight;
		xScroll = document.body.offsetWidth;
  	}
	$("#AP_overlay").css({"height": yScroll, "width": xScroll});
}

function AP_parseQuery ( query ) {
	// return empty object
	if( !query )
		return {};
	var params = {};
	
	// parse query
	var pairs = query.split(/[;&]/);
	for ( var i = 0; i < pairs.length; i++ ) {
		var pair = pairs[i].split('=');
		if ( !pair || pair.length != 2 )
			continue;
		// unescape both key and value, replace "+" with spaces in value
		params[unescape(pair[0])] = unescape(pair[1]).replace(/\+/g, ' ');
   }
   return params;
}
