﻿// Global functions for the Web Technology Framework
// Contributors:
//      Kamau Malone
//      Phillip Cave
//      David Wood
//
// Last Update:
//      12/19/2006 - Phillip Cave
//

//-------------------------------------------------------------------/
// Global variables
//-------------------------------------------------------------------/
var _argumentString; // holds comma delimited string that will be passed to InitiateAsyncRequest when updating via ajax
var _ajaxIsActive = false; // make sure only one request is active at a time in internet explorer
var _displayControlCallback; // holds string of javascript to be evaulated after ajax response has ended when adding content to a page

//-------------------------------------------------------------------/
// Opens modal window for content picker
//-------------------------------------------------------------------/
function OpenContentPicker(region, cpUrl, useAjax)
{    
    // prevents 2 requests for starting at the same time
    if (!_ajaxIsActive)
        {
        var win = new Window(region, {className: "alphacube", title: "", width:450, height:500, resizable: false, draggable: false, url: cpUrl, showEffectOptions: {duration:.1}, hideEffectOptions: {duration:0}});
        win.setDestroyOnClose();
        // set callback function to ajax or a refresh
        if (useAjax == 'True')
        {        
            win.setDelegate(RefreshContentRegionViaAjax);
        }
        else
        {
            win.setDelegate(RefreshContentRegion);
        }        
        win.showCenter(true);
    }
    else
    {
        alert("Please wait for the page to finish updating before making another request.");
    }    
}

//-------------------------------------------------------------------/
// Opens modal window for display rules editor
//-------------------------------------------------------------------/
function OpenDisplayRulesEditor(region, drEditorUrl, useAjax)
{    
    // prevents 2 requests for starting at the same time
    if (!_ajaxIsActive)
    {
        var win = new Window(region, {className: "alphacube", title: "", width:450, height:500, zIndex:150, opacity:1, resizable: true, url: drEditorUrl, showEffectOptions: {duration:.1}, hideEffectOptions: {duration:0}})		
	    win.setDestroyOnClose();
	    // set callback function to ajax or a refresh
	    if (useAjax == 'True')
	    {
	        win.setDelegate(RefreshContentRegionViaAjax);
        }
        else
        {
            win.setDelegate(RefreshContentRegion);
        }    
	    win.showCenter(true);    
	}
	else
    {
        alert("Please wait for the page to finish updating before making another request.");
    }   
}

//-------------------------------------------------------------------/
// Opens modal window for delete content confirmation
//-------------------------------------------------------------------/
function RemoveContent(region, drConfirmUrl, useAjax)
{
    var win = new Window(region, {className: "alphacube", title: "", width:300, height:200, zIndex:150, opacity:1, resizable: true, url: drConfirmUrl, showEffectOptions: {duration:.1}, hideEffectOptions: {duration:0}})		
	
	win.setDestroyOnClose();	
	// set callback function to ajax or a refresh
	if (useAjax == 'True')
	{
	    win.setDelegate(RefreshContentRegionViaAjax);
    }
    else
    {
        win.setDelegate(RefreshContentRegion);
    }    
	win.showCenter(true);     
}

//-------------------------------------------------------------------/
// Opens a modal window for page admin panel functions
//-------------------------------------------------------------------/
function OpenPageAdminWindow(action, actionUrl, height, width)
{   
    var win = new Window('PageAdminWin', {className: "alphacube", title: "", width:width, height:height, zIndex:150, opacity:1, resizable: true, url: actionUrl, showEffectOptions: {duration:.1}, hideEffectOptions: {duration:.5}})		
	win.setDestroyOnClose();				
	win.showCenter(true);
}

//-------------------------------------------------------------------/
// Opens a modal window for page publishing
//-------------------------------------------------------------------/
function OpenPagePublishWindow(action, actionUrl, height, width)
{
    var win = new Window('PageAdminWin', {className: "alphacube", title: "", width:width, height:height, zIndex:150, opacity:1, resizable: true, url: actionUrl, showEffectOptions: {duration:.1}, hideEffectOptions: {duration:.5}})		
	win.setDestroyOnClose();			
	win.showCenter(true);
}


//-------------------------------------------------------------------/
// Opens a modal window for emailing pages to friends/colleagues
//-------------------------------------------------------------------/
function OpenEmailPageWindow(actionUrl, height, width)
{   var win = new Window('PageEmail', {className:"alphacube", title: "", width:width, height:height, zIndex:150, opacity:1, resizable:true, url: actionUrl, showEffectOptions: {duration:.1}, hideEffectOptions: {duration:.5}})
    win.setDestroyOnClose();
    win.showCenter(true);
}

//-------------------------------------------------------------------/
// Updates a content region with ajax
//-------------------------------------------------------------------/
var RefreshContentRegionViaAjax = {
    canClose: function(win) {                
        _argumentString = win.getId(); // set the argument string to the id of the region being updated
	    window.setTimeout(InitiateAsyncRequest, 0); // get back on main window thread in firefox
		return true;
    }		
}    

//-------------------------------------------------------------------/
// Updates a content region with a browser refresh
//-------------------------------------------------------------------/
var RefreshContentRegion = {
    canClose: function(win) {
        window.location = window.location;
        return true;
    }
}

//-------------------------------------------------------------------/
// Closes the open modal window
//-------------------------------------------------------------------/
function CloseModalWindow(region)
{            
    parent.Windows.close(region); // region will be passed back in the callback delegate
    //parent.location = parent.location;
}   

//-------------------------------------------------------------------/
// Called when the ajax request to update content region begins
//-------------------------------------------------------------------/
function RegionUpdateStart(sender, arguments)
{
    // stub method. implement if we want to do something fancy    
    _ajaxIsActive = true;      
}

