window.onload = function() {
	preloadImages();
	initRollovers();
	expandOnload();
}

var browser = navigator.appName;
var IE = "Microsoft Internet Explorer";
var vendor = navigator.vendor;
var safari = "Apple Computer, Inc.";

/* Detect if is IE6 and set a flag for DropDowns */
version=0;
ddFlag = 'none';
if (navigator.appVersion.indexOf("MSIE")!=-1){
	temp=navigator.appVersion.split("MSIE")
	version=parseFloat(temp[1])
}
if (version < 7) {
	ddFlag = "hide";
} else {
	ddFlag = 'none';
}

/* Begin show/hide Flash navigation function */
function flashNavigation(state) {
	var fullFlashNav = $('flashNav');

    var ddMain = $('ddMainCategory');
    var ddSub = $('ddSubCategory');

	if (state == 'visible'){
		/* Had to specify browser or Netscape hides
		   dropdowns too, unless (version == 6) - SR */
		fullFlashNav.style.height="400px";
		if(version < 7 && browser == IE){
        	ddMain.style.visibility = 'hidden';
        	ddSub.style.visibility = 'hidden';
		}
    } else {
		fullFlashNav.style.height="20px";
		if(version < 7 && browser == IE){
	        ddMain.style.visibility = 'visible';
    	    ddSub.style.visibility = 'visible'; 
		}
    }
}

function fromFlash(page) {
	window.location=page;
}

/* End show/hide Flash navigation function */

/* Begin preload images function */
var loadImg = new Array('http://www.wilson.com/fastpitch/img/a_up_on.png','http://www.wilson.com/fastpitch/img/a_up_off.png','http://www.wilson.com/fastpitch/img/a_down_on.png','http://www.wilson.com/fastpitch/img/a_down_off.png');

function preloadImages() {
	for (i=0; i<loadImg.length; i++) {
		var images = new Array();
		images[i] = new Image;
		images[i].src = loadImg[i];
	}
}
/* End preload images function */

/* Begin expandable table function */

function imageSwap(mouseevent,arrowstate,id) {        
	var c = classAttr();
	if (mouseevent == 'over'){
                //alert('over http://www.wilson.com/fastpitch/img/a_'+arrowstate+'_on.png');
		$(id+'_img').setAttribute('src','http://www.wilson.com/fastpitch/img/a_'+arrowstate+'_on.png');
		if(arrowstate == 'up'){
			$(id).setAttribute(c, 'expandHeaderOver');
		} else {
			$(id).setAttribute(c, 'expandedHeaderOver');
		}
	}
	
	else if (mouseevent == 'out') {
                //alert('out http://www.wilson.com/fastpitch/img/a_'+arrowstate+'_off.png');
		$(id+'_img').setAttribute('src','http://www.wilson.com/fastpitch/img/a_'+arrowstate+'_off.png');
		if(arrowstate == 'up'){
			$(id).setAttribute(c, 'expandHeader');
		} else {
			$(id).setAttribute(c, 'expandedHeader');
		}
	}	
	else {
		return;
	}
}

 function expand(mousestate,headerID) {
	var h3Expand = $(headerID);
	var tblExpand = $(headerID+'_table');
	var c = classAttr();
	var imgObj = $(headerID + "_img");
	var alt = getImgAlt(imgObj);
	var height = getImgHeight(imgObj);
	var width = getImgWidth(imgObj);
	
	if (tblExpand.style.display == "block") {
		tblExpand.style.display = "none";
		h3Expand.setAttribute(c,'expandHeaderOver');
		h3Expand.onmouseover = function(){imageSwap('over','up',headerID);};
		h3Expand.onmouseout = function(){imageSwap('out','up',headerID);};
		h3Expand.innerHTML = "<img src=\"http://www.wilson.com/fastpitch/img/a_up_"+mousestate+".png\" id=\""+headerID+"_img\" width=\""+width+"\" height=\""+height+"\" alt=\""+alt+"\" title=\""+alt+"\" />"+alt;
		
	}
	
	else {
		tblExpand.style.display = "block";
		h3Expand.setAttribute(c,'expandedHeaderOver');
		h3Expand.onmouseover = function(){imageSwap('over','down',headerID);};
		h3Expand.onmouseout = function(){imageSwap('out','down',headerID);};
		h3Expand.innerHTML = "<img src=\"http://www.wilson.com/fastpitch/img/a_down_"+mousestate+".png\" id=\""+headerID+"_img\" width=\""+width+"\" height=\""+height+"\" alt=\""+alt+"\" title=\""+alt+"\" />"+alt;
		
	}
}

function expandOnload() {
	var sURL = window.document.URL.toString();
	
	if (sURL.indexOf("?") > 0){//If there is a query string.
		var params = getURLParameters();
		var p = params.split("=")[0];	// parameter name
		var pvalue = params.split("=")[1];	// paramater value
		var openTbl = "gloves_" + pvalue;	// table to be displayed
		
		if ((pvalue == null) || (pvalue == '') || (p != "p")) {		// If there is no query string, no parameter value, or the wrong parameter name has been used, do nothing.
			;
		}
		
		else {		// Expand one of the collapseable tables.
			expand('off',openTbl);
		}	
	}
}
/* End expandable table function */

