$(function() {
  $('#searchTextbox').defaultVal('Enter Keywords');
  $('#mainCol div.mainSlider').inSlide();
  $('#mainTabs').mainTabs();
  if(document.getElementById('catMenu')) {
    var cm = new catMenu();
  }
  if(document.getElementById('productFilter')) {
    productFilter();
  }
  if(document.getElementById('cartoonistSelector')) {
   cartoonistSelector(); 
  }
  if(document.getElementById('featuredCartoonistSelector')) {
   featuredCartoonistSelector();
  }
  if(document.getElementById('browseCategories')) {
   browseCategories();
  }
  if(document.getElementById('searchButton')) {
   var sp = new searchProducts(); 
  }
  if(document.getElementById('productArchive')) {
    productArchive();
  }
  if(document.getElementById('productTags')) {
    var pt = new productTags();
  }
});

window.onload = function() {
    $('#logoBubble img').css('visibility','visible');
}


$.fn.mainTabs = function() {
  this.find('a').click(function(e) {
    e.preventDefault();
    $('#mainCol').attr('class',$(this).attr('href').substr(1));
  });
}

$.fn.inSlide = function() {
  var duration, ease, delay;
  duration = 1000;
  ease = 'easeInOutQuart';
  delay = 7000;
  
  return this.each(function() {
    
    var $this, $li, current, max, $dots, timer, hover;
    
    $this = $(this);
    $li = $this.find('ul.frame li');
    current = 0;
    max = $li.length - 1;
    hover = false;
    
    $dots = $this.find('ul.indicators li');
    
    function slideFromRight(num) {
      $li.eq(num).stop().css({left:580}).animate({left: 0}, duration, ease);
      $li.eq(current).stop().animate({left: -580}, duration, ease);
      current = num;
      animDone(num);
    }
    function slideFromLeft(num) {
      $li.eq(num).stop().css({left:-580}).animate({left: 0}, duration, ease);
      $li.eq(current).stop().animate({left: 580}, duration, ease);
      current = num;
      animDone(num);
    }
    function animDone(num) {
      $dots.removeClass('on');
      $dots.eq(num).addClass('on');
      if(!hover) {
        timer = setTimeout(function() { nextSlide(); }, delay);
      }
    }
    function nextSlide() {
      var num = current + 1;
      if(num > max) {
        num = 0;
      }
      slideFromRight(num);
    }
    
    $this.find('a.slideRight').click(function(e) {
      clearTimeout(timer);
      timer = null;
      e.preventDefault();
      var num = current + 1;
      if(num > max) {
        num = 0;
      }
      slideFromRight(num);
    });
    $this.find('a.slideLeft').click(function(e) {
      clearTimeout(timer);
      timer = null;
      e.preventDefault();
      var num = current - 1;
      if(num < 0) {
        num = max;
      }
      slideFromLeft(num);
    });
    $dots.each(function(i) {
      $(this).click(function(e) {
        clearTimeout(timer);
        timer = null;
        e.preventDefault();
        if(i > current) {
          slideFromRight(i);
        }
        else if(i < current) {
          slideFromLeft(i);
        }
      });
    });
    $this.find('img').hover(function() {
      hover = true;
      clearTimeout(timer);
      timer = null;
    },function() {
      hover = false;
      timer = setTimeout(function() { nextSlide(); }, delay);
    });
    timer = setTimeout(function() { nextSlide(); }, delay);
  });
}

function catMenu() {
  var current = "", currentA, timer;
  
  function hideRel(rel,a) {
    if(rel.length > 0) {
      $('#'+rel).hide().unbind();
      $(a).removeClass('hover');
      $('#subCatsArrow').hide();
      current = "";
      currentA = null;
    }
  }
  function showRel(rel,a) {
    if($.browser.msie) {
      $('#'+rel).show().hover(function() { clearTimeout(timer); },function() { hideRel(current,currentA); });
    }
    else {
      $('#'+rel).fadeIn(300).hover(function() { clearTimeout(timer); },function() { hideRel(current,currentA); });
    }
    var pos = $(a).addClass('hover').position();
    $('#subCatsArrow').css({
      display: 'block',
      top: pos.top
    });
  }
  
  $('#catMenu a').hover(function() {
    var rel = $(this).attr('rel');
    if(rel.length > 0) {
      if(current.length > 0) {
        clearTimeout(timer);
        var self = this;
        timer = setTimeout(function() {
          hideRel(current,currentA);
          current = rel;
          currentA = self;
          showRel(rel,self);
        }, 250);
      }
      else {
        current = rel;
        currentA = this;
        showRel(rel,this);
      }
    }
  },function() {
    var past = current;
    var pastA = currentA;
    clearTimeout(timer);
    timer = setTimeout(function() { hideRel(past,pastA); }, 150);
  });
}
  
function productFilter() {
  $('#productFilter input').click(function() {
    window.location = this.value;
  });
}

function cartoonistSelector() {
  $('#cartoonistSelector').change(function() {
    if(this.value.length > 0) {
      window.location = "#" + this.value;
    }
  });
}
function featuredCartoonistSelector() {
  $('#featuredCartoonistSelector').change(function() {
    if(this.value.length > 0) {
      window.location = this.value;
    }
  });
}
function browseCategories() {
  $('#browseCategories').change(function() {
    if(this.value.length > 0) {
      window.location = this.value;
    }
  });
}
  
function searchProducts() {
  function search() {
    var color = $.getQuery('color');
    if(color && color.length > 0) {
      color = '&color='+color;
    }
    else {
     color = ''; 
    }
    var num = $.getQuery('num');
    if(num && num.length > 0) {
      num = '&num='+num;
    }
    else {
     num = ''; 
    }
    var val = $('#searchTextbox').val();
    if(val == 'Enter Keywords' || val == '') {
        return;
    }
    window.location = '/search.aspx?search='+val+color+num;
  }
  $('#searchButton').click(function() {
    search();
  });
  $('#searchTextbox').keypress(function(e) {
    if(e.which == 13) {
     search();
     e.preventDefault();
    }
  });
}
  
function productArchive() {
  $('select.resultNumPerPage').change(function() {
    if(this.value.length > 0) {
      window.location = this.value;
    }
  });
}
  
function productTags() {
  var tags = $('#productTags');
  var html = tags.html();
  if(html.replace(/<.*?>/g,"").length > 60) {
    tags.html('<span id="productTagsFull"></span><span id="productTagsShort"></span>');
    var full = $('#productTagsFull');
    var short = $('#productTagsShort');
    full.html(html).hide();
    var truncate = html.truncate(40,'&hellip; <a class="more" id="productTagsOpen" href="#ReadMore" title="Read More">Read&nbsp;More</a>');
    short.html(truncate);
    $('#productTagsOpen').click(function(e) {
      e.preventDefault();
      short.hide();
      full.show();
    });
  }
}

