
/// <reference name="MicrosoftAjax.debug.js" />
/// <reference path="jquery-1.3.2-vsdoc.js" />
var type_button; //site_tree;				
var orphaned;
var isPostback = false; // would be removed
var navigation_hierarchy_temp = 0;
$(document).ready(function() {
    $('.defaultNav').each(function() {
        $(this).find('li:first').addClass('first');
        var index = $('>li', this).length - 2;
        $('li:eq(' + index + ')', this).addClass('last');
    });
    WMS.init();
    WMS.initRTCTabs();
    WMS.initNavTabs();
    $("#topNav .nav li").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });
    $("#middleNav .subNav li").not(".arrow,.editSite,.disable").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });

    $('#middleNav a.publish').mouseover(function() {
        $('img', this).attr('src', '/awms/images/bg/buttons/icon_review_over.png');
    }).mouseout(function() {
        $('img', this).attr('src', '/awms/images/bg/buttons/icon_review.png');
    }).mousedown(function() {
        $('img', this).attr('src', '/awms/images/bg/buttons/icon_review_on.png');
        $(this).unbind("mouseover").unbind("mouseout");
    });

    $('#middleNav a.sitemap').click(function() {
        loadUserControl('~/components/sitemap.ascx'); isPostback = false;
        Sitemap.navigation_hierarchy_id = 0;
        $('img', this).attr('src', '/awms/images/bg/buttons/icon_sitemap_on.png');
        $(this).unbind("mouseover").unbind("mouseout");
        WMS.loadPopup();
        return false;
    }).attr("href", "javascript:{};");
    actions.initSitemap();
    actions.initPage();
    actions.initSite();
    
    $('#middleNav a.site').click(function() {
        loadUserControl('~/components/siteManage.ascx'); isPostback = false;
        WMS.loadPopup();
        $('img', this).attr('src', '/awms/images/bg/buttons/icon_config_on.png');
        $(this).unbind("mouseover").unbind("mouseout");
        return false;
    }).attr("href", "javascript:{};");

    $('#middleNav a.page').click(function() {
        loadUserControl('~/components/pageManage.ascx'); isPostback = false;
        WMS.loadPopup();
        $('img', this).attr('src', '/awms/images/bg/buttons/icon_config_on.png');
        $(this).unbind("mouseover").unbind("mouseout");
        return false;
    }).attr("href", "javascript:{};");

    $('#middleNav a.preview').click(function() {
        AwmsUI.Actions.mode = 'preview';
        wcf.wmsService.IsRefreshRequired(AwmsUI.Actions.page_id, AwmsUI.Actions.onIsRequireReresh);
        $('img', this).attr('src', '/awms/images/bg/buttons/icon_preview_on.png');
        $(this).unbind("mouseover").unbind("mouseout");
        actions.initEdit();
        //AwmsUI.Actions.preview();        
        //AwmsUI.Actions.mode = 'preview';
        return false;
    }).attr("href", "javascript:{};");

    $('#middleNav a.edit').click(function() {
        AwmsUI.Actions.mode = 'edit';
        wcf.wmsService.IsRefreshRequired(AwmsUI.Actions.page_id, AwmsUI.Actions.onIsRequireReresh);
        $('img', this).attr('src', '/awms/images/bg/buttons/icon_edit_on.png');
        $(this).unbind("mouseover").unbind("mouseout");
        actions.initPreview();
        //AwmsUI.Actions.showEdit();
        //AwmsUI.Actions.mode = 'edit';
        return false;
    }).attr("href", "javascript:{};");

    actions.initPreview();
    actions.initEdit();

    $('#middleNav a.arrowBtn').toggle(
        function() {
            $('img', this).attr('src', '/awms/images/bg/arrowUp.png');
            $('#topNav, #topNav .outer').slideDown();
            $("#middleNav").animate({ "top": "112px" });
        },
		function() {
		    $('img', this).attr('src', '/awms/images/bg/arrowDown.png');
		    $('#topNav, #topNav .outer').slideUp();
		    $("#middleNav").animate({ "top": "0px" });
		}
	 ).attr("href", "javascript:{};");

    $(".middleRight").mouseover(function() {
        if (delayTimer) clearTimeout(delayTimer);
        if (beingShown || shown) { return; }
        else {
            beingShown = true;
            beingShown = false; shown = false;
            var pos = $(this).position();
            $("#urlTooltip").css("left", pos.left + 50).css("top", pos.top + 25);
            $("#urlTooltip").fadeIn();
        }
        return false;
    }).mouseout(function() {
        if (delayTimer) clearTimeout(delayTimer);
        delayTimer = setTimeout(function() { delayTimer = null; $("#urlTooltip").fadeOut(); }, 400);
    });

    $("#addComponent").mouseover(function() {
        if (delayTimer) clearTimeout(delayTimer);
        if (beingShown || shown) { return; }
        else {
            beingShown = true;
            $('#addComponent .add img').attr("src", "/awms/images/bg/arrowRight.png");
            $('#addComponent, #addComponent .inner').css('width', '500px');
            $('#addComponent .addBelow').show();

            beingShown = false; shown = false;
        }
        return false;
    }).mouseout(function() {
        if (delayTimer) clearTimeout(delayTimer);
        delayTimer = setTimeout(function() {
            delayTimer = null;
            $('#addComponent .addBelow').hide();
            $('#addComponent .add img').attr("src", "/awms/images/bg/arrowLeft.png");
            $('#addComponent, #addComponent .inner').css('width', '31px');
        }, 400);
        return false;
    });

    $('#addComponent .tabs li.up').click(function() {
        $('#addComponent .tabs li').removeClass('active');
        $(this).addClass('active');
        $('#addComponent .components li.new, #addComponent .components li.old, #addComponent .components li.other').hide();
        $('#addComponent .components li.tabs:eq(' + $('#addComponent .tabs li').index(this) + ')').show();
        return false;
    });

    $('#addComponent .tabs li.up:eq(1)').click(function() {
        addLoadUserControlMiddle('~/components/currentDomain.ascx')
    });

    $('#addComponent .tabs li.up:last').click(function() {
        addLoadUserControl('~/components/otherDomain.ascx')
    });
    /// tree section
    Sitemap.initTree('.siteTree');
    $('#content a').each(function() {
        if ($(this).attr('href') == '#')
            $(this).attr('href', 'javascript:{};');
    });
});
var Sitemap = {
    action: null, element_id: null, element_type: null, page_id: null, sort_order: 0, parent_id: 0, navigation_hierarchy_id: 0, orphaned_id: 0, sitemap_id: 0,
    isLastTab: true,
    toggle_offlineItem: function() { // this function is no longer used; logic has been switched to the server-side
        $('ul#orphaned').toggle();
        //$('#sitemap .offline').toggle();
    },
    initTree: function(name) {
        type_button = $(name).st({
            isTree: false,
            drag: false,
            autoclose: false,
            afterClick: function(node) { },
            afterDblClick: function(node) { },
            afterMove: function(destination, source, pos) { },
            afterAjax: function() { },
            afterContextMenu: function() { },
            animate: true,
            docToFolderConvert: false
        });
    },
    init: function() {
        Sitemap.action = 0;
        type_button = $('.new_type').st({
            isTree: false,
            autoclose: false,
            isOrphaned: false,
            afterClick: function(node) { },
            afterDblClick: function(node) { },
            afterMove: function(destination, source, pos) { },
            afterAjax: function() { },
            afterContextMenu: function() { },
            animate: true,
            docToFolderConvert: false
        });
        orphaned = $('#orphaned').st({
            isTree: true,
            isOrphaned: true,
            autoclose: false,
            afterClick: function(node) { },
            afterDblClick: function(node) { },
            afterMove: function(destination, source, pos) { },
            afterAjax: function() { },
            afterContextMenu: function() { },
            animate: true,
            docToFolderConvert: false
        });

        if ($(".siteTree ul li#-1").length > 0)
            $(".siteTree ul li#-1").attr("id", 0);
        else if ($(".siteTree ul li.empty").length > 0)
            $(".siteTree ul li#1").attr("id", 0);

        site_tree = $('#sitemap, #domain').st({
            isSitemap: true,
            isTree: true,
            autoclose: false,
            isOrphaned: false,
            afterClick: function(node) { },
            afterDblClick: function(node) { },
            afterMove: function(destination, source, pos) {
                Sys.Debug.trace("destination-" + $(destination).attr("id") + " source-" + $(source).attr("id") + " pos-" + pos);
                Sys.Debug.trace(destination);
                Sys.Debug.trace(source);
                Sys.Debug.trace(pos);
                Sys.Debug.trace("afterMove----------------");
                Sitemap.action = 0;
                wcf.wmsService.updateNavElementPositions($(source).attr("id"), $(destination).attr("id"), pos);
            },
            afterAjax: function() { },
            afterContextMenu: function() { },
            animate: true,
            docToFolderConvert: true
        });
    },
    reReshNavigation: function() {
        $('.defaultNav').each(function() {
            $('li:first', this).addClass('first');
            var index = $('>li', this).length - 2;
            $('li:eq(' + index + ')', this).addClass('last');
        });
        // for the nav
        $(".defaultNav li").not(".last, .first").hover(function() { $(this).addClass("hover"); }, function() { $(this).removeClass("hover"); });
        $(".defaultNav li.last").hover(
             function() { $(".defaultNav").addClass("defaultNavLastHover"); $(this).addClass("hover"); },
             function() { $(".defaultNav").removeClass("defaultNavLastHover"); $(this).removeClass("hover"); });
        $(".defaultNav li.first").hover(
             function() { $(this).addClass("firstHover"); },
             function() { $(this).removeClass("firstHover"); }
        );
    },
    edit: function() {
        switch (Sitemap.element_type) {
            case 'nondelete': document.location.href = "/awms/ManageSite.aspx?pid=" + Sitemap.page_id + "&mode=" + AwmsUI.Actions.mode; break;
            case 'orphaned': document.location.href = "/awms/ManageSite.aspx?pid=" + Sitemap.page_id + "&mode=" + AwmsUI.Actions.mode; break;
            case 'page': document.location.href = "/awms/ManageSite.aspx?pid=" + Sitemap.page_id + "&mode=" + AwmsUI.Actions.mode; break;
            case 'link':
                loadInnerPopup('~/components/editNav.ascx', Sitemap.element_id, Sitemap.element_type); WMS.loadextrapopUp();
                break;
            case 'heading':
                loadInnerPopup('~/components/editNav.ascx', Sitemap.element_id, Sitemap.element_type); WMS.loadextrapopUp();
                break;
        }
    },
    rename: function() {
        loadInnerPopup('~/components/rename.ascx', Sitemap.element_id, Sitemap.element_type);
        WMS.loadextrapopUp();
    },
    del: function() {
        loadInnerPopup('~/components/delete.ascx', Sitemap.element_id, Sitemap.element_type);
        WMS.loadextrapopUp();
    },
    online: function(orphaned) {
        if (orphaned)
            loadInnerPopup('~/components/onlineOffline.ascx', Sitemap.element_id, 'orphaned_page');
        else
            loadInnerPopup('~/components/onlineOffline.ascx', Sitemap.element_id, Sitemap.element_type);
        WMS.loadextrapopUp();
    },
    popup: function(nav_element_id, inner, orphaned) {
        Sys.Debug.trace('---------Sitemap Popup function---------');
        // if this.action is -1, then it comes from an orphaned
        Sys.Debug.trace(this.action);
        Sys.Debug.trace(nav_element_id);
        Sys.Debug.trace(Sitemap.parent_id);
        Sys.Debug.trace(Sitemap.element_id);
        Sys.Debug.trace(orphaned);
        switch (this.action) {
            case -1:
                loadInnerPopup('~/components/addOrphanedToSitemap.ascx', Sitemap.orphaned_id, Sitemap.element_type, Sitemap.parent_id, Sitemap.sort_order);
                WMS.loadextrapopUp();
                break;
            case '1': // new page
                loadInnerPopup('~/components/newPageControl.ascx', Sitemap.element_id, Sitemap.element_type, Sitemap.parent_id, Sitemap.sort_order, Sitemap.page_id, inner, orphaned);
                WMS.loadextrapopUp();
                break;
            case '2':  // existing page
                if (!orphaned) {
                    loadInnerPopup('~/components/ExistingPageControl.ascx', Sitemap.element_id, Sitemap.element_type, Sitemap.parent_id, Sitemap.sort_order, Sitemap.page_id, inner);
                    WMS.loadextrapopUp();
                }
                else
                    alert("not allowed to add on existing page within orphaned items");
                break;
            case '3': // new heading
                if (!orphaned) {
                    loadInnerPopup('~/components/newHeadingControl.ascx', Sitemap.element_id, Sitemap.element_type, Sitemap.parent_id, Sitemap.sort_order, Sitemap.page_id, inner);
                    WMS.loadextrapopUp();
                }
                else
                    alert("not allowed to add on new heading within orphaned items");
                break;    // new link
            case '4':
                if (!orphaned) {
                    if (($('.awms_map').length == 0 && Sitemap.parent_id == 0 && $('.componentConfig li.hover').text().indexOf('Domain') == -1 && nav_element_id == '')) {

                        if ($('.awms_map').length == 0 && Sitemap.parent_id == 0 && $('.componentConfig li.hover').text().indexOf('Management') != -1 && nav_element_id == '') {

                            loadInnerPopup('~/components/newLinkControl.ascx', Sitemap.element_id, Sitemap.element_type, Sitemap.parent_id, Sitemap.sort_order, Sitemap.page_id,
                            inner, false, Sitemap.navigation_hierarchy_id);
                            WMS.loadextrapopUp();
                        }
                        else {
                            alert("not allowed to add on a parent node under root directory"); Sitemap.action = 0;
                        }
                    }
                    else {
                        loadInnerPopup('~/components/newLinkControl.ascx', Sitemap.element_id, Sitemap.element_type, Sitemap.parent_id, Sitemap.sort_order, Sitemap.page_id,
                        inner, false, Sitemap.navigation_hierarchy_id);
                        WMS.loadextrapopUp();
                    }
                }
                else
                    alert("not allowed to add on new link within orphaned items");
                break;
            case 5:
                loadInnerPopup('~/components/removeElementsToOrphaned.ascx', Sitemap.sitemap_id);
                WMS.loadextrapopUp();
                break;
        }
    }
};

