var blank = new Image();
blank.src = '/images/blank.gif';

/**
 * A better onload function made possible with jQuery
 */
$(function () {
	/**
	 * Hover effects for menus
	 * <ul class="hovermenu">
	 * Must have css styles to work correctly
	 * Does not fade <span> elements with class="selected"
	 */
	// Set opacity to nill on page load
	$('.hovermenu span:not(.selected)').css('opacity','0');
	// On mouse over event to fade in, does not fade elements with class="selected"
	$('.hovermenu span:not(.selected)').hover(function () {
		// animate opacity to full
		$(this).stop().animate({
			opacity: 1
		}, 'fast');
	},
	// on mouse out
	function () {
		// animate opacity to nill
		$(this).stop().animate({
			opacity: 0
		}, 'fast');
	});

	// Set images with class="dropshadow" to have a...turkey?
	$('img.dropshadow').wrap('<div class="shadow0"><div class="shadow1"><div class="shadow2"><div class="shadow3"></div></div></div></div>');

	// Makes images open in a very pretty modal frame box
	$("a[rel*='lightbox']").fancybox({
		centerOnScroll	: true,
		overlayShow		: true,
		transitionIn	: 'elastic',
		transitionOut	: 'elastic',
		titlePosition	: 'over',
		titleFormat		: function(title, currentArray, currentIndex, currentOpts) {

			if(currentArray.length == 1) {
				if (title && title.length) {
					return '<span id="fancybox-title-over">' + title + '</span>';
				}
				return false;
			}
			else {
				if (title && title.length) {
					return '<span id="fancybox-title-over"><span class="index">[' +  (currentIndex + 1) + ' / ' + currentArray.length + ']</span>' + title + '</span>';
				}
				else {
					return '<span id="fancybox-title-over"><span class="index">[' +  (currentIndex + 1) + ' / ' + currentArray.length + ']</span>&nbsp;</span>';
				}
			}

			return false;
		},
		onComplete		: function() {
			$('#fancybox-wrap').hover(function() {
				$('#fancybox-title').show();
			}, function() {
				$('#fancybox-title').hide();
			});
		}
	});

	// Inline and iframe replacement
	$("a.thickbox").click(function() {
		var myUrl = $(this).attr('href');
		var myTitle = $(this).attr('title');
		var newWidth = 0, newHeight = 0;
		var queryString = (myUrl.indexOf("?") > -1) ? myUrl.substr(myUrl.indexOf("?") + 1) : null;
		myUrl = (myUrl.lastIndexOf("#") > -1) ? myUrl.slice(0, myUrl.lastIndexOf("#")) : myUrl;

		if (queryString != null && typeof queryString != 'undefined') {
			var queryVarsArray = queryString.split("&");
			for (var i = 0; i < queryVarsArray.length; i++) {
				var queryElement = unescape(queryVarsArray[i].split("=")[0]);

				if (queryElement.toLowerCase() == 'width') {
					var newWidth = queryVarsArray[i].split("=")[1];
				}
				if (queryElement.toLowerCase() == 'height') {
					var newHeight = queryVarsArray[i].split("=")[1];
				}
			}
		}
		// let's run through all possible values: 90%, nothing or a value in pixels
		if (newHeight != 0) {
			if (newHeight.indexOf('%') > -1) {
				newHeight = Math.floor(parseInt($(window).height()) * (parseInt(newHeight) / 100));
			}
			var newTop = Math.floor(parseInt($(window).height() - newHeight) / 2);
		} else {
			newHeight = 560;
		}
		if (newWidth != 0) {
			if (newWidth.indexOf('%') > -1) {
				newWidth = Math.floor(parseInt($(window).width() / 100) * parseInt(newWidth));
			}
			var newLeft = Math.floor(parseInt($(window).width() / 2) - parseInt(newWidth) / 2);
		} else {
			newWidth = 340;
		}

		$.fancybox( {
			'href'			: myUrl,
			'title'   		: myTitle
		}, {
			autoDimensions	: false,
			overlayShow		: true,
			centerOnScroll	: true,
			transitionIn	: 'elastic',
			transitionOut	: 'elastic',
			width			: parseInt(newWidth),
			height			: parseInt(newHeight),
			type			: 'iframe'
		});
		return false;
	});

	$("a.inline").click(function() {
		var $href = $(this).attr('href');
		var $title = $(this).attr('title');

		$.fancybox( {
			'href'			: $href,
			'title'   		: $title
		}, {
			autoDimensions	: true,
			overlayShow		: true,
			centerOnScroll	: true,
			transitionIn	: 'elastic',
			transitionOut	: 'elastic',
			onStart         : function() {
				$($href).show();
			},
			onCleanup : function() {
				$($href).hide();
			}
		});
		return false;
	});

	// Add a hint value to input boxes with a title attribute
	$("input[title!='']").hint();
});



/**
 * Some cheesy JS code to set a hidden input field because
 * IE doesn't handle the <button> tag like it is supposed to
 */
