/*
 * GLOBAL VAIRABLES DEFINITION
 */
//var _g_service_data = null;

/*
 * MAIN FUNCTION, PROGRAM ENTRENCE
 */
document.observe('dom:loaded',function(){
    // INIT GLOBAL
    initGlobal();
    // EVERY PAGE MAY HAVE ONE INIT FUNCTION TO INIT PAGE DISPALY
    if(typeof initFrame != "undefined" && Object.isFunction(initFrame) ){
        initFrame();
    }
    if(typeof initPage != "undefined" && Object.isFunction(initPage) ){
        initPage();
    }
    if(typeof initPage2 != "undefined" && Object.isFunction(initPage2) ){
        initPage2();
    }
    if(typeof initPage3 != "undefined" && Object.isFunction(initPage3) ){
        initPage3();
    }
    if(typeof initPage4 != "undefined" && Object.isFunction(initPage4) ){
        initPage4();
    }
    if(typeof initPage5 != "undefined" && Object.isFunction(initPage5) ){
        initPage5();
    }
    // NOT USED ANYMORE
    if(typeof correctPNG != "undefined" && Object.isFunction(correctPNG) ){
        correctPNG();
    }
    
});

// This function is for stripping leading and trailing spaces
String.prototype.trim = function () {
    return this.replace(/^\s*(\S*(\s+\S+)*)\s*$/, "$1");
};
//This function is for stripping trailing spaces
String.prototype.Rtrim = function() {
    return this.replace(/([\s]*$)/g,"");
};

