$(document).ready(function(){
	
    //GALLERY IMAGES
    //VIDEOS -> YOUTUBE AND BACK AGAIN
	$("a[rel='gallery']").colorbox();
    $(".video-link").colorbox({
		iframe:true,
		innerWidth:425,
		innerHeight:344
	});

    //PLACEHOLDER TEXT
	$('#newsletter form #email').val($('#newsletter form #email').attr('title'));

    // clear it out on focus
    $('#newsletter form #email').focus(function(){
        $(this).val("");
		$(this).removeClass('error');
    });

    // if it's blank then reset back the title attribute
    $('#newsletter form #email').blur(function(){
        // alert($(this).val().length);
        if($(this).val().length < 1){
            $('#newsletter form #email').val($('#newsletter form #email').attr('title'));
        }
    });
	
	$('#newsletter form').submit(function(e) {
	    
	    var pattern = /^([a-zA-Z0-9_\.\-\+])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/;
        var email = $("input#email").val();
        
        if (email == '' || email == 'email@domain.com') {
           $("#email").addClass('error');
           alert('Please enter an email address');
           return false;
        }
        
        else if(!pattern.test(email)) {
          $("#email").addClass('error');
          alert('Please enter an valid email address.');
          return false;
        }
        
        $("#newsletter form").addClass('loading');
        $("#newsletter button").hide();
        
        // Grab form action
		formAction = $("#newsletter form").attr("action");
		
        // Serialize form values to be submitted with POST
        var str = $("#newsletter form").serialize();
        
        
        // Add form action to end of serialized data
		final = str + "&action=" + formAction;
		
		// Submit the form via ajax
		$.ajax({
			url: "/embeds/cm-proxy",
			type: "POST",
			data: final,
			success: function(data){
				//Check to make sure that the email was accepted
				if (data.search(/invalid/i) != -1) {
					alert('The email address you supplied is invalid and needs to be fixed before you can subscribe to this list.');
				}
				else
				{
				    
					 $('#newsletter form span').animate({opacity: 0}, 1000, null, function(){
                            $(this).hide();
                            $("#newsletter form").removeClass('loading')
                            $('#newsletter form p').append('<strong>Successfully subscribed</strong>');
                            $('#newsletter form strong').css({opacity: 0}).animate({opacity: 1});
                    });
				}
			}
		});

        e.preventDefault();       
               
    });
    
    
	$('#slides').cycle({
		fx: 'fade',
		before: onBefore,
		after: onAfter,
		timeout: 6000
	});
	
	$('#main-img-frame').cycle({
		fx: 'fade'
	});
	
	$('#quote-box div').cycle({
		fx: 'fade',
		speed: 2000,
		timeout: 7000
	});

	$('#slides .caption').hide();

	function onBefore() {
		var caption = $(this).find('.caption').html();
		$('#frame .caption').html(caption);
	}

	function onAfter() {}

	$('#slides').css('width', '958px');

	// shop links
	$('ul#nav-sub a[href$="/explore/heligan-wild/"]').attr('target', '_blank');
	$('ul#nav-sub a[href$="/shopping/online-shop/"]').attr('target', '_blank');
	$('ul#sitemap a[href$="/explore/heligan-wild/"]').attr('target', '_blank');
	$('ul#sitemap a[href$="/shopping/online-shop/"]').attr('target', '_blank');
	
	// bookmark link
	$('#bookmark-page').jFav();
	
	// bird song popup
	$('#header a.bird-song').click(function(e){
		var url = $(this).attr("href");
		var options = 
			",directories=no" +
			",location=no" +
			",menubar=no" +
			",scrollbars=no" +
			",status=no" +
			",toolbar=no" +
			",resizable=no" +
			",fullscreen=true";
			
		window.open(url,'Heligan', 'width=440, height=220' + options);
		e.preventDefault();
	});

});