function setvalue(ElementID,ElementVal) {
	if(document.getElementById) {
		if(document.getElementById(ElementID)) {
			document.getElementById(ElementID).value = ElementVal;
		}
	}
}

/**
 * Sets opacity of an elelment
 */
function setOpacity(sEl,val) {
	oEl = document.getElementById(sEl);
	if(oEl) {
		oEl.style.opacity = val/10;
		oEl.style.filter = 'alpha(opacity=' + val*10 + ')';
	}
}

/**
 * Confirm dialog replacement
 */
/*function confirm(msg,callback) {
	$('#confirm')
		.jqmShow()
		.find('p.jqmConfirmMsg')
			.html(msg)
		.end()
		.find(':submit:visible')
			.click(function(){
				$('#confirm').jqmHide();
				if(this.value == 'yes')
					return true;
				else
					return false;
			});
}*/

/**
 * Alert dialog replacement
 */
/*function alert(msg) {
	$('#alert')
		.jqmShow()
		.find('div.jqmAlertContent')
		.html(msg);
}*/


/**
 * Input text hinting
 */
jQuery.fn.hint = function () {
	return this.each(function () {
		// get jQuery version of 'this'
		var t = jQuery(this);
		// get it once since it won't change
		var title = t.attr('title');
		// only apply logic if the element has the attribute
		if (title) {
			// on blur, set value to title attr if text is blank
			t.blur(function () {
				if (t.val() == '') {
					t.val(title);
					t.addClass('blur');
				}
			});
			// on focus, set value to blank if current value
			// matches title attr
			t.focus(function () {
				if (t.val() == title) {
					t.val('');
					t.removeClass('blur');
				}
			});

			// clear the pre-defined text when form is submitted
			t.parents('form:first-child').submit(function() {
				if (t.val() == title) {
					t.val('');
					t.removeClass('blur');
				}
			});

			// now change all inputs to title
			t.blur();
		}
	});
};

/**
 * Copyright (c) 2006-2007 Mathias Bank (http://www.mathias-bank.de)
 * Dual licensed under the MIT (http://www.opensource.org/licenses/mit-license.php) 
 * and GPL (http://www.opensource.org/licenses/gpl-license.php) licenses.
 * 
 * Version 2.1
 * 
 * Thanks to 
 * Hinnerk Ruemenapf - http://hinnerk.ruemenapf.de/ for bug reporting and fixing.
 * Tom Leonard for some improvements
 * 
 */
jQuery.fn.extend({
/**
* Returns get parameters.
*
* If the desired param does not exist, null will be returned
*
* To get the document params:
* @example value = $(document).getUrlParam("paramName");
* 
* To get the params of a html-attribut (uses src attribute)
* @example value = $('#imgLink').getUrlParam("paramName");
*/ 
	getUrlParam: function(strParamName){
		strParamName = escape(unescape(strParamName));

		var returnVal = new Array();
		var qString = null;

		if ($(this).attr("nodeName")=="#document") {
			//document-handler
			if (window.location.search.search(strParamName) > -1 ){
				qString = window.location.search.substr(1,window.location.search.length).split("&");
			}
		} else if ($(this).attr("src")!="undefined") {
			var strHref = $(this).attr("src");
			if ( strHref.indexOf("?") > -1 ) {
				var strQueryString = strHref.substr(strHref.indexOf("?")+1);
				qString = strQueryString.split("&");
			}
		} else if ($(this).attr("href")!="undefined") {
			var strHref = $(this).attr("href");
			if ( strHref.indexOf("?") > -1 ){
				var strQueryString = strHref.substr(strHref.indexOf("?")+1);
				qString = strQueryString.split("&");
			}
		} else {
			return null;
		}

		if (qString==null) return null;

		for (var i=0;i<qString.length; i++){
			if (escape(unescape(qString[i].split("=")[0])) == strParamName){
				returnVal.push(qString[i].split("=")[1]);
			}
		}

		if (returnVal.length==0) return null;
		else if (returnVal.length==1) return returnVal[0];
		else return returnVal;
	}
});


function swap(arg) {
	var parent = $(arg.parentNode);
	var viewport = '#' + parent.attr('id') + '-view';

	$('#hNavWrap ol li.selected').each(function() {
		var l = jQuery(this);
		l.removeClass('selected');
	});

	$('.viewport').each(function() {
		var v = jQuery(this);
		v.hide();
	    v.removeClass('showing');
	});

	parent.addClass('selected');
	$(viewport).show();
	$(viewport).addClass('showing');

	return false;
}


/**
 * Overlib defines for style
 */
var ol_width=220;
var ol_offsetx=15;
var ol_offsety=15;
var ol_hauto=1;
var ol_vauto=1;
var ol_fgclass='overlib_fg';
var ol_bgclass='overlib_border';
var ol_textfontclass='overlib_maintext';
var ol_captionfontclass='overlib_captiontext';
var ol_closefontclass='overlib_closetext';
