/*=============================================================================

			 	 TITLE:		Rocky Top - Core JavaScript Utilities
		  MODIFIED:		2006.08.24
		 AUTHOR(S): 	Graham Wheeler - NetMediaOne - www.netmediaone.com
		  REQUIRES:		Prototype 1.5.0 ( www.conio.com )
									Scriptaculous 1.6.1
									YAHOO! Event Utility

=============================================================================*/

var RT = window.RT || {

	version: "1.0.0",
	isIE: /MSIE/.test( navigator.userAgent.toUpperCase() ),
	ddMenus: $A( new Array() ),
	mouseX: 0,
	mouseY: 0,

	init: function() {
		Event.observe( document, "mousemove", RT.onMouseMove, false );
		var fc = $$("#subNav li").first();
		if ( fc != null ) {
			fc.style.background = "none";
			fc.style.padding = "0px";
		}
		
		$$("#subNav .Active").each( function(li) {
			li.parentNode.style.display = "block";
		} );
		
		$$(".DropDownMenu").each( function(m) {
			RT.ddMenus.push( new RT.DropDownMenu(m) );
		} );
		
		$$(".PreviewImage").each( function(img) {
			img.parentNode.setAttribute( "defaultImage", img.src );
		} );
		
		$$(".ProductThumbnail").each( function(a) {
			YAHOO.util.Event.addListener( a, "mouseover", RT.showProductPreviewImage, a, true );
			YAHOO.util.Event.addListener( a, "mouseout", RT.showProductDefaultImage, a, true );
		} );
		
		$$(".ImageGallery").each( function(ig) {
			var caption = ig.getAttribute("caption");
			$A( ig.getElementsByTagName("img") ).each( function(img) {
				img.setAttribute( "title", "Click for large photo" );
				Event.observe( img, "click", function(e) {
					var url = "photo-viewer.aspx?img=" + this.src.replace( "_m.", "_l." ) + "&caption=" + caption;
					var picViewer = window.open( url, "", "toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width=640,height=560,left=" + ((screen.width - 640) / 2) + ",top=" + ((screen.height - 560) / 2) );
					picViewer.focus();
				}.bind(img), false );
			} );
		} );
		
		$$(".TexturePreview").each( function(ig) {
			var caption = ig.getAttribute("caption");
			$A( ig.getElementsByTagName("img") ).each( function(img) {
				img.setAttribute( "title", "Click for large photo" );
				Event.observe( img, "click", function(e) {
					var url = "photo-viewer.aspx?img=" + this.src.replace( "_m.", "_l." ) + "&caption=" + caption;
					var picViewer = window.open( url, "", "toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width=540,height=600,left=" + ((screen.width - 540) / 2) + ",top=" + ((screen.height - 600) / 2) );
					picViewer.focus();
				}.bind(img), false );
			} );
		} );

	},
	
	onMouseMove: function(e) {
		RT.mouseX = Event.pointerX(e);
		RT.mouseY = Event.pointerY(e);
	},

	imgOn: function( img ) {
		$(img).src = $(img).src.replace( "_off", "_on" );
	},
	
	imgOff: function( img ) {
		$(img).src = $(img).src.replace( "_on", "_off" );
	},
	
	showProductPreviewImage: function() {
		var img = this.getElementsByTagName("img")[0];
		var container = RT.findParentByClassName( this, "ProductInfoBox" );
		var previewImg = container.getElementsByTagName("img")[0];
		previewImg.src = img.src.replace( "_s.", "_m." );
	},
	
	showProductDefaultImage: function() {
		var container = RT.findParentByClassName( this, "ProductInfoBox" );
		var previewImg = container.getElementsByTagName("img")[0];
		previewImg.src = container.getAttribute("defaultImage");
	},

	showLargeProductImage: function(aTag, caption) {
		var img = aTag.getElementsByTagName("img")[0].src.replace( "_s.", "_l." );
		var url = "photo-viewer.aspx?img=" + encodeURIComponent(img) + "&caption="+ encodeURIComponent(caption);
		var picViewer = window.open( url, "", "toolbar=no,location=no,directories=no,status=no,scrollbars=no,resizable=no,copyhistory=no,width=540,height=620,left=" + ((screen.width - 540) / 2) + ",top=" + ((screen.height - 620) / 2) );
		picViewer.focus();
		return false;
	},
	
	findParentByClassName: function( el, className ) {
		var foundEl = "NULL";
		if ( Element.hasClassName( el.parentNode, className ) ) {
			foundEl = el.parentNode;
		}	else {
			foundEl = RT.findParentByClassName( el.parentNode, className );
		}
		return foundEl;
	},
	
	//
	// getPageScroll()
	// Returns array with x,y page scroll values.
	// Core code from - quirksmode.org
	//
	getPageScroll: function() {
	
		var yScroll;
	
		if (self.pageYOffset) {
			yScroll = self.pageYOffset;
		} else if (document.documentElement && document.documentElement.scrollTop){	 // Explorer 6 Strict
			yScroll = document.documentElement.scrollTop;
		} else if (document.body) {// all other Explorers
			yScroll = document.body.scrollTop;
		}
	
		arrayPageScroll = new Array('',yScroll) 
		return arrayPageScroll;
	},
	
	// -----------------------------------------------------------------------------------
	
	//
	// getPageSize()
	// Returns array with page width, height and window width, height
	// Core code from - quirksmode.org
	// Edit for Firefox by pHaez
	//
	getPageSize: function() {
		
		var xScroll, yScroll;
		
		if (window.innerHeight && window.scrollMaxY) {	
			xScroll = document.body.scrollWidth;
			yScroll = window.innerHeight + window.scrollMaxY;
		} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
			xScroll = document.body.scrollWidth;
			yScroll = document.body.scrollHeight;
		} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
			xScroll = document.body.offsetWidth;
			yScroll = document.body.offsetHeight;
		}
		
		var windowWidth, windowHeight;
		if (self.innerHeight) {	// all except Explorer
			windowWidth = self.innerWidth;
			windowHeight = self.innerHeight;
		} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
			windowWidth = document.documentElement.clientWidth;
			windowHeight = document.documentElement.clientHeight;
		} else if (document.body) { // other Explorers
			windowWidth = document.body.clientWidth;
			windowHeight = document.body.clientHeight;
		}	
		
		// for small pages with total height less then height of the viewport
		if(yScroll < windowHeight){
			pageHeight = windowHeight;
		} else { 
			pageHeight = yScroll;
		}
	
		// for small pages with total width less then width of the viewport
		if(xScroll < windowWidth){	
			pageWidth = windowWidth;
		} else {
			pageWidth = xScroll;
		}
	
		arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight) 
		return arrayPageSize;
	}

};

