$(document).ready(function(){

$(function() {
	$('#homeSlideshow').cycle({ 
		fx:     'fade', 
		speed:  1000, 
		timeout: 3000,
		pager:  '#slideNav',
		pause: 'true',
		pauseOnPageHover: 'true'
	});
});


$('#playButton').hide();
$('#stopButton').click(function() { 
    $('#homeSlideshow').cycle('pause');
	$(this).hide(); 
	$('#playButton').show();
});
$('#playButton').click(function() { 
    $('#homeSlideshow').cycle('resume');
	$(this).hide();
	$('#stopButton').show(); 
});


// Carousel

$("#categoryNav a:first").css({"margin-left": 0});
$("#categoryNav a:last").css({"margin-right": 0});

$("#categoryNav a.active").click(function(){return false;});

var n = $("ul.carouselList li").length;
var w = n * 182;
$(".carouselList").width(w);
var position = $(".carouselList").position();
var theLeft = w - w - w + 728;
var checkLeft = theLeft + 728;

$(".carouselList").ready(function(){
	if(n <= 4){
		$('.next').hide();
		$('.prev').hide();
	}
});

$('.next').click(function(){
  var pos = $(".carouselList").position();	
	if(pos.left < w &&  pos.left > theLeft) {
	if(pos.left <= checkLeft) {
      $('.next').fadeOut('fast');
	}
        $('.carousel ul').animate({left:'-=728px'}, 1000);
	  }
  $('.prev').fadeIn('fast');
  return false;	
});

$('.prev').click(function(){
  var pos = $(".carouselList").position();	
	if(pos.left < 0 ) {
        $('.carousel ul').animate({left:'+=728px'}, 1000);
	if(pos.left >= -728) {
        $('.prev').fadeOut('fast');
	}
	 }
  $('.next').fadeIn('fast');
  return false;	
});

  // Carousel Content Swap	
$("#carouselContent div:first").addClass('visible');
$('#carouselContent div').hide();
$('.visible').show();

$(".carouselList li:first").addClass('activeTmb');

$('.carouselList li').hover(
  function () {
	 if ( $(this).hasClass('activeTmb') ) {
       $(this).css({border: "1px solid #7c9e04"});
      } else {  
    $(this).css({border: "1px solid #7c9e04"});
	  }
  },
  function () {
     if ( $(this).hasClass('activeTmb') ) {
       $(this).css({border: "1px solid #7c9e04"});
      } else {  
    $(this).css({border: "1px solid #eddfd2"});
	  }
  }
);

  
$('.carouselList a').click(function() {
	$('.carouselList li').removeClass('activeTmb').css({border: "1px solid #eddfd2"});	
	$(this).parent().addClass('activeTmb').css({border: "1px solid #7c9e04"});
	var href = $(this).attr('href');
	
	
  $('#carouselContent div:visible').animate({opacity: 0}, 500, function() {
    $('#carouselContent div').hide().css({opacity: 1});
	$(href).fadeIn(800);

	
   });	
	return false;
  });


});

function externalLink() { 
 if (!document.getElementsByTagName) return; 
 var anchors = document.getElementsByTagName("a"); 
 for (var i=0; i<anchors.length; i++) { 
   var anchor = anchors[i]; 
   if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") { 
       anchor.target = "_blank"; 
   }
 } 
} 
window.onload = externalLink;
