$(document).ready(function () {

// External links with REL
    $('a[href^=http://]').each(function(){
			if(this.href.indexOf(location.hostname) == -1) {
				$(this).attr("rel","external");
			}
		});
		$('a[rel="external"]').click(function(){
			if(this.href.indexOf(location.hostname) == -1) { //check if they point outside 
				window.open( $(this).attr('href') ); //force open the new window 
				return false;
			} 
		});  
	

    
// Logo onclick return home
    $('h1#logo').click(function () {
        window.location = "/";
    });
   
    
// Init equal heights
    $('div.content').equalizeCols();
    
    
// Slideshow    
    $("#reel_index").jFlow({
        slides: "#slideshow",
        width: "630px",
        height: "301px",
        duration: 400
    });

    $(".jFlowControl").click(function(e){
        e.preventDefault();
    });
        

// Form input clearing / populating
    $('form input.prefil')
        .focus(function() {
            var val_orig = $(this).prev('label').html();
            if($(this).attr('value') == val_orig) $(this).attr('value', '');
            $(this).addClass('active');
        })
        .blur(function() {
            var val_orig = $(this).prev('label').html();
            if($(this).attr('value') == '') $(this).attr('value', val_orig);
        });
    
    
// Site map magic
    // $('#nav_sub li a').each(function() {
    //         if($(this).parent().find('li').length) {
    //             $(this).addClass('parent');
    //         }
    //     });
    //     
    //     $('#nav_sub ul li:last-child').addClass('last');
    // 
    //     $('#nav_sub li a.parent').click( function(e) {
    //         if ($(this).next('ul').length) {
    //             // e.preventDefault();
    //         }
    //         $(this).parent().children('ul').slideToggle(300,function() {
    //             $(this).parent().find('a:first').toggleClass("open");
    //             $('div.content').equalizeCols(); 
    //         });
    //     });
    //     
    //     $('#nav_sub li a.parent').each(function() {
    //         $(this).parent().find('ul').slideUp();
    //     })
    // 	$('#nav_sub li.current ul').slideDown(300,function() {
    // 		$(this).parent().find('a:first').addClass("open");
    // 		$('div.content').equalizeCols(); 
    // 	});
    //     $('#nav_sub li.current a.current').each(function() {
    //         $(this).parents('ul').slideDown(300,function() {
    //             $(this).parent().find('a:first').addClass("open");
    //             $('div.content').equalizeCols(); 
    //         });
    //     })
    
    
// Image captions
    $('img.captioned').each(function() {
        $(this).wrap('<div class="caption"></div>');
        
        var caption = '<p><span><strong>' + $(this).attr('title') + '</strong>' + $(this).attr('alt') + '</span></p>';
        
        $(this).parent().append(caption);
        
        $(this).parent().width($(this).width() + 19);
        
        $(this).css('margin','0px');
        
        if ($(this).hasClass('right')) {
            $(this).parent().css('float','right');
            $(this).parent().css('margin','0 0 15px 15px');
        }

        $(this).attr('title','');
        $(this).attr('alt','');
    })
    
    $('div.caption p').slideUp('fast');
    
    $('div.caption')
        .mouseenter(function() {
            $('p', $(this)).stop(0,1).slideDown('fast');
        })
        .mouseleave(function() {
            $('p', $(this)).stop(0,1).slideUp('fast');            
        })
        
        
// Form validator
    $("form").submit(function(e){
        var trigger = "0";
        var emailre = /^[a-z0-9._-]+@[a-z0-9.-]+\.[a-z]{2,4}$/i;
        $(".validation").remove();
        $("input").blur();
        $(".required", this).each(function(){
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");
            if ($(this).attr("value") == '') {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">This is a required Field</div>');
                trigger = "1";
            }
            else {
                $(listItem).removeClass("invalid");
            }
        });
        $(".required" + ".email", this).each(function(){
            var listItem = $(this).parent().get(0);
            var theLabel = $(this).attr("id");
            if ((!$(this).attr("value").match(emailre)) && ($(this).attr("value") != '')) {
                $(listItem).addClass("invalid");
                $(listItem).append('<div class="validation">Invalid e-mail, try again</div>');
                trigger = "1";
            }
        });
        if (trigger == "1") {
            e.preventDefault();
        }
        $(".invalid > .required:first").focus();
    });    
});


// Init all Cufon font replacement
    Cufon.replace('#nav_primary li a, form#quick_search button', {
        hover: true   
    });
    
    Cufon.replace('#callouts ul li a, a.more', {
        hover: true,
        textShadow: '1px 1px rgba(0, 0, 0, 0.5)'
    });
    
    Cufon.replace('#newsletter legend span', {
        textShadow: '1px 1px rgba(0, 0, 0, 0.5)'
    });
    
    Cufon.replace('#reel ul#slideshow li div h3', {
        textShadow: '2px 2px rgba(255, 255, 255, 1)'
    })
    
    Cufon.replace('#content h2, #content h3, #reel ul#slideshow li div p');


// Equal height columns
    (function($) {
        $.fn.equalizeCols = function(){
            var height = 0,
                reset = $.browser.msie ? "1%" : "auto";
      
            return this
                .css("height", reset)
                .each(function() {
                    height = Math.max(height, this.offsetHeight);
                })
                .css("height", height)
                .each(function() {
                    var h = this.offsetHeight;
                    if (h > height) {
                        $(this).css("height", height - (h - height));
                    };
                });
        };
    })(jQuery);

/*

    $(".tabs > li > a").click(function(e){
        e.preventDefault();
        var container = $(this).attr("id");
        $(".tabs > li > a").removeClass("current");
        $(".tab_content").hide();
        $("#container_" + container).show();
        $(this).addClass("current");
    });

*/