//
// Allgemeine Travel Suite Funktionen 
//
//
// Version: 03.12.2008
//
// pixell daten & design
//


var travelsuite_xml_path = "http://info.gtitravel.de/fileadmin/ts_upload/Live/Xml/";
var travelsuite_pic_path = "http://info.gtitravel.de/TSMedia/";

// onLoad
var blank = new Image();
blank.src = 'img/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(xml) {
	renderOffers(new TsOffers(parseXML(xml)));
}

function parseXML(xml) {
    if (window.ActiveXObject && window.GetObject) {
        var dom = new ActiveXObject('Microsoft.XMLDOM');
        dom.loadXML(xml);
        return dom;
    }
    if (window.DOMParser)
        return new DOMParser().parseFromString(xml, 'text/xml');
    throw new Error('No XML parser available');
}

// 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;
    }
    
    this.getByName = function(name) {
        var obc = new Array();
        for (var i = 0; i < offers.length; i++)
        {
            var regex = eval("/(" + name + ")/");
            if (this.offers[i].positionName != undefined)
            {
                var matched = this.offers[i].positionName.match(regex);
                if (matched)
                {
                    obc.push(this.offers[i]);
                }
            }
        }
        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();
        
    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();

    this.getStars = function() {
        var html = ""
        var stars = this.hotelClass
        var full = new Number(stars.charAt(0));
        var half = new Number(stars.charAt(2));
        for (var i = 1; i <= full; i++) {
            html = html + '*';
        }
        if (half == 5) html = html + '+';

        return html;
    }
    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_path + travelsuite_pic_fullStar;
            html += '" />';
        }
        if (halfStars > 0) {
            html += '<img border="0" src="';
            html += travelsuite_pic_path + 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 parseXML( xml ) {
    if( window.ActiveXObject && window.GetObject ) {
        var dom = new ActiveXObject( 'Microsoft.XMLDOM' );
        dom.loadXML( xml );
        return dom;
    }
    if( window.DOMParser )
        return new DOMParser().parseFromString( xml, 'text/xml' );
    throw new Error( 'No XML parser available' );
}

