/*
  Copyright (C) 2001 - 2011 Astro Share, The Netherlands.
  All rights reserved.

  This file is part of the Astro-Online Software as defined in the License
  Agreement on www.Astro-Share.com.

  Astro-Online is distributed with NO WARRANTY OF ANY KIND.  No author or
  distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.

  The License Agreement grants you the right to copy and modify the Astro-Online
  source code and documentation, but only under certain conditions described in
  the License Agreement. You ARE ALLOWED to copy the content delivered with the
  source code and documentation. You are NOT ALLOWED to modify the content or use
  the content in other services without the written permission of Astro Share.
  Among other things, the License Agreement requires that the copyright notices
  and this notice be preserved on all copies.

  Author: Peter Schulz
*/

google.load("yui", "2.8.0r4");
google.setOnLoadCallback(AO_YuiInit);

var signText      = new Array("Ram 21/3-19/4", "Stier 20/4-20/5", "Tweeling 21/5-20/6", "Kreeft 21/6-22/7", "Leeuw 23/7-22/8", "Maagd 23/8-22/9", "Weegschaal 23/9-22/10", "Schorpioen 23/10-21/11", "Boogschutter 22/11-21/12", "Steenbok 22/12-19/1", "Waterman 20/1-19/2", "Vissen 20/2-20/3");
var monthText     = new Array("Januari", "Februari", "Maart", "April", "Mei", "Juni", "Juli", "Augustus", "September", "Oktober", "November", "December");
var dailyText     = "Daghoroscoop";
var weeklyText    = "Weekhoroscoop";
var monthlyText   = "Maandhoroscoop";
var weekText      = "week";

var signTextEN    = new Array("Aries 21/3-19/4", "Taurus 20/4-20/5", "Gemini 21/5-20/6", "Cancer 21/6-22/7", "Leo 23/7-22/8", "Virgo 23/8-22/9", "Libra 23/9-22/10", "Scorpio 23/10-21/11", "Sagittarius 22/11-21/12", "Capricorn 22/12-19/1", "Aquarius 20/1-19/2", "Pisces 20/2-20/3");
var monthTextEN   = new Array("January","February","March","April","May","June","July","August","September","October","November","December");
var dailyTextEN   = "Daily horoscope";
var weeklyTextEN  = "Weekly horoscope";
var monthlyTextEN = "Monthly horoscope";
var weekTextEN    = "week";

var signTextDE    = new Array("Widder 21/3-19/4", "Stier 20/4-20/5", "Zwilling 21/5-20/6", "Krebst 21/6-22/7", "Löwe 23/7-22/8", "Jungfrau 23/8-22/9", "Waage 23/9-22/10", "Skorpion 23/10-21/11", "Schütze 22/11-21/12", "Steinbock 22/12-19/1", "Wassermann 20/1-19/2", "Fische 20/2-20/3");
var monthTextDE   = new Array("Januar","Februar","Marz","April","Mai","Juni","July","August","September","Oktober","November","Dezember");
var dailyTextDE   = "Tageshoroskop";
var weeklyTextDE  = "Wochenhoroskop";
var monthlyTextDE = "Monatshoroskop";
var weekTextDE    = "Woche";

function AO_YuiInit() {
    var AO_Loader = new YAHOO.util.YUILoader({
         base: "http://ajax.googleapis.com/ajax/libs/yui/2.8.0r4/build/"
        ,require: ["event","connection","datasource","utilities","datatable"]
        ,loadOptional: true
        ,combine: false
        ,filter: "MIN"
        ,allowRollup: true
        ,onSuccess: function() {
            YAHOO.util.Get.css("/fileadmin/astroshare.com/widgets/horoscopeDatatableExpanded.css");
            var AO_Widgets = [
                "/fileadmin/astroshare.com/widgets/assets/rowexpansion.js"
            ];
            YAHOO.util.Get.script(AO_Widgets,
            {
                onSuccess: function(osuccess) {
                    var aoRequest = YAHOO.util.Connect.asyncRequest("GET", aoUrl, { success:successHandler, failure:failureHandler } );
                },
                onFailure: function(ofailure) {
                }
            }
            );
        }
        ,onFailure: function() {
            // alert("YUI library could not be loaded! Please try again later...");
        }
    });
    AO_Loader.insert();
}

