var MyUtils = {
    truncate: function(element, length){
        element = $(element);
        return element.update(element.innerHTML.truncate(length));
    }
}

Element.addMethods(MyUtils);


function default_behaviors(){
	

	Event.observe(window,"load",function(){

		//logo to homepage
		if ($("logo")){
			Event.observe($("logo"),"click",function(){
				window.location.href=webroot+"/products/pen";
			})
		}
		
		
		//show/hide product boxes
		$$("div.collapse").each(function(e){
			e.select("span.show").each(function(s){
				Event.observe(s,"click",function(){
					e.select("div.expand").each(function(x){
						x.setStyle({display:"block"});
							e.select("*.toggle").each(function(t){
								t.toggle();
							})
					})
				})
			});
			
			e.select("span.hide").each(function(s){
				Event.observe(s,"click",function(){
					e.select("div.expand").each(function(x){
						x.setStyle({display:"none"});
						e.select("*.toggle").each(function(t){
							t.toggle();
						})
					})
				})
			})
		});
		
		//slideshow
		if ($("slide-content") && ($("slide-content").select("img").size()>1)){
			
			if ($("gallery-arrows")){
				Event.observe($("gallery"),"mouseover",function(){
					$("gallery-arrows").show();
				})

				Event.observe($("gallery"),"mouseout",function(){
					$("gallery-arrows").hide();
				})
				
			}
			

			
			var my_glider = new Glider('my-glider', {duration:0.5});
			var slideWidth = 1;		
			
			
			//this function is a callback to hide the prev / next arrows where there are no more imgs in that direction
			var checkArrows = function(){
				if (!my_glider.current){
					my_glider.current = $("slide-content").select("img").first();
				}
				$("gallery-arrows").select("div.next").first().show();
				$("gallery-arrows").select("div.prev").first().show();
				$("gallery-arrows").select("div.prev").first().setStyle("padding-right:0px");
				

				if (($("slide-content").select("img").first()) && my_glider.current ==$("slide-content").select("img").first()){
					$("gallery-arrows").select("div.next").first().show();
					$("gallery-arrows").select("div.prev").first().hide();
				}
				if (($("slide-content").select("img").first()) && my_glider.current ==$("slide-content").select("img").last()){
						$("gallery-arrows").select("div.next").first().hide();
						$("gallery-arrows").select("div.prev").first().show();
						$("gallery-arrows").select("div.prev").first().setStyle("padding-right:18px");
						
				}
		
			}
			
			//call this to on load to hide the previous slide arrow
			checkArrows();
			
			if ($("slide-content").select("img").size() >1){
				$("slide-content").select("img").each(function(e){
					slideWidth += e.getWidth() + 0;
				});
				$("slide-content").setStyle({width:slideWidth+"px"});
			}
			
			if ($("gallery-arrows")){
			$("gallery-arrows").select("div.prev").each(function(e){
				Event.observe(e,"click",function(){
					my_glider.previous();
					checkArrows();
				})
			})

			$("gallery-arrows").select("div.next").each(function(e){
				Event.observe(e,"click",function(){
					my_glider.next();
					checkArrows();
				})
			})
		}



		}

		if ($("slide-content") && $("upcoming-products-menu")){
			var img_links = null;
			$("slide-content").select("img").each(function(e,i){
				var l = new Element("a");
				l.innerHTML = (i+1);
				$("upcoming-products-menu").insert(l);
				Event.observe(l,"click",function(){
					my_glider.moveTo(my_glider.sections[i],my_glider.wrapper.down("div.scroller"),{duration:0.5})
					checkArrows();
					})
				l.setStyle({cursor:"pointer",paddingRight:"8px"});
			})
		}


		//red highlight for top links
		$("header").select("a").each(function(e){
			if (e.readAttribute("href")==window.location.href){
				e.setStyle({color:"#ee2929"});
			}
		})


		//red highlight for nav links
		$("left-nav").select("a").each(function(e){
			if (e.readAttribute("href")==window.location.href){
				e.setStyle({color:"#ee2929"});
			}
		})



		function mailingListSubmit(el){
			var mform = el;
			new Ajax.Request(webroot+"customers/mailinglist_add", {
			  method: 'post',
			  evalScripts:true,
			  parameters:el.serialize(true),
			  onLoading: function(transport){
				el.update("Processing...");
				},
			  onSuccess: function(transport) {
				el.update(transport.responseText);
				}
			})
		}
		
		//ajax mailing list
		$$("*.mailing-list-button").each(function(e){
			Event.observe(e,"click",function(){
				e.up(2).select("form").each(function(el){
					el.select("input").each(function(elt){
						if(elt.value.split("@").size() > 1){
							//we have a valid email address, do ajax sumit
							mailingListSubmit(el);
						}
					})
				})
			})
		})
			
		//mailing list button states
			$$("*.mailing-list-button").each(function(e){
				Event.observe(e,"mousedown",function(){
					e.src = domain+"/img/mailsubmit_onclick.png";
				});
				Event.observe(e,"mouseup",function(){
					e.src = domain+"/img/mailsubmit_onRollover.png";
				});
				Event.observe(e,"mouseover",function(){
					e.src = domain+"/img/mailsubmit_onRollover.png";
				});
				Event.observe(e,"mouseout",function(){
					e.src = domain+"/img/submit-button.png";
				});	
			});

		$$("*.mailing-list-input").each(function(e){
			Event.observe(e,"focus",function(){
				e.value=null;
			});
			Event.observe(e,"keypress",function(el){
				if(el.keyCode==13){
					
				}
				return false;
			})
		})

		//disable submit on return key
		Event.observe(document,"keypress",function(e){
			if (e.keyCode==13){	
				e.stop();					
				return false;
			}
		});	
	
		
		
	

		//scroll arrows
		if ($("scroll-up")){
			var sliderTop = 0;
			var sliderHeight = $("handle1").getHeight();
			var trackHeight = $("track1").getHeight();
			var scrollDistance = $("scrollable1").getHeight() / 20;
			var sliderDistance = (trackHeight-sliderHeight) / 20;

		     Event.observe($("scroll-up"),
			'click',function(){ 
				var t = $("scrollable1").scrollTop;
				$("scrollable1").scrollTop = t - scrollDistance;
				sliderTop = sliderTop -sliderDistance;
				if (sliderTop <0) sliderTop = 0;
				$("handle1").setStyle({top:sliderTop+"px"});
				});  
		}

		if ($("scroll-down")){
		     Event.observe($("scroll-down"),
			'click',function(){ 
				var t = $("scrollable1").scrollTop;
				$("scrollable1").scrollTop = t + scrollDistance;
				
				if ((sliderTop + sliderHeight) > trackHeight){
					sliderTop = sliderTop;
				} else {
					sliderTop = sliderTop + sliderDistance;
				}
				
				var t = $("handle1").setStyle({top:sliderTop+"px"});
				})
		}


		if ($("track1")){
			// vertical slider control
			var slider1 = new Control.Slider('handle1', 'track1', {
				axis: 'vertical',
				onSlide: function(v) { scrollVertical(v, $('scrollable1'), slider1);  },
				onChange: function(v) { scrollVertical(v, $('scrollable1'), slider1); }
			});


			// scroll the element vertically based on its width and the slider maximum value
			function scrollVertical(value, element, slider) { 
				element.scrollTop = Math.round(value/slider.maximum*(element.scrollHeight-element.offsetHeight));
			}

			// disable vertical scrolling if text doesn't overflow the div
			if ($('scrollable1').scrollHeight <= $('scrollable1').offsetHeight) {
				slider1.setDisabled();
				$('track1').hide();
				$('scroll-up').hide();
				$('scroll-down').hide();
			}
			
		}
		


	//pen comparison
	if ($("learn-more")){
		$("learn-more").select("div.panel").each(function(e){e.hide()});
		
		$("learn-more").select("div.panel").first().show();

		$("learn-more").select("div.menu-item").each(function(e){
			var f = e.select("span").first();
			Event.observe(f,"mouseover",function(){
				f.addClassName("over");
			})
			Event.observe(f,"mouseout",function(){
				f.removeClassName("over");
			})
			Event.observe(f,"click",function(){
				$("learn-more").select("div.menu-item").each(function(el){el.select("span").first().removeClassName("active")});
				f.addClassName("active");
				
				//highlight parent - if the container div is a "menu item" highlight that one as well
				if (e.up("div").hasClassName("menu-item")) {
					e.up("div").select("span").first().addClassName("active");
				}
				
				//if parent is clicked, highlight first child
				if ((e.select("div").first()) &&  e.select("div").first().hasClassName("menu-item")){
					e.select("div.menu-item").first().select("span").first().addClassName("active");
				}
				
				$("learn-more").select("div.panel").each(function(p){
					p.hide();
				})
				$("learn-more").select("div.panel").each(function(p){
					if (p.readAttribute("rel")==f.readAttribute("rel")){
						p.show();
					}
				})

			})
		});
	}


	if ($("gallery") && ($("gallery").select("img").size()>0)){
		$("gallery").select("img").each(function(e){
			e.setStyle({margin:"0px",padding:"0px",borderStyle:"none"});
		})
	}
	//end window onload
	

	})
}







