jQuery(document).ready(function($) {

    $('#nomikos_et_info').qtip({
        content: '<span style="color:#1D398C">ET = Eastern Time Zone<br><a target="_blank" class="eventMain" style="color:#FF6600" rel="nofollow" href="http://wwp.greenwichmeantime.com/time-zone/usa/eastern-time/">See what (ET) time it is now...</a></span>',
        position: {
            corner: {
                target: 'rightMiddle',
                tooltip: 'leftMiddle'
            }
        },
        hide: {
            when: 'mouseout',
            fixed: true,
            delay: 500
        },
        style: {
            padding: 5,
            textAlign: "center",
            border: {
                width: 1,
                radius: 4,
                color: "#DDDDDD"
            },
            tip: "leftMiddle"
        }
    });

    $('#HowDidYouHear').change(function(){
        var option = $(this).find('option').filter(':selected').text();
        if (option == 'Other')
        {
            $("#HowDidYouHearOther").fadeIn();
            $("#HowDidYouHearOther").select().focus();
        }
        else
        {
            $("#HowDidYouHearOther").hide();
        }
    });

    $("#HowDidYouHearOther").focus(function() {
        this.select();
    });

    $("#HowDidYouHearOther").blur(function() {
        if (this.value != "")
        {
            $('#HowDidYouHear').find('option').filter(':selected').val(this.value);
        }
    });

    if ($("#cost").val() == "")
        var nomikos_tmp_costs = "$00.00";
    else
        var nomikos_tmp_costs = $("#cost").val(); // for edit
    var prev = "0";

    $("#setCost0").click(function() {
        if ($("#cost").val() != "")
            nomikos_tmp_costs = $("#cost").val();
        $("#cost").val("");
        $("#cost").attr("disabled", true);
        prev = "0";
    });

    $("#setCost1,#setCost2").click(function() {
        if (prev == "0"){
            if ($("#cost").val() == "")
                $("#cost").val("$00.00")
            else
                $('#cost').val(nomikos_tmp_costs);
        }
        $("#cost").removeAttr("disabled");
        $("#cost").select().focus();
        prev = "1";
    });

    $("#cost").blur(function() {
        var test = $.trim(this.value);
        if (test == ""){
            alert("The field is empty so the cost will set to free.")
            $("#setCost0").click();
        }
    });

    $('.nomikos_cat_selector').qtip({
        content: '<span style="color:#1D398C">Categories selector</span>',
        position: {
            corner: {
                target: 'rightMiddle',
                tooltip: 'bottomLeft'
            }
        },
        show: {
            when: 'mouseover',
            delay: 0
        },
        hide: {
            when: 'mouseout',
            fixed: false,
            delay: 0
        },
        style: {
            padding: 5,
            textAlign: "left",
            border: {
                width: 1,
                radius: 4,
                color: "#DDDDDD"
            },
            tip: "bottomLeft"
        }
    });

    // categories
    var begin_count_cats = $(".nomikos_cat_selector_checkbox:checkbox:checked").length;
    var begin_main_catID = $("input[name='main_catID']:checked").val();
    begin_main_catID = begin_main_catID ? begin_main_catID : 0;
    var message_ready = 0;

    if ($.browser.safari || $.browser.mozilla){
        $(".nomikos_cat_selector_radio").click(function() {
            var main_catID = this.value;

            var count = $(".nomikos_cat_selector_checkbox:checkbox:checked").length;

            if (typeof(nomikos_editing_webinar) != "undefined"){
                if (count + 1 > begin_count_cats){
                    if ($("#catID_" + main_catID).attr("checked") != true)
                    {
                        alert("You can't add more categories.");
                        $("#main_catID_" + begin_main_catID).attr("checked", true);
                        return false;
                    }
                }
            }
            else{
                $("#catID_" + main_catID).attr("checked", true);
                if (count >= 4 && message_ready == 0) {
                    message_ready = 1;
                    alert("You will be charged $10 for each additional category over 3.")
                }
            }
        });

        $(".nomikos_cat_selector_checkbox").click(function() {
            var catID = this.value;
            var count_r = $(".nomikos_cat_selector_radio:checked").length;
        
            if (count_r == 0) {
                // force main cat
                $("#main_catID_" + catID).attr("checked", true);
            }

            if ($("#main_catID_" + catID).attr("checked") == true) {
                $(this).attr("checked", true);
            }

            if (this.form.name != "eventSearch"){
                var count = $(".nomikos_cat_selector_checkbox:checkbox:checked").length;

                if (typeof(nomikos_editing_webinar) != "undefined"){
                    if (count > begin_count_cats){
                        alert("You can't add more categories.");
                        return false;
                    }
                }
                else
                {
                    if (count >= 4 && message_ready == 0){
                        message_ready = 1;
                        alert("You will be charged $10 for each additional category over 3.")
                    }
                }
            }
        });
    }

    $("#Video").blur(function() {
        if (this.value != "")
        {
            $("#nomikos_preview_video > div").html($("#Video").val());
            $("#nomikos_preview_video").show();
            $("html, body").animate({
                scrollTop: $("#Video").offset().top - 25
            }, 1000);
        }
        else
            $("#nomikos_preview_video").hide();
    });
    $("#Video_recorded").blur(function() {
        if (this.value != "")
        {
            $("#nomikos_preview_video_recorded > div").html($("#Video_recorded").val());
            $("#nomikos_preview_video_recorded").show();
            $("html, body").animate({
                scrollTop: $("#Video_recorded").offset().top - 25
            }, 1000);
        }
        else
            $("#nomikos_preview_video_recorded").hide();
    });

    $("#nomikos_settings_mantain1").click(function() {
        $("#nomikos_div_settings_recurrence1").fadeIn();
        $("#nomikos_div_settings_recurrence2").fadeOut();
    });

    $("#nomikos_settings_mantain2").click(function() {
        $("#nomikos_div_settings_recurrence2").fadeIn();
        $("#nomikos_div_settings_recurrence1").fadeOut();
    });

    $(".nomikos_jq_delete").click(function() {
        return confirm ('Proceed to delete this webinar?');
    });

    $("#nomikos_jq_show_more").click(function() {
        if ($("#nomikos_jq_show_more_div").is(":visible"))
        {
            $("#nomikos_jq_show_more_div").fadeOut();
            $("#nomikos_jq_show_more").val("More");
        }
        else
        {
            $("#nomikos_jq_show_more_div").fadeIn();
            $("#nomikos_jq_show_more").val("Less");
        }
    });

    $("#NOMIKOS_WEBINARLISTINGS_ADDONS_FORM_COMMENT").submit(function() {
        var m = "";

        if ($("#NOMIKOS_WEBINARLISTINGS_ADDONS_FORM_COMMENT :input[name='comment_body']").val() == "")
            m += "Please fill in the \"Review\" box.\n"
        if ($("#NOMIKOS_WEBINARLISTINGS_ADDONS_FORM_COMMENT :input[name='user_name']").val() == "")
            m += "Please fill in the \"First and Last name\" box.\n"
        if ($("#NOMIKOS_WEBINARLISTINGS_ADDONS_FORM_COMMENT :input[name='user_email']").val() == "")
            m += "Please fill in the \"E-mail\" box.\n"

        if (m)
        {
            alert("Required information was not submitted:\n\n" + m)
            return false;
        }
    });

    $("#NOMIKOS_WEBINARLISTINGS_ADDONS_FORM_RATING").submit(function() {
        var m = "";

        if ( ! $("#NOMIKOS_WEBINARLISTINGS_ADDONS_FORM_RATING input:radio[name='rating']:checked").val())
            m += "Please select rating.\n"

        if (m)
        {
            alert("Required information was not submitted:\n\n" + m)
            return false;
        }
    });

    if ($('#NOMIKOS_WEBINARLISTINGS_RECORDED_TABLE').ingrid)
    {
        $('#NOMIKOS_WEBINARLISTINGS_RECORDED_TABLE').ingrid({
            url: 'index_ajax.php',
            height: 250,
            colWidths: [410,175,100]
        });
    }

    $('#NOMIKOS_WEBINARLISTINGS_RECORDED_TABLE_CONTAINER').hide();

    if  (typeof nomikos_base_js == 'undefined')
    {
        nomikos_base_js =  window.location.protocol + '//' + window.location.hostname + '/calendar/';
    }

    $('#NOMIKOS_WEBINARLISTINGS_RECORDED_FORM').submit(function(){
        $(this).ajaxSubmit({
            url:            nomikos_base_js + 'events/index_ajax.php',
            type:           'POST',
            dataType:       'data',
            success:        function(jsonRecord, statusText) {
                if (statusText != 'success') return false;

                $("#NOMIKOS_WEBINARLISTINGS_RECORDED_TABLE_CONTAINER").animate({
                    opacity: "show"
                });
                $("html, body").animate({
                    scrollTop: $("#NOMIKOS_WEBINARLISTINGS_RECORDED_TABLE_CONTAINER").offset().top
                }, 1000);
                $("#nomikos_start_pager").click();
            }
        });
        return false;
    });

    $(":input[name=setSearch]").click(function() {
        var type = this.value;

        if (type == 'live' || type == 'both')
        {
            $('#div_search_date').fadeIn();
        }
        else
        {
            $('#div_search_date').fadeOut();
        }
    });

    // reset webinars type
    $('#nomikos_setFeaturedBasic').attr("checked", true);
    $('#setTypeLive').attr("checked", true);

    $(":input[name=setType]").click(function() {
        var type = this.value;

        if (type == 'live')
        {
            $('#div_live').show();
            $('#div_recorded').hide();
        }
        else
        {
            $('#div_live').hide();
            $('#div_recorded').show();
        }
    });


    $(':input[name=setTypeRecorded]').click(function() {
        var type = this.value;

        $('.div_recorded').hide();
        $('#div_recorded_' + type).fadeIn();
    });
})

var nmksObjectHelios = {
    check_radio: function() {
        var aux = jQuery('input:radio[name=main_catID]:checked').val();
        return aux;
    }
}

