// jQuery fade in fade out
$(function() {
	$('body').removeClass('no-js');

	$('#archive_more').click(function() { $('.extra').slideToggle(); return false; })

	var $locationCycle = $('#location_cycle');
	if ($locationCycle.length == 1) {
		$locationCycle.cycle();
	}

	$('.news-wrap .thumb img').addClass('fade-img');
	$(".fade-img").css("opacity","1");
			
	$(".fade-img").hover(
		function () { $(this).stop().animate({ opacity: 0.5 }, "fast"); },
		function () { $(this).stop().animate({ opacity: 1 }, "fast"); }
	);
	
	var initial_btm = $('h1.title-footer').css( 'bottom' );
	$('h1.title-footer').data( 'initial_btm', initial_btm.replace( 'px', '' ) );
	$('.languages-trigger').click(function() { 
		var top, open, 
			bottom = parseInt( $( 'h1.title-footer' ).data( 'initial_btm' ) );
		
		if (!!$(this).data('open')) {
		  top = 0;
		  open = false;
		} else {
		  top = $('#lang_dd').innerHeight();
		  open = true;
		}

		$('#kd_container').animate({ top : top });
		$('h1.title-footer').animate({ bottom : bottom - top });
		$(this).data('open', open).toggleClass('active');
		return false;
	});

	$('#menu-main > li').hoverIntent({
		sensitivity : 5,
		timeout : 250,
		over : function() { 
			var $my_link = $('a:first', $(this));
			var $target = $('ul:first', $(this));
			if ($target.length == 1) {
				$my_link.addClass('active');
				$target
					.stop(true, true)
					.slideDown('normal');
			}
		},
		out: function() {
			var $my_link = $('a', $(this));
			var $target = $('ul:first', $(this));
			$target
				.stop(true, true)
				.slideUp('normal', function() {
					if (!!!$my_link.data('open')) {
						$my_link.removeClass('active');
					}
				});
		}
	});

	$('#cycle').bind({
		mouseenter : function() {
			var $buttons = $('#cycle .arrow-left, #cycle .arrow-right');
			$buttons.stop(true, true).fadeIn(500);
		},
		mouseleave : function() {
			var $buttons = $('#cycle .arrow-left, #cycle .arrow-right');
			$buttons.stop(true, true).fadeOut(500);
		}
	});

	$cycle = $('#cycle');
	$popOver = $('#pop_over');
	var cycle_options = {
		prev : '#cycle .arrow-left a',
		next : '#cycle .arrow-right a'
	};
	if ($cycle.length == 1 && $popOver.length == 1) {
		$.extend(cycle_options, {
			delay : 1000,
			autostop : true
		});
	}
	$('#cycle_wrapper').cycle(cycle_options);

	var $signupMailing = $('#signup-mailing');
	var flag = false;
	$signupMailing.data('open', flag);
	$('a', $signupMailing).click(function() {
//		if ($signupMenu.data('open')) {
//			$('a', $signupMenu).trigger('click');
//		}
		flag = $signupMailing.data('open');
		var width = flag ? 0 : 195;
		
		$('form', $signupMailing).animate({ width: width }, 750);
		$(this).animate({ paddingRight : width }, 750);

		$signupMailing.data('open', !flag)
		return false;
	});
	
	var $signupMenu = $('#signup-menu');
	flag = false;
	$signupMenu.data('open', flag);
	$('a', $signupMenu).click(function() { 
//		if ($signupMailing.data('open')) {
//			$('a', $signupMailing).trigger('click');
//		}
		flag = $signupMenu.data('open');
		var width = flag ? 0 : 195;
		
		$('form', $signupMenu).animate({ width: width }, 750);
		$(this).animate({ paddingRight : width }, 750);

		$signupMenu.data('open', !flag)
		return false;
	});

	$('input[type=text]', $signupMailing)
		.each(function() { $(this).data('initial_text', $(this).val()); })
		.bind({
			focus : function() {
				if ($(this).val() == $(this).data('initial_text')) {
					$(this).val('');
				}
			},
			blur : function() {
				if ($(this).val() == '') {
					$(this).val($(this).data('initial_text'));
				}
			}
		});

	$('form', $signupMailing).submit(function() {
		if ($('#newsletter').val() == $('#newsletter').data('initial_text')) {
			$('#newsletter').val('');
		}

		var email = $('#newsletter').val();

		if (email == '') {
			$.prompt('Please enter your email address');
		} else {
			$.ajax({
			  type: 'POST',
			  url : '/wp-content/themes/mogford/contact.php',
			  dataType: 'json',
			  data: {
					email : email,
					type : 'mailing-list', 
					list : 'mailing'
				},
			  success: function(d,ts,xhr) {
					if (d.success) {
						$signupMailing.html('Thank you for joining our mailing list');
					} else {
						$.prompt(d.message);
					}
				}
			});
		}
		return false;
	});
	
	$('input[type=text]', $signupMenu)
	.each(function() { $(this).data('initial_text', $(this).val()); })
	.bind({
		focus : function() {
			if ($(this).val() == $(this).data('initial_text')) {
				$(this).val('');
			}
		},
		blur : function() {
			if ($(this).val() == '') {
				$(this).val($(this).data('initial_text'));
			}
		}
	});
	
	$('form', $signupMenu).submit(function() {
		if ($('#menu-email').val() == $('#menu-email').data('initial_text')) {
			$('#menu-email').val('');
		}

		var email = $('#menu-email').val();
		if (email == '') {
			$.prompt('Please enter your email address');
		} else {
			$.ajax({
			  type: 'POST',
			  url : '/wp-content/themes/mogford/contact.php',
			  dataType: 'json',
			  data: {
					email : email,
					type : 'mailing-list',
					list : 'menu'
				},
			  success: function(d,ts,xhr) { 
					if (d.success) {
						$signupMenu.html('Thank you for joining our mailing list');
					} else {
						$.prompt(d.message);
					}
				}
			});
		}
		return false;
	});

	$('#gallery').bind({
		mouseenter : function() {
			var $buttons = $('#gallery .arrow-left, #gallery .arrow-right');
			$buttons.stop(true, true).fadeIn(500);
		},
		mouseleave : function() {
			var $buttons = $('#gallery .arrow-left, #gallery .arrow-right');
			$buttons.stop(true, true).fadeOut(500);
		}
	});
	
	$('#gallery-wrap').cycle({
		fx : 'scrollHorz',
		timeout : 0, 
		prev : '#gallery .arrow-left a',
		next : '#gallery .arrow-right a'
	});
	$('#cycle_gallery').cycle({
		fx : 'fade',
		timeout : 0,
		prev : '#cycle_prev',
		next : '#cycle_next',
		pager:  '#gallery-wrap',
		pagerAnchorBuilder: function(idx, slide) {
			return '#gallery-wrap li:eq(' + idx + ') a';
		}
	});

	var $popOver = $('#pop_over');
	if ($popOver.length == 1) {
		$('p', $popOver).condense({
			moreText : 'MORE',
			lessText : '',
			ellipsis : '...'
		});

		$('#close')
			.bind('click', function() {
				$('.condense_control_less', $popOver).trigger('click');
				$(this).hide();
				return false;
			});
		$('.condense_control_more', $popOver).bind('click', function() { $('#close').css('display', 'block'); })
	}

	var bfdoa = $( '#bf_doa' );
	if ( bfdoa.length == 1 ) {
		bfdoa
			.find( '#date_of_arrival' )
				.datepicker({ 
					dateFormat : 'dd-mm-yy',
					currentText: 'Now'
				})
			.end()
			.find( 'img' )
				.bind( 'click', function() { bfdoa.find( '#date_of_arrival' ).focus() });
	}

	$( '#bf_amend a' ).click( function() {
		var form = $( '#booking_form form' ),
			action = form.attr( 'action' ), 
			hotel = $( '#dd_hotels option:selected' );

		form
			.find( 'input[name=start]' )
				.val( 'searchres' )
			.end()
			.find( 'input[name=shell]' )
				.val( hotel.attr( 'class' ) )
			.end()
				.submit();
		return false;
	});
	
	$( '.press_thumb' ).colorbox({
		maxWidth : '500px', 
		maxHeight : '500px', 
		title : function() { 
			var web = $( this ).attr( 'href' ), 
				full = $( this ).find( '.press_full' ).text(), 
				title = 'Download <a href="' + web + '">web quality image</a> (72DPI)<br />';
			title += 'Download <a href="' + full + '">print quality image</a> (300DPI)';
			return title;
		}, 
		rel : 'press'
	});
});
