
function nodeClick(nid, fromSide) {
    
    new Request.JSON({
        method: "get",
        url: "pages/req/reqGetNodeData.php",
        data: { 'nid':nid },
        onRequest: function (){
            loading(true);
        },
        onComplete: function (res){
            // check for cont area 1. full main , 2.entry area
            // check for existing page, url
            // reset submenu or not
            menuClick(nid, res[0].caid, fromSide, res[0].url, res[0].title);
        }
    }).send();

}

function menuClick(nid, caid, fromSide, url, title) {
    // set path
    setPath(nid);
    
    // is main
    if (caid == "1") {
        // show side menu
        $("sideMenuArea").setStyle('display','none');
        
        // show curTitle
        $("curTitle").setStyle('display','none');
        
        // expand entry
        $("entryArea").setStyle('width','100%');

        // set padding to entry area
        $('entryArea').setStyle('padding', '0px');
        // clear entry area
        $('entryArea').innerHTML = "";
    }
    // entry
    else if (caid == "2") {
        // show side menu
        $("sideMenuArea").setStyle('display','block');
        
        // show cur title
        $("curTitle").setStyle('display','block');

        // expand entry
        $("entryArea").setStyle('width','680px');

        // set padding to entry area
        $('entryArea').setStyle('padding', '0px 5px 5px 5px');

        if (!fromSide) {
            // build side menu
            new Request.HTML({
                method: 'get',
                url: "pages/getSideMenu.php",
                data: {'cid' : nid },
                update: 'sideMenuArea',
                onRequest: function () {
                    loading(true);
                },
                onComplete: function (res) {
                    loading(false);
                    if (!fromSide) {
                        submenu_init();
                    }
                }
            }).send();
        }
    }

    // check url
    if (url == null || url == "") {
        new Request.JSON({
            method: 'get',
            url: "pages/getEntry.php",
            data: {'cid' : nid },
            onRequest: function () {
                loading(true);
            },
            onComplete: function (res) {
                loading(false);
                $('entryArea').innerHTML = res[0].content;
                if (!fromSide) {
                    // set cur node title
                    $('curTitle').set('text', title);

                    submenu_init();
                }
            }
        }).send();
    }
    else {
        new Request.HTML({
            method: 'get',
            url: url,
            data: {'cid' : nid },
            update: 'entryArea',
            onRequest: function () {
                loading(true);
            },
            onComplete: function (res) {
                loading(false);
                if (!fromSide) {
                    // set cur node title
                    $('curTitle').set('text', title);

                    submenu_init();
                }
            }
        }).send();
    }
}
function setPath(nid) {
    new Request({
        method: "get",
        url: "pages/getPaths.php",
        data: {'nid':nid},
        onComplete: function (res) {

            var paths = res.split(';');

            var html = "";
            for (var i=paths.length-1; i>0; i--) {
                // get id
                titleNid = paths[i].split(':', 2);
                title = titleNid[0];
                id = titleNid[1];
                if (i == 1) html += "<div class='curPath' id='"+ id +"' >"+ title +"</div>";
                else html += "<div class='path' id='"+ id +"' >"+ title +"</div>";
            }
            $('pathCont').innerHTML = html;

            $$('.curPath, .path').each(function (path, id) {
                path.addEvent('click', function (e) {
                    e.stop();
                    
                    nodeClick(this.id, false);
                });
            });
        }
    }).send();
}

