﻿/// <reference path="jquery-1.3.2-vsdoc.js" />
/// really appreciated for not touching this piece of codes here
/// unless you fully understand what it's serving for
/// there are 4 types different tree behaviors controlled by this
var shown = false;
var shown = false;
var beingShown = false;
var delayTimer = 400;
// tree sitemap is based on this to populate
$.fn.st = function(opt) {
    return this.each(function() {
        var TREE = this;
        var ROOT = $('.root', this); // tree view root class
        var mousePressed = false;
        var mouseMoved = false;
        var dragMoveType = false;
        var dragNode_destination = false;
        var dragNode_source = false;
        var dragDropTimer = false;
        var ajaxCache = Array();
        // variables declaration here
        TREE.option = {
            isSitemap: false,
            isTree: false,
            isOrphaned: false,
            drag: true,
            animate: true,
            autoclose: false,
            speed: 'normal',
            afterAjax: false,
            afterMove: false,
            afterClick: false,
            afterDblClick: false,
            afterContextMenu: true,
            docToFolderConvert: false
        };
        TREE.option = $.extend(TREE.option, opt);
        $.extend(this, { getSelected: function() {
            return $('span.active', this).parent();
        }
        });
        TREE.closeNearby = function(obj) {
            //Removes all elements from the set of matched elements that do not match the specified expression(s). narrow down the search
            $(obj).siblings().filter('.folder-open, .folder-open-last').each(function() {
                var childUl = $('>ul', this);
                var className = this.className;
                this.className = className.replace('open', 'close');
                if (TREE.option.animate) {
                    childUl.animate({ height: "toggle" }, TREE.option.speed);
                } else {
                    childUl.hide();
                }
            });
        };
        TREE.nodeToggle = function(obj) {
            var childUl = $('>ul', obj);

            if (childUl.is(':visible')) {
                obj.className = obj.className.replace('open', 'close');

                if (TREE.option.animate) {// sliding event needs defining speed
                    childUl.animate({ height: "toggle", "opacity": "toggle" }, TREE.option.speed);
                } else { // getta remove this parameters
                    childUl.hide();
                }
            } else {
                obj.className = obj.className.replace('close', 'open');
                if (TREE.option.animate) {
                    childUl.animate({ height: "toggle", "opacity": "toggle" }, TREE.option.speed, function() {
                        if (TREE.option.autoclose) TREE.closeNearby(obj);
                        if (childUl.is('.ajax')) TREE.setAjaxNodes(childUl, obj.id);
                    });
                } else {
                    childUl.show();
                    if (TREE.option.autoclose) TREE.closeNearby(obj);
                    if (childUl.is('.ajax')) TREE.setAjaxNodes(childUl, obj.id);
                }
            }
        };
        TREE.setAjaxNodes = function(node, parentId, callback) {
            // not huge fun this caching => totaly faking 
            if ($.inArray(parentId, ajaxCache) == -1) {
                ajaxCache[ajaxCache.length] = parentId;
                var url = $.trim($('>li', node).text());
                if (url && url.indexOf('url:')) {
                    url = $.trim(url.replace(/.*\{url:(.*)\}/i, '$1'));
                    $.ajax({
                        type: "GET",
                        url: url,
                        contentType: 'html',
                        cache: false,
                        success: function(responce) {
                            node.removeAttr('class');
                            node.html(responce);
                            $.extend(node, { url: url });
                            TREE.setTreeNodes(node, true);
                            if (typeof TREE.option.afterAjax == 'function') {
                                TREE.option.afterAjax(node);
                            }
                            if (typeof callback == 'function') {
                                callback(node);
                            }
                        }
                    });
                }
            }
        };
        // behaviors implementation shown below:
        TREE.setTreeNodes = function(obj, useParent) {
            obj = useParent ? obj.parent() : obj;
            //
            $('li>span', obj).addClass('text')
			.bind('selectstart', function() {// remove the control for text selection
			    return false;
			}).click(function() {
			    $('.active', TREE).attr('class', 'text');
			    if (this.className == 'text') {
			        this.className = 'active';
			    }
			    if (typeof TREE.option.afterClick == 'function') {
			        TREE.option.afterClick($(this).parent());
			    }
                mousePressed = false;
                Sitemap.action = 0;
			    return false;
			}).dblclick(function() {
			    mousePressed = false;
                Sitemap.action = 0;
			    TREE.nodeToggle($(this).parent().get(0));
			    if (typeof TREE.option.afterDblClick == 'function') {
			        TREE.option.afterDblClick($(this).parent());
			    }
			    return false;
			    // we could do right-click as well whenever necessary				
			    //			}).bind("contextmenu",function(){
			});
            if (TREE.option.drag) {
                //$('li>span', obj).mouseover(function() {
                $('li>span', obj).mouseover(function() {
                    Sys.Debug.trace('+++++++++++++++++++++++++++++++++++++++++++++++++++');
                    Sys.Debug.trace(mouseMoved);
//                    alert(TREE.option.isTree);
                    if (TREE.option.isTree) {
                        if (!mouseMoved) {// reverse all the tags
                            $('ul.siteTree div').remove(); // testing only for cleaning up
                            // don't name variable as class, which is not accepted by IE
                            var status;
                            if ($(this).attr('class').indexOf('offline') != -1) {
                                status = 'Online';
                            }
                            else {
                                status = 'Offline';
                            }
                            var c = $.trim($(this).attr('class').replace('text', '').replace('offline', '').replace('online', ''));

                            Sitemap.element_id = $(this).parent().attr('id');
                            var n = $.trim($(this).parent().attr('name').replace('False', '').replace('True', ''));
                            Sitemap.page_id = n;

                            Sitemap.element_type = c;
                            if ($(this).parent().attr('name').indexOf('True') != -1 || $(this).parent().attr('name').indexOf('False') != -1) {
                                if ($(this).parent().attr('name').indexOf('True') == -1)
                                    $('a.deleteBtn').text('');
                                else
                                    $('a.deleteBtn').text('Remove');
                            }
                            else {
                                $('a.deleteBtn').text('Delete');
                            }
                            switch (c) {
                                case 'page': $(this).after($('#consoleWrapper').html()); $('a.onlineBtn').text(status); break;
                                case 'link': $(this).after($('#linkWrapper').html()); $('a.onlineBtn').text(status); break;
                                case 'heading': $(this).after($('#headingWrapper').html()); $('a.onlineBtn').text(status); break;
                                case 'nondelete': $(this).after($('#nonDeleteWrapper').html()); $('a.onlineBtn').text(status);break;
                                case 'orphaned': $(this).after($('#orphanedWrapper').html());$('a.onlineBtn').text(status); break;
                            }
                            var pos = $(this).position();
                            //$('.treeMenu').css("left", pos.left + 155).css("top", pos.top - 22);
                            $('.treeMenu').css("left", pos.left + 150).css("top", pos.top - 22);
                            $(this).parent().bind("mouseleave", function() {
                                $('ul.siteTree div').fadeOut().remove();
                                return false;
                            });
                            //alert(pos.left + " " + pos.top);
                        }
                    }
                    // events are not bound with the popups tightly
                    $('.active', TREE).attr('class', 'text');
                    if (this.className == 'text') {
                        this.className = 'active';
                    }
                    if (typeof TREE.option.afterContextMenu == 'function') {
                        TREE.option.afterContextMenu($(this).parent());
                    }
                    return false;
                    //                }).mouseout(function() {
                    //                    if (TREE.option.isTree) {
                    //                        //setTimeout(function(){$('ul.siteTree div').remove();}, 300);
                    //                    }
                    //                    return false;
                    //                }).bind("mouseleave", function() {
                    //                    if (TREE.option.isTree) {
                    //                        $('ul.siteTree div').fadeOut().remove();
                    //                    }
                    //                    return false;                    
                }).mousedown(function(event) {
                    if (TREE.option.isTree) {
                        //$('ul.siteTree div').remove(); // testing only for cleaning up
                    }
                    mousePressed = true;
                    cloneNode = $(this).parent().clone();
                    var LI = $(this).parent();
                    //                    Sys.Debug.trace(LI.html());
                    if (TREE.option.drag) {
                        Sys.Debug.trace(TREE.option.isTree + 'TREE.option.isTree');
                        if (this.className.indexOf('topRoot') == -1) {// make this top-level orphaned-item not draggable by default setting
                            if (TREE.option.isTree) {
                                Sys.Debug.trace('@@');
                                $('>ul', cloneNode).hide();
                            }
                            else {
                                Sitemap.action = $(this).parent().attr('id');
                            }
                            //					Sys.Debug.trace(Sitemap.action);
                            $('div', cloneNode).hide(); // hide extra flow-ups
                            $('body').append('<div id="drag_container"><ul></ul></div>');
                            $('#drag_container').hide().css({ opacity: '0.8' }).attr("style", "z-index: 10090");
                            $('#drag_container >ul').append(cloneNode);
                            $("<img>").attr({ id: "tree_plus", src: "/awms/images/tree/plus.gif" }).css({ width: "7px", display: "block", position: "absolute", left: "5px", top: "5px",
                                display: "none"
                            }).appendTo("body");

                            $(document).bind("mousemove", { LI: LI }, TREE.dragStart).bind("mouseup", TREE.dragEnd);
                            if (TREE.option.isOrphaned && mousePressed) {
                                Sitemap.action = -1;
                                Sitemap.orphaned_id = parseInt($(this).parent().attr("id"));
                            }
                            if (TREE.option.isSitemap && mousePressed){
                                Sitemap.sitemap_id = parseInt($(this).parent().attr("id"));
                                Sitemap.action = 5;
                            }
                        }
                        //                        Sys.Debug.trace(TREE.dragStart);
                    }
                    return false;
                }).mouseup(function() {
                    if (TREE.option.isTree) {
                        Sys.Debug.trace('X=======================================================X');
                        Sys.Debug.trace(mousePressed);
                        Sys.Debug.trace(mouseMoved);
                        Sys.Debug.trace(dragNode_source);
                        Sys.Debug.trace($(this).parent().attr('id'));
                        if (!mousePressed && !mouseMoved) {
                            Sitemap.popup($(this).parent().attr('id'), true, TREE.option.isOrphaned);
                        }
                    }
                    if (mousePressed && mouseMoved && dragNode_source) {
                        TREE.moveNodeToFolder($(this).parent());
                    }
                    TREE.eventDestroy();
                });
            }
            $('li', obj).each(function(i) {
                var className = this.className;
                var open = false;
                var cloneNode = false;
                var LI = this;
                var childNode = $('>ul', this);
                if (childNode.size() > 0) {
                    var setClassName = 'folder-';
                    if (className && className.indexOf('open') >= 0) {
                        setClassName = setClassName + 'open';
                        open = true;
                    } else {
                        setClassName = setClassName + 'close';
                    }
                    this.className = setClassName + ($(this).is(':last-child') ? '-last' : '') + ' ' + this.className; // be careful multiple class names

                    if (!open || className.indexOf('ajax') >= 0) { childNode.hide(); }

                    TREE.setTrigger(this);
                } else {
                    var setClassName = 'doc';
                    $(this).addClass(setClassName + ($(this).is(':last-child') ? '-last' : ''));
                }
            });
            if (TREE.option.isTree) {
                $('li', obj).each(function() { }).before('<li class="line">&nbsp;</li>').filter(':last-child').after('<li class="line-last"></li>');
                $('li>span', obj).each(function() {
                    if ($(this).attr('title') == 'offline') {
                        $(this).addClass('offline');
                    }
                });
            }
            //Sys.Debug.trace('1-----------------------------wanna c this-------1xx');
            TREE.setEventLine($('.line, .line-last', obj));
            //Sys.Debug.trace($('.line, .line-last', obj));
        };
        TREE.setTrigger = function(node) {
            $('>span', node).before('<img class="trigger" src="/awms/images/tree/spacer.gif" border=0>');
            var trigger = $('>.trigger', node);
            trigger.click(function(event) {
                TREE.nodeToggle(node);
            });
            if (!$.browser.msie) {
                trigger.css('float', 'left');
            }
        };
        TREE.dragStart = function(event) {
            //			Sys.Debug.trace(event);

            var LI = $(event.data.LI);
            if (mousePressed) {
                mouseMoved = true;
                if (dragDropTimer) clearTimeout(dragDropTimer);
                if ($('#drag_container:not(:visible)')) {
                    $('#drag_container').show();
                    LI.prev('.line').hide();
                    dragNode_source = LI;
                }
                $('#drag_container').css({ position: 'absolute', "left": (event.pageX + 5), "top": (event.pageY + 15) });
                // there you are
                if (LI.is(':visible')) {
                    if (TREE.option.isTree) {
                        LI.hide();
                    }
                }
                var temp_move = false;
                if (event.target.tagName.toLowerCase() == 'span' && $.inArray(event.target.className, Array('text', 'active', 'trigger')) != -1) {
                    //					Sys.Debug.trace(event);
                    var parent = event.target.parentNode;
                    var offs = $(parent).offset({ scroll: false });
                    var screenScroll = { x: (offs.left - 3), y: event.pageY - offs.top };
                    var isrc = $("#tree_plus").attr('src');
                    var ajaxChildSize = $('>ul.ajax', parent).size();
                    var ajaxChild = $('>ul.ajax', parent);
                    screenScroll.x += 19;
                    screenScroll.y = event.pageY - screenScroll.y + 5;

                    if (parent.className.indexOf('folder-close') >= 0 && ajaxChildSize == 0) {
                        if (isrc.indexOf('minus') != -1) $("#tree_plus").attr('src', '/awms/images/tree/plus.gif');
                        $("#tree_plus").css({ "left": screenScroll.x, "top": screenScroll.y }).show();
                        dragDropTimer = setTimeout(function() {
                            parent.className = parent.className.replace('close', 'open');
                            $('>ul', parent).show();
                        }, 700);
                    } else if (parent.className.indexOf('folder') >= 0 && ajaxChildSize == 0) {
                        if (isrc.indexOf('minus') != -1) $("#tree_plus").attr('src', '/awms/images/tree/plus.gif');
                        $("#tree_plus").css({ "left": screenScroll.x, "top": screenScroll.y }).show();
                    } else if (parent.className.indexOf('folder-close') >= 0 && ajaxChildSize > 0) {
                        mouseMoved = false;
                        $("#tree_plus").attr('src', '/awms/images/tree/minus.gif');
                        $("#tree_plus").css({ "left": screenScroll.x, "top": screenScroll.y }).show();

                        $('>ul', parent).show();
                        TREE.setAjaxNodes(ajaxChild, parent.id, function() {
                            parent.className = parent.className.replace('close', 'open');
                            mouseMoved = true;
                            $("#tree_plus").attr('src', '/awms/images/tree/plus.gif');
                            $("#tree_plus").css({ "left": screenScroll.x, "top": screenScroll.y }).show();
                        });

                    } else {// let's move here
                        if (TREE.option.docToFolderConvert) {
                            $("#tree_plus").css({ "left": screenScroll.x, "top": screenScroll.y }).show();
                        } else {
                            $("#tree_plus").hide();
                        }
                    }
                } else {
                    $("#tree_plus").hide();
                }
                return false;
            } // remove below by the time it goes into production environment
            else {
                Sys.Debug.trace('--------------------------------------------------');
                Sys.Debug.trace($(this).parent());
                Sys.Debug.trace('--------------------------------------------------');
            }
            return true;
        };
        TREE.dragEnd = function() {
            Sys.Debug.trace('bug c u');
            // that's where reset comes in
            Sitemap.action = 0;
            if (dragDropTimer) clearTimeout(dragDropTimer);
            TREE.eventDestroy();
        };
        TREE.setEventLine = function(obj) {
            obj.mouseover(function() {
                Sys.Debug.trace(Sitemap.action);
                Sys.Debug.trace('eric');
                if ((this.className.indexOf('over') < 0 && Sitemap.action != 0) ||
                (this.className.indexOf('over') < 0 && mousePressed && mouseMoved && !TREE.option.isOrphaned)) {
                    if (delayTimer) clearTimeout(delayTimer);
                    if (beingShown || shown) { return; }
                    else {
                        beingShown = true;
                        //   if(this.className.indexOf('over')<0 && mousePressed && mouseMoved
                        Sys.Debug.trace(Sitemap.action);
                        Sys.Debug.trace(mousePressed);
                        Sys.Debug.trace(mouseMoved);
                        Sys.Debug.trace('TREE.setEventLine');
                        Sys.Debug.trace(TREE.option.isOrphaned);
                        if((!TREE.option.isOrphaned && Sitemap.action == -1) || (Sitemap.action != -1) || (Sitemap.action != 0))
                            this.className = this.className.replace('line', 'line-over');

                        beingShown = false; shown = false;
                    }
                }
                //Sitemap.action = 0;
                return false;
            }).mouseout(function() {
                Sys.Debug.trace(TREE);
                Sys.Debug.trace('mouseout');
                if ((this.className.indexOf('over') >= 0) || (this.className.indexOf('over') >= 0 && mousePressed && mouseMoved)) {
                    // && Sitemap.action != 0
                    this.className = this.className.replace('-over', '');
                }
                return false;
            }).mouseup(function() {
                Sys.Debug.trace('mouseup');
                if (mousePressed && dragNode_source && mouseMoved) {
                    dragNode_destination = $(this).parents('li:first');
                    if (Sitemap.action != -1)
                        TREE.moveNodeToLine(this);
                }
                if (!mousePressed && !mouseMoved && Sitemap.action != 0) {
                    Sys.Debug.trace('@@@@@@@@@-------------------------------@@@@@@@@@@@@@@')
                    Sys.Debug.trace($(this).prevAll(':not(.line)').size()); // sort order
                    Sys.Debug.trace($(this).parents('li:first').attr('id')); // parent_id

                    Sitemap.sort_order = $(this).prevAll(':not(.line)').size();
                    Sitemap.parent_id = $(this).parents('li:first').attr('id');
                    Sitemap.popup($(this).parent().attr('id'), false, TREE.option.isOrphaned);
                }
                //  reset this
                TREE.eventDestroy();

                return false;
            });
        };
        TREE.checkNodeIsLast = function(node) {
            if (node.className.indexOf('last') >= 0) {
                var prev_source = dragNode_source.prev().prev();
                if (prev_source.size() > 0) {
                    prev_source[0].className += '-last';
                }
                node.className = node.className.replace('-last', '');
            }
        };
        TREE.checkLineIsLast = function(line) {
            if (line.className.indexOf('last') >= 0) {
                var prev = $(line).prev();
                if (prev.size() > 0) {
                    prev[0].className = prev[0].className.replace('-last', '');
                }
                dragNode_source[0].className += '-last';
            }
        };
        TREE.eventDestroy = function() {
            $(document).unbind('mousemove', TREE.dragStart).unbind('mouseup').unbind('mousedown');
            $('#drag_container, #tree_plus').remove();
            if (dragNode_source) {
                $(dragNode_source).show().prev('.line').show();
            }
            dragNode_destination = dragNode_source = mousePressed = mouseMoved = false;
            //ajaxCache = Array();
        };
        TREE.convertToFolder = function(node) {
            node[0].className = node[0].className.replace('doc', 'folder-open');
            node.append('<ul><li class="line-last"></li></ul>');
            TREE.setTrigger(node[0]);
            TREE.setEventLine($('.line, .line-last', node));
        };
        TREE.convertToDoc = function(node) {
            //			alert('eric');
            Sys.Debug.trace(node);
            $('>ul', node).remove();
            $('img', node).remove();
            node[0].className = node[0].className.replace(/folder-(open|close)/gi, 'doc');
        };
        TREE.moveNodeToFolder = function(node) {
            if (!TREE.option.docToFolderConvert && node[0].className.indexOf('doc') != -1) {
                return true;
            } else if (TREE.option.docToFolderConvert && node[0].className.indexOf('doc') != -1) {
                TREE.convertToFolder(node);
            }
            TREE.checkNodeIsLast(dragNode_source[0]);
            var lastLine = $('>ul >.line-last', node);
            if (lastLine.size() > 0) {
                TREE.moveNodeToLine(lastLine[0]);
            }
        };
        TREE.moveNodeToLine = function(node) {
            TREE.checkNodeIsLast(dragNode_source[0]);
            TREE.checkLineIsLast(node);
            var parent = $(dragNode_source).parents('li:first');
            var line = $(dragNode_source).prev('.line');
            $(node).before(dragNode_source);
            $(dragNode_source).before(line);
            node.className = node.className.replace('-over', '');
            var nodeSize = $('>ul >li', parent).not('.line, .line-last').filter(':visible').size();
            if (TREE.option.docToFolderConvert && nodeSize == 0) {
                TREE.convertToDoc(parent);
            } else if (nodeSize == 0) {
                parent[0].className = parent[0].className.replace('open', 'close');
                $('>ul', parent).hide();
            }

            if ($('span:first', dragNode_source).attr('class') == 'text') {
                $('.active', TREE).attr('class', 'text');
                $('span:first', dragNode_source).attr('class', 'active');
            }

            if (typeof (TREE.option.afterMove) == 'function') {
                var pos = $(dragNode_source).prevAll(':not(.line)').size();
                TREE.option.afterMove($(node).parents('li:first'), $(dragNode_source), pos);
            }
        };

        TREE.addNode = function(id, text, callback) {
            //			alert("addNode");
            var temp_node = $('<li><ul><li id="' + id + '"><span>' + text + '</span></li></ul></li>');
            TREE.setTreeNodes(temp_node);
            dragNode_destination = TREE.getSelected();
            dragNode_source = $('.doc-last', temp_node);
            TREE.moveNodeToFolder(dragNode_destination);
            temp_node.remove();
            if (typeof (callback) == 'function') {
                callback(dragNode_destination, dragNode_source);
            }
        };
        TREE.delNode = function(callback) {
            //			alert("delNode");
            dragNode_source = TREE.getSelected();
            TREE.checkNodeIsLast(dragNode_source[0]);
            dragNode_source.prev().remove();
            dragNode_source.remove();
            if (typeof (callback) == 'function') {
                callback(dragNode_destination);
            }
        };

        TREE.init = function(obj) {
            TREE.setTreeNodes(obj, false);
        };
        TREE.init(ROOT);
    });
}


