﻿

function FinWeb()
{
	this.simpleVersionKey = ":8080/";
	this.traditionalVersionKey = "/";
	
	this.simpleImagesPath = "__gbk__img__images";
	this.traditionalImagesPath = "__big5_img__images";

	// for record the last changed time.
	this.lastVerstionTime = new Date();

	// to record the old time version
	this.oldImagePath = "";
	this.oldStylePath = "";
}

FinWeb.prototype.isBig5 = function()
{
	var url = window.location.href;

    var portPos = url.indexOf(this.simpleVersionKey, 0);

    if(portPos > 0 )
    {
		return(false);
    }
	
	return true;
}

// isFirst = true, it's the first time to invoke this function in the current circle
// .
FinWeb.prototype.getTimeVersion = function(isFirt) {
    var timeVersion = "day";

    if (isFirt) {
        this.lastVerstionTime = new Date();
    }

    var now = this.lastVerstionTime;
    var hour = now.getHours();
    var minute = now.getMinutes();

    if (hour >= 7 && hour < 17) {
        timeVersion = "day";
    }
    else if (hour >= 17 && hour < 18) {
        timeVersion = "night";
    }
    else if (hour >= 18 && hour < 24) {
        timeVersion = "midnight";
    }
    else if (hour >= 0 && hour < 5) {
        timeVersion = "midnight";
    }
    else if (hour >= 5 && hour < 7) {
        timeVersion = "night";
    }
    else {
        timeVersion = "day";
    }

    return timeVersion;
    //return "day";
}

///
FinWeb.prototype.loadImages = function() {
    var timePath = this.getTimeVersion(false);
    
    if (timePath == this.oldImagePath) {
        return ;
    }
    
    for (var i = 0; i < arguments.length; i++) {
        var obj = document.getElementById(arguments[i]);
        if (obj && typeof (obj) == "object") {
            if (obj.src) {
                // to change the src.
                var srcUrl = obj.src;
                var oldUrl = srcUrl;



                if (!(srcUrl.indexOf("/" + timePath + "/", 0) > 0)) {
                    srcUrl = srcUrl.replace("midnight", timePath);
                    if (!(srcUrl.indexOf("/" + timePath + "/", 0) > 0)) {
                        srcUrl = srcUrl.replace("night", timePath);

                        if (!(srcUrl.indexOf("/" + timePath + "/", 0) > 0)) {
                            srcUrl = srcUrl.replace("day", timePath);
                        }
                    }
                }

                ///
                if (oldUrl != srcUrl) {
                    obj.src = srcUrl;
                }
            }
        }
    }

    this.oldImagePath = timePath
}

/// to change the css of
FinWeb.prototype.changeCSS = function() {
    var timePath = this.getTimeVersion(false);
    if (timePath == this.oldStylePath) {
        return;
    }

    var timeSavePath = timePath;

    timePath = timePath + "_";
    var arrLinks = document.getElementsByTagName("link");

    document.getElementById("day_lnkCommon").disabled = false;

    for (var i = 0; i < arrLinks.length; i++) {
        if (arrLinks.item(i).rel == "stylesheet" && arrLinks.item(i).type == "text/css") {
            // this is for stylesheet.
            var linkID = arrLinks.item(i).id;
            if (linkID == "undefined") continue;
            
            if (linkID.substring(0, timePath.length) == timePath) {
                // we should enable this style.
                arrLinks.item(i).disabled = false;
            }
            else {
                arrLinks.item(i).disabled = true;
            }
        }
    }


this.oldStylePath = timeSavePath;
}

/// to change the css of
//FinWeb.prototype.loadCSS = function() {
//    var timePath = this.getTimeVersion(false);
//    if (timePath == this.oldStylePath) {
//        return;
//    }
//    
//    for (var i = 0; i < arguments.length; i++) {
//        var obj = document.getElementById(arguments[i]);
//        if (obj && typeof (obj) == "object") {
//            if (obj.href) {
//                // to change the src.
//                var srcUrl = obj.href;
//                var oldUrl = srcUrl;



//                if (!(srcUrl.indexOf("/" + timePath + "/", 0) > 0)) {
//                    srcUrl = srcUrl.replace("midnight", timePath);
//                    if (!(srcUrl.indexOf("/" + timePath + "/", 0) > 0)) {
//                        srcUrl = srcUrl.replace("night", timePath);

//                        if (!(srcUrl.indexOf("/" + timePath + "/", 0) > 0)) {
//                            srcUrl = srcUrl.replace("day", timePath);
//                        }
//                    }
//                }

//                ///
//                if (oldUrl != srcUrl) {
//                    obj.disabled = true;
//                    obj.href = srcUrl;
//                    obj.disabled = false;
//                }
//            }
//        }
//    }

//    this.oldStylePath = timePath;
//}

FinWeb.prototype.getImagesPath = function()
{
	if(this.isBig5())
	{
		return this.traditionalImagesPath;
	}
	else
	{
		return this.simpleImagesPath;
	}
}


var finWeb = new FinWeb();