// TAB CLASS
function Tab(contId, styleId) {
    this.tabId = 0;
    // get cont
    this.container = $(contId);
    //this.container.set('class','tab_cont');

    // tabs
    this.tabs = [];

    // inject tab container
    this.tabCont = new Element('div', {id: 'tabCont'});
    this.tabCont.inject(this.container);
    this.tabCont.set('class','tab_tabCont'+ styleId);
    
    // inject body container
    this.bodyCont = new Element('div', {id: 'bodyCont'});
    this.bodyCont.set('class','tab_bodyCont'+ styleId);
    this.bodyCont.inject(this.container);
    // init
    this.curTab;
    this.add = function (title) {
        // inject tab
        
        var tab = new Element('div', {'id': 'tab_'+ title});
        tab.obj = this;
        tab.title = title;

        this.tabs.push(tab);
        
        // request get tab titles
        new Request({
            url : "pages/req/reqGetName.php",
            data : {'name': title},
            onComplete : function (res) {
                // set title
                tab.set('text', res);
            }
        }).send();
        
        // set class
        tab.set('class', 'tab_tab'+ styleId);
        // inject tab
        tab.inject(this.tabCont);
        // add event
        tab.addEvent('mouseover', function (e) {
            if (e) e.stop();

            // set body
            this.obj.bodyCont.innerHTML = this.body;
            
            // remove previous tab class
            if (this.obj.curTab) {
                this.obj.curTab.set('class', 'tab_tab'+ styleId);
            }
            // set current tab
            this.set('class', 'tab_curTab'+ styleId);
            this.obj.curTab = this;
            
            // set news title onclick
            $$('.tab_newsTitle a').each(function (a, id) {
                // alert(a);
                a.addEvent('click', function (e) {
                    e.stop();
                    
                    newsClick(this.id);
                });
            });
        });
        
        return tab;
    }
    this.addNews = function (nid, title, content) {
        // get image from html
        var rxGetImgTag = /<img[^>]*>/gi;
        var rxGetSrc = /src="[^"]*"/i;
        
        // first inject html to temp div
        $('tempDiv').innerHTML = content;

        var row = 5;

        if ($$('#tempDiv img')[0]) {
            var imgWidth = $$('#tempDiv img')[0].width;
            var imgHeight = $$('#tempDiv img')[0].height;

            // get img src
            var src = content.match(rxGetSrc)[0].substr(4);
            // clear image
            content = content.replace(rxGetImgTag, "");

            // and resize image
            var ratio = imgHeight / imgWidth;
            // vertical image : max height 100px
            if (ratio > 1) {
                imgHeight = 100;
                imgWidth = imgHeight / ratio;
            }
            // horizontal image : max width 100px
            else {
                imgWidth = 100;
                imgHeight = imgWidth * ratio;
            }
            row = (imgHeight * 0.08);
        }
        // cut news body from image height

        var chars = row * 42;

        var length = Math.floor(row * 42);

        content = content.substr(0, length);
        content += " ...";
        
        var html = " <div class='tab_newsCont' > ";
        html += "       <div class='tab_newsImg' > ";
        html += "           <img src="+ src +" width='"+ imgWidth +"' height='"+ imgHeight +"' /> ";
        html += "       </div>";
        html += "       <div class='tab_newsSubCont' >";
        html += "           <div class='tab_newsTitle' id='"+ nid +"' ><a id='"+ nid +"' >"+ title.toUpperCase() +"</a></div> ";
        html += "           <br class='clearBoth' />";
        html += "           <div class='tab_newsBody' >"+ content +"</div> ";
        html += "       </div>";
        html += "   </div>";

        return html;
    }
    this.addNewsByType = function (typeId) {

        // add tab
        var tempTab = this.add(typeId);

        // add news by type
        new Request.JSON({
            method : 'get',
            url: "pages/req/reqGetNewsByType.php",
            data: {'type': typeId},
            onRequest: function (){
                //
            },
            onComplete: function (res){
                loading(false);

                if (!res) {
                    res = {};
                }
                
                var html = "";
                for (var i=0; i<res.length; i++) {
                    html += tab.addNews(res[i].nid, res[i].title, res[i].content);
                }
                tempTab.body = html;
            }
        }).send();
    }
    this.addNode = function (nid, title) {

        // add tab
        var tempTab = this.add(title);
        
        // add news by type
        new Request.JSON({
            method : 'get',
            url: "pages/getNode.php",
            data: { 'cid': nid },
            onRequest: function (){
                //
            },
            onComplete: function (res){
                loading(false);
                
                if (!res) {
                    res = {};
                }
                
                // check for url
                // no url, get content
                if (res[0].url == "") {
                    tempTab.body = res[0].content;
                }
                // have url, get html
                else {
                    new Request.HTML({
                        url: res[0].url,
                        update: "",
                        onRequest: function () {
                            loading(true);
                        },
                        onSuccess: function (responseTree, responseElements, responseHTML, responseJavaScript) {
                            tempTab.body = responseHTML;
                            loading(false);
                        }
                    }).send();
                }
            }
        }).send();
    }
}

