//
// Allgemeine Travel Suite Funktionen TUI
//
// author: sascha.rose@pixell.de
// version: 02.12.2008
//
// 02.04.2008: bugfix ".class" => ".hotelClass" (problem IE)
// 26.06.2008: added debug links in getLinkPackage()
// 05.08.2008: 12fly getByCodeOrdered()
// 05.08.2008: added travelsuite_page_link_suffix (tracking)
// 05.11.2008: remove getLinkProductInfo()
// 03.12.2008: fix new tui path (case sensitive)

var travelsuite_xml_path = "fileadmin/tui/pixell/Live/Xml/";
var travelsuite_pic_path = "fileadmin/tui/pixell/TSMedia/";

// onLoad
var blank = new Image();
blank.src = 'http://www.tui.com/fileadmin/tui/pixell/pics/icons/x.gif';

jQuery(document).ready(function() {
    travelsuite_start();
});

function travelsuite_start_params(xml_path, pic_path)
{
    travelsuite_xml_path = xml_path;
    travelsuite_pic_path = pic_path;
    travelsuite_start();
}

function travelsuite_start()
{
    try {
        if (travelsuite_code.indexOf('|') > 0)
        {
            var codes = travelsuite_code.split('|');
            for (var i = 0; i < codes.length; i++)
                ts_loadData(codes[i]);
        }
        else
        {
            ts_loadData(travelsuite_code);
        }
    } 
    catch (e) {
        if (e.message != "travelsuite_code is not defined")
        {
            throw(e);
        }
    }
}

function fixPng(png) {
   // get src
   var src = png.src;
   
   // set width and height
   if (!png.style.width) { png.style.width = $(png).width(); }
   if (!png.style.height) { png.style.height = $(png).height(); }
   // replace by blank image
   png.onload = function() { };
   png.src = blank.src;
   // set filter (display original image)
   png.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}

// Load Travel Suite XML-Data
function ts_loadData(code) {
    var url = travelsuite_xml_path + code + ".xml";
    
    // <debug>
    /*if (window.location.hostname == "localhost" || window.location.hostname.indexOf("byron-stange") > -1)
    {
        url = "fileadmin/TUI/pixell/Live/Xml/" + code + ".xml"; 
        travelsuite_pic_path = "fileadmin/TUI/pixell/TSMedia/";
    }*/
    /*if (window.location.hostname == "tui.ts.pixell.de")
    {
        url = "fileadmin/TUI/pixell/Test/Xml/" + code + ".xml"; 
        travelsuite_pic_path = "fileadmin/TUI/pixell/TSMedia/";
    }
    // </debug> */
    
    jQuery.ajax({
      url: url,
      dataType: "xml",
      success: function(xml){
        renderOffers(new TsOffers(xml));
      }
    });
}

// Travel Suite Offers Object (page, offers collection)
function TsOffers(xml) {
    this.page = new TsPage(xml);
    var l = jQuery(xml).find("Position").length;
    var offers = new Array(l);
    jQuery(xml).find("Position").each(function(i){
        offers[i] = new TsOffer(this);
    });
    this.offers = offers;
    
    this.getByCode = function(code) {
        var obc = new Array();
        for (var i = 0; i < offers.length; i++)
        {
            if (this.offers[i].code != undefined 
                && this.offers[i].code.toLowerCase() == code.toLowerCase())
            {
                obc.push(this.offers[i]);
            }
        }
        return obc;
    }
    
    this.getByCodeOrdered = function(code) {
        
        this.byPrice = function(a, b) {
            return a.price - b.price;
        }
        
        var obc = this.getByCode(code);
        obc.sort(this.byPrice);
        return obc;
    }
}

