$(document).ready(function() {

// Open External Links in New Window --------------------//	
  	$("a[href^='http:']").not("[href*='bluelinepaddlesurf.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();
		});
	};
	
// Fading Links --------------------//
	$("#logo span, .mainmenu span, #social span, .submenu span, #storelink span, #leftexpand span").css("opacity","0");
	$("#logo span, .mainmenu span, #social span, .submenu span, #storelink span, #leftexpand span").hover(function (){
		$(this).stop().animate({opacity: 1}, 200);
	},
	function (){
		$(this).stop().animate({opacity: 0}, 200);
	});
	
	$(".popup span").css("opacity","0.6");
	$(".popup span").hover(function (){
		$(this).stop().animate({opacity: 1}, 200);
	},
	function (){
		$(this).stop().animate({opacity: 0.6}, 200);
	});

	$(".nthumb a, .thumbrow a, .aboutimg a, .featimg a").css("opacity","1");
	$(".nthumb a, .thumbrow a, .aboutimg a, .featimg a").hover(function (){
		$(this).stop().animate({opacity: 0.25}, 200);
	},
	function (){
		$(this).stop().animate({opacity: 1}, 200);
	});
	
	$(".boardbox span, .paddlebox span").css("opacity","0");
	$(".boardbox span, .paddlebox span").hover(function (){
		$(this).stop().animate({opacity: 1}, 200);
	},
	function (){
		$(this).stop().animate({opacity: 0}, 200);
	});

// About Image Zoom --------------------//	
/*
	$("img.aboutimgleft, img.aboutimgright, img.aboutimgbleft, img.aboutimgbright").hover(function (){
		$(this).stop().animate({width: [570, 'easeOutQuint'], height: [330, 'easeOutQuint']}, 200);
	},
	function (){
		$(this).stop().animate({width: [300, 'easeOutQuart'], height: [174, 'easeOutQuart']}, 400);
	});
*/
						   
