﻿
$j = $;

function ajaxProxyAsync(url, type, fn) {
    $.get("ueeeusearch.axd", "action=Ajax&url=" + encodeURIComponent(url), fn, type || "xml");
}

function speaking(words) {
    if (words == null || words == "")
        return;

    var thisObj = this;
    $.post("ueeeusearch.axd", "action=GetVoiceUrl&format=json&words=" + words, function(data) {
        $("#speak_flashControl").remove();
        $('<embed flashVars="filename=' + data.url + '" style="margin-left: 80px; float: left" height="100" width="100" allowscriptaccess="always" loop="false" menu="false" wmode="transparent" quality="high" id="speak_flashControl" src="pics/cartoon.swf" type="application/x-shockwave-flash"/>')
            .appendTo("#searchResultPanel");
    }, "json");
}

function getRemote(url, charset, fn) {

    $.get("ueeeusearch.axd?action=GetHtmlParse" +
        "&url=" + encodeURIComponent(url) +
        "&encoding=" + charset +
        "&type=" + "*" +
        "&lang=" + I18N.langCode, function(data) {
            var content = $("HtmlBag", data).text();
            fn(content);
        });
}         

function getGeocode(address, fn) {
    if (typeof GClientGeocoder == "undefined") {
        $.getJSON("http://" + (I18N.lang == "zh-CN" ? "ditu" : "maps") + ".google.com/maps/geo",
            "q=" + encodeURIComponent(address) + "&output=json&key=" + Settings.GoogleMapKey.getLocale() + "&callback=?", fn);
    } else {
        window.geo = window.geo || new GClientGeocoder();
        geo.getLocations(address, fn);
    }
}

function getDomain(url) {
    var results = url.match(/http:\/\/([^\/]+)/im);
    return results != null && results.length > 1 ? results[1] : url;
}

function numberToLength(number, length, prefixChar) {
    //debugger;
    if (number != null && typeof (number) == "number") {
        if (number.toString().length < length) {
            prefixChar = prefixChar || "0";
            var prefix = "";
            for (var i = 0; i < length - number.toString().length; i++)
                prefix += prefixChar;
            return prefix + number;
        }
    }
    return number.toString();
}

function waitTrigger(obj, fn) {
    for (var i = 0; i < obj.length; i++) {
        eval("var isObj = typeof(" + obj[i] + ")");
        eval("var refObj = " + obj[i] + "");
        if (isObj == "undefined" || refObj == null) {
            setTimeout(function() { waitTrigger(obj, fn) }, 500);
            return;
        }
    }
    fn(obj);
}

function consoleLog(msg) {
    if (window.console && console.log) {
        console.log(msg);
    }
}

