// Javascript Document

var Inlays = {
	
	current_event : Number,
	current_menu : Object,
	current_submenu : Object,
	current_series : Object,
	current_sku : String,
	events : Array,
	IE : Boolean,
	zoom : Object,
	zoomint : Object,
	zoomshowing : Boolean,
	
	init : function()
	{
		Inlays.current_menu = $('rollover_'+$('current_slug').value.toLowerCase());
		Inlays.current_submenu = $('rollover_'+$('current_subslug').value);
		if($('current_series')) Inlays.current_series = $('heading_'+$('current_series').value);
		Inlays.zoom = $('zoom');
		Inlays.current_sku = "";
		
		Inlays.IE = (BrowserDetect.browser=="Explorer")?true:false;
		
		$$('a.hover').each( function(a)
		{
			Event.observe( a, 'mouseover', Inlays.show_submenu_hover )
			Event.observe( a, 'mouseout', Inlays.hide_submenu_hover )
		})
		
		// expand and contract sidemenu
		$$('h3.expand').each( function( h3 )
		{
			Event.observe( h3, 'mouseup', Inlays.expand_menus )
		})
		
		// expand and contract sidemenu
		$$('h4.expand').each( function( h4 )
		{
			Event.observe( h4, 'mouseup', Inlays.expand_submenus )
		})
		
		// expand dropdowns (i.e. FAQ's)
		$$('a.expand').each( function(expand)
		{
			expand.onclick = function()
			{
				$(this.rel).toggle();
				this.ancestors()[0].toggleClassName('expanded');
				return false;
			}
		});
		
		// toggle between listed and tiled views
		$$('a[rel^=tile]','a[rel^=list]').each(function(btn){ btn.onclick = function(){ Inlays.change_view(this); return false; }});
		
		// scroll timeline on homepage
		if($('billboard'))
		{
			new Ajax.Request('/ajax/get_events/',
			{
				method:'post',
				onSuccess: function(transport){
			    	var response = transport.responseText || "no response text";
					Inlays.current_event = 0;
					Inlays.events = JSON.parse(response);
			    	//alert("Success! \n\n" + events.length);
					$('headline').hide();
					$('event_name').update(Inlays.events[0].event_name);
					new Effect.Appear( 'headline', {duration:0.2} );
					if(Inlays.events.length > 1) new PeriodicalExecuter(Inlays.display_next_event, 5);
				},
				onFailure: function(){ alert('Something went wrong...') }
			});
		}
		
		// search buttons
		$$('a[rel=search]').each(function(btn)
		{
			Event.observe( btn, 'click', Inlays[btn.id+'_search'] );
		});
		
		// rollover zoom images
		$$('img.zoom').each(function(img)
		{
			Event.observe( img, 'mouseover', Inlays.pause_before_zoom );
			Event.observe( img, 'mouseout', Inlays.zoom_out );
		});
		
		if($('zoom'))
		{
			$('zoom').onclick = function()
			{ 
				Inlays.hide_zoom();
			}
		}
	},
	
	show_submenu_hover : function()
	{
		if(!this.placed_image)
		{
			var sku = this.rel.split('|')[0];
			var w = this.rel.split('|')[1];
			var img = Builder.node('img',{src:'/images/products/tiny/'+sku+'.jpg', width:w+'px'});
			this.firstDescendant().firstDescendant().insert({top:img});
			this.placed_image = true;
		}
		this.firstDescendant().show();
	},
	
	hide_submenu_hover : function()
	{
		this.firstDescendant().hide();
	},
	
	pause_before_zoom : function()
	{
		//new PeriodicalExecuter(pollChatRoom, 3);
		var img = this;
		clearTimeout(Inlays.zoomint);
		Inlays.zoomint = setTimeout( function(){ Inlays.show_zoom(img) },250 );
		//Event.observe( this,'mouseout',Inlays.cancel_zoom );
	},
	
	cancel_zoom : function()
	{
		//alert("cancel_zoom");
		clearTimeout(Inlays.zoomint);
	},
	
	show_zoom : function(img)
	{
		var hit = img;
		var sku = hit.id.split("_")[1];
		if(sku != Inlays.current_sku)
		{
			Inlays.zoom.setOpacity(0);
		
			Inlays.current_sku = sku;
			new Ajax.Request('/ajax/get_zoom',
			{
		    	method:'post',
				parameters: { sku: sku },
			    onSuccess: function(transport){
			    	var response = transport.responseText || "no response text";
					var json = JSON.parse(response);
				
					var imgPreloader = new Image();
					imgPreloader.onload = function()
					{
						Inlays.zoom.src = "/images/products/medium/"+sku+".jpg";
					
						var leftpos = ((hit.cumulativeOffset()[0]+hit.getWidth()+json.w)>document.viewport.getWidth()) ? (hit.cumulativeOffset()[0] - json.w) : (hit.cumulativeOffset()[0]+hit.getWidth()-10);
						Inlays.zoom.setStyle(
						{
							width: json.w+"px",
							height: json.h+"px",
							left: leftpos+"px",
							top: hit.cumulativeOffset()[1]+((hit.getHeight()-json.h)*0.5)+"px"
						});
						new Effect.Appear(Inlays.zoom, { duration:0.2, delay:0.15 });
					
					}
					imgPreloader.src = "/images/products/medium/"+sku+".jpg";
			    },
			    onFailure: function(){ alert('Something went wrong...') }
		  	});
		}
	},
	
	zoom_out : function()
	{
		//alert("zoom_out");
		clearTimeout(Inlays.zoomint);
		Inlays.zoomint = setTimeout(Inlays.hide_zoom,1000);
	},
	
	hide_zoom : function()
	{
		//alert("hide_zoom");
		Inlays.zoom.hide();
		Inlays.current_sku = "";
	},
	
	guided_search : function()
	{
		$('sku_form').hide();
		if(Inlays.IE)
		{
			$('guided_form').show();
		}
		else
		{
			new Effect.BlindDown('guided_form', {duration:0.3});
		}
		
	},
	
	sku_search : function()
	{
		$('guided_form').hide();
		if(Inlays.IE)
		{
			$('sku_form').show();
		}
		else
		{
			new Effect.BlindDown('sku_form', {duration:0.3});
		}
	},
	
	display_next_event : function()
	{
		$('headline').hide();
		$('event_name').update(Inlays.events[ ++Inlays.current_event%Inlays.events.length].event_name);
		new Effect.Appear( 'headline', {duration:0.2} );
	},
	
	change_view : function(btn)
	{
		Inlays.zoom.hide();
		Inlays.current_sku = "";
		
		var v = btn.rel.split("_")[0];
		var s = btn.rel.split("_")[1];
		switch(v)
		{
			case "tile":
			//$('series_'+s).addClassName('tile').removeClassName('list');
			$$('ul[id^=series_]').each(function(ul){ ul.addClassName('tile').removeClassName('list') });
			$$('a[id^=tilebtn_]').each(function(a){ a.removeClassName('inactive'); });
			$$('a[id^=listbtn_]').each(function(a){ a.addClassName('inactive'); });
			break;
			
			case "list":
			//$('series_'+s).addClassName('list').removeClassName('tile');
			$$('ul[id^=series_]').each(function(ul){ ul.addClassName('list').removeClassName('tile') });
			$$('a[id^=tilebtn_]').each(function(a){ a.addClassName('inactive'); });
			$$('a[id^=listbtn_]').each(function(a){ a.removeClassName('inactive'); });
			break;
		}
		Inlays.set_cookie(v);
		btn.scrollTo();
		return false;
	},
	
	expand_menus : function()
	{
		// close all submenus
		if(Inlays.current_submenu)
		{
			if(Inlays.IE)
			{
				$('submenu_'+Inlays.current_submenu.id.split("_")[1]).hide();
			}
			else
			{
				new Effect.BlindUp( 'submenu_'+Inlays.current_submenu.id.split("_")[1], { duration:0.4 });
			}
		}
		Inlays.current_submenu = null;
		
		if( Inlays.current_menu != this )
		{
			var old_code;
			var new_code = this.id.split("_")[1];
			if(Inlays.current_menu) old_code = Inlays.current_menu.id.split("_")[1];
		
			// collapse old menu
			if(old_code)
			{
				if(Inlays.IE)
				{
					$( 'menu_'+old_code ).hide();
				}
				else
				{
					new Effect.BlindUp( 'menu_'+old_code, { duration:0.4 });
				}
				Inlays.current_menu.removeClassName('minus');
				Inlays.current_menu.addClassName('plus');
			}
			
			// expand new menu
			Inlays.current_menu = this;
			if(Inlays.IE)
			{
				$('menu_'+new_code).show();
			}
			else
			{
				new Effect.BlindDown( 'menu_'+new_code, { duration:0.4 });
			}
			Inlays.current_menu.removeClassName('plus');
			Inlays.current_menu.addClassName('minus');
			
		}
		else
		{
			if(Inlays.IE)
			{
				$( 'menu_'+Inlays.current_menu.id.split("_")[1] ).hide();
			}
			else
			{
				new Effect.BlindUp( 'menu_'+Inlays.current_menu.id.split("_")[1], { duration:0.4 });
			}
			Inlays.current_menu.removeClassName('minus');
			Inlays.current_menu.addClassName('plus');
			Inlays.current_menu = null;
		}
	},
	
	expand_submenus : function()
	{
		if( Inlays.current_submenu != this )
		{
			var old_code;
			var new_code = this.id.split("_")[1];
			if(Inlays.current_submenu) old_code = Inlays.current_submenu.id.split("_")[1];
		
			// collapse old menu
			if(old_code)
			{
				if(Inlays.IE)
				{
					$( 'submenu_'+old_code ).hide();
				}
				else
				{
					new Effect.BlindUp( 'submenu_'+old_code, { duration:0.4 });
				}
				Inlays.current_menu.removeClassName('minus');
				Inlays.current_menu.addClassName('plus');
			}
			
			// expand new menu
			Inlays.current_submenu = this;
			if(Inlays.IE)
			{
				$( 'submenu_'+new_code ).show();
			}
			else
			{
				new Effect.BlindDown( 'submenu_'+new_code, { duration:0.4 });
			}
			Inlays.current_submenu.removeClassName('plus');
			Inlays.current_submenu.addClassName('minus');
			
		}
		else
		{
			if(Inlays.IE)
			{
				$( 'submenu_'+Inlays.current_submenu.id.split("_")[1] ).hide();
			}
			else
			{
				new Effect.BlindUp( 'submenu_'+Inlays.current_submenu.id.split("_")[1], { duration:0.4 });
			}
			//Inlays.current_menu.removeClassName('minus');
			//Inlays.current_menu.addClassName('plus');
			Inlays.current_submenu = null;
		}
	},
	
	css_validation : function(form)
		{
		if(!form) form = document.forms[0];
		var submit_it = true;
		for( i=0;i<form.length;i++ )
		{
			if( form[i].className && form[i].className.indexOf('REQUIRED') > -1 )
			{
				form[i].oldclass = form[i].parentNode.parentNode.className;
				if( form[i].value == '' )
				{
					form[i].parentNode.parentNode.className = form[i].oldclass+" error";
					submit_it = false;
				}
				else
				{
					while(form[i].parentNode.parentNode.className.indexOf(" error")>-1)
					{
						form[i].parentNode.parentNode.className = form[i].parentNode.parentNode.className.replace(" error","");
					}
				}
			}
		}
		if( !submit_it )
		{
			$('errormsg').scrollTo().innerHTML = '<strong>Please review the highlighted fields and resubmit.</strong>';
		}
		return submit_it;
	},

	set_cookie : function(v)
	{
		var expiredays = 60;
		var exdate=new Date();
		exdate.setDate(exdate.getDate()+expiredays);
		document.cookie="view =" +escape(v)+((expiredays==null) ? "" : ";path=/;expires="+exdate.toGMTString());
	}
}

Event.observe( window, 'load', Inlays.init );