/*PopUp Window*/
(function($){
    var defaults = {
        height: 300,
        width: 400,
        closetime: 1500
    }
    var methods = {
        init: function (params)
        {
            var data = $(this).data('popupWindow');
            var options = $.extend({}, defaults, params);
            options.ajax_lasturl = options.ajax_url;
            if (data)
            {
                $(this).data('popupWindow', {element : this, options: options});
                return this;
            }
            else
            {
                $(this).data('popupWindow', {element : this, options: options});
                $(this).html('<h3 class="closeButton">[X]</h3><div class="popupboxcontent"></div>');
            }
            return this;
        },
        open: function (htmlcontent)
        {
            if($(this).css('display') != 'block')
            {
                var $this = $(this), data = $this.data('popupWindow');
                var marginLeft =  - data.options.width/2 + 'px';
                var marginTop =  - data.options.height/2 + 'px';
                $(this).find("div.popupboxcontent").html(htmlcontent);
                $(this).css({'margin-left':marginLeft, 'margin-top':marginTop, 'width': data.options.width + 'px', 'height': data.options.height + 'px', 'display': 'block'});
                $('#pwboxbg').css('display', 'block');
                $('#pwboxbg').height($(document).height());
                $(this).find("h3.closeButton").bind("click.catalogViewer", {options: data.options, el: this, elbg: $('#pwboxbg')}, function(event)
                {
                    methods.closeBoxLayer(event.data.el, event.data.elbg, event.data.options);
                });
            }
        },
        close: function ()
        {
            if($(this).css('display') == 'block')
            {
                $(this).fadeOut("fast");
                $('#pwboxbg').fadeOut("fast");
                $(this).find("h3.closeButton").unbind('click.popupWindow');
            }
        },
        content: function (htmlcontent)
        {
            var $this = $(this), data = $this.data('popupWindow');
            $(this).css({'width': data.options.width + 'px', 'height': data.options.height + 'px'});
            $(this).find("div.popupboxcontent").html(htmlcontent);
        },
        contentClose: function (htmlcontent, timeout)
        {
            if($(this).css('display') == 'block')
            {
                var $this = $(this), data = $this.data('popupWindow');
                $(this).css({'width': data.options.width + 'px', 'height': data.options.height + 'px'});
                $(this).find("div.popupboxcontent").html(htmlcontent);
                setTimeout(function()
                {
                    $this.fadeOut("slow");
                    $('#pwboxbg').fadeOut("slow");
                    $this.find("h3.closeButton").unbind('click.popupWindow');
                }, timeout);
            }
        },
        ctrlBox : function (htmlcontent)
        {
            var $this = $(this), data = $this.data('popupWindow');
            var marginLeft =  - data.options.width/2 + 'px';
            var marginTop =  - data.options.height/2 + 'px';
            if($(this).css('display') == 'block')
            {
                $(this).fadeOut("fast");
                $('#pwboxbg').fadeOut("fast");
                $(this).find("h3.closeButton").unbind('click.popupWindow');
            }
            else
            {
                $(this).find("div.popupboxcontent").html(htmlcontent);
                $(this).css({'margin-left':marginLeft, 'margin-top':marginTop, 'width': data.options.width + 'px', 'height': data.options.height + 'px', 'display': 'block'});
                $('#pwboxbg').css('display', 'block');
                $('#pwboxbg').height($(document).height());
                $(this).find("h3.closeButton").bind("click.catalogViewer", {options: data.options, el: this, elbg: $('#pwboxbg')}, function(event)
                {
                    methods.closeBoxLayer(event.data.el, event.data.elbg, event.data.options);
                });
            }
        },
        closeBoxLayer : function (el, elbg, opt)
        {
            $(el).fadeOut("fast");
            elbg.fadeOut("fast");
            $(el).find("h3.closeButton").unbind('click.popupWindow');
        }
    }
    // Сам плагин
    $.fn.popupWindow = function(method)
    {
        if (methods[method]) {return methods[ method ].apply( this, Array.prototype.slice.call( arguments, 1 ));}
        else if ( typeof method === 'object' || ! method ) {return methods.init.apply( this, arguments );}
        else {$.error( 'Метод "' +  method + '" не найден в плагине jQuery.catalogViewer' );}
        return this;
    };
})(jQuery);
