var browseComponentManager;
/**

browseComponentManager:
   Manages all components for browse section

author: dedmondson
date:   16/01/2009
*/

// Object Def
function  browseComponentManager(){
 

   // Private functions 

	var subscriptionRequestSuccessCallback = function() {
	
   	 $('#QS_SubscriptionBoxDefaultView').fadeOut('normal', function(){ $('.QS_SubscriptionBoxThanksView').fadeIn('normal') } );
   	 window.setTimeout(function() {
   		$('#QS_subscriptionBoxForm').validate().resetForm();
		 $('#QS_subscriptionBoxForm .subscribeFirstName').hide();
	     $('#QS_subscriptionBoxForm .subscribeLastName').hide();
		 $('dd.subscribeFirstName input').val("");
	     $('dd.subscribeLastName input').val("");
	     $('dd.subscribeEmailAddress input').val("");
   		$('.QS_SubscriptionBoxThanksView').fadeOut('normal', function(){ $('#QS_SubscriptionBoxDefaultView').fadeIn('normal') } ); 
   	  }, 5000); 
	   
    }
	
    var subscriptionRequestErrorCallback = function(errorMessages) {
    	sessionController.showErrorOverlay(errorMessages);
    }
	 
   
	 
  // xxxManager: Public functions
  return{
  

	 submitSubscriptionRequest:function(formId) {

  	  var form = $(''+formId);
	
  	  if( form.validate().form()) {
  	    var firstName = '';
  	    var lastName = '';
        if ( form.find('input[name=subscribeFirstName]').length > 0 ) {
          firstName = form.find('input[name=subscribeFirstName]').val();
        }
        if ( form.find('input[name=subscribeLastName]').length > 0 ) {
          lastName = form.find('input[name=subscribeLastName]').val();
        }
  		basketController.subscribe( form.find('input[name=listName]').val(),
  				                    '',
  				                    firstName,
  				                    lastName,
  				                    form.find('input[name=subscribeEmailAddress]').val(),
  				                    form.find('input[name=setting]').val(),
  				                    subscriptionRequestSuccessCallback,
  				                    subscriptionRequestErrorCallback)
  	  }
    },
    
    initValidation:function(){

        $("#QS_subscriptionBoxForm").validate({errorClass: "errorHighlight",
    		errorContainer: "#QS_subscriptionBoxErrorSection",
    		errorLabelContainer: "#QS_subscriptionBoxErrors",
    		onfocusout: false,
    		onclick: false,
    		onkeyup: false,
    		wrapper: "li",
    		highlight: function(element, errorClass) {
    			$(element).addClass(errorClass);
    		},
    		rules: QS_Constants.validation_rules("#QS_subscriptionBoxForm"),
		    messages: QS_Constants.validation_messages
    	});
		
		$("#pageNLForm").validate({errorClass: "errorHighlight",
    		errorContainer: "#pageNLFormErrorSection",
    		errorLabelContainer: "#pageNLFormErrors",
    		onfocusout: false,
    		onclick: false,
    		onkeyup: false,
    		wrapper: "li",
    		highlight: function(element, errorClass) {
    			$(element).addClass(errorClass);
    		},
    		rules: QS_Constants.validation_rules("#pageNLForm"),
		    messages: QS_Constants.validation_messages
    	});
    },
	
	initSubsriptionBox:function(){
	 $('.subscribeFirstName').show();
	 $('.subscribeLastName').show();
	 $('.subscribeEmailAddress').show();
	},
	initConciseSubsriptionBox:function(){
	 $('#subscribeFirstName').show();
	 $('#subscribeLastName').show();
	 $('#subscribeEmailAddress').val("");
	}

	  
  }
}


// On page load
$(function(){
 browseComponentManager = new browseComponentManager();
 browseComponentManager.initValidation();
 $('#QS_SubscriptionBoxThankyouWrapper').hide()
});
$(function() {
$('.QS_toggleSetTitles').hide();

$('.QS_setTitles .QS_hideShowSetTitles').click(function(event) {
	var booksets = $(event.target).parents("div.QS_setTitles");
	if ( booksets.find('.QS_toggleSetTitles').is(":hidden"))
    {
		 
         booksets.find('.QS_toggleSetTitles').slideDown("slow");
         booksets.find('.QS_hideShowSetTitles').html('Hide included titles');
		 booksets.addClass('expanded');
		 booksets.removeClass('collapsed');
    } else {
    	booksets.find('.QS_toggleSetTitles').slideUp("slow");
    	booksets.find('.QS_hideShowSetTitles').html('Show included titles');
		booksets.addClass('collapsed');
		booksets.removeClass('expanded');
    }

		}); 
});


var verticalCatNavList;
function  VerticalCatNavList(){
  return{
   init:function(){	
		var categoryIdentifier = sessionController.getSelectedTopCategory();
		$("li.topCatLink").each(function(n){
		 if(this.id == ("category_"+categoryIdentifier) ){
		   $(this).addClass("selected");
		   $(this).find("ul.subCategories").show();
		 }
		 else{
		   $(this).removeClass("selected");
		   $(this).find("ul.subCategories").hide();
		 }
		});
		
		$("#category_"+categoryIdentifier+ " ul.subCategories").show();	
		
    },
	selectTopCategory:function(categoryIdentifier) {
       sessionController.setSelectedTopCategory(categoryIdentifier);
       $("li.topCatLink").each(function(n){
		 if(this.id == ("category_"+categoryIdentifier) ){
		   $(this).addClass("selected");
		   $(this).find("ul.subCategories").show();
		 }
		 else{
		   $(this).removeClass("selected");
		   $(this).find("ul.subCategories").hide();
		 }
		});
 	
    }
  }
}
$(function(){
 verticalCatNavList = new VerticalCatNavList();
 verticalCatNavList.init();
});


var searchBox;
function  SearchBox(){
  return{
   
	init:function(){
	  var term = $("#searchField").val();
	  
	  if(term == "Search..."){
		  $("#searchField").val("");
	  }
    },
	doSearch:function(term) {
	  if(term){
	    $("#searchField").val(term);
	  }
	  else{
	    term = $("#searchField").val();
	  }
	  if(term && new String(term).length > 0 ){
	  	document.searchForm.submit();
	  }
    }
  }
}
$(function(){
 searchBox = new SearchBox();
});






