/* Author: Brian Fletcher
*/

kc=window.kc || {};

kc.common = (function() {
    
    // private
    function init() {
        //initialize common code
        fixIE();
        initContactForm();
        initFancyBox();
    }

    function fixIE(){
        if(!document.getElementsByTagName('html')[0].className.match(/ie\d/gi)) return; // looking for ie6,ie7,ie8
        // adds a class (.third) to every third LI for IE since it doesn't handle nth-child selectors
        $('.portfolio-list').each(function(){
           var $kids = $(this).children('li:nth-child(3n+3)');
           $kids.addClass('third');    
        });
    }
    
    function initContactForm(){
        var $form = $('form#contact'),
            $inputs = $form.find('input, textarea'); 
        if(!$form.size) return;   
        
        // custom validator method for default values
        jQuery.validator.addMethod("defaultInvalid", function(value, element) {
             switch (element.value) {
              case "Name":
               if (element.name == "name")
                  return false;
                  break;
              case "Email":
                if (element.name == "email")
                  return false;
                  break;
              case "Inquiry":
                if (element.name == "message")
                  return false;
                  break;
              default: 
                  return true;
             }
         });
        
        // validate using the validate plugin
        $form.validate({
            errorElement: "p",
            errorPlacement: function(error, element) {
                 error.appendTo( element.parent() );
            },
            rules: {
                   name: "required defaultInvalid",
                   email: "required email defaultInvalid",
                   message: "required defaultInvalid"
                  },
            messages: 
                  {
                   name: "Please enter your name",
                   email: "Please enter a valid email address",
                   message: "Please enter a message"
                  },
            submitHandler: function(form){
                // ajax submit
                var $data = $(form).serialize();
                $.ajax({
                   type: "POST",
                   url: "email.php",
                   data: $data,
                   success: function(msg){
                        $(form).append('<p class="success">'+msg+'</p>');
                    }   
                });
            }
        });
        
        // form field hints
        $inputs.each(function(){
            var $this = $(this),
                $label = $this.prev('label');
            if($label.size){
                var txt = $label.text();
                $this.data('inputHint',txt);
                $this.val(txt);
            }   
            // events 
            var blurStyle = {'text-transform':'uppercase'},
                focusStyle = {'text-transform':'none'};
             $this.focus(function(){
                 if($this.data('inputHint') == $this.val()){
                     $this.val('');
                     $this.css(focusStyle);
                 }
             }).blur(function(){
                 if($this.val() == ''){
                     $this.val($this.data('inputHint'));
                     $this.css(blurStyle);
                 }
             });
            
        });    
    }
    
    function initFancyBox(){
        $("a[rel=working_group]").fancybox({
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'titlePosition' 	: 'over',
			'titleFormat'		: function(title, currentArray, currentIndex, currentOpts) {
				return '<span id="fancybox-title-over">Image ' + (currentIndex + 1) + ' / ' + currentArray.length + (title.length ? ' &nbsp; ' + title : '') + '</span>';
			}
		});
    }
    
    return {
        // public
        init: init
    };
})();

jQuery(document).ready(kc.common.init);