function obj2str(o) {
    var r = [];
    if (typeof o == "string") return "\"" + o.replace(/([\'\"\\])/g, "\\$1").replace(/(\n)/g, "\\n").replace(/(\r)/g, "\\r").replace(/(\t)/g, "\\t") + "\"";
    if (typeof o == "undefined") return "undefined";
    if (typeof o == "object") {
        if (o === null) return "null";
        else if (!o.sort) {
            for (var i in o)
                r.push(i + ":" + obj2str(o[i]))
            r = "{" + r.join() + "}"
        } else {
            for (var i = 0; i < o.length; i++)
                r.push(obj2str(o[i]))
            r = "[" + r.join() + "]"
        }
        return r;
    }
    return o.toString();
}

function htmlCut(desc, count) {
    var text = "";
    count = count || 80;
    if (desc == "") {
        text = desc;
    }
    else {
        if (desc.length > count)
            text = desc.substr(0, count) + " ...";
        else
            text = desc;

        if (text.indexOf("<") != -1 && text.indexOf(">") != -1)
            text = $("<span>" + text + "</span>").text();

        text = jQuery.trim(text);

    }
    return text;
}

function translate(jqueryExp, lang) {
    if ($ && $.translate)
        $(jqueryExp).translate(lang || I18N.specificLang);
    else
        waitTrigger(["$", "$.translate"], function() {
            $(jqueryExp).translate(lang || I18N.specificLang);
        });
}

function createWindow(url, width, height, title, scrollbar, win, isModal) {
    width = width || 600;
    height = height || 400;
    var left = (window.screen.width - width) / 2;
    var top = (window.screen.height - height) / 2;
    var scrollbar = scrollbar || "no";
    window.openUrl = url;
    if (!isModal)
        var winPopup = window.open(url, win, "scrollbars=" + scrollbar + ",channelmode=no,location=no,menubar=no,resizable=yes,status=no,titlebar=no,directories=no,fullscreen=no,toolbar=no,left=" + left + ",top=" + top + ",width=" + width + ",height=" + height);
    else
        var winPopup = window.showModelessDialog(url, window, "status:no;center:yes;scroll:" + scrollbar + ";unadorned:no; dialogWidth:" + width + "px;dialogHeight:" + height + "px");
    try {
        winPopup.document.title = title || "New window";
    } catch (e) { };
    winPopup.focus();
    return winPopup;
}

var Settings = {
    "GoogleMapKey": {
        "www.uuhello.cn": "ABQIAAAA1KRxUFhLFpGYq0udupa2vhSRQOZn_4TvfMr4xuk9L8GZ7H8VkBSDAqCA_MmWNRgSOM6CVfnaAp6XTg",
        "www.uuhello.com": "ABQIAAAA1KRxUFhLFpGYq0udupa2vhQhmmeOqWnKgdi0LP0gRfcy9_GxoBTB6MYT8OplDB_o1SxGwm3MC2iSig",
        "uuhello.hellouu.com": "ABQIAAAA1KRxUFhLFpGYq0udupa2vhR3cvvvNGwt37O4hGk8su43IaUNUhShilDoQpkjOrFvM8AxCQkth1LBQw",
    	"iframe.uuhello.cn": "ABQIAAAA1KRxUFhLFpGYq0udupa2vhSfaJYRro7Fg4TKnftGZEGWCPQYsxSHvD3bw4mBiJC99T1ULpV40Oh-ng",
        getLocale: function() {
            return Settings.GoogleMapKey[location.hostname.toLowerCase()];
        }
    },
    /*"GoogleMapKey": "ABQIAAAAW8zqlZ-pOGnKY_1qNa2DRBT214HkMH7g6PNWhFH5hGzKVKRATxTvCb2YShaAZFK9HG_yb5FAu2xyKw"   my.ueeeu.com     */
    "SubSiteDomain_MyUeeeu": "my.uuhello.cn"
};

// extend utilitys
(function($) {
    window.addLink = function(url) {
        if (jQuery.browser.msie) {
            //var styleSheet = new styleSheet();
            //document.styleSheets[0].addImport(url);
            document.createStyleSheet(url);
        }
        else
            $(document.createElement('link')).attr({
                "id": "style_slideshow",
                "rel": "stylesheet",
                "type": "text/css",
                "href": url,
                "media": "screen"
            }).appendTo(document.getElementsByTagName('head')[0]);
    };

    window.addScript = function(url, fn) {
        // instance a new DOM element
        var element = document.createElement("script");
        element.src = url;
        element.type = 'text/javascript';
        element.language = 'javascript';

        // set up onload handler
        var me = this;
        if (jQuery.browser.msie) {
            element.onreadystatechange = function() {
                var state = this.readyState;
                if (state == "loaded" || state == "interactive" || state == "complete") {
                    (fn || function() { })();
                }
            };
        } else {
            element.onload = function() {
                (fn || function() { })();
            };
        }
        // bind the element to the browser DOM to begin loading the resource
        document.getElementsByTagName("head")[0].appendChild(element);

        //if (jQuery.browser.msie) {
        //$("head").append("<script type='text/javascript' src='" + url + "'></script>");
        //} else {
        //    $.getScript(url);
        //}
    };
})(jQuery);