var browseManager;

/**

browseManager:
   Section specific file for browse behaviour

author: dedmondson
date:   17/01/2009
*/

// Object Def
function  browseManager(){
 

   // Private functions 
    var addProductSuccessCallback = function(){
	
	 }
	 
	 var addProductErrorCallback = function(errorMessages){
	   sessionController.showErrorOverlay(errorMessages);
	 }
  
	 var sentToFriendSuccessCallback = function(){
		 $('#friendForm').fadeOut('normal', function(){ $('#QS_friendThankyou').fadeIn('normal') } );
     }
		 
     var sentToFriendErrorCallback = function(errorMessages){
    	 sessionController.showErrorOverlay(errorMessages);
     }
     var sentReviewSuccessCallback = function(){
		 $('#reviewForm').fadeOut('normal', function(){ $('div.QS_reviewThanks').fadeIn('normal') } );
     }
		 
     var sentReviewErrorCallback = function(errorMessages){
    	 sessionController.showErrorOverlay(errorMessages);
     }
	 
	 var basketNotification = function(catEntryId) { 
		 /*
		 $('div#QS_basketNotification_'+catEntryId+':hidden').stop().fadeIn(100).animate({top:"-=20px"},100).animate({top:"+=20px"},100).animate({top:"-=10px"},100)
    	 .animate({top:"+=10px"},100).animate({top:"-=3px"},100).animate({top:"+=3px"},100).animate({dummy:1},1000).fadeOut('slow');
    	 */
		 $('div#QS_basketNotification_'+catEntryId+':hidden').stop().fadeIn('slow').animate({dummy:1},3000).fadeOut('slow')
	 }
 
  // Browse: Public functions
  return{
  
    addMultipleProduct:function(catEntryId){
	  basketNotification(catEntryId);
      var qtyFieldSelector = '.QS_addMultipleProductQty_'+catEntryId;
      var qty = $(qtyFieldSelector).val();
      this.addProduct(catEntryId, qty); 
    },
    
    addSingleProduct:function(catEntryId){
  	  basketNotification(catEntryId);
      this.addProduct(catEntryId, 1);  
    },
    
    addProduct:function(catEntryId, qty){
      sessionController.addProduct(catEntryId, qty, addProductSuccessCallback , addProductErrorCallback);
    },
           
    submitSendToAFriend:function() {
      var form = $('#QS_sendToAFriendForm');
      if( form.validate().form()) {
    	basketController.sendToAFriend( form.find('input[name=title]').val(),
    									form.find('input[name=author]').val(),
    									form.find('input[name=ourPrice]').val(),
    									form.find('input[name=rrp]').val(),
    									form.find('input[name=save]').val(),
    									form.find('input[name=urlInEmail]').val(),
    									form.find('input[name=thumbnail]').val(),
    									form.find('input[name=fromname]').val(), 
    									form.find('input[name=toname]').val(), 
    									form.find('input[name=fromemail]').val(), 
    									form.find('input[name=toemail]').val(), 
    									form.find('textarea[name=emailMessage]').val(), 
 		       		 				    form.find('input[name=sendMeACopy]').val(),
 		       		 				    sentToFriendSuccessCallback,
 		       		 				    sentToFriendErrorCallback)
      }
    },
    
  /// start to modify review form
    submitReviewForm:function() {
        var reviewForm =  $('#QS_reviewForm');
        
        if( reviewForm.validate().form()) {
        	
        	var rating = reviewForm.find('select[name=rating]').val();
        	if (!rating) {
        		rating = '5';
        	}
        	
          	basketController.postReview( 
          			     reviewForm.find('input[name=productIds]').val(),
          			     reviewForm.find('input[name=productId]').val(),
	          			 reviewForm.find('input[name=itemName]').val(),
			      		 reviewForm.find('input[name=itemMaker]').val(),
			      		 reviewForm.find('input[name=productFeature]').val(),
			      		 reviewForm.find('input[name=reviewStatusRef]').val(),
			      		 reviewForm.find('input[name=reviewTypeRef]').val(),
			      		 rating,
			      		 reviewForm.find('input[name=foreName]').val(),
			      		 reviewForm.find('input[name=emailAddress]').val(), 
			      		 reviewForm.find('input[name=reviewTitle]').val(), 
			      		 reviewForm.find('textarea[name=reviewText]').val(), 
					     sentReviewSuccessCallback,
	 				     sentReviewErrorCallback)

        }
      },
      /// end section 1
    
    initValidation:function(){
        $("#QS_sendToAFriendForm").validate({errorClass: "errorHighlight",
			errorContainer: "#QS_sendToAFriendErrorSection",
			errorLabelContainer: "#QS_sendToAFriendErrors",
			onfocusout: false,
			onclick: false,
			onkeyup: false,
			wrapper: "li",
			highlight: function(element, errorClass) {
				$(element).addClass(errorClass);
			},
			rules: {
			     fromname:{
			    	 required: true,
					  maxlength: 100
				},
				toname:{
			    	 required: true,
					  maxlength: 100
				},
				fromemail:{
					required:true,
					  email: true,
					  maxlength:100
				},
				toemail:{
					required:true,
					  email: true,
					  maxlength:100
				},
				emailMessage:{
					required:true,
					maxlength: 1000
				}
			},
			messages: {
			
			    fromname:{
			    	 required:"Your name is required to send.",
			    	 maxlength:"Forename must not exceed 100 characters in length."
			     },
			     toname:{
			    	 required:"Your friend's name is required.",
			    	 maxlength:"Surname must not exceed 100 characters in length."
			     },
			     fromemail:{
			    	 required:"Please enter a valid email address.",
			    	 email:"The email you entered seems to have an error, please re-enter.",
			    	 maxlength:"The length of the email address cannot exceed 100 characters."
			     },
			     toemail:{
			    	 required:"Please enter a valid email address.",
			    	 email:"Your friend's email seems to have an error, please re-enter.",
			    	 maxlength:"The length of the email address cannot exceed 100 characters."
			     },
			     emailMessage:{
			    	 required:"Please supply a message to send to your friend.",
			    	 maxlength:"Message cannot contain more then 1000 characters."
			     }
		    }
		});
        
/// start to modify review form
       
         
              $("#QS_reviewForm").validate({errorClass: "errorHighlight",
      			errorContainer: "#QS_reviewFormErrorSection",
      			errorLabelContainer: "#QS_reviewFormErrors",
      			onfocusout: false,
      			onclick: false,
      			onkeyup: false,
      			wrapper: "li",
      			highlight: function(element, errorClass) {
      				$(element).addClass(errorClass);
      			},
      			rules: {
      			     rating:{
      			    	 required: true
      					  
      				},
      				foreName:{
      			    	 required: true,
      					  maxlength: 100
      				},
      				emailAddress:{
      					required:true,
      					  email: true,
      					  maxlength:100
      				},
      				reviewTitle:{
      					required:true,
      					maxlength:100
      				},
      				reviewText:{
      					required:true,
      					maxlength: 1000
      				}
      			},
      			messages: {
      			
      			    rating:{
      			    	 required:"Please choose a rating from 1 to 5 stars."
      			    	
      			     },
      			     foreName:{
      			    	 required:"Your name is required for this review.",
      			    	 maxlength:"Your name not exceed 100 characters in length."
      			     },
      			     emailAddress:{
      			    	 required:"Please enter a valid email address.",
      			    	 email:"The email you entered seems to have an error, please re-enter.",
      			    	maxlength:"The length of the email address cannot exceed 100 characters."
      			     },
      			     reviewTitle:{
      			    	 required:"Please give a title to your review.",
      			    	 maxlength:"Your title cannot exceed 100 characters."
      			     },
      			     reviewText:{
      			    	 required:"Please supply comments to your review.",
      			    	 maxlength:"Your review cannot contain more then 1000 characters."
      			     }
      		    }
      		});
/// end modify review form
    	
    }
    
    
  }

}

// On page load
$(function(){
 browseManager = new browseManager();
 browseManager.initValidation();
 $('#QS_friendThankyou').hide();
 $('.reviewThanks').hide();
 $('.QS_basketNotification').hide();
});
       
