// Place your application-specific JavaScript functions and classes here
// This file is automatically included by javascript_include_tag :defaults

window.onscroll = function (e) {
  console.log(window.pageYOffset);
  
  if(window.pageYOffset > 2000)
  	$('#distance-movie').html("");
}

function subscribe() {
	
	$.ajax({
		type: "POST",
		url: "/subscribe",
		data: "email="+$("#email-field").val(),
		beforeSend: function() {	
			$('#newsletter').addClass('disabled');
			$('#submit-button').disabled = true;
			
			$('#notice').html("Subscribing to newsletter..."); 
			$('#notice').addClass('status');
			$('#notice').removeClass('error');
		},
		error: function() {
			$('#newsletter').removeClass('disabled');
			$('#notice').html("Something went wrong. Please try again."); 
			$('#notice').addClass('error');
			$('#notice').removeClass('status');
			
			$('#submit-button').disabled = false;
		},
		success: function(msg){
			$('#newsletter').html("You have successfully subscribed. Thank you!"); 
		}
	 });
}


function togglePlaceholder() {
	var searchField = document.getElementById('email-field');

	if(searchField.value == searchField.getAttribute('alt')) {
		searchField.value = '';
		searchField.setAttribute('style', 'color: #FFF;');
	} 

	else if(searchField.value == '') {
		searchField.value = searchField.getAttribute('alt');
		searchField.removeAttribute('style');
	}
}


function setInitialFocus() {
	
	togglePlaceholder();

	var photoDivs = document.getElementsByClassName("photos");

	for(var key in photoDivs) {
		var div = photoDivs[key];

		if(div.childNodes)
			div.setAttribute('class', div.getAttribute('class')+' fade-in')
	}


	var photoHolders = document.getElementsByClassName("photo-holder");

	for(var key in photoHolders) {
		var holder = photoHolders[key];

		if(holder.childNodes && holder.style.width < 650)
			holder.addEventListener("mouseover", makePhotoFocus, true);
	}
	
		
	var blurryPhotoHolders = document.getElementsByClassName("photo-holder blurry");
	
	for(var key in blurryPhotoHolders) {
		var holder = blurryPhotoHolders[key];
		
		if(holder.childNodes) {
			var sharpPhoto = holder.childNodes[1];	
			var blurryPhoto = holder.childNodes[3];
		}
	}
}


function playMovie(name, element) {
	$(element).html('<video width="320" height="460" autoplay controls><source src="http://shows-app.s3.amazonaws.com/site/'+name+'.m4v" type="video/mp4" /><source src="http://shows-app.s3.amazonaws.com/site/'+name+'.ogv" type="video/ogg" />Your browser does not support the video tag.</video>');
}

function makePhotoFocus() {
	
	var thisPhotoHolder = this
	var thisSharpPhoto = thisPhotoHolder.childNodes[1];	
	var thisBlurryPhoto = thisPhotoHolder.childNodes[3];
	
	var otherPhotoHolder = this.parentNode;
	
	if(this == otherPhotoHolder.childNodes[1]) {
		otherPhotoHolder = otherPhotoHolder.childNodes[3]
		thisPhotoHolder.setAttribute('class', 'photo-holder left sharp')
		otherPhotoHolder.setAttribute('class', 'photo-holder right blurry')
	}
		
	else {
		otherPhotoHolder = otherPhotoHolder.childNodes[1]
		thisPhotoHolder.setAttribute('class', 'photo-holder right sharp')
		otherPhotoHolder.setAttribute('class', 'photo-holder left blurry')
	}
}

function toggleSegmentedControl(el, force) {

	if(force != 'dates' && (force == 'artists' || el.getAttribute('class').indexOf('right') != -1)) {
		el.setAttribute('class', 'left moving')
		setTimeout(function(){el.setAttribute('class', 'left still disabled-animation')}, 200)
		document.getElementById('dual-artists').style.opacity = 1;
	}
		
	else {
		el.setAttribute('class', 'right moving')
		setTimeout(function(){el.setAttribute('class', 'right still disabled-animation')}, 200)
		document.getElementById('dual-artists').style.opacity = 0;
	}
}
