HJ.PublishResource = Class.create();

HJ.PublishResource.prototype = Object.extend(new Af.HtmlDialog(), {
      
      initialize: function(title, handler, content) {
         this._initializeDialog(title, handler, content);        
		 this.info = null;
		 this.bName = null;		 
		 this.rankPoint = "0";
		 this.resRublished = false; 
      },

	templateLoaded: function() {
		var ec = new Af.ElementCollection(this.element);              		
		this.editor = new Af.ObjEditor();	 
		this.save = ec.getFirstElementById("save");
		this.armedButton = this.save;
		this.resBusinessName = ec.getFirstElementById("resBusinessName");
		this.resResourceInfo = ec.getFirstElementById("resResourceInfo");		
	    this.resComments = ec.getFirstElementById("resComments");
		this.publishDontSave = ec.getFirstElementById("publishDontSave");
		this.publishSave = ec.getFirstElementById("publishSave"); 

		this.ratingStars = ec.getFirstElementById("ratingStars"); 
		
		this.pubToSeeingStarPage = document.getElementById("pubToSeeingStarPage");	
		this.pubToSeeingStarPage.onclick = this.doShowPoints.bind(this);

		this.rankStatus = document.getElementById("rankStatus");	

		this.firstStar = document.getElementById("firstStar");		   
	    this.secondStar = document.getElementById("secondStar");		   
   	    this.thirdStar = document.getElementById("thirdStar");		   
	    this.fourthStar = document.getElementById("fourthStar");		   
	    this.fifthStar = document.getElementById("fifthStar");		   

	    this.firstStar.onmouseover = this.doFirstStar.bind(this);
	    this.secondStar.onmouseover = this.doSecondStar.bind(this);
	    this.thirdStar.onmouseover = this.doThirdStar.bind(this);
	    this.fourthStar.onmouseover = this.doFourthStar.bind(this);
	    this.fifthStar.onmouseover = this.doFifthStar.bind(this);

		
		this.resCategory = this.editor.addDynamicSelectByEC(ec, "resCategory", new Array(), null, "name", "UUID");       
        
		this.publishSave.onclick = this.doPublishSave.bind(this);
		this.publishDontSave.onclick = this.doPublishDontSave.bind(this);
		this.setInnerHtmlInfo();
		var req = new Af.DataRequest(svcURL, this.requestInfoListCompleted.bind(this), requestFailedCommon.bind(this), null, requestTimedoutCommon.bind(this));
		req.addService("CommunityResourceService", "getResourceCategoryList");
		ajaxEngine.processRequest(req);

	},
	setInfo: function(info, bName) {		
		 this.info = info;
		 this.bName = bName;   		 
		 this.setInnerHtmlInfo();
		 if(this.l != null || this.l != undefined) {
			this.resCategory.setData(this.l);
		 }
	},
	setInnerHtmlInfo: function() {
		if(this.resBusinessName != null) {
		 this.resBusinessName.innerHTML = this.bName; 
		}
		if(this.resResourceInfo != null) {
		 this.resResourceInfo.innerHTML =  this.info;
		}	
		if(this.resComments != null) {
			this.resComments.focus();
		}
	},
	requestInfoListCompleted: function(response) {
		 var xds = new Af.XMLToDataSet(response.responseXML);
	     var list = xds.data["CategoryInfo"];
	     this.l = new Array();
	     var o = new Object();
	     o["name"] = "Choose Category";
	     o["UUID"] = "";
	     this.l.push(o);
	     for (var i=0; i<list.length; i++) {
	        var c = list[i];
	        o = new Object();
			var str = (c["name"]).toString();
	        o["name"] = " "+str.replace("&amp;","&")+" ";	       
	        o["UUID"] = c["UUID"];
	        this.l.push(o);	        
	     }
	     
	     this.resCategory.setData(this.l);
	  },

	doPublishDontSave: function() {
		this.handler.obj["published"] = false;
		this.cleanData();
		this.hide();
	},
	doPublishSave: function() {
		
		if(this.resCategory.value == null || this.resCategory.value == "" ) {
			showMessageDialog("Please select the best category for this resource. ", "Request", 400, 100);
			return;
		}
	/*	if(this.resComments.value == null || this.resComments.value == "" ) {
			showMessageDialog("Please enter comments ", "Error", 400, 100);
			return;
		}		 */
		if(this.rankPoint == "0") {
			showMessageDialog("Please select a star-rating for your resource.  To do this, mouse-over the"+
							" stars to see the ratings for each.<br/>  Click on a star to select the rating.", "Request", 350, 150);
			return;
		}
		this.handler.category = this.resCategory.value;		 
		this.handler.comments = this.resComments.value;					
		this.handler.publishBtnPressed = true;
		this.handler.obj["published"] = "true";	
		this.resRublished = true;
		this.hide();
		this.handler.doSave();
	},
	doSave: function() { 		
		if(this.handler.publishResourceUUID == null || this.handler.publishResourceUUID == undefined ) {
			return;
		}		
		this.resRublished = false;

		 var s = "<messages>";
		  s += "<comment>" + xmlEncode(trim(this.resComments.value)) + "</comment>";		 
		  s += "</messages>";

		var req = new Af.DataRequest(svcURL,this.reqSaveReviewDetailCompleted.bind(this), this.reqFailed.bind(this), null, this.reqTimedOut.bind(this));		   
		req.addService("CommunityResourceService", "saveReviewDetails");		   	
		req.addParameter("UUID", this.handler.publishResourceUUID);		
		req.addParameter("rankPoint", this.rankPoint);	

		req.xmlDoc = s;
		  
		ajaxEngine.processRequest(req);				
		this.cleanData();
		this.handler.publishResourceUUID = null;
	
	},
	reqSaveReviewDetailCompleted: function(response) {			
		this.totPoints = response.responseText;	
		app.thankYouNote.doOpenThankYouDialog(this.totPoints);
	},
	reqFailed: function(dataRequest, msg) {
		showMessageDialog("We are currently unable to save your review. Please try again." + msg, "Excuse us!", 500, 200);
	},

	reqTimedOut: function(dataRequest) {
		showMessageDialog("We are currently unable to save your review. Please try again." , "Excuse us!", 500, 200);
	},	  
	doShowPoints: function() {		
		app.seeingStar.doLearnAboutPoints();
		return;
	},
	  
	doFirstStar: function() {
		this.firstStar.src = "../hj/img/colorstar.gif";

		this.secondStar.src = "../hj/img/blackstar.gif";
		this.thirdStar.src = "../hj/img/blackstar.gif";
		this.fourthStar.src = "../hj/img/blackstar.gif";
		this.fifthStar.src = "../hj/img/blackstar.gif";

		this.rankStatus.innerHTML = "Ugh !";
		this.rankPoint = "1";
	},

	doSecondStar: function() {
		this.firstStar.src = "../hj/img/colorstar.gif";
		this.secondStar.src = "../hj/img/colorstar.gif";
				
		this.thirdStar.src = "../hj/img/blackstar.gif";
		this.fourthStar.src = "../hj/img/blackstar.gif";
		this.fifthStar.src = "../hj/img/blackstar.gif";

		this.rankStatus.innerHTML = "Poor ";
		this.rankPoint = "2";
	},

	doThirdStar: function() {
		this.firstStar.src = "../hj/img/colorstar.gif";
		this.secondStar.src = "../hj/img/colorstar.gif";
		this.thirdStar.src = "../hj/img/colorstar.gif";

		this.fourthStar.src = "../hj/img/blackstar.gif";
		this.fifthStar.src = "../hj/img/blackstar.gif";

		this.rankStatus.innerHTML = "Average ";
		this.rankPoint = "2";
	},

	doFourthStar: function() {
		this.firstStar.src = "../hj/img/colorstar.gif";
		this.secondStar.src = "../hj/img/colorstar.gif";
		this.thirdStar.src = "../hj/img/colorstar.gif";
		this.fourthStar.src = "../hj/img/colorstar.gif";

		this.fifthStar.src = "../hj/img/blackstar.gif";

		this.rankStatus.innerHTML = "Very Good ";
		this.rankPoint = "4";

	},

	doFifthStar: function() {
		this.firstStar.src = "../hj/img/colorstar.gif";
		this.secondStar.src = "../hj/img/colorstar.gif";
		this.thirdStar.src = "../hj/img/colorstar.gif";
		this.fourthStar.src = "../hj/img/colorstar.gif";
		this.fifthStar.src = "../hj/img/colorstar.gif";
		this.rankStatus.innerHTML = "Wow ! ";
		this.rankPoint = "5";
	},
	cleanData: function() {
	   this.resComments.value = "";
	   this.resCategory.value = "";
	   this.firstStar.src = "../hj/img/blackstar.gif";
	   this.secondStar.src = "../hj/img/blackstar.gif";
	   this.thirdStar.src = "../hj/img/blackstar.gif";
	   this.fourthStar.src = "../hj/img/blackstar.gif";
	   this.fifthStar.src = "../hj/img/blackstar.gif";
	    this.rankStatus.innerHTML = "Ugh !";

	},

	hideSelectBoxes: function() {         
		  if (is_ie && !is_ie7 && this.handler != null && this.handler.state != null) {
              this.handler.state.element.style.visibility = "hidden";
		  }
	  },
	  
	  showSelectBoxes: function() {	    
		  if (is_ie && !is_ie7 && this.handler != null && this.handler.state != null) {
              this.handler.state.element.style.visibility = "visible";
		  }
	  }
});
