var GM = window.GM || {};
GM.main = function(){
	return {
		init:function(){
			// main dropdowns
			var drop1 = YAHOO.util.Dom.get('drop1');
			var drop2 = YAHOO.util.Dom.get('drop2');
			YAHOO.util.Dom.setStyle(drop1, 'display', 'block');
			YAHOO.util.Dom.setStyle(drop2, 'display', 'block');
			var myMenu = new YAHOO.widget.MenuBar('menubar',{autosubmenudisplay:true,hidedelay:50, constraintoviewport:false});
			myMenu.render();
		 	myMenu.show();
			Shadowbox.init({displayCounter:false,continuous:true,animate:true, animSequence:"sync",resizeDuration:0.1, handleLgImages:"resize", onFinish:function(item){
				//make an async call to the server
				var myEl = YAHOO.util.Dom.get('shadowbox_content');
				var myURL = "";
				if(YAHOO.util.Dom.get('info')){
					myEl.parentNode.removeChild(YAHOO.util.Dom.get('info'));
				}
				if(!YAHOO.util.Dom.get('price_list')){
					myURL = 'includes/galleryDetail.php?id='+(myEl.src.substring(myEl.src.indexOf("_")+1,myEl.src.lastIndexOf("_")));
				} else {
					 myURL = 'includes/priceListDetail.php?id='+(myEl.src.substring(myEl.src.indexOf("_")+1,myEl.src.lastIndexOf("_")));
				}
				var myDiv = document.createElement('div');
				myDiv.id = "info";
				var myData = YAHOO.util.Connect.asyncRequest('GET', myURL, { success: function(o){myDiv.innerHTML = o.responseText}, failure: function(){myDiv.innerHTML = "the information could not be loaded."} });
				myEl.parentNode.appendChild(myDiv)
				YAHOO.util.Dom.setStyle(myDiv, 'zIndex', '1000');
				YAHOO.util.Dom.setStyle(myEl, 'left', '0');
				YAHOO.util.Dom.setStyle(mEl, 'top', '0');
				}
			});
			if(YAHOO.util.Dom.get('price_list')){
				var thumbs = YAHOO.util.Dom.getElementsByClassName('pl_thumb', 'img');
				for (var i=0; i < thumbs.length; i++) {
					var w = thumbs[i].offsetWidth;
					var h = thumbs[i].offsetHeight;
					if(h>w){
						YAHOO.util.Dom.setStyle(thumbs[i], 'height', '100%');
					} else {
						YAHOO.util.Dom.setStyle(thumbs[i], 'width', '100%');
					}
				};
			}	
		},
		slideshow: {
			properties:{
				speed:5000,
				animSpeed:3,
				isAnimating:false,
				container:""
			},
			init:function(slideshowProperties){
				// set properties
				this.properties.speed = slideshowProperties.speed;
				this.properties.animSpeed = slideshowProperties.animSpeed;
				this.properties.container = slideshowProperties.container;
				// set currslide
				this.currSlide = 0;
				// get the rotating image divs
				this.slides = YAHOO.util.Dom.getElementsByClassName('slide', 'div', 'slides');
				this.slideshow = YAHOO.util.Dom.get('slideshow');
				YAHOO.util.Dom.setStyle(this.slideshow, 'position', 'relative');
				for (var i=0; i < this.slides.length; i++) {
					YAHOO.util.Dom.setStyle(this.slides[i], 'opacity', '0');					
					YAHOO.util.Dom.setStyle(this.slides[i], 'position', 'absolute');					
					YAHOO.util.Dom.setStyle(this.slides[i], 'top', '0');					
				//	YAHOO.util.Dom.setStyle(this.slides[i], 'display', 'none');	

				};
				this.setSlide(this.currSlide);				
			},
			setSlide:function(slideIndex){
				//alert('setslide called');
				this.slideshow.appendChild(this.slides[slideIndex]);
				var that=this;
				var myAnim = new YAHOO.util.Anim(this.slides[slideIndex], {opacity:{from:0, to:.95}}, this.properties.animSpeed, YAHOO.util.Easing.easeOut);
				myAnim.onComplete.subscribe(function(){
					 //alert(slideIndex);
					setTimeout(function(){
					//	alert('timeout set');
						that.fadeOut(slideIndex);
					}, that.properties.speed);
				});
					myAnim.animate();
		},
			fadeOut:function(slideIndex){
				//alert('doh');
				var that = this;
				var myAnim = new YAHOO.util.Anim(this.slides[slideIndex], {opacity:{from:.95, to:0}}, this.properties.animSpeed, YAHOO.util.Easing.easeOut);
				var nextSlide = (slideIndex<this.slides.length-1)?slideIndex+1:0;
				if(YAHOO.util.Dom.getStyle(this.slides[slideIndex], 'opacity')>.5){
					that.setSlide(nextSlide);
				}
				myAnim.onComplete.subscribe(function(){
					that.slideshow.removeChild(that.slides[slideIndex]);
				});
			myAnim.animate();
			}
		}
	}
}();
// YAHOO.util.Event.onContentReady("page", GM.main.init);
YAHOO.util.Event.on(window, 'load', GM.main.init);
YAHOO.util.Event.onContentReady('slides', function(){
	GM.main.slideshow.init({animSpeed:1.5, speed:3500});
});