/* Begin image rollover funtion */
function initRollovers(){	
	var rollovers = getElementsByClass('rollover',null,null);
	
	if (rollovers.length > 0){
		for(var i=0;i<rollovers.length;i++){
			addEvent(rollovers[i],'mouseover',rollover);	
			addEvent(rollovers[i],'mouseout',rollover);	
		}		
	}
}

function rollover(event) {	//This simply adds a rollover function to any image with a class of "rollover".
	var id = getTarget(event).id;
	var etype = event.type;
	var imgExt = getImgExt($(id));
	
	var img = id.split('_image')[0];
	
	if (etype == 'mouseover'){
		$(id).setAttribute('src','images/'+img + "_on." + imgExt);
	}
	
	else if (etype == 'mouseout') {
		$(id).setAttribute('src','images/'+img + "_off." + imgExt);
	}
}
/* End image rollover function */

/* Begin pop up function */
function popWindow(href,title,parameters) {
	//var parameters = 'toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=0,resizable=0,width=340,height=275,top=150,left=150';
	window.open(href,title,parameters);
}

function goToClose(href) {
	window.opener.location.href=href;
	window.close();
}
/* End pop up function */

/* Begin helper functions */
/* Extracted from Prototype JS Library, http://prototype.conio.net/
 Written by Sam Stephenson, http://conio.net/ */
function $() {
  var elements = new Array();
  for (var i = 0; i < arguments.length; i++) {
    var element = arguments[i];
    if (typeof element == 'string') element = (document.getElementById) ? document.getElementById(element) : eval("document.all."+element) ;
    if (arguments.length == 1) return element;
    elements.push(element);
  }
  return elements;
}

function getElementsByClass(className, elm, tag){
	if(elm == null) elm = document;
	if(tag == null) tag = '*';
	var elems = (tag == "*" && document.all)? document.all : elm.getElementsByTagName(tag);
	var returnElems = new Array();
	className = className.replace(/\-/g, "\\-");
	var pattern = new RegExp("(^|\\s)" + className + "(\\s|$)");
	for(var i=0; i<elems.length; i++){
		if(pattern.test(elems[i].className)) returnElems.push(elems[i]);
	}
	return returnElems
}

/* Function to remove all child nodes of an element. */
function clean(elm) {
	while($(elm).hasChildNodes()) {
		$(elm).removeChild($(elm).childNodes[0]);
	}
	return true;
}

/* Function to return the class attribute. IE and all other standards compliant browsers have different names for the class attribute object. */
function classAttr(){
	if (browser == IE ){
		class_att = 'className';
	}
	else {
		class_att = 'class';
	}
	
	return class_att;
	
}

/* Function to get img extension */
function getImgExt(obj){
	var src = obj.getAttribute('src');
	var imgExtPosition = src.lastIndexOf('.') + 1;	//Get the extension. Add 1 to get only the extension, not .extension
	
	var extension;
	
	if(src.match('clearpixel.gif')){	//If IE and the image is a PNG, return 'png', because the PNG transparency function causes the img src to be clearpixel.gif.
		extension = 'png';
	}
	
	else {
		var extension = src.slice(imgExtPosition);
	}
	return extension;
}

/* Function to get img alt text */
function getImgAlt(obj){
	var alt_text = obj.getAttribute('alt');	
	return alt_text;
}

/* Function to get img height text */
function getImgHeight(obj){
	var img_h = obj.getAttribute('height');	
	return img_h;
}

/* Function to get img width text */
function getImgWidth(obj){
	var img_w = obj.getAttribute('width');	
	return img_w;
}

/* Functions to add/remove event listeners to objects
	Written by John Resig, http://ejohn.org */
function addEvent( obj, type, fn ) {
	if (obj.addEventListener) obj.addEventListener( type, fn, false );
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
		obj.attachEvent( "on"+type, obj[type+fn] );
	}
}

function removeEvent( obj, type, fn ) {
	if (obj.removeEventListener) obj.removeEventListener( type, fn, false );
	else if (obj.detachEvent) {
		obj.detachEvent( "on"+type, obj[type+fn] );
		obj[type+fn] = null;
		obj["e"+type+fn] = null;
	}
}
/* Functions to handle cross-browser events.
 Written by Zack Gilbert of Seen Creative, http://www.weareseencreative.com. */
/* get the target of an event object */
function getTarget(e) {
	return (e.target) ? e.target : e.srcElement;
}


/* Functions for glove patterns page */

/* Begin preload images function */
var gloveLoadImg = new Array();
loadImg[0] = 'gloves_pitcher_up_on.png';
loadImg[1] = 'gloves_catcher_up_on.png';
loadImg[2] = 'gloves_infield_up_on.png';
loadImg[3] = 'gloves_outfield_up_on.png';
loadImg[4] = 'gloves_1B_up_on.png';
loadImg[5] = 'gloves_pitcher_down_on.png';
loadImg[6] = 'gloves_catcher_down_on.png';
loadImg[7] = 'gloves_infield_down_on.png';
loadImg[8] = 'gloves_outfield_down_on.png';
loadImg[9] = 'gloves_1B_down_on.png';
loadImg[10] = 'gloves_pitcher_down_off.png';
loadImg[11] = 'gloves_catcher_down_off.png';
loadImg[12] = 'gloves_infield_down_off.png';
loadImg[13] = 'gloves_outfield_down_off.png';
loadImg[14] = 'gloves_1B_down_off.png';

