/**
 * 初始化類別
 * 
 * @version v 1.9 2009/01/13 23:22
 * @author Sewii <sewii@sewii.com.tw>
 * @copyright Sewii Design Studio. (sewii.com.tw)
 */
var initialize = 
{
    libraries: 
    [
        /* jQuery core */
        '*jQuery/jquery-1.3.2.min.js',

        /* jQuery UI */
        '*jQuery/ui/themes/base/ui.all.css',
        '*jQuery/ui/ui.core.js',
        'jQuery/ui/ui.accordion.js',
        'jQuery/ui/ui.datepicker.js',
        'jQuery/ui/ui.datepicker-zh-TW.js',
        'jQuery/ui/ui.draggable.js',
        'jQuery/ui/ui.droppable.js',
        'jQuery/ui/ui.progressbar.js',
        'jQuery/ui/ui.resizable.js',
        'jQuery/ui/ui.selectable.js',
        'jQuery/ui/ui.slider.js',
        'jQuery/ui/ui.sortable.js',
        'jQuery/ui/ui.tabs.js',
        'jQuery/ui/ui.dialog.js',

        /* jQuery effects */
        '*jQuery/effects/effects.core.js',
        'jQuery/effects/effects.blind.js',
        'jQuery/effects/effects.bounce.js',
        'jQuery/effects/effects.clip.js',
        'jQuery/effects/effects.drop.js',
        'jQuery/effects/effects.explode.js',
        'jQuery/effects/effects.fold.js',
        'jQuery/effects/effects.highlight.js',
        '*jQuery/effects/effects.pulsate.js',
        'jQuery/effects/effects.scale.js',
        'jQuery/effects/effects.shake.js',
        'jQuery/effects/effects.slide.js',
        'jQuery/effects/effects.transfer.js',
        
        /* jQuery external */
        '*jQuery/external/bgiframe/jquery.bgiframe.js',

        /* jQuery plugins */
        '*jQuery/plugins/jquery.php.js',
        '*jQuery/plugins/jquery.sewii.js',
        '*jQuery/plugins/jquery.form.js',
        '*jQuery/plugins/jquery.validate/validate.js',
        '*jQuery/plugins/jquery.validate/validate.methods.js',
        '*jQuery/plugins/jquery.validate/validate.tw.js',
        '*jQuery/plugins/jquery.validate/validate.setDefaults.js',
        '*jQuery/plugins/jquery.pager/pager.js',
        'jQuery/plugins/jquery.pager/list/list.js',
        'jQuery/plugins/jquery.pager/list/list.css',
        'jQuery/plugins/jquery.pager/comboBox.js',
        'jQuery/plugins/jquery.pager/projects/newManvd.js',
        'jQuery/plugins/jquery.notify/notify.js',
        'jQuery/plugins/jquery.notify/notify.css',
        'jQuery/plugins/jquery.autocomplete/autocomplete.js',
        'jQuery/plugins/jquery.autocomplete/autocomplete.css',
        'jQuery/plugins/jquery.address.js',
        'jQuery/plugins/jquery.sexy-combo/sexy-combo.js',
        'jQuery/plugins/jquery.sexy-combo/sexy-combo.css',
        'jQuery/plugins/jquery.sexy-combo/skins/sexy/sexy.css',
        'jQuery/plugins/jquery.imagePreview/imagePreview.js',
        'jQuery/plugins/jquery.imagePreview/imagePreview.css',
        'jQuery/plugins/jquery.fancybox/fancybox.js',
        'jQuery/plugins/jquery.fancybox/fancybox.css',
        'jQuery/plugins/jquery.fancybox/jquery.easing.js',
        'jQuery/plugins/jquery.scrollTo.js',
        'jQuery/plugins/jquery.swfobject.js',
        'jQuery/plugins/jquery.md5.js',
        '*jQuery/plugins/jquery.galleryview/galleryview.css',
        '*jQuery/plugins/jquery.galleryview/galleryview-1.1.js',
        '*jQuery/plugins/jquery.galleryview/timers.js',
        '*jQuery/plugins/jquery.tools.js',

        /* others */
        'shared.js',
        'Shadowbox/shadowbox.css',
        'Shadowbox/shadowbox.js',
        'TinyMCE/tiny_mce.js',
        'swfobject.js',
    ],

    /*
     * 初始化
     */
    init: function ()
    {
        var baseURL = this.getBaseURL ();
        for (var i = 0; i <= this.libraries.length; i++) {
            if (!this.libraries[i]) continue;
            //只載入帶 * 符號的必要元件
            if (!this.libraries[i].match (/^\*/)) continue;
            var path = baseURL + '/' + this.libraries[i].replace (/^\*/, '');
            this.include (path);
        }
    },

    /*
     * 載入函式庫
     * @param string libs 函式庫名稱，使用, 符號區隔。
     * @param string domMode 使用 DOM 模式載入
     */
    load: function (libs, domMode)
    {
        var baseURL = this.getBaseURL ();
        var search = libs.split (/\s?,\s?/);
        for (keyword in search) {
            for (i in this.libraries) {
                if (this.libraries[i].match (search[keyword])) {
                    //跳過帶 * 符號的必要元件
                    //避免重複載入函式庫
                    if (this.libraries[i].match (/^\*/)) continue;
                    var path = baseURL + '/' + this.libraries[i];
                    this.include (path, domMode);
                    //加上 * 號表示已經載入過
                    this.libraries[i] = '*' + this.libraries[i];
                }
            }
        }
    },

    /*
     * 引用檔案
     * @param string path CSS 或 JavaScript 路徑
     * @param string domMode 使用 DOM 模式載入
     */
    include: function (path, domMode)
    {
        if (path.match (/\.js$/)) {
            this.getScript (path, domMode);
        } else if (path.match (/\.css$/)) {
            this.getStyleSheet (path, domMode);
        }
    },

    /*
     * 載入 Javascript
     * @param string path JavaScript 路徑
     * @param string domMode 使用 DOM 模式載入
     */
    getScript: function (path, domMode)
    {
        if (domMode) {
            var headElement = document.getElementsByTagName ('head')[0];
            if (headElement) {
                var scriptElement = document.createElement ('script');
                scriptElement.setAttribute('type', 'text/javascript');
                scriptElement.setAttribute('src', path);
                headElement.appendChild (scriptElement);
            }
        } else
            document.write ('<script type="text/javascript" src="' + path + '"></script>');
    },

    /*
     * 載入 CSS
     * @param string path CSS 路徑
     * @param string domMode 使用 DOM 模式載入
     */
    getStyleSheet: function (path, domMode)
    {
        if (domMode) {
            var headElement = document.getElementsByTagName ('head')[0];
            if (headElement) {
                var linkElement = document.createElement ('link');
                linkElement.setAttribute('type', 'text/css');
                linkElement.setAttribute('rel', 'stylesheet');
                linkElement.setAttribute('href', path);
                headElement.appendChild (linkElement);
            }
        } else 
            document.write ('<link type="text/css" rel="stylesheet" href="' + path + '" />');
    },

    /*
     * 等候 DOM 載入完成執行 callback
     * @param string fn 函數
     */
    waitingDomReady: function (fn) 
    {
        if (document && document.body && 
            document.getElementsByTagName && 
            document.getElementById) {
            fn();
            return;
        }
        var that = this;
        setTimeout (function () {
            that.waitingDomReady (fn);
        }, 10);
    },

    /*
     * 取得不包含檔案名稱的基礎路徑的絕對 URL
     * @return string
     */
    getBaseURL: function ()
    {
        var that = this, base;

        // 如果找到 base 標籤就放在 baseURL 的前面
        var baseElement = document.getElementsByTagName ('base');
        for (var i = 0; i < baseElement.length; i++) {
            var baseHref;
            if (baseHref = baseElement[i].href)
            {
                // 如果是絕對 URL: http://site.com or http://site.com:8008
                if (/^https?:\/\/[^\/]+$/.test(baseHref))
                    baseHref += '/';

                //只抓取路徑目錄名
                base = baseHref ? baseHref.match (/.*\//)[0] : '';
            }
        }

        function getBase (scriptElement) {
            //抓取這個 script 檔名，因為已經被 include 進來，所以路徑有效。
            if (scriptElement.src.match (/initialize\.js/)) {
                baseURL = scriptElement.src.substring(0, scriptElement.src.lastIndexOf('/'));
                //如果 script 是相對路徑而且有找到 base 標籤就把 href 加在 src 的前面
                //所以對非 IE 瀏覽器和 IE8 而言，這個 src 屬性將永遠都是絕對 URL 路徑
                //這個邏輯只會在舊版的 IE 瀏覽器上執行。
                if (base && baseURL.indexOf('://') == -1 && baseURL.indexOf('/') !== 0)
                    baseURL = base + baseURL;
                return baseURL;
            }
            return null;
        };

        var scriptElements;

        // 在整份 DOM 文件尋找 script 標籤
        scriptElements = document.getElementsByTagName('script');
        for (var i = 0; i < scriptElements.length; i++) {
            var baseURL = getBase(scriptElements[i]);
            if (baseURL)
                return baseURL;
        }

        // 在 head 裡尋找 script 標籤
        var headElement = document.getElementsByTagName('head')[0];
        if (headElement) {
            scriptElements = headElement.getElementsByTagName('script');
            for (var i = 0; i < scriptElements.length; i++) {
                var baseURL = getBase(scriptElements[i]);
                if (baseURL)
                    return baseURL;
            }
        }
        return null;
    }
}

initialize.init ();
