
$(document).ready(function() {
  
  if(document.getElementById('searchButton')) {
    new searchButton();
  }
  if(document.getElementById('kSearchButton')) {
    new kSearchButton();
  }
  if(document.getElementById('productFeatures')) {
    createProductFeatures();
  }
  if(document.getElementById('productDisplayShow')) {
    createProductDisplay();
  }
  if(document.getElementById('productModule')) {
    createProductModule();
  }
  if(document.getElementById('gradTabs')) {
    createGradTab();
  }
  if(document.getElementById('events')) {
    events();
  }
  if(document.getElementById('mapDisplay')) {
  mapDisplay();
  }
  
  $('input.placeholder, textarea.placeholder').placeholder();
  
  $('#hdSlideshow').slideshow();

});

function searchButton() {
  var $st = $('#searchText');
  var $sb = $('#searchButton');
  $sb.click(function(e) {
    e.preventDefault();
    gSearch();
  });
  $st.keypress(function(e) {
    if(e.which == 13) {
      e.preventDefault();
      gSearch();
    }
  });
  
  function gSearch() {
    var sValue = $st.val();
    if($st.hasClass('userinput') && sValue.length > 0) {
      window.location = $sb.attr('href') + "?search=" + sValue;
    }
  }
}

function kSearchButton() {
  var $st = $('#kSearchText');
  var $sb = $('#kSearchButton');
  $sb.click(function(e) {
    e.preventDefault();
    kSearch();
  });
  $st.keypress(function(e) {
    if(e.which == 13) {
      e.preventDefault();
      kSearch();
    }
  });
  
  function kSearch() {
    var sValue = $st.val();
    if($st.hasClass('userinput') && sValue.length > 0) {
      var cat = "";
      var dValue = $('#kSearchDropdown').val();
      if(dValue.length > 0) {
        cat = "&cat=" + dValue;
      }
      window.location = $sb.attr('href') + "?keyword=" + sValue + cat;
    }
  }
}

function events() {
  $('#events div.rep').each(function() {
    $this = $(this);
    var text = $this.html();
    var newText = text.replace(/[A-Z0-9._%+-]+@[A-Z0-9.-]+\.[A-Z]{2,6}/ig, function(email) {
      return '<a href="mailto:'+email+'" title="'+email+'">'+email+'</a>';
    }).replace(/((https?|s?ftp|ssh)\:\/\/[^"\s\<\>]*[^.,;'">\:\s\<\>\)\]\!])/g, function(url) {
      return '<a href="'+url+'" title="'+url+'">'+url+'</a>';
    });
    if(text != newText) {
      $this.html(newText);
    }
  });
}

function createProductModule() {
  var $pm = $('#productModule');
  var full = $pm.find('div.detailFull').html();
  $pm.append('<div class="detailShort">' + HTMLtruncate(full,325,'&hellip; <a class="more" title="Read More" id="productModuleMore" href="#"><span>Read&nbsp;More</span>&nbsp;&gt;</a>') + '</div>');
  $('#productModuleMore').click(function(e) {
    e.preventDefault();
    $pm.addClass('productModuleFull');
  });
}

function createProductFeatures() {
  var $pf = $('#productFeatures');
  var full = $pf.find('div.full');
  var readMore = ' <a class="more" title="Read More" href="#"><span>Read&nbsp;More</span>&nbsp;&gt;</a>';
  var close = ' <div><a class="more" href="#" title="Close">Close</a></div>';
  for(var x=0;x<full.length;x++) {
    var teaser = '<div class="teaser">' + HTMLtruncate(full[x].innerHTML,165,'&hellip;'+readMore) + '</div>';
    $(full[x]).append(close).after(teaser);
  }
  
  $pf.find('h4 a, a.more').click(function(e) {
    e.preventDefault();
    $(this).parents('li').toggleClass('full');
  });
}

function createProductDisplay() {
  var display = new productDisplay();
  display.show = document.getElementById('productDisplayShow');
  display.thumbs = document.getElementById('productDisplayThumbs');
  display.next = document.getElementById('productDisplayNext');
  display.previous = document.getElementById('productDisplayPrevious');
  display.init();
}

function createGradTab() {
  $('#gradTabs .gradTabs a').click(function() {
    document.getElementById('gradTabs').className = this.className;
    return false;
  });
}

