/**
 * require tt.js, hib_core.js, jQuery
 */
IBE_NS.selectIffOrt = function () {
    var infowin = window.open("http://ttibe.traveltainment.pl/maps/google_v2/select_iff_ort_google.php?url=" + IBE_NS.baseUrl + "&mTStartMod=1&KID=" + IBE_NS.KID + "&ORTE=" + IBE_NS.ortList,"Kartenansicht","width=1000,height=500,scrollbars=no");
    infowin.focus();
};

//______________________________________________________________________________

IBE_NS.openagb = function (w_info, fenstername) {
    window.open(w_info,fenstername,"width=520,height=550,scrollbars=yes,menubar=yes");
};

//______________________________________________________________________________

IBE_NS.openInsurance = function (info,nameWindow) {
	window.open(info,nameWindow,"width=520,height=550,scrollbars=yes,menubar=yes");
};

//______________________________________________________________________________

IBE_NS.flugzeiten = function (abflugshafen, rueckflugsflughafen, Buchungs_ID, personen, veranstalter){
	var top = screen.height, left = screen.width, posx=((left / 2) - 190), posy = ((top / 2) - 150);
	window.open('http://ttibe.traveltainment.pl/booking_pl/flugzeiten.php?KID=' + this.KID + '&flugzeiten_kunde=&port=' + this.port + '&start=' + abflugshafen + '&ziel=' + rueckflugsflughafen + '&Buchungs_ID=' + Buchungs_ID + '&ordner_name=' + this.portalName + '&personen=' + personen + '&veranstalter=' + veranstalter,'Flugzeiten','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=no,copyhistory=no,width=500,height=250,screenX=' + posx + ',screenY=' + posy + ',left=' + posx + ',top=' + posy + '');
};

//______________________________________________________________________________

IBE_NS.legende = function () {
    var legwin = window.open('popup_legende.php?KID=' + this.KID + '&ordner_name=' + this.portalName,'Legende','width=420,height=250,scrollbars=no,statusbar=false,locationbar=false,toolbar=false');
    legwin.focus();
};

//______________________________________________________________________________

IBE_NS.datePickerConfig = function (op) {
    return {
        dateFormat : 'd.mm.yy',
        prevText : '',
        nextText : '',
        dayNamesMin : ['Nd', 'Pn', 'Wt', 'Sr', 'Cz', 'Pt', 'So'],
        monthNames : ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'],
        monthNamesShort: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'],
        firstDay : 1,
        minDate : new Date(tt.getTerminMin() * 1000),
        maxDate : new Date(tt.getTerminMax() * 1000),
        beforeShow : function(input, inst) {
            input.value = input.value.slice(-10);
        },
        onSelect : function (dateText, inst) {
            var form = tt.doc.getElementById('bengine'),
                tmp = (Date.UTC(parseInt(dateText.split('.')[2], 10), parseInt(dateText.split('.')[1], 10) - 1, parseInt(dateText.split('.')[0], 10))) / 1000;
            if (op === 0) {
                var date1 = tt.doc.getElementById('date1');
                var dayDate1 = new Date(
                    parseInt(dateText.split('.')[2], 10), parseInt(dateText.split('.')[1], 10) - 1, parseInt(dateText.split('.')[0], 10)
                );
                date1.value = date1.value;
                form.termin.value = tmp;
            } else if (op === 1) {
                var date2 = tt.doc.getElementById('date2');
                var dayDate2 = new Date(
                    parseInt(dateText.split('.')[2], 10), parseInt(dateText.split('.')[1], 10) - 1, parseInt(dateText.split('.')[0], 10)
                );
                date2.value = date2.value;
                form.ruecktermin.value = tmp;
            }
        },
        onClose : function (dateText, inst) {
            if (tt.doc.getElementById('bengine').termin.value > tt.doc.getElementById('bengine').ruecktermin.value) {
                $('#date2').css('color','#cc0000');
                tt.doc.getElementById('bengine').ruecktermin.value = tt.getTerminMax();
            } else {
                $('#date2').css('color','#838383');
            }
        }
    };
};

//______________________________________________________________________________

IBE_NS.showHotelMap = function (iffcode) {
    location.href = '#usfanchor';
	var xhr = tt.ajaxObj();
	xhr.onreadystatechange = function () {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
				if (xhr.responseText != 'error') {
					$('#google_error').hide('slow');
					$('#google_hotel_map').fadeIn('slow');
                    $('#map_canvas').fadeIn('slow');
					initialize(xhr.responseText.split('[%|%]')[0], xhr.responseText.split('[%|%]')[1]);
					//selectHotel(id);
				} else {
                    $('#google_hotel_map').fadeIn('slow');
					$('#google_error').html('Przepraszamy. Współrzędne dla tego hotelu są niedostępne');
					$('#google_error').show('slow');
					$('#map_canvas').hide('slow');
				}
			}
		}
	};
	xhr.open('POST', ((IBE_NS.engine === 'erde' || IBE_NS.engine === 'fewo') ? '../' : '') + 'inc/google_maps_cord.php', true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.send('IFF=' + iffcode);
    var initialize = function (lat, lng) {
        var latlng = new google.maps.LatLng(lat, lng);
        var myOptions = {
          zoom: 18,
          center: latlng,
          mapTypeId: google.maps.MapTypeId.HYBRID,
          streetViewControl: true
        };
        var map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
        var marker = new google.maps.Marker({
            position: latlng,
            map: map
        });
    }
    $('#close_map').click(function () {
        $('#google_hotel_map').fadeOut('slow');
    });
};

