var HELPERS =
{   
	
	popupprefs : [],
	linkPopups : function(popupClass, popupOptions)
	{
		if(typeof popupClass.href == "string"){
			var ops = popupOptions;
			popupClass.onclick = function(){
				var mywin=open(this.href, 'XYZ', ops);
				this.blur();
				return false;
			}
		} else {
			HELPERS.popupprefs[popupClass] = popupOptions
			$ES('a').each(function(pl){
				if(pl.hasClass(popupClass)){
					pl.onclick = HELPERS.launchPopup;
				}
			})
		}
	},

	launchPopup : function(){
		var  i, prefs;
		prefs = "";
		// cycle through class names and match to popupprefs
		// this enables multiple classes to be applied to an anchor
		this.className.split(' ').each(function(cn){
			if(typeof HELPERS.popupprefs[cn] == "string"){
				prefs = HELPERS.popupprefs[cn];
			}
		})
		mywin=open(this.href, 'Actuate', prefs);
		this.blur();
		return false;
	}, 
	
    // Make link change display of another element from block to none (and vice versa)
    // linkDropDown : function(linkid, dropdownid){
    //     if(!$(linkid) || !$(dropdownid)) return false;
    //     $(dropdownid).onclick = function(e){
    //         /* Cancel Event Propogation*/
    //         if (!e) var e = window.event;
    //         e.cancelBubble = true;
    //         if (e.stopPropagation) e.stopPropagation();
    //     }
    //     $(linkid).onclick = function(e){         
    //         /* Cancel Event Propogation*/
    //         if (!e) var e = window.event;
    //         e.cancelBubble = true;
    //         if (e.stopPropagation) e.stopPropagation();
    //         
    //         var dropdowns = document.getElementsByClassName($(dropdownid).className);
    //         for(var i = 0; i < dropdowns.length; i++){
    //             if(dropdowns[i].getAttribute("id") != dropdownid)
    //             dropdowns[i].style.display = "none";
    //         }
    //         $(dropdownid).style.display = $(dropdownid).style.display == "block" ? "none" : "block";
    //         
    //         this.blur();
    //        
    //         document.onclick = function(){
    //              $(dropdownid).style.display = "none";
    //              document.onclick = null;             
    //          }   
    //          return false;
    //     }                    
    // },
    
    // Smart Text Input (e.g. search fields)
    memorizeTextInput : function(id){
        var input = $(id);
        if(!input) return false;
        input.oldval = input.value;
        input.onfocus = function(){
			if(this.value == this.oldval)
            this.value = "";
        }
        input.onblur = function(){
            if(this.value == "")
            this.value = this.oldval;
        }
    },
    
	checkEnable : function(checkid, selectid){
		$(checkid).onchange = function(){
			if(this.checked){
				$(selectid).disabled = false;
			} else {
				$(selectid).disabled = true;
			}
		}
	},
    
    pause : function(millis) 
    {
        date = new Date();
        var curDate = null;
        do { var curDate = new Date(); } 
        while(curDate-date < millis);
    }
};    


