/**
 * global.js
 */

var global = {
	
	 pageimages_counter: false
	,page_background_timer: false
	,cycle_pageimages: true
	,cycle_pageimages_delay: 7000
	
	,init: function() {

		global.init_fontreplacement();
		global.init_anchorlinks();
		global.init_availabilitylink();
		global.init_footer_emailaddress()
		global.init_page_picture_images();
		global.init_page_picture_images_cycle();
		global.init_bottomstrapfooter();
		global.init_scalable_backgrounds_cycle();
		global.init_datepickers();
		global.init_googletranslate();
		global.init_openinnewwindows();
		global.init_newsletter_signup();
		global.autoformat_breaks();
		global.init_sliding_offer(); // under construction
		
		$('.cross-link-logos h3 a').hover(function() {
			$(this).stop().fadeTo('fast', 1);
		}, function() {
			$(this).stop().fadeTo('normal', 0.4);
		});
		
	}
	
	,init_newsletter_signup: function() {
		
		$('.footer-newsletter-form').submit(function() {
			
			var emailaddress = $(this).find('#emailaddress').val();
			
			if(emailaddress == '' || emailaddress == 'Your Email address...') {
				$(this).find('#emailaddress').focus();
				return false;
			}
			
			var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
			if( reg.test(emailaddress) == false ) {
				$(this).find('#emailaddress').val('').focus();
				return false;
			}
			
			$.ajax({
				 type: 'GET'
				,url: '/cms.custom/plugins/newsletter/submit_form.php'
				,data: 'emailaddress=' + emailaddress + '&url=' + window.location
				,success: function(msg) {
					$('.footer-newsletter-form').html('<div class="newsletter-feedback-message">Thank-you for subscribing!</div>');
				}
			});

			return false;
			
		});
		
	}
	
	,init_fontreplacement: function() {

		Cufon.replace('.header-contactdetails .phone-number, .content h1, .content h2, .side-testimonial h5, .replace, h2, h3');
		
	}
	
	,init_openinnewwindows: function() {

		$('._blank').click(function() {
			window.open( $(this).attr('href') );
			return false;
		});

	}
	
	,init_tablerows: function() {
		
		$('.tariff-table tbody tr:even').addClass('alt');
		
	}
	
	,init_datepickers: function() {
		
		$('#availability_arrive').datepicker({
			changeMonth: true,
			changeYear: true,
			minDate: 0,
			dateFormat: 'dd/mm/yy'
		});

	}
	
	,init_googletranslate: function() {
		
		$('.translate_image').click(function() {
			$(this).blur();
			$('#google_translate_element').toggle();
			$('#google_translate_element select').focus();
		});

	}
	
	,init_anchorlinks: function() {
		
		$('a').click(function() {
			if( $(this).attr('href') == '#' ) {
				return false;
			}
		});
		
	}
	
	,init_footer_emailaddress: function() {

		$('.footer #emailaddress').focus(function() {
			if( $(this).val() == 'Your Email address...' ) {
				$(this).val('').addClass('focussed');
			}
		})
		.blur(function() {
			if( $(this).val() == '' ) {
				$(this).removeClass('focussed').val('Your Email address...');
			}
		});

	}
	
	,init_availabilitylink: function() {

		$('.link-checkavailability').click(function() {
			$(this).blur();
			if( $('.inline_availability_check').is(':visible') ) {
				$('.inline_availability_check').fadeOut('normal');
			} else {
				$('.inline_availability_check').fadeIn('normal', function() {
					$('#availability_arrive').focus();
				});
			}
			return false;
		});
		$('.close-panel a').click(function() {
			$('.inline_availability_check').fadeOut('normal');
		});
		$('.check-now a').click(function() {
			if( $('#availability_arrive').val() == '' ) {
				$('#availability_arrive').focus();
				return false;
			}
			if( $('#availability_nights').val() == '' ) {
				$('#availability_nights').focus();
				return false;
			}
			$(this).parents('form').submit();
			return false;
		});

	}
	
	,init_page_picture_images: function() {

		$('.page-picture-thumbnails a').click(function() {

			$(this).blur();

			clearTimeout( global.pageimages_counter );
			
			var src = $(this).attr('href');
			
			$('.content-headerimage .preloader').remove();
			$('.content-headerimage').append('<div class="preloader"></div>');
			$('.content-headerimage .preloader').fadeTo('fast', 0.8);
			
			$('.content-headerimage img').attr('src', src);
			$('.content-headerimage img').unbind('load').load(function() {
				$('.content-headerimage .preloader').fadeOut('fast');
			});

			$('.page-picture-thumbnails a').removeClass('selected');
			$(this).addClass('selected');

			if( global.cycle_pageimages == true ) {
				global.init_page_picture_images_cycle();
			}

			return false;
			
		});

	}
	
	,init_page_picture_images_cycle: function() {
		
		global.pageimages_counter = setTimeout(function() {
			
			var howmany = $('.page-picture-thumbnails a').length;
			
			if( howmany < 2 ) return;

			var index = $('.page-picture-thumbnails a').index( $('.page-picture-thumbnails a.selected') );

			var newindex = index + 1;
			if(newindex >= howmany) newindex = 0;
			
			$('.page-picture-thumbnails a').eq(newindex).trigger('click');
			
		}, global.cycle_pageimages_delay);
		
	}
	
	,init_scalable_backgrounds_cycle: function() {

		clearTimeout( global.page_background_timer );
		
		var siteid = $('#site-id').attr('name');
		
		var current_image = $('.scalable .background img:last').attr('src');
		
		$.ajax({
			url: "/cms.custom/plugins/homepages/loadsplash.ajax.php?site=" + siteid + "&ignore=" + current_image,
			type: "GET",
			success: function(msg){

				$('.scalable .background img:first').attr('src', msg);
					
				global.page_background_timer = setTimeout(function() {
					clearTimeout( global.page_background_timer );
				   
					$('.scalable .background img:last').fadeOut('normal', function() {
						$('.scalable .background').prepend( $(this) );
						$('.scalable .background img:first').show();
						
						global.init_scalable_backgrounds_cycle();
					});
					
				}, 6000);
				
			}
		});
		
	}
	
	,init_bottomstrapfooter: function() {
		
		var fromBottom = $('.bottom-zone').height();
		var thisHeight = $('.scalable .bottomstrapfooter').height();
		var initialBottom = fromBottom - thisHeight + 40;
		
		$('.scalable .bottomstrapfooter').css( 'bottom', initialBottom );
		
		$('.scalable .top-zone, .scalable .bottomstrapfooter').hoverIntent(function() {
																			  
			$('.scalable .bottomstrapfooter').stop().animate({
				bottom: fromBottom
			}, 500);
			
		}, function() {

			$('.scalable .bottomstrapfooter').stop().animate({
				bottom: initialBottom
			}, 500);

		});
		
	}
	
	,validate_contactform: function(form) {
		
		if( form.firstname.value == '' ) {
			alert('Please enter your first name to continue');
			form.firstname.focus();
			return false;
		}
		if( form.surname.value == '' ) {
			alert('Please enter your surname to continue');
			form.surname.focus();
			return false;
		}
		if( form.emailaddress.value == '' ) {
			alert('Please enter your email address to continue');
			form.emailaddress.focus();
			return false;
		}
		if( form.message.value == '' ) {
			alert('Please enter a message to continue');
			form.message.focus();
			return false;
		}
		
		return true;
		
	}
	
	,autoformat_breaks: function() {
		
		var url = String(window.location);
		
		if( url.search(/breaks/i) != -1 ) {
			
			$('.content').addClass('breaks-page');
			$('.content ul').addClass('breaks-ul');
			
		}
		
	}
	
	,init_sliding_offer: function() {
		
		//
		
	}
	
};

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