/* 

	jquery.custom.js
	========================================
	Handles some of the custom jQuery 
	functionality for ColsenKeane.com and 
	should be loaded on every page
	========================================
	

*/
$(document).ready(function(){
		
	/* 
	
		add a dismiss-button to any div with a
		class of dismissable.  

	*/

		$("#extra-code .dismiss-button").clone().appendTo(".dismissable");


	/* 
	
		handles closing message, error, or warning
		windows when the dismiss-button icon
		is clicked by the user
	
	*/

		$(".dismissable .dismiss-button").click(function(){
			
			$(this).fadeOut(25).parent(".dismissable").slideUp(500);	
		});




	/* 
	
		element hover functionality.  adds 'hovered' class to any
		hovered element with class 'hoverable'
		
	*/
	

 
    $(".hoverable").hover(
        function () {
            $(this).addClass("hovered");
        },
        function () {
            $(this).removeClass("hovered");
        
        }
    );
 
 
    $(".hoverable").click(function() {
        
        var newPage = $(this).find("a:first").attr("href"); 
        
		if (newPage) {
			window.location = newPage;
		} else {
			return false;
		}        

    
    }); 
 
    // handle initials stuff...
    $('#option-initials').keyup(function(){
        $('#option-final-initials').val('');
        $('#options-submit').attr('disabled','disabled');
        var initials = $(this).val();
        var pattern = /^[A-Z]{1,3}$/;
        var search = initials.search(pattern);
       // console.log(search);
        if (initials.length == 0) {
            clearError('initials-error');
            $('#options-submit').attr('disabled','');
        } else if (initials.length > 3) {
            setError("Please limit your initials to a maximum of three characters",'initials-error');
            
        } else if (search != 0){
            setError("Please use all capital Alphabetical characters only",'initials-error');
        } else {
            clearError('initials-error');
            if (initials.length > 0) {
            var newValue = initials+'{p+15.00}'
                $('#option-final-initials').val(newValue);
                $('#options-submit').attr('disabled','');
            }
            
            
            
        }
        
    });

 
 	// handle product option select changes....
 	$(".option-select").change(function(){
 		
 		// text of the current CSS ID of the SELECT element	
 		var currentID		= $(this).attr('id');
 	
 		// Current ID with #added
 		var selID			= "#"+currentID;
		// CSS ID for the info box we will need to change (i.e. option image change)
		var infoID			= selID+"-info";
 		
 		// all of the select OPTIONS
 		var allOpts			= selID+" option";
 		
 		// the CURRENTLY selected OPTION
 		var currentOpt		= allOpts+":selected";

		// the INDEX of the CURRENTLY SELECTED OPTION 		
 		var currentIdx		= $(allOpts).index($(currentOpt));
 		//console.log(currentIdx);
 		
		var infoActive	= infoID+' .current';
		
		var infoNext		= infoID+' .option-info-'+currentIdx+'';
		//console.log("infoNext: "+infoNext);


		$(infoActive).removeClass('current'); 		
		$(infoNext).addClass('current');

 	});
 

    function setError(errText,errContainer) {
        var containerLabel = "#"+errContainer;
        var container = $(containerLabel);
        
        container.html(errText);
        container.slideDown();
    
    
    }
    
    function clearError(errContainer) {
        var containerLabel = "#"+errContainer;
        $(containerLabel).slideUp();
    }
    
    /* Figure out if the Referral box has been set to Other */
    $('#referrerSelect').change(function(){
        var ref = $(this).val();
        if (ref=='other') {
            
            var field = $('#other-text-input').clone();
            $("#OtherSpec").prepend(field);
            
            $('#OtherSpec div').slideDown(100);
            
        } else {
            $('#OtherSpec *').slideUp(100,function(){
                $('#OtherSpec').html('');
            });
            
        }
    }
    );
    
    
    
    
    
    


});
