(function() {
    try {
        if (!ognc.exists) { throw "ogncObjUndefined" }

        //Commonly used functions/helpers
        goodnites =
        {
            exists: true,
            nav_timeout: 500,
            nav_closetimer: 0,
            nav_ddmenuitem: 0,

            init: function() {
                // Allow console.log debugging cross all browsers
                try { console.log('...goodnites is now running...'); } catch (e) { console = { log: function() { } } }
            },

            topNav_open: function() {
                this.nav_ddmenuitem = jQuery(this).find('ul').css('visibility', 'visible');
                this.nav_ddmenuitem.addClass("openDropWidth");
            },

            topNav_close: function(argEle) {
                if (argEle != undefined && argEle !== 0) {
                    argEle.css('visibility', 'hidden');
                }
            },

            //This will increment a helpful value by 1 when requested
            helpfulItem: function(label, argId) {
                if (argId) {
                    $.post(baseUrl + "/ajax/like.ashx?action=set&id=" + argId, '',
			          function(data) {
			              $('#helpful_' + argId).html(label + '(' + data + ')');
			              $('#helpful_' + argId).removeAttr("href");
			              ognc.tracking.linkTrack('Content', 'Helpful' + argId);
			          });

                }
            },

            //This will get a total helpful count for one content item and return it to the requesting id
            /*<a><script>$(function() {goodnites.getHelpfulCount('my_id')});</script></a>*/
            getHelpfulCount: function(label, argId) {
                if (argId) {
                    $.post(baseUrl + "/ajax/like.ashx?action=get&id=" + argId, '',
			          function(data) {
			              $('#helpful_' + argId).html(label + '(' + data + ')');
			          });
                }
            },

            //Set Base Share Buttons
            intShareButtons: function() {
                if ($('.share')) {
                    addthis.button('.share');
                }
            },

            topNav_timer: function() {
                goodnites.topNav_close(this.nav_ddmenuitem);
            },

            /*Focus Method for comments form*/
            focusForm: function(argForm) {
                //Open form fields if focus on form
                $("#" + argForm + "_hidden_form_fields").slideDown("slow");

                //Check for blur of form and close
                $("body").click(function(event) {
                    var found = false;
                    jQuery.each($(event.target).parents(), function() {
                        if (this.id == argForm + "_form_container") {
                            found = true;
                        }
                    });
                    if (!found) {
                        $("#" + argForm + "_hidden_form_fields").slideUp("slow");
                        $("body").unbind('click');
                    }
                });
            },

            //Clears form fields when submit is click and there are validation errors on the page
            form_field_clear: function() {
                if ($("#title_text").val() == 'Enter title here') { $("#title_text").val(''); }
                if ($("#story_text").val() == 'Enter your story here') { $("#story_text").val(''); }
                if ($("#username_text").val() == 'Enter your user name') { $("#username_text").val(''); }
                if ($("#ctl00_ctl00_Content_Content_question_text").val() == 'Enter your question here') { $("ctl00_ctl00_Content_Content_question_text").val(''); }
                if ($("#email_text").val() == 'Enter your email here') { $("#email_text").val(''); }
                if ($("#comment_text").val() == '*Share your thoughts.') { $("#comment_text").val(''); }
                if ($("#username_text").val() == '*First name or user name.') { $("#username_text").val(''); }
            }
        }

        goodnites = window.goodnites;

        jQuery(document).ready(function($) {
            goodnites.init();

            goodnites.intShareButtons();

            $('li.primaryNav').bind('mouseover', goodnites.topNav_open);
            $('li.primaryNav').bind('mouseout', goodnites.topNav_timer);

            //Text Search 
            $('input[id$="txtSearch"]').focus(function() {
                $(this).val('');
            });

            $('input[id$="txtSearch"]').blur(function() {
                if ($(this).val() == '')
                    $(this).val('Search Goodnites.com');
            });

            $('input[id$="txtSearch"]').live('keypress', function(event) {
                if (event.keyCode == 13)
                    $(".search-link").click();
            });

            //Text Search Content
            $('input[id$="txtSearch_content"]').focus(function() {
                $(this).val('');
            });

            $('input[id$="txtSearch_content"]').blur(function() {
                if ($(this).val() == '')
                    $(this).val('Search Goodnites.com');
            });

            $('input[id$="txtSearch_content"]').live('keypress', function(event) {
                if (event.keyCode == 13)
                    $(".search-link-content").click();
            });

            //Ask Question Search text
            $('input[id$="ask_question_search_text"]').focus(function() {
                $(this).val('');
            });

            $('input[id$="ask_question_search_text"]').blur(function() {
                if ($(this).val() == '')
                    $(this).val('Ask your question here');
            });

            $('input[id$="ask_question_search_text"]').live('keypress', function(event) {
                if (event.keyCode == 13)
                    $(".ask_button_main").click();
            });

            //Widget Ask Question Search text
            $('input[id$="widget_ask_a_question_search_text"]').focus(function() {
                $(this).val('');
            });
            $('input[id$="widget_ask_a_question_search_text"]').blur(function() {
                if ($(this).val() == '')
                    $(this).val('Ask your question here');
            });

            $('input[id$="widget_ask_a_question_search_text"]').live('keypress', function(event) {
                if (event.keyCode == 13)
                    $(".ask_button").click();
            });

            //Ask Question Form Events
            $('textarea[id$="question_text"]').focus(function() {
                if ($(this).val() == 'Enter your question here')
                    $(this).val('');
            });

            $('input[id$="email_text"]').focus(function() {
                if ($(this).val() == 'Enter your email here')
                    $(this).val('');
            });

            $('input[id$="title_text"]').focus(function() {
                if ($(this).val() == 'Enter title here')
                    $(this).val('');
            });

            $('textarea[id$="story_text"]').focus(function() {
                if ($(this).html() == 'Enter your story here')
                    $(this).empty();
            });

            $('input[id$="username_text"]').focus(function() {
                if ($(this).val() == 'Enter your user name')
                    $(this).val('');
            });

            //Comments
            $('input[id$="comment_text"]').focus(function() {
                if ($(this).val() == '*Share your thoughts.')
                    $(this).val('');
            });

            $('input[id$="comment_text"]').blur(function() {
                if ($(this).val() == '' && !($(this).hasClass('error')))
                    $(this).val('*Share your thoughts.');
            });

            $('input[id$="username_text"]').focus(function() {
                if ($(this).val() == '*First name or user name.')
                    $(this).val('');
            });

            $('input[id$="username_text"]').blur(function() {
                if ($(this).val() == '' && !($(this).hasClass('error')))
                    $(this).val('*First name or user name.');
            });

            //Search Link Events
            $(".search-link").live('click', function(event) {
                if ($('input[id$="txtSearch"]').val() == 'Search Goodnites.com')
                    $('input[id$="txtSearch"]').val('');
                ognc.tracking.linkTrack('Search', $('input[id$="txtSearch"]').val());

                if (window.location.host == "community.goodnites.com" || window.location.host == "goodnitesparents.stage.liveworld.com") {
                    window.location.href = 'http://www.goodnites.com/na/Search.aspx?q=' + $('input[id$="txtSearch"]').val();
                } else {
                    window.location.href = baseUrl + '/Search.aspx?q=' + $('input[id$="txtSearch"]').val();
                }
            });

            $(".search-link-content").live('click', function(event) {
                if ($('input[id$="txtSearch_content"]').val() == 'Search Goodnites.com')
                    $('input[id$="txtSearch_content"]').val('');
                ognc.tracking.linkTrack('Search_content', $('input[id$="txtSearch_content"]').val());
                window.location.href = baseUrl + '/Search.aspx?q=' + $('input[id$="txtSearch_content"]').val();
            });

            //Ask Button Events  
            $(".ask_button").live('click', function(event) {
                if ($('input[id$="widget_ask_a_question_search_text"]').val() == 'Ask your question here')
                    $('input[id$="widget_ask_a_question_search_text"]').val('');
                ognc.tracking.linkTrack('Promo', 'Ask_A_Question');
                window.location.href = baseUrl + '/ask-nitelite-panelist/Default.aspx?q=' + encodeURIComponent($('input[id$="widget_ask_a_question_search_text"]').val());
            });
            $(".ask_button_main").live('click', function(event) {
                if ($('input[id$="ask_question_search_text"]').val() == 'Ask your question here')
                    $('input[id$="ask_question_search_text"]').val('');
                ognc.tracking.linkTrack('Content', 'Ask_A_Question');
                window.location.href = baseUrl + '/ask-nitelite-panelist/Default.aspx?q=' + encodeURIComponent($('input[id$="ask_question_search_text"]').val());
            });
            $(".tell_story_button_main").live('click', function(event) {
                ognc.tracking.linkTrack('Content', 'Personal_Stories_Tell_Your_Story_Action');
                window.location.href = baseUrl + '/bedwetting-education/tell-us-your-story.aspx';
            });

            //Ask select events
            $('#dropdown').live('click', function() {

                if ($('#item_options').css('display') === 'none') {
                    $('#item_options').css('display', 'block');
                } else {
                    $('#item_options').css('display', 'none');
                }
            });

            $('#item_options div').each(function(index) {
                $(this).click(function() {
                    $('#current_item').html($(this).html());
                    if ($(this).html() == 'Products/Other') {
                        //redirect to contact us
                        window.location.href = baseUrl + '/Contact.aspx';
                    }
                    else {

                        $('#category').val($(this).html());
                        var $topic = $("#item_menu [name='topic']");

                        $topic.val($(this).html());
                        $topic.trigger('change');
                        //console.log($(this).html());
                    }
                });
            });

            $('#item_options').mouseleave(function() {
                $(this).css('display', 'none');
            });

            // Image swap function
            $(function() {
                $(".img_swap").hover(
                    function() {
                        if ((this.src).indexOf("_on") == -1) {
                            curState = 'off';
                        }
                        else {
                            curState = 'on';
                        }
                        this.src = this.src.replace("_off", "_on");
                    },
                    function() {
                        if (curState == "off") {
                            this.src = this.src.replace("_on", "_off");
                        }
                    }
                );
            });

            // Submit button background image swaps
            $(".button_roll").bind({
                mouseenter: function() {
                    if ($(this).hasClass('locate_store_button')) {
                        thisID = 'locate_store';
                    }
                    else if ($(this).hasClass('buy_online_button')) {
                        thisID = 'buy_online';
                    }
                    else if ($(this).hasClass('submit_button')) {
                        thisID = 'submit';
                    }
                    else {
                        thisID = this.id;
                    }
                    $(this).css('background-image', 'url(/na/img/' + thisID + '_button_on.gif)');
                },
                mouseleave: function() {
                    $(this).css('background-image', 'url(/na/img/' + thisID + '_button_off.gif)');

                }
            });

            // deactivate the link in the tile_roll
            $(".tile_roll").find('a:last').click(function(e) {
                e.preventDefault();
            });

            // Handles List Tile Rollovers
            $(".tile_roll").bind({
                click: function() {
                    var tile_path = $(this).find('a:last').attr('href');
                    var tile_target = $(this).find('a:last').attr('target');
                    var tile_tracking_lpos = $(this).find('a:last').attr('rel');
                    var tile_tracking_id = $(this).find('a:last').attr('rev');

                    //console.log(tile_tracking_lpos +', '+ tile_tracking_id);
                    if (tile_target) {
                        ognc.tracking.linkTrack(tile_tracking_lpos, tile_tracking_id);
                        window.open(tile_path);
                    }
                    else {
                        window.location = tile_path;
                        ognc.tracking.linkTrack(tile_tracking_lpos, tile_tracking_id);
                    }
                },
                mouseenter: function() {
                    $(this).addClass('tile_over');
                    $(this).find('a:last').css('color', '#F9A21E');
                },
                mouseleave: function() {
                    $(this).removeClass('tile_over');
                    $(this).find('a:last').css('color', '#214097');
                }
            });


            // Handles Promo Tile Rollovers
            $(".promo_roll").bind({
                click: function() {
                    tile_path = $(this).find('a').last().attr('href');
                    var tile_tracking_lpos = $(this).find('a:last').attr('rel');
                    var tile_tracking_id = $(this).find('a:last').attr('rev');

                    window.location = tile_path;
                    ognc.tracking.linkTrack(tile_tracking_lpos, tile_tracking_id);
                },
                mouseenter: function() {
                    $(this).addClass('promo_over');
                    $(this).find('a').last().css('color', '#F9A21E');
                },
                mouseleave: function() {
                    $(this).removeClass('promo_over');
                    $(this).find('a').last().css('color', '#214097');
                }
            });

        });


    } catch (e) { console.log("error: " + e); }
})();

//Sharing Templates
var addthis_share =
{ 
	email_template: "Goodnites",
	email_vars: { description: $("meta[name='description']").attr("content") }
}

//Base Sharing Configuration
var addthis_config =
{
	ui_hover_direction:1,
	services_compact:"facebook,email,twitter,favorites,more",
	ui_cobrand:"Goodnites",
	ui_header_background:"#214198",
	ui_offset_left:"-5",
	ui_offset_top:"2",
	ui_header_color:"#ffffff",
	ui_click:true,
	data_track_linkback:true,
	data_ga_tracker: ognc.tracking.pageTracker,
	ui_use_addressbook:true
}

function checkForEnter(event) {
    if (event.keyCode == 13) {
        {
            return true;
        }
    }
}