//______________________________________________________________________________

IBE_NS.displayGuide = function (getRegId) {
	var xhr = tt.ajaxObj(),
        getRegIdSplit = getRegId.split(';'), that = this;
	xhr.onreadystatechange = function () {
		if (xhr.readyState == 4) {
			if (xhr.status == 200) {
                if (xhr.responseText.split('|')[0] === 'error') {
                    alert('Przewodnik dla tego regionu jest niedostępny.');
                } else {
                    that.hib.configHotelInfoBox();
                    var el = $('#hibguide');
                    hib.show({view:'regdesc', ref_id: xhr.responseText.split('|')[0], weatherAirport: xhr.responseText.split('|')[1], elements:{main: $(el).after('<div class="hib"></div>'), remove: $(el.nextSibling)}});
                    location.href = '#usfanchor';   
                }
			}
		}
	};
	xhr.open('POST', ((IBE_NS.engine === 'erde' || IBE_NS.engine === 'fewo') ? '../' : '') + 'inc/guider.php', true);
	xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	xhr.send('flugdauer=' + getRegIdSplit[1] + '&zk=' + getRegIdSplit[0]);
};

//______________________________________________________________________________

IBE_NS.hib = {
    
    configHotelInfoBox : function () {
        var pfadImages = (IBE_NS.fav ? '../' : '') + ((IBE_NS.engine === 'erde' || IBE_NS.engine === 'fewo') ? '../' : '')  + 'public/images/hib';
        hib.KID = IBE_NS.KID;
        hib.config.starImage = pfadImages  + '/hotelinfobox/star.gif';
        hib.config.starHalfImage = pfadImages  + '/hotelinfobox/star_half.gif';
        hib.config.structureHTML = '<div id="menu"></div><div id="content"></div><div class="closebar"><span onclick="hib.hideCurrentBox()">zwiń</span></div>';
        hib.config.loadingAnimationHTML = '<div class="loading"><img src="' + pfadImages + '/loader.gif" alt="" /><p>Ładowanie danych</p></div>';
        hib.config.areaMoreLinkTxt = '<img src="' + pfadImages  + '/hotelinfobox/okolica_more.gif" alt="[+]" />';
        hib.pfadImages = pfadImages;
        hib.config.slideshowInitialSize = 2;
        hib.config.menuType = 'TWOLINES';
        hib.config.connector = (IBE_NS.fav ? '../' : '') + ((IBE_NS.engine === 'erde' || IBE_NS.engine === 'fewo') ? '../' : '')  + 'inc/hib_connector.php';
        hib.config.animationTime = 0;
        op.config.show_detailed = '<span class="op02"><a href="#" onclick="return false;"><img src="' + pfadImages + '/readComment.png" alt="" /></a></span>';
    },

    //__________________________________________________________________________

	makePlaceForTopRegionHIB : function (el) {
		this.configHotelInfoBox();
        $(el.parentNode.parentNode.parentNode).after('<div class="hib"></div>');
        var hibdiv = $(el.parentNode.parentNode.parentNode.nextSibling);
		return {main: hibdiv, remove: hibdiv};
	},
    
    //__________________________________________________________________________
    
    makePlaceForRegionHIB : function (el, weather) {
        this.configHotelInfoBox();
        var hibdiv = null;
        if (weather) {
            $(el).parent().after('<div class="hib"></div>');
            hibdiv = $(el).parent().next();            
        } else {
            $(el).parent().parent().after('<div class="hib"></div>');
            hibdiv = $(el).parent().parent().next();
        }
        return {main: hibdiv, remove: hibdiv};
    },
    
    //__________________________________________________________________________
    
    makePlaceForHotelHIB : function (el) {
        this.configHotelInfoBox();
        $(el).parent().parent().after('<div class="hib"></div>');
        var hibdiv = $(el).parent().parent().next();
        return {main: hibdiv, remove: hibdiv};
    },
    
    //__________________________________________________________________________
    
    makePlaceForBookingHIB : function (el) {
        this.configHotelInfoBox();
        $(el).parent().parent().after('<div class="hib"></div>');
        var hibdiv = $(el).parent().parent().next();
        return {main: hibdiv, remove: hibdiv};
    },
    
    //__________________________________________________________________________
    
    makePlaceForTerminHIB : function (el, op) {
        this.configHotelInfoBox();
        var hibdiv = null;
        if (op === 1) {
            $(el).parent().parent().after('<div class="hib"></div>');
            hibdiv = $(el).parent().parent().next();
        } else if (op === 2) {
            $(el).parent().parent().parent().parent().after('<div class="hib"></div>');
            hibdiv = $(el).parent().parent().parent().parent().next();
        } else {
            $(el).parent().parent().parent().after('<div class="hib"></div>');
            hibdiv = $(el).parent().parent().parent().next();
        }
        return {main: hibdiv, remove: hibdiv};
    },
    
    //__________________________________________________________________________
    
    makePlaceForOpinionHIB : function (el, op) {
        this.configHotelInfoBox();
        var hibdiv = null;
        if (op === 1) {
            $(el).parent().parent().parent().after('<div class="hib"></div>');
            hibdiv = $(el).parent().parent().parent().next();
        } else if (op === 2) {
            $(el).parent().parent().parent().parent().parent().before('<div class="hib"></div>');
            hibdiv = $(el).parent().parent().parent().parent().parent().prev();
        } else {
            $(el).parent().after('<div class="hib"></div>');
            hibdiv = $(el).parent().next();
        }
        return {main: hibdiv, remove: hibdiv};
    },
    
    //__________________________________________________________________________
    
    makePlaceForHIBAirport : function (el, op) {
        this.configHotelInfoBox();
        var hibdiv = null;
        if (op === 1) {
            $(el).parent().parent().parent().parent().after('<div class="hib"></div>');
            hibdiv = $(el).parent().parent().parent().parent().next();
        } else {
            $(el).parent().parent().after('<div class="hib"></div>');
            hibdiv = $(el).parent().parent().next();
        }
        return {main: hibdiv, remove: hibdiv};
    }
};


