Cufon.replace('#subSubNavi a, div.price span', { fontFamily: 'Unive' });
Cufon.replace('#mainLeftColumn h1, #mainLeftColumn h2, .content-browse h1, .content-browse h2', { fontFamily: 'Unive' });
Cufon.replace('#mainCenterColumn h1, #mainCenterColumn h1.subHeader, #mainCenterColumn h2, #mainCenterColumn h3', { fontFamily: 'Unive' });
Cufon.replace('#newsLifts a span.day, #subSubNavi span.infoText', { fontFamily: 'Unive' });
Cufon.replace('.accessory h3, #bottomButtons a', { fontFamily: 'Unive' });
Cufon.replace('a.butRed, a.butGray, a.butLGray', { fontFamily: 'Unive' });

function setPagerPosition(){
  var pager = $('#scrollerPagerCont');
  var pagerWidth = $('#scrollerPagerCont').width();
  var fullWidth = ($('#scrollerPagerCont').parent().width()/2);
  var offset = (pagerWidth/2);
  var fromLeft = (fullWidth - offset);
  pager.css("left", fromLeft);
}
function setExactPagerPosition(pagerId){
  var pager = $('#'+pagerId);
  var pagerWidth = $('#'+pagerId).width();
  var fullWidth = ($('#'+pagerId).parent().width()/2);
  var offset = (pagerWidth/2);
  var fromLeft = (fullWidth - offset);
  pager.css("left", fromLeft);
}
function searchForNormalCycle(){
  $('.scrollerNormal').each(function(e){
    var cycleDivId = $(this).attr('id');
    setExactPagerPosition(cycleDivId+'_pager');
  });
}
function setProductNavi(){
  var fullW = $('#productNavi').width();
  var liAmou = $('#productNavi li').length;
  var aWidth = 0;
  $('#productNavi li a').each(function(e){
    aWidth += $(this).width();
  });
  var extraPixels = fullW-aWidth;
  var paddingPerA = Math.ceil(extraPixels/liAmou);
  var totalPadding = paddingPerA*liAmou;
  var lastPadding = paddingPerA+(extraPixels-totalPadding);
  $('#productNavi li').each(function(e){
    if(liAmou <= 1){
      $(this).css('width', $(this).children('a').width()+lastPadding+'px');
    } else {
      $(this).css('width', $(this).children('a').width()+paddingPerA+'px');
    }
    $(this).children('a').css('display','block');
    liAmou--;
  });
}

function selectColour( relId, target ) {
  if($('#'+relId+':visible').length == 0){
    $('#bikeImageColours a.colour').removeClass('selected');
    target.addClass('selected');
    $("#currentBikeColourImagesFrame div[id*='colour_']").hide();
    $('#'+relId).show();
    $('.productSmallFrame').removeClass('active');
    $('#'+relId+' a:first-child').children('span').addClass('active');
    var firstImageId = $('#'+relId+' a:first-child').attr('rel');
    $('#currentBikeImageFrame a.bikeImage').addClass('hide');
    $('#'+firstImageId).removeClass('hide');
  }  
}

var timeout;
function showColourName( relId ) {  
  var target = $('#bikeImageColours').find("a[rel='"+relId+"']");
  var tip = target.attr('title');
  if(tip){
		$('#bikeImageColourName').html(
			'<div class="infoTooltipColour">'
				+'<div class="top">'
					+'<span><span>'+tip+'</span></span>'
				+'</div>'
			+'</div>'
		);
    var offsetWidth = 349-$('.infoTooltipColour').width();
    var fromLeft = (offsetWidth/2);
    $('.infoTooltipColour').css("left", fromLeft);
    $('.infoTooltipColour').fadeIn(100);
    
    timeout = setTimeout("hideColourName()",2000);
	}
}
function hideColourName() {
  clearTimeout(timeout);
  $('.infoTooltipColour').fadeOut(1000);
}