function selectReplacement(obj){obj.className+=' replaced';var ul=document.createElement('ul');ul.className='selectReplacement';var opts=obj.options;var selectedOpt=(!obj.selectedIndex)?0:obj.selectedIndex;for(var i=0;i<opts.length;i++){var li=document.createElement('li');var txt=document.createTextNode(opts[i].text);li.appendChild(txt);li.selIndex=i;li.selectID=obj.id;li.onclick=function(){selectMe(this)};if(i==selectedOpt){li.className='selected';li.onclick=function(){this.parentNode.className+=' selectOpen';this.onclick=function(){selectMe(this)}}}if(window.attachEvent){li.onmouseover=function(){this.className+=' hover'};li.onmouseout=function(){this.className=this.className.replace(new RegExp(" hover\\b"),'')}}ul.appendChild(li)}obj.onfocus=function(){ul.className+=' selectFocused'};obj.onblur=function(){ul.className='selectReplacement'};obj.onchange=function(){var idx=this.selectedIndex;selectMe(ul.childNodes[idx])};obj.onkeypress=obj.onchange;obj.parentNode.insertBefore(ul,obj);return false;};function selectMe(obj){var lis=obj.parentNode.getElementsByTagName('li');for(var i=0;i<lis.length;i++){if(lis[i]!=obj){lis[i].className='';lis[i].onclick=function(){selectMe(this)}}else{setVal(obj.selectID,obj.selIndex);obj.className='selected';obj.parentNode.className=obj.parentNode.className.replace(new RegExp(" selectOpen\\b"),'');obj.onclick=function(){obj.parentNode.className+=' selectOpen';this.onclick=function(){selectMe(this)}}}};return false;};function setVal(objID,val){var obj=document.getElementById(objID);obj.selectedIndex=val;return false;};

