// init.js
jQuery.noConflict();

function verticalPosition() {
    // vertical align site
    var wrapper = jQuery('#wrapper');
    var wrapperHeight = wrapper.height();
    var screenHeight = jQuery(window).height();
    if (wrapperHeight < screenHeight) wrapper.css('margin-top', (screenHeight - wrapperHeight) / 2);
}

jQuery(document).ready(function() {


    // drop downs
    jQuery('#nav-search').click(function() {
        var drop_down = jQuery('#dd-search');
        var other_dd = jQuery('#dd-sign_in');
        var examples_dd = jQuery('#dd-examples');
        // hide other drop-downs
        if (other_dd.hasClass('open')) other_dd.removeClass('open').fadeOut('fast');
        if (examples_dd.hasClass('open')) examples_dd.removeClass('open').fadeOut('fast');
        //
        if (drop_down.hasClass('open')) drop_down.removeClass('open').fadeOut('fast'); // hide search
        else drop_down.addClass('open').fadeIn('slow'); // show search
        return false;
    });
    jQuery('#nav-sign_in').click(function() {
        var drop_down = jQuery('#dd-sign_in');
        var other_dd = jQuery('#dd-search');
        var examples_dd = jQuery('#dd-examples');
        // hide other drop-downs
        if (other_dd.hasClass('open')) other_dd.removeClass('open').fadeOut('fast');
        if (examples_dd.hasClass('open')) examples_dd.removeClass('open').fadeOut('fast');
        //
        if (drop_down.hasClass('open')) drop_down.removeClass('open').fadeOut('fast'); // hide sign in
        else drop_down.addClass('open').slideDown(); // 500, 'easeOutBack' show sign in with custom easing						
        return false;
    });
    jQuery('.show_examples').click(function() {
        var drop_down = jQuery('#dd-examples');
        var first_dd = jQuery('#dd-sign_in');
        var other_dd = jQuery('#dd-search');
        // hide other drop-downs
        if (other_dd.hasClass('open')) other_dd.removeClass('open').fadeOut('fast');
        if (first_dd.hasClass('open')) first_dd.removeClass('open').fadeOut('fast');
        //
        if (drop_down.hasClass('open')) drop_down.removeClass('open').fadeOut('fast'); // hide examples
        else {
            drop_down.addClass('open').slideDown(); // show examples with custom easing
            // have to calculate heights here as they return 0 when hidden
            // vertically align example name
            /*
            var height = jQuery('.example span').height();
            jQuery('.example span').each(function() {
            var text_h = jQuery(this).height();
            var ph = 40;
            var mh = (ph - text_h) / 2;
            jQuery(this).css('padding-top', mh);
            });
            */
        }
        return false;
    });
    // close drop down
    jQuery('.btn-close').click(function() {
        jQuery(this).parent().parent('.drop-down').removeClass('open').fadeOut('fast');
        return false;
    });
    // sign in labels 
    jQuery('.input_sign_in').each(function() {
        if (jQuery(this).val() != '') jQuery(this).prev('.label_sign_in').hide();
    });
    jQuery('.label_sign_in').click(function() {
        jQuery(this).hide();
        jQuery(this).next('.input_sign_in').focus();
        return false;
    });
    jQuery('.input_sign_in').focus(function() {
        jQuery(this).prev('.label_sign_in').hide();
    });
    jQuery('.input_sign_in').blur(function() {
        if (jQuery(this).val() == '') jQuery(this).prev('.label_sign_in').show();
        return false;
    });

    // create bespoke drop-down

    jQuery('.bespoke').each(function() {
        // get drop down ID and options
        var selectID = jQuery(this).attr("id");
        var select_options = new Array();
        select_options = jQuery('#' + selectID + ' option');
        var output = '';
        var output_options = '';
        var first_option = '';
        var inline_styles = '';
        // loop through options
        for (i = 0; i < select_options.length; i++) {
            var option_value = select_options[i].getAttribute('value');
            var option_text = select_options[i].text;
            if (i == 0) first_option = option_text;
            if (option_value == null) option_value = '';
            if (i > 13) inline_styles = ' style="height: 200px;"';
            //alert(option_text);
            output_options += '<a href="#' + option_value + '" rel="' + selectID + '">' + option_text + '</a>\n';
        }
        output = '<div class="select_box">\n<span class="default">' + first_option + '</span>\n<div class="menu">\n<div class="options"' + inline_styles + '>' + output_options + '</div>\n</div>\n</div>';

        // write html
        jQuery(this).after(output);

        // bind functions to output
        jQuery(this).next('.select_box').children('.default').bind("click", function() {
            // get menu vars
            var menu = jQuery(this).parent().children('.menu');
            // check open status
            if (menu.hasClass('open')) {
                // close open menus
                jQuery('.menu.open').removeClass('open').slideUp();
                //menu.removeClass('open').slideUp();
            } else {
                // close open menus
                jQuery('.menu.open').removeClass('open').slideUp();
                menu.addClass('open').slideDown();
                jQuery(this).next('.menu').children('.options').jScrollPane();
                // turn off body click when menu open
                jQuery('.select_box').mouseover(function() {
                    jQuery('body').unbind("click");
                }).mouseout(function() {
                    // turn body click on
                    jQuery('body').bind("click", function() {
                        jQuery('.menu.open:not(:animated)').removeClass('open').slideUp();
                    });
                });
            }
        });

        jQuery('.options a').bind("click", function() {
            var value_text = jQuery(this).text();
            var link = jQuery(this).attr("href");
            var link_val = link.split('#');
            var value = link_val[1];
            var targetID = '#' + jQuery(this).attr("rel");
            // change drop down label to selected
            var default_value = value_text.split("/");
            jQuery(this).parent('.options').parent('.jScrollPaneContainer').parent('.menu').prev('.default').text(default_value[0]);
            // go through and select real form option
            //alert(link_val[1]);
            jQuery(targetID + ' option').each(function() {
                if (jQuery(this).val() == value) jQuery(this).attr("selected", "selected");
            });
            jQuery(this).parent('.options').parent('.jScrollPaneContainer').parent('.menu').removeClass('open').slideUp();
            return false;
        });
    });
    // close menu on body click
    jQuery('body').click(function() {
        jQuery('.menu.open:not(:animated)').removeClass('open').slideUp();
    });
    // show and hide initial text input value on focus/blur
    jQuery('.show_hide_value').each(function() {
        // get initial value on page load									 
        var value = jQuery(this).val();
        jQuery(this).bind("focus", function() {
            // compare current value with initial on focus
            if (jQuery(this).val() == value) jQuery(this).val('');
        }).bind("blur", function() {
            var current_value = jQuery(this).val();
            if (current_value == '') jQuery(this).val(value);
        });
    });

});