$(document).ready(function(){
    /**
     * Add default values to inputs
     *
     * @note    Specify default values with the "rel" attribute, i.e:
     *          <input type="text" name="email" value="" rel="Email..." />
     * @see     http://www.unwrongest.com/projects/defaultvalue/
     */
    $('input.defaultValue, textarea.defaultValue').defaultValue();

    $.blockUI.defaults.css = {};

    $('.show-login').click(function(e)
    {
        e.preventDefault();

        var offset = $('#login').show().offsetParent().offset();
        $('#login').hide();

        $.blockUI({
            message: $('#login'),
            css: { top: offset.top + 'px', left: offset.left + 'px' },
            overlayCSS: { opacity: '0.7' },
            baseZ: 998 // make sure dialog is always on top
        });

        $('#login').find('input:first').trigger('focus');
    });

    $('.close-login').click(function(e)
    {
        e.preventDefault();
        $('#login').fadeOut();
        $.unblockUI();
    });

    $('.show-contact').click(function(e)
    {
        e.preventDefault();
        var dialog = new Graphite.Dialog({
            dialog:     $('#contact')
        });
        dialog.open();
    });

    $(':input')
        .focus(function(e)
        {
            $(this).addClass('focus');
        })
        .blur(function(e)
        {
            $(this).removeClass('focus');
        });

    /**
     * Privacy Policy
     */
    $('a.privacy-policy').click(function(e)
    {
        e.preventDefault();
        new Graphite.SubPage({
            uri: '/privacy-policy'
        });
    });

    /**
     * Terms & Conditions
     */
    $('a.terms-and-conditions').click(function(e)
    {
        e.preventDefault();
        new Graphite.SubPage({
            uri: '/terms-and-conditions'
        });
    });

    /**
     * Refund Policy
     */
    $('a.refund-policy').click(function(e)
    {
        e.preventDefault();
        new Graphite.SubPage({
            uri: '/refund-policy'
        });
    });

    /**
     * ...
     */
    $('a.feedburner-email').click(function(e)
    {
        e.preventDefault();
        new Graphite.SubPage({
            uri: 'http://feedburner.google.com/fb/a/mailverify?uri=GraphiteBlog&loc=en_US'
        });
    });

    /**
     * ...
     */
    $('.nav a, #header a, #main-nav a').click(function(e)
    {
        $(this).trigger('blur');
    });

    /**
     * ...
     */
    $("ul.subnav").parent().addClass('has-drop-down');
    $('ul.mast-nav-links li.has-drop-down').children('a').click(function(e)
    {
        e.preventDefault();
        $(this).parent().addClass('hover').find("ul.subnav").slideDown('fast').show(); //Drop down the subnav on click
        $(this).parent().hover(function() {  
        }, function(){  
            $(this).removeClass('hover').find("ul.subnav").slideUp('normal'); //When the mouse hovers out of the subnav, move it back up  
        });
    });

    /**
     * jQuery SmoothScroll | Version 09-11-02
     */
    $('a[href*=#]').click(function(e)
    {
        if (this.hash === '')
        {
            return;
        }

        // duration in ms
        var duration=1000;

        // easing values: swing | linear
        var easing='swing';

        // get / set parameters
        var newHash=this.hash;
        var target=$(this.hash).offset().top;
        var oldLocation=window.location.href.replace(window.location.hash, '');
        var newLocation=this;

        // make sure it's the same location      
        if(oldLocation+newHash==newLocation)
        {
            // animate to target and set the hash to the window.location after the animation
            $('html:not(:animated),body:not(:animated)').animate({ scrollTop: target }, duration, easing, function()
            {
                // add new hash to the browser location
                window.location.href=newLocation;
            });

            // cancel default click action
            e.preventDefault();
        }
    });

    /**
     * ...
     */
    $('a.image-zoom, a[href^=/site/images/tour/]').fancybox({
        padding:        1,
        zoomSpeedIn:    850,
        zoomSpeedOut:   750,
        easingIn:       'easeOutBack',
        easingOut:      'easeInBack',
        overlayShow:    true,
        overlayColor:   '#000000',
        overlayOpacity: 0.7
    });
});
