var months = ['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December'];

$(window).load(function() {
	
	if (!cycle_speed) { cycle_speed = 6; }
	if (auto_cycle == 'yes') { auto_cycle = cycle_speed; } else { auto_cycle = 0; }

	$('#slider').jcarousel({
		visible: 1,
		auto: auto_cycle,
		scroll: 1,
		wrap: "both",
		animation: animation_speed,
		easing: 'easeInOutExpo',
		initCallback: slider_initCallback,
		itemFirstInCallback: slider_itemFirstInCallback
	});
	
	if($('.player').length) {
		setTimeout(function() {
			$('.player').hide();
		}, 1000);
	}

	$('.field, textarea').focus(function() {
        if(this.title==this.value) {
            this.value = '';
        }
    }).blur(function(){
        if(this.value=='') {
            this.value = this.title;
        }
    });
    
    $('.fancybox').fancybox();

    var austDay = new Date();
    var date_str = $('header .next h4').text().replace(',', '').split(' ');

    var month_str = date_str[0];
    for(i=0;i<months.length;i++) {
    	if(months[i] == month_str) {
    		var austMonth = i;
    	}
    }
    var austDate = parseInt(date_str[1]);
    var austYear = parseInt(date_str[2]);

	austDay = new Date(austYear, austMonth, austDate);
	$('header .next h4').html('<span>in</span> <span class="container" />').find('.container').countdown({until: austDay});
	$('header .next h4').fadeIn('slow');

	$('#slider .button').click(function() {
		if($(this).text().indexOf('Audio') != -1) {
			$(this).parent().find('.player').slideDown(function() {
				$(this).find('.APV1_play_button').trigger('click');
			});
			if($.browser.msie) {
				$(this).hide();
			} else {
				$(this).fadeOut();
			}
			return false;
		} else if($(this).text().indexOf('Video') != -1) {
			if($(this).parent().find('.url').attr('rel') == 'youtube') {
				var vid_string = $(this).parent().find('.url').text();
				var vid_html = '<iframe width="100%" height="408" src="http://www.youtube.com/embed/' + vid_string + '?rel=0&amp;autoplay=1&amp;wmode=transparent&amp;hd=1" frameborder="0"></iframe>'
			} else if($(this).parent().find('.url').attr('rel') == 'vimeo') {
				var vid_string = $(this).parent().find('.url').text();
				var vid_html = '<iframe src="http://player.vimeo.com/video/' + vid_string + '?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0" width="100%" height="408" frameborder="0"></iframe>';
			}
			$(this).fadeOut();
			$(this).parent().css('background', '#000').find('.caption').animate({
				bottom: '-100px'
			}, 200, function() {
				$(this).parent().find('img').fadeOut(function() {
					$(this).parent().append(vid_html);
					//$(this).remove();
				});
				//$(this).remove();
			});
			return false;
		}
	});

	if($('article .image').length) {
		$('article .image').each(function() {
			$(this).append('<span class="corner-tl" /><span class="corner-tr" /><span class="corner-bl" /><span class="corner-br" />');
		});
	}
	
	if($('.gallery .image').length) {
		$('.gallery .image').each(function() {
			var randomNumber = Math.floor(Math.random()*3);
			var subRandomNumber = Math.floor(Math.random()*3);
			randomNumber = randomNumber - subRandomNumber;
			$(this).css('-webkit-transform','rotate('+randomNumber+'deg)');
			$(this).css('-moz-transform','rotate('+randomNumber+'deg)');
		});
	}

	if($.browser.msie && $.browser.version < 8) {
		$('html').css('overflow-x', 'hidden');
	}
	
	$('.jcarousel-next, .jcarousel-prev').click(function(){
		
		// Get the current slide's position
		var currentSlidePosition = $('#slider').find('ul').position();
		currentSlidePosition = ((currentSlidePosition.left * -1) / 940);
	
		// Get the current slide
		var currentSlide = $('.jcarousel-item').eq(currentSlidePosition);
		
		// Hide the audio player
		currentSlide.find('.player').hide();
		currentSlide.find('.APV1_playing').find('.APV1_play_button').trigger('click');
		
		// Hide the video player
		currentSlide.find('iframe').remove();
		currentSlide.find('img').fadeIn();
		
		// Show the button
		currentSlide.find('.button').show();
		
	});
	
	if($('article .image').length && $.browser.msie) {
		$('article .image').each(function() {
			$(this).css('width', $(this).find('img').width());
		});
	}
	
	$('nav li:last-child').addClass('last');
	$('nav ul li').has('ul').addClass('hasSub');
	
	$('nav ul li').hover(function(){
		$(this).find('ul:first.sub-menu').fadeIn('fast');
	},function(){
		$(this).find('ul:first.sub-menu').hide();
	});
	
	/* Ajax Contact form validation and submit */
	$('form#contactForm').submit(function() {
		$(this).find('.error').remove();
		var hasError = false;
		$(this).find('.requiredField').each(function() {
			if($.trim($(this).val()) == '') {
				if ($(this).is('textarea')){
					$(this).parent().addClass('input-error');
				} else {
					$(this).addClass('input-error');
				}
				hasError = true;
			} else if($(this).hasClass('email')) {
				var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
				if(!emailReg.test($.trim($(this).val()))) {
					$(this).addClass('input-error');
					hasError = true;
				}
			}
		});
		if(!hasError) {
			$(this).find('#born-submit').fadeOut('normal', function() {
				$(this).parent().parent().find('.sending-message').show('normal');
			});
			var formInput = $(this).serialize();
			var contactForm = $(this);
			$.ajax({
				type: "POST",
				url: $(this).attr('action'),
				data: formInput,
				success: function(data){
					contactForm.parent().fadeOut("normal", function() {
						$(this).prev().prev().show('normal'); // Show success message
					});
				},
				error: function(data){
					contactForm.parent().fadeOut("normal", function() {
						$(this).prev().show('normal');  // Show error message
					});
				}
			});
		}
		
		return false;
		
	});
	
	$('.requiredField').blur(function() {
		if($.trim($(this).val()) != '' && !$(this).hasClass('email')) {
			if ($(this).is('textarea')){
				$(this).parent().removeClass('input-error');
			} else {
				$(this).removeClass('input-error');
			}
		} else {
			if ($(this).is('textarea')){
				$(this).parent().addClass('input-error');
			} else {
				$(this).addClass('input-error');
			}
		}
	});
	
	$('.email').blur(function() {
		var emailReg = /^([\w-\.]+@([\w-]+\.)+[\w-]{2,4})?$/;
		if(emailReg.test($.trim($(this).val())) && $(this).val() != '') {
			$(this).removeClass('input-error');
		} else {
			$(this).addClass('input-error');
		} 
	});	
	
})

