/*
 * Message Store
 * -----------------
 * � The Roundhouse - 2008
 * All Rights Reserved
 */
 
var MessageStore = {
	
	read: function(objXMLResponse, bDoRefresh)
	{
		if(objXMLResponse)
		{
			var arrMessages 	= objXMLResponse.getElementsByTagName('message');
			var arrMessageObjs	= new Array();
                        var strURL              = window.location;
			if(arrMessages)
			{
				for(var i = 0; i < arrMessages.length; i++)
				{
					var objMessage = new Object();
					for(var j = 0; j < arrMessages[i].childNodes.length; j++)
					{
						var strNodeName = arrMessages[i].childNodes[j].nodeName;
						switch(strNodeName)
						{
							case "title":
							case "content":
							case "type": 	objMessage[strNodeName] = arrMessages[i].childNodes[j].firstChild.nodeValue;
											break;
                                                        case "url": strURL = arrMessages[i].childNodes[j].firstChild.nodeValue; break;
						}
					}
					arrMessageObjs.push(objMessage);
				}
			}
			if(arrMessageObjs.length)
				this.__write(arrMessageObjs, bDoRefresh, null, strURL);
		}
	},
	
	__write: function(arrMessageObjs, bDoRefresh, fnCallback, strURL)
	{
		var divHTML = null;
		var divBody = $(document.body);
		var arrHTML = $$('html');
		if(arrHTML.length)
			divHTML = arrHTML[0];
		
		if(divHTML && divBody)
		{
			this.iScrollPos 		= divBody.getScroll().y;
			
			divBody.scrollTo(0,0);
			
			if(!divHTML.hasClass('noscroll'))
				divHTML.addClass('noscroll');
			
			if($('alert'))
				$('alert').destroy();
			
			var divAlert			= new Element('div');
			var frmAlert			= new Element('form');
			var divClicker			= new Element('div');
			var divAlertBox			= new Element('div');
			var divAlertBoxTop		= new Element('div');
			var divAlertBoxMid		= new Element('div');
			var divAlertBoxBottom	= new Element('div');
			var olMessageList		= new Element('ol');
			
			divAlert.setAttribute('id', 'alert');
			//divClicker.setAttribute('href', strURL);
			divClicker.setAttribute('id', 'clicker');
			frmAlert.setAttribute('action', strURL);
			frmAlert.setAttribute('method', 'post');
			divAlertBox.setAttribute('id', 'alertbox');
			/*
			if(!bDoRefresh)
			{
				if(site)
					site.disableAnchor(aClicker);
					
				aClicker.addEvent('click', 	function()
											{
                                                                                         
                                                                                               if ($('i_device') && $('i_device').value !='') {
                                                                                                    
                                                                                                   var req = new Request({
                                                                                                   method: 'get',
                                                                                                   url: $('clicker').get('href'),
                                                                                                   data: { 'i_device' : $('i_device').value }
                               
                                                                                                    }).send();
 
                                                                                                }
                                                                                            
												if($('alert'))
													$('alert').destroy();
												var arrHTML = $$('html');
												var divBody = $(document.body);
												if(arrHTML.length)
													arrHTML[0].removeClass('noscroll');
												
												if($defined(divBody) && $defined(MessageStore) && MessageStore.iScrollPos)
													divBody.scrollTo(0, MessageStore.iScrollPos);
											});
				
				if(fnCallback)
					aClicker.addEvent('click', fnCallback);
			}*/
			
			divAlertBoxTop.addClass('top');
			divAlertBoxMid.addClass('mid');
			divAlertBoxBottom.addClass('bottom');
			
			divAlertBox.adopt(divAlertBoxTop);
			divAlertBox.adopt(divAlertBoxMid);
			divAlertBox.adopt(divAlertBoxBottom);
			
			var bShow = false;
			
			for(var i = 0; i < arrMessageObjs.length; i++)
			{
				var objMessage 	= arrMessageObjs[i];
				var liMessage 	= new Element('li');
				if(i < arrMessageObjs.length - 1)
					liMessage.addClass('breaker');
					
				var divMessageHolder 		= new Element('div');
				var divMessageContainer		= new Element('div');
				var h5MessageTitle			= new Element('h5');
				var pMessageContent			= new Element('p');
				
				h5MessageTitle.set('text', objMessage.title);
				pMessageContent.set('html', objMessage.content);
				divMessageHolder.addClass(objMessage.type);
				divMessageContainer.setAttribute('id', 'message_container');
				divMessageContainer.adopt(h5MessageTitle);
				divMessageContainer.adopt(pMessageContent);
				divMessageHolder.adopt(divMessageContainer);
				
				liMessage.adopt(divMessageHolder);
				olMessageList.adopt(liMessage);
				
				if(objMessage.type == "information")
				{
					bShow = true;
				}
			}
			
			if(bShow)
			{
				var liAlertNoInfo 	= new Element('li');
				var divAlertNoInfo	= new Element('div');
				var fsNoInfo		= new Element('fieldset');
				var inNoInfo		= new Element('input');
				var lbNoInfo		= new Element('label');
				
				divAlertNoInfo.setAttribute('id', 'alert_no_info_holder');
				inNoInfo.setAttribute('type', 'checkbox');
				inNoInfo.setAttribute('id', 'alert_no_info');
				inNoInfo.setAttribute('name', 'alert_no_info');
				inNoInfo.setAttribute('value', '1');
				lbNoInfo.setAttribute('for', 'alert_no_info');
				lbNoInfo.set('text', 'Don\'t show me information messages again');
				
				fsNoInfo.adopt(inNoInfo);
				fsNoInfo.adopt(lbNoInfo);
				divAlertNoInfo.adopt(fsNoInfo);
				liAlertNoInfo.adopt(divAlertNoInfo);
				olMessageList.adopt(liAlertNoInfo);
			}
			
			var liContinue = new Element('li');
			var inContinue = new Element('input');
			inContinue.setAttribute('type', 'image');
			inContinue.setAttribute('src', '/z_images/btns/btn_continue.gif');
			inContinue.setAttribute('alt', 'Submit');
			inContinue.setAttribute('id', 'submit_alert');
			inContinue.setAttribute('name', 'submit_alert');
			inContinue.setAttribute('value', '1');
			
			olMessageList.adopt(inContinue);
			
			divAlertBoxMid.adopt(olMessageList);
			
			if(site && !bDoRefresh)
			{
				site.disableForm(frmAlert);
				frmAlert.addEvent('submit', function()
											{
												if($('alert_no_info') && $('alert_no_info').checked)
													Cookie.write('GCSEPOD_NO_INFO', 'true', {duration:365, domain:document.domain, path:"/"});
												
                                                                                                if ($('i_device') && $('i_device').value !='') {
                                                                                                    
                                                                                                   var req = new Request({
                                                                                                   method: 'get',
                                                                                                   url: $('clicker').get('href'),
                                                                                                   data: { 'i_device' : $('i_device').value }
                               
                                                                                                    }).send();
 
                                                                                                }
                                                                                                            
												if($('alert'))
													$('alert').destroy();
												
												var arrHTML = $$('html');
												var divBody = $(document.body);
												if(arrHTML.length)
													arrHTML[0].removeClass('noscroll');
												
												if($defined(divBody) && $defined(MessageStore) && MessageStore.iScrollPos)
													divBody.scrollTo(0, MessageStore.iScrollPos);
											});
			}
			
			frmAlert.adopt(divAlertBox);
			divAlert.adopt(divClicker);
			divAlert.adopt(frmAlert);
			
			divBody.adopt(divAlert);
                        setTimeout("setupDeviceOptions()", 50);
		}
	}
};