var WMS = {
    height: 0, width: 0, isCorporate: false, corporate_ccid: 0,
    showSessionOutAlert: function(timer) {
        setTimeout(function() {
            alert('You online session will expire in approximately two minutes.\n Please save your work as soon as possible');
        }, timer * 60000);
    },
    loadNavLastTab: function() {
        $('.awms_nav .setting').hide();
        $('.awms_nav ul.componentConfig li').removeClass('hover');
        $('.awms_nav ul.componentConfig li:eq(2)').addClass('hover');
        $('.awms_nav .setting:eq(2)').show();
        return false;
    },
    loadLastTab: function() {
        $('.awms_nav .setting').hide();
        $('.awms_nav ul.componentConfig li').removeClass('hover');
        $('.awms_nav ul.componentConfig li:eq(2)').addClass('hover');
        $('.awms_nav .setting:eq(2)').show();
        return false;
    },
    loadNavigationControl: function(id) {
        loadUserControl('~/components/navComponent.ascx', 2, id);
    },
    loadLastTreeTab: function() {
        var length;
        if (Sitemap.isLastTab)
            length = $('.genericComponent .setting').length - 1;
        else
            length = $('.genericComponent .setting').length - 2;
        $('.genericComponent .setting').hide();
        $('.genericComponent ul.componentConfig li').removeClass('hover');
        $('.genericComponent ul.componentConfig li:eq(' + length + ')').addClass('hover');
        $('.genericComponent .setting:eq(' + length + ')').show();
        return false;
    },
    loadNavigationControl: function(id) {
        loadUserControl('~/components/navComponent.ascx', 2, id);
        WMS.loadPopup();
        return false;
    },
    reloadSitemap: function() {
        loadUserControl('~/components/sitemap.ascx'); isPostback = false;
        return false;
    },
    logout: function() { Sys.Services.AuthenticationService.logout(null, null, null, null); },
    initRTCTabs: function() {
        $('.awms_rtc ul.componentConfig li').click(function() {
            $('.awms_rtc .setting').hide();
            $('.awms_rtc ul.componentConfig li').removeClass('hover');
            var index = $(".awms_rtc ul.componentConfig li").index(this);
            Sys.Debug.trace(index);
            Sys.Debug.trace('oooooooooooo');
            $('.awms_rtc ul.componentConfig li:eq(' + index + ')').addClass('hover');
            $('.awms_rtc .setting:eq(' + index + ')').show();
            return false;
        });
    },
    initNavTabs: function() {
        $('.awms_nav ul.componentConfig li').click(function() {
            $('.awms_nav .setting').hide();
            $('.awms_nav ul.componentConfig li').removeClass('hover');
            var index = $(".awms_nav ul.componentConfig li").index(this);
            Sys.Debug.trace(index);
            Sys.Debug.trace('oooooooooooo');
            $('.awms_nav ul.componentConfig li:eq(' + index + ')').addClass('hover');
            $('.awms_nav .setting:eq(' + index + ')').show();
            return false;
        });
    },

    loadGenericComponentPopup: function(user_control, json_comp, newCloseButton) {
        Sys.Debug.trace(json_comp);
        loadUserControlWithJsonComp(user_control, json_comp);
        WMS.loadPopup();
        if (newCloseButton) {

        }
        return false;
    },
    // from last integration ** 3rd June 2009
    loadGenericControl: function(json_comp) {
        Sys.Debug.trace('loadGenericControl');
        loadUserControlWithJsonComp('~/components/newGenericComponent.ascx', json_comp);
        WMS.loadPopup();
        return false;
    },
    // from last integration ** 3rd June 2009
    initTabs: function() {
        Sys.Debug.trace('initTabs');

        if ($('.corporateTree').length > 0) {
            navigation_hierarchy_temp = Sitemap.navigation_hierarchy_id;
            Sys.Debug.trace(navigation_hierarchy_temp);
        }

        $('ul.componentConfig li').click(function() {
            var rtcComponent = $(this).parent().parent();
            rtcComponent.children('.displayInner').children('.slider').children('.setting').hide();
            rtcComponent.children('ul.componentConfig').children('li').removeClass('hover');
            var index = $(this).parent().children('ul.componentConfig li').index(this);

            if ($('.corporateTree').length > 0) {
                if ($(this).text().indexOf('Domain') > -1) {
                    Sitemap.navigation_hierarchy_id = Sitemap.domain_navigation_hierarchy_id;
                    $('.corporateTree #-1').attr('id', 0);
                } // fixes applied here for empty tree
                else {
                    Sitemap.navigation_hierarchy_id = navigation_hierarchy_temp;
                }
            }
            if ($('.corporateTree .doubleTabs').length > 0) {
                if ($(this).text().indexOf('Domain') > -1) { Sitemap.isLastTab = true; }
                else { Sitemap.isLastTab = false; }
            } else { Sitemap.isLastTab = true; }

            rtcComponent.children('ul.componentConfig').children('li:eq(' + index + ')').addClass('hover')
            rtcComponent.children('.displayInner').children('.slider').children('.setting:eq(' + index + ')').show();
            return false;
        });

        // meaning generic component edit popup
        if ($("#wrapperHfCurrentTabIndex").length > 0) {
            PopupEditTab.init();
        }

        $('ul.componentConfig li.hover:visible').each(function() {
            if ($('.corporateTree').length > 0) {
                if ($(this).text().indexOf('Domain') > -1) {
                    Sys.Debug.trace('domain');
                    Sitemap.navigation_hierarchy_id = Sitemap.domain_navigation_hierarchy_id;
                    $('.corporateTree #-1').attr('id', 0);
                } // fixes applied here for empty tree
            }
        });
    },
    bindCorporateNavigationInstance: function(re) {
        WMS.corporate_ccid = re;
    },
    admin_init: function() {
        if (typeof document.body.style.maxHeight === "undefined") {
            $("body", "html").css({ height: "100%", width: "100%" });
            $("html").css("overflow-x", "auto");
        }
        $(window).resize(function() {
            WMS.getWidth();
            $("#popUp, #pnlRTEWindow").css('left', (WMS.width - 800) / 2 + 'px');
            $("#extrapopUp").css('left', (WMS.width - 560) / 2 + 'px');
            // special case for enlarged window


            if ($.browser.msie && $.browser.version < 7) {
                $("#popUp, #pnlRTEWindow").css('top', (WMS.height - 560) / 2 + 'px');
                $("#extrapopUp").css('top', WMS.height / 5 + 'px');

            }
            else {
                $("#popUp, #pnlRTEWindow").css('top', (WMS.height - 560) / 2 + 'px');
                $("#extrapopUp").css('top', WMS.height / 4 + 'px');

            }

            $("#blockUIPopup").css({ height: $(document).height(), width: $(document).width() });
        });

        // do not do this!!!
        $('#popUp .close a.closeBtn, #blockUIPopup').click(function() {
            if ($('#popUp h1:first').attr('class').indexOf('awms_admin') != -1) {
                WMS.closePopup();
                return;
            }

            if (isPostback) { window.location.reload(false); }
            else { WMS.closePopup(); }

            if ($('#popUp h1:first').attr('class').indexOf('awms_map') != -1) {
                updatedNavigationInstances();
                actions.initSitemap();
            }
            if ($('#popUp h1:first').attr('class').indexOf('awms_site') != -1) {
                actions.initSite();
            }
            if ($('#popUp h1:first').attr('class').indexOf('awms_page') != -1) {
                actions.initPage();
            }
            if ($('#popUp div.rtcComponent').length > 0) {
                if ($('#popUp div.rtcComponent').attr('class').indexOf('awmsCorporate') != -1) {
                    AwmsUI.Actions.reloadUndraggableComponent(AwmsUI.Actions.instance_id);
                }
                if ($('#popUp div.rtcComponent').attr('class').indexOf('awmsMBG') != -1) {
                    AwmsUI.Actions.reloadPageWithoutInsertion(AwmsUI.Actions.instance_id);
                }
            }

            return false;
        }).attr('href', 'javascript:{};');

        $('#extrapopUp .close a.closeBtn').click(function() {
            WMS.closeextrapopUp();
            if ($('#extrapopUp h1:first').attr('class').indexOf('awms_admin') == -1) {
                updateParent();
            }
            return false;
        }).attr('href', 'javascript:{};');

        if ($.browser.msie && $.browser.version < 7) {
            $(window).scroll(function() {
                $("#blockUIPopup").css({ height: $(document).height(), width: $(document).width() });
            });
        }
    },
    init: function() {
        if (checkDomain() === "True") {
            WMS.isCorporate = true;
            wcf.wmsService.GetCorporateNavInstanceIdByPageId(Utility.UrlParam("pid"), WMS.bindCorporateNavigationInstance);
        }
        Sys.Debug.trace(WMS.isCorporate);

        //$('img', this).attr('src', '/awms/images/bg/arrowDown.png');
        $('#topNav, #topNav .outer').hide();
        $("#middleNav").css({ "top": "0px" });

        WMS.admin_init();
    },

    bindUpdateSubNavInstance: function(result) {
        for (var i in result) {
            var _s = result[i].split('|');
            Sys.Debug.trace(_s[0] + '  -- eric hello');
            $('#_' + _s[0] + '_pnlContent1').html(_s[1]);
        }
        Utility.unblockUI();
        Utility.ClearAnimate();
    },

    bindCorporateUpdateNavigationInstance: function(result) {
        for (var i in result) {
            var _s = result[i].split('|');
            Sys.Debug.trace(_s[0] + '  -- eric hello');
            $('#_' + _s[0] + '_pnlContent1').html(_s[1]);
        }
        Utility.unblockUI();
        Utility.ClearAnimate();
    },

    bindUpdateNavigationInstance: function(result) {
        for (var i in result) {
            var _s = result[i].split('|');
            Sys.Debug.trace(_s[0] + '  -- eric hello');
            $('#_' + _s[0] + '_pnlContent1').html(_s[1]);
        }
        Sitemap.reReshNavigation();
        Utility.unblockUI();
        Utility.ClearAnimate();
    },

    loadPageConsole: function() {
        loadUserControl('~/components/pageManage.ascx');
        WMS.loadPopup();
    },

    closePopup: function(isRefresh, isUpdate, container_component_id) {
        if (isRefresh != undefined) { window.location.reload(false); }
        if (container_component_id == undefined) {
            $("#blockUIPopup, #popUp").hide();
            return;
        }
        if (isUpdate != undefined) {
            if (isUpdate == true) { AwmsUI.Actions.reloadUndraggableComponent(container_component_id); }
            else { AwmsUI.Actions.reloadPageWithoutInsertion(container_component_id); }
        }
        $("#blockUIPopup, #popUp").hide();

        AwmsUI.Actions.updateCPH();
    },
    closeextrapopUp: function() {
        $("#extrablockUI, #extrapopUp").hide();
    },
    loadPopup: function() {
        WMS.getWidth();
        //$("#blockUIPopup").fadeIn();
        $("#blockUIPopup").show();
        //$("#popUp").fadeIn('slow').css('left', (WMS.width - 800) / 2 + 'px');
        $("#popUp").show().css('left', (WMS.width - 800) / 2 + 'px');
        if ($.browser.msie && $.browser.version < 7)
            $("#popUp").css('top', (WMS.height - 560) / 2 + 'px');
        else
            $("#popUp").css('top', (WMS.height - 560) / 2 + 'px');
    },
    loadRTCPopup: function() {
        WMS.getWidth();
        //$("#pnlRTEWindow").fadeIn('slow').css('left', (WMS.width - 800) / 2 + 'px');
        $("#pnlRTEWindow").show().css('left', (WMS.width - 800) / 2 + 'px');
        if ($.browser.msie && $.browser.version < 7)
            $("#pnlRTEWindow").css('top', (WMS.height - 560) / 2 + 'px');
        else
            $("#pnlRTEWindow").css('top', (WMS.height - 560) / 2 + 'px');
    },
    loadextraBigPopup: function() {
        WMS.getWidth();
        $("#extrablockUI").show(); //$("#extrablockUI").fadeIn();
        $("#extrapopUp").show().css('left', (WMS.width - 760) / 2 + 'px'); //$("#extrapopUp").fadeIn().css('left', (WMS.width - 560) / 2 + 'px');
        $("#extrapopUp").css('top', (WMS.height - 520) / 2 + 'px');
    },
    loadextrapopUp: function(goOut) {
        Sys.Debug.trace('-------------------------------------------------------');
        //Sys.Debug.trace(goOut);
        Sys.Debug.trace('-------------------------------------------------------');
        if (goOut != undefined) {
            loadInnerPopupGeneric('~/components/leaveSite.ascx', null, goOut)
        }

        WMS.getWidth();
        $("#extrablockUI").show(); //$("#extrablockUI").fadeIn();
        $("#extrapopUp").show().css('left', (WMS.width - 560) / 2 + 'px'); //$("#extrapopUp").fadeIn().css('left', (WMS.width - 560) / 2 + 'px');
        if ($.browser.msie && $.browser.version < 7)
            $("#extrapopUp").css('top', WMS.height / 5 + 'px');
        else
            $("#extrapopUp").css('top', WMS.height / 4 + 'px');
    },
    getWidth: function() {
        var de = document.documentElement;
        WMS.width = window.innerWidth || self.innerWidth || (de && de.clientWidth) || document.body.clientWidth;
        WMS.height = window.innerHeight || self.innerHeight || (de && de.clientHeight) || document.body.clientHeight;
    }
};

