/*



Custom Functions
*/

var _debugMode = true;
document.observe('dom:loaded', init);
Event.observe(window, 'load', initAfterImages);


/*
This is the function that attached the Javascript functions to the page
*/
function init(){

	//createSlideShow();

	/*

	Makes Internet Explorer render 32 bit PNGs properly
	*/
	IE_PNG_Hack("PNGHack1");
	IE_PNG_Hack("PNGHack2");
	/*
	
	
	#PrimaryNav Drop Down show's the first ul child
	*/
	$$("#PrimaryNav li").each(function(node, i){
		if(node.getElementsBySelector("ul").length > 0){
			var ul = node.getElementsBySelector("ul")[0];
			node.observe(
				"mouseover",
				function(){
					node.getElementsBySelector("a")[0].toggleClassName("hover");
					node.toggleClassName("hover");
					ul.toggle();
				}
			);
			node.observe(
				"mouseout",
				function(){
					node.getElementsBySelector("a")[0].toggleClassName("hover");
					node.toggleClassName("hover");
					ul.toggle();
				}
			);
		}
	});

	if($("InventoryItemDetail")) {
		$$("ul#item_thumbs li a").each(function(node, i){
			node.observe("click",function(event) {
				$("item_image").update('<a href="'+node.title+'"><img src="'+node.href+'" border="0" align="left" width="220"></a><div class="clear"></div><p class="tiny">click for larger image.</p> ');
				event.stop();
			});
		});
	}


	/*
	
	
	
	Forms
	*/
	
		/*
		
		
		
		Clear the email input on the Newsletter form
		*/
		$$("form.newsletter input.newsletter_email").each(function(node, i){
	
			node = $(node);
	
			AlreadyReset = false;
			
			node.onclick = function(){
	
				if (AlreadyReset != true){
					node.value = "";
					AlreadyReset = true;
				}
	
			};
	
		});
}

function initAfterImages () {
	if($('slideshow')) {
		
		window.slideshow_start_frame 	= 1;
		window.slideshow_end_frame 		= $$('div.slide').length;
		window.slideshow_delay 			= 5000;
		window.slideshow_frame			= 1;
		window.slideshow_status			= true;
		
		// SLIDESHOW
		$$("ul#slideList li a").each(function(node, i){
			node.observe("click",function(event) {
				event.stop();
				var a = node.href.substr(node.href.lastIndexOf("#")+1,node.href.length);
				change_slide(a);
			});		
		});
		
		start_slideshow();
		
		$('slideshow_control').observe("click",function(event) {
			event.stop();
			if(window.slideshow_status) {
				pause_slideshow();
				window.slideshow_status = false;
			} else {
				resume_slideshow();
				window.slideshow_status = true;
			}
		});
		
	}
}






function pause_slideshow() {
	clearInterval(window.timer);
	$$('#slideshow_control a').each(function(node) {
		node.update('Play');
	});
}

function resume_slideshow() {
	var delay = window.slideshow_delay;
	$$('#slideshow_control a').each(function(node) {
		node.update('Pause');
	});
	window.timer = setInterval("switch_slides();", window.slideshow_delay);
}

function start_slideshow() {
	
	var start_frame = window.slideshow_start_frame;
	var end_frame 	= window.slideshow_end_frame;
	var delay 		= window.slideshow_delay;
	
	$('slideButton1').addClassName('activeSlide');
	$('slideshow').update($('slide1').innerHTML);
	
	window.slideshow_frame = start_frame+1;
	window.timer = setInterval("switch_slides();", delay);
	
}

function change_slide(frame) {
	window.slideshow_frame = frame;
	switch_slides();
}
                            
function switch_slides() {
					 
	var start_frame = window.slideshow_start_frame;
	var end_frame 	= window.slideshow_end_frame;
	var frame		= window.slideshow_frame;
	
	$('slideshow').update($('slide'+frame).innerHTML);
	$$('li.slidebutton').each(function(node) {
		node.removeClassName('activeSlide');
	});
	$('slideButton'+frame).addClassName('activeSlide');
	
	if (frame == end_frame) { window.slideshow_frame = (start_frame*1); } else { window.slideshow_frame = (window.slideshow_frame*1)+1; }
}







/*

This function makes 32 bit PNG's with transparency work
*/	
function IE_PNG_Hack(idorobject){
	if (navigator.userAgent.indexOf("MSIE ") == -1){
		return false;
	} else {
		var node = $(idorobject);
		if (node.src.indexOf("png") == -1) {
			return;
		}
		var oldSrc = node.src;
		node.src = "interface/images/transparent.gif";
		node.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + oldSrc + "',sizingMethod='scale')";
	}
}

function openImagePopup(url,width,height) {
	window.open(url,"imagewindow","location=0,status=0,scrollbars=0,width="+(width+20)+",height="+(height+30)); 
}


