$(document).ready(function() {

// Open External Links in New Window --------------------//	
  	$("a[href^='http:']").not("[href*='boardworkssup.com'], [href*='boardworkssurf.com']").attr('target','_blank');
						   
// IE Cleartype Fix	--------------------//				   
	$.fn.fadeIn = function(speed, callback) {
		return this.animate({
			opacity: 'show' }, speed, function() {
				if ($.browser.msie)
					this.style.removeAttribute('filter');
				if (typeof callback == 'function')
					callback();
		});
	};
	
	$.fn.fadeOut = function(speed, callback) {
		return this.animate({
			opacity: 'hide' }, speed, function() {
				if (jQuery.browser.msie)
					this.style.removeAttribute('filter');
				if (typeof callback == 'function') callback();
		});
	};
	
	$.fn.fadeTo = function(speed,to,callback) {
		return this.animate({opacity: to }, speed, function() {
			if (to == 1 && $.browser.msie)
				this.style.removeAttribute('filter');
			if (typeof callback == 'function') callback();
		});
	};
		
// Subscribe Form --------------------//
	$(".text").focus(function(e){
		$(this).addClass("active");
	});
	$(".text").blur(function(e){
		$(this).removeClass("active");
	});
	
	$("#subs").focus(function(){
		if($(this).attr("value") == "Email Address")
			$(this).attr("value", "");
	});
	$("#subs").blur(function(){
		if($(this).attr("value") == "")
			$(this).attr("value", "Email Address");
	});
	
// Fading Links --------------------//
	$("#bcinside ul span, ul.boardmenu span, #pcinside ul span, ul.paddlemenu span, .axslider ul span").animate({opacity: 0});
	$("#bcinside ul span, ul.boardmenu span, #pcinside ul span, ul.paddlemenu span, .axslider ul span").hover(function (){
		$(this).stop().animate({opacity: 1}, 150);
	}, function (){
		$(this).stop().animate({opacity: 0}, 150);
	});
						   
// Subnav --------------------//
	$(".sf-menu li.boards a, .sf-menu li.paddles a").hover(function(){ 
		$(this).parent().find("ul.boardmenu, ul.paddlemenu").stop(true, true).slideDown(600, 'easeOutExpo');
		$(this).parent().hover(function() {
		}, function() {
			if($.browser.msie){
				$(this).parent().find("ul.boardmenu, ul.paddlemenu").hide();
			}else{
				$(this).parent().find("ul.boardmenu, ul.paddlemenu").fadeOut(200, 'easeOutExpo');
			}
		return false; 
		});
	});
	
// Footer Boards Pop-Up --------------------//	
	$(function () {
        $(".bubbleInfo").each(function () {
            var distance1 = 10;
            var distance2 = -10;
            var time = 350;
            var hideDelay = 350;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
            var trigger = $('.trigger', this);
            var info = $('.popup', this).css('opacity', 0);


            $([trigger.get(0), info.get(0)]).mouseover(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    // reset position of info box
                    beingShown = true;

                    info.css({
                        top: -144,
                        left: 95,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance2 + 'px',
                        opacity: 1
                    }, time, 'swing', function() {
                        beingShown = false;
                        shown = true;
                    });
                }

                return false;
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    info.animate({
                        top: '-=' + distance1 + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
                    });

                }, hideDelay);

                return false;
            });
        });
    });

