Date.prototype.setISO8601 = function (string) {
    var regexp = "([0-9]{4})(-([0-9]{2})(-([0-9]{2})" +
        "(T([0-9]{2}):([0-9]{2})(:([0-9]{2})(\.([0-9]+))?)?" +
        "(Z|(([-+])([0-9]{2}):([0-9]{2})))?)?)?)?";
    var d = string.match(new RegExp(regexp));

    var offset = 0;
    var date = new Date(d[1], 0, 1);

    if (d[3]) { date.setMonth(d[3] - 1); }
    if (d[5]) { date.setDate(d[5]); }
    if (d[7]) { date.setHours(d[7]); }
    if (d[8]) { date.setMinutes(d[8]); }
    if (d[10]) { date.setSeconds(d[10]); }
    if (d[12]) { date.setMilliseconds(Number("0." + d[12]) * 1000); }
    if (d[14]) {
        offset = (Number(d[16]) * 60) + Number(d[17]);
        offset *= ((d[15] == '-') ? 1 : -1);
    }

    offset -= date.getTimezoneOffset();
    time = (Number(date) + (offset * 60 * 1000));
    this.setTime(Number(time));
};
//popup for rss widgets
var widgetPopup={
OffsetX:20, // MODIFY THESE VALUES TO
OffsetY:20, // CHANGE THE POSITION.
popupdiv:null,
init: function(widgetId){
if(this.popupdiv==null){
this.popupdiv = document.createElement("div");
this.popupdiv.setAttribute("id","popupdiv");
this.popupdiv.setAttribute("style","display:none;");
document.getElementById('skip').appendChild(this.popupdiv);
}
},
popup : function(event,node,msg) {
		
		this.popupdiv.innerHTML = msg;
		this.popupdiv.style.display='';	
		var x = (document.getElementById && !document.all) ? event.pageX : event.x + document.body.scrollLeft;
		var y = (document.getElementById && !document.all) ? event.pageY : event.y + document.body.scrollTop;
		if(document.all){
			this.popupdiv.style.left= returnCoord(event,'x');
			this.popupdiv.style.top= returnCoord(event,'y');
		}
		else{
			this.popupdiv.style.left=x+this.OffsetX+'px';
			this.popupdiv.style.top=y+this.OffsetY+'px';
		}
		
		},
popuptext : function(event,node) {
		var msg='';
		if(node.parentNode.lastChild.childNodes.length==1){
			msg=node.parentNode.lastChild.childNodes[0].innerHTML;
		}
		else{
			msg=node.parentNode.lastChild.innerHTML;
		}
		if(msg!=''){
		var content = "<div class=\"newsPopup\">" + msg + "</div>";
		this.popupdiv.innerHTML = content;		
		this.popupdiv.style.display='';	
		var x = (document.getElementById && !document.all) ? event.pageX : event.x + document.body.scrollLeft;
		var y = (document.getElementById && !document.all) ? event.pageY : event.y + document.body.scrollTop;
		if(document.all){
			this.popupdiv.style.left= returnCoord(event,'x');
			this.popupdiv.style.top= returnCoord(event,'y');
		}
		else{
			this.popupdiv.style.left=x+this.OffsetX+'px';
			this.popupdiv.style.top=y+this.OffsetY+'px';
		}
		
		}
},
remove_popup : function (){
		this.popupdiv.style.display='none';
		//this.popupdiv.setAttribute("style","display:none;");
}
}

function returnCoord(e,coord) {
  var xcoord, ycoord;
  if( !e ) { e = window.event; }
  if( !e ) { return; }
  if( typeof( e.pageX ) == 'number' ) {
    xcoord = e.pageX;
    ycoord = e.pageY;
  } else if( typeof( e.clientX ) == 'number' ) {
    xcoord = e.clientX;
    ycoord = e.clientY;
    if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
      xcoord += document.body.scrollLeft;
      ycoord += document.body.scrollTop;
    } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
      xcoord += document.documentElement.scrollLeft;
      ycoord += document.documentElement.scrollTop;
    }
  } else { return; }
  if(coord=='x'){
  	return xcoord;
  }
  else{
  	return ycoord;
  }
  //window.alert('Mouse coordinates are ('+xcoord+','+ycoord+')');
}



//check if panel is not toolbox
function isNotToolbox(widgetId){
	var panel=document.getElementById('iw-'+widgetId.substring(6)).parentNode.id;
	if(panel=='toolBoxSource'){
		return false;
	}
	else{
		return true;
	}
}

