jQuery(function($){
	$('html').addClass('js');
	
	// Menu
	$('.main-menu li').hoverIntent({
		interval:50,
		sensivity:2,
		timeout:150,
		over: function() {
			$(this).addClass('hovering');
			$('.menu-contents',this).show();
		}, 
		out: function() {
			$(this).removeClass('hovering');
			$('.menu-contents',this).fadeOut(200);
		}
		});
	
	// Slideshow 
	if ($('.home .slideshow-top').length) {
		$('.home .slideshow-top').cycle({ 
			fx:    		'fade', 
			speed:		2000,
			timeout:	5500,
			fastOnEvent:500,
			pager:  	'.slide-pager'/*,
			autostop:1*/
		});  
	}

	// slightly slower on pages
	if ($('.singular .slideshow-top').length) {
		$('.singular .slideshow-top').cycle({ 
			fx:    		'fade', 
			speed:		2000,
			timeout:	6000,
			fastOnEvent:500,
			pager:  	'.slide-pager'
		});  
	}
	
	// Preload images
	preload([
		'/images/btn-learn-more_o.gif'
	]);

	// Rollovers
	$('.rollover').hover(
		function(){
			t = $(this);
			t.attr('src',t.attr('src').replace(/([^.]*)\.(.*)/, "$1_o.$2"));
		},
		function(){ 
			t = $(this);
			t.attr('src',t.attr('src').replace('_o',''));
		}
	);
	
	// BigTarget
	loadBigTarget(".register-box, .bigtarget, .main-aside");
	
	// Contact form
	var trip = $('input#referer');
	if (trip.length) {
		$('input#trip').val(trip.val());
		//console.log(trip.val());
	}
	
	// Logo fade
	$('#logo').hover(function(){$(this).fadeTo(400,0.7);},function(){$(this).fadeTo(400,1);});
	
	// AA Select
	if ($('body.page-template-select-php').length) {
		var sidebar = $('#select-home-sidebar');
		sidebar.css({left:sidebar.outerWidth(),display:'none'});
		
		sidebar.fadeIn(2000).animate({left:0},2000,'',function() {
			$(this).cycle({
				fx:    'fade', 
				speed:	3000,
				timeout:6000,
				delay:	-3000
			});
		});
	}
	
    $('.newsletter-signup input[type=submit]').attr("disabled", false);
	$('.newsletter-signup').submit( function() {
		$('input[type=submit]',this).attr("disabled", true);
		mailchimp(true, this);
		return false;
	});
	
	// Bind hellobar event
	$(window).hashchange( function(){
		if (location.hash == "#newsletter") {
			$('#email-popup').simplebox();
		}
	})

	// Trigger the event (useful on page load).
	$(window).hashchange();	
	
	$('.register-widget .text-button').click(function(e) { 
		e.preventDefault();
		regSlide(this);
		//$(this).prev().slideToggle();
	});
	
	$('.thai-photos a').colorbox({rel:'group1'});
	
});

var aaRegClicked = false;
function regSlide(button) {
	var pane = jQuery(button).prev();
	if (!aaRegClicked) {
		pane.slideDown(300, function() {
			jQuery(button)[0].innerHTML ='Send Itinerary Request<img src="/wp-content/themes/authenticasia/images/arrow-right-blue.png" width="17" height="21"/>';
			aaRegClicked = true;
			jQuery(button).parents('form:first').submit( function(e) {
				return true;
			});			
		});
	} else {
		jQuery(button).parents('form:first').submit();
	}
}


	
	
function mailchimp(submitGoal, selector) {
	if (typeof(selector) == 'undefined') {
		mailchimpProcessResponse(false);
		return;
	}
	var myEmail = jQuery('.email-input',selector).val();
	jQuery.get('/mailchimp/inc/store-address.php',{email: myEmail }, function(result) {
		if (result == 'Success!') {
			if (typeof(submitGoal) != 'undefined') {
				_gaq.push(['_trackPageview', '/newsletter/success']);
			}
			//console.log('Newsletter signup successful!');
			mailchimpProcessResponse(true, selector);
		} else {
			mailchimpProcessResponse(result, selector);
		}
	});
}


function mailchimpProcessResponse(status, selector) {
	jQuery('input[type=submit]', selector).attr("disabled", false);
	var news = jQuery(selector);
	var response = jQuery('.newsletter-response',selector.parentNode);
	if (status === true) {
		news.hide();
		response.html('Thank you for signing up!');
		response.show('fast');
	} else {
		//news.hide();
		response.html(status);
		response.slideDown('fast');
		setTimeout(function() {
			response.slideUp('slow');
			//news.show('fast');
		}, 4000);
	}	
}


function preload(arrayOfImages) {
    jQuery(arrayOfImages).each(function(){
        (new Image()).src = this;
    });
}

// Selector must be a string
function loadBigTarget(selector) {
	jQuery(selector).each( function() {
		var anchors = jQuery('a', this);
		//if (anchors.length == 1) {
			anchors.bigTarget({
				hoverClass: 'hover', // CSS class applied to the click zone onHover
				clickZone : selector // jQuery parent selector
			});
		//}
	});
}

//jQuery(function($){if(!Modernizr.input.placeholder){$('[placeholder]').focus(function(){if(this.val()==this.attr('placeholder')){this.val('');this.removeClass('placeholder')}}).blur(function(){if(this.val()==''||this.val()==this.attr('placeholder')){this.val(this.attr('placeholder'));this.addClass('placeholder')}});$('[placeholder]').parents('form').submit(function(){$(this).find('[placeholder]').each(function(){if(this.val()==this.attr('placeholder')){this.val('')}})})}});
