/* Called when the Webupdate window is closed. */
function OnClientClose(radWindow) { 
  var oValue = radWindow.Argument;
  window.location.href = window.location.href; //refresh window          
}
function menuOff() {
  $('#nav').hide();
}
/* Method used on the product category overview pages. */
function ShowPage(page, totalPages)
{
    var pageDiv;
    for (i = 1; i <= totalPages; i++)
    {
        pageDiv = document.getElementById("prodcontainer_page_" + i);
        if (pageDiv)
        {
            if (i == page)
                pageDiv.style.display = "block";
            else
                pageDiv.style.display = "none";
        }
    }
}

/* 
Methods for resizing the flash stage at runtime.

setFlashWidth(divid, newW)
divid: id of the div containing the flash movie.
newW: new width for flash movie

setFlashWidth(divid, newH)
divid: id of the div containing the flash movie.
newH: new height for flash movie

setFlashSize(divid, newW, newH)
divid: id of the div containing the flash movie.
newW: new width for flash movie
newH: new height for flash movie

canResizeFlash()
returns true if browser supports resizing flash, false if not. 
*/
function setFlashWidth(divid, newW) {
  document.getElementById(divid).style.width = newW+"px";
}
function setFlashHeight(divid, newH) {
  document.getElementById(divid).style.height = newH+"px";		
}
function setFlashSize(divid, newW, newH) {
	setFlashWidth(divid, newW);
	setFlashHeight(divid, newH);
}
function canResizeFlash() {
  var ua = navigator.userAgent.toLowerCase();
  var opera = ua.indexOf("opera");
  if( document.getElementById ) {
    if(opera == -1) return true;
      else if(parseInt(ua.substr(opera+6, 1)) >= 7) return true;
  }
  return false;
}

/**
 * COMMON DHTML FUNCTIONS
 * These are handy functions I use all the time.
 *
 * By Seth Banks (webmaster at subimage dot com)
 * http://www.subimage.com/
 *
 * Up to date code can be found at http://www.subimage.com/dhtml/
 *
 * This code is free for you to use anywhere, just keep this comment block.
 */

/**
 * X-browser event handler attachment and detachment
 *
 * @argument obj - the object to attach event to
 * @argument evType - name of the event - DONT ADD "on", pass only "mouseover", etc
 * @argument fn - function to call
 */ 
function addEvent(obj, evType, fn){
 if (obj.addEventListener){
    obj.addEventListener(evType, fn, false);
    return true;
 } else if (obj.attachEvent){
    var r = obj.attachEvent("on"+evType, fn);
    return r;
 } else {
    return false;
 }
}
function removeEvent(obj, evType, fn, useCapture){
  if (obj.removeEventListener){
    obj.removeEventListener(evType, fn, useCapture);
    return true;
  } else if (obj.detachEvent){
    var r = obj.detachEvent("on"+evType, fn);
    return r;
  } else {
    alert("Handler could not be removed");
  }
}

/**
 * Code below taken from - http://www.evolt.org/article/document_body_doctype_switching_and_more/17/30655/
 *
 * Modified 4/22/04 to work with Opera/Moz (by webmaster at subimage dot com)
 *
 * Gets the full width/height because it's different for most browsers.
 */
function getViewportHeight() {
	if (window.innerHeight!=window.undefined) return window.innerHeight;
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientHeight;
	if (document.body) return document.body.clientHeight; 

	return window.undefined; 
}
function getViewportWidth() {
	var offset = 17;
	var width = null;
	if (window.innerWidth!=window.undefined) return window.innerWidth; 
	if (document.compatMode=='CSS1Compat') return document.documentElement.clientWidth; 
	if (document.body) return document.body.clientWidth; 
}

/**
 * Gets the real scroll top
 */
function getScrollTop() {
	if (self.pageYOffset) // all except Explorer
	{
		return self.pageYOffset;
	}
	else if (document.documentElement && document.documentElement.scrollTop)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollTop;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollTop;
	}
}
function getScrollLeft() {
	if (self.pageXOffset) // all except Explorer
	{
		return self.pageXOffset;
	}
	else if (document.documentElement && document.documentElement.scrollLeft)
		// Explorer 6 Strict
	{
		return document.documentElement.scrollLeft;
	}
	else if (document.body) // all other Explorers
	{
		return document.body.scrollLeft;
	}
}