Object.extend(String.prototype, {
    templateDecode: function() {
        var decoded = this.replace(/#%7B([a-zA-Z0-9_-]+)%7D/gi, "#{$1}");
        return decoded;
    }
});

function initGlobal(){
// LOAD HEADER
//    HeaderFooter.setHeaderMenuSelection();
}

// PRE_DEFINED PAGE INIT FUNCTION FOR EVERY PAGE.
//function initPage(){
//	alert("initPage");
//	return null;
//}

/**
 * HEADER FOOTER FUNCTIONS
 */
var HeaderFooter = {
    // HIGLLIGHT MENU IF THE URL MATCHES
    setHeaderMenuSelection: function(){
        
        // GET CURRENT PAGE STRING FROM URL
        var currentPage = document.location.pathname;
        //        var lastIdx = currentPage.lastIndexOf('/')+1;

        //        currentPage = currentPage.substring(lastIdx).strip();
        currentPage = currentPage.strip();
        var subCurrentPage = currentPage;
        
        if(currentPage.include("network")){
            currentPage = "network";
            subCurrentPage = currentPage + "_null";
        }else if(currentPage.include("profile1") || currentPage.startsWith("upload") || currentPage.startsWith("link")){
            currentPage = "network";
            subCurrentPage = "profile1";
        }else if(currentPage.include("profile2")){
            currentPage = "network";
            subCurrentPage = "profile2";
        }else if(currentPage.include("profile.jsp") || currentPage.startsWith("slide_list_other")){
            currentPage = "network";
            subCurrentPage = currentPage + "_null";
        }else if(currentPage.include("inner_invite")){
            currentPage = "mail";
            subCurrentPage = currentPage + "_null";
        }else if(currentPage.include("invitation")){
            currentPage = "network";
            subCurrentPage = "invitation";
        //        }else if(currentPage.include("slide_list_pub") || currentPage.include("slide_list_search")){
        }else if(currentPage.include("slides")){
            currentPage = "slides";
            subCurrentPage = currentPage + "_null";
        //        }else if(currentPage.include("slide_list_mybookmark")){
        //            currentPage = "slide_list_pub";
        //            subCurrentPage = "slide_list_mybookmark";
        //        }else if(currentPage.include("slide_list_my.")){
        //            currentPage = "slide_list_pub";
        //            subCurrentPage = "slide_list_my.";
        //        }else if(currentPage.include("slide_upload")){
        //            currentPage = "slide_list_pub";
        //            subCurrentPage = "slide_upload";
        }else if(currentPage.include("groups")){
            currentPage = "groups";
            subCurrentPage = currentPage + "_null";
        }else if(currentPage.include("events")){
            currentPage = "events";
            subCurrentPage = currentPage + "_null";
        }
        
        // IF ROOT
        if(currentPage.length<2){
            currentPage = "/";
        }
        // GET MATCHED ELEMENT
        if($('menu') == null){
            return;
        }
        //        var aSelectedPage = $('frame_sidebar').select('dt');
        //        var subaSelectedPage = $('frame_sidebar').select('dd');

        var aSelectedPage = $("HeaderMenu1").select("li");
        var subaSelectedPage = $("HeaderMenu2").select("li");

        for(var i=0;i<aSelectedPage.size();i++){
            var tmp = aSelectedPage[i];
            
            if(tmp.innerHTML.include(currentPage)){
                // SET ID=CURRENT, WILL HIGH LIGHT MENU
                tmp.setAttribute('id', 'selected');
            }else{
                // REMOVE ALL OTHER ID, IF PRE-SET
                tmp.removeAttribute('id');
            }
        }
        
        for(i=0;i<subaSelectedPage.size();i++){
            var subTmp = subaSelectedPage[i];

            if(subTmp.innerHTML.include(subCurrentPage)){
                // SET ID=CURRENT, WILL HIGH LIGHT MENU
                subTmp.setAttribute('id', 'selected');
            }else{
                // REMOVE ALL OTHER ID, IF PRE-SET
                subTmp.removeAttribute('id');
            }
        }
    }
}


var BackGray = Class.create({
    initialize: function(){
        this.body = document.body;
        this.midDivId = "mid_div";
        this.body_width = (this.body.scrollLeft + this.body.scrollWidth > window.screen.availWidth) ? window.screen.availWidth : this.body.scrollLeft + this.body.scrollWidth;
        this.body_height = (this.body.scrollTop + this.body.scrollHeight < window.screen.availHeight) ? window.screen.availHeight : this.body.scrollTop + this.body.scrollHeight;
    },
    
    _createTopDiv : function(topDivId, template){
        var DivTop = document.createElement("div");
        with(DivTop.style)
        {
            display = "none";
            zIndex = 3;
            position = "absolute";
            border = "0px"; 
            }
        DivTop.id = topDivId;
        DivTop.innerHTML = template;
        this.body.appendChild(DivTop);
        
    },
    _createMidDiv : function(midDivId){
        var DivMid = document.createElement("div");
        
        with(DivMid.style)
        {
            display = "none";
            zIndex = 2;
            position = "absolute";
            top = 0;
            left = 0;
            if(location.href.include("home")){
                height = "150%";
            }else if(location.href.include("profile")){
                height = "120%";
            }else{
                height = "100%";
            }
            width = "100%";
            border = "0px";
            backgroundColor = "#000000";
        
            
            }
        DivMid.id = this.midDivId;
        this.body.appendChild(DivMid);
    },
    _center : function(element){
        //xinjun change
        //$(element).style.position ="absolute";
        $(element).style.top = "25%";
        $(element).style.left = "50%";
        //$(element).style.width = "50%";
        //$(element).style.height ="100%";
        if($(element) != null) {
    //            /*
    //            if(typeof window.innerHeight != 'undefined') {
    //                $(element).style.top =
    //                    Math.round(document.viewport.getScrollOffsets().top +
    //                    ((window.innerHeight - $(element).clientHeight) + 100)/2)+'px';
    //                $(element).style.left =
    //                    Math.round(document.viewport.getScrollOffsets().left +
    //                    ((window.innerWidth))/2)+'px';
    //            } else {
    //                $(element).style.top =
    //                    Math.round(document.body.scrollTop +
    //                    (($$('body')[0].clientHeight - $(element).clientHeight))/2)+'px';
    //                $(element).style.left =
    //                    Math.round(document.body.scrollLeft +
    //                    (($$('body')[0].clientWidth))/2)+'px';
    //            }//*/
    }
    },
    action : function(topDivId, template){
        var trans = 75;
        if(Prototype.Browser.IE)
            trans = 35;
        
        this._createTopDiv(topDivId, template);
        this._createMidDiv(this.midDivId);
   
        with(this.body.style){
            width = this.body_width;
            height = this.body_height;
            }

        with($(this.midDivId).style){
            display ="";
            height = this.body_height;
            width = "100%";
            
            if(Prototype.Browser.IE){
                filter = " Alpha(Opacity="+trans+")";
            }else{
                opacity = trans/250;
            }
            }
        with($(topDivId).style){
            display = "";
            }
        this._center(topDivId);
    },
    closeTopDiv : function(topDivId){
        with(this.body.style){
            overflow = "auto";
            }
        with($(this.midDivId).style){
            display = "none";
            }
                
        with($(topDivId).style){
            display = "none"; 
            }
    }
});

var Popup = Class.create({
    initialize : function(inputElement, tip){
        this._showPopup(inputElement, tip);
        this._hidePopup(inputElement);
    },
    _showPopup : function(inputElement, tip){
        inputElement.observe("focus", function(){
            if($(inputElement.id +"_span") != null){
                $(inputElement.id +"_span").remove();
            }
            //            var div = new Element("div", {"class" : "tishi"});
            //            var subDiv = new Element("div", {"class" : "tishibottom"});
            //            div.appendChild(subDiv);
            //            subDiv.insert({"after" : tip});
            //            var span = new Element('span', {"id" : inputElement.id + "_span", "style" : "position:absolute"});
            //            span.appendChild(div);
            //            inputElement.insert({"after" : span});
            var popHTML ='<div class="tishileft"></div>' + 
            '<div class="rounded">' +
            '<h2></h2>' +
            '<div class="main"></div>' +
            '<div class="footer"><p></p></div>' +
            '</div>';
            var div = new Element("span", {
                "id" : inputElement.id + "_span",
                "class" : "tishi"
            }).update(popHTML);
            var tipDiv = div.select('[class="main"]')[0];
            tipDiv.width = "500px";
            tipDiv.update(tip);
            inputElement.insert({
                "after" : div
            });
        })
    },
    _hidePopup : function(inputElement){
        inputElement.observe("blur", function(){
            if($(inputElement.id +"_span") != null){
                $(inputElement.id +"_span").remove();
            }
        }),
        inputElement.observe("keypress", function(){
            if($(inputElement.id +"_span") != null){
                $(inputElement.id +"_span").remove();
            }
        })
    }
});

var PopupForButton = Class.create({
    initialize : function(inputElement, tip){
        this._showPopup(inputElement, tip);
        this._hidePopup(inputElement);
    },
    _showPopup : function(inputElement, tip){
        inputElement.observe("mousemove", function(){
            if($(inputElement.id +"_span") != null){
                $(inputElement.id +"_span").remove();
            }
            var popHTML ='<div class="tishileft"></div>' +
            '<div class="rounded">' +
            '<h2></h2>' +
            '<div class="main"></div>' +
            '<div class="footer"><p></p></div>' +
            '</div>';
            var div = new Element("span", {
                "id" : inputElement.id + "_span",
                "class" : "tishi"
            }).update(popHTML);
            var tipDiv = div.select('[class="main"]')[0];
            tipDiv.width = "500px";
            tipDiv.update(tip);
            inputElement.insert({
                "after" : div
            });
        })
    },
    _hidePopup : function(inputElement){
        inputElement.observe("mouseout", function(){
            if($(inputElement.id +"_span") != null){
                $(inputElement.id +"_span").remove();
            }
        }),
        inputElement.observe("keypress", function(){
            if($(inputElement.id +"_span") != null){
                $(inputElement.id +"_span").remove();
            }
        })
    }
});

function correctPNG()
{
    if (Prototype.Browser.IE) {
        for(var i=0; i<document.images.length; i++)
        {
            var img = document.images[i];
            var imgName = img.src.toUpperCase();
            if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
            {
                var imgID = (img.id) ? "id='" + img.id + "' " : "";
                var imgClass = (img.className) ? "class='" + img.className + "' " : "";
                var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' ";
                var imgStyle = "display:inline-block;" + img.style.cssText;
                if (img.align == "left") imgStyle = "float:left;" + imgStyle;
                if (img.align == "right") imgStyle = "float:right;" + imgStyle;
                if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle;
                var strNewHTML = "<span "+ imgID + imgClass + imgTitle + " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";" + "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader" + "(src='" + img.src + "', sizingMethod='scale');\"></span>";
                img.outerHTML = strNewHTML;
                i = i-1;
            }
        }
    }
}

function isAllNumberStr(str){
    if(/^\d+$/.test(str)){
        return true;
    }else{
        return false;
    }
}

var Paging = {
    pagination: function(page){
        $("page_num").value = page;
        $("search_form").submit();
    }
}

function setBackground(actType, e){
    if(actType == "over"){
        e.style.background = "#F3F7FC";
    }else{
        e.style.background = "#FFFFFF";
    }
}

function compareTwoDateIsEqual(bDate,eDate){  
    if(bDate.getTime() == eDate.getTime()){
        return true;
    }else{
        return false;
    }
}

function focusHide(e,str){
    if(e.value.indexOf(str) != -1){
        e.value = "";
        e.style.color="#000";
    }
}

function focusShow(e,str){
    if(e.value == ""){
        e.style.color="#999";
        e.value = str;
    }
}