function getAttributeFromXML(xml, att) {
    try {
        switch (aoHoroscopeType) {
            case "W":
                return xml.getElementsByTagName("AstroShareWeeklyHoroscope")[0].getAttribute(att); 
            case "M":
                return xml.getElementsByTagName("AstroShareMonthlyHoroscope")[0].getAttribute(att); 
            default: // daily horoscopes is the default
                return xml.getElementsByTagName("AstroShareDailyHoroscope")[0].getAttribute(att); 
        }
    } catch(err){
        return "";
    }
}

function successHandler(e) {
    try {
        // (re)set language if applicable (default is NL)
        switch (aoHoroscopeLang) {
            case "EN":
                 signText    = signTextEN;
                 monthText   = monthTextEN;
                 dailyText   = dailyTextEN;
                 weeklyText  = weeklyTextEN;
                 monthlyText = monthlyTextEN;
                 weekText    = weekTextEN;
                 break;
            case "DE":
                 signText    = signTextDE;
                 monthText   = monthTextDE;
                 dailyText   = dailyTextDE;
                 weeklyText  = weeklyTextDE;
                 monthlyText = monthlyTextDE;
                 weekText    = weekTextDE;
                 break;
        }
    } catch(err){
    }

    var xml = e.responseXML;

    var year = getAttributeFromXML(xml, "year");
    var month = getAttributeFromXML(xml, "month");
    var week = getAttributeFromXML(xml, "week");
    var day = getAttributeFromXML(xml, "day");
    try {
        month = monthText[parseInt(month)-1];
    } catch(err){
    }

    dsRoot = new YAHOO.util.DataSource(xml);

    ds = new YAHOO.util.DataSource(xml);
    ds.responseType = YAHOO.util.DataSource.TYPE_XML;
    ds.responseSchema = {
            resultNode: "sign",
            fields: ["id","txt"]
        };

    var expansionTemplate = function(state) {
        state.liner_element.innerHTML =
        '<div class="horoscope"> \
             <img src="http://www.astro-share.com/fileadmin/astroshare.com/public/duo_' + (parseInt(state.data.getId().substring(7))+1) + '.gif" border="0" align="right"/>' + state.data.getData("txt") + ' \
         </div>';
    }

    var dt = new YAHOO.widget.RowExpansionDataTable(
        "AO_ContainerRight",
        [
            {
                key:"id",
                label:""
            }
        ],
        ds,
            {
                rowExpansionTemplate:expansionTemplate
            }
        );

    dt.subscribe("cellClickEvent", dt.onEventToggleRowExpansion);
    dt.subscribe("rowClickEvent", dt.onEventSelectRow);

    switch (aoHoroscopeType) {
        case "W":
            YAHOO.util.Dom.get("AO_Title").innerHTML = "<h1>" + weeklyText + " " + weekText + " " + week + " " + year + "</h1>";
            break;
        case "M":
            YAHOO.util.Dom.get("AO_Title").innerHTML = "<h1>" + monthlyText + " " + month + " " + year + "</h1>";
            break;
        default: // daily horoscopes is the default
            YAHOO.util.Dom.get("AO_Title").innerHTML = "<h1>" + dailyText + " " + day + " " + month + " " + year + "</h1>";
    }
    for (var i=0; i<dt.getRecordSet().getLength(); i++) {
        dt.updateCell(dt.getRecord(i), dt.getColumn(0), "<b><nobr>" + signText[i] + "</nobr></b>");
    }
}

function failureHandler(e) {
    // alert("Could not load XML! Please try again later...");
}

