
function BxSctrMain(oOptions)
{
    this._sCustomBlock = 'site_customize';
    this._sPublishBlock = 'dynamicPopup';

    this.sBaseUrl = oOptions.sBaseUrl;
    this.sReset = oOptions.sReset == undefined ? 'Reset?' : oOptions.sReset;
    this.sErrThemeName = oOptions.sErrThemeName == undefined ? 'Please fill name of theme' : oOptions.sErrThemeName;
    this.sErrChooseTheme = oOptions.sErrChooseTheme == undefined ? 'Choose any theme' : oOptions.sErrChooseTheme;
    this.sDeleteTheme = oOptions.sDeleteTheme == undefined ? 'Delete theme?' : oOptions.sDeleteTheme;
    this.sResetPage = oOptions.sResetPage == undefined ? 'Reset page?' : oOptions.sResetPage;
}

BxSctrMain.prototype.showBlock = function() {
	var $this = this;
	var oDate = new Date();
	var iOpen = $('#' + this._sCustomBlock).is(':visible') ? 0 : 1;

	$.get(
		this.sBaseUrl + 'open/' + iOpen,
		{
			_t:oDate.getTime()
		},
		function() {
			$('#' + $this._sCustomBlock).bx_anim('toggle', 'fade', 'slow', function() {
				if($(this).is(':visible'))
					dbTopMenuLoad('site_customize');
			});
		},
		'json'
	);
};

BxSctrMain.prototype.updateBlock = function(sName, sUrl)
{
    var oBlock = $('#' + sName);
    
    if ($(oBlock).length > 0)
        getHtmlData(oBlock, sUrl, null);
};

BxSctrMain.prototype.saveChanges = function(oCallback)
{
    var oForm = $('#' + this._sCustomBlock + ' form');
    
    if ($(oForm).length > 0)
    {
        var options = {
            success: function(data) {
                oCallback(data);
            }
        }; 
        
        $(oForm).ajaxSubmit(options);
    }
};

BxSctrMain.prototype.reloadCustomizeBlock = function(sUrl, isReset)
{
	var oForm = $('#' + this._sCustomBlock + ' form');
	
	if ($(oForm).length > 0)
	{
	    
	    if (isReset)
	    {
	        if (!confirm(this.sReset))
	            return;
    	    var sNewAction = $(oForm).attr('action') + '/1';
    	    $(oForm).attr('action', sNewAction);
	    }
	    
	    var $this = this;
	    
        var options = {
            success: function(data) {
                getHtmlData($('#' + $this._sCustomBlock), sUrl, null);
            }
        };
        
        $(oForm).ajaxSubmit(options);
	}
	else
	    getHtmlData($('#' + this._sCustomBlock), sUrl, null);
};

BxSctrMain.prototype.resetCustom = function(sPage, sTarget, sAction)
{
    if (confirm(this.sReset))
    {
        var oForm = $('#' + this._sCustomBlock + ' form');
        
        if ($(oForm).length > 0)
        {
            var sNewAction = $(oForm).attr('action') + '/1';
            var $this = this;
            var options = {
                success: function(data) {
                	window.location.href = window.location.href;
                }
            };

            $(oForm).attr('action', sNewAction);
            $(oForm).find("[name='action']").val(sAction);
            $(oForm).ajaxSubmit(options);
        }
    }
};

BxSctrMain.prototype.reloadCustom = function(sPage, sTarget, sAction)
{
	$("#" + this._sCustomBlock + " form [name='action']").val(sAction);

    this.saveChanges(function(data) {
    	window.location.href = window.location.href;
    });
};

BxSctrMain.prototype.showPublish = function(sUrl)
{
    var oForm = $('#' + this._sCustomBlock + ' form');
    var $this = this;
    
    if ($(oForm).length > 0)
    {
        var options = {
            success: function(data) {
                if (!$('#' + $this._sPublishBlock).length) {
                    $('<div id="' + $this._sPublishBlock + '" style="width:490px; display:none;"></div>').prependTo('body');
                }
                
                $('#' + $this._sPublishBlock).load(
                    sUrl,
                    function() {
                        $(this).dolPopup({
                            fog: {
                                color: '#fff',
                                opacity: .7
                            }, 
                            position: 'centered-absolute',
                            left: 0,
                            top: 0
                        });
                    }
                );
            }
        }; 
        
        $(oForm).ajaxSubmit(options);
    }
};

BxSctrMain.prototype.savePublish = function()
{
    var oForm = $('#' + this._sPublishBlock + ' form');
    
    if ($(oForm).length > 0)
    {
        if ($('.form_input_text').val())
        {
            var $this = this;
            var options = {
                success: function(data) {
                    var oPublishBlock = $('#' + $this._sPublishBlock);
                    if ($(oPublishBlock).length > 0)
                        $(oPublishBlock).html(data);
                }
            }; 
            
            $(oForm).ajaxSubmit(options);
        }
        else
            alert(this.sErrThemeName);
    }
};

BxSctrMain.prototype.selectTheme = function(oElement, iThemeId)
{
    var oRadio = $(oElement).find('input[type=radio]');
    
    if (oRadio.length > 0)
        oRadio.attr('checked', 1);
};

BxSctrMain.prototype.previewTheme = function()
{
    var iSelectTheme = this.getSelectTheme();

    if (iSelectTheme != -1) {
    	var oForm = $('#' + this._sCustomBlock + ' form');
    	var oOptions = {
            success: function(data) {
            	window.location.href = window.location.href;
            }
        };

    	$(oForm).ajaxSubmit(oOptions);
    }
    else
        alert(this.sErrChooseTheme);
};

BxSctrMain.prototype.saveTheme = function()
{
    var iSelectTheme = this.getSelectTheme();
    if(iSelectTheme != -1)
    	getHtmlData($('#' + this._sCustomBlock), $('#save_url').val() + iSelectTheme, function() {
    		window.location.href = window.location.href;
    	});
    else
        alert(this.sErrChooseTheme);
};

BxSctrMain.prototype.deleteTheme = function(sUrl)
{
    var iSelectTheme = this.getSelectTheme();
    
    if (iSelectTheme != -1)
    {
        if (confirm(this.sDeleteTheme))
            getHtmlData($('#' + this._sCustomBlock), sUrl + iSelectTheme);
    }
    else
        alert(this.sErrChooseTheme);
};

BxSctrMain.prototype.resetAll = function(sUrl)
{
    if (confirm(this.sResetPage))
    	getHtmlData($('#' + this._sCustomBlock), sUrl, function() {
    		window.location.href = window.location.href;
    	});
};

BxSctrMain.prototype.getSelectTheme = function()
{
    var oSelectTheme = $('#' + this._sCustomBlock + ' form input[type=radio]:checked');
    
    if ($(oSelectTheme).length > 0)
        return $(oSelectTheme).val();
    
    return -1;
};
