// vim: sw=4:ts=4:nu:nospell

/**
 * HyperCities InfoPanel Control
 *
 * @author    Jih-Chung Fan, Chen-Kuei Lee
 * @copyright (c) 2008, by HyperCities Tech Team
 * @date      2008-12-29
 * @version   0.7
 *
 */

// Create Namespace
var GInfoPanel = function() {
    this._id       = "[GInfoPanel] ";
    this._panelDiv;
    this._city     = "";
    this._dateFrom = "";
    this._dateTo   = "";
    this._cityWidth = 0;
    this._dateWidth = 0;
};

// Prototyping GControl(printable?:Boolean, selectable?:Boolean)
GInfoPanel.prototype = new GControl(false, false);

// Prototyping getDefaultPosition(), return an object of type GControlPosition.
GInfoPanel.prototype.getDefaultPosition = function() {
    return new GControlPosition(G_ANCHOR_BOTTOM_LEFT, new GSize(0,0));
};

// Prototyping initialize(), return a DOM element.
GInfoPanel.prototype.initialize = function(map) {

    this._panelDiv = $(document.createElement("div"));
    this._panelDiv.attr("id", "GInfoPanel");

    var bgDiv = $(document.createElement("div"));
    bgDiv.attr("id", "GInfoBackground");

    var logoDiv = $(document.createElement("div"));
    logoDiv.attr("id", "GInfoLogo");
    logoDiv.html('<img src="./images/hyperCitiesLogo.png" ALT="HyperCities" />');

    var infoDiv = $(document.createElement("div"));
    infoDiv.attr("id", "GInfoWrapper");

    var placeDiv  = $(document.createElement("div"));
    placeDiv.attr("id", "GPlaceName");

    var dateFromDiv  = $(document.createElement("div"));
    dateFromDiv.attr("id", "GDateFrom");

    var dashDiv  = $(document.createElement("div"));
    dashDiv.attr("id", "GDash");
    dashDiv.html("-").hide();

    var dateToDiv  = $(document.createElement("div"));
    dateToDiv.attr("id", "GDateTo");

    infoDiv.append(placeDiv).append(dateFromDiv).append(dashDiv).append(dateToDiv);
    this._panelDiv.append(bgDiv).append(logoDiv).append(infoDiv);

    map.getContainer().appendChild(this._panelDiv.get(0));
    return this._panelDiv.get(0);
};

GInfoPanel.prototype.adjustLayout = function($show) {

    $("#GInfoWrapper").width(this._cityWidth + this._dateWidth + "px");

    var newLeft = ($("#GInfoBackground").width() - 250 - $("#GInfoWrapper").width()) / 2;

    $("#GInfoWrapper").css("left", Math.floor(newLeft) + 75 + "px");

    if ( $show && (this._dateWidth > 0) ) {
        $("#GInfoWrapper").fadeIn("slow");
    }
};

GInfoPanel.prototype.setCity = function($name) {

    if (typeof($name) !== "string")
        return;

    if ( $name === this._city )
        return;
    else
        this._city = $name;

//    HyperCities.debug(this._id + "Set City To " + $name);

    var nameSplitResult = $name.split(" ");
    var placeDiv = $("#GPlaceName").empty();
    var length   = nameSplitResult.length; 
    var charDiv;

    this.clearPanel();
    this._cityWidth = 0 ;

    for ( var i = length - 1 ; i >= 0 ; i--) {
        length = nameSplitResult[i].length - 1;
        if ( length >= 0 ) {
            charDiv = $(document.createElement("div"));
            charDiv.attr("class", "holder end");
            charDiv.html(nameSplitResult[i].charAt(length));
            this._cityWidth = this._cityWidth + 24;

            placeDiv.prepend(charDiv);
        }

        for( var j = length - 1 ; j >= 0 ; j-- ) {
            charDiv = $(document.createElement("div"));
            charDiv.attr("class", "holder");
            charDiv.html(nameSplitResult[i].charAt(j));
            this._cityWidth = this._cityWidth + 18;

            placeDiv.prepend(charDiv);
        }
    }

    this.adjustLayout(false);
}; 

GInfoPanel.prototype.setTimespan = function($startTime, $endTime) {
    
//    HyperCities.debug(this._id + "Set Time Span");
    if ((typeof($startTime) !== 'object') || (typeof($endTime) !== 'object'))
        return;

    //var dateFrom = ( $startTime == null ) ? "" : $startTime.toString("yyyy");
    var dateFrom = ( $startTime == null ) ? "" : $startTime.getFullYear().toString();
    //var dateTo   = ( $endTime == null )   ? "" : $endTime.toString("yyyy");
    var dateTo   = ( $endTime == null )   ? "" : $endTime.getFullYear().toString();

    if ( this._dateFrom == dateFrom && this._dateTo == dateTo && this._dateWidth != 0 ) {
        return;
    } else {
        this._dateFrom = dateFrom ;
        this._dateTo   = dateTo ;
    }

	//HyperCities.debug(this._id + dateFrom + " - " + dateTo);
    var startYear = dateFrom;
    var endYear   = dateTo;
    var dateFromDiv = $("#GDateFrom");
    var dateToDiv   = $("#GDateTo");
    var dashDiv     = $("#GDash");

    this.clearPanel();
    this._dateWidth = 0 ;

    if ( startYear.length > 0 && endYear.length > 0 ) {

        //add Padding Blank
        while ( startYear.length < 4 )
            startYear = " " + startYear;
        while ( endYear.length < 4 )
            endYear = " " + endYear;

        this._dateWidth = this._dateWidth + 18*4 + 6 ;
        for ( var i = startYear.length - 1 ; i >= 0 ; i-- ) {
            charDiv = $(document.createElement("div"));
            charDiv.attr("class", "holder");
            charDiv.html(startYear.charAt(i));
            dateFromDiv.prepend(charDiv);
        }

        if ( endYear !== startYear ) {

            dashDiv.show();
            this._dateWidth = this._dateWidth + 18*4 + 24;

            for ( var i = endYear.length - 1 ; i >= 0 ; i-- ) {
                charDiv = $(document.createElement("div"));
                charDiv.attr("class", "holder");
                charDiv.html(endYear.charAt(i));
                dateToDiv.prepend(charDiv);
            }
        }
    }

    this.adjustLayout(true);
};

GInfoPanel.prototype.clearPanel = function() {

    $("#GInfoWrapper").hide();
    $("#GDateFrom").empty();
    $("#GDateTo").empty();
    this._dateWidth = 0;
};
