var appletOn = true;

window.addEvent('domready', function() {

menuOvers();

//swaps language on select dropdown
$$('input.butlang').setStyle('display', 'none');
$$('select.langselect').addEvent('change', function(e) {
    new Event(e).stop();
    $('language').submit();
});

  if ($('thumbGroup')) {
    var thumbs = $$('div#thumbGroup a.thumb');
    thumbs.each(function(thumb){
      thumb.addEvent('click',function(e){
        e = new Event(e);
        $$('.main_product_image').setProperty('src',thumb.getProperty('href'));
        e.stop();
      });
    });
  }

  if ($('shoeGroup1')) {
    var thumbs = $$('div#shoeGroup1 a.thumb');
    thumbs.each(function(thumb){
      thumb.addEvent('click',function(e){
        e = new Event(e);
        $$('#shoe_main_image_1').setProperty('src',thumb.getProperty('href'));
        e.stop();
      });
    });
  }
  
  if ($('shoeGroup2')) {
    var thumbs = $$('div#shoeGroup2 a.thumb');
    thumbs.each(function(thumb){
      thumb.addEvent('click',function(e){
        e = new Event(e);
        $$('#shoe_main_image_2').setProperty('src',thumb.getProperty('href'));
        e.stop();
      });
    });
  }

	if ($('shoeGroup3')) {
    var thumbs = $$('div#shoeGroup3 a.thumb');
    thumbs.each(function(thumb){
      thumb.addEvent('click',function(e){
        e = new Event(e);
        $$('#shoe_main_image_3').setProperty('src',thumb.getProperty('href'));
        e.stop();
      });
    });
  }
function menuOvers () {
  var lis = $$('ul#navproducts li ul li'
               ,'div.videos div#downloadvideo'
               ,'div.e2_photo_gallery div#buttonback'
               ,'div#thumbnails div.wallpaper'
               ,'div#thumbnails div.advertising'
               ,'div#thumbnails div.press'
               ,'div.thumbwrap span.title');
  lis.each(function(li){
    li.addEvent('mouseenter',function(){
      li.addClass('over');
    });
    li.addEvent('mouseleave',function(){
      li.removeClass('over');
    });
  });
 }

function GAwithoutAS(imageUrl) {
  alert(imageUrl);
  pageTracker._trackEvent('product','download', imageUrl ,1);
}

//style adjustments
var SAFARI = (window.webkit420);
if (SAFARI) {
  if ($E('div#quote-in div#quote-hero p span')) {
      $E('div#quote-in div#quote-hero p span').addClass('safari');
  }
  if ($E('div.videos div.media-flash a.degrade')) {
      $E('div.videos div.media-flash a.degrade').setStyles('position: relative; left: -5px;');
  }
  if ($E('div#holder div.oldclubs div#bottomside-signpost')) {
      $E('div#holder div.oldclubs div#bottomside-signpost').setStyles('margin-right:3px;');
  }
  if ($E('div#side-swingdna div.snb')) {
      $E('div#side-swingdna div.snb').setStyles('margin-top:7px;');
  }
  if ($E('div.item-wrapper div.playerdna table#playerdna tr td')) {
      $E('div.item-wrapper div.playerdna table#playerdna tr td').setStyles('padding-top:14px;');
  }
    if ($E('div.item-wrapper div.playerdna table#playerdna tr th')) {
      $E('div.item-wrapper div.playerdna table#playerdna tr th').setStyles('padding-top:14px;');
  }
}

});
//end of domready functions

if (document.getElementsByClassName == undefined) {
	document.getElementsByClassName = function(className)
	{
		var hasClassName = new RegExp("(?:^|\\s)" + className + "(?:$|\\s)");
		var allElements = document.getElementsByTagName("*");
		var results = [];

		var element;
		for (var i = 0; (element = allElements[i]) != null; i++) {
			var elementClass = element.className;
			if (elementClass && elementClass.indexOf(className) != -1 && hasClassName.test(elementClass))
				results.push(element);
		}

		return results;
	}
}