/*
Taken from http://www.hunlock.com/blogs/Totally_Pwn_CSS_with_Javascript
Gets a CSS rule on a page by class name
modified by remove the "delete" option
*/

function getCSSRule(ruleName)                                   // Return requested style obejct
{                           
    ruleName = ruleName.toLowerCase();                          // Convert test string to lower case.
    if (document.styleSheets)                                   // If browser can play with stylesheets
    {
        for (var i = 0; i < document.styleSheets.length; i++)   // For each stylesheet
        {
            var styleSheet = document.styleSheets[i];           // Get the current Stylesheet
            var ii = 0;                                         // Initialize subCounter.
            var cssRule = false;                                // Initialize cssRule. 
            do                                                  // For each rule in stylesheet
            {
                if (styleSheet.cssRules)                        // Browser uses cssRules?
                {
                    cssRule = styleSheet.cssRules[ii];          // Yes --Mozilla Style
                } else {                                        // Browser usses rules?
                    cssRule = styleSheet.rules[ii];             // Yes IE style. 
                }                                               // End IE check.
                if (cssRule)                                    // If we found a rule...
                {
                    if (cssRule.selectorText.toLowerCase()==ruleName)  //  match ruleName?
                    {
                        return cssRule;                         // return the style object.
                    }                                           // End found rule name
                }                                               // end found cssRule
                ii++;                                           // Increment sub-counter
            } while (cssRule)                                   // end While loop
        }                                                       // end For loop
    }                                                           // end styleSheet ability check
    return false;                                               // we found NOTHING!
}                                                               // end getCSSRule 

/***************************************************
 "Product - Accessory Category"-related functions
***************************************************/
function UpdateAccessoryContent(productName, shopPid, sourceImageId)
{
    if ($('#mainProductImage').attr('src') != $(sourceImageId).attr('src'))
    {
        $('#mainProductImage').fadeOut(200, function() {
            $('#productName').text(productName);
            $('#mainProductImage').attr('src', $(sourceImageId).attr('src'));
            $('#mainProductImage').attr('width', $(sourceImageId).width());
            $('#mainProductImage').attr('height', $(sourceImageId).height());
            $('#mainProductImage').attr('alt', $(sourceImageId).attr('alt'));
            /*
            if (shopPid == '')
            {
                $('#shopButton').fadeOut(200);
            }
            else
            {
                $('#shopButton').fadeIn(200);
                $('#shopButtonLink').attr('href', shopUrlPrefix.replace('{0}',shopPid));
            }
            */

            var altSourceImageId = '';
            var altSourceThumbImageId = '';
            if (sourceImageId.indexOf('_Alt') >= 0)
            {
                altSourceImageId = sourceImageId.replace('_Alt', '');
                altSourceThumbImageId = sourceImageId.replace('_Alt', '_Thumb');
            }
            else
            {
                altSourceImageId = sourceImageId + '_Alt';
                altSourceThumbImageId = sourceImageId + '_AltThumb';
            }

            if ($(altSourceThumbImageId).attr('src') == undefined)
            {
                $('#productAltThumbWrapper').fadeOut(200);
            }
            else
            {
                $('#productAltThumbWrapper').fadeIn(200);
                $('#mainProductAltThumbImage').attr('src', $(altSourceThumbImageId).attr('src'));
                $('#mainProductAltThumbImage').unbind('click');
                $('#mainProductAltThumbImage').click(function() {
                    UpdateAccessoryContent(productName, shopPid, altSourceImageId);
                });
            }
        });
        $('#mainProductImage').fadeIn(200);
    }
}

/***************************************************
 SiteCatalyst-related functions
***************************************************/
function scEventTracking(eventId) {
  trackEvent(eventId);
}
function trackEvent(eventId) {
  // 20100208 FKC: Assumes that the page calling this method has SiteCatalyst embedded in it.
  if ((s != null) && (eventId != null) && (eventId != '')) {
    if (s.events != null)
      s.events = s.events + "," + eventId;
    else
      s.events = eventId;
    s.t();
  }
}