// Colorbox --------------------//
	$("#bcinside ul li a, a[rel='bgal'], #pcinside ul li a, a[rel='ytvid'], a[rel='vmvid'], a[rel='llvid'], a[rel='tgal-1'], a[rel='tgal-2'], a[rel='tgal-3'], a[rel='tgal-4'], a[rel='tgal-5'], a[rel='tgal-6'], a[rel='tgal-7'], a[rel='tgal-8'], a[rel='tgal-9'], a[rel='tgal-10'], .bio a, .empbox a, .vids a").colorbox({speed:400, opacity:1, current:'{current} / {total}'});
	$("a[rel='ytvid'], .vids a.yt").colorbox({iframe:true, innerWidth:640, innerHeight:385});
	$("a[rel='vmvid'], .vids a.vm").colorbox({iframe:true, innerWidth:640, innerHeight:360});
	$("a[rel='llvid'], .vids a.ll").colorbox({iframe:true, innerWidth:640, innerHeight:400});
	
	$("a.board-1, a.paddle-1, a.bio-1, a.emp-1").colorbox({inline:true, href:".cboxboard_1"});
	$("a.board-2, a.paddle-2, a.bio-2, a.emp-2").colorbox({inline:true, href:".cboxboard_2"});
	$("a.board-3, a.paddle-3, a.bio-3, a.emp-3").colorbox({inline:true, href:".cboxboard_3"});
	$("a.board-4, a.paddle-4, a.bio-4, a.emp-4").colorbox({inline:true, href:".cboxboard_4"});
	$("a.board-5, a.paddle-5, a.bio-5, a.emp-5").colorbox({inline:true, href:".cboxboard_5"});
	$("a.board-6, a.paddle-6, a.bio-6, a.emp-6").colorbox({inline:true, href:".cboxboard_6"});
	$("a.board-7, a.paddle-7, a.bio-7, a.emp-7").colorbox({inline:true, href:".cboxboard_7"});
	$("a.board-8, a.paddle-8, a.bio-8, a.emp-8").colorbox({inline:true, href:".cboxboard_8"});
	$("a.board-9, a.paddle-9, a.bio-9, a.emp-9").colorbox({inline:true, href:".cboxboard_9"});
	$("a.board-10, a.paddle-10, a.bio-10, a.emp-10").colorbox({inline:true, href:".cboxboard_10"});
	$("a.board-11").colorbox({inline:true, href:".cboxboard_11"});
	$("a.board-12").colorbox({inline:true, href:".cboxboard_12"});
	$("a.board-13").colorbox({inline:true, href:".cboxboard_13"});
	$("a.board-14").colorbox({inline:true, href:".cboxboard_14"});
	$("a.board-15").colorbox({inline:true, href:".cboxboard_15"});
	$("a.board-16").colorbox({inline:true, href:".cboxboard_16"});
	$("a.board-17").colorbox({inline:true, href:".cboxboard_17"});
	$("a.board-18").colorbox({inline:true, href:".cboxboard_18"});
	$("a.board-19").colorbox({inline:true, href:".cboxboard_19"});
	$("a.board-20").colorbox({inline:true, href:".cboxboard_20"});
	$("a.board-21").colorbox({inline:true, href:".cboxboard_21"});
	$("a.board-22").colorbox({inline:true, href:".cboxboard_22"});
	$("a.board-23").colorbox({inline:true, href:".cboxboard_23"});
	$("a.board-24").colorbox({inline:true, href:".cboxboard_24"});
	$("a.board-25").colorbox({inline:true, href:".cboxboard_25"});
	$("a.board-26").colorbox({inline:true, href:".cboxboard_26"});
	$("a.board-27").colorbox({inline:true, href:".cboxboard_27"});
	$("a.board-28").colorbox({inline:true, href:".cboxboard_28"});
	$("a.board-29").colorbox({inline:true, href:".cboxboard_29"});
	$("a.board-30").colorbox({inline:true, href:".cboxboard_30"});
	
	$("a.ax1-1").colorbox({inline:true, href:".box_1-1"});
	$("a.ax1-2").colorbox({inline:true, href:".box_1-2"});
	$("a.ax1-3").colorbox({inline:true, href:".box_1-3"});
	$("a.ax1-4").colorbox({inline:true, href:".box_1-4"});
	$("a.ax1-5").colorbox({inline:true, href:".box_1-5"});
	$("a.ax1-6").colorbox({inline:true, href:".box_1-6"});
	$("a.ax1-7").colorbox({inline:true, href:".box_1-7"});
	$("a.ax1-8").colorbox({inline:true, href:".box_1-8"});
	$("a.ax1-9").colorbox({inline:true, href:".box_1-9"});
	$("a.ax1-10").colorbox({inline:true, href:".box_1-10"});
	
	$("a.ax2-1").colorbox({inline:true, href:".box_2-1"});
	$("a.ax2-2").colorbox({inline:true, href:".box_2-2"});
	$("a.ax2-3").colorbox({inline:true, href:".box_2-3"});
	$("a.ax2-4").colorbox({inline:true, href:".box_2-4"});
	$("a.ax2-5").colorbox({inline:true, href:".box_2-5"});
	$("a.ax2-6").colorbox({inline:true, href:".box_2-6"});
	$("a.ax2-7").colorbox({inline:true, href:".box_2-7"});
	$("a.ax2-8").colorbox({inline:true, href:".box_2-8"});
	$("a.ax2-9").colorbox({inline:true, href:".box_2-9"});
	$("a.ax2-10").colorbox({inline:true, href:".box_2-10"});
	
	$("a.ax3-1").colorbox({inline:true, href:".box_3-1"});
	$("a.ax3-2").colorbox({inline:true, href:".box_3-2"});
	$("a.ax3-3").colorbox({inline:true, href:".box_3-3"});
	$("a.ax3-4").colorbox({inline:true, href:".box_3-4"});
	$("a.ax3-5").colorbox({inline:true, href:".box_3-5"});
	$("a.ax3-6").colorbox({inline:true, href:".box_3-6"});
	$("a.ax3-7").colorbox({inline:true, href:".box_3-7"});
	$("a.ax3-8").colorbox({inline:true, href:".box_3-8"});
	$("a.ax3-9").colorbox({inline:true, href:".box_3-9"});
	$("a.ax3-10").colorbox({inline:true, href:".box_3-10"});
	
	$("a.ax4-1").colorbox({inline:true, href:".box_4-1"});
	$("a.ax4-2").colorbox({inline:true, href:".box_4-2"});
	$("a.ax4-3").colorbox({inline:true, href:".box_4-3"});
	$("a.ax4-4").colorbox({inline:true, href:".box_4-4"});
	$("a.ax4-5").colorbox({inline:true, href:".box_4-5"});
	$("a.ax4-6").colorbox({inline:true, href:".box_4-6"});
	$("a.ax4-7").colorbox({inline:true, href:".box_4-7"});
	$("a.ax4-8").colorbox({inline:true, href:".box_4-8"});
	$("a.ax4-9").colorbox({inline:true, href:".box_4-9"});
	$("a.ax4-10").colorbox({inline:true, href:".box_4-10"});
	
	$("a.novid").colorbox({inline:true, href:".cbox_novid"});
	
