// remap jQuery to $
var handle_jobs_events = null;
(function($){
    handle_jobs_events = function() {
        var td = $( 'div.tabledata' );
        td.each( function(k,v) {
            var $v = $( v );
            if( !$v.is( '[hasevent]' ) ) {
                $v.attr( 'hasevent', 1 );
                $v.find(".toggle_container").hide();
                $v.find(".trigger").toggle(function(){
                    $(this).addClass("active");
                    $(this).parents().filter('tr').addClass("active");
                }, function () {
                    $(this).removeClass("active");
                    $(this).parents().filter('tr').removeClass("active");
                });
                $v.find(".trigger").click(function(){
                    $(this).next(".toggle_container").slideToggle("normal");
                });
            }
        } );
    }
    /* trigger when page is ready */
    $(document).ready(function (){
        $('#secondary').find('aside:last-child').css('border','none');
        Cufon.replace('.ie header nav ul li a', {fontFamily: 'Gotham Rounded Bold', hover:true});
        Cufon.replace('.ie #main h1', {fontFamily: 'Gotham Rounded Bold', hover:true});
        Cufon.replace('.ie #main h2', {fontFamily: 'Gotham Rounded Bold', hover:true});
        Cufon.replace('.ie #main h3', {fontFamily: 'Gotham Rounded Bold', hover:true});
        Cufon.replace('.ie #main h4', {fontFamily: 'Gotham Rounded Bold', hover:true});
        Cufon.replace('.ie #main h5', {fontFamily: 'Gotham Rounded Bold', hover:true});
        Cufon.replace('.ie #main h6', {fontFamily: 'Gotham Rounded Bold', hover:true});
        Cufon.replace('.ie .find-jobs h1', {fontFamily: 'Gotham Rounded Bold', hover:true});


        /* Twitter & Facebook scrollbars replacement */
        //jQuery(function(){
        jQuery('.twitter ul').jScrollPane();
        jQuery('.facebook ul').jScrollPane();
        jQuery('.social-feeds ul.feeds').jScrollPane();
        jQuery('.make-jscroll-pane' ).jScrollPane();
        //});
    //});
    //$(document).ready(function() {
        $.fn.simpleTabs = function(){
        //Default Action
        $(this).find(".tab_content").hide(); //Hide all content
        $(this).find("ul.tabs li:first a").addClass("buttonPro rounded active")
        $(this).find("ul.tabs li:first").addClass("active").show(); //Activate first tab
        $(this).find(".tab_content:first").show(); //Show first tab content

        //On Click Event
        $("ul.tabs li").click(function() {
            $(this).parent().parent().find("ul.tabs li").removeClass("active"); //Remove any "active" class
            $(this).parent().parent().find("ul.tabs li a").removeClass("buttonPro rounded active");
            $(this).addClass("active"); //Add "active" class to selected tab
            $(this).parent().parent().find(".tab_content").hide(); //Hide all tab content

            var activeTab = $(this).find("a").addClass("buttonPro rounded active");
            var activeTab = $(this).find("a").attr("href"); //Find the rel attribute value to identify the active tab + content
            $(activeTab).fadeIn(); //Fade in the active content
            return false;
        });
        };//end function
        $("div[class^='simpleTabs']").simpleTabs(); //Run function on any div with class name of "Simple Tabs"
    //}); 
    /*------ Accordion ---------*/
    //$(document).ready(function(){
        handle_jobs_events();
    //});

    /*------ Inputs focus-blur and various other stuff ------*/
    //$( document ).ready( function() {
        /**
         * This is John's code for FancyBox
         */
        $(".various").fancybox( {
            fitToView : false,
            width : 940,
            maxHeight   : 778,
            height : 738,
            padding     : 0,
            autoSize : false,
            closeClick : false,
            openEffect : 'none',
            closeEffect : 'none'
        } );
        
        $( '.focus-blur' ).focus( function() {
            var v = $( this ).val();
            if( $( this ).is( '.focus-blur-done' ) )
                return true;
            $( this ).attr( 'init-val', v );
            $( this ).val( '' );
        } );
        $( '.focus-blur' ).blur( function() {
            var v = $( this ).val();
            var i = $( this ).attr( 'init-val' );
            if( typeof i != 'undefined' && i !== false ) {
                if( $.string( v ).blank() )
                    $( this ).val( i );
                else
                    $( this ).addClass( 'focus-blur-done' );
            }
        } );
        /////////////////////////////////
        /*------  ------*/
        /////////////////////////////////
        var ji = $( '#primary.jobs_index' );
        if( ji.length ) {
            ji.find( "#content .simpleTabs ul.tabs li a" ).click( function() {
                var t = $( this );
                var h = t.attr( 'href' );
                var url = jobs_url + '/';
                switch( h ) {
                    default:
                    case '#tab1':
                        url += '7';
                        break;
                    case '#tab2':
                        url += '14';
                        break;
                    case '#tab3':
                        url += '30';
                        break;
                    case '#tab4':
                        url += '90';
                        break;
                }
                $( h ).html( '<p>Loading...</p>' );
                $( h ).load( url, handle_jobs_events );
            } );
        }
        /////////////////////////////////
        /*------ Account sign in ------*/
        /////////////////////////////////
        var asi = $( '.account_sign_in' );
        if( asi.length ) {
            asi.find( 'input[name=authorize]' ).change( function(){
                if( this.checked ) {
                    $( '.authorized' ).find( 'input, textarea, select' ).removeAttr( 'disabled' );
                    $( '.authorized' ).css( 'opacity', '1' )
                } else {
                    $( '.authorized' ).find( 'input, textarea, select' ).attr( 'disabled', 'disabled' );
                    $( '.authorized' ).css( 'opacity', '0.4' )
                }
            } ).change();
        /////////////////////////////////
        /*------ Account Phone ------*/
        /////////////////////////////////
            asi.find( '.phoneNumber' ).parent().click( function(evt) {
                var t = $( evt.target );
                if( t.hasClass( 'remove_number' ) ) {
                    if( ! asi.find( 'input[name=authorize]' ).get(0).checked )
                        return false;
                    t.parent().remove();
                    return false;
                }
            } );
            asi.find( '.add_number' ).click( function() {
                if( ! asi.find( 'input[name=authorize]' ).get(0).checked )
                    return false;
                var tpl = ' ' +
                    '<div class="phoneNumber">' +
                        '<input type="text" value="Number" name="org_number[]" class="focus-blur">' +
                        '<select name="org_number_type[]">' +
                            '<option value="phone">Phone</option>' +
                            '<option value="fax">Fax</option>' +
                        '</select>' +
                        '<a href="#" class="remove_number">remove number</a>' +
                    '</div>';
                asi.find( '.phoneNumber' ).parent().append( tpl );
                asi.find('.phoneNumber select:last').select_skin();
                asi.find('.phoneNumber .focus-blur:last' ).focus( function() {
                    var v = $( this ).val();
                    $( this ).attr( 'init-val', v );
                    $( this ).val( '' );
                } );
                asi.find( '.phoneNumber .focus-blur:last' ).blur( function() {
                    var v = $( this ).val();
                    var i = $( this ).attr( 'init-val' );
                    if( typeof i != 'undefined' && i !== false ) {
                        if( $.string( v ).blank() )
                            $( this ).val( i );
                    }
                } );
                return false;
            } );
        }
        // original nu era pus in event
        /*------ On/Off Switch ---------*/
        $(".switch .button").toggle(function(){
            $(this).addClass("active");
        }, function () {
            $(this).removeClass("active");
        });
    } );
})(window.jQuery);