/***************************************************
 JW Player functions
***************************************************/
var flashplayer = 'http://objects.odysseygolf.com/shared/scripts/jwplayer/jwplayer-5.6.1768.swf';
var skin = 'http://objects.odysseygolf.com/shared/scripts/jwplayer/skins/glow/glow.zip';
function setupVideoPlayer(containerId, file, image, autostart, width, height, bufferlength, duration) {
  if (duration != null) {
    jwplayer(containerId).setup({
      'autostart': autostart,
      'bufferlength': bufferlength,
      'duration': duration,
      'flashplayer': flashplayer,
      'file': file,
      'image': image,
      'skin': skin,
      'width': width,
      'height': height
    });
  }
  else {
    jwplayer(containerId).setup({
      'autostart': autostart,
      'bufferlength': bufferlength,
      'flashplayer': flashplayer,
      'file': file,
      'image': image,
      'skin': skin,
      'width': width,
      'height': height
    });
  }
}
/***************************************************
 jQuery Tools overlay
***************************************************/
function setupOverlay()
{
  $(function() {
    $('a[rel="#video-overlay-pt-customs-truck"],a[rel="#video-overlay-protype"]').overlay({
      closeOnClick: false,
      effect: 'apple',
      fixed: false,
      mask: {
        color: '#E5E5E5',
        loadSpeed: 200,
        opacity: 0.9
      }
    });
    $('a[rel="#newsletterOverlay"],a[rel="#prodRegOverlay"]').overlay({
      closeOnClick: false,
      effect: 'apple',
      fixed: false,
      mask: {
        color: '#E5E5E5',
        loadSpeed: 200,
        opacity: 0.9
      },
      onLoad: function() {
        var wrap = this.getOverlay().find('.contentWrap');
        var link = this.getTrigger().attr('href');
        if (link.indexOf('http') < 0) {
          wrap.load(link);
        }
        else {
          var iFrame = $('<iframe id="innerFrame" frameborder="0"></iframe>');
          $(iFrame).attr({ src: link, style: 'border: none; height: 562px; width: 100%;' });
          wrap.html(iFrame);
        }
    },
      onBeforeClose: function() {
        var wrap = this.getOverlay().find('.contentWrap');
        wrap.html('');
    }
    });
  });
}

/***************************************************
 jQuery Tools tabs
***************************************************/
function displaySingleImage(images)
{
    $(function() {
        $('.' + images).children('div').css('display', 'block');
    });
}
function setupImages(images, thumbnails, transitionEffect, intervalTime, playOnPageLoad, autopauseOnHover, rotateAtEnd, showReplyButton, centerReplayButton, replayButtonCss)
{ 
    $(function() {
        $("." + thumbnails).tabs("." + images + " > div", {
            effect: transitionEffect,
            fadeOutSpeed: 'slow',
            interval: intervalTime,
            rotate: rotateAtEnd
        }).slideshow({ autopause: autopauseOnHover, autoplay: playOnPageLoad });
        
        if(showReplyButton  && rotateAtEnd == false)
        {  
           var lastIndex = ($("."+images).children().length)-1;
           var apiTabs = $("." + thumbnails).data("tabs");
           var replayButtonTimer = null; 
           var replayButtonTimeHandler = function () {replayButtonTimer = setTimeout(function(){$("." + replayButtonCss).fadeIn('slow');},2500); };
           if(centerReplayButton){centerPosReplayButton(images,replayButtonCss);}
           apiTabs.onClick(function(){if(apiTabs.getIndex() == lastIndex){replayButtonTimeHandler();} else{clearTimeout(replayButtonTimer);  $("." + replayButtonCss).fadeOut('slow');}});
           $("." + replayButtonCss).click(function(){
             $(this).fadeOut('slow');
             setTimeout(function(){apiTabs.click(0);},800);
           });
        }
    });
}
function centerPosReplayButton(banners,replayButtonCss)
{
  var topPos = ($("." + banners).height()/2)-$("." + replayButtonCss).height();
  var leftPos = ($("." + banners).width()/2)-$("." + replayButtonCss).width(); 
  $("." + replayButtonCss).css({'top': topPos, 'left': leftPos});
}

