﻿/// <reference name="MicrosoftAjax.js"/>

var allPageTags = new Array(); 

function onDrop( sender, e )
{
    var container = e.get_container();
    var item = e.get_droppedItem();
    var position = e.get_position();
    
    //alert( String.format( "Container: {0}, Item: {1}, Position: {2}", container.id, item.id, position ) );
    
    var widgets = getElementsByClass("widget", document.getElementById("leftSide"));
    var newsfeeds = "";
    for(var i = 0; i < widgets.length; i++)
        newsfeeds += widgets[i].getAttribute("NewsFeedId")+ ",";
    
    newsfeeds = newsfeeds.substring(0, newsfeeds.length-1);
    
    //Save that array to the cookie
    createCookie("HomePageOrderLeft",newsfeeds,365);
    //alert(newsfeeds);
    widgets = getElementsByClass("widget", document.getElementById("rightSide"));
    newsfeeds = "";
    for(var i = 0; i < widgets.length; i++)
        newsfeeds += widgets[i].getAttribute("NewsFeedId")+ ",";
    
    newsfeeds = newsfeeds.substring(0, newsfeeds.length-1);
    
    //Save that array to the cookie
    createCookie("HomePageOrderRight",newsfeeds,365);
}


function createCookie(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=/";
}

function getElementsByClass(searchClass,node,tag) {
	var classElements = new Array();
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp('(^|\\\\s)'+searchClass+'(\\\\s|$)');
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function $hide(id)
{
    document.getElementById(id).style.display="none";
}

function closeAndRedirect(newUrl) {
 	$("#TB_imageOff").unbind("click");
	$("#TB_closeWindowButton").unbind("click");
	$("#TB_window").fadeOut("fast",function(){$('#TB_window,#TB_overlay,#TB_HideSelect').trigger("unload").unbind().remove();});
	$("#TB_load").remove();
	$("body","html").css({height: "auto", width: "auto"});
	$("html").css("overflow","");

	document.onkeydown = "";
	document.onkeyup = "";
	
	if(newUrl != null && newUrl != "" && newUrl != "refresh")
	    window.location = newUrl;
	else if (newUrl != null && newUrl == "refresh")
	    location.reload(true);
}

function pageUnload()
{
    
}

function showHelp()
{
    var request = new Sys.Net.WebRequest();
    request.set_httpVerb("GET");
    request.set_url('help.aspx');
    request.add_completed( function( executor )
    {
        if (executor.get_responseAvailable()) 
        {
            var helpDiv = $get('HelpDiv');
            var helpLink = $get('HelpLink');
            
            var helpLinkBounds = Sys.UI.DomElement.getBounds(helpLink);
            
            helpDiv.style.top = (helpLinkBounds.y + helpLinkBounds.height) + "px";
            
            var content = executor.get_responseData();
            helpDiv.innerHTML = content;
            helpDiv.style.display = "block";                       
        }
    });
    
    var executor = new Sys.Net.XMLHttpExecutor();
    request.set_executor(executor); 
    executor.executeRequest();
}

var Utility = 
{
    // change to display:none
    nodisplay : function(e) 
    { 
        if( typeof e == "object") e.style.display = "none"; else if( $get(e) != null ) $get(e).style.display = "none"; 
    },
    // change to display:block
    display : function (e,inline) 
    { 
        if( typeof e == "object") e.style.display = (inline?"inline":"block"); else if( $get(e) != null ) $get(e).style.display = (inline?"inline":"block"); 
    },
    getContentHeight : function()
        {
        if( document.body && document.body.offsetHeight ) {
            return document.body.offsetHeight;
        }
    },


    blockUI : function()
    {
        Utility.display('blockUI');
        var blockUI = $get('blockUI');
    
        if( blockUI != null ) // it will be null if called from CompactFramework
        blockUI.style.height = Math.max( Utility.getContentHeight(), 1000) + "px";    
    },

    unblockUI : function()
    {
        Utility.nodisplay('blockUI');
    }
};

var DeleteWarning =
{
    yesCallback : null,
    noCallback : null,
    _initialized : false,
    init : function()
    {
        if( DeleteWarning._initialized ) return;
        
        var hiddenHtmlTextArea = $get('DeleteConfirmPopupPlaceholder');
        var html = hiddenHtmlTextArea.value;
        var div = document.createElement('div');
        div.innerHTML = html;
        document.body.appendChild(div);
        
        DeleteWarning._initialized = true;
    },
    show : function( yesCallback, noCallback )
    {
        DeleteWarning.init();
        
        Utility.blockUI();
        
        var popup = $get('DeleteConfirmPopup');
        Utility.display(popup);
        
        DeleteWarning.yesCallback = yesCallback;
        DeleteWarning.noCallback = noCallback;
        
        $addHandler( $get("DeleteConfirmPopup_Yes"), 'click', DeleteWarning._yesHandler );
        $addHandler( $get("DeleteConfirmPopup_No"), 'click', DeleteWarning._noHandler );
    },
    hide : function()
    {
        DeleteWarning.init();
        
        var popup = $get('DeleteConfirmPopup');
        Utility.nodisplay(popup);
        
        $clearHandlers( $get('DeleteConfirmPopup_Yes') );
        
        Utility.unblockUI();
        
    },
    _yesHandler : function()
    {
        DeleteWarning.hide();
        DeleteWarning.yesCallback();    
    },
    _noHandler : function()
    {
        DeleteWarning.hide();
        DeleteWarning.noCallback();
    }
};

