/* 	document on load cue
============================================================= */

$(document).ready(function(){ 			// always load these function when the DOM is ready
	p80jq_zebra_table(); 				// alternating row colors for class tables.p80jq_zebra_table
	p80jq_src_hover();					// quick way of setting a swap image functionality on images with class img.p80jq_src_hover
	p80jq_element_restore_value();   	// swap a classname on inputfields input|textarea.p80jq_element_restore_value
	imageHover();						// hover for mouseovers textImage
	p80_fix_bgiframe();					// a fix for Win/IE6 when using div popups with form inputs behind it
	p80jq_tabs();						// easy tabs
	p80jq_toggle_billing_address_field();
});

/* 	jQuery alternating row colors (zebra_stripe_tables) 
	adds a 'odd' or 'even' classnames on all tables which have a classname of 'p80_zebra_table'
============================================================= */

function p80jq_zebra_table() {
	$("table.p80jq_zebra_table tbody tr").each(function(index) {	// to all tr-tags
		$(this).addClass(index % 2 == 0 ? "odd" : "even");			// add an 'odd' or 'even' classname
	});
}

/* 	jQuery rollovers on elements with a src-attribute
	on rollover 'demo.gif' becomes 'demo_hover.gif'
============================================================= */

function p80jq_src_hover() {
	$(".p80jq_src_hover").hover(function() { 									// classname to trigger
		var filename = $(this).attr("src").replace(/\.(\w+)$/, "_hover.$1");	// mouse-over: add the filename suffix
		$(this).attr("src", filename);
	}, function() {
		var filename = $(this).attr("src").replace(/_hover\.(\w+)$/, ".$1");	// mouse-out: restore, remove filename suffix
		$(this).attr("src", filename);
	});
}

/* 	jQuery rollovers on elements with a src-attribute
	on rollover 'demo.gif' becomes 'demo_hover.gif'
============================================================= */

function imageHover() {
	$(".imageHover").hover(function() { 									// classname to trigger
		var filename = $(this).attr("src").replace(/(\/textimage\/)/, "$1hover/");	// mouse-over: add the filename suffix
		$(this).attr("src", filename);
	}, function() {
		var filename = $(this).attr("src").replace(/(\/textimage\/)hover\//, "$1");	// mouse-out: restore, remove filename suffix
		$(this).attr("src", filename);
	});
}

/* 	jQuery FAQ like expanding list
============================================================= */

$(document).ready(function(){									// body.onload()
	var aExpandingList = "dl.p80jq_expandinglist"; 				// dl-tag classname to trigger
	$(aExpandingList+" dt").click(function () { 				// add click function to dt-tags
		if($(this).hasClass('open')) {							// check to see if item is already open
			$(this).removeClass('open'); 						// remove the classname 'open' on all dt-tags
			$(this).next('dd').slideUp('fast');					// animate the dd-tag when clicked
		} else {
			$(aExpandingList+" dd:visible").slideUp('fast'); 	// hide the dd-tag
			$(aExpandingList+" dt").removeClass('open'); 		// remove the classname 'open' on all dt-tags
			$(this).toggleClass('open'); 						// toggle the classname 'open' on dt-tags
			$(this).next('dd').slideDown('fast'); 				// animate the dd-tag when clicked
		}
	});
});

/*	Restore defaultValue on form elements onblur
============================================================= */

function p80jq_element_restore_value() {
	$("input:text.p80jq_element_restore_value, textarea.p80jq_element_restore_value").each(function() {
		$(this).attr('p80_org_formvalue', $(this).val());
		$(this).addClass('p80_form_label');
	})
	.focus(function () {
		if($(this).val() != '' && $(this).val() == $(this).attr('p80_org_formvalue')) {
			$(this).val('');
			$(this).removeClass('p80_form_label');
		}					
	 })
	.blur(function() {
		if($(this).val() == '') {
			$(this).val($(this).attr('p80_org_formvalue'));
			$(this).addClass('p80_form_label');
		}
	});
}

/*	Thumbnail list on template index list
============================================================= */

function p80jq_toggle_thumbs(){
	$("img.index_thumb").each(function() {					// img-tags with 'index_thumb' classnames
		$(this).toggleClass("index_thumb_show"); 			// toggle the classname
	});
}

/*	Return a key/value array of the URL query-string
============================================================= */

function p80_get_query_string() {
	var querystring = new Array; 
	// parse current url into an array with the keys/values
	var q = String (document.location).split ('?')[1];
	// return if there is no url query string
	if (!q) return false;
	q = q.split ('&');	
	for (var i = 0 ; i < q.length; i++)	{
		// for each key/value, split them at the '='
		// and add them to the qerystring array
		var o = q[i].split('=');		
		// create the array and url decode the string
		querystring[o[0]] = unescape(o[1]);
	}	
	// return the querystring array
	return querystring;
}

/*	Win/IE6 BGiFrame fix
============================================================= */

function p80_fix_bgiframe() {
	if ($.browser.msie && $.browser.version.substr(0,1)<7) { // Check for Win/IE6 and lower
		$('.p80popup').bgiframe(); // Add bgiFrame fix to popup div's
	}	
}

/*	jQuery tabs
============================================================= */

function p80jq_tabs() {
	$("ul.p80jq_tabs").tabs("div.p80jq_tab_panes > div"); // bind the ul li-tags to div panes and use them as tabs
}

/*	Toggle visibility of billing address field (T24)
============================================================= */

function p80jq_toggle_billing_address_field() {
//	$("#billingAddress").hide();
	$("#jqToggleBillingAddressField").click(function(){
		$("#billingAddress").toggle();
	});
}
