/**
 * harlyn.js
 */

var harlyn = {
	
	// initialise
	init: function() {
		
		$('.top-navigation-links').show();
		
		harlyn.init_sifr();
		harlyn.init_subnavigation();
		harlyn.init_signup_panel();
		harlyn.init_getintouch_form();
		harlyn.init_specialoffers_page();

		$("a[href^='http']").live('click', function(e) {
			var hostname = window.location.hostname;
			var url = $(this).attr('href');
			if( url.match( hostname ) ) {
				return;
			}
			e.preventDefault();
			window.open( url );
		});
		
		$("a._blank").click(function(e) {
			e.preventDefault();
			window.open($(this).attr('href'));
		});

		if( $(".gallery").attr('class') ) {
			$(".gallery").yoxview({dataFolder:"/assets/js/yoxview/data/", lang:"en"});
		}
		
		if( $.datepicker ) {
			$('#morning-date, #afternoon-date').datepicker({
				changeMonth: true,
				changeYear: true,
				minDate: 0,
				dateFormat: 'dd/mm/yy'
			});
		}
		
		var hash = window.location.hash;
		
		if(hash && hash == '#!autoplayvideo') {
			if( $('#homepage-movie').attr('id') ) {
				setTimeout(function() {
					harlyn.init_movie_homepage();
				}, 2000);
			}
		}

		if( $('#coasteering-movie').attr('id') ) {
			$('#coasteering-movie').css({
				position: 'relative'
			});
			$('#coasteering-movie').append('<a href="javascript:harlyn.init_movie_coasteering();" class="playvideolink" style="position:absolute; bottom:10px; left:10px; font-weight:bold; color: #fff; font-size:18px;">&raquo; Click here to watch our coasteering video</a>');
			setTimeout(function() {
				if(hash && hash == '#!autoplayvideo') {
					harlyn.init_movie_coasteering();
				}
			}, 3000);
		}
		
		var ac=0;
		$('.useful-links-page a').each(function() {
			$(this).after('&nbsp;');
			$(this).before('&nbsp;');
		});
		
		$('.header-image-sidepanel-handle a').click(function(e) {
			e.preventDefault();
			$(this).blur();
			
			if( $('.header-image-sidepanel').hasClass('open') ) {
				
				$('.header-image-sidepanel').removeClass('open').animate({
					right: '-227px'
				}, 'normal', function() {
					$(this).addClass('closed');
				});
				
			} else {
				
				$('.header-image-sidepanel').animate({
					right: '0px'
				}, 'normal', function() {
					$(this).removeClass('closed').addClass('open');
				});
				
			}
		});
		setTimeout(function() {
			$('.header-image-sidepanel.closed .header-image-sidepanel-handle a').trigger('click');
		}, 1000);
		$(".header-image-sidepanel a[rel='video']").click(function(e) {
			e.preventDefault();
			
			if( !$('.header-image-insidecontent-alt').is(':hidden') ) {
				setTimeout(function() {
					harlyn.init_movie_homepage('hide');
				}, 500);
			} else {
				setTimeout(function() {
					harlyn.init_movie_homepage();
				}, 500);
			}
		});
		
		$('.cross-selling-panels a').hoverIntent(function() {
			$(this).attr('rel', 'on');
			$(".cross-selling-panels a[rel!='on']").css('opacity', 0.65);
		}, function() {
			$(this).removeAttr('rel');
			$(".cross-selling-panels a").css('opacity', 1);
		});
		
		harlyn.init_slideshow();
		
		$("a[rel='closeSignupFormButton']").click(function(e) {
			e.preventDefault();
			$('.top-signup-form').slideUp('normal');
		});

		$("a[rel='closeContactFormButton']").click(function(e) {
			e.preventDefault();
			$('.top-contact-form').slideUp('normal');
		});

		$("a[rel='openSignupForm']").click(function(e) {
			e.preventDefault();
			$('.top-contact-form').slideUp('normal');
			$('.top-signup-form').slideToggle('normal');
		});

		$("a[rel='openContactForm']").click(function(e) {
			e.preventDefault();
			$('.top-signup-form').slideUp('normal');
			$('.top-contact-form').slideToggle('normal');
		});

	}

	,init_slideshow: function() {
		
		/*
		$('.header-image-insidecontent img').each(function() {
			if( $(this).find('img').length < 1 ) {
				var img = $(this).find('span').attr('title');
				$(this).html('<img src="' + img + '" alt="" />');
			}
		});
		*/
		
		if( $('.header-image-insidecontent img').length > 1 ) {
			$(window).load(function() {
				$('.header-image-insidecontent img:last').addClass('active');
				setInterval( "harlyn.cycle_images()", 5000 );
			});
		} else {
			$('.subpage-headerimage').height( $('.header-image-insidecontent img:first').height() + "px" );
			$('.header-image-insidecontent img:last').addClass('active');
		}
		
	}
	
	,cycle_images: function() {
		var $active = $('.header-image-insidecontent .active');
		
		if ( $active.length == 0 ) {
			$active = $('.header-image-insidecontent img:last');
		}
		
		var $next =  $active.next().length ? $active.next() : $('.header-image-insidecontent img:first');
		
		$active.addClass('last-active');
		
		$next.css({opacity: 0.0})
			 .addClass('active')
			 .animate({opacity: 1.0}, 2000, function() {
			     $active.removeClass('active last-active');
			 });

	}

	// initialise subnavigation behaviour
	,init_subnavigation: function() {
		
		$('ul.subnavigation a').click(function() {
			
			/*
			if( $(this).parent().find('ul').length ) {
				$(this).blur();
				$(this).parent().find('ul:first').slideToggle('normal');
				return false;
			}
			*/
			
			if( $(this).attr('href') == '#' ) {
				return false;
			}
			
		});
		
	}
	
	// initialise cufon font replacement
	,init_sifr: function() {
		Cufon.replace('.replace');
	}
	
	// initialise ajax signup panel
	,init_signup_panel: function() {
		
		$('#signup_emailaddress').focus(function() {
			
			if( $(this).val() == 'Your email address' ) {
				$(this).val('');
			}
			
		}).blur(function() {
			
			if( $(this).val() == '' ) {
				$(this).val('Your email address');
			}
			
		});
		
		/*
		$('.signup-panel-form').submit(function() {
			var emailaddress = $(this).find("input[name='emailaddress']").val();
			var thisEl = $(this);
			
			if(emailaddress) {
				
				harlyn.show_overlay( thisEl );
				
				$.ajax({
					 url: '/ajax/signup.php'
					,data: 'a=submit&emailaddress=' + emailaddress
					,type: 'POST'
					,success: function(data) {
						harlyn.hide_overlay( thisEl );
						thisEl.html( data );
					}
				});
				
			}
			return false;
		});
		*/
		
	}

	// initialise footer get in touch ajax functionality
	,init_getintouch_form: function() {

		$('#top-contact-form').submit(function() {
			var thisEl = $(this).parent();
			var formdata = $(this).serialize();

			harlyn.show_overlay( $(thisEl) );
			
			$.ajax({
				 url: '/ajax/getintouchfooter.php'
				,data: formdata
				,type: 'POST'
				,success: function(data) {
					harlyn.hide_overlay( thisEl );
					thisEl.html( data );
				}
			});
			
			return false;
		});
		
	}

	// show an overlay
	,show_overlay: function( el ) {
		
		el.append('<div class="overlay"></div>');
		el.find('.overlay').css('opacity', 0).show().fadeTo('fast', 0.8);
		
	}

	// hide an overlay
	,hide_overlay: function( el ) {
		
		el.find('.overlay').fadeOut('fast', function() {
		});
		
	}
	
	,init_movie_homepage: function( mode ) {
		
		if( !mode || mode == 'show' ) {
			
			$('.header-image').height( $('.header-image').height() );
			$('.header-image-insidecontent').fadeOut('normal', function() {
				$('.header-image-insidecontent-alt').fadeIn('fast', function() {
					$('.header-image').animate({
						height: '592px'
					}, 'normal', function() {
	
						flashvars = { 'skinName' : 'assets/swf/flvplayer/Corona_Skin_3'
									 ,'streamName' : '/assets/media/HSS-Generic-web-890'
									 ,'autoPlay' : true
									 ,'autoRewind' : false};
						params    = { 'wmode' : 'transparent' };
						
						swfobject.embedSWF(
							"/assets/swf/flvplayer/FLVPlayer_Progressive.swf"
							,"header-image-insidecontent-alt"
							,"890"
							,"592"
							,"9.0.0"
							,"/assets/js/expressInstall.swf"
							,flashvars
							,params
						);
						
						setTimeout(function() {
							$('.header-image-sidepanel.open .header-image-sidepanel-handle a').trigger('click');	
						}, 1000);
						
						$('.header-image-insidecontent-alt').append('<a href="/" id="header-image-insidecontent-alt-stopbutton" title="Click to stop and close the video"><span>X</span></a>');
						
						$('.header-image-insidecontent-alt').hover(function() {
							$('#header-image-insidecontent-alt-stopbutton').fadeIn('normal');
						}, function() {
							$('#header-image-insidecontent-alt-stopbutton').fadeOut('normal');
						});
						
						$('#header-image-insidecontent-alt-stopbutton').click(function(e) {
							e.preventDefault();
							harlyn.init_movie_homepage( 'hide' );
							setTimeout(function() {
								$('.header-image-sidepanel.closed .header-image-sidepanel-handle a').trigger('click');
							}, 1000);
						});
	
					});
				});
			});
			
		} else {
			
			$('.header-image-insidecontent-alt').html('');
			$('#header-image-insidecontent-alt').remove();

			$('.header-image').animate({
				height: '390px'
			}, 'normal', function() {
				$('.header-image-insidecontent-alt').hide();
				$('.header-image-insidecontent-alt').append('<div id="header-image-insidecontent-alt"></div>');
				$('.header-image-insidecontent').fadeIn('normal');
			});

		}
		
	}

	,init_movie_coasteering: function() {
		
		var img_width = $('#coasteering-movie img').width();
		var img_height = $('#coasteering-movie img').height();
		
		$('#coasteering-movie').css({
			 width: img_width + 'px'
			,height: img_height + 'px'
		});
		
		$('#coasteering-movie img').fadeOut('normal', function() {
			
			$('#coasteering-movie').animate({
				height: '372px'
			}, 'normal', function() {
				
				flashvars = { 'skinName' : 'assets/swf/flvplayer/Corona_Skin_3'
							 ,'streamName' : '/assets/media/HSS-Coasteering-HD-web-665'
							 ,'autoPlay' : true
							 ,'autoRewind' : false};
				params    = { 'wmode' : 'transparent' };
				
				swfobject.embedSWF(
					"/assets/swf/flvplayer/FLVPlayer_Progressive.swf"
					,"coasteering-movie"
					,"665"
					,"372"
					,"9.0.0"
					,"/assets/js/expressInstall.swf"
					,flashvars
					,params
				);

			});
			
		});
		
	}
	
	,init_specialoffers_page: function() {
		
		$('.soffer-panel:odd').addClass('green');
		
		var height = 0;
		setTimeout(function() {
			$('.soffer-panel').each(function() {
				if( $(this).height() > height ) {
					height = $(this).height();
				}
			});
			setTimeout(function() {
				$('.soffer-panel').height( height+"px" );
			}, 500);
		}, 500);
		
	}

};

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