var rssFead={
title :new Object(),
news :new Object(),
link :new Object(),
feedURL:'',
rows:'',
expandRss:'',
items:null,
init:function(itemStr,id){
widgetPopup.init();
this.items=eval("("+itemStr.substring(8, itemStr.length - 1)+")");
this.news=document.getElementById('news'+id);
this.title=document.getElementById('title'+id);
this.link=document.getElementById('link'+id);
this.feedURL=this.items[0].value;
this.rows=this.items[2].value;
this.expandRss=this.items[1].value;
this.loadRSS(this.feedURL,this.rows,this.title,this.news,this.link,this.expandRss);
},
loadRSS : function(feedURL, rows, title, news, link,expandRss) {
		dojo.xhrGet( {

			url :"http://" + document.location.hostname + "/servlet/RssAtomFeed",
			handleAs :"text",
			content : {
				feedid :feedURL
			},
			timeout :15500,

			load : function(response, ioArgs) {
			news.innerHTML='';
			var jsonObject = eval("(" + unescape(response) + ")");
			title.innerHTML='';
			if (jsonObject.image.url != null) {
				var img = document.createElement("img");
				img.setAttribute("src", jsonObject.image.url);
				title.appendChild(img);
			}
				var anch = document.createElement("a");
				if (jsonObject.link != null) {
					anch.setAttribute("href", jsonObject.link);
					anch.setAttribute('target', '_blank');
				}
				var text = document.createTextNode(jsonObject.title);
				anch.appendChild(text);
				link.innerHTML='';
				link.appendChild(anch);
				title.appendChild(link);

			
			var numRows=0;
			rows= (rows=='')? jsonObject.entries.length:rows;
			if(jsonObject.entries.length < (Number(rows))){
				numRows=jsonObject.entries.length;
			}
			else{
				numRows=(Number(rows));
			}	
			
			for ( var i = 0; i < numRows; i++) {
				var date = new Date();
				if(jsonObject.entries[i].publishedDate!=null){
					date = new Date(jsonObject.entries[i].publishedDate);
				//	if(isNaN(date)){
					//	date.setISO8601(jsonObject.items[i].date);
					//}
				}
				var entrydiv = document.createElement("div");
				entrydiv.className="newsEntry";
				var timediv = document.createElement("div");
				timediv.className="newsTime";
				timediv.innerHTML = date.toLocaleTimeString();
				entrydiv.appendChild(timediv);
				var a = document.createElement("a");
				if (jsonObject.entries[i].link != null) {
					a.setAttribute("href", jsonObject.entries[i].link);
					a.setAttribute('target', '_blank');
				}
				var text = document.createTextNode(jsonObject.entries[i].title);
				a.appendChild(text);
				entrydiv.appendChild(a);
				var expanddiv = document.createElement("div");
				if(expandRss=='true'){
					expanddiv.className="expand";
				}
				else{
					expanddiv.className="collapse";
				}
				var newsContentDiv = document.createElement("div");
				newsContentDiv.className="newsContentDiv";		
				newsContentDiv.innerHTML = jsonObject.entries[i].description.value;
				
				if(expandRss!='true'){
					if(document.all){
						a.onmouseover = new Function("widgetPopup.popuptext(event,this)");
						a.onmousemove = new Function("widgetPopup.popuptext(event,this)");
						a.onmouseout = new Function("widgetPopup.remove_popup()");
					}
					else{
						a.setAttribute("onmouseover","widgetPopup.popuptext(event,this)");
						a.setAttribute("onmousemove","widgetPopup.popuptext(event,this)");
						a.setAttribute("onmouseout","widgetPopup.remove_popup()");
					}	
				}				
				expanddiv.appendChild(newsContentDiv);
				var images=newsContentDiv.childNodes;
				var image=null;
				var newsImageDiv=document.createElement("div");
				newsImageDiv.className="newsImageDiv";
				expanddiv.appendChild(newsContentDiv);
				for(k=0;k < images.length;k++){
					if(images[k].nodeName=='IMG'){
							if(images[k].getAttribute('height')=="1"){
							try{
								newsContentDiv.removeChild(images[k]);
							}
							catch(e){
								
							}
						}							
						else if(images[k].src.indexOf('jpg')!=-1|images[k].src.indexOf('gif')!=-1|images[k].src.indexOf('png')!=-1){
							image=images[k];
							if(document.all){
								image.onmouseover = new Function("widgetPopup.popup(event,this,'<img src=\""+image.src+"\" style=\"width:200px;height:auto;border: 1px solid #858585;\"/>')");
								image.onmousemove = new Function("widgetPopup.popup(event,this,'<img src=\""+image.src+"\" style=\"width:200px;height:auto;border: 1px solid #858585;\"/>')");
								image.onmouseout = new Function("widgetPopup.remove_popup()");
							}
							else{
								image.setAttribute("onMouseOver","widgetPopup.popup(event,this,'<img src=\""+image.src+"\" style=\"width:200px;height:auto;border: 1px solid #858585;\"/>')");
								image.setAttribute("onMouseMove","widgetPopup.popup(event,this,'<img src=\""+image.src+"\" style=\"width:200px;height:auto;border: 1px solid #858585;\"/>')");
								image.setAttribute("onMouseOut","widgetPopup.remove_popup()");
							}	
							newsImageDiv.appendChild(image);
							expanddiv.appendChild(newsImageDiv);
							try{
								newsContentDiv.removeChild(images[k]);
							}
							catch(e){
								
							}								
						}
						else{
							newsContentDiv.removeChild(images[k]);
						}	
					}	
				}	
				
				entrydiv.appendChild(expanddiv);
				news.appendChild(entrydiv);
				
			}			
			return response;
		},

			error : function(response, ioArgs) {
				console.error("HTTP status codex: ", ioArgs.xhr.status);
			}
		});
	}
}

