var NOOP = function() { return false; };

$(document).ready(function() {

    // Shows and hides the dropdown menus
    $('#nav_main .nav_main_item').hover(
        function() {$(this).addClass('hover')},
        function() {$(this).removeClass('hover')}
    );
    
    // Shows and hides the tooltips on the sustainability widget
    $('.widget_sustainability li').hover(
        function() {$(this).children('.tooltip').css('visibility','visible').css('display','block'); },
        function() {$(this).children('.tooltip').css('visibility','hidden').css('display','none'); }
    );
    
    /*$('a').click(function() {
        if($(this).attr('rel')) {
            $("#external_link").overlay({
                top: 272,
                expose: {
                    color: '#000',
                    loadSpeed: 150,
                    opacity: 0.5
                },
                closeOnClick: false,
                api: true
            }).load();
        }
    });*/
    
    $('a[title=pdf]').addClass('pdf');
    
    $(".like_button").click(function() {
        if(!$(this).hasClass('disabled_like_button')) {
            var cmd_url = $(this).attr('rel');
            $(this).addClass('disabled_like_button');
            $.get(cmd_url, '', function(data, statusText) {
                var response = data.split('|');
                // response array:
                // 0 - response message
                // 1 - count and flavor text (person or people)
                // 2 - debug template string
                $('#like_count').text(response[1]);
                $('#like_count_wrapper').css('display', 'inline');
            });
        }
        return false;
    });

    // Shows and hides the share tooltips
    $('#share_options .service').click(function() {
            $("#share_options li.hover").removeClass("hover");
            $(this).parent('li').addClass('hover');
            return false;
        }
    );

    // Closes the share tooltips
    $('#share_options .tooltip_close').click(function() {
            $("#share_options li.hover").removeClass("hover");
        }
    );  
    
    //hides all definitions on faq page
    $('body.faq dd').hide();
    $('body.faq dd').each(function() {
                this.innerHTML = '<div>' + this.innerHTML + '</div>';
    });
    
    //toggles visibility of dd's that follow the dt's
    $('body.faq dt').toggle(function () {
            $(this).next().slideDown();
        }, function () {
            $(this).next().slideUp();
        }
    ).hover(
        //and adds a "hover" class to dt's when rolled over
        function () {
            $(this).addClass('hover');
        },function () {
            $(this).removeClass('hover');
        }
    );
    
    //add zebra stripe class
    $('body.faq dd:even').addClass('alt');
    $('body.faq dt:even').addClass('alt');
    
    
    $('a.print').click(function() {
        window.print();
        return false;
    });
    
    //$('#share_email div.wrap').load($("#share_options .email a").attr('href'));
    
    
    
    // Email share
    
    $("#share_options .email").click(function() {
        $("#share_options .email a").click();
    });
    
    var share_options_loading = false;
    $('#recipe_share .email').click(function() {
        $('#share_options .email a').click();
    });
    
    $('#tellafriend_form').validate({
        rules: {
            from: { required: true, email: true },
            name: { required: true },
            to: { required: true, email: true },
            subject: { required: true },
            message: { required: true }
        },
        focusCleanup: false,
        onfocusin: function() {},
        onfocusout: function() {},
        onkeyup: function() {},
        onclick: function() {},
        errorPlacement: function() {},
        highlight: function(element) {
            $(element).parent().addClass('error')
        },
        unhighlight: function(element) {
            $(element).parent().removeClass('error')
        }
    });
    
    $("#share_options .email a").click(function() {
            //console.log('onBeforeLoad');
            if(!share_options_loading) {
                var url = $("#share_options .email a").attr('href');
                //console.log('loading ' + url);
                //{path='share/email'}{embed:entry_id}
                share_options_loading = true;
//                $('#share_email div.wrap').load(url, '', function(responseText, textStatus, XMLHttpRequest) {
//                });
            }
            return false;
            /*var wrap = this.getContent().find("div.wrap");
            console.log(wrap);
            wrap.load(this.getTrigger().attr("href"));
            console.log(wrap);*/
        }).overlay({
        expose: { 
            color: '#000', 
            loadSpeed: 200, 
            opacity: 0.7
        } /*,
        onBeforeLoad: function() {
            console.log('onBeforeLoad');
            if(!share_options_loading) {
                var url = $("#share_options .email a").attr('href');
                console.log('loading ' + ur);
                share_options_loading = true;
                $('#share_email div.wrap').load(url, '', function(responseText, textStatus, XMLHttpRequest) {
                    $('#tellafriend_form').validate({
                        rules: {
                            from: { required: true, email: true },
                            name: { required: true },
                            to: { required: true, email: true },
                            subject: { required: true },
                            message: { required: true }
                        },
                        focusCleanup: false,
                        onfocusin: function() {},
                        onfocusout: function() {},
                        onkeyup: function() {},
                        onclick: function() {},
                        errorPlacement: function() {},
                        highlight: function(element) {
                            $(element).parent().addClass('error')
                        },
                        unhighlight: function(element) {
                            $(element).parent().removeClass('error')
                        }
                    });
                });
            }
            // *var wrap = this.getContent().find("div.wrap");
            console.log(wrap);
            wrap.load(this.getTrigger().attr("href"));
            console.log(wrap);* //
        }*/
    });
    
    
/*    $('#share_options li.email').click(function() {
        $share_email_overlay.load();
        return false;
    });*/
    
    
    $('#share_submit').click(NOOP);
    
    
    $('input#healthcare_pro, input#children_under_18').isChecked();
    
});


jQuery.fn.isChecked = function(val)
{
    var check = function(ele)
    {
        ele.each(function()
        {
            if( $(this).attr('type') == 'radio' )
            {
                if( $(this).is(':checked') && $(this).val() ==  val ) {
                    $(this).parents('li.form_checkbox').next().show();
                } else {
                    $(this).parents('li.form_checkbox').next().hide();
                }
            }
            else
            {
                if( $(this).is(':checked') ) {
                    $(this).parents('li.form_checkbox').next().show();
                } else {
                    $(this).parents('li.form_checkbox').next().hide();
                }
            }
        });
    }
    
    check(this);
    
    this.each(function(){
		$(this).click(function(){
            check( $(this) );
        });
    });
}