//______________________________________________________________________________


IBE_NS.favourite = {
    add : function (el, arr) {
        var str_Hattbut = '',
            np = String(parseInt(arr[1] / 5000) * 5000),
            bg_movement = (100 - (arr[9] * 100 / 6)) + '% 0;',
            opinions_text = '',
            xhr = tt.ajaxObj();
        while (np.length < 5) {
            np = '0' + np;
        } 
        if (arr[5] == 0) {
            opinions_text = 'brak opinii';
        } else {
            opinions_text = arr[5];
        }
        var params = 'IFF=' + arr[1] + '&rurl=' + escape(location.href) + '&data=' + arr[3] + '|' + arr[4].replace('&','') + '|/images/content/booking_thumbs_mi/' + np + '/THB_999_H' + arr[1] + '.jpg|';
        params += arr[7].replace('&','') + '<br/>' + arr[8].replace('&','') + '|' + ConvertPriceValues(arr[0],IBE_NS.recalculationType,'EUR') + '|' + opinions_text + '|' + arr[9] + '|' + arr[10] + '|' + bg_movement + '|' + arr[1] + '|';
        params += arr[2] + '|' + arr[6] + '|' + '&engine=' + IBE_NS.engine;
        xhr.onreadystatechange = function () {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    $(el.parentNode).html('W schowku');
                    IBE_NS.favourite.count($('#favourite'));
                }
            }
        }
        xhr.open('POST', ((IBE_NS.engine === 'erde' || IBE_NS.engine === 'fewo') ? '../' : '') + 'favourite/request.php', true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.send(params);
    },
    open : function () {
        window.open(((IBE_NS.engine === 'erde' || IBE_NS.engine === 'fewo') ? '../' : '') + 'favourite/popup.php','markerWindow','width=800,height=500,menubar=yes,location=yes,resizable=yes,scrollbars=yes,status=yes');
    },
    count : function (el) {
        var xhr = tt.ajaxObj();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    el.html('<h1>Schowek</h1><span onclick="IBE_NS.favourite.open();">Oferty w schowku: ' + xhr.responseText + '</span>');
                }
            }
        }
        xhr.open('POST', ((IBE_NS.engine === 'erde' || IBE_NS.engine === 'fewo') ? '../' : '') + 'favourite/request.php', true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.send('count_offers=1&engine=' + IBE_NS.engine);
    },
    del : function (iff) {
        var xhr = tt.ajaxObj();
        xhr.onreadystatechange = function() {
            if (xhr.readyState == 4) {
                if (xhr.status == 200) {
                    location.reload();
                }
            }
        }
        xhr.open('POST', ((IBE_NS.engine === 'erde' || IBE_NS.engine === 'fewo' || IBE_NS.fav) ? '../' : '') + 'favourite/request.php', true);
        xhr.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xhr.send('engine=' + IBE_NS.engine + '&IFF=' + iff + '&delete=true');        
    }
};