//modified by Karen Comber 260809 - adds ability for banner above to swap in line with tabs
function attachTabsetsJP() {

  var tabsets = $$('.tabsetjp');

  var banners = $$('.bannertab');
  if ($('tabs-banner') != null) $('tabs-banner').setStyle('display','block');

  tabsets.each(function(tabset,i){

  	tabset.removeClass('tabsetjp');
  	tabset.addClass('activetabsetjp');
    var ul = new Element('ul', {'class': 'tabs'});
    ul.inject(tabset,'top');

    var sections = tabset.getElements('.sectionjp');
    var headings = sections.getPrevious();

    headings.each(function(heading,j){

	  // Inject heading into the tabs ul.
      var li = new Element('li');
      heading.inject(li);
      li.inject(ul,'bottom');

      // Class adjustments on tab click.
      var parentli = heading.getParent();
      parentli.addEvent('click',function(){
        tabset.getElements('.current').each(function(element){element.removeClass('current');});
        tabset.getElements('.previous').each(function(element){element.removeClass('previous');});
        tabset.getElements('.next').each(function(element){element.removeClass('next');});
        heading.addClass('current');
        if (element = heading.getParent().getPrevious()) element.getChildren().addClass('previous');
        if (element = heading.getParent().getNext()) element.getChildren().addClass('next');
        sections[j].addClass('current');
        parentli.addClass('current');

        if (banners != null) {
          banners.each(function(banner,i){
              if (banners[i].hasClass('current')) banners[i].removeClass('current');
          });
          if ($defined(banners[0])) banners[j].addClass('current');
        }

        });

      // Tab hover events.
      parentli.addEvent('mouseenter',function(){
        heading.addClass('hover');
      });
      parentli.addEvent('mouseleave',function(){
        heading.removeClass('hover');
      });

    });

    // Find out if we have a hash (and need to open a different tab by default).
    if (location.href.contains('#')) {
      var thehash = location.href.split('#')[1].trim();
    }
    else var thehash = false;

    // Initial class assignment.
    if (thehash) {

      theheading = tabset.getElement('#'+thehash); //get the heading element inside the current tabset only

      if(theheading) //if we are in the right tabset for this hash and the hash has not previously been found
      {
      	theheading.addClass('current');
        theheading.getParent().addClass('current');
        theheading.getParent().fireEvent('click'); //fire click event, which will add current class to the section div
	  }
	  else
	  {
		sections[0].addClass('current');
        headings[0].getParent().addClass('current');
        headings[0].addClass('current');
        //banners[0].addClass('current');
        if ($defined(banners[0])) banners[0].addClass('current');
	  }
    }
    else {
      sections[0].addClass('current');
      headings[0].getParent().addClass('current');
      headings[0].addClass('current');
      if ($defined(banners[0])) banners[0].addClass('current');
    }

    headings[0].getParent().addClass('first');
    headings.getParent().getLast().addClass('last');
  });
}

function attachExternals() {
  var extLinks = $$('a.external');
  extLinks.each(function(extLink, i){
    extLink.setProperty('target','_blank');
  });
}

function attachDropDowns(nav) {
	navitems = $$('ul#' + nav + ' li');
	navitems.each(function(item){

		// Add and remove the "over" class (for emulating li:hover in IE6).
		item.addEvent('mouseenter',function(){
			item.addClass('over');
			item.setStyle('zindex','200');

			// For pages with the Blaze3D applet, we need to cover it with a dummy when mousing over the item.
			if ($('panelApplet')) {

			}
		});
		item.addEvent('mouseleave',function(){
			item.removeClass('over');
			item.setStyle('zindex','0');

			// For pages with the Blaze3D applet, we need to remove the dummy when mousing out of the item.
			if ($('panelApplet')) {

			}
		});

	});
}

