/**
 * Trida jez zabaluje fce pro funci novinky e-mailem
 *
 * @param string idBanner ID objecktu, na ktery se po kliknuté zobrazi vrstva s formularem
 */
function NewsEmail(lng, url) {
    /**
     * @var DOMobject Objekt panelu pro zobrazeni formulare
     */
    this.elBanner = jQ('#newsEmail');
    /**
     * @var string URL adresa na soubor pro AJAX
     */
    this.url = '/' + lng + url;
    
    this.idClose = '#newsClose, #bottomClose';
    
    this.idPopup = '#popup_container';
    
    this.idLayer = '#popup_overlay';
}

/**
 * Zakladni fce. Volana v bloku po nacteni DOM objectu
 */
NewsEmail.prototype.create = function() {

    var thisObject = this;

    // Po udalosti se zobrazi formular
    jQ(this.elBanner).click(function() {

        var strContent = thisObject.getForm();
        thisObject.setContent(strContent, true);
        
        // Po udalosti se zobrazi formular
        jQ(thisObject.idClose).click(function() {
            thisObject.closePopup();
        });
    });
}

/**
 * Zobrazuje popup
 *
 * @param DOMobject elPopup 
 */
NewsEmail.prototype.enablePopup = function(elPopup) {
    var strId = this.idPopup + ', ' + this.idLayer;
    jQ(strId).addClass('show').removeClass('hide');
}

/**
 * Zobrazuje popup
 */
NewsEmail.prototype.closePopup = function() {
    var strId = this.idPopup + ', ' + this.idLayer;
    jQ(strId).addClass('hide').removeClass('show');
}

/**
 * Vraci HTML s formularem 
 *
 * @return string 
 */
NewsEmail.prototype.getForm = function() {

    var strContent = '';
    var thisObject = this;
    jQ.ajax({
        type: "POST",
        url: thisObject.url,
        async: false,
        data: 'viewForm=1',
        dataType: "xml",
   
        // Spracovava stazena data
        success: function(dgData) {
        	strContent = jQ('/page/content', dgData).text();
        },
    
        // Neuspesne stazeni dokumentu
        error: function(xmlObject, errorMsg, exception) {
            strContent = '';
        }
     });
     
     return strContent;
}

/**
 * Zobrazuje HTML predane metode
 *
 * @param string strContent HTML obsah
 * @param boolean enableForms Udava, jestli se maji nastavit formularove
 */
NewsEmail.prototype.setContent = function(strContent, enableForms) {

    // Zobrazuje pruhlednou vrstvu
    jQ.alerts.overlayOpacity = 0.9;
    jQ.alerts.focusCancelButton = true;
    jQ.alerts.draggable = false;
    jAlert(strContent);
    this.hideSysItems();
    
    if (enableForms == true) {
        inputCheckbox.create();
        inputRadio.create();
    }
    
    this.setLoginFunction();
}

/**
 * Nastavuje udalost
 */
NewsEmail.prototype.setLoginFunction = function() {

    var thisObject = this;

    var elForm = jQ('#newsMailForm');
    
    formNewByMail = new CheckForm('#newsMailForm');
    formNewByMail.setEvent('#unsubscribeSubmit', 'click');

    formNewByMail.isSelected('sex', '#newsSex');
    formNewByMail.isEmpty('news_name', '#newsAlertName');
    formNewByMail.isEmpty('news_surname', '#newsAlertSurname');
    formNewByMail.checkReg('news_email', '#newsAlertEmail', '^([+]?[_a-zA-Z0-9\-]+([.][_a-zA-Z0-9\-]+)*@([0-9a-zA-Z][0-9a-zA-Z\-]*[.])+[a-zA-Z]{2,6})$');    
    formNewByMail.isChecked('agreee', '#newsAlertAgree');
    
    // Predpokladaji se otestovana data, resi pouze zaslani pozadavku
    jQ(elForm).submit(function() {
          
        jQ('#newsContentDesign').html('<div class="waiting"></div>');

        
        var strSex = jQ(elForm).find('input:radio[@name=sex]:checked').val();
        var strName = jQ(elForm).find('input:text[@name=news_name]').val();        
        var strSurname = jQ(elForm).find('input:text[@name=news_surname]').val(); 
        var strEmail = jQ(elForm).find('input:text[@name=news_email]').val();
        
        var strData = 'saveData=1&sex=' + strSex + '&firstname=' + strName + '&name=' + strSurname + '&mail=' + strEmail;

        jQ.ajax({
            type: "POST",
            url: thisObject.url,
            async: false,
            data: strData,
            dataType: "xml",

            // Spracovava stazena data
            success: function(dgData) {
            	var strContent = jQ('/page/content', dgData).text();
            	
            	thisObject.setContent(strContent, true);
            	jQ(thisObject.idClose).click(function() {
                    thisObject.closePopup();
                });
            },
        
            // Neuspesne stazeni dokumentu
            error: function(xmlObject, errorMsg, exception) {}
        });

        return false;
    });
}

/**
 * Uklada uzivatelem vyplnene informace
 */
NewsEmail.prototype.hideSysItems = function() {
    jQ('#popup_panel, #popup_title').addClass('hide');
}

/**
 * Schovava komponenty, kde jsou dane frameworkem
 */
NewsEmail.prototype.hideSysItems = function() {
    jQ('#popup_panel, #popup_title').addClass('hide');
}