function getScrollPosition(d){
	var scrollX, scrollY; // define scroll position variables
	// Netscape compliant
  if (typeof(window.pageYOffset) == 'number'){
    //alert('first');
    scrollX = window.pageXOffset;
    scrollY = window.pageYOffset;
  }
  // DOM compliant
  else if (document.body && (document.body.scrollLeft || document.body.scrollTop)){
    //alert('second');
    scrollX = document.body.scrollLeft;
    scrollY = document.body.scrollTop;
  }
  // IE6 standards compliant mode
  else if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)){
    //alert('third');
    scrollX = document.documentElement.scrollLeft;
    scrollY = document.documentElement.scrollTop;
  }
  // needed for IE6 (when vertical scroll bar was on the top)
  else {
    scrollX = scrollY = 0;
    //alert('fourth');
    }
  // return scroll position
  if (d == 'X') 
    return scrollX;
  else          
    return scrollY;
}
var actions = {
    initSite: function() {
        $('#middleNav a.site').mouseover(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_config_over.png');
        }).mouseout(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_config.png');
        })
        $('#middleNav a.site img').attr('src', '/awms/images/bg/buttons/icon_config.png');
    },
    initPage: function() { 
        $('#middleNav a.page').mouseover(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_config_over.png');
        }).mouseout(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_config.png');
        })
        $('#middleNav a.page img').attr('src', '/awms/images/bg/buttons/icon_config.png');
    },
    initSitemap: function() {
        $('#middleNav a.sitemap').mouseover(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_sitemap_over.png');
        }).mouseout(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_sitemap.png');
        })
        $('#middleNav a.sitemap img').attr('src', '/awms/images/bg/buttons/icon_sitemap.png');
    },
    initPreview: function() { 
        $('#middleNav a.preview').mouseover(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_preview_over.png');
        }).mouseout(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_preview.png');
        })
        $('#middleNav a.preview img').attr('src', '/awms/images/bg/buttons/icon_preview.png');
    },
    initEdit: function() {
        $('#middleNav a.edit').mouseover(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_edit_over.png');
        }).mouseout(function() {
            $('img', this).attr('src', '/awms/images/bg/buttons/icon_edit.png');
        })
        $('#middleNav a.edit img').attr('src', '/awms/images/bg/buttons/icon_edit.png');
    }
};
// KB FAQ
var FAQ = {
    // KB FAQ control ** 16th June 2009
    loadFaqControl: function(json_comp) {
        Sys.Debug.trace("loadFaqControl");
        Sys.Debug.trace(json_comp);
        //json_comp
        loadUserControlWithJsonComp('~/components/faqComponent.ascx', json_comp);
        WMS.loadPopup();
        return false;
    },
    loadFaqItemPopup: function(faq_id, faq_display_id, cont_comp_id, page_id) {
        Sys.Debug.trace("loadFaqItemPopup " + faq_id + " " + faq_display_id + " " + cont_comp_id);
        $("#extrapopUp").css("width", "760px").css("height", "520px");
        $("#extrapopUp .out").css("height", "450px");
        loadInnerPopup('~/components/faqItemComponent.ascx', faq_id, null, faq_display_id, cont_comp_id, page_id);
        //WMS.loadextrapopUp();
        WMS.loadextraBigPopup();
        return false;
    },
    toggleStatus: function(faq_id, cont_comp_id, page_id) {
        Sys.Debug.trace("toggleStatus FAQ " + faq_id);
        loadInnerPopup('~/components/onlineOffline.ascx', faq_id, 'faq_item', cont_comp_id, null, page_id);
        WMS.loadextrapopUp();
    },
    remove: function(faq_id, cont_comp_id, page_id) {
        Sys.Debug.trace("remove FAQ " + faq_id);
        loadInnerPopup('~/components/delete.ascx', faq_id, 'faq_item', cont_comp_id, null, page_id);
        WMS.loadextrapopUp();
    },
    reload: function(tab) {
        //loadUserControl('~/components/faqComponent.ascx'); 
        isPostback = false;
        $('.awms_nav .setting').hide();
        $('.awms_nav ul.componentConfig li').removeClass('hover');
        $('.awms_nav ul.componentConfig li:eq(' + tab + ')').addClass('hover');
        $('.awms_nav .setting:eq(' + tab + ')').show();
        return false;
    }
}

