/**************************************************************************
 * PC Specific
 **************************************************************************/

$(document).ready(function(){

    /**************************************************************************
     * Main Navigation Dropdowns
     **************************************************************************/ 

    $('#nav .dropdown').mouseenter( function() {
            $(this).addClass('now');
            var dropdownMenu = $(this).find('.dropdown-menu:not(.sub)').show(),
                dropdownLinkHeight = $(this).height(),
                offset = $(this).offset();
                leftPosition = $(dropdownMenu).width();
            $(dropdownMenu).css( {'top' : dropdownLinkHeight} );
            // $(this).find('.dropdown-menu').css({'display' : 'block'});
        }).mouseleave( function (e) {
            $(this).removeClass('now');
            $(this).find('.dropdown-menu').fadeOut('fast');
            // $(this).find('.dropdown-menu').css({'display' : 'none'});
        });

		// Hierarchical Submenus
		dropdownSubOver = function() {
            $(this).addClass('now');
            $(this).find('dropdown-sub-link').addClass('now');
            var dropdownMenu = $(this).find('.dropdown-menu').show(),
                dropdownLinkHeight = $(this).height(),
                offset = $(this).offset();
                leftPosition = $(dropdownMenu).width();
            $(dropdownMenu).css( {'top' : 0 , 'left' : 181} );
            // $(this).find('.dropdown-menu').css({'display' : 'block'});
        };

		dropdownSubOut = function (e) {
            $(this).removeClass('now');
            $(this).find('dropdown-sub-link').removeClass('now');
            $(this).find('.dropdown-menu').fadeOut('fast');
            // $(this).find('.dropdown-menu').css({'display' : 'none'});
        };

		var configSub = {
			over: dropdownSubOver,
			timeout: 250,
			out: dropdownSubOut
			// sensitivity: 10
			// interval: 250
		};

    	$('#nav .dropdown-sub').hoverIntent(configSub);

    // $('#nav .dropdown-menu').mouseenter( function() {
    //         $(this).prev().addClass('now');
    //         $(this).css({'display' : 'block'});
    //     }).mouseleave( function() {
    //         $(this).prev().removeClass('now');
    //         $(this).css({'display' : 'none'});
    //     });
    // 
    // $('#nav ul li').mouseenter( function() {
    //         $(this).addClass('now');
    //     }).mouseleave( function (e) {
    //         $(this).removeClass('now');
    //     });


    /**************************************************************************
     * User Notifification panel close
     **************************************************************************/ 

    $('.close').click( function(e){
        $(e.target).parent().fadeOut();
        $(e.target).parent().parent().slideUp('slow');
        e.preventDefault();
    })


    /**************************************************************************
     * Help Toggle
     **************************************************************************/ 

    var $helpClone = $('#help-content-wrapper').clone(true).addClass('clone').css({'position':'relative','top':'0','left':'0'});

    // is <hr> after the <h1> present?
    if ( $('hr.page-heading').length ) {
        // it is so insert help after the <hr>
        $helpClone.insertAfter($('hr.page-heading'))
        $('#help-content-wrapper.clone').css('margin-top','-1px')
        // $('hr.page-heading').css('margin-top','5px')
    }
    // it is <div class="subhead"> after the <h1> present?
    else if ( $('div.subhead').length) {
        // it is so insert help after div.subhead
        $helpClone.insertAfter($('div.subhead'))
        $('#help-content-wrapper.clone').css('margin-top','0')
    }
    // default to insert help after the h1 since neither div.subhead or hr is present
    else {
        $helpClone.insertAfter($('h1'));
        $('#help-content-wrapper.clone').css('margin-top','0')
    }

    $('#help-content-wrapper:not(.clone)').remove();

    var $clonedHelp = $('#help-content-wrapper.clone'),
        helpHeight = $clonedHelp.height();

    $clonedHelp.hide().css({ height : 0 });

    $('#help-close').click(function () {
        $clonedHelp.animate( { height: 0 }, { duration: 500, complete: function () {
            $clonedHelp.hide()
            $('#help-open').fadeIn();
            }
        });
        return false
    });
    
    $('#help-open').click(function () {
        if ( $clonedHelp.is(':visible') ) {
            $clonedHelp.animate({ height: 0 }, { duration: 500, complete: function () {
                $clonedHelp.hide();
                $('#help-open').fadeIn();
            }
        });
        } else {
            $clonedHelp.show().animate({ height : helpHeight }, { duration: 250 });
            $(this).fadeOut();
        }
      return false;
    });

    /**************************************************************************
     * Checkbox Select/Deselect All
     **************************************************************************/ 

    $('.all-items-checkbox').click(function(e) {
        var checked_status = this.checked;
        $("tbody input:checkbox").each(function() {
            this.checked = checked_status;
        });
    });


    /**************************************************************************
     * Imageless buttons and their dropdown
     **************************************************************************/ 

    $('.pc-button:not(.pc-button-disabled)').hover( function() {
            $(this).addClass('pc-button-hover');
        },
        function() {
            $(this).removeClass('pc-button-hover');
        }
    );

    $('.pc-button:not(.pc-button-disabled)').mousedown( function() {
        $(this).addClass('pc-button-active');
    }).mouseup( function() {
        $(this).removeClass('pc-button-active');
    });

    var buttonDropdownToggle =  $('.pc-button-dropdown').toggle(
        function(e){
            $(this).addClass('pc-button-open');
            var dropdownMenu = $(this).find('.dropdown-menu-panel').show(),
                dropdownLinkHeight = $(this).height(),
                offset = $(this).offset();
                leftPosition = $(dropdownMenu).width();
            $(dropdownMenu).css( {'top' : dropdownLinkHeight -2} );

            $('body').one('click', function() {
                $(this).find('table.dropdown-menu-panel').hide();
                $('.pc-button-open').removeClass('pc-button-open').unbind('click', buttonDropdownToggle).click();
            });
            
            e.stopPropagation();
        }, function() {
            $(this).removeClass('pc-button-open');
            $(this).find('table.dropdown-menu-panel').hide();
        });


    /**************************************************************************
     * Link dropdowns
     **************************************************************************/ 

    $('.pc-link-dropdown').append('<span class="dropdown-arrow">&#x25BC;</span>').hover( function() {
        // close button dropdowns if open
        $('.dropdown-menu-panel').hide();
        $('.pc-button-open').removeClass('pc-button-open').unbind('click', buttonDropdownToggle).click();

        $(this).addClass('pc-link-dropdown-hover');
        var dropdownMenu = $(this).find('.dropdown-menu-panel').show(),
            dropdownLinkHeight = $(this).height(),
            offset = $(this).offset();
            leftPosition = $(dropdownMenu).width();
        $(dropdownMenu).css( {'top' : dropdownLinkHeight -1} );
    }, function() {
        $(this).removeClass('pc-link-dropdown-hover');
        $(this).find('table.dropdown-menu-panel').hide();
    });


    /**************************************************************************
     * Forms
     **************************************************************************/ 
    // $('form.form li').hover(function(){
    //     $(this).find('.instruct').show();
    // }, function () {
    //     $(this).find('.instruct').hide();
    // });


    /**************************************************************************
     * User Notifification panel close
     **************************************************************************/ 

    $('a.showToggle').toggle(function(e){
        $(this).prev().slideDown('fast');
        $(this).html('Less <span class="arrow">&#x25B2;</span>');
    }, function(){
        $(this).prev().slideUp('fast');
        $(this).html('More <span class="arrow">&#x25BC;</span>');
    });

	// Hehe.
	var kkeys = [], konami = "38,38,40,40,37,39,37,39,66,65";
	$(document).keydown(function(e) {
	  kkeys.push( e.keyCode );
	  if ( kkeys.toString().indexOf( konami ) >= 0 ){
	    $(document).unbind('keydown',arguments.callee);
	    $.getScript('http://www.cornify.com/js/cornify.js',function(){
	      cornify_add();
	      $(document).keydown(cornify_add);
	    });          
	  }
	});

});