function startList(nav) {

    
    socialRoot = document.getElementById('social-network');
    if (socialRoot) {
	    for (i=0; i<socialRoot.childNodes.length; i++) {
	      node = socialRoot.childNodes[i];
	      if (document.all && document.getElementById('social-network')) {
		      if (node.nodeName=="A") {
		        node.onmouseover=function() {
		          this.className+=" over";
		          this.style.zIndex=200;
		        }
		        node.onmouseout=function() {
		          this.className=this.className.replace(" over", "");
		          this.style.zIndex=0;
		        }
	          }
	      }
	    }
	}
	
    faqRoot = document.getElementById('faq-button');
    if (faqRoot) {
	    for (i=0; i<faqRoot.childNodes.length; i++) {
	      node = faqRoot.childNodes[i];
	      if (document.all && document.getElementById('faq-button')) {
		      if (node.nodeName=="A") {
		        node.onmouseover=function() {
		          this.className+=" over";
		          this.style.zIndex=200;
		        }
		        node.onmouseout=function() {
		          this.className=this.className.replace(" over", "");
		          this.style.zIndex=0;
		        }
	          }
	      }
	    }
	}
    
    navRoot = document.getElementById(nav);
    for (i=0; i<navRoot.childNodes.length; i++) {
      node = navRoot.childNodes[i];
      if (document.all && document.getElementById(nav)) {
	      if (node.nodeName=="LI") {
	        node.onmouseover=function() {
	          this.className+=" over";
	          this.style.zIndex=200;
	        }
	        node.onmouseout=function() {
	          this.className=this.className.replace(" over", "");
	          this.style.zIndex=0;
	        }
          }
      }
      
            
      if(document.getElementById('panelApplet')){
      if (node.nodeName=="LI") {
      	for (j=0; j<node.childNodes.length; j++) {
     		nodesub = node.childNodes[j];
     		if(nodesub.nodeName=="UL"){

				if(document.all){
					node.onmouseover = function() {
						this.className+=" over";
          				this.style.zIndex=200;
						el = document.getElementById('panelApplet');
						eld = document.getElementById('panelDummy');
						// z = (el.childNodes[1].nodeName=="SPAN") ? 3 : 1 ;
						if(this.childNodes[2].offsetHeight>80 && el.style.visibility=='visible'){
		        			// el.childNodes[z].style.visibility = "hidden";
		        			el.childNodes[1].style.visibility = "hidden";
		        			eld.className = "dummyImage";
							appletOn = false;
						}
					}
					node.onmouseout = function() {
						if(this.childNodes[2].offsetHeight>80 && appletOn==false){
							el = document.getElementById('panelApplet');
							// z = (el.childNodes[1].nodeName=="SPAN") ? 3 : 1 ;
	        				//el.childNodes[z].style.visibility = "visible";
	        				el.childNodes[1].style.visibility = "visible";
							appletOn = true;
							eld.className = "";
						}
						this.className=this.className.replace(" over", "");
          				this.style.zIndex=0;
					}
				} else {

					node.onmouseover = function() {
						el = document.getElementById('panelApplet');
						eld = document.getElementById('panelDummy');
						if(this.childNodes[2].offsetHeight>80 && el.style.visibility=='visible'){
		        			el.childNodes[3].style.visibility = "hidden";
		        			eld.className = "dummyImage";
							appletOn = false;
						}
					}
					node.onmouseout = function() {
						if(this.childNodes[2].offsetHeight>80 && appletOn==false){
							el = document.getElementById('panelApplet');
	        				el.childNodes[3].style.visibility = "visible";
	        				eld.className = "";
							appletOn = true;
						}
					}
				}

      		}
      	}

      }}
  	}
}

function fixControlElements(controlElement) {
  controlElements = document.getElementsByTagName(controlElement);
  for (var i = 0; i < controlElements.length; i++) {
    controlElements[i].outerHTML = controlElements[i].outerHTML;
  }
}

