/*
 * SimpleModal OSX Style Modal Dialog
 * http://www.ericmmartin.com/projects/simplemodal/
 * http://code.google.com/p/simplemodal/
 *
 * Copyright (c) 2009 Eric Martin - http://ericmmartin.com
 *
 * Licensed under the MIT license:
 *   http://www.opensource.org/licenses/mit-license.php
 *
 * Revision: $Id: osx.js 213 2009-09-17 04:10:02Z emartin24 $
 */
var updated = false;

//var _cbstatus_loading_follow = '';
multiselectnameprefix = "member_topics_follow";

var clb_id = null;

function club_getCBStatus_()
{
    var multiselectname = multiselectnameprefix + "_" + clb_id;
    /* get topic ids to enable */
    var _out = '';
    //    if ($("#item_" + multiselectname + "0").attr("checked")) {
    //        $("#multiselectboxitems_" + multiselectname + " input[type=checkbox]:gt(0)").each(function() {
    //            if (_out != '') {
    //                _out += "|";
    //            }
    //            _out += this.value;
    //        });
    //        return _out;
    //    }
    var _checkboxes = $("#multiselectboxitems_" + multiselectname + " input[type=checkbox]:checked");
    _checkboxes.each(function() {
        if (_out != '')
            _out += "|";
        _out += this.value;
    });
    return _out;
}

jQuery(function ($) {


    $(".clubfollowAction").each(function ()
    {


        function __closure(element_)
        {
            element_.click(function (e) {
                e.preventDefault();

                updated = false;

                var cid = $(this).attr('clb_id');
                var cname = $(this).attr('clb_name');

                //                if (cid > 0)
                //                {
                //                    document.forms['followForm_'+cid].clb_id.value = cid;
                //                    document.getElementById('get_alerts_from_x').innerHTML = cname;
                //                    document.getElementById('get_alerts_from_x2').innerHTML = cname;
                //                }

                clb_id = cid;

                club_showFollow();

            });
        }

        __closure($(this));


    });

    var OSXFL_CLUB = {
        container: null,
        open: function (d) {
            var self = this;
            self.container = d.container[0];
            d.overlay.fadeIn('slow', function () {
                $("#osx-modal-content-follow_" + clb_id, self.container).show();
                //                var title = $("#osx-modal-title-follow_"+clb_id, self.container);
                //                title.show();
                d.container.slideDown('slow', function () {

                    setTimeout(function () {

                        var h = $(window).height() - 30;

                        // // padding
                        d.container.animate(
                        {height: h+'px'},
                                327+'px',
                                function () {
                                    $("div.close", self.container).show();
                                    $("#osx-modal-data-follow_" + clb_id, self.container).show();
                                    club_multipl_select();

                                    club_saveAction();
                                }
                                );


                    }, 300);

                });
            })
        },

        close: function (d) {
            var self = this;
            d.container.animate(
            {top:"-" + (d.container.height() + 20)},
                    327,
                    function () {
                        self.close(); // or $.modal.close();
                    }
                    );
            if (updated) {
                window.location.href = window.location.href;
            }
        }
    };

    club_showFollow = function club_showFollow() {

        var h = $(window).height() - 30;


        $("#osx-modal-content-follow_" + clb_id).modal({
            overlayId: "osx-overlay-follow",
            containerId: 'osx-container-follow_' + clb_id,
            closeHTML: '',
            minHeight:80,
            opacity:65,
            position:['0',],
            overlayClose:true,
            onOpen:OSXFL_CLUB.open,
            containerCss:{'overflow':'hidden','width':'427px','height':h+'px'},
            onClose:OSXFL_CLUB.close
        });

        //////////////////////////////////////////////////////////////////////////////

        //////////////////////////////////////////////////////////////////////////////
    };


});


function club_multipl_select() {
    var multiselectname = multiselectnameprefix + "_" + clb_id;
    if ($("#item_" + multiselectname + "0").attr("checked")) {
        $("#multiselectboxitems_" + multiselectname + " input[type=checkbox]:gt(0)").attr("checked", false);
        $("#multiselectboxitems_" + multiselectname + " input[type=checkbox]:gt(0)").attr("disabled", "disabled");
    }

    $("#item_" + multiselectname + "0").click(function() {
        if ($(this).attr("checked")) {
            $("#multiselectboxitems_" + multiselectname + " input[type=checkbox]:gt(0)").attr("disabled", "disabled");
        }
        else {
            $("#multiselectboxitems_" + multiselectname + " input[type=checkbox]:gt(0)").removeAttr("disabled", "disabled");
        }
        getChecked(multiselectname, $("#multiselectboxitems_" + multiselectname + " input[type=checkbox]").length);
    });

    $("#multiselectboxitems_" + multiselectname + " input[type=checkbox]:gt(0)").each(function() {
        $(this).click(function() {
            if ($(this).attr("checked")) {
                $("#item_" + multiselectname + "0").removeAttr("checked").attr("disabled", "disabled");
            }
            else {
                if ($("#multiselectboxitems_" + multiselectname + " input[type=checkbox]:checked").length == 0) {
                    $("#item_" + multiselectname + "0").removeAttr("disabled");
                }
            }
        });
    });
}

function club_saveAction() {
    ////////////////////////////////////////////////////////////


    $('.saveFollow_' + clb_id).click(function() {
        //        alert('ss');

        url = $('input[name=url_' + clb_id + ']').val();


        var _out = club_getCBStatus_();
        $('input[name=member_topics_follow_hidden]').val(_out);

        var param = {'handler':'clubs',
            'action':'followActionAjax',
            'member_topics_follow_hidden':_out,
            'clb_id':$('input[name=clb_id_' + clb_id + ']').val()
        };

        $.post(url + '/index.php', param, function(data) {
            if (data == '1') {
                $('#msgResult_' + clb_id).show();
                $('#subscribeTxt_' + clb_id).hide();
                updated = true;
                $('#followActionMain_' + clb_id).hide();

            } else {
                //                alert(data);
                $('#errorMsgResult_' + clb_id).html(data);
                $('#errorMsgResult_' + clb_id).show();
            }
        })

    });
    ////////////////////////////////////////////////////////////
}