//-------------------------------------------------------------------/
// Called when the ajax request to update content region ends
//-------------------------------------------------------------------/
function RegionUpdateEnd(sender, arguments)
{
    // stup method. implement if we want to do something fancy
    _ajaxIsActive = false;   
    
    // if the content type has a callback defined, evaulate it
    if (_displayControlCallback != '')
    {        
        eval(_displayControlCallback);    
    }
    
    // reset rollovers
    init();
}

//-------------------------------------------------------------------/
// Called when an individul display control is rolled over
//-------------------------------------------------------------------/
function showEditControl(controlId)
{
   var wrapperDiv = $(controlId);
   wrapperDiv.className = "content-region-hover";
   
   var divs = wrapperDiv.getElementsByTagName('div');
   divs = $A(divs);
   
   var editControl = divs.find(function(item){return item.className == 'edit-controls';});  
   
   if (editControl != null) 
   {
       editControl.style.visibility = 'visible';
   }
}

//-------------------------------------------------------------------/
// Called when an individul display control is rolled out
//-------------------------------------------------------------------/
function hideEditControl(controlId)
{
   var wrapperDiv = $(controlId);
   wrapperDiv.className = "content-region";
   
   var divs = wrapperDiv.getElementsByTagName('div');
   divs = $A(divs);
   
   var editControl = divs.find(function(item){return item.className == 'edit-controls';});   
   if (editControl != null)
   { 
       editControl.style.visibility = 'hidden';
   }
}

//-------------------------------------------------------------------/
// Called when a content panel is rolled over
//-------------------------------------------------------------------/
function showPanelAdmin(panelId)
{
    var panelDiv = $(panelId);
    var anchors = panelDiv.getElementsByTagName('a');
    anchors = $A(anchors);
    
    var panelControl = anchors.find(function(item, index){return item.className == 'panel-admin';});
    if (panelControl != null)
    {
        panelControl.className = "panel-admin-hover";
    }
}

//-------------------------------------------------------------------/
// Called when a content panel is rolled out
//-------------------------------------------------------------------/
function hidePanelAdmin(panelId)
{
    var panelDiv = $(panelId);
    var anchors = panelDiv.getElementsByTagName('a');
    anchors = $A(anchors);
    
    var panelControl = anchors.find(function(item, index){return item.className == 'panel-admin-hover';});
    if (panelControl != null)
    {
        panelControl.className = "panel-admin";
    }    
    
}

//Begin Content------------------------------------------------------------------------------
function UpdateSortOrder(contentContainer){
    var contentOrder = [];
    try{
        var children  = contentContainer.childNodes;
        if(children.length > 0){
        for(i = 0; i < children.length; i++){
            if( children[i] && children[i].tagName == 'DIV'){
                var hiddenFields = children[i].getElementsByTagName('INPUT');
                for(j=0; j<hiddenFields.length; j++){
                    if(hiddenFields[j].id.indexOf('txtHiddenContentID') > -1){
                        contentOrder.push(hiddenFields[j].value);
                    }
                }
           }
        }
        //Send the updated ordered list to the server
        var updateContentSortOrder = new Ajax.Request('/ajax/Content.ashx', {method:"post", parameters:"content=" +contentOrder.toJSON()+"&action=SORTCONTENT",onComplete:AfterContentSortOrderUpdate})
    }
    }catch(e){
     
    }
}

function show_add_content_menu(id) {        
    $(id).style.zIndex = 1;
    add_content_divs = $(id).getElementsBySelector('.content-panel-controls');
    add_content_divs.each(function(item) {
        item.style.display = 'block';    
        item.style.zIndex = 10;                          
    });
}

function hide_add_content_menu(id) {
    $(id).style.zIndex = 0;
    add_content_divs = $(id).getElementsBySelector('.content-panel-controls');			
	add_content_divs.each(function(item) {		
		item.style.display = 'none';
	});
}

function show_menu(id) {	
    $(id).style.zIndex = 1;  	    						            
	edit_control_divs = $(id).getElementsBySelector('.content-menu');
	edit_control_divs.each(function(item) {	    
	    item.style.display = 'block';					
		item.style.zIndex = 10;
	});				
}

function hide_menu(id) {
    $(id).style.zIndex = 0;    
	edit_control_divs = $(id).getElementsBySelector('.content-menu');			
	edit_control_divs.each(function(item) {		
		item.style.display = 'none';
	});
}
        
function AfterContentSortOrderUpdate(originalRequest){
}

function addLoadEvent(func) {
	var oldonload = window.onload;
	if (typeof window.onload != 'function') {
		window.onload = func;
	} else {
		window.onload = function() {
			if (oldonload) {
				oldonload();
			}
			func();
		}
	}
}

function init() {   
	var items = document.getElementsByClassName("content-region");
	var item;
	for(i=0;i<items.length;i++){
		item = items[i];				
		item.onmouseover=function() { show_menu(this.id); }
		item.onmouseout=function() { hide_menu(this.id); }
	}
	
	var content_panel_items = document.getElementsByClassName("content-panel");
	var content_panel_item;
	for(i=0;i<content_panel_items.length;i++){
	    content_panel_item = content_panel_items[i];
	    if (content_panel_item != null)
	    {
	        content_panel_item.onmouseover=function() { show_add_content_menu(this.id); }
		    content_panel_item.onmouseout=function() { hide_add_content_menu(this.id); }
		}    
	}
}

// Add onLoadEvent to page. This is only run in edit mode and adds the appropriate mouseover/out events to content objects
addLoadEvent(init);


//End Content------------------------------------------------------------------------------