function HTMLtruncate(text, maxLength, suffix) {
  if(!maxLength) maxLength = 0;
  var emptyTags = "|hr|br|img|input|link|meta|";
  var opClosingTags = "|dt|dd|li|option|";
  var opClosingParent = {'dt':'|dl|','dd':'|dl|','li':'|ul|ol|','option':'|select|','td':'|tr|','th':'|tr|','tr':'|table|tbody|thead|','tbody':'table','thead':'table'};
  var html = text.replace(/& /g,"&amp; ").replace(/< /g,"&lt; ").replace(/\n|\t/g," ").replace(/(<script\ .*?>.*?<\/script>)|<!.*?>/ig,"").replace(/  /g," ");
  var re = /(?:(?:<([a-z0-9\/]+)([^<>]*)>)|(&[a-z]+;|#[0-9]+;))([^<&]*)/im;
  var teaser = "";
  if(html.match(re)) {
    teaser = html.substring(0, html.indexOf(html.match(re)[0]));
  }
  else {
    teaser = html;
  }
  var count = teaser.length;
  if(count >= maxLength) {
    teaser = teaser.substring(0, maxLength);
    if(suffix && count != maxLength) {
      teaser += suffix;
    }
  }
  else if(html.length > maxLength) {
    var stack = new Array();
    var startIndex = 0;
    var totalLength = html.length;
    var result, pos, temp;
    while(startIndex < totalLength) {
      result = re.exec(html);
      if (result && result.length > 0 && result[0].length > 0 && count < maxLength) {
        pos = html.indexOf(result[0]);
        startIndex += pos;
        if(result[1] && result[1].indexOf('/') < 0) {
          if(emptyTags.indexOf('|'+result[1]+'|') == -1 && (result[2].length == 0 || result[2].lastIndexOf('/') != result[2].length-1)) {
            stack.push(result[1]);
          }
          teaser += "<" + result[1] + result[2] + ">";
        }
        else if(result[3]) {
          teaser += result[3];
          count++;
        }
        else {
          var pop = stack.pop();
          if("/" + pop == result[1]) {
            teaser += "<" + result[1] + ">";
          }
          else if(opClosingTags.indexOf('|'+pop+'|') > -1) {
            pop = stack.pop();
            while(opClosingTags.indexOf('|'+pop+'|') > -1) {
              pop = stack.pop();
            }
            if("/" + pop == result[1]) {
              teaser += "<" + result[1] + ">";
            }
            else {
              alert('invalid html: ' + result[0]);
              break;
            }
          }
          else {
            alert('invalid html: ' + result[0]);
            break;
          }
        }
        temp = result[4];
        if(count + temp.length > maxLength) {
          temp = temp.substring(0, maxLength - count);
          teaser += temp;
          if(suffix) {
            teaser += suffix;
          }
          break;
        }
        else {
          teaser += temp;
          count += temp.length;
        }
        html = html.substr(pos + result[0].length);
        startIndex += result[0].length;
      }
      else if(suffix && count == maxLength && result[4].length > 0) {
        teaser += suffix;
        break;
      }
      else {
        break;
      }
    }
    var needParent = "";
    for(var x=stack.length-1;x>-1;x--) {
      if(needParent.length == 0 || needParent.length > 0 && needParent.indexOf('|'+stack[x]+'|') > -1) {
        teaser += "</" + stack[x] + ">";
        needParent = "";
      }
      else if(opClosingTags.indexOf('|'+stack[x]+'|') == -1) {
        alert('invalid html: /' + stack[x]);
        break;
      }
      if(opClosingTags.indexOf('|'+stack[x]+'|') > -1) {
        if(needParent.length > 0 && needParent != opClosingParent[stack[x]]) {
          alert('invalid html: /' + stack[x]);
          break;
        }
        needParent = opClosingParent[stack[x]];
      }
    }
  }
  else {
    teaser = html;
  }
  return teaser;
}


/*function HTMLtruncate(text, maxLength, suffix) {
  if(!maxLength) maxLength = 100;
  var html = text.replace(/& /g,"&amp; ").replace(/< /g,"&lt; ").replace(/\n|\t/g,"").replace(/(<script\ .*?>.*?<\/script>)|<!.*?>/ig,"").replace(/  /g," ");
  var re = /(?:(?:<([a-z0-9\/]+)([^<>]*)>)|(&[a-z]+;|#[0-9]+;))([^<&]*)/im;
  var teaser = "";
  if(html.match(re)) {
    teaser = html.substring(0, html.indexOf(html.match(re)[0]));
  }
  else {
    teaser = html;
  }
  var count = teaser.length;
  if(count >= maxLength) {
    teaser = teaser.substring(0, maxLength);
    if(suffix && count != maxLength) {
      teaser += suffix;
    }
  }
  else if(html.length > maxLength) {
    var stack = new Array();
    var startIndex = 0;
    var totalLength = html.length;
    var result, pos, temp;
    while(startIndex < totalLength) {
      result = re.exec(html);
      if (result && result.length > 0 && result[0].length > 0 && count < maxLength) {
        pos = html.indexOf(result[0]);
        startIndex += pos;
        if(result[1] && result[1].indexOf('/') < 0) {
          if(result[2].length == 0 || result[2].lastIndexOf('/') != result[2].length-1) {
            stack.push(result[1]);
          }
          teaser += "<" + result[1] + result[2] + ">";
        }
        else if(result[3]) {
          teaser += result[3];
          count++;
        }
        else {
          var pop = stack.pop();
          if("/" + pop == result[1]) {
            teaser += "<" + result[1] + ">";
          }
          else {
            alert('invalid html: ' + result[0]);
            break;
          }
        }
        temp = result[4];
        if(count + temp.length > maxLength) {
          temp = temp.substring(0, maxLength - count);
          teaser += temp;
          if(suffix) {
            teaser += suffix;
          }
          break;
        }
        else {
          teaser += temp;
          count += temp.length;
        }
        html = html.substr(pos + result[0].length);
        startIndex += result[0].length;
      }
      else if(suffix && count == maxLength && result[4].length > 0) {
        teaser += suffix;
        break;
      }
      else {
        break;
      }
    }
    for(var x=stack.length-1;x>-1;x--) {
      teaser += "</" + stack[x] + ">";
    }
  }
  else {
    teaser = html;
  }
  return teaser;
}*/


function productDisplay() {
  var self = this;
  this.show = null;
  this.thumbs = null;
  /*this.features = null;*/
  this.next = null;
  this.previous = null;
  this.count = 0;
  this.current = 0;
  this.slider = null;
  this.init = function() {
      if(!this.thumbs) { return; }
    var thumbas = this.thumbs.getElementsByTagName('li');
    this.count = thumbas.length;
    if(this.count > 1) {
        for(var x=0;x<thumbas.length;x++) {
          this.addClick(thumbas[x],x);
        }
        if(this.next) {
          this.next.onclick = function() {
            self.select('next');
            return false;
          };
        }
        if(this.previous) {
          this.previous.onclick = function() {
            self.select('previous');
            return false;
          }
        }
        /*var featureNumbers = $(this.features).find('div.number');
        for(var x=0;x<featureNumbers.length;x++) {
          this.addClick(featureNumbers[x], x);
        }*/
        
        
          this.slider = new filmstrip();
          this.slider.frame = this.show;
          this.slider.init();
          
            this.select(0);
    }
    else {
        this.thumbs.style.display = "none";
        this.next.style.display = "none";
        this.previous.style.display = "none";
    }
    
  };
  this.select = function(arg) {
    var num;
    if(typeof(arg) == 'number') {
      num = arg;
    }
    else if(arg == 'next') {
      num = this.current + 1;
      if(num >= this.count) {
        num = 0;
      }
    }
    else if(arg == 'previous') {
      num = this.current - 1;
      if(num < 0) {
        num = this.count - 1;
      }
    }
    else {
      num = Number(arg);
    }
    
    var thumbas = this.thumbs.getElementsByTagName('li');
    thumbas[this.current].className = "";
    thumbas[num].className = "selected";
    //this.show.innerHTML = thumbas[num].innerHTML;
    this.slider.go(num+1);
    
    /*var featurelis = this.features.getElementsByTagName('li');
    featurelis[this.current].className = "";
    //featurelis[num].className = "active";
    */
    
    this.current = num;
    /*self.showList(num);*/
  }
  this.addClick = function(object, num) {
    object.onclick = function() {
      self.select(num);
      return false;
    }
  }
  /*this.showList = function(num) {
    var i = Number(num)+1;
    $('#productMain .productDisplay .features').hide();
    $('#productDisplayFeatures'+i).show();
  }*/
}


function filmstrip(frame, next, previous) {
  var self = this;
  this.current = 1;
  this.count = 1;
  this.settings = {direction:0, totalVisible:1, prefix:"filmstrip", ease:"sineInOut", trimEnding:false, disableWrap:false, setRotate:0, scrollWheel:false, pageSlide:false, debug:false};
  this.events = {onBeforeMove:null, onMoveComplete:null};
  this.frame = frame;
  this.next = next;
  this.previous = previous;
  this.anim = {time:0, begin:0, change:0.0, timer:null, duration:50, speed:15, rotate:null};
  this.init = function() {
    if(this.frame.id.length > 0 && this.settings.prefix == "filmstrip") {
      this.settings.prefix = this.frame.id + '_';
    }
    
    var slides = this.frame.getElementsByTagName('li');
    this.count = slides.length;
    for(var x=0;x<this.count;x++) {
      slides[x].id = this.settings.prefix + (x+1);
    }
    
    var wrapper = this.frame.getElementsByTagName('ul')[0];
    if(this.settings.direction == 0) {
      wrapper.style.width = slides[0].offsetWidth * (slides.length + this.settings.totalVisible) + "px";
      //wrapper.style.paddingRight = this.frame.offsetWidth + "px"
    }
    else {
      //wrapper.style.height = slides[0].offsetHeight * (slides.length + this.settings.totalVisible) + "px";
      wrapper.style.paddingBottom = this.frame.offsetHeight + "px"
    }
    
    if(this.settings.scrollWheel) {
      this.initWheel();
    }
    
    this.go(1);
    //if(this.settings.setRotate > 0) {
    //  this.anim.rotate = setTimeout(function() { self.moveNext(); }, this.settings.setRotate);
    //}
    
    if(this.next) {
      this.next.onclick = function() {
        self.moveNext();
        return false;
      };
    }
    if(this.previous) {
      this.previous.onclick = function() {
        self.movePrevious();
        return false;
      };
    }
    
    if(this.settings.debug) {
      if(this.settings.trimEnding && this.settings.pageSlide) alert('Warning:\nsettings.trimEnding and settings.pageSlide are both set to "true".');
    }
  };
  this.go = function(num) {
    clearTimeout(this.anim.rotate);
    
    if(this.events.onBeforeMove) {
      this.events.onBeforeMove(num);
    }
    
    this.current = num;
    
    var position = c2.findElementPos(document.getElementById(this.settings.prefix + this.current));
    var offsetPos = c2.findElementPos(document.getElementById(this.settings.prefix + 1));
    position[this.settings.direction] = position[this.settings.direction] - offsetPos[this.settings.direction];
    
    var scrollOffset = 0;
    if(this.settings.direction == 0) {
      scrollOffset = this.frame.scrollLeft;
    }
    else if(this.settings.direction == 1) {
      scrollOffset = this.frame.scrollTop;
    }
    
    if (this.anim.timer != null) {
      clearInterval(this.anim.timer);
      this.anim.timer = null;
    }
    this.anim.time = 0;
    this.anim.begin = scrollOffset;
    this.anim.change = position[this.settings.direction] - scrollOffset;
    
    this.anim.timer = setInterval(function() { self.scroll(); }, this.anim.speed);
  };
  this.scroll = function() {
    if (this.anim.time > this.anim.duration) {
      clearInterval(this.anim.timer);
      this.anim.timer = null;
      
      if(this.settings.setRotate > 0) {
        this.anim.rotate = setTimeout(function() { self.moveNext(); }, this.settings.setRotate);
      }
      
      if(this.events.onMoveComplete) {
        this.events.onMoveComplete();
      }
    }
    else {
      var move = c2.ease[this.settings.ease](this.anim.time, this.anim.begin, this.anim.change, this.anim.duration);
      if(this.settings.direction == 0) {
        this.frame.scrollLeft = move;
      }
      else if(this.settings.direction == 1) {
        this.frame.scrollTop = move;
      }
      this.anim.time++;
    }
  };
  this.moveNext = function() {
    var num;
    if(this.settings.pageSlide) {
      num = this.current + this.settings.totalVisible;
    }
    else {
      num = this.current + 1;
    }
    if((this.settings.trimEnding && num > this.count - this.settings.totalVisible + 1) || (!this.settings.trimEnding && num > this.count)) {
      if(this.settings.disableWrap) {
        return false;
      }
      else {
        num = 1;
      }
    }
    this.go(num);
  };
  this.movePrevious = function() {
    var num;
    if(this.settings.pageSlide) {
      num = this.current - this.settings.totalVisible;
    }
    else {
      num = this.current - 1;
    }
    if(num < 1) {
      if(this.settings.disableWrap) {
        return false;
      }
      else {
        if(this.settings.trimEnding) {
          num = this.count - this.settings.totalVisible + 1;
        }
        else {
          if(this.settings.pageSlide) {
            if(this.count % this.settings.totalVisible != 0) {
              num = this.count - (this.count % this.settings.totalVisible) + 1;
            }
            else {
              num = this.count - this.settings.totalVisible + 1;
            }
          }
          else {
            num = this.count;
          }
        }
      }
    }
    this.go(num);
  };
  this.initWheel = function() {
    if (this.frame.addEventListener) {
      /** DOMMouseScroll is for mozilla. */
      this.frame.addEventListener('DOMMouseScroll', this.slideEventWheel, false);
    }
    /** IE/Opera. */
    this.frame.onmousewheel = this.slideEventWheel;
  };
  this.slideEventWheel = function(event) {
        var delta = 0;
        if (!event) /* For IE. */
                event = window.event;
        if (event.wheelDelta) { /* IE/Opera. */
                delta = event.wheelDelta/120;
        } else if (event.detail) { /** Mozilla case. */
                delta = -event.detail/3;
        }
        if (delta) {
      if(delta < 0) {
        self.moveNext();
      }
      else {
        self.movePrevious();
      }
    }
        if (event.preventDefault)
      event.preventDefault();
    event.returnValue = false;
  };
}


var c2 = {
  findElementPos: function(elemFind) {
    var elemX = 0;
    var elemY = 0;
    do {
      elemX += elemFind.offsetLeft;
      elemY += elemFind.offsetTop;
    } while ( elemFind = elemFind.offsetParent )
    return Array(elemX, elemY);
  },
  randomInt: function(min, max) {
    return Math.floor(Math.random()*(max+1-min)) + min;
  },
  getQuery: function(param) {
    var p = escape(unescape(param));
    var regex = new RegExp("[?&]" + p + "(?:=([^&]*))?","i");
    var match = regex.exec(window.location.search);
    var value = null;
    if( match != null ){
      value = match[1];
    }
    return value;
  },
  getKey: function(e) {
    if(!e) e = window.event;
    var key = e.which;
    if(!key) key = e.keyCode;
    return key;
  },
  ease: {

    sineInOut: function (t, b, c, d) {
      return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
    }
  }
};

jQuery.extend( jQuery.easing,
{
  easeInQuad: function (x, t, b, c, d) {
    return c*(t/=d)*t + b;
  },
  easeOutQuad: function (x, t, b, c, d) {
    return -c *(t/=d)*(t-2) + b;
  },
  easeInOutQuad: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t + b;
    return -c/2 * ((--t)*(t-2) - 1) + b;
  },
  easeInCubic: function (x, t, b, c, d) {
    return c*(t/=d)*t*t + b;
  },
  easeOutCubic: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t + 1) + b;
  },
  easeInOutCubic: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t + b;
    return c/2*((t-=2)*t*t + 2) + b;
  },
  easeInQuart: function (x, t, b, c, d) {
    return c*(t/=d)*t*t*t + b;
  },
  easeOutQuart: function (x, t, b, c, d) {
    return -c * ((t=t/d-1)*t*t*t - 1) + b;
  },
  easeInOutQuart: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t + b;
    return -c/2 * ((t-=2)*t*t*t - 2) + b;
  },
  easeInQuint: function (x, t, b, c, d) {
    return c*(t/=d)*t*t*t*t + b;
  },
  easeOutQuint: function (x, t, b, c, d) {
    return c*((t=t/d-1)*t*t*t*t + 1) + b;
  },
  easeInOutQuint: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return c/2*t*t*t*t*t + b;
    return c/2*((t-=2)*t*t*t*t + 2) + b;
  },
  easeInSine: function (x, t, b, c, d) {
    return -c * Math.cos(t/d * (Math.PI/2)) + c + b;
  },
  easeOutSine: function (x, t, b, c, d) {
    return c * Math.sin(t/d * (Math.PI/2)) + b;
  },
  easeInOutSine: function (x, t, b, c, d) {
    return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
  },
  easeInExpo: function (x, t, b, c, d) {
    return (t==0) ? b : c * Math.pow(2, 10 * (t/d - 1)) + b;
  },
  easeOutExpo: function (x, t, b, c, d) {
    return (t==d) ? b+c : c * (-Math.pow(2, -10 * t/d) + 1) + b;
  },
  easeInOutExpo: function (x, t, b, c, d) {
    if (t==0) return b;
    if (t==d) return b+c;
    if ((t/=d/2) < 1) return c/2 * Math.pow(2, 10 * (t - 1)) + b;
    return c/2 * (-Math.pow(2, -10 * --t) + 2) + b;
  },
  easeInCirc: function (x, t, b, c, d) {
    return -c * (Math.sqrt(1 - (t/=d)*t) - 1) + b;
  },
  easeOutCirc: function (x, t, b, c, d) {
    return c * Math.sqrt(1 - (t=t/d-1)*t) + b;
  },
  easeInOutCirc: function (x, t, b, c, d) {
    if ((t/=d/2) < 1) return -c/2 * (Math.sqrt(1 - t*t) - 1) + b;
    return c/2 * (Math.sqrt(1 - (t-=2)*t) + 1) + b;
  },
  easeInElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return -(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
  },
  easeOutElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d)==1) return b+c;  if (!p) p=d*.3;
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    return a*Math.pow(2,-10*t) * Math.sin( (t*d-s)*(2*Math.PI)/p ) + c + b;
  },
  easeInOutElastic: function (x, t, b, c, d) {
    var s=1.70158;var p=0;var a=c;
    if (t==0) return b;  if ((t/=d/2)==2) return b+c;  if (!p) p=d*(.3*1.5);
    if (a < Math.abs(c)) { a=c; var s=p/4; }
    else var s = p/(2*Math.PI) * Math.asin (c/a);
    if (t < 1) return -.5*(a*Math.pow(2,10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )) + b;
    return a*Math.pow(2,-10*(t-=1)) * Math.sin( (t*d-s)*(2*Math.PI)/p )*.5 + c + b;
  },
  easeInBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*(t/=d)*t*((s+1)*t - s) + b;
  },
  easeOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158;
    return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
  },
  easeInOutBack: function (x, t, b, c, d, s) {
    if (s == undefined) s = 1.70158; 
    if ((t/=d/2) < 1) return c/2*(t*t*(((s*=(1.525))+1)*t - s)) + b;
    return c/2*((t-=2)*t*(((s*=(1.525))+1)*t + s) + 2) + b;
  },
  easeInBounce: function (x, t, b, c, d) {
    return c - jQuery.easing.easeOutBounce (x, d-t, 0, c, d) + b;
  },
  easeOutBounce: function (x, t, b, c, d) {
    if ((t/=d) < (1/2.75)) {
      return c*(7.5625*t*t) + b;
    } else if (t < (2/2.75)) {
      return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
    } else if (t < (2.5/2.75)) {
      return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
    } else {
      return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
    }
  },
  easeInOutBounce: function (x, t, b, c, d) {
    if (t < d/2) return jQuery.easing.easeInBounce (x, t*2, 0, c, d) * .5 + b;
    return jQuery.easing.easeOutBounce (x, t*2-d, 0, c, d) * .5 + c*.5 + b;
  }
});