// nfoTabs --------------------//
	$("ul#nfotabs li, ul#nfotabs-big li").click(function() {

		$("ul#nfotabs li, ul#nfotabs-big li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab-content").hide(); //Hide all tab content

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).stop(true, true).fadeIn(1000);
		
		return false;
		
	});
	
// jCarouselLite --------------------//
	$("#bcinside, #pcinside").jCarouselLite({
		btnNext: ".next",
		btnPrev: ".prev",
		circular: false,
		visible: 6,
		easing: "easeOutExpo",
		speed: 400,
		mouseWheel: true
	});
	
	$("#traction-in").jCarouselLite({
		btnNext: ".ax1-next",
		btnPrev: ".ax1-prev",
		circular: false,
		visible: 3,
		easing: "easeOutExpo",
		speed: 400,
		mouseWheel: true
	});
	
	$("#leashes-in").jCarouselLite({
		btnNext: ".ax2-next",
		btnPrev: ".ax2-prev",
		circular: false,
		visible: 3,
		easing: "easeOutExpo",
		speed: 400,
		mouseWheel: true
	});
	
	$("#fins-in").jCarouselLite({
		btnNext: ".ax3-next",
		btnPrev: ".ax3-prev",
		circular: false,
		visible: 3,
		easing: "easeOutExpo",
		speed: 400,
		mouseWheel: true
	});
	
	$("#gear-in").jCarouselLite({
		btnNext: ".ax4-next",
		btnPrev: ".ax4-prev",
		circular: false,
		visible: 3,
		easing: "easeOutExpo",
		speed: 400,
		mouseWheel: true
	});
	
	$("#bcbtn-prev a, #bcbtn-next a, #pcbtn-prev a, #pcbtn-next a, #tbtn-prev a, #tbtn-next a").click(function() {
		return false;
	});

// Store Locator --------------------//
	$("ul#regbox1 li, ul#regbox2 li").click(function(e) {

		e.preventDefault();

		$("ul#regbox1 li, ul#regbox2 li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".sl-content").hide(); //Hide all tab content

		var el = $(this).find('a'),
		rgn = el.attr('dataregion'),
		hdr = '<h2>' + rgn + '</h2>';
		$('#sl-container h2').remove();
		$(hdr).hide().prependTo('#sl-container').fadeIn();

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).stop(true, true).fadeIn(1000);
				
	});
	$('.slboxsingle:nth-child(4n+1)').css("marginLeft", "0"); // Give IE some love 
	
// About Fader --------------------//
	$('.aboutfader').cycle({ 
		fx:     'fade', 
		speed:   2500,
		timeout: 5000,
		pause:   1,
		random:  1
	 }); 
	 
// Showcase Fader --------------------//
	$('#showbox-fader').before('<div id="showbox-nav">').cycle({ 
		fx: 'fade', 
		speed: 1500,
		timeout: 8000,
		pause: 0,
		pager: '#showbox-nav'
	});

// Subscribe Submit ---------------//
	// Also referenced in globalwp.js since they decided to copy all assets into wp instead of linking
	$('#signup').submit(function() {
		// Prepare query string and send AJAX request
		$.ajax({
			url: 'http://boardworkssup.com/inc/sform-send.php', 
			data: 'ajax=true&email=' + escape($('#subs').val()),
			success: function(msg) {
				$('#signup-error').remove();
				$('#signup-response')
					.append(msg)
					.fadeIn('slow')
					.delay(3000)
					.fadeOut('slow');
			}
		});
	
		return false;
	});	

});
