//MyNotification dojo object inherits the DojoAjax js object from SPETheme.
dojo.declare("MyNotification", DojoAjax, 
{
	
	//the DojoAjax constructor that takes the following parameters:
	//
	// servletUrl - the URL of the AJAX servlet
	// portletUniqueName - the unique name of the portlet
	// portletMode - the mode the portlet is currently in
	// namespace - the encoded namespace of the portlet
	constructor: function(servletUrl, namespace, imagepath, portletUniqueName, portletMode ,rssLink, subsLink)
	{
		this.servletUrl = servletUrl;
		this.portletUniqueName = portletUniqueName;
		this.portletMode = portletMode;
		this.namespace = namespace;
		this.imagepath = imagepath;
		this.rssLink = rssLink;
		this.subsLink = subsLink;
		this.localeSpecificStrings = null;

	},
	makeAjaxCall: function(action)
	{
 		if(action == 'GetProductNoticesAction')
 		{
 			var parms = {"com.ibm.wplc.controller.action": "com.ibm.spenotificationportlet.actions.GetProductNoticesAction",  "com.ibm.wplc.controller.uniqueName": this.portletUniqueName, "com.ibm.wplc.controller.portletMode": this.portletMode,"spe.notification.portlet.namespace":this.namespace};
 		}
 		if(action == 'GetNonSignedInProductsAction')
 		{
 			var parms = {"com.ibm.wplc.controller.action": "com.ibm.spenotificationportlet.actions.GetNonSignedInProductsAction",  "com.ibm.wplc.controller.uniqueName": this.portletUniqueName, "com.ibm.wplc.controller.portletMode": this.portletMode,"spe.notification.portlet.namespace":this.namespace};
 		}
 		this.sendRequest(this.servletUrl,parms,dojo.hitch(this,this.helloCallback));
	},
  	helloCallback: function(data,ioArgs) {
        products=JSON.parse(data,function (key, value) { return value;});
        if(products.length>0)
        {
 		dojo.hitch(this,this.createHTMLContent());
 	}
 	else
 	{
 		dojo.hitch(this,this.createAdditionalLink());
 	}
    },
      createHTMLContent: function()
      {
	var parentDivNode = document.getElementById(this.namespace + "productNoticesDiv");
	//parentDivNode.className = "ibm-container-recent-group-padding";
	var imgNode;
	var imageId;
	var divId;
	var h3node;
	var headerNode;
	var insideDivNode;
	var productDivNode;
	var ul;
	var noticeList;
	var li;
	var a;
	var text;
	var span;
	var spanDate;
	var dateText;
	var des;
	var br;
	var spanBlank;
	var space;
	var moreLinkText;
	var moreLinkDiv;
	var moreLinkA;
	var moreLinkSpan1;
	var moreLinkSpan2;
	var moreLinkSpan1Text;
	var moreLinkSpan2Text;
	var messagesCount;
	var moreLinkDivId ;
	var expandCollapseFunction = dojo.hitch(this, this.expandCollapse);
	var alternateImgPath;

		         
     	for(i=0;i<products.length;i++)
     	{
		productDivNode = document.createElement("div");
		productDivNode.className = "ibm-container-recent-group-padding";
		productDivNode.style.paddingRight= "5px";
		divId = "noticeDiv"+i;
		imageId = +i;
		h3node = document.createElement("h3");
		h3node.setAttribute("id",this.namespace + "showHideImageDiv" +imageId);
		h3node.className = "highContrast";   
		imgNode = document.createElement("img");
		imgNode.setAttribute("id",this.namespace + showHideImageName+imageId);
		imgNode.setAttribute("alt",showHideImageName);
		//If there is only one product , the image to be shown is "-", instead show "+" icon
		if(products.length ==1)
		{
			alternateImgPath = this.imagepath;
			alternateImgPath = alternateImgPath.replace(/plus_icon_sm.gif/, "minus_icon_sm.gif");
			imgNode.setAttribute("src",alternateImgPath);
		}
		else
		{
		imgNode.setAttribute("src",this.imagepath);
		}
		imgNode.style.verticalAlign = "middle";
		imgNode.style.paddingRight = "2px";
		imgNode.style.marginLeft = "-1.5em";
		headerNode = document.createTextNode(products[i].displayName); 
		h3node.appendChild(imgNode);
		h3node.appendChild(headerNode);
		h3node.onclick = function() {expandCollapseFunction(this.childNodes[0],noticeDivName,showHideImageName,false);}

		//add a keypress event listener for accessibility.  set a tab index so that a tab will go to a expand/collapse twistie.
		if (h3node.addEventListener)
		{
			//firefox
			h3node.addEventListener("keypress", function() {expandCollapseFunction(this.childNodes[0],noticeDivName,showHideImageName,false);}, false);	
		}
		else if (h3node.attachEvent)
		{
			//IE
			h3node.attachEvent("onkeypress", function() {expandCollapseFunction(this.childNodes[0],noticeDivName,showHideImageName,false);});
		}
		
		insideDivNode = document.createElement("div");
		insideDivNode.setAttribute("id",this.namespace + divId);
		insideDivNode.className = "ibm-container-body-group";
		//If there is only one product displayed the product should be expanded
		if(products.length ==1)
		{
			insideDivNode.style.display = "block";
		}
		else
		{
		insideDivNode.style.display = "none";
		}
		ul = document.createElement("ul");
		ul.className = "ibm-link-list";
		//Create the password link for My Subscription URL 
		noticeList = products[i].notices;
		messagesCount = noticeList.length;
		for(j=0;j<noticeList.length;j++)
		{
			// This will happen when "No messages available" text needs to be shown for the product
			if(noticeList[j].link =="")
			{
				span = document.createElement("span");
				span.style.fontSize = "0.75em";
				text = document.createTextNode(noticeList[j].title);
				span.appendChild(text);
				ul.appendChild(span);
			}
			else
			{
				li = document.createElement("li");
				//last link is the link to My Subscription URL
				if(j==(noticeList.length - 1))
				{
					li.className = "ibm-password-link";
					a = document.createElement("a");
					a.setAttribute("href",noticeList[j].link);
					a.className="ibm-tooltip-notifications";
					if(messagesCount == 1)
					{
						//This product is not subscribed so show Create subscription link
						//On hover show the full title
						a.setAttribute("title",this.localeSpecificStrings[3]);
						//Show the short title for the messages
						text = document.createTextNode(this.localeSpecificStrings[3]);
					}
					else
					{
						//This product is subscribed so show View/Update subscription link
						//On hover show the full title
						a.setAttribute("title",this.localeSpecificStrings[4]);
						//Show the short title for the messages
						text = document.createTextNode(this.localeSpecificStrings[4]);
					}
				}
				else
				{
					li.className = "ibm-forward-link";
					spanDate = document.createElement("span");
					spanDate.className = "ibm-bullet-list-recent-date";
					dateText = document.createTextNode(noticeList[j].dateModified+":");
					spanDate.appendChild(dateText);
					li.appendChild(spanDate);
				a = document.createElement("a");
				a.setAttribute("href",noticeList[j].link);
				a.className="ibm-tooltip-notifications";
				//On hover show the full title
				//a.setAttribute("title",noticeList[j].longDescription);
				a.setAttribute("title",noticeList[j].title);
				//text = document.createTextNode(noticeList[j].title);
				//Show the short title for the messages
				text = document.createTextNode(noticeList[j].shortTitle);
				}
				a.appendChild(text);
				li.appendChild(a);
				//Commented because the short description/abstract should not be shown in the portlet
				//span = document.createElement("span");
				//span.style.fontSize = "0.75em";
				//des = document.createTextNode(noticeList[j].shortDescription);
				//span.appendChild(des);
				br= document.createElement("br");
				li.appendChild(br);
				//li.appendChild(span);
				ul.appendChild(li);
			}
		}
		insideDivNode.appendChild(ul);
		if(messagesCount>5)
		{
			moreLinkDiv = document.createElement("div");
			moreLinkDiv.setAttribute("id",this.namespace + "showMoreDiv" +"_"+i);
			moreLinkDiv.className = "navigationshowalldynamic";
			moreLinkDiv.style.display = "block";
			moreLinkSpan1 = document.createElement("span");
			moreLinkSpan1.className = "ibm-wrapper";
			//moreLinkSpan1Text = document.createTextNode("[");
			//moreLinkSpan1.appendChild(moreLinkSpan1Text);
			moreLinkA = document.createElement("a");
			moreLinkA.setAttribute("id",this.namespace + "linkMore" + "_"+i);
			moreLinkA.setAttribute("href",products[i].moreLink);
			//moreLinkText= document.createTextNode(products[i].more);
			moreLinkText= document.createTextNode(this.localeSpecificStrings[5]);
			moreLinkA.appendChild(moreLinkText);
			moreLinkDiv.style.display = "block";
			moreLinkSpan2 = document.createElement("span");
			moreLinkSpan2.className = "ibm-wrapper";
			//moreLinkSpan2Text = document.createTextNode("]");
			//moreLinkSpan2.appendChild(moreLinkSpan2Text);
			moreLinkDiv.appendChild(moreLinkSpan1);
			moreLinkDiv.appendChild(moreLinkA);
			moreLinkDiv.appendChild(moreLinkSpan2);
			insideDivNode.appendChild(moreLinkDiv);
		}
		else
		{
			insideDivNode.appendChild(br);
		}
		productDivNode.appendChild(h3node);
		productDivNode.appendChild(insideDivNode);
		parentDivNode.appendChild(productDivNode);
	}

	dojo.byId(this.namespace + "showImageDiv").style.display = "none";
	// The ibm-tooltip-notifications style class uses jQuery. When creating a new html element, it is necessary
	// to bind a jQuery event to that element in order for it work properly. The following line re-binds
	// the ibm-tooltip-notifications class to the jQuery tooltip function.
	jQuery(".ibm-tooltip-notifications").tooltip({showTitle: "||"});
      },
      expandCollapse: function(element,contextDivId, iconId, shouldPersistChoice)
      {

		var passedIconIdCount = element.getAttribute("id");
		var hideShowIcon = dojo.byId(passedIconIdCount);
		var divIdCount = passedIconIdCount;
		divIdCount = divIdCount.replace(iconId,contextDivId);
		var userContext = dojo.byId(divIdCount);

		var hideShowIconSrc = hideShowIcon.src;
		var fileNamePosition = hideShowIconSrc.indexOf("/images");
		var twistieIcon = "";
		if (hideShowIconSrc.substring(fileNamePosition) == "/images/minus_icon_sm.gif")
		{
			twistieIcon = "plus_icon_sm.gif"
			hideShowIcon.src = hideShowIconSrc.replace(/minus_icon_sm.gif/, twistieIcon);
			userContext.style.display = "none";
		}
		else
		{
			twistieIcon = "minus_icon_sm.gif"
			hideShowIcon.src = hideShowIconSrc.replace(/plus_icon_sm.gif/, twistieIcon);
			userContext.style.display = "block";	
		}
      },
      createAdditionalLink: function()
     {
	var mySubscriptionul = dojo.byId(this.namespace + "mySubscriptionURLUL");
	var liRss = document.createElement("li");
	liRss.className="ibm-forward-link";
	var aRss = document.createElement("a");
	aRss.className="ibm-tooltip-notifications";
	aRss.setAttribute("href",this.rssLink);
	//aRss.setAttribute("title",this.rssKey);
	aRss.setAttribute("title",this.localeSpecificStrings[1]);
	//var rssText = document.createTextNode(this.rssKey);
	var rssText = document.createTextNode(this.localeSpecificStrings[1]);
	aRss.appendChild(rssText);
	liRss.appendChild(aRss);
	mySubscriptionul.appendChild(liRss);

	var liSub = document.createElement("li");
	liSub.className="ibm-forward-link";
	var aSub = document.createElement("a");
	aSub.className="ibm-tooltip-notifications";
	aSub.setAttribute("href",this.subsLink);
	//aSub.setAttribute("title",this.subsKey);
	aSub.setAttribute("title",this.localeSpecificStrings[2]);
	//var SubText = document.createTextNode(this.subsKey);
	var SubText = document.createTextNode(this.localeSpecificStrings[2]);
	aSub.appendChild(SubText);
	liSub.appendChild(aSub);
	mySubscriptionul.appendChild(liSub);	
	// The ibm-tooltip-notifications style class uses jQuery. When creating a new html element, it is necessary
	// to bind a jQuery event to that element in order for it work properly. The following line re-binds
	// the ibm-tooltip-notifications class to the jQuery tooltip function.
	jQuery(".ibm-tooltip-notifications").tooltip({showTitle: "||"});

	dojo.byId(this.namespace + "showImageDiv").style.display = "none";	

	
     },
      errorHandler: function(err)
     {

		//this.inherited(arguments);// calling superclass DojoAjax errorHandler
		//display an error message if the the request timed out.
		//if (err.dojoType == "timeout")
		//{
		//	alert("Your request timed out.  Please try again");
		//}
		//call the specified timeout function, then clear the field to indicate there is not timeout function.  it'll be set the
		//next time it's needed by the class that's making the AJAX call (not all AJAX calls need to do something when a timeout occurs).
		//if (this.timeoutFunction != null)
		//{
		//	this.timeoutFunction();
		//	this.timeoutFunction = null;
		//}
		//if the error is a syntax error, it most likely means that the user's session timed out.  issue an error message informing
		//the user of the problem and then reload the page.
		//the message is defined in the SpeMessages.xx.js file in the js folder of the theme
		//if (err.name == 'SyntaxError')
		//{
		//	alert(timeoutMessage);		
		//}
		// Hiding the MySubscription URL and the Spinner Image
		//dojo.byId(this.namespace + "productNoticesDiv").innerHTML = '<span style="font-size:0.75em">' +  this.errorMessage + '</span>';
		
		//This is to fix a strange issue in IE6 , while refreshing the page it throws this error after rendering
		// the html content
		if(!(err.message == 'Object doesn\'t support this property or method'))
		{
			dojo.byId(this.namespace + "mySubscriptionURLDiv").style.display = "none";
			dojo.byId(this.namespace + "showImageDiv").style.display = "none";
			dojo.byId(this.namespace + "productNoticesDiv").innerHTML = '<span style="font-size:0.75em">' +  this.localeSpecificStrings[0] + '</span>';
      		}	
      }	

});