/**
 * Nacitani veskerych JS fci pro homepage. Fce jsou vetsinou zdokumentovany.
 * Kazda fce, pokud se nejedna o knihovnu typu modulu do jQuery je zdokumentovana
 * nad jeji deklaraci.
 */
jQ(document).ready(function() {

    /* ********
    * ZALOZKY *
    **********/

    // Prepinani mezi zalozkami bookingu
    switchDomObject('#bookingFrameSwitch', '#bookingFrameForms', 'active');
    
    // Prepinani mezi zalozkami nadvahy a checkin
    switchDomObject('#bottomFrameSwitch', '#bottomFrameForms', 'active');    
    
    // Prepinani mezi zalozkami login OK PLUS/OK PLUS Corporate
    switchDomObject('#loginFrameSwitch', '#loginFrameForms', 'active');  
    
    /************************
    ** BOOKING *
    *************************/
    
    onReadyBooking(/*calendarDep, calendarRet*/);
    
    /************************
    ** FORMULAR CheckMyTrip *
    *************************/

    var formCheckMyTrip = new CheckForm('#checkMyTrip');
    formCheckMyTrip.isEmpty('name', '#nameUserAlert');
    formCheckMyTrip.isEmpty('locator', '#nameUserLocator');

    /*******************************
    ** FORMULAR Internet-Check-In ** 
    *******************************/

    var formCheckIn = new CheckForm('#internetCheckin');
    formCheckIn.isEmpty('sourceOrigin', '#sourceOriginCheckin');
    formCheckIn.checkReg('ticketNumber', '#ticketCheckin', '[0-9]{13,14}$');
    formCheckIn.checkReg('flightCode', '#flightCodeCheckin', '^[A-Z]{2}$');
    formCheckIn.checkReg('flightNumber', '#flightNumberCheckin', '^[0-9]{2,4}$');    
    
    /****************************
    ** FORMULAR Excess baggage ** 
    ****************************/

    var formCheckIn = new CheckForm('#loginExcessBaggage');
    formCheckIn.isEmpty('passenger_surname', '#surnameExbag');
    formCheckIn.isEmpty('passenger_name', '#nameExbag');
    formCheckIn.isEmpty('locator', '#locatorExbag');  
    
    /***************************
    ** FORMULAR OK PLUS login ** 
    ***************************/
    
    var formCheckLogin = new CheckForm('#okplusKonto');
    formCheckLogin.setCheckFunction(function () {
        jQ('#csanoLogin').addClass('hide').removeClass('show');
        var result = true;
        var elValue = jQ('#csano').val();        
        if (elValue.length == 0) {
            result = false;
        // TODO nefunguje 
        /*} else if (isValidCSAno(elValue)) {
            result = false;*/
        }
        if (result == false) {
            jQ('#csanoLogin').removeClass('hide').addClass('show');
        }
		
		return result;
    });
    formCheckLogin.setCheckFunction(function () {
        jQ('#sitano_pomLogin').addClass('hide').removeClass('show');
        var result = false;
        var strLen = jQ('#sitano_pom').val().length;
        if (strLen >= 5 && strLen <= 10) {
            result = true;
        }
        if (result == false) {
            jQ('#sitano_pomLogin').removeClass('hide').addClass('show');
        }
		
		return result;
    });
    formCheckLogin.setCheckFunction(function () {
        copyItem('okplusKonto', 'sitano_pom', 'sitano');
        clearItem('okplusKonto', 'sitano_pom');
    });
    
    /*************************************
    ** FORMULAR OK PLUS CORPORATE login ** 
    *************************************/
    
    var formCheckLoginCor = new CheckForm('#okplusCorp');
    formCheckLoginCor.isEmpty('login', '#loginLoginCor');
    formCheckLoginCor.isEmpty('pwd_pom', '#passwdLoginCor');
    formCheckLoginCor.isEmpty('language', '#lngLoginCor');  
    formCheckLoginCor.setCheckFunction(function () {
        //changeActionFBP();
        copyItem('okplusCorp', 'pwd_pom', 'password');
        clearItem('okplusCorp', 'pwd_pom');
    });
    
    
    /*******************
    ** BANNERY Sluzby ** 
    *******************/
    
    bannersListing('#bannersServices', '#bannersFrames', '#previewBanner', '#nextBanner');
    
    /***************
    ** INFOSTRIPE ** 
    ***************/
    
    var infostripe = new Inforstripe();
    infostripe.setInfostripe();
    
    /******************
    ** NEWS BY EMAIL **
    ******************/
    
    var newsEmail = new NewsEmail(lng, '/info/xmldata_news_email.xml');
    newsEmail.create();
    
    
    /*****************************************
    ** BOTTOM_TEXT_BODY >> EXTENDED WRAPPER **
    *****************************************/
    jQ(".bottom_text_body").addClass("ie"); // IE workaround, aby prekreslil div a zobrazil jej korektne    
    var btbVyska = jQ(".bottom_text_body").outerHeight();
    btbVyska = btbVyska + 10;
    jQ("#wrapper").css("padding-bottom", btbVyska);
    
});