(function($) {

$.fn.placeholder = function(defaultValue) {
  return this.each(function() {
    var val = this.defaultValue;
    if(defaultValue) { val = defaultValue; }
    if(this.value != val) {
      $(this).removeClass('placeholder').addClass('userinput');
    }
    $(this).focus(function() {
      if(val == this.value) {
        this.value = "";
      }
      $(this).removeClass('placeholder').addClass('userinput');
    });
    $(this).blur(function() {
      if(this.value.replace(/\s/g,"").length < 1) {
        this.value = val;
        $(this).addClass('placeholder').removeClass('userinput');
      }
    });
  });
};

$.fn.slideshow = function(options) {
  var opts = $.extend({}, $.fn.slideshow.defaults, options);
  
  return this.each(function() {
    var $home = $(this);
    var $images = $home.find(opts.images);
    if($images.length < 2) {
      return;
    }
    var $content = $home.find(opts.content);
    var current = 0;
    var timer;
    var isAnimating = -1;
    var queue = -1;
    var clicked = false;
    var hover = false;
    var switchDirection = false;
    
    //var width = $images.width();
    var width = 960;
    
    var $bullets = '<span class="'+opts.leftButtonClass+'"></span><span id="'+opts.bulletsId+'">';
    $images.each(function(i) {
      if(i == 0) {
        $bullets += '<span class="active"></span>';
      }
      else {
        $bullets += '<span></span>';
      }
    });
    $bullets += '</span><span class="'+opts.rightButtonClass+'"></span>';
    if(opts.bulletsAppend) {
      $home.find(opts.bulletsAppend).append($bullets);
    }
    else {
      $home.append($bullets);
    }
    $bullets = $('#'+opts.bulletsId+' span');
    
    function slideTo(num) {
      queue = -1;
      clearTimeout(timer);
      if(num >= $images.length) { num = 0; switchDirection = true; }
      if(num < 0) { num = $images.length - 1; switchDirection = true; }
      if(isAnimating == -1) {
        if(num != current) {
          isAnimating = num;
          $bullets.eq(current).removeClass('active');
          $bullets.eq(num).addClass('active');
          if((num > current && !switchDirection) || (num < current && switchDirection)) {
            switchDirection = false;
            slideDirection(num,-width,width);
          }
          else {
            switchDirection = false;
            slideDirection(num,width,-width);
          }
        }
        else {
          queue = -1;
        }
      }
      else {
        queue = num;
      }
    }
    function slideDirection(num,to,from) {
      $images.eq(current).animate({left:to},opts.animSpeed,opts.ease, function() {
        $(this).hide();
      });
      $images.eq(num).css({display:'block',left:from}).animate({left:0},opts.animSpeed,opts.ease,function() {
        current = num;
        isAnimating = -1;
        if(queue > -1) {
          slideTo(queue);
        }
        else if(!clicked && !hover) {
          startRotate(opts.delay);
        }
      });
      if($.browser.msie && $.browser.version < 9) {
        $content.eq(current).hide();
        $content.eq(num).show();
      }
      else {
        $content.eq(current).fadeOut(opts.animSpeed - 200);
        $content.eq(num).fadeIn(opts.animSpeed - 200);
      }
    }
    function startRotate(delay) {
      clearTimeout(timer);
      timer = setTimeout(function() { slideTo(current + 1); },delay);
    }
    
    $bullets.each(function(i) {
      $(this).click(function() {
        clicked = true;
        slideTo(i);
      });
    });
    $home.find('span.'+opts.leftButtonClass).show().click(function() {
      clicked = true;
      if(isAnimating > -1) {
        slideTo(isAnimating - 1);
      }
      else {
        slideTo(current - 1);
      }
    });
    $home.find('span.'+opts.rightButtonClass).show().click(function() {
      clicked = true;
      if(isAnimating > -1) {
        slideTo(isAnimating + 1);
      }
      else {
        slideTo(current + 1);
      }
    });
    $home.hover(function() {
      hover = true;
      clearTimeout(timer);
    },function() {
      hover = false;
      if(!clicked) {
        startRotate(opts.delay - 2000);
      }
    });
    startRotate(opts.delay);
  });
};
$.fn.slideshow.defaults = {
  images: "ul.hdSlide li",
  content: "ul.hdBox>li",
  leftButtonClass: "hdLeftButton",
  rightButtonClass: "hdRightButton",
  bulletsId: "hdBullets",
  bulletsAppend: null,
  animSpeed: 1200,
  delay: 10000,
  ease: 'easeInOutQuart'
};

})(jQuery);


function mapDisplay() {
  var $map = $('#mapDisplay'),
      currentRegion = $map.attr('class').substring(12);
  
  function changeMap(region) {
    $map.attr('class','mapDisplay2 ' + region);
  }
  function revertMap() {
    $map.attr('class','mapDisplay2 ' + currentRegion);
  }
  
  $map.find('a').hover(function () {
    changeMap($(this).attr('data-region'));
  }, function () {
    revertMap();
  });
}