function setLinks(nid, lid) {
    var icons = new Array('right_20x20.gif', 'star_20x20.gif', 'sign_20x20.gif', 'coin_20x20.gif');
    new Request.JSON({
        method: "get",
        url : "pages/getLinks.php",
        data : {'nid' : nid},
        onComplete : function (res) {
            if (!res) return;
            else {
                for (var i=0; i<res.length; i++) {
                    //$('entryArea').innerHTML += res[i].title +", "+ res[i].content;
                    $('entryArea').innerHTML += "<img src='images/icons/"+ icons[i] +"' width='20' height='20' />";
                }
            }
        }
    }).send();
}
function menuClickMini(nid, lid, url, fromSide) {
    // set path
    setPath(nid);

    // show side menu
    $("sideMenuArea").setStyle('display','block');

    // show cur title
    $("curTitle").setStyle('display','block');

    // expand entry
    $("entryArea").setStyle('width','680px');

    // set padding to entry area
    $('entryArea').setStyle('padding', '0px 5px');

    // build side menu
    if (!fromSide) {
        new Request.HTML({
            method: 'get',
            url: "pages/getSideMenu.php",
            data: {'cid' : nid , 'lid': lid },
            update: 'sideMenuArea',
            onRequest: function () {
                loading(true);
            },
            onComplete: function (res) {
                loading(false);
                
                submenu_init();
            }
        }).send();
    }
    // check url
    if (url == null) {
        new Request.JSON({
            method: 'get',
            url: "pages/getEntry.php",
            data: {'cid' : nid , 'lid': lid },
            onRequest: function () {
                loading(true);
            },
            onComplete: function (res) {
                loading(false);
                $('entryArea').innerHTML = res[0].content;
                if (!fromSide) {
                    submenu_init();
                }
                // insert child submenu links
                // setLinks(nid, lid);
                // set cur node title
                if (res[0].type == 3) {
                    $('curTitle').set('text', res[0].title);
                }
            }
        }).send();
    }
    else {
        new Request.HTML({
            method: 'get',
            url: url,
            data: {'cid' : nid , 'lid': lid },
            update: 'entryArea',
            onRequest: function () {
                loading(true);
            },
            onComplete: function (res) {
                loading(false);
                if (!fromSide) {
                    // set cur node title
                    $('curTitle').set('text', title);

                    submenu_init();
                }
            }
        }).send();
    }
}

function initSideNews(nid) {
    // show side menu
    $("sideMenuArea").setStyle('display','block');
    
    new Request.JSON({
        url: 'pages/req/reqGetNews.php',
        data : {'nid': nid},
        onRequest: function () {
            loading(true);
        },
        onComplete: function (res) {
            loading(false);
            
            if (res == null) res = {};
            
            var html = "";
            for (var i=0; i<res.length; i++) {
                html += "<div class='newsTitleLink' id='"+ res[i].id +"' >"+ res[i].title +"</div>";
            }
            $('sideMenuArea').innerHTML = html;

            // init behavior
            $$('.newsTitleLink').each(function (title, id) {
                title.addEvent('click', function (e) {
                    e.stop();

                    newsClick(this.id, 0);
                });
            });
        }
    }).send();

}

function newsClick(nid) {
    
    // show curTitle
    $("curTitle").setStyle('display','block');
    
    // set padding to entry area
    $('entryArea').setStyle('padding', '5px');

    // set cur node title
    new Request({
        url: "pages/req/reqGetName.php",
        data: {'name': 'news'},
        onComplete: function (res) {
            $('curTitle').set('text', res);
        }
    }).send();
    
    // build side news titles
    initSideNews(nid);
    
    // expand entry
    $("entryArea").setStyle('width','500px');
    
    // request get content data
    new Request.JSON({
        method: 'get',
        url: 'pages/getNews.php',
        data: {'cid' : nid },
        onRequest: function () {
            loading(true);
        },
        onComplete: function (res) {
            loading(false);

            if (!res) {
                res = {};
            }
            
            $('entryArea').innerHTML = res[0].content;
            
            //alert(res);
            //if (!fromSide) {
            //    submenu_init();
            //}
        }
    }).send();
}

function loading(show) {
    var el = $('loading');
    if (show) {
        el.morph({opacity:1});
        //el.fade('in');
    }
    else {
        el.morph.pass({opacity:0}, el).delay(100);
        //el.fade('out');
    }
}

window.addEvent('domready', function(){

    // center loading
    var load = $('loading');

    var docCor = document.getCoordinates();
    var loadCor = load.getCoordinates();

    var x = (docCor.width - loadCor.width)/2;
    var y = (docCor.height - loadCor.height)/2;

    load.setStyle('left',x);
    load.setStyle('top',y);

    loading(false);
});