// Travel Suite Page Object
function TsPage(xml) {
    var page = jQuery(xml).find("Page");
    this.id = page.attr("Id");
    this.ordinal = page.attr("Ordinal");
    this.name = page.attr("Name");
    this.code = page.attr("Code");
    this.parentId = page.attr("ParentId");
    this.children = page.attr("Children");
    this.plugin = page.attr("Plugin");
    this.extra1 = page.attr("Extra1");
    this.extra2 = page.attr("Extra2");
    this.extra3 = page.attr("Extra3");
    this.extra4 = page.attr("Extra4");
    this.extra5 = page.attr("Extra5");
    this.extra6 = page.attr("Extra6");
    this.extra7 = page.attr("Extra7");
    this.extra8 = page.attr("Extra8");
    this.extra9 = page.attr("Extra9");
    this.extra10 = page.attr("Extra10");
}

// Travel Suite Offer Object
function TsOffer(xml) {
    this.positionId = jQuery(xml).attr("Id");
    this.code = jQuery(xml).attr("Code");
    this.positionOrdinal = jQuery(xml).attr("Ordinal");
    this.positionName = jQuery(xml).attr("Name");
    this.positionParentId = jQuery(xml).attr("ParentId");
    this.positionExtra1 = jQuery(xml).attr("Extra1");
    this.positionExtra2 = jQuery(xml).attr("Extra2");
    this.positionExtra3 = jQuery(xml).attr("Extra3");
    this.positionExtra4 = jQuery(xml).attr("Extra4");
    this.updated = jQuery(xml).find("Updated").text();
    this.updatedUnix = jQuery(xml).find("Updated").attr("Unix");
    this.type = jQuery(xml).find("Type").text();
    this.typeLinkLevel = jQuery(xml).find("Type").attr("LinkLevel");
    this.headline = jQuery(xml).find("Headline").text();
    this.picture = jQuery(xml).find("Picture").text();
    this.text = jQuery(xml).find("Text").text();
    this.textline1 = jQuery(xml).find("Text-Line[number='1']").text();
    this.textline2 = jQuery(xml).find("Text-Line[number='2']").text();
    this.textline3 = jQuery(xml).find("Text-Line[number='3']").text();
    this.textline4 = jQuery(xml).find("Text-Line[number='4']").text();
    this.link = jQuery(xml).find("Link").attr("HRef");
    this.linkText = jQuery(xml).find("Link").text();
    this.duration = jQuery(xml).find("Duration").text();
    this.durationMeasure = jQuery(xml).find("Duration").attr("Measure");
    this.price = jQuery(xml).find("Price").text();
    this.priceCurrency = jQuery(xml).find("Price").attr("Currency");
    this.priceSuffix = jQuery(xml).find("Price").attr("Suffix");
    this.pack = new TsPackage(xml);
    this.destination = new TsDestination(xml);
    this.hotel = new TsHotel(xml);
    this.extras = jQuery(xml).find("Extra");
    this.extra1 = this.extras.find("Extra[number='1']").text();
    this.extra2 = this.extras.find("Extra[number='2']").text();
    this.extra3 = this.extras.find("Extra[number='3']").text();
    this.extra4 = this.extras.find("Extra[number='4']").text();
        
    // liefert den Inhalt der Variable travelsuite_page_link_suffix, falls vorhanden
    this.getPageLinkSuffix = function()
    {
        var result = "";
        if (typeof travelsuite_page_link_suffix != "undefined") 
            result = travelsuite_page_link_suffix;
        return result;
    }

    // Beim Preis muss ohne PopUp auf das Angebot verlinkt werden.
    this.getLinkPackage = function() {
        return this.link + this.getPageLinkSuffix();
    };
    
    this.getPrice = function(){
        if (this.price.length > 3){
            var price = "";
            price =  price.concat(this.price.substring(1, this.price.length - 4), "." ,this.price.substring(this.price.length - 3, this.price.length));
            return price;
        }
        return this.price;}
    
    this.getPicture = function() {
        if (this.picture.indexOf("://") > 0)
            return this.picture;    
        return travelsuite_pic_path + this.picture;    
    }
}