// Subnav --------------------//
	$(function () {
        $(".bubbleInfo").each(function () {
            var distance1 = 0;
            var distance2 = 0;
            var time = 250;
            var hideDelay = 100;

            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: 38,
                        left: 15,
                        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 --------------------//
	$(".boardbox a, .paddlebox a, a[rel='gal'], .riderphoto a, .shaperinfo a").colorbox({speed:400, opacity:1, current:'{current} / {total}'});
	$("a[rel='ytvid']").colorbox({iframe:true, innerWidth:640, innerHeight:385});
	$("a[rel='vmvid']").colorbox({iframe:true, innerWidth:640, innerHeight:360});
	$("a[rel='llvid']").colorbox({iframe:true, innerWidth:640, innerHeight:400});
	$("a.viewmap").colorbox({iframe:true, innerWidth:1000, innerHeight:600});
	
	$("a.shp-1").colorbox({inline:true, href:".cbox_shp1"});
	
	$("a.b1, a.p1, a.bio-1").colorbox({inline:true, href:".cbox_b1"});
	$("a.b2, a.p2, a.bio-2").colorbox({inline:true, href:".cbox_b2"});
	$("a.b3, a.p3, a.bio-3").colorbox({inline:true, href:".cbox_b3"});
	$("a.b4, a.p4, a.bio-4").colorbox({inline:true, href:".cbox_b4"});
	$("a.b5, a.p5, a.bio-5").colorbox({inline:true, href:".cbox_b5"});
	$("a.b6, a.p6, a.bio-6").colorbox({inline:true, href:".cbox_b6"});
	$("a.b7, a.p7, a.bio-7").colorbox({inline:true, href:".cbox_b7"});
	$("a.b8, a.p8, a.bio-8").colorbox({inline:true, href:".cbox_b8"});
	$("a.b9, a.p9, a.bio-9").colorbox({inline:true, href:".cbox_b9"});
	$("a.b10, a.p10, a.bio-10").colorbox({inline:true, href:".cbox_b10"});
	$("a.b11, a.p11, a.bio-11").colorbox({inline:true, href:".cbox_b11"});
	$("a.b12, a.p12, a.bio-12").colorbox({inline:true, href:".cbox_b12"});
	$("a.b13, a.p13, a.bio-13").colorbox({inline:true, href:".cbox_b13"});
	$("a.b14, a.p14, a.bio-14").colorbox({inline:true, href:".cbox_b14"});
	$("a.b15, a.p15, a.bio-15").colorbox({inline:true, href:".cbox_b15"});
	$("a.b16").colorbox({inline:true, href:".cbox_b16"});
	$("a.b17").colorbox({inline:true, href:".cbox_b17"});
	$("a.b18").colorbox({inline:true, href:".cbox_b18"});
	$("a.b19").colorbox({inline:true, href:".cbox_b19"});
	$("a.b20").colorbox({inline:true, href:".cbox_b20"});
	
	$(".set2 a.b1").colorbox({inline:true, href:".cbox2_b1"});
	$(".set2 a.b2").colorbox({inline:true, href:".cbox2_b2"});
	$(".set2 a.b3").colorbox({inline:true, href:".cbox2_b3"});
	$(".set2 a.b4").colorbox({inline:true, href:".cbox2_b4"});
	$(".set2 a.b5").colorbox({inline:true, href:".cbox2_b5"});
	$(".set2 a.b6").colorbox({inline:true, href:".cbox2_b6"});
	$(".set2 a.b7").colorbox({inline:true, href:".cbox2_b7"});
	$(".set2 a.b8").colorbox({inline:true, href:".cbox2_b8"});
	$(".set2 a.b9").colorbox({inline:true, href:".cbox2_b9"});
	$(".set2 a.b10").colorbox({inline:true, href:".cbox2_b10"});
	$(".set2 a.b11").colorbox({inline:true, href:".cbox2_b11"});
	$(".set2 a.b12").colorbox({inline:true, href:".cbox2_b12"});
	$(".set2 a.b13").colorbox({inline:true, href:".cbox2_b13"});
	$(".set2 a.b14").colorbox({inline:true, href:".cbox2_b14"});
	$(".set2 a.b15").colorbox({inline:true, href:".cbox2_b15"});
	$(".set2 a.b16").colorbox({inline:true, href:".cbox2_b16"});
	$(".set2 a.b17").colorbox({inline:true, href:".cbox2_b17"});
	$(".set2 a.b18").colorbox({inline:true, href:".cbox2_b18"});
	$(".set2 a.b19").colorbox({inline:true, href:".cbox2_b19"});
	$(".set2 a.b20").colorbox({inline:true, href:".cbox2_b20"});
	
	$(".set3 a.b1").colorbox({inline:true, href:".cbox3_b1"});
	$(".set3 a.b2").colorbox({inline:true, href:".cbox3_b2"});
	$(".set3 a.b3").colorbox({inline:true, href:".cbox3_b3"});
	$(".set3 a.b4").colorbox({inline:true, href:".cbox3_b4"});
	$(".set3 a.b5").colorbox({inline:true, href:".cbox3_b5"});
	$(".set3 a.b6").colorbox({inline:true, href:".cbox3_b6"});
	$(".set3 a.b7").colorbox({inline:true, href:".cbox3_b7"});
	$(".set3 a.b8").colorbox({inline:true, href:".cbox3_b8"});
	$(".set3 a.b9").colorbox({inline:true, href:".cbox3_b9"});
	$(".set3 a.b10").colorbox({inline:true, href:".cbox3_b10"});
	$(".set3 a.b11").colorbox({inline:true, href:".cbox3_b11"});
	$(".set3 a.b12").colorbox({inline:true, href:".cbox3_b12"});
	$(".set3 a.b13").colorbox({inline:true, href:".cbox3_b13"});
	$(".set3 a.b14").colorbox({inline:true, href:".cbox3_b14"});
	$(".set3 a.b15").colorbox({inline:true, href:".cbox3_b15"});
	$(".set3 a.b16").colorbox({inline:true, href:".cbox3_b16"});
	$(".set3 a.b17").colorbox({inline:true, href:".cbox3_b17"});
	$(".set3 a.b18").colorbox({inline:true, href:".cbox3_b18"});
	$(".set3 a.b19").colorbox({inline:true, href:".cbox3_b19"});
	$(".set3 a.b20").colorbox({inline:true, href:".cbox3_b20"});
	
	$(".set4 a.b1").colorbox({inline:true, href:".cbox4_b1"});
	$(".set4 a.b2").colorbox({inline:true, href:".cbox4_b2"});
	$(".set4 a.b3").colorbox({inline:true, href:".cbox4_b3"});
	$(".set4 a.b4").colorbox({inline:true, href:".cbox4_b4"});
	$(".set4 a.b5").colorbox({inline:true, href:".cbox4_b5"});
	$(".set4 a.b6").colorbox({inline:true, href:".cbox4_b6"});
	$(".set4 a.b7").colorbox({inline:true, href:".cbox4_b7"});
	$(".set4 a.b8").colorbox({inline:true, href:".cbox4_b8"});
	$(".set4 a.b9").colorbox({inline:true, href:".cbox4_b9"});
	$(".set4 a.b10").colorbox({inline:true, href:".cbox4_b10"});
	$(".set4 a.b11").colorbox({inline:true, href:".cbox4_b11"});
	$(".set4 a.b12").colorbox({inline:true, href:".cbox4_b12"});
	$(".set4 a.b13").colorbox({inline:true, href:".cbox4_b13"});
	$(".set4 a.b14").colorbox({inline:true, href:".cbox4_b14"});
	$(".set4 a.b15").colorbox({inline:true, href:".cbox4_b15"});
	$(".set4 a.b16").colorbox({inline:true, href:".cbox4_b16"});
	$(".set4 a.b17").colorbox({inline:true, href:".cbox4_b17"});
	$(".set4 a.b18").colorbox({inline:true, href:".cbox4_b18"});
	$(".set4 a.b19").colorbox({inline:true, href:".cbox4_b19"});
	$(".set4 a.b20").colorbox({inline:true, href:".cbox4_b20"});
	
// Showbox Slides --------------------//
	$("#showbox-nav").css("opacity","0");
	$("#showbox").hover(function (){
		$("#showbox-nav").stop().animate({opacity: 1}, 400);
	},
	function (){
		$("#showbox-nav").stop().animate({opacity: 0}, 200);
	});

	$('#showbox-slides').before('<div id="showbox-nav">').cycle({ 
		fx: 'scrollHorz',
		before: function(curr, next, opts) {
			opts.animOut.opacity = 0;
		},
		speed: 1200, 
		easing: 'easeOutQuad',
		timeout: 8000,
		pause: 1,
		sync: 1,
		pager: '#showbox-nav'
	});
	
// Vids Slides --------------------//
	$("#showbox-nav2").css("opacity","0").append('<p>Choose a featured video:</p>');
/*
	$("#showboxvids").hover(function (){
		$("#showbox-nav2").stop().animate({opacity: 1}, 400);
	},
	function (){
		$("#showbox-nav2").stop().animate({opacity: 0}, 200);
	});
*/

	$('#showboxvids-slides').before('<div id="pager"><p>Choose a featured video:</p><div id="showbox-nav2">').cycle({ 
		fx: 'scrollHorz',
		before: function(curr, next, opts) {
			opts.animOut.opacity = 0;
		},
		speed: 1200, 
		easing: 'easeOutQuad',
		timeout: 0,
		sync: true,
		pager: '#showbox-nav2'
	});
	
// Board Tabs --------------------//
	$("ul#boardtabs li").click(function() {

		$("ul#boardtabs 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(400);
		
		return false;
		
	});
	
// Contact Form --------------------//
	
	$('#contact-form').jqTransform();
	$("button").click(function(){
		$(".formError").hide();
	});
	
	var use_ajax=true;
	$.validationEngine.settings={};
	
	$("#contact-form").validationEngine({
		inlineValidation: false,
		promptPosition: "centerRight",
		success :  function(){use_ajax=true},
		failure : function(){use_ajax=false;}
	 })
	
	$("#contact-form").submit(function(e){
			if(!$('#subject').val().length)
			{
				$.validationEngine.buildPrompt(".jqTransformSelectWrapper","* This field is required","error")
				return false;
			}
			
			if(use_ajax)
			{
				$('#loading').css('visibility','visible');
				$.post('submit.php',$(this).serialize()+'&ajax=1',
				
					function(data){
						if(parseInt(data)==-1)
							$.validationEngine.buildPrompt("#captcha","* Wrong verification number!","error");
						
						else
						{
							$("#contact-form").hide('slow').after('<h3 class="big">Success!</h3><p>Thank you for sending us a message. We will get back to you shortly!</p>');
						}
						$('#loading').css('visibility','hidden');
					}
				);
			}
			e.preventDefault();
	})

});
