// JavaScript Document
function emailIsValid(email) {
	var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;
	if(reg.test(email) == false) {
		return false;
	}else{
		return true;
	}
}

function initCommentForm(){
	formSubmitted = false;
	
	var submitBt = $("#commentform #submit-bt");
	var nameField = $("#commentform #author");
	var emailField = $("#commentform #email");
	var urlField = $("#commentform #url");
	var commentField = $("#commentform #comment");
	
	submitBt.click(function(){
		if(validateCommentsForm()){
			$("#commentform").submit();
		}else{
				
		}
		return false;
	})
	
	nameFieldDefault = nameField.val();
	emailFieldDefault = emailField.val();
	urlFieldDefault = urlField.val();
	
	nameField.focus(function(){
		if($(this).val() == nameFieldDefault){
			$(this).val("");
		}
	})
	
	nameField.blur(function(){
		if($(this).val() == ''){
			$(this).val(nameFieldDefault);
		}
	})
	
	emailField.focus(function(){
		if($(this).val() == emailFieldDefault){
			$(this).val("");
		}
		if(formSubmitted){
			validateCommentsForm();
		}
	})
	
	emailField.blur(function(){
		if($(this).val() == ''){
			$(this).val(emailFieldDefault);
		}
		if(formSubmitted){
			validateCommentsForm();
		}
	})
	
	urlField.focus(function(){
		if($(this).val() == urlFieldDefault){
			$(this).val("");
		}
	})
	
	urlField.blur(function(){
		if($(this).val() == ''){
			$(this).val(urlFieldDefault);
		}
		if(formSubmitted){
			validateCommentsForm();
		}
	})
	
	commentField.blur(function(){
		if(formSubmitted){
			validateCommentsForm();
		}
	})
}


function validateCommentsForm(){
	var errors = [];
	
	var nameField = $("#commentform #author");
	var emailField = $("#commentform #email");
	var urlField = $("#commentform #url");
	var commentField = $("#commentform #comment");
	
	nameField.css("border","solid 1px #666666");
	emailField.css("border","solid 1px #666666");
	commentField.css("border","solid 1px #666666");
	

	if(nameField.val().length==0 || nameField.val() == nameFieldDefault){
		errors.push({field:nameField,msg:"Please enter a name"})
	}
	if(!emailIsValid(emailField.val())){
		errors.push({field:emailField,msg:"Please enter a valid email"})
	}
	if(commentField.val().length==0){
		errors.push({field:commentField,msg:"Please enter a comment"})
	}
	
	for(var i=0;i<errors.length;i++){
		errors[i].field.css("border","solid 2px #990000");
	}
	formSubmitted = true
	if(errors.length > 0){
		return false;
	}else{
		return true;	
	}
	
}



$(document).ready(function(){
	$("#like-box").html('<iframe src="http://www.facebook.com/plugins/likebox.php?href=http%3A%2F%2Fwww.facebook.com%2Fpremiumbeat&amp;width=225&amp;colorscheme=light&amp;show_faces=true&amp;stream=false&amp;header=true&amp;height=300" scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:225px; height:300px;" allowTransparency="true"></iframe> ')
})















var currentSound = null;
var selectedSound = null;

$(document).ready(function(){
		   
	var playerId = 0;
	$("a").each(function(){
		var href = $(this).attr("href");
		var text = $(this).html();
		if(href.indexOf(".mp3")!=-1){
			$(this).html('Play track');
		   $(this).addClass("play-bt");
		   $(this).click(function(e){
				if(!$(this).hasClass('playing')){
					unsetSelectedSound()
					selectedSound = $(this);
					setSelectedSound();
				}else{
					unsetSelectedSound()
				}
				return false;
			});
		}
		
		if(text.indexOf("Buy track")!=-1){
			$(this).addClass("buy-track");
		}
	})

	
	
	/* SOUND MANAGER */
	soundManager.url = '/javascript/soundManager/';
	soundManager.debugMode = false;
	soundManager.flashVersion = 9;
	soundManager.useFlashBlock = false; // optionally, enable when you're ready to dive in
	
	// soundManager.useHTML5Audio = true;
	soundManager.onready(function() {
	  // Ready to use; soundManager.createSound() etc. can now be called.
	  
	});
	
	setSelectedSound = function(){
		//selectedSound.closest('tr').addClass('playing')
		selectedSound.html('Stop track');
		selectedSound.removeClass('playing');
		selectedSound.addClass('playing');
		playSound(selectedSound.attr('href'));	
	}
	
	unsetSelectedSound = function(){
		
		if(currentSound){
			currentSound.stop();
			currentSound.destruct();
		}
		if(selectedSound){
			selectedSound.html('Play track');
			selectedSound.removeClass('playing');
			//selectedSound.closest('tr').removeClass('playing')
			selectedSound = null;
		}
	}
	
	playSound = function(p_path){
		//p_path = 'The+Hands+Of+Stone+-+Deeds.mp3'
		currentSound = soundManager.createSound({
			id:'sfx',
			//serverURL: 'rtmp://s2cbc6ndie9kpq.cloudfront.net/cfx/st/',
			url:p_path,
			onfinish:function() {
				unsetSelectedSound();
			}
		}).play();
		
	}
	
});

$(document).ready(function(){
	initCommentForm();
	
	var scrollingDiv = $(".share-box");
	/*
	$(window).scroll(function(){
		//scrollingDiv.html(($(".post-wrapper").offset().top-$(window).scrollTop()))
		if(($(".share-box-wrapper").offset().top-$(window).scrollTop()-20)<0){
			if(!scrollingDiv.hasClass("fixed")){
				scrollingDiv.addClass("fixed");
			}
		}else{
			scrollingDiv.removeClass("fixed");	
		}	
	});
	*/
})