/**
 * Volani AJAX - specialni nabidky
 */
function callSpecialOffers(url, zastoupeni, isBoardLink) {
  jQ.ajax({
   type: "GET",
   async: true,
   url: url,
   data: 'zastoupeni='+zastoupeni+'&boardLink='+isBoardLink,
   dataType: "xml",
   success: function(dgData){
   		xmlData = dgData;
   		drawSpecialOffersHtml(isBoardLink);
   		setTooltip();
   		
   		// SelectBox create
        aagForms.createSelectBox();
   		
   },
   error: function(xmlObject, errorMsg, exception) { 
    //alert('AJAX error');
   }
 });
}

function drawSpecialOffersHtml(isBoardLink) {
    var item, aProduct;
    var sToOptgr;
    if (!xmlData) return;
    
    var htmlSpecialOffers = jQ(xmlData).find('page');
    jQ("#specialOfferWaiting").hide();
    jQ("#specialOfferAjaxTarget").append( htmlSpecialOffers.text() );
    
    //je nastaven board link ve vlastnostech dokumentu a je ulozen v property - specialframe/specialoffer
    if(isBoardLink == 'true') {
        jQ("#board_link_combo_filter").removeAttr('action');
        jQ("#board_link_combo_filter").attr('action', boardLink);
        
        jQ("#board_link_all_offers").removeAttr('href');
        jQ("#board_link_all_offers").attr('href', boardLink);    
    }
    
    // Nastavuje zobrazeni informaci o poplatcich
    jQ('#specialOffersCharges, #specialOffersCharges2').click(function() {
        viewHelp(1);
    });
   
    return true;
}


/**
 * Nastavuje tooltip na ikony ve specialnich nabidkach po najeti mysi
 */
function setTooltip() {
    var elSpecOff = jQ('#specialOfferAjaxTarget');
    var elTooltip = jQ('#specialOfferTooltip', elSpecOff);
    
    var iconTimer = null;
    
    var closeFunction = function () {
        jQ(elTooltip).addClass('hide').removeClass('show');
    }
    
    jQ(elTooltip).hover(
        function() {
            clearTimeout(iconTimer);
        },
        function() {
            iconTimer = setTimeout(closeFunction, 500);
        }
    );
    
    jQ('table td.ico', elSpecOff).hover(
        function() {

            jQ(elTooltip).find('#specialOffersCharges2 span').removeClass('show').addClass('hide');

            var isWithInc = jQ(this).hasClass('inc');
            var isRoundtrip = jQ(this).hasClass('roundtrip');

            if (isRoundtrip == true && isWithInc == true) {
                jQ(elTooltip).find('#specOffRoundInc').removeClass('hide').addClass('show');
            } else if (isRoundtrip == false && isWithInc == true) {
                jQ(elTooltip).find('#specOffOneInc').removeClass('hide').addClass('show');
            } else if (isRoundtrip == true && isWithInc == false) {
                jQ(elTooltip).find('#specOffRound').removeClass('hide').addClass('show');
            } else if (isRoundtrip == false && isWithInc == false) {
                jQ(elTooltip).find('#specOffOne').removeClass('hide').addClass('show');          
            }
        
            clearTimeout(iconTimer);
        
            var posY = this.offsetTop;
            posY = posY + 30; // Upravuje pozici nabidky pro design
            
            jQ(elTooltip).css('top', posY);
            jQ(elTooltip).addClass('show').removeClass('remove');
        },
        function() {
            iconTimer = setTimeout(closeFunction, 4000);
            
        }
    );
}