$(document).ready(function() {
  bindHighSlideAjax();
  bindHighSlideImage();
  setProductNavi();

  $('.info').hover(
		function() {
  		this.tip = this.title;
  		if(this.tip){
    		$(this).append(
    			'<div class="infoTooltip">'
    				+'<div class="top">'
    					+this.tip
    				+'</div>'
    				+'<div class="bottom"></div>'
    			+'</div>'
    		);
    		this.title = "";
    		this.width = $(this).width();
  		}
  	},
  	function() {
  		if(this.tip){
    		$(this).children().remove();
  			this.title = this.tip;
  		}
		}
	);
  $('.info').click(function(e){ e.preventDefault(); });
  
  $('#bigScroller').cycle({
    fx: 'fade',
    speed: 800, // transition speed
    timeout: 8000, // wait
    slideExpr: 'div',
    prev: '#scrollLeft',
    next: '#scrollRight',
    cleartypeNoBg: true,
    pager: '#scrollerPagerCont',
    pause: 1,
    pagerAnchorBuilder: function() { 
      return '<a href="#"></a>'; 
    }
  });
  setPagerPosition();
  
  $('.frontScrollerSmall').each(function(e){
    var cycleDivId = $(this).attr('id');
    var cycleDivPrevId = '#'+cycleDivId+'_l';
    var cycleDivNextId = '#'+cycleDivId+'_r';
    var cycleDivPagerId = '#'+cycleDivId+'_pager';
    $('#'+cycleDivId).cycle({
      fx: 'fade',
      speed: 800, // transition speed
      timeout: 0, // wait
      slideExpr: 'div',
      prev: cycleDivPrevId,
      next: cycleDivNextId,
      cleartypeNoBg: true,
      pager: cycleDivPagerId,
      pause: 1,
      pagerAnchorBuilder: function() { 
       return '<a href="#"></a>'; 
      }
    });
    setExactPagerPosition(cycleDivId+'_pager');
  });
  
  $('.scrollerNormal').each(function(e){
    var cycleDivId = $(this).attr('id');
    var cycleDivPrevId = '#'+cycleDivId+'_l';
    var cycleDivNextId = '#'+cycleDivId+'_r';
    var cycleDivPagerId = '#'+cycleDivId+'_pager';
    $('#'+cycleDivId).cycle({
      fx: 'scrollHorz',
      speed: 500, // transition speed
      timeout: 0, // wait
      slideExpr: 'div.slide',
      prev: cycleDivPrevId,
      next: cycleDivNextId,
      cleartypeNoBg: true,
      pager: cycleDivPagerId,
      pause: 1,
      pagerAnchorBuilder: function() { 
       return '<a href="#"></a>'; 
      }
    });
    setExactPagerPosition(cycleDivId+'_pager');
  });
  
  $('.product .image a').hover(
    function () {
      $('div.productRow').css("z-index","300");
      var relToShow = $(this).attr('rel');
      $('#mainCenterColumn .extraImage').hide();
      if(relToShow){
        $('#'+relToShow).show();
      }
      $(this).children('span').show();
    }, 
    function () {
      $('div.productRow').css("z-index","100");
      $('#mainCenterColumn .extraImage').hide();
      $(this).children('span').hide();
    }
  );
  
  $('#productNavi a').click(function(e){
    e.preventDefault();
    var relDiv = $(this).attr('rel');
    if($('#'+relDiv+':visible').length == 0){
      $('#productNavi li').removeClass('selected');
      $(this).parent().addClass('selected');
      $('div.productArea').hide();
      $('#'+relDiv).show();
      searchForNormalCycle();
    }
  });
  
  $('a.sitemap, a#closeSitemap').click(function(e){
    e.preventDefault();
    $('a#closeSitemap').toggle();
    $("#sitemap").slideToggle(200);
    $.scrollTo('+=300px', 200);
  });
  
  $('#currentBikeColourImagesFrame a').click(function(e){
    e.preventDefault();
    current = $(this).children('span');
    $('#currentBikeColourImagesFrame span').not(current).removeClass('active');
    var relId = $(this).attr('rel');
    if($('#'+relId+':visible').length == 0){
      $('#currentBikeImageFrame a.bikeImage').addClass('hide');
      $('#'+relId).removeClass('hide');
      $(this).children('span').addClass('active');
    }
  });
  $('#bikeImageControls a#prev, #bikeImageControls a#next').click(function(e){
    e.preventDefault();
    if($(this).hasClass('enabled')){
      var colourAmount = $('#currentBikeColourImagesFrame div').length;
      var currentColour = '';      
      $('#currentBikeColourImagesFrame div').each(function(e){
        var relId = $(this).attr('id');
        if($('#'+relId+':visible').length > 0){ // is visible
          if(currentColour == ''){
            currentColour = '#'+relId;
          }
        }
      });
      var divIdSplit= currentColour.split('_');
      var currentPosition = parseInt(divIdSplit[1]);
      var newDiv = '';
      if($(this).attr('id') == 'prev'){
        if(currentPosition == 1){
          newDiv = 'colour_'+colourAmount;
        } else {
          newDiv = 'colour_'+(currentPosition-1);
        }
      } else if($(this).attr('id') == 'next'){
        if(currentPosition == colourAmount){
          newDiv = 'colour_1';
        } else {
          newDiv = 'colour_'+(currentPosition+1);
        }
      }
      
      var $target = $('#bikeImageColours').find("a[rel='"+newDiv+"']");
		  $('#bikeImageColours').stop().scrollTo( $target , 300, {offset:-124}, {onAfter: selectColour( newDiv, $target ) } );
		  
      clearTimeout(timeout);
      hideColourName();
      timeout = setTimeout("showColourName('"+newDiv+"')",500);
    }
  });
  $('#bikeImageColours a.colour').click(function(e){
    e.preventDefault();
    var relId = $(this).attr('rel');
    
    var $target = $('#bikeImageColours').find("a[rel='"+relId+"']");
		$('#bikeImageColours').stop().scrollTo( $target , 300, {offset:-124}, {onAfter: selectColour( relId, $target ) } );

    clearTimeout(timeout);
    hideColourName();
    timeout = setTimeout("showColourName('"+relId+"')",500);
  });
  
  $('#currentBikeImageFrame a.noHighslideImage').click(function(e){
    e.preventDefault();
  });
  
  $('.dealerMap').hide();
  $('.showMap').click(function(e){
    e.preventDefault();
    var relVal = $(this).attr('rel');
    var elID = '#dealerMap' + relVal;
    $(elID).slideToggle(200);
    if ($(this).hasClass('showMapOpen')) {
      $(this).removeClass('showMapOpen');
    } else {
      $(this).addClass('showMapOpen');
    }
  });
  
  dropdownBox();
  dropdownBoxDark();
  
  $('a.mediaInfo').click(function(){
    infoButton(this);
  });
  
  $('.startSearch').click(function(e){
    e.preventDefault();
    $(this).parent().submit();
  });
  $('.emptyDropdowns').click(function(e){
    e.preventDefault();
    $(this).siblings('.dropdownBox').each(function(){
      var htmlValue = $(this).children('a').attr('title');
      $(this).children('a').html(htmlValue);
      $(this).children('input').val('');
    });
  });
  
  $('.checkboxArea a.checkbox, .checkboxArea span, .mailercheckbox a.checkbox, .mailercheckbox span').click(function(e){
    e.preventDefault();
    var clickedEl = $(this);
    if ( !$(this).hasClass('checkbox') ) {
      clickedEl = $(this).siblings('a.checkbox');
    }
    var value = clickedEl.attr('rel');
    if (clickedEl.hasClass('checked')) {
       clickedEl.removeClass('checked');
       clickedEl.siblings('input').attr('checked', false);
    } else {
       clickedEl.addClass('checked');
       clickedEl.siblings('input').attr('checked', true);
    }
  });
  
  $('#advancedSearch .advancedStartSearch').click(function(e){
    e.preventDefault();
    $('#advancedSearch').submit();
  });
  $('#advancedSearchLeft .advancedStartSearch').click(function(e){
    e.preventDefault();
    $('#advancedSearchLeft').submit();
  });
  $('#advancedSearch .advancedEmptySearch').click(function(e){
    e.preventDefault();
    $('.checkboxArea input:checked').attr( 'checked', false );
    $('.checkboxArea .checkbox').removeClass( 'checked' );
    $('#searchText').val('');
    
    var htmlValue = $('.dropdownBox a.selected').attr('title');
    $('.dropdownBox a.selected').html(htmlValue);
    $('.dropdownBox input').val('');
  });
  
  $('.emptyDropdowns').click(function(e){
    e.preventDefault();
    $(this).siblings('.dropdownBox').each(function(){
      var htmlValue = $(this).children('a').attr('title');
      $(this).children('a').html(htmlValue);
      $(this).children('input').val('');
    });
  });
  
});

