﻿//
// Allgemeine Travel Suite Funktionen
//
// author: sascha.rose@pixell.de
// version: 02.12.2008
//

var travelsuite_xml_path = "/Pixell/";
var travelsuite_pic_path = "/Pixell/";
var travelsuite_pic_fullStar = "/Pixell/starblack.png";
var travelsuite_pic_halfStar = "/Pixell/halfstarblack.png";

// onLoad

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);
        }
    }
}



// Load Travel Suite XML-Data
function ts_loadData(code) {
    var url = travelsuite_xml_path + code + ".xml";

    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
        var retVal = '';
        if (this.hotelClass.charAt(0) != 0) {
            var stars = parseInt(this.hotelClass.charAt(0));
            var half = parseInt(this.hotelClass.charAt(2));
            for (var i = 0; i < stars; i++) {
                retVal = retVal + '<div class="pxStar"></div>';
            }
            if (half != 0) retVal = retVal + '<div class="pxHalfStar"></div>';
        }
        return retVal;
    };

    this.getHotelClass = function() {
        var stars = parseInt(this.hotelClass.substring(0, this.hotelClass.indexOf('.')));
        var halfStars = parseInt(this.hotelClass.substring(this.hotelClass.indexOf('.') + 1));
        var html = '';
        for (var i = 0; i < stars; i++) {
            html += '<img border="0" src="';
            html += travelsuite_pic_fullStar;
            html += '" />';
        }
        if (halfStars > 0) {
            html += '<img border="0" src="';
            html += travelsuite_pic_halfStar;
            html += '" />';
        }
        return html;
    }
    // Liefert das Image-Tag zur Darstellung der Sterne
    this.getCinemaStars = function() {
        var stars = parseInt(this.hotelClass.substring(0, this.hotelClass.indexOf('.')));
        var halfStars = parseInt(this.hotelClass.substring(this.hotelClass.indexOf('.') + 1))
        var html = '';
        for (var i = 0; i < stars; i++) {
            html += '<div class="pxStar"></div>';
        }
        if (halfStars > 0) {
            html += '<div class="pxHalfStar"></div>';
        }

        html += '<div class="pxClearer"></div>';

        return html;
    }

}
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;
}