RT.DropDownMenu = Class.create();
RT.DropDownMenu.prototype = {

	initialize: function(elementRef) {
		this.HIDE_DELAY = 400;
		this.hideTimer = "";
		this.hasFocus = false;
		this.container = $(elementRef);
		this.boundTo = $( this.container.getAttribute("bindto") );
		this.shadow = document.createElement("iframe");
		Element.addClassName( this.shadow, "DropDownMenuShadow");
		this.shadow.setAttribute( "frameborder", "0" );
		this.shadow.setAttribute( "scrolling", "no" );
		this.shadow.setAttribute( "src", "menushadow.html" );
		$("layoutWrapper").appendChild(this.shadow);
		Element.setOpacity( this.shadow, .40 );
		this.container.style.width = Number( this.container.getAttribute("menuwidth") || 160 ) + "px";
		this.leftOffset = Number( this.container.getAttribute("leftoffset") || 0 );
		this.topOffset = Number( this.container.getAttribute("topoffset") || 30 );		
		if ( this.boundTo != null ) {
			YAHOO.util.Event.addListener( this.boundTo, "mouseover", this.show, this, true );
		}
		this.group = this.container.getAttribute("menugroup") || "default";
	},
	
	think: function() {
		clearTimeout( this.hideTimer );
		if ( Position.within( this.container, RT.mouseX, RT.mouseY ) || Position.within( this.boundTo, RT.mouseX, RT.mouseY ) ) {
			this.hideTimer = setTimeout( function() { this.think(); }.bind(this), this.HIDE_DELAY );
		} else {
			this.hide();
		}
	},
	
	show: function() {
		RT.ddMenus.each( function(m) {
			if ( m.group == this.group && m.container.id != this.container.id ) {
				m.hide();
			}
		}.bind(this) );
		this.container.style.display = "block";
		this.shadow.style.display = "block";
		if ( this.boundTo != null ) {
			Position.clone( this.boundTo, this.container, { setWidth: false, setHeight: false, offsetTop: this.topOffset, offsetLeft: this.leftOffset } );
			Position.clone( this.container, this.shadow, { offsetTop: 5, offsetLeft: 5 } );
		}
		this.hasFocus = true;
		this.hideTimer = setTimeout( function() { this.think(); }.bind(this), this.HIDE_DELAY );
	},
	
	hide: function() {
		this.hasFocus = false;
		this.shadow.style.display = "none";
		this.container.style.display = "none";
	}

};

Event.observe( window, "load", RT.init, false );
