function ApplicationController(){
	var lastHashVal;

	this.location_watcher = function() {
    		var hash = location.hash.substring(1);

    		if (hash == lastHashVal) return;
    		else lastHashVal = hash;

    		var hashdict = StatusManager.strToDict(hash);
    		for (var key in hashdict) {
			var now = StatusManager.get(key);
        		if (!now || now != hashdict[key]) {
            			if (key != 'current_tab') {
               				// -1 defers history rewrite
                			StatusManager.set(key, hashdict[key], -1);
            			}
       			}
    		}

    		// set current_tab last
    		var key = 'current_tab';
    		if (hashdict[key]) {
        		StatusManager.set(key, hashdict[key]);
    		}
	}
	
  if (typeof CommunicationManager == 'undefined') CommunicationManager = new CommunicationManagerSingleton();
  if (typeof StatusManager == 'undefined') StatusManager = new StatusManagerSingleton();
  CookieManager = new CookieManagerBase();
  ReportingManager = new $.ReportingManager();

  if(CookieManager.get("campaign_id")) {
	var id = CookieManager.get("campaign_id");
	var div = "welcome member_greetings_library";
	CommunicationManager.get("/campaign?campaign_id="+id, "campaign", displayCampaignMessage);
  }

    var initvalues = StatusManager.getInitValues();
    if (initvalues['current_tab']){
        StatusManager.set('current_tab', initvalues['current_tab']);
    }else{
        StatusManager.set("current_tab", "library", true);
    }
    setInterval( this.location_watcher, 100 );
    if(typeof $.preloadCssImages != "undefined"){ 
        var preload = $.preloadCssImages();
    }
}

function displayCampaignMessage(xmlBody, messageType, targetDiv) {
	var ele = xmlBody.documentElement;
	if(ele)
	{
		var message = getMessage(ele.getAttribute("welcome_message"));
		StatusManager.set("welcome_message", message);
		$("div.welcome p").html(message);
		
message = getMessage(ele.getAttribute("search_message"));
		 StatusManager.set("search_message", message);
		$("p#search_p").html("<span style='color:#990033;font-weight:bold;'>"+message + "</span><br /><br />" + $("p#search_p").html());
		
		message = getMessage(ele.getAttribute("digi_comp_message"));
		StatusManager.set("digi_comp_message", message);
	}
}

function getMessage(m){
	if(m.indexOf("[CDATA[") > -1)
		return  m.substring(9, m.length -3);
	else
		return m;
}

$(document).ready(function() {
    PortalController = new ApplicationController();
});
