/**
* @version		1.4
* @package		SimpleTabs
* @author    Fotis Evangelou - http://nuevvo.com/labs/simpletabs
* @copyright	Copyright (c) 2009-2011 Fotis Evangelou / Nuevvo Webware Ltd. All rights reserved.
* @license		GNU/GPL license: http://www.gnu.org/copyleft/gpl.html
*/

// Main SimpleTabs function
var kmrSimpleTabs = {

    sbContainerClass: "simpleTabs",
    sbNavClass: "simpleTabsNavigation",
    sbContentClass: "simpleTabsContent",
    sbCurrentNavClass: "current",
    sbCurrentTabClass: "currentTab",
    sbIdPrefix: "tabber",


    init: function () {
        if (!document.getElementsByTagName) return false;
        if (!document.getElementById) return false;

        var containerDiv = document.getElementsByTagName("div");

        for (var i = 0; i < containerDiv.length; i++) {
            if (containerDiv[i].className == kmrSimpleTabs.sbContainerClass) {

                var _cookieBase = containerDiv[i].getAttribute("name");

                // assign a unique ID for this tab block and then grab it
                containerDiv[i].setAttribute("id", kmrSimpleTabs.sbIdPrefix + [i]);
                var containerDivId = containerDiv[i].getAttribute("id");

                // Navigation
                var ul = containerDiv[i].getElementsByTagName("table");

                for (var j = 0; j < ul.length; j++) {
                    if (ul[j].className == kmrSimpleTabs.sbNavClass) {

                        var a = ul[j].getElementsByTagName("a");
                        for (var k = 0; k < a.length; k++) {

                            var _nameunion = a[k].getAttribute("name");

                            var _type = '';
                            var _class = '';

                            if (_nameunion != null && _nameunion != '') {
                                var _info = _nameunion.split("_");
                                if (_info.length == 2) {
                                    _type = _info[0];
                                    _class = _info[1];
                                }
                            }


                            if (_type == 'menutabs') {

                                a[k].setAttribute("id", containerDivId + "_a_" + k);

                                // get current
                                if (kmrSimpleTabs.readCookie(_cookieBase + 'Cookie')) {
                                    var cookieElements = kmrSimpleTabs.readCookie(_cookieBase + 'Cookie').split("_");
                                    var curTabCont = cookieElements[1];
                                    var curAnchor = cookieElements[2];

                                    if (a[k].getAttribute("id") == kmrSimpleTabs.sbIdPrefix + curTabCont + "_a_" + curAnchor) {
                                        a[k].className = _class + 'current';
                                        if (a[k].parentNode.nextSibling.nodeType == 3) {
                                            a[k].parentNode.nextSibling.nextSibling.firstChild.nextSibling.className = "selected_r";
                                            a[k].parentNode.previousSibling.previousSibling.firstChild.nextSibling.className = "selected_l"; // Moz. Opera
                                        }
                                        else {
                                            a[k].parentNode.nextSibling.firstChild.className = "selected_r";
                                            a[k].parentNode.previousSibling.firstChild.className = "selected_l"; // IE
                                        }

                                    } else {
                                        a[k].className = _class;
                                        if (a[k].parentNode.nextSibling.nodeType == 3) {
                                            a[k].parentNode.nextSibling.nextSibling.firstChild.nextSibling.className = "ns_r";
                                            a[k].parentNode.previousSibling.previousSibling.firstChild.nextSibling.className = "ns_l"; // Moz. Opera
                                        }
                                        else {
                                            a[k].parentNode.nextSibling.firstChild.className = "ns_r";
                                            a[k].parentNode.previousSibling.firstChild.className = "ns_l"; // IE
                                        }
                                    }
                                } else {
                                    a[k].className = _class;
                                    if (k == 0) {
                                        a[0].className = _class + 'current';
                                        if (a[0].parentNode.nextSibling.nodeType == 3) {
                                            a[0].parentNode.nextSibling.nextSibling.firstChild.nextSibling.className = "selected_r";
                                            a[0].parentNode.previousSibling.previousSibling.firstChild.nextSibling.className = "selected_l"; // Moz. Opera
                                        }
                                        else {
                                            a[0].parentNode.nextSibling.firstChild.className = "selected_r";
                                            a[0].parentNode.previousSibling.firstChild.className = "selected_l"; // IE
                                        }
                                    }
                                }

                                a[k].onclick = function () {
                                    kmrSimpleTabs.setCurrent(this, _cookieBase + 'Cookie');
                                    return false;
                                }
                            }
                        }
                    }
                }

                // Tab Content
                var div = containerDiv[i].getElementsByTagName("div");
                var countDivs = 0;
                var _default = -1;
                for (var l = 0; l < div.length; l++) {
                    if (div[l].className == kmrSimpleTabs.sbContentClass) {
                        div[l].setAttribute("id", containerDivId + "_div_" + [countDivs]);
                        if (kmrSimpleTabs.readCookie(_cookieBase + 'Cookie') && _default == -1) {
                            var cookieElements = kmrSimpleTabs.readCookie(_cookieBase + 'Cookie').split("_");
                            var curTabCont = cookieElements[1];
                            var curAnchor = cookieElements[2];
                            if (div[l].parentNode.getAttribute("id") == kmrSimpleTabs.sbIdPrefix + curTabCont) {
                                if (div[l].getAttribute("id") == kmrSimpleTabs.sbIdPrefix + curTabCont + "_div_" + curAnchor) {
                                    div[l].className = kmrSimpleTabs.sbContentClass + " " + kmrSimpleTabs.sbCurrentTabClass;
                                } else {
                                    div[l].className = kmrSimpleTabs.sbContentClass;
                                }
                            } else {
                                _default = l;
                                div[_default].className = kmrSimpleTabs.sbContentClass + " " + kmrSimpleTabs.sbCurrentTabClass;
                            }
                        } else if (_default == -1) {
                            _default = l;
                            div[_default].className = kmrSimpleTabs.sbContentClass + " " + kmrSimpleTabs.sbCurrentTabClass;
                        }
                        countDivs++;
                    }
                }

                // End navigation and content block handling	
            }
        }
    },

    // Function to set the current tab
    setCurrent: function (elm, cookie) {       
        this.eraseCookie(cookie);
        //get container ID

        var thisContainer = elm;
        var tableContainer = elm;
        var thisContainerID = "";
        var _stop = false;
        var _count = 0; //avoiding endless cicle
        while (!_stop && _count < 10) {
            _count++;
            thisContainer = thisContainer.parentNode;

            if (thisContainer.nodeName == "DIV") {
                _stop = true;
            }
            if (tableContainer.nodeName != "TABLE") {

                tableContainer = tableContainer.parentNode;
            }
        }

        thisContainerID = thisContainer.getAttribute("id");




        // get current anchor position
        var regExpAnchor = thisContainerID + "_a_";
        var thisLinkPosition = elm.getAttribute("id").replace(regExpAnchor, "");

        // change to clicked anchor


        var otherLinks = tableContainer.getElementsByTagName("a");
        for (var n = 0; n < otherLinks.length; n++) {
            //otherLinks[n].className = baseclass;
            this.setCurrentClass(otherLinks[n], true);
        }
        this.setCurrentClass(elm, false);

        // change to associated div
        var otherDivs = document.getElementById(thisContainerID).getElementsByTagName("div");
        var RegExpForContentClass = new RegExp(kmrSimpleTabs.sbContentClass);
        for (var i = 0; i < otherDivs.length; i++) {
            if (RegExpForContentClass.test(otherDivs[i].className)) {
                otherDivs[i].className = kmrSimpleTabs.sbContentClass;
            }
        }

        var _tempDiv = document.getElementById(thisContainerID + "_div_" + thisLinkPosition);
        _tempDiv.className = kmrSimpleTabs.sbContentClass + " " + kmrSimpleTabs.sbCurrentTabClass;

        // get Tabs container ID
        var RegExpForPrefix = new RegExp(kmrSimpleTabs.sbIdPrefix);
        var thisContainerPosition = thisContainerID.replace(RegExpForPrefix, "");

        // set cookie
        this.createCookie(cookie, 'simpleTabsCookie_' + thisContainerPosition + '_' + thisLinkPosition, 1);
    },

    // Cookies
    createCookie: function (name, value, days) {
        //        if (days) {
        //            var date = new Date();
        //            date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
        //            var expires = "; expires=" + date.toGMTString();
        //        }
        //        else var expires = "";
        //        document.cookie = name + "=" + value + expires + "; path=/";
    },

    readCookie: function (name) {
        //        var nameEQ = name + "=";
        //        var ca = document.cookie.split(';');
        //        for (var i = 0; i < ca.length; i++) {
        //            var c = ca[i];
        //            while (c.charAt(0) == ' ') c = c.substring(1, c.length);
        //            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length, c.length);
        //        }
        return null;
    },

    eraseCookie: function (name) {
        //        this.createCookie(name, "", -1);
    },

    setCurrentClass: function (elm, reset) {
        var _nameunion = elm.getAttribute("name");

        var _type = '';
        var _class = '';

        if (_nameunion != null && _nameunion != '') {
            var _info = _nameunion.split("_");
            if (_info.length == 2) {
                _type = _info[0];
                _class = _info[1];
            }
        }

        if (_type == 'menutabs') {
            if (reset == true) {
                elm.className = _class;
                if (elm.parentNode.nextSibling.nodeType == 3) {
                    elm.parentNode.nextSibling.nextSibling.firstChild.nextSibling.className = "ns_r";
                    elm.parentNode.previousSibling.previousSibling.firstChild.nextSibling.className = "ns_l"; // Moz. Opera
                }
                else {
                    elm.parentNode.nextSibling.firstChild.className = "ns_r";
                    elm.parentNode.previousSibling.firstChild.className = "ns_l"; // IE
                }

            }
            else {
                elm.className = _class + 'current';
                if (elm.parentNode.nextSibling.nodeType == 3) {
                    elm.parentNode.nextSibling.nextSibling.firstChild.nextSibling.className = "selected_r";
                    elm.parentNode.previousSibling.previousSibling.firstChild.nextSibling.className = "selected_l"; // Moz. Opera
                }
                else {
                    elm.parentNode.nextSibling.firstChild.className = "selected_r";
                    elm.parentNode.previousSibling.firstChild.className = "selected_l"; // IE
                }
            }
        }



    },

    // Loader
    addLoadEvent: function (func) {
        var oldonload = window.onload;
        if (typeof window.onload != 'function') {
            window.onload = func;
        } else {
            window.onload = function () {
                if (oldonload) {
                    oldonload();
                }
                func();
            }
        }
    }

    // END
};

// Load SimpleTabs
kmrSimpleTabs.addLoadEvent(kmrSimpleTabs.init);