function glovePreloadImages() {
	for (i=0; i<gloveLoadImg.length; i++) {
		var images = new Array();
		images[i] = new Image;
		images[i].src = 'http://www.wilson.com/fastpitch/patterntitles/' + loadImg[i];
	}
}
/* End preload images function */

/* Begin expandable table function */

function glovesImageSwap(mouseevent,arrowstate,id) {
	if (mouseevent == 'over'){
		document.getElementById(id+'_img').setAttribute('src','http://www.wilson.com/fastpitch/patterntitles/'+ id +'_'+arrowstate+'_on.png');
	}
	
	else if (mouseevent == 'out') {
		document.getElementById(id+'_img').setAttribute('src','http://www.wilson.com/fastpitch/patterntitles/'+ id +'_'+arrowstate+'_off.png');
	}	
	else {
		return;
	}
}

 function gloveExpand(mousestate,headerID) {
	var h3Expand = document.getElementById(headerID);
	var tblExpand = document.getElementById(headerID+'_table');
	var alt;
	
	switch(headerID) {
		case "gloves_pitcher":
			alt = "Pitcher Patterns"
			break
		case "gloves_catcher":
			alt = "Catcher Patterns"
			break
		case "gloves_infield":
			alt = "Infield Patterns"
			break
		case "gloves_outfield":
			alt = "Outfield Patterns"
			break
		case "gloves_1B":
			alt = "First Base Patterns"
			break
		default:
			alt="OTHER"		
	}
	
	if (tblExpand.style.display == "block") {
		tblExpand.style.display = "none";
		h3Expand.setAttribute('class','glovesExpandHeader');
		//IE needs to set 'className'
		h3Expand.setAttribute('className','glovesExpandHeader');
		h3Expand.onmouseover = function(){glovesImageSwap('over','up',headerID);};
		h3Expand.onmouseout = function(){glovesImageSwap('out','up',headerID);};
		h3Expand.innerHTML = "<img src=\"http://www.wilson.com/fastpitch/patterntitles/"+ headerID+"_up_"+mousestate+".png\" id=\""+headerID+"_img\" width=\"218\" height=\"10\" alt=\""+alt+"\" title=\""+alt+"\" />";
		
	}
	
	else {
		tblExpand.style.display = "block";
		h3Expand.setAttribute('class','glovesExpandedHeader');
		//IE needs to set 'className'
		h3Expand.setAttribute('className','glovesExpandedHeader');
		h3Expand.onmouseover = function(){glovesImageSwap('over','down',headerID);};
		h3Expand.onmouseout = function(){glovesImageSwap('out','down',headerID);};
		h3Expand.innerHTML = "<img src=\"http://www.wilson.com/fastpitch/patterntitles/"+ headerID+"_down_"+mousestate+".png\" id=\""+headerID+"_img\" width=\"218\" height=\"10\" alt=\""+alt+"\" title=\""+alt+"\" />";
		
	}
}

function gloveExpandOnload() {
	var params = getURLParameters('split');
	var openTbl = "gloves_" + params;
	
	/*******************
	If there is no query string, no parameter value, or the wrong parameter name has been used, do nothing. - KM
	*******************/
	if ((params == null) || (params == '')) {
		;
	}
	
	/*******************
	Flash can't use "1B" as an instance name, so we must use "firstbase" in the query string. "1B" is already used in image names and ID's for the patterns page, 
	so it can't be changed easily. When the parameter position=firstbase is grabbed from the query string, it is being changed to "1B". - KM
	*******************/
	else if (params == "firstbase"){
		openTbl = "gloves_1B"
		gloveExpand('off',openTbl);
	}
	
	else {
		gloveExpand('off',openTbl);
	}	
}

/* End glove patterns functions */

/* Begin international popup function */
function open_intl(url){
    screenwidth = window.screen.width;
    screenheight = window.screen.height;

    windowleft = (screenwidth - 600)/2;
    windowtop = (screenheight - 500)/2;

    contacts = window.open(url,'open_contacts','width=630,height=500,history=no,resizable=no,status=no,scrollbars=yes,menubar=no,top=' + windowtop + ',left=' + windowleft);
}
/* End international popup function */

/* Begin registratio popup function */
function open_reg(url){
    screenwidth = window.screen.width;
    screenheight = window.screen.height;

    windowleft = (screenwidth - 630)/2;
    windowtop = (screenheight - 610)/2;

    contacts = window.open(url,'open_contacts','width=630,height=610,history=no,resizable=no,status=no,scrollbars=yes,menubar=no,top=' + windowtop + ',left=' + windowleft);
}
/* End registration popup function */