var PRODUCTS = 
{   
    open : "x",
	disableimages : true,
	init : function()
	{           
		if(!$("wrapper")) return;
		//return false; // uncomment this line to view all product details simultaenously 
		$$(".detail").each(function(el){
		    PRODUCTS.hidedetail(el);
			$E('a', el).onclick = function(){
				PRODUCTS.hidedetail(el)
				return false;
			}
			el.onclick = function(e){
				if (!e) var e = window.event;
			    e.cancelBubble = true;
			}
			
			if(PRODUCTS.disableimages){
				$$('#'+el.getProperty("id")+' img').each(function(img){
					img.setProperty("src", img.getProperty("src").replace(".png", '.pngx'))
				})                         
			}
			
		})        
				
		$E("body").onclick = function(){
			if(typeof PRODUCTS.open != "string") PRODUCTS.hidedetail(PRODUCTS.open);
			PRODUCTS.open = "x";
		}

		$$("#wrapper a.getdetail").each(function(el){
		   el.onclick = function(e){
			if (!e) var e = window.event;
			e.cancelBubble = true;
			if(typeof PRODUCTS.open != "string") PRODUCTS.hidedetail(PRODUCTS.open)
			
			// Position Box
			var x = this.href.match(/#.*/) + "";
			var detail = $(x.match(/[a-z-].*/)+"")
			var dheight = detail.getSize()["size"]["y"];
			var wheight = Window.getHeight();
			if(dheight < wheight){
				var newtop = (wheight-dheight)/2;
			} else {
				var newtop = 10;
			}
            detail.setStyles({
				top: newtop+Window.getScrollTop()+"px",
				marginTop: "0"
			})
		    
			//Show Images
			if(PRODUCTS.disableimages){
				$$('#'+detail.getProperty("id")+' img').each(function(img){
					img.setProperty("src", img.getProperty("src").replace(".pngx", '.png'))
				})
			}
		    
		
			PRODUCTS.open = detail;
			return false;
		}
		})
	},
	
	hidedetail : function(el)
	{
		$(el).setStyles({
			marginTop: "-9999px",
			marginBottom: "0",
			position: "absolute"
		}); 
	}
}

var imgholder = new Object();
function videoOver(){
	$$(".vidlist img").each(function(img){
		var imgsrc = img.getProperty("src").replace(".jpg", "_over.jpg")
		imgholder[imgsrc] = new Image()
		imgholder[imgsrc].src = imgsrc;
		
		img.onmouseover = function(){
			this.setProperty("src", this.getProperty("src").replace(".jpg", "_over.jpg"))
		}                                                                             
		img.onmouseout = function(){
			this.setProperty("src", this.getProperty("src").replace("_over.jpg", ".jpg"))
		}
	})
}

// domFunction (will load things faster than addEvent can)
// http://brothercake.com/site/resources/scripts/domready/
function domFunction(f, a)
{ var n = 0; var t = setInterval(function()
{ var c = true; n++; if(typeof document.getElementsByTagName != 'undefined' && (document.getElementsByTagName('body')[0] != null || document.body != null))
{ c = false; if(typeof a == 'object')
{ for(var i in a)
{ if
( (a[i] == 'id' && document.getElementById(i) == null) || (a[i] == 'tag' && document.getElementsByTagName(i).length < 1)
)
{ c = true; break;}
}
}
if(!c) { f(); clearInterval(t);}
}
if(n >= 60)
{ clearInterval(t);}
}, 250);};              

var detail = new domFunction(PRODUCTS.init, "wrapper")
var vids = new domFunction(videoOver, "content")
var indepth = new domFunction(function(){
	if(!$("barpos")) return;           
	$("barpos").onclick = function(){
		var mywin=open(this.href, 'Easton', "width=847,height=725,toolbar=no,copyhistory=now,scrollbars=no,resizable=no,directories=no,menubar=no,status=no,location=no") ;
		this.blur();
		mywin.focus();
		return false;
	}
}
, "barpos"); 


var popupimages = new domFunction(function(){
	if(!$("sidebar")) return;           
	if($E("#sidebar .images a")){
		$E("#showathlete a").onclick = $E("#showathlete img").onclick = function(){
				$("showathlete").setStyle("display", "none");
				return false;
			}
	}
	$ES("#sidebar .images a").each(function(poplink){
		poplink.onclick = function(){
			var imgcopy = $E("#showathlete img").clone()
			$E("#showathlete img").remove();
			imgcopy.src = this.href;
			imgcopy.injectInside("showathlete");
			$E("#showathlete .credits").innerHTML = this.title;
			$("showathlete").setStyle("display", "block");
			return false;
		}
	});
}, "sidebar");

var launchselector = new domFunction(function(){
	if(!$E("#sidebar .configure a")) return;
	HELPERS.linkPopups($E("#sidebar .configure a"), "width=847,height=635,toolbar=no,copyhistory=now,scrollbars=no,resizable=no,directories=no,menubar=no,status=no,location=no")
})    

var buildsystem = new domFunction(function(){
	if(!$E("a.configure")) return;
	HELPERS.linkPopups($E("a.configure"), "width=847,height=635,toolbar=no,copyhistory=now,scrollbars=no,resizable=no,directories=no,menubar=no,status=no,location=no")
})  
var barpos = new domFunction(function(){
	if(!$E("a#barpos")) return;
	HELPERS.linkPopups($E("a#barpos"), "width=847,height=725,toolbar=no,copyhistory=now,scrollbars=no,resizable=no,directories=no,menubar=no,status=no,location=no")
}, "sidebar")

var indepth = new domFunction(function(){
	if(!$E("a#indepth")) return;
	HELPERS.linkPopups($E("a#indepth"), "width=800,height=600,toolbar=no,copyhistory=now,scrollbars=no,resizable=no,directories=no,menubar=no,status=no,location=no")
}, "sidebar")

var regform = new domFunction(function(){
	if(!$("productregistration")) return; 
	var productreg =  new FormValidator($('productregistration'), {evaluateFieldsOnBlur: false, evaluateFieldsOnChange: false, onFormValidate : function(success){
			if(!success) {
				$("warning").style.display = "block";                      	   
				var winScroller = new Fx.Scroll(window, {
				    wait:false,
				    offset: {'y': -15}
				});
				winScroller.toElement("warning");
      	} else {
      	    $("warning").style.display = "none";    
      	}
      	}
	});                                   
	
	HELPERS.checkEnable("clamp", "clampmodel");
	HELPERS.checkEnable("hardware", "hardwaremodel");
	HELPERS.checkEnable("grips", "gripsmodel")
}, "productregistration");
  

var serialnums = new domFunction(function(){
	if(!$("serialnums")) return;
	$ES("#serialnums a.add").each(function(a){
		a.onclick = function(){
			this.getParent().getNext().setStyle("display", "block");
			this.getParent().getElements('a').setStyle("display", "none");
			return false;
		}
	})
	$ES("#serialnums a.remove").each(function(a){
		a.onclick = function(){
			this.getParent().getElement('input').setProperty('value', '');
			this.getParent().getPrevious().getElements('a').setStyle("display", "inline");
			this.getParent().setStyle("display", "none");

			
			return false;
		}
	})
	
   
}, "serialnums");

var posblocker = new domFunction(function(){
	if(!$("blocker")) return;
   	$("blocker").setStyle("height", $("wrapper").getSize()["size"]["y"]+"px")
	   $("blocker").onclick = function(e){
	       if (!e) var e = window.event;
	       e.cancelBubble = true;
	   } 
}, "blocker")


           
// cookie testing thingy
function VisitTest() {
	//var visit = getCookie("Visited");
	//if (visit) window.location = "http://www.eastonexp.com/performance/index.html";
	//else document.cookie="Visited=true";
}
function getCookie(c_name)
{
if (document.cookie.length>0)
  {
  c_start=document.cookie.indexOf(c_name + "=");
  if (c_start!=-1)
    { 
    c_start=c_start + c_name.length+1; 
    c_end=document.cookie.indexOf(";",c_start);
    if (c_end==-1) c_end=document.cookie.length;
    return unescape(document.cookie.substring(c_start,c_end));
    } 
  }
return "";
}           
 var cookietest = new domFunction(function(){
	if(!$("homepage")) return;
	VisitTest();
}, "homepage");         

/* sIFR setup */
	if(typeof sIFR == "object"){
var frutiger = {
	src: '../swfs/frutiger_roman.swf'
	,ratios: [8,1.3,11,1.21,12,1.2,14,1.19,21,1.16,28,1.13,38,1.12,61,1.11,94,1.1,95,1.09,103,1.1,107,1.09,110,1.1,119,1.09,120,1.1,1.09]
};
sIFR.delayCSS  = true;
 // sIFR.domains = ['novemberborn.net'] // Don't check for domains in this demo

sIFR.activate(frutiger);

//sIFR.debug.ratios(frutiger, {selector: '#content .system p'});

sIFR.replace(frutiger, {
	selector: '#content .system p'
	,css: ['.sIFR-root {color:#FFFFFF; leading: 5px}']
	,wmode: 'transparent'
})  }    