function dropdownBox() {
  $('.dropdownBox a').click(function(e) {
    e.preventDefault();
    if($(this).hasClass('selected') && !$(this).hasClass('active')) {
      $(this).addClass('active');
      $(this).siblings('div').show();
    } else {
      var relValue = $(this).attr('rel');
      var htmlValue = $(this).html();
      if($(this).hasClass('selected')){
        $(this).removeClass('active');
        $(this).siblings('div').hide();
        // $(this).siblings('input').val(relValue);
      } else {
        if(relValue == ''){
          htmlValue = $(this).parent().parent().parent().children('a').attr('title');
        }
        $(this).parent().parent().parent().children('a').removeClass('active');
        $(this).parent().parent().parent().children('a').html(htmlValue);
        $(this).parent().parent().parent().children('div').hide();
        $(this).parent().parent().parent().children('input').val(relValue);
      }
    }
  });
}
function dropdownBoxDark() {
  $('.dropdownBoxDark a').click(function(e) {
    e.preventDefault();
    if($(this).hasClass('selected') && !$(this).hasClass('active')) {
      $(this).addClass('active');
      $(this).siblings('div').show();
    } else {
      var htmlValue = $(this).html();
      if($(this).hasClass('selected')){
        $(this).removeClass('active');
        $(this).siblings('div').hide();
      } else if($(this).hasClass('second')){
        htmlValue = $(this).parent().parent().parent().children('a').attr('title');
        $(this).parent().parent().parent().children('a').html(htmlValue);
        $(this).parent().parent().parent().children('a').removeClass('active');
        $(this).parent().parent().parent().children('div').hide();
      } else {
        $(this).parent().parent().parent().children('a').removeClass('active');
        $(this).parent().parent().parent().children('div').hide();
        if($(this).attr('target') == '_blank'){
          mywindow = window.open($(this).attr('href'));
        } else {
          window.location = $(this).attr('href');
        }
      }
    }
  });
}

