/**
 * 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() {
    
    /**************************
    ** jQuery - PROMO BANNER **
    **************************/
    // alternativa k XML body bannerum - 2 moznosti k zobrazeni banneru
    // nastavuje se v property - <showSlidesData/>
    // overime vlozeni HTML pro Promo
    if ((typeof promoDataArray != 'undefined') && insertPromoHtml(promoDataArray)) {
        // spustime Promo animaci
        runPromoAnimation();
    }
    
    /*********************
    ** BOTTOM TEXT BODY **
    *********************/
    bottomTextEffect();

    /**********************
    ** SPECIALNI NABIDKY **
    **********************/
    callSpecialOffers();
    
    /*******************************
    ** NASTAVUJE NAHRADU ZA FLASH **
    *******************************/
    setAlternativeContent(); 
    
    /* ********
    * 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}$');    
    if (jQ('#internetCheckin').length != 0) {
         jQ('#internetCheckin').submit(function() {
             origin = this.sourceOrigin.value.match(/\(([^\)]+)\)$/);
             if (origin) this.sourceOrigin.value = origin[1];
             // ochrana, nesmi se odesilat s prazdnymi zavorkami
             this.sourceOrigin.value = this.sourceOrigin.value.replace(/( ?\(\))$/, '');
         });
    }
    
    // Naseptavac
    var isIciWhisperer = true;
    setWhisperer('from1', 'from1target', 'selectTargetListFrom1', true, isIciWhisperer);
    
    /****************************
    ** 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();
    
    
    
});

/**
 * Nastavuje nahradu za promo banner v pripade vypleho flashe
 * Fce je napsana konkretne pro homepage
 */
function setAlternativeContent() {
    
    var elData = jQ('#dataBanner');
    var linkUrl = jQ(elData).children('input[@name="param1"]').val();
    var imgSrc = jQ(elData).children('input[@name="param2"]').val();
    var isTarget = jQ(elData).children('input[@name="param3"]').val();     
    
    var playerVersion = swfobject.getFlashPlayerVersion();
    
    if (playerVersion.major == 0) {
    
        var html = '';
        
        if (linkUrl == '') {
            html = '<div class="promo"><img src="' + imgSrc + '"></div>';
        } else if (isTarget == '_blank') {
            html = '<div class="promo"><a href="' + linkUrl + '" target="_blank"><img src="' + imgSrc + '"></a></div>';
        } else {
            html = '<div class="promo"><a href="' + linkUrl + '"><img src="' + imgSrc + '"></a></div>';
        }
        
        jQ('#promo').replaceWith(html);
    }
}

/**
 * Nastavuje funkcionalitu do paticky stranky
 *
 * @retun void
 */
/*
function bottomTextEffect() {
    
    var idSecBottomTextBody = 'sec_hp_bottom_text_body';
    var idSecBottomTextBodyInner = 'sec_hp_bottom_text_body_inner';
    var idButtonBottomTextBody = 'item_show_text_hp_body';
    
    var bottomTextBodyHeight = jQ('#'+idSecBottomTextBody).height();
    var bottomTextPHeight = jQ('#'+idSecBottomTextBody+' p:eq(0)').height() + jQ('#'+idSecBottomTextBody+' p:eq(1)').height();
    var diffHeight = (jQ('#'+idSecBottomTextBody).height() - bottomTextPHeight) * 1;
    var paddingWrapper = jQ('#wrapper').css("padding-bottom").replace("px", "") * 1;
    
    // sbalime div na velikost prvnich dvou odstavcu textu
    jQ('#'+idSecBottomTextBody).css('height', (bottomTextPHeight + 'px'));
    
    jQ('#'+idButtonBottomTextBody).bind('click.sec_hp_bottom_text_body', function(){
        if (jQ('#'+idButtonBottomTextBody).hasClass('sec_hp_up')) {
            
            jQ('#wrapper').animate( { paddingBottom: paddingWrapper + 'px'}, 600, function() {});
                    
            jQ('#'+idSecBottomTextBody).animate( { height: bottomTextPHeight+'px'}, 600, function(){
                jQ('#'+idButtonBottomTextBody).removeClass('sec_hp_up');
            });
        } else {

            jQ('#wrapper').animate( { paddingBottom: (paddingWrapper+diffHeight) + 'px'}, 600, function() {});
            
            jQ('#'+idSecBottomTextBody).animate( { height: bottomTextBodyHeight + 'px' }, 600, function(){
                jQ('#'+idButtonBottomTextBody).addClass('sec_hp_up');
            });
        }
        
    });
}
*/

/**
 * Nastavuje funkcionalitu do paticky stranky
 *
 * @retun void
 */
