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

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

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

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

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

    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]);
    var austHour = parseInt(date_str[3]);
    var austMinute = parseInt(date_str[4]);

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

	jQuery('#slider .button').click(function() {
		if(jQuery(this).text().indexOf('Audio') != -1) {
			jQuery(this).parent().find('.player').slideDown(function() {
				jQuery(this).find('.APV1_play_button').trigger('click');
			});
			if(jQuery.browser.msie) {
				jQuery(this).hide();
			} else {
				jQuery(this).fadeOut();
			}
			return false;
		} else if(jQuery(this).text().indexOf('Video') != -1) {
			if(jQuery(this).parent().find('.url').attr('rel') == 'youtube') {
				var vid_string = jQuery(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(jQuery(this).parent().find('.url').attr('rel') == 'vimeo') {
				var vid_string = jQuery(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>';
			}
			jQuery(this).fadeOut();
			jQuery(this).parent().css('background', '#000').find('.caption').animate({
				bottom: '-100px'
			}, 200, function() {
				jQuery(this).parent().find('img').fadeOut(function() {
					jQuery(this).parent().append(vid_html);
					//jQuery(this).remove();
				});
				//jQuery(this).remove();
			});
			return false;
		}
	});

	if(jQuery('article img').length) {
		jQuery('article img').each(function() {
			if ( jQuery(this).parents('figure.image').length != 1 ) { 
				var imageClasses = jQuery(this).attr('class');
				jQuery(this).wrap('<figure class="image '+imageClasses+'">');
				jQuery(this).attr('class','');
				var imageCaptionBlock = jQuery(this).parent().parent().parent().find('.wp-caption-text');
				var imageCaption = imageCaptionBlock.html();
				imageCaptionBlock.hide();
				if (imageCaption){
					jQuery(this).parent().append('<br><figcaption>'+imageCaption+'</figcaption>');
				}
			}
			jQuery(this).parent().append('<span class="corner-tl" /><span class="corner-tr" /><span class="corner-bl" /><span class="corner-br" />');
		});
	}
	
	if(jQuery('.gallery .image').length) {
		jQuery('.gallery .image').each(function() {
			var randomNumber = Math.floor(Math.random()*3);
			var subRandomNumber = Math.floor(Math.random()*3);
			randomNumber = randomNumber - subRandomNumber;
			jQuery(this).css('-webkit-transform','rotate('+randomNumber+'deg)');
			jQuery(this).css('-moz-transform','rotate('+randomNumber+'deg)');
		});
	}

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

window.autoScrollPermOff = false;

function slider_initCallback(carousel) {
	if(jQuery('#slider span.url').length) {
		jQuery('#slider span.url').each(function() {
			if(jQuery(this).text().indexOf('www.') != -1) {
				var remove_www = jQuery(this).text().toString().replace('www.', '');
				jQuery(this).text(remove_www);
			}
			if(jQuery(this).text().indexOf('youtube') != -1) {
				jQuery(this).attr('rel', 'youtube');
				var replaced_string = jQuery(this).text().toString().replace('http://youtube.com/watch?v=', '');
				jQuery(this).text(replaced_string);
			} else if(jQuery(this).text().indexOf('vimeo') != -1) {
				jQuery(this).attr('rel', 'vimeo');
				var replaced_string = jQuery(this).text().toString().replace('http://vimeo.com/', '');
				jQuery(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();
    		}
    	}
    });

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

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