PLEX = {
    domain : "plexwerk.net", // the domain name where project run.
                         // ex: "google.com"
                         // NOTE:  there should be no slash "/" at the end, and no "www." at the begining
    startMaximized: true,
    indexPage : "http://plexwerk.net"
}

function getLocation() {
    var regexS = ".*" + PLEX.domain + "\/(.*)$";
    var regex = new RegExp(regexS);
    var results = regex.exec(window.location.href);
    if (!results || !results[1])
        return PLEX.indexPage;
    else
        return results[1];

}
function readCookie(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;
}
//plexter navigation column
(function($) {
    $.fn.plexterColumnNavigation = function(configuration, panel, statusDiv) {
        var navPanelConfig = $.extend({
            columnSelectFontWeight:"normal",
            columnSelectColor:"rgb(0,0,0)",
			columnSeperatorStyle:"1px solid rgb(194,204,255)",
            columnSelectBackgroundColor:"rgb(194,204,255)",
            columnSelectBackgroundRepeat:"",
            columnSelectBackgroundPosition:"",
            columnSelectBackgroundImage:"",

            callBackFunction: function() {/*do nothing*/}
        }, configuration);

        // LI element select state

        var liSelect = {
            backgroundColor:navPanelConfig.columnSelectBackgroundColor,
            backgroundImage:navPanelConfig.columnSelectBackgroundImage,
            backgroundRepeat:navPanelConfig.columnSelectBackgroundRepeat,
            backgroundPosition:navPanelConfig.columnSelectBackgroundPosition
        };
        // A element select state
        var aSelect = {
            color:navPanelConfig.columnSelectColor,
            textDecoration:"none"
        };

        $(this).find("a").click(function() {
            var href = this.href;//this.getAttribute('href', 2);
            
            window.currentHref = href; // this is not the best practice, but i don't want more singletons
            if (href.match("#$") != "#") {
                statusDiv.text("");
                statusDiv.append("wait while loading.<blink>.</blink>");
                statusDiv.show();
                $("#close").attr("class", "btn-close-loading");
                $("#close").unbind("click");
                if (href.indexOf(PLEX.domain) == -1) { // if it's external document
                    if (window.frames[0] == undefined) {
                        document.documentElement.style.overflow = 'hidden'; // ff, safari, opera
                        document.body.scroll = "no";    // ie only

                        $('body').children().each(function(){
                            if($(this).attr("id") != panel.attr("id")){
                                $(this).remove();
                            }
                        });
                        $('body').append('<iframe id="_plexframe" name="_plexframe" src="" width="100%" scrolling="auto" frameborder="0" style="background: #FFF"> <\/iframe>');
                        $('body').find("#_plexframe").height(document.documentElement["clientHeight"]);
                        $(window).resize(function() {
                            $('body').find("#_plexframe").height(document.documentElement["clientHeight"]);
                            $('body').find("#_plexframe").width(document.documentElement["clientWidth"]);
                        });
                        $('body').find("#_plexframe").load(function() {
                            $("#close").attr("class", "btn-close");
                            statusDiv.text("");
                            statusDiv.append("&nbsp;");
                            $("#close").click(function() {
                                panel.fadeOut("slow");
                                window.location = window.currentHref;
                            });
                        });
                    }

                    window.frames[0].location.replace(href);

                } else {
                    $(this).unbind("click"); // otherwise navigation column will show subcategory content before refresh
                    return true;
                }
            }
            return false;
        });

        $(this).columnNavigation(navPanelConfig);

        var locationUrl = getLocation();
        $(this).find("a[href$=" + locationUrl + "]").each(function (i) {
            var parentCategory = $(this).parent().parent().parent().parent();
            if (parentCategory.is("li")) {

                parentCategory.find("ul" + ":first").show();
                parentCategory.css(liSelect);
		scroll(parentCategory);		
                parentCategory = parentCategory.parent().parent().parent();
                if (parentCategory.is("li")) {
                    parentCategory.find("ul" + ":first").show();
                    parentCategory.css(liSelect);
   		    scroll(parentCategory);
                }
            }
            // Show child menu
            $(this).parent().find("ul" + ":first").show();
            // Select this level
            $(this).parent().css(liSelect);
            // Highlight the text if required
            $(this).css(aSelect);
	    scroll($(this).parent());
        });

	function scroll(category){
	     category.parent().scrollTo({top:'+='+( category.offset().top - category.parent().parent().find("li:first").offset().top )+'px'}, 0 );
		
	}

    }
})(jQuery);
//navigationPanel
(function($) {
    $.fn.navigationPanel = function(treeList, statusDiv, config) {
        $(this).css({position:"fixed"});
        var clickAndDragText = statusDiv.text();
        var position = readCookie("navigation_div_location");
        if (position != undefined) {
            var x = parseInt(position.split("_")[0], 10);
            var y = parseInt(position.split("_")[1], 10);
            this.get(0).style.left = x + "px";
            this.get(0).style.top = y + "px";

        }
        treeList.plexterColumnNavigation({}, $(this), statusDiv);

        this.get(0).style.display = "block";
        var wholeBox = this;
        $(this).draggable({
            ghosting: "false",
            fx: "1",
            containment: "document",
            iframeFix: true,
            opacity: $.browser.msie ? false : "0.4",
            handle: "#window-top",
            cancel: "a",
            stop: function(event, ui) {
               document.cookie = 'navigation_div_location=' + wholeBox.get(0).style.left + "_" + wholeBox.get(0).style.top + '; ; path=/';

            }
        });

        // dirty bootstraping
        $(this).find(".b").width($(this).width());             //this is just for ie
        $(this).find("#window-top").width($(this).width()-7);  //this is just for ie
        var hoveredCloseImg = new Image();
        hoveredCloseImg.src = "../imgs/minimize-mousedown.gif";
        hoveredCloseImg.src = "../imgs/minimize-mouseover.gif";
        hoveredCloseImg.src = "../imgs/maximize-mousedown.gif";
        hoveredCloseImg.src = "../imgs/maximize-mouseover.gif";
        hoveredCloseImg.src = "../imgs/close-mouseover.gif";
        hoveredCloseImg.src = "../imgs/close-mousedown.gif";
        hoveredCloseImg.src = "../imgs/load.gif";


        var minimize = function() {
            $(this).attr("class", "btn-maximize");
            if($.browser.msie) {
                $("#myTree").parent().slideUp("slow", function(){
                    $("#gap").hide();
                });
            } else {
                $("#drag").slideUp("slow", function() {$("#gap").hide();});
            }
            $(this).unbind("click");
            $("#window-top").focus();
            $(this).click(maximize); //this looks mystic. but works.
        };
        var maximize = function() {
            $(this).attr("class", "btn-minimize");
            if($.browser.msie) {
                $("#myTree").parent().slideDown();
            } else {
                $("#drag").slideDown();
            }
            $("#gap").show();
            $(this).unbind("click");
            $("#window-top").focus();
            $(this).click(minimize);
        };
        $("#min_max").click(minimize);

        if(!readCookie("not_first_visit") && !PLEX.startMaximized){
            document.cookie = 'not_first_visit=true; ; path=/';
            if($.browser.msie) {
                $("#myTree").parent().hide();
            } else {
                $("#drag").hide();
            }
            $(this).show();
            $("#min_max").click();
        }
    }
})(jQuery);