// WMS.components.PopupEditTab.promoTileEdit 2010-11-11 JT
var PromoTileEdit = {
    showAddPromoTile: function () {
        $('.promoTileEdit .addPromoTile').show();
        $('.promoTileEdit .tileList').hide();
        return false;
    },
    showPromoTileList: function () {
        $('.promoTileEdit .addPromoTile').hide();
        $('.promoTileEdit .tileList').show();
        return false;
    }
}


function init_rte() {
    function initRte() {
        $(".rte-zone").rte({
            cssUrl: "/awms/css/wms.css",
            mediaUrl: "/awms/images/rte/",
            listenToClass: "cmdBtn"
        });
    }
    $(initRte); // on DOM load

    if ($('#popUp h1:first').attr('class') == undefined) return;
    if ($('#popUp h1:first').attr('class').indexOf('awms_tell_a_friend') != -1) {
        $(".rte-zone").css("width", "650px").css("height", "300px").css("overflow", "auto");
    }else
        $(".rte-zone").css("width", "550px").css("height", "250px").css("overflow", "auto");
}

var PopupEditTab = {
    init: function() {
        $('#genericEditPopup ul.componentConfig li').click(function() {
            var index = $(this).parent().children('ul.componentConfig li').index(this);
            //Sys.Debug.trace("index" + index);
            PopupEditTab.activateTab(index);
        });

        //show hide display option
        if ($("#divAvaiDisplayOption").find("table").length > 0) {
            $("#pnlPopupDisplayOption").show();
            $("li.liDisplayOptions:first").show();
        }
        else {
            $("#pnlPopupDisplayOption").hide();
            $("li.liDisplayOptions:first").hide();
        }

        var hfCurrentTabIndexID = PopupEditTab.gethfCurrentTabIndex();
        PopupEditTab.activateTab(hfCurrentTabIndexID.val());
    },
    activateTab: function(index) {
        var popupWrapperInner = $('ul.componentConfig li:eq(' + index + ')').parent().parent();
        popupWrapperInner.children('.displayInner').children('.slider').children('.setting').hide();
        popupWrapperInner.children('ul.componentConfig').children('li').removeClass('hover');
        var hfCurrentTabIndexID = PopupEditTab.gethfCurrentTabIndex();
        hfCurrentTabIndexID.val(index);

        popupWrapperInner.children('ul.componentConfig').children('li:eq(' + index + ')').addClass('hover')
        popupWrapperInner.children('.displayInner').children('.slider').children('.setting:eq(' + index + ')').show();

        return false;
    },
    gethfCurrentTabIndex: function() {
        return $("#wrapperHfCurrentTabIndex input");
    }
}