// Travel Suite Package Object
function TsPackage(xml) {
    var n = jQuery(xml).find("Package");
    this.departureDate = n.find("DepartureDate").text();
    this.departureDateFormat = n.find("DepartureDate").attr("Format");
    this.departureAirport = n.find("DepartureAirport").text();
    this.departureAirportAcronym = n.find("DepartureAirport").attr("Acronym");
    this.tourOperator = n.find("TourOperator").text();
    this.tourOperatorAcronym = n.find("TourOperator").attr("Acronym");
    this.mealPlan = n.find("MealPlan").text();
    this.mealPlanAcronym = n.find("MealPlan").attr("Acronym");
    this.roomType = n.find("RoomType").text();
    this.roomTypeAcronym = n.find("RoomType").attr("Acronym");
    this.roomTypeText = n.find("RoomType").attr("Text");
}

// Travel Suite Destination Object
function TsDestination(xml) {
    var n = jQuery(xml).find("Destination");
    this.country = n.find("Country").text();
    this.region = n.find("Region").text();
    this.location = n.find("Location").text();
    this.airport = n.find("Airport").text();
    this.airportAcronym = n.find("Airport").attr("Acronym");
}

// Travel Suite Hotel Object
function TsHotel(xml) {
    var n = jQuery(xml).find("Hotel");
    this.name = n.find("Name").text();
    this.hotelClass = n.find("Class").text();
    
    // Liefert das Image-Tag zur Darstellung der Sterne
    this.getClassImageTag = function() {
        //nur bei mehr als 0 Sternen etwas zurückgebeben
        if(this.hotelClass.charAt(0) != 0){
            // halbe Sterne
            if (this.hotelClass.charAt(2) == 5 && this.hotelClass.charAt(0) > 1 ){
                return "<img src=\"http://www.tui.com/pics/wot/de_DE/icons/" + this.hotelClass.charAt(0) + "plus_sterne.gif\">";
            }
            // 1 Stern
            if (this.hotelClass.charAt(0) == 1){
                if (this.hotelClass.charAt(2) == 5){
                    return "<img src=\"http://www.tui.com/pics/wot/de_DE/icons/1plus_stern.gif\">";
                }
                else{return "<img src=\"http://www.tui.com/pics/wot/de_DE/icons/1_stern.gif\">";}
            }
            return "<img src=\"http://www.tui.com/pics/wot/de_DE/icons/" + this.hotelClass.charAt(0) + "_sterne.gif\">";
        }
        return "<!-- 0 Sterne -->";
    };

}
function get12flyStars(text) {
    var noStars = String(text.match(/\*/g)).split(',').length;
    var result = text;
    if (result.indexOf("*+") > 0) result = result.replace(/\*+\+*/g, noStars + '*+');
    else result = result.replace(/\*+\+*/g, noStars + '*');
    return result;
}

function getTuiClassTag(text, white) {
    var result = text;
    if(!white){
        if (text.indexOf("*") > -1)
            result = text.replace(/\*/g, "<img src=\"http://www.tui.com/fileadmin/tui/pixell/pics/icons/star.png\" alt=\"\" height=\"10\" width=\"10\" border=\"0\" />");
        
        if (result.indexOf("+") > -1)
            result = result.replace(/\+/g, "<img src=\"http://www.tui.com/fileadmin/tui/pixell/pics/icons/halfstar.png\" alt=\"\" height=\"10\" width=\"10\" border=\"0\" />");
    }
    else{
        if (text.indexOf("*") > -1)
            result = text.replace(/\*/g, "<img src=\"fileadmin/tui/pixell/pics/icons/starwhite.png\" alt=\"\" height=\"10\" width=\"10\" border=\"0\" />");
        
        if (result.indexOf("+") > -1)
            result = result.replace(/\+/g, "<img src=\"fileadmin/tui/pixell/pics/icons/halfstarwhite.png\" alt=\"\" height=\"10\" width=\"10\" border=\"0\" />");
    }
    
    return result;
}
