// application.js
$(document).ready( function() {
    $("img.rollover").hover(
        function() { this.src = this.src.replace(/\.(\w+)$/, '_h.$1'); },
        function() { this.src = this.src.replace(/_h\.(\w+)$/, '.$1'); }
    );
        
    var pathname = window.location.pathname;
    pathname = /\/[a-z]*\//.exec(pathname);
    if (pathname != null) {
        pathname = pathname.toString().replace(/\//g, "");
        $('#nav-'+ pathname).addClass('expandfirst');
    }
    
    initNav();
    initTabs();
    initGallery();
    $("a[rel='gallery']").colorbox();
    $(".signup").colorbox({width:"500", height:"390",iframe:true});
    //$("#promo-feedback").validate();

    $(".poll-block:first").css('border-top', '1px solid #1f1f1f');

});

function initNav() {
    $('.nav-image').fadeTo(1, 0.35);
    $('.nav-image').hover(
    function() {
        $(this).fadeTo(250, 1);
    }
    , function() {
        $(this).fadeTo(250, 0.45);
    }
    );

    $('ul.menu ul').hide();

    $.each($('ul.menu'), function(){
        $('.expandfirst ul').show();
    });
}

function initTabs()  {
    $('div.image-tab').hover(function() {
        // on hovering over, find the element we want to fade *up*
        var fade = $('> div.tab-active', this);
        fade.fadeIn(350);
    }, function () {
        // on hovering out, fade the element out
        var fade = $('> div.tab-active', this);
        fade.fadeOut(350);
    });
}

function initGallery() {
    $('#thumbs li img').fadeTo(250, 0.4);
    $('#thumbs li img').hover( function() {
            $(this).fadeTo(250, 1);
        }, function (){
            $(this).fadeTo(250, 0.4);
        }
    );
    $('#thumbs li img').click(function() {
        var type = $(this).attr('class');


        var img = $(this).attr('src');
        img = img.replace(/_t/, "");
        if (type == 'fade') {
            var fade = img.replace(/\.jpg/, "_fade.jpg");
            $('#gallery-image').html('<img class="ifader" src="' + fade +'" /><img src="' + img + '" />');
            $('img.ifader').fadeOut(2000);
        } else {
            $('#gallery-image').html('<img src="' + img +'" />');
        }
    });
}

jQuery.fn.simpleImageRollover = function(preload) {
    if (preload) {
        this.each(function() {
            var i = new Image;
            i.src = this.src;
        });
    }
    this.hover(
        function() { this.src = this.src.replace(/\.(\w+)$/, '_h.$1'); },
        function() { this.src = this.src.replace(/_h\.(\w+)$/, '.$1'); }
    );
};

