/******************************************************************************/
/* Catalog Home Page
/*
/* This file initializes all of the interactive functionality outside of the
/* carousel moevement which is in catalogCarousel.js
/******************************************************************************/
$(document).ready(function(){
   
   // Quick order form
   var orderQuery = $("#quickOrder #query"); // query field
       
   if( !$.trim(orderQuery.val()) ) {
    	 orderQuery.val("Enter Item Number");
    }
	      
    orderQuery.focus(function(){
          
          if($(this).val() == "Enter Item Number")
              $(this).val("");
              
          $(this).removeClass('error');    
     });

	// Validation                    
    $("#quickOrderForm").submit(function(){
   		
  		if(( orderQuery.val() == "Enter Item Number") || ( !$.trim(orderQuery.val()) )){
  			orderQuery.addClass('error').val("Enter Item Number");
  			return false;
  		}
  		else return true;
    });
	
	// Tooltip   
   $("#quickOrder #help").hover(function(){

            $("#helpTip").stop().show();

    },function(){

        $("#helpTip").stop().hide();
        
    })
	// END QUICK ORDER FORM
	
	
   // Carousel Tooltips - display centered over catalog image 
    $("#catItems li a").live( 'mouseover', function(){
                                
                                var thumb   = $(this).children('img');
                                var tooltip = $(this).children('p');
                                var xPos    = ( thumb.height() - tooltip.outerHeight(true) )/2 + parseInt( thumb.css("margin-top"), 10);
                                                                    
                                $(this).children('p').css({"top": xPos + "px"}).css({"display": "block"});
                                
                             });
    $("#catItems li a").live( 'mouseout',  function(){  $(this).children('p').css({"display": "none" }); });                    

   // Modal for request form utilizes JQModal
   $('body').append($('.jqmWindow'));
   $('#requestCatalogForm').jqm({      
      onShow : function(hash){
         hash.w.show();
         var minWindowHeight = hash.w.outerHeight(true) + parseInt(hash.w.css("top").replace("px", ""), 10);
        
        if( minWindowHeight > $(window).height() ){
            /*
              If the window is too small we place the modal 50px from the
              top and use overflow to add scrollbars
            */
            var nheight = $(window).height() - 150; 
            hash.w.css({ height: nheight + "px", top:"50px", overflow: "auto"});
        }
        else { hash.w.css({"height": hash.w.height()  + "px" , overflow: "auto"}); }

        },
        onHide : function(hash){  hash.w.fadeOut('2000',function(){ hash.o.remove(); hash.w.css({"height":"auto"});  }); }
   });

   // Form Validation utilizes Jquery.Validate
   
   /*
     Custom validation method
     required since IT script relies on each catalog having a unique name value
     instead of using an array (ie: catalogs[] )
   */
   jQuery.validator.addMethod("catalogGroup", function(value, element) { return $("#catList input:checked").length > 0; }, "Please select at least one catalog");

   // Assigns all validation rules and form action
   $("#catalogform").validate({
         rules: {
             catalog_0:  {catalogGroup:true},
             catalog_1:  {catalogGroup:true},
             catalog_2:  {catalogGroup:true},
             catalog_3:  {catalogGroup:true},
             catalog_4:  {catalogGroup:true},
             fName:      "required",
             lName:      "required",
             address1:   "required",
             state:      "required",
             city:       "required",
             zipid:      { required:true, digits: true , minlength:5 },
             email:      "email",
             confEmail:  { equalTo: "#edt_email1" }
         },
         messages: {
             fName:      "Please provide your first name",
             lName:      "Please provide your last name",
             address1:   "Please provide your mailing address",
             state:      "Please select a state",
             city:       "Please provide your city",
             zipid:      { digits:"Please enter only numbers", minlength:"Zip code should be 5 digits"},
             confEmail:  { equalTo: "E-mail addresses must match" }
         },
        /* submitHandler: function() {
              $.post('thankyou_10153_12605',  $("#catalogform").serialize(), function(data) {
                  $('#requestCatalogForm div').empty().append(data);
                  //console.log(data);
               });
          },*/
          groups: {
            catalogs: "catalog_0 catalog_1 catalog_2 catalog_3 catalog_4"
         },
         errorPlacement: function(error, element) {
            if (element.attr("type") == "checkbox")
               error.insertAfter("#catalogsCol2");
            else error.insertAfter(element);
         }
   });
});    

