/**
 * website.js
 */

var website = {
	
	init: function() {
		
		website.init_fonts(); // initialise the website fonts [replacement]
		website.init_headerimages(); // initialise header case study panels
		website.init_textresizers(); // initialise the text increase/decrease links
		website.init_callsignup_panel(); // initialise the call/signup panel
		website.init_contactform(); // initialise the contact form functionality
		website.init_casestudy_gallery();
		website.init_diagrams();
		prettyLinks.init();
		
	}
	
	,init_fonts: function() {
		
		Cufon('h1, h2');
		
	}
	
	,init_diagrams: function() {

		$(".side-column-content a[href*='.jpg'], .middle a[href*='.jpg']").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	true
		});

	}
	
	,init_headerimages: function() {
		
		$('.header-images').hoverIntent(function() {
			$(this).find('.header-images-caption-moreinfo').slideDown('fast');
		}, function() {
			$(this).find('.header-images-caption-moreinfo').slideUp('fast');
		});
		
		website.init_slideshow();
		
	}

	,init_slideshow: function() {

		$('.slideshow li').each(function() {
			if( $(this).find('img').length < 1 ) {
				var img = $(this).find('span').attr('title');
				$(this).html('<img src="' + img + '" alt="" />');
			}
		});

		var $active = $('.slideshow li.active');
		
		if ( $active.length == 0 ) {
			$('.slideshow li:last').addClass('active');
		}
		
		if( $('.slideshow li').length > 1 ) {
			$(window).load(function() {
				setInterval( "website.cycle_images()", 7000 );
			});
		}
		
	}
	
	,cycle_images: function() {
		var $active = $('.slideshow li.active');
		
		if ( $active.length == 0 ) {
			$active = $('.slideshow li:last');
		}
		
		var $next =  $active.next().length ? $active.next() : $('.slideshow li:first');
		
		$active.addClass('last-active');
		
		$next.css({opacity: 0.0})
			 .addClass('active')
			 .animate({opacity: 1.0}, 4000, function() {
			     $active.removeClass('active last-active');
			 });

	}

	,init_callsignup_panel: function() {
		
		$(".panel-call-signup input[name='emailaddress']").addClass('ghosted').val('Enter your email for updates');
		$(".panel-call-signup input[name='emailaddress']").focus(function() {
			if( $(this).val() == 'Enter your email for updates' ) {
				$(this).removeClass('ghosted').val('');
			}
		}).blur(function() {
			if( $(this).val() == '' ) {
				$(".panel-call-signup input[name='emailaddress']").addClass('ghosted').val('Enter your email for updates');
			}
		});
		
		$(".panel-call-signup").height( $(".panel-call-signup").height() );
		
		$('#calluspanel').submit(function(e) {
			e.preventDefault();
			var emailaddress = $(this).find("input[name='emailaddress']").val();
			if( !emailaddress || emailaddress == 'Enter your email for updates' ) {
				return false;
			}
			var data = 'emailaddress=' + emailaddress;
			$.ajax({
				 url: '/ajax/submit-calluspanel.php'
				,type: 'post'
				,data: data
				,success: function(retHTML) {
					if(retHTML) {
						$('#calluspanel .signup-container').html( retHTML );
					}
				}
			});
		});
		
	}
	
	,init_textresizers: function() {
		
		$('.text-resizers a').click(function(e) {
			e.preventDefault();
			$(this).blur();
			
			var rel = $(this).attr('rel');

			if( rel == 'increase' ) {
				var currentFontSize = $('html').css('font-size');
				var currentFontSizeNum = parseFloat(currentFontSize, 10);
				var newFontSize = currentFontSizeNum * 1.2;
				$('html').css('font-size', newFontSize);
			} else if(rel == 'decrease') {
				var currentFontSize = $('html').css('font-size');
				var currentFontSizeNum = parseFloat(currentFontSize, 10);
				var newFontSize = currentFontSizeNum * 0.8;
				$('html').css('font-size', newFontSize);
			}
		});
		
	}
	
	,init_contactform: function() {
		
		$("#contact-form").validationEngine();
		
		$('#interest_all').click(function() {
			if( $(this).attr('checked') ) {
				$(this).parents('.tickbox-group').find("input[type='checkbox']").attr('checked', true);
			} else {
				$(this).parents('.tickbox-group').find("input[type='checkbox']").attr('checked', false);
			}
		});
		
		$('#property_other').click(function() {
			if( $(this).attr('checked') ) {
				$('#property_other_text').focus();
			} else {
			}
		});
		
	}
	
	,init_casestudy_gallery: function() {
		
		$("a.gallery_grouped").fancybox({
			'transitionIn'	:	'elastic',
			'transitionOut'	:	'elastic',
			'speedIn'		:	600, 
			'speedOut'		:	200, 
			'overlayShow'	:	true
		});
		
		$('.casestudy-link').click(function(e) {
			e.preventDefault();
			$(this).parents('.casestudy-list-item-text').next().find('img:first').trigger('click');
		});
		
	}
	
};

var prettyLinks = {
	
	hostname: ''
	
	,init: function() {
		
		prettyLinks.hostname = window.location.hostname;;
		
		$('.download-links a').wrapInner('<span />').addClass('pretty-link');
		
		prettyLinks.do_pdf();
		prettyLinks.do_link();
		prettyLinks.do_diagramlinks();

	}
	
	,do_pdf: function() {
		
		$("a.pretty-link[href*='.pdf']").each(function() {
			$(this).addClass('pdf-link');
			$(this).click(function(e) {
				e.preventDefault();
				$(this).blur();
				window.open( $(this).attr('href') );
			});
		});
		
	}
	
	,do_link: function() {

		$("a[href^='http://']").each(function() {
			var href = $(this).attr('href');
			if( !href.match(prettyLinks.hostname) ) {
				$(this).click(function(e) {
					e.preventDefault();
					$(this).blur();
					window.open( $(this).attr('href') );
				});
			}
		});

	}

	,do_diagramlinks: function() {
		
		$('.middle a, .side-column-content a').each(function() {
			if( $(this).html() == 'Click to enlarge diagram' ) {
				$(this).wrapInner('<span />');
				$(this).addClass('plus-link');
			}
		});
		
	}

};

$(document).ready(website.init);
