/* Author: Mike Turner - http://www.hi-hatconsulting.com */
/* Date: May 2011 */

$(function(){

	//class
	ExperienceRenewal = new function(){
	
		//private properties and methods
		var page = null;
		
		
		//public properties and methods
		return{
			
			initialized: false,
			elements: {},
			settings: { 
				'linksDuration':400
			},	
		
			//methods
			
			//initialize method is used to call page specific classes
			initialize: function($param){
			
				if ( this.initialized ) return false;				
				this.initialized = true;	
				
				o = this;

				//save page elements to object for use later as context for jquery selectors	
				this.elements.header 				= $('header');
				this.elements.mainNav 				= $('nav ul', this.elements.header);
				this.elements.homeContent 			= $('.home-content');
				this.elements.homeClientLogos 		= $('.client-logos', this.elements.homeContent);
				this.elements.blogFeature			= $('.feature', '.blog');
				this.elements.blogComments			= $('.comments-wrapper', '.blog');
				this.elements.servicesNav			= $('.icon-list', '.services');
				this.elements.servicesInfo			= $('.services-info', '.services');
				this.elements.aboutFeature			= $('.feature', '.about');
				this.elements.footer 				= $('footer');
				this.elements.footerForm 			= $('form', this.elements.footer);
				
				//perform all site wide actions
				this.initSite();
				
				//perform all pages specific actions
				switch($param){
					case 'home':
						page = this.initHomePage();
						break;
					case 'blog':
						page = this.initBlogPage();
						break;
					case 'blog-single':
						page = this.initBlogSingle();
						break;
					case 'services':
						page = this.initServicesPage();
						break;
					case 'about':
						page = this.initAboutPage();
						break;
				}//switch
				
			},//initialize
			
			
			/*******************************************************************************
			initSite method is used for all site wide functions
			*******************************************************************************/
			initSite: function(){

				//ie fixes
				this.ieFixes();				
								
				//initialize links fade effect
				initLinkEffects([ 
			 		$('a', this.elements.mainNav),
			 		$('a', this.elements.homeContent),
			 		$('a', this.elements.footer)
				], 'general');
				
				//initialize label fadeouts for footer form
				//$('label', this.elements.footerForm).inFieldLabels();		
				
				//populate current year
				$('.current-year').text((new Date).getFullYear());		
	
			},
			
			
			/*******************************************************************************
			initHomePage method is used for all home page specific functions
			*******************************************************************************/			
			initHomePage: function(){
			
				//fade the client logo images on mouseover
				$('img', this.elements.homeClientLogos).css({ 'opacity':0.8 });
				$('img', this.elements.homeClientLogos).hover(function(){
					$(this).fadeTo(o.settings.linksDuration, 1);				
				},function(){
					$(this).fadeTo(o.settings.linksDuration, 0.8);
				});
								
			},
			
			
			
			
			/* =============================================================================
			   initBlogPage method is used for all blog page(s) specific functions
			   ========================================================================== */
			initBlogPage: function(){
			
				//scroll the read more link
				$('.older-posts', this.elements.blogFeature).click(function($event){ console.log('testing');
					$event.preventDefault();
					$.scrollTo('.secondary-posts-wrapper', 800);	
				});			
			
			},


			

			/* =============================================================================
			   initBlogPage method is used for all blog page(s) specific functions
			   ========================================================================== */
			initBlogSingle: function(){

				//scroll the read more link
				$('.scroll-to-comments').click(function($event){
					console.log('testing');
					$event.preventDefault();
					$.scrollTo('.comments-wrapper', 800);
				});			
			
			},
			
			
			
						
			/* =============================================================================
			   initServicesPage method is used for all services page specific functions
			   ========================================================================== */
			initServicesPage: function(){
			
				//$('.item:nth-child(2n-2)', this.elements.servicesInfo).addClass('copy-right');
			
			},
			
			
			
			
			/* =============================================================================
			   initAboutPage method is used for all about page specific functions
			   ========================================================================== */
			initAboutPage: function(){
			
				//scroll the read more link
				$('.older-posts', this.elements.aboutFeature).click(function($event){ 
					$event.preventDefault();
					$.scrollTo('.meet-the-team', 800);	
				});
				
				$('.headshot').colorbox({ 
					rel:'.headshot',
					current: '(image {current} of {total})'
				});
		
			},
			
			
			
			

			/* =============================================================================
			   ieFixes method is for all Internet Explorer related correcttions (lack of CSS support, etc.)
			   ========================================================================== */
			ieFixes: function(){

				//ie 6 fixes
				if($.browser.msie && $.browser.version.substr(0, 1) < 7){ 

					
					//png fix
					DD_belatedPNG.fix('.logo, .polariod-1, .polariod-2, .home-content, .lower-banner, footer');
					
					//add classs names to input fields
					$('input[type="text"]', this.elements.footerForm).addClass('txt');
					$('textarea', this.elements.footerForm).addClass('textarea');
					$('input[type="submit"]', this.elements.footerForm).addClass('submit');
					
				};
				
				//ie fixes
				if($.browser.msie){
					
					//remove right margin from last nav item
					$('li:last-child', this.elements.mainNav).css({ 'margin':'0' });
					$('li:last-child', this.elements.servicesNav).css({ 'margin':'0' });
					$('.about .profile-wrapper .profile:nth-child(3n+3)').css({ 'margin-right':'0' });
					
				}				
				
			}
			
		}//return
		
	}//thepaddock
	
}); //dom ready








/*************************************/
/* functions */
/*************************************/

initLinkEffects = function(el, page){
	
	var duration = ExperienceRenewal.settings.linksDuration;
	//default link colours
	var origColor = '#bae13c';
	var hoverColor = '#c9f441';
	var isNavLink = false;
	
	//remove the "use-hover" class from <body> tag, this will allow for CSS :hover when JS is disabled
	$('body').removeClass('use-hover');

		$(el).each(function(i, e) {
			
			isNavLink = false;
			
			//animate nav links only, this is because we have to look for the current page/nav state
			if(i == 0){	
				//loop through nav links
				$(e).each(function(index , element){ 
					//if the current nav item color is NOT the same as the hoverColor, animate it
					if(rgb2hex($(this).css('color')) != hoverColor){
						
						//mouseover
						$(this).hover(function() {
							//animate link
							$(this)
								.css({ 'color' : '#c8c6c4' })
								.animate({ 'color' : '#ffffff' }, duration); 
						//mouseout		
						}, function(){
							//animate link
							$(this)
								.css({ 'color' : '#ffffff' })
								.animate({ 'color' : '#c8c6c4' }, duration);
						});//.hover										
					}
					
				});//each	
	

			//animate all other links
			}else{

				if(! e.hasClass('post-title')){
					$(this).hover(function() {
						//mouseover
						$(this)
							.css({ 'color' : origColor })
							.animate({ 'color' : hoverColor }, duration);
						//mouseout		
						}, function(){
							$(this)
								.css({ 'color' : hoverColor })
								.animate({ 'color' : origColor }, duration);
					});//.hover		
				}		
			};//if				
			
		})//each
	
		
};//initLinkEffects






//this function will convert RGB values to Hexidecimal
function rgb2hex(color) {
    if (color.substr(0, 1) === '#') {
        return color;
    }
    var digits = /(.*?)rgb\((\d+), (\d+), (\d+)\)/.exec(color);
    var red = parseInt(digits[2]);
    var green = parseInt(digits[3]);
    var blue = parseInt(digits[4]);
    var rgb = blue | (green << 8) | (red << 16);
    return digits[1] + '#' + rgb.toString(16);
};

