function activeContentHider(id) {
//  el = document.getElementById(id);
  el = id;
  //document.getElementById("appletBox").style.display = "none";
//  ifrm = document.createElement("iframe");
//  ifrm.setAttribute("id", "dummyframe");
//  ifrm.setAttribute("src", "#");
//  ifrm.setAttribute("scroll", "no");
//  ifrm.setAttribute("frameborder", "0");
//  ifrm.style.setAttribute("position", "absolute");
//  ifrm.style.setAttribute("visibility", "hidden");
//  ifrm.style.filter='Alpha(opacity=0)';
//  ifrm.style.width = "500px";//el.offsetWidth + "px";
//  ifrm.style.height = el.offsetHeight + "px";
//  ifrm.style.left = el.offsetLeft + "px";
//  ifrm.style.top = el.offsetTop + "px";
//  ifrm.style.zIndex = el.style.zIndex-1;
//  ifrm.style.backroundColor = "green";
//  el.appendChild(ifrm);
}


function limitRegions(){
	if(document.getElementById){
		el = document.getElementById("id_country");
		el2 = document.getElementById("id_county");
		if(el && el2){
			el2.disabled = "disabled";
			document.body.style.cursor = 'Wait';
			c = el.options[el.selectedIndex].value;
			if(c){
/*
				var ajax = new Request.HTML({
					url: "/store/index.php",
					update: 'frow2',
					success: function(responseText, responseXML){
						document.body.style.cursor = 'default';
					}

				})*/
			/*
			var ajax = new Ajax.Updater(
			    "frow2",
			    "/store/index.php",
			    {
			      method : "get",
			      onSuccess : function(){
			      	//el2 = document.getElementById("id_county");
			      	//el2.disabled = "";
			      	document.body.style.cursor = 'default';
			      },
			      parameters : "ajax_country="+c
			    }
			);
*/

				new Ajax('/store/index.php',{
					method:'get',
					update:'frow2',
					data:"ajax_country="+c,
					onSuccess : function(){
			      		document.body.style.cursor = 'default';
			      	}
				}).request();


			}
		}

	}
}

function countryRegionSetUp(){
	if(document.getElementById){
		el = document.getElementById("id_country");
		if(el){
			el.onchange = function(){ limitRegions(); }

			el.selectedIndex = 0;
			limitRegions();
		}

	}

}


function enableCounty(id){
	if(document.getElementById){
		ce = document.getElementById(id);
		ce.disabled = false;
	}
	return true;
}

function disableCounty(id){
	if(document.getElementById){
		ce = document.getElementById(id);
		ce.disabled = true;
	}
	return true;
}

function LightboxOpen () {
  // Remove position relative from menu items on lightbox opening.
  navRoot = document.getElementById('navbar');
  for (i=0; i<navRoot.childNodes.length; i++) {
    node = navRoot.childNodes[i];
    if (node.nodeName=="LI") {
      node.style.position='static';
    }
  }
  navRoot = document.getElementById('navproducts');
  for (i=0; i<navRoot.childNodes.length; i++) {
    node = navRoot.childNodes[i];
    if (node.nodeName=="LI") {
      node.style.position='static';
    }
  }
}
function LightboxClose () {
  // Add position relative to menu items on lightbox closing.
  navRoot = document.getElementById('navbar');
  for (i=0; i<navRoot.childNodes.length; i++) {
    node = navRoot.childNodes[i];
    if (node.nodeName=="LI") {
      node.style.position='relative';
    }
  }
  navRoot = document.getElementById('navproducts');
  for (i=0; i<navRoot.childNodes.length; i++) {
    node = navRoot.childNodes[i];
    if (node.nodeName=="LI") {
      node.style.position='relative';
    }
  }
}

function init() {
	attachExternals();
	//attachDropDowns();
  startList('navbar');
  startList('navproducts');
  //fixControlElements('applet');
  countryRegionSetUp();
  attachTabsetsJP();
}

