var Main_Nav = function(){ 
}
Main_Nav.prototype.init = function(){
  
    //$('ul.sub-menu').addClass('on');    
    //$('#menu-item-17').addClass('on');
    $('ul.sub-menu li:first-child').find('a').addClass('sub-no-top-border');
    $('ul.sub-menu li:last-child').find('a').addClass('sub-no-bottom-border');
}

var Home_Gallery = function(){

}

Home_Gallery.prototype.init = function(){
  
  var self = this;
  
  $('#home-gallery-slider').cycle({ 
        after: self.changeNav
        ,fx:      'fade' 
        ,speed:    900
        ,timeout:  7000
        ,pause: 1 
    });
  
  $('#home-gallery-controls li a').bind('click',function(){
    var index = $(this).attr('rel');
    $('#home-gallery-slider').cycle(index - 1);
  });
}

Home_Gallery.prototype.changeNav = function(currSlideElement, nextSlideElement, options, forwardFlag){
  
  var slideIndex = nextSlideElement.id.split('-');
  slideIndex = slideIndex[slideIndex.length - 1];
  
  $('#home-gallery-controls').find('.active').removeClass('active');
  $('#home-gallery-controls').find('li:eq('+(slideIndex - 1)+') a').addClass('active');
}

var Preloader = function(){
	
	this.imagesArr			= 		null;
	this.imageCount 		= 		null;
	this.callBack 			= 		null;
	this.loadCount			= 		0;
}

Preloader.prototype.init = function(imagesArr, callback, path){
	
	this.imagesArr = imagesArr;
	this.imageCount = imagesArr.length;
	this.callback	= callback;
	this.path = path;
}

Preloader.prototype.loadImages = function(){

	for(a = 0; a < this.imagesArr.length; a++){
				
		var i = new Image();
		i.src = this.path + this.imagesArr[a];
		i.onLoad = this.doneLoadingImg(i);
	}
}

Preloader.prototype.doneLoadingImg = function(i){
	
	this.loadCount++;
	
	if(this.loadCount == this.imageCount){
		
		if(this.callback){
			this.callback();
		}else{
			//you can put more code here to fire after all images have loaded
		}
	}
}

var BioPage = function(){
    
  $('#button-make-a-referral').bind('click',function(){
    $('#bio-information, #contact-container, #bio-confirmation').addClass('off');
    $('#referral-container, #back-to-bio').removeClass('off');
  });
  
  $('#button-contact').bind('click',function(){
    $('#bio-information, #referral-container, #bio-confirmation').addClass('off');
    $('#contact-container, #back-to-bio').removeClass('off');
  });
  
  $('#back-to-bio').bind('click',function(){
    $('#bio-information').removeClass('off');
    $('#referral-container, #contact-container, #back-to-bio, #bio-confirmation').addClass('off');
  });
  
  var uri = window.location.hash;
  if(uri == '#contact'){
    $('#button-contact').trigger('click');
  }else if(uri =='#referral'){
    $('#button-make-a-referral').trigger('click');
  }
  
  if($('#load-referral').length){
    $('#button-make-a-referral').trigger('click');
  }
  
  $.validator.addMethod("phoneUS", function(phone_number, element) {
      phone_number = phone_number.replace(/\s+/g, ""); 
  	return this.optional(element) || phone_number.length > 9 &&
  		phone_number.match(/^(1-?)?(\([2-9]\d{2}\)|[2-9]\d{2})-?[2-9]\d{2}-?\d{4}$/);
  }, "Please specify a valid phone number");
  
  $("#contact-form").validate({
     debug: true
     ,rules: {
       // simple rule, converted to {required:true}
       'contact-name': "required"
       // compound rule
       ,'contact-email': {
         required: true
         ,email: true
       }
       ,'contact-phone': {
         required: true
         ,phoneUS:true
       }
       ,'contact-message': "required"
     }
     ,errorPlacement: function(error, element) {
        var $errorRow = $('<tr class="error-row"><td></td><td></td></tr>');
        $errorRow.find('td:eq(1)').html(error);
        $(element).parent().parent().before($errorRow);
      }
     ,invalidHandler: function(form,validator){
     }
     ,submitHandler: function(form){
       form.submit();
     }
  })
  
  $('#contact-submit').bind('click',function(){
    $(this).closest('form').submit();
  });
  
  $("#referral-form").validate({
     debug: true
     ,rules: {
       // simple rule, converted to {required:true}
       'referral-name': "required"
       // compound rule
       ,'referral-email': {
         required: true
         ,email: true
       }
       ,'referral-phone': {
         required: true
         ,phoneUS:true
       }
       ,'referral-message': "required"
     }
     ,errorPlacement: function(error, element) {
        var $errorRow = $('<tr class="error-row"><td></td><td></td></tr>');
        $errorRow.find('td:eq(1)').html(error);
        $(element).parent().parent().before($errorRow);
      }
     ,invalidHandler: function(form,validator){
     }
     ,submitHandler: function(form){
       form.submit();
     }
  })
    
  $('#referral-submit').bind('click',function(){
    $(this).closest('form').submit();
  });
}  

$(document).ready(function(){
  
  var main_nav = new Main_Nav();
  main_nav.init();
  
  if($('body').hasClass('home')){
    var home_gallery = new Home_Gallery();
    home_gallery.init();
  }
  
  var path = '/wp-content/themes/one/images/';
  var imagesArr = ['button-blue-hover.png','nav-left-hover.png','nav-right-hover.png'];
	var callback = function(){
		//alert('done')
	}
			
	var preloader = new Preloader();
	preloader.init(imagesArr, callback, path);
	preloader.loadImages();
	
	$('input:radio').uniform();
	$('.gform_body input, .gform_body select, .gform_body textarea').uniform();
	$('.datepicker').datepicker();
	
	var bioPage = new BioPage();
	
	if($('#search-bar select').length){
	  $('#search-bar select').select_skin();
	}
		
});

// usage: log('inside coolFunc',this,arguments);
// paulirish.com/2009/log-a-lightweight-wrapper-for-consolelog/
window.log = function(){
  log.history = log.history || [];   // store logs to an array for reference
  log.history.push(arguments);
  if(this.console){
    console.log( Array.prototype.slice.call(arguments) );
  }
};

// catch all document.write() calls
(function(){
  var docwrite = document.write;
  document.write = function(q){ 
    log('document.write(): ',q); 
    if (/docwriteregexwhitelist/.test(q)) docwrite(q);  
  }
})();