function bottomTextEffect() {
    
    var consBlockHeight = 56;//56px
    var idSecBottomTextBody = 'sec_hp_bottom_text_body';
    var idSecBottomTextBodyInner = 'sec_hp_bottom_text_body_inner';
    var idButtonBottomTextBody = 'item_show_text_hp_body';
    
    var bottomTextBodyHeight = jQ('#'+idSecBottomTextBody).height();
    var bottomTextPHeight = jQ('#'+idSecBottomTextBody+' p').height();
    // prvni height je 54 tzn. ze pro zvetseni potrebujeme velikost bloku celeho textu
    // minus consBlockHeight - protoze potrebujeme + prave tento rozdil
    var diffHeight = (bottomTextBodyHeight - consBlockHeight) * 1;
    var elWrapper = jQ('#wrapper');
    var paddingWrapper = jQ(elWrapper).css("padding-bottom").replace("px", "") * 1;

    // sbalime div na velikost prvnich dvou odstavcu textu - natvrdo
    //jQ('#'+idSecBottomTextBody).css('height', (bottomTextBodyHeight + 'px'));
    jQ('#'+idSecBottomTextBody).css('height', (consBlockHeight + 'px'));
    
    //hack pro IE6 - neumi dopocitat padding bottom, proto se musi znovu nastavit na to tu samou hodnotu
    jQ(elWrapper).css('padding-bottom', (paddingWrapper) + 'px');
    
    jQ('#'+idButtonBottomTextBody).bind('click.sec_hp_bottom_text_body', function(){
        if (jQ('#'+idButtonBottomTextBody).hasClass('sec_hp_up')) {
            jQ(elWrapper).css('padding-bottom', paddingWrapper + 'px');
            jQ('#'+idSecBottomTextBody).height(consBlockHeight + 'px');
            jQ('#'+idButtonBottomTextBody).removeClass('sec_hp_up');    
        } else {
            jQ(elWrapper).css('padding-bottom', (paddingWrapper + diffHeight) + 'px');
            jQ('#'+idSecBottomTextBody).height(bottomTextBodyHeight + 'px');
            jQ('#'+idButtonBottomTextBody).addClass('sec_hp_up');
        }
        
    });
}

/**
 * Volani AJAX - specialni nabidky
 */
function callSpecialOffers() {

   var elData = jQ('#dataSpecOff');
   var url = jQ(elData).children('input[@name="param1"]').val();
   var zastoupeni = jQ(elData).children('input[@name="param2"]').val();
   var isBoardLink = jQ(elData).children('input[@name="param3"]').val();     
   var boardLink = jQ(elData).children('input[@name="param4"]').val();
   

  jQ.ajax({
   type: "GET",
   async: true,
   url: url,
   data: 'zastoupeni='+zastoupeni+'&boardLink='+isBoardLink,
   dataType: "xml",
   success: function(dgData){
   		xmlData = dgData;
   		drawSpecialOffersHtml(isBoardLink, boardLink);
   		setTooltip();
   		
   		// SelectBox create
        aagForms.createSelectBox();
   		
   },
   error: function(xmlObject, errorMsg, exception) { 
    //alert('AJAX error');
   }
 });
}

function drawSpecialOffersHtml(isBoardLink, boardLink) {
    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);
            
        }
    );
}


/* JS OBSTARAVAJICI POP-UP DIV NA HOMEPAGE - PF 2011 */
var selectedObj = null;
var offsetX, offsetY;

function dragDiv(event) {
    if (window.event) event = window.event;
    if (selectedObj) {
    	if (document.all) {
            selectedObj.pixelLeft = event.clientX - offsetX;
            selectedObj.pixelTop = event.clientY - offsetY;
        } else {
            selectedObj.left = event.clientX - offsetX;
            selectedObj.top = event.clientY - offsetY;
        }
        return false;
    }
}

function releaseDiv() {
    if (selectedObj) {
        selectedObj.zIndex = 999;
        selectedObj = null;
    }
}
function engageDiv(id,event) {
    if (document.getElementById) {
        var obj = document.getElementById(id);
        if (obj.style) {
            selectedObj = obj.style;
            selectedObj.zIndex = 999;
            if (document.all) {
                offsetX = event.clientX - selectedObj.pixelLeft;
                offsetY = event.clientY - selectedObj.pixelTop;
            } else {
                offsetX = event.clientX - parseInt(selectedObj.left,10);
                offsetY = event.clientY - parseInt(selectedObj.top,10);
            }
            return false;
        }
    }
}

function closeDiv(id) {
    if (document.getElementById) {
        var obj = document.getElementById(id);
        if (obj.style) {
            selectedObj = obj.style;
            selectedObj.display = "none";
            selectedObj = null;
            return false;
        }
    } else if (document.layers) {
        document.layers[id].visibility = "hide";
        return false;
    }
}

document.onmousemove = dragDiv;
document.onmouseup = releaseDiv;

/* ------------------------------------------------- */
