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

$(function() {

	$('.gallery a').colorbox();

	$('nav li.has-dd').hover(function() {
		$(this).toggleClass('hover').find('ul').stop(true,true).slideToggle(300);
	});
	
	var austDay = new Date();
    var date_str = $('#countdown h4 span').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]);
    var austHour = parseInt(date_str[3]);
    var austMinute = parseInt(date_str[4]);

	austDay = new Date(austYear, austMonth, austDate, austHour, austMinute);
	$('#countdown h4').find('span').countdown({until: austDay});

	$('#slider .slide-link').on('click', function() {
		if($(this).find('.audio').length) {
			if (audio_link != 1) {
				$(this).parent().find('.players').animate({
					bottom: 0
				}, {
					duration: 400,
					queue: false,
					complete: function() {
						if (jQuery.browser.msie && jQuery.browser.version < 9) {
							var container = $(this).find('.flash-player:eq(0)');
							loadFlashPlayer(container, 900, 'audioPlayer1');
						} else {
							$(this).find('.APV1_play_button').trigger('click');
						};
					}
				});
				$(this).fadeOut(400)
				return false;
			}
		} else if($(this).find('.video').length) {
			if (video_link != 1){
				var vid_string = $(this).find('.notext').html();
				if($(this).find('.video').attr('rel') == 'youtube') {
					var vid_html = '<iframe width="940" height="410" src="http://www.youtube.com/embed/' + vid_string + '?rel=0&amp;autoplay=1&amp;wmode=transparent" frameborder="0"></iframe>'
				} else if($(this).find('.video').attr('rel') == 'vimeo') {
					var vid_html = '<iframe src="http://player.vimeo.com/video/' + vid_string + '?title=0&amp;autoplay=1&amp;byline=0&amp;portrait=0" width="940" height="410" frameborder="0"></iframe>';
				}
	
				$(this).fadeOut(300);
				$(this).parent().css('background', 'black').find('img').fadeOut(300);
				$(this).parent().find('.caption').animate({
					top: '-100px'
				}, {
					duration: 300,
					complete: function() {
						$(this).parent().append(vid_html);
					}
				});
	
				return false;
			}
		}
	});

	$('#recent a').hover(function() {
		if(!$(this).hasClass('clicked')) {
			$(this).find('.overlay').animate({
				opacity: 0.3
			}, {
				duration: 200,
				queue: false
			});
			$(this).find('.icon').animate({
				marginTop: '-32px'
			}, {
				duration: 200,
				queue: false
			});
		}
	}, function() {
		if(!$(this).hasClass('clicked')) {
			$(this).find('.overlay').animate({
				opacity: 0
			}, {
				duration: 200,
				queue: false
			});
			$(this).find('.icon').animate({
				marginTop: 75
			}, {
				duration: 200,
				queue: false
			});
		}
	});

	$('#recent .three-quarters a').bind('click', function(e) {
	
		if($(e.target).parents('a:eq(0)').find('.audio').length && $(e.target).parents('a:eq(0)').hasClass('clicked') == false) {
		
			if (audio_link != 1){
			
				autoPermOff = true;

				$(e.target).parents('a:eq(0)').addClass('clicked');
	
				$(e.target).parents('a:eq(0)').find('.icon').animate({
					marginTop: '-52px'
				}, {
					duration: 200,
					queue: false
				});
				$(e.target).parents('a:eq(0)').find('.mini-player').animate({
					bottom: 10
				}, {
					duration: 200,
					complete: function() {
						if (jQuery.browser.msie && jQuery.browser.version < 9) {
							var container = $(this).parents('.clicked:eq(0)').find('.flash-player:eq(0)');
							loadFlashPlayer(container, 200, 'audioPlayer2');
						} else {
							$(this).parents('.clicked:eq(0)').find('.APV1_play_button').trigger('click');
						};
					}
				});

			} else { document.location = $(this).attr('href'); }
			
		} else if($(e.target).parents('a:eq(0)').find('.video').length) {
		
			if (video_link != 1){
			
				autoPermOff = true;
			
				var vid_string = $(this).find('.icon .notext').html();
	
				if($(this).find('.video').attr('rel') == 'youtube') {
					var vid_href = 'http://www.youtube.com/embed/' + vid_string + '?rel=0&amp;autoplay=1&amp;wmode=transparent'
				} else if($(this).find('.video').attr('rel') == 'vimeo') {
					var vid_href = 'http://player.vimeo.com/video/' + vid_string + '?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0';
				}
	
				$.colorbox({
					href: vid_href,
					width: 920,
					height: 563,
					iframe: true
				});

			} else { document.location = $(this).attr('href'); }
			
		} else if($(this).find('.gallery').length) {
			document.location = $(this).attr('href');
		} else if($(this).find('.post').length) {
			document.location = $(this).attr('href');
		}
		
		return false;
	
	});

	$('#upcoming a').hover(function() {
		$(this).find('.overlay').animate({
			opacity: 0.3
		}, {
			duration: 200,
			queue: false
		});
	}, function() {
		$(this).find('.overlay').animate({
			opacity: 0
		}, {
			duration: 200,
			queue: false
		});
	});

	$(window).load(function() {
		if($('#slider').length) {
			init_slider();
		}

		if($('#share-area').length) {
			$('#share-area').css('opacity', 0.5).animate({
				marginLeft: '-605px'
			});

			$('#share-area').hover(function() {
				$(this).animate({
					opacity: 0.9
				}, {
					duration: 200,
					queue: false
				});
			}, function() {
				$(this).animate({
					opacity: 0.5
				}, {
					duration: 200,
					queue: false
				});
			});

			$(window).scroll(function() {
				if($(window).scrollTop() > 150) {
					$('#share-area').css({
						position: 'fixed',
						top: 30
					});
				} else {
					$('#share-area').css({
						position: 'absolute',
						top: 180
					});
				}
			});
		}
	});
	
	/* Ajax Contact form validation and submit */
	$('form#contactForm').submit(function() {
		$(this).find('.error').remove();
		var hasError = false;
		$(this).find('.requiredField').each(function() {
			if(jQuery.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 = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
				if(!emailReg.test(jQuery.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);
			jQuery.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(jQuery.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 = /^[a-zA-Z0-9._-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,4}$/;
		if(emailReg.test(jQuery.trim($(this).val())) && $(this).val() != '') {
			$(this).removeClass('input-error');
		} else {
			$(this).addClass('input-error');
		} 
	});
	
});

function page_loaded() {
	$('nav > ul > li').each(function() {
		if($(this).find('ul').length) {
			$(this).addClass('has-dd').find('li:last').addClass('last');
		}
	});

	if($('#slider').length) {
		load_slider();
	}

	if($('#recent').length) {
		$('#recent .overlay').css('opacity', 0);
	}

	if($('#upcoming').length) {
		$('#upcoming .overlay').css('opacity', 0);
	}

	if($('.entry aside').length) {
		$('.entry aside').each(function() {
			$(this).find('img').wrap('<span class="thumbnail" />');
		});
	}

	if($('#recent audio').length) {
		$('#recent audio').attr('width', 200);
	}

	if (video_link != 1){
		if($('#recent span.video').length) {
			$('#recent span.video').each(function() {
				if($(this).find('.notext').text().indexOf('www.') != -1) {
					var remove_www = $(this).text().toString().replace('www.', '');
					$(this).find('.notext').text(remove_www);
				}
				if($(this).find('.notext').text().indexOf('youtube') != -1) {
					$(this).attr('rel', 'youtube');
					var replaced_string = $(this).text().toString().replace('http://youtube.com/watch?v=', '');
					$(this).find('.notext').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).find('.notext').text(replaced_string);
				}
			});
		}
	}
	
}

function load_slider() {
	$('#slider').addClass('loading').find('.container').css('opacity', 0);
	$('#slider').after('<div class="dots" />');

	if($('#slider audio').length) {
		$('#slider audio').attr('width', 900);
	}

	var slides_leng = $('#slider .container li').length;
	for(i=0;i<slides_leng;i++) {
		$('.dots').append('<span />');
	}
	$('#slider .overlay').css({ opacity: 0 });
	$('#slider .caption').css({ top : '-100px' });
}

window.autoScrollPermOff = false;

function init_slider() {
	$('.dots').slideDown(200);
	$('#slider .container').animate({
		opacity: 1
	}, {
		duration: 800,
		easing: 'easeInOutQuint',
		complete: function() {
			$('#slider').removeClass('loading');
			if (auto_cycle == 'yes'){ cycle_speed = cycle_speed; } else { cycle_speed = 0; }
			$('#slider .container > ul').jcarousel({
				scroll: 1,
				auto: cycle_speed,
				animation: animation_speed,
				wrap: 'both',
				easing: 'easeInOutQuint',
				initCallback: function(carousel) {
				
					if (video_link != 1){
						if($('#slider span.video').length) {
							$('#slider span.video').each(function() {
								if($(this).find('.notext').text().indexOf('www.') != -1) {
									var remove_www = $(this).text().toString().replace('www.', '');
									$(this).find('.notext').text(remove_www);
								}
								if($(this).find('.notext').text().indexOf('youtube') != -1) {
									$(this).attr('rel', 'youtube');
									var replaced_string = $(this).text().toString().replace('http://youtube.com/watch?v=', '');
									$(this).find('.notext').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).find('.notext').text(replaced_string);
								}
							});
						}
					}

					$('#slider').hover(function() {
						$(this).find('.overlay').animate({
							opacity: 0.3
						}, {
							duration: 300,
							queue: false
						});
						$(this).find('.icon').animate({
							marginTop: '-32px'
						}, {
							duration: 300,
							queue: false
						});
						$('.jcarousel-prev').animate({
							left: 15
						}, {
							duration: 300,
							queue: false
						});
	
						$('.jcarousel-next').animate({
							right: 15
						}, {
							duration: 300,
							queue: false
						});
						$('#slider').find('.caption').animate({
							top: 0
						}, {
							duration: 300,
							queue: false
						});
						carousel.stopAuto();
					}, function() {
						$(this).find('.overlay').animate({
							opacity: 0
						}, {
							duration: 300,
							queue: false
						});
						$(this).find('.icon').animate({
							marginTop: 240
						}, {
							duration: 300,
							queue: false
						});
						$('.jcarousel-prev').animate({
							left: -40
						}, {
							duration: 300,
							queue: false
						});
	
						$('.jcarousel-next').animate({
							right: -40
						}, {
							duration: 300,
							queue: false
						});
						$('#slider').find('.caption').animate({
							top: -100
						}, {
							duration: 300,
							queue: false
						});
						if (!autoPermOff) { carousel.startAuto(); }
					});

					$('.dots span').live('click', function() {
						var dot_idx = $(this).index() + 1;
						carousel.scroll($.jcarousel.intval(dot_idx));
					});
				},
				itemFirstInCallback: {
					onBeforeAnimation: function(carousel,item,idx,state) {
						if(state != 'init') {
							if($('#slider iframe').length) {
								$('#slider iframe').fadeOut(300, function() {
									$(this).parent().find('img, .slide-link').fadeIn(400);
									$(this).remove();
								});
							}
							if($('#slider .players').length) {
								$('#slider .players').animate({
									bottom: '-80px'
								}, {
									duration: 400,
									queue: false,
									complete: function() {
										$(this).parent().find('img, .slide-link').fadeIn(400);
									}
								});
								if (jQuery.browser.msie && jQuery.browser.version < 9) {
									$('#slider .flash-player').hide().html('');
								} else {
									$('.AudioPlayerV1').AudioPlayerV1('pause');
								}
							}
						}
						$('.dots span').eq(idx-1).addClass('current').siblings('.current').removeClass('current');
					}
				}
			});
		}
	});
}

function loadFlashPlayer(container, playerWidth, playerId) {
	var song = container.attr('rel');
	if (container.find('div').length == 0) {
		container.append('<div id="'+ playerId +'"></div>');
	};
	var flashvars = {};
	var params = {};
	var attributes = {};
	
	flashvars.autoLoad = "true";
	flashvars.autoPlay = "true";
	flashvars.repeat = "true";
	flashvars.volume = "1";
	flashvars.width = playerWidth;
	flashvars.song1url = song;
	// END SETUP
	
	params.play = "true";
	params.scale = "noscale";
	params.salign = "tl";
	params.wmode = "transparent";
	params.swliveconnect = "true";
	params.allowscriptaccess = "always";
	
	attributes.id = "mp3player";
	
	embedCode(playerId, playerWidth, flashvars, params, attributes);
	container.show();
}