function bindHighSlideAjax() {
  $('a.highslideHtml, area.highslideHtml').each(function() {
    this.onclick = function() {
      var ssGroup = $(this).attr('rel');
      var options = '';
      
      if(ssGroup == 'ajax'){
        options = ajaxOptions;
      } else if(ssGroup == 'iframe') { 
        options = iframeOptions;
      } else if(ssGroup == 'video') { 
        options = iframeVideoOptions;
      } else if(ssGroup == 'funding') { 
        options = fundingVideoOptions;
      }else if(ssGroup == 'nettimoto') {
        options = nettimotoOptions;
      }
      
      if(options != ''){
        return hs.htmlExpand( this, options );
      } else {
        return hs.htmlExpand( this );
      }
    };
  });
}
function bindHighSlideImage() {
  $('a.highslideImage, area.highslideImage').each(function() {
    this.onclick = function() {
      var ssGroup = $(this).attr('rel');
      var options = '';
      
      if(ssGroup == 'gallery'){
        options = galleryOptions;
      } else if(ssGroup == 'content') { 
        options = contentOptions;
      }
      
      if(options != ''){
        return hs.expand( this, options );
      } else {
        return hs.expand( this );
      }
    };
  });
}

function infoButton(val){
  var infoBox = $(val).attr('rel');
  var content = $('#'+infoBox);
  if ( content.is(':visible') ) {
    content.hide();
    return false;
  }
  content.removeClass('hidden');
  var boxes = $('table.mediaTable td div');
  boxes.hide();
  content.show();
}