window.autoScrollPermOff = false;

function slider_initCallback(carousel) {
	if($('#slider span.url').length) {
		$('#slider span.url').each(function() {
			if($(this).text().indexOf('www.') != -1) {
				var remove_www = $(this).text().toString().replace('www.', '');
				$(this).text(remove_www);
			}
			if($(this).text().indexOf('youtube') != -1) {
				$(this).attr('rel', 'youtube');
				var replaced_string = $(this).text().toString().replace('http://youtube.com/watch?v=', '');
				$(this).text(replaced_string);
			} else if($(this).text().indexOf('vimeo') != -1) {
				$(this).attr('rel', 'vimeo');
				var replaced_string = $(this).text().toString().replace('http://vimeo.com/', '');
				$(this).text(replaced_string);
			}
		});
	}
	
	var autoPermOff = false;
	
	// Stop autoscrolling when a button is clicked
	carousel.clip.find('.button').click(function(){
		carousel.stopAuto();
		autoPermOff = true;
	});
	
    // Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
    	if (!autoPermOff) {
    		if (auto_cycle) {
    			carousel.startAuto();
    		}
    	}
    });

	$('.jcarousel-prev, .jcarousel-next').append('<span class="icon" />');
}

function slider_itemFirstInCallback(carousel, item, idx, state) {
	$('.player').hide();
	$('#slider img, #slider .button').fadeIn('slow');
	$('#slider .jcarousel-list > li .caption').css('bottom', '-100px');
	$('#slider .jcarousel-list > li').eq(idx-1).find('.caption').animate({
		bottom: '0px'
	}, 500, 'easeOutQuint');
}
