//DOM loaded
var counter=0;
function update_color(color){
	$('span#new_name.selected').css('color',color);
}
function play_control(player){
		target_player=$('#play.'+player);
		if(target_player.hasClass('playing')){
			$('#player').jPlayer("pause");
			$('.playing').find('img').attr('src','images/play.png');
			target_player.removeClass('playing').addClass('paused');
		}else{
			if(target_player.hasClass('paused')){
				$("#player").jPlayer("play");
				target_player.removeClass('paused');
			}else{
				$("#player").jPlayer( "setFile", "media/"+player+".mp3" ).jPlayer("play");
			}
			$('.playing').find('img').attr('src','images/play.png');
			$('.paused').removeClass('paused');
			$('.playing').removeClass('playing');
			target_player.addClass('playing').find('img').attr('src','images/pause.png');
		}
}

$(document).ready(function($){
	
	//$('#colorSelector').ColorPicker({
	//		color:'#0000ff',
	//		onShow:function (colpkr) {
	//				$(colpkr).fadeIn(500);
	//				return false;
	//				},
	//		onHide: function (colpkr) {
	//				$(colpkr).fadeOut(500);
	//				return false;
	//				},
	//		onChange: function (hsb, hex, rgb) {
	//				hex_color='#' + hex;
	//				$('#colorSelector #color_color').css('backgroundColor', hex_color);
	//				update_color(hex_color);
	//	}
	//});
	init_drag();
	$('#player').jPlayer({
		ready: function ()
		{
			this.element.jPlayer("setFile", "../media/moon.mp3").jPlayer();
		
		}});

	$(".moon").click(function(){
		play_control('moon');
	});
	
	$(".nites").click(function(){
		play_control('nites');
	});
	
	$(".remember").click(function(){
		play_control('remember');
	});
	
});



function draw_name(){
	counter +=1;
	value=$('input#name_input').val();
	addition='<span onClick="select('+counter+');" id="new_name" style="font-family:\'Budmo\'; font-size:3em;left:20%;top:20%;" class="'+counter+' selected">'+value+'</span>';
	$('#text_stage').html(addition);
	$('#text_stage').css('display','block');
	select(counter);
	init_drag();
}
function check_input(){
	value=$('input#name_input').val();
	if (value=='' || value=='Your name idea here...') {return false;} else {return true;}
}
function goto_step(step){
	step_id='#step_'+step;
	if (step==2){
		if(check_input()){
			$('.step.active').fadeOut('slow',function(){
				$(step_id).fadeIn('slow');
				draw_name();
				//$(step+' h2').delay(5000).fadeOut();
				$('#alerts').fadeIn();
				}
			);
		}else
		{
			alert('Before proceeding, please enter a name idea in the field.  Thanks!');
		}
	}else{
		$('.step').fadeOut('fast');
		$(step_id+'.step').addClass('active').fadeIn('slow');
	}
	if(step==4){
		
	}
}
function toggle_add_word(){
	current = $('#text_stage span').text();	
	$('#add_another').toggle();
	$('input#add_word').val(current);
}

function edit_word(){
	edit_word_val=$('input#add_word').val();
	$('#text_stage span').text(edit_word_val);
	$('#add_another').css('display','none');
}
function add_word(){
	counter +=1;
	value=$('input#add_word').val();
	addition='<span onClick="select('+counter+');" id="new_name" class="'+counter+'">'+value+'</span>';
	current_html=$('#text_stage').html();
	$('#text_stage').html(current_html+addition);
	init_drag();
	toggle_add_word();
	select(counter);
}
//functions to handle input states and etc etc etc etc 
function clear_input(input_id){
	input_id='input#'+input_id;
	$(input_id).val('');
}


function restart(){
	counter =0;
	$('#text_stage span').fadeOut().parent('#text_stage').html('');
	$('input#name_input').val('Your name idea here...');
	$('#alerts').css('display','none');$('#finish').css('display','none');
	$('.step').fadeOut('slow',function(){$('#step_1').fadeIn('slow');});
	
}
function init_drag(){
	$('#text_stage span').draggable();
}

function select(thisclass){
	$('.selected').removeClass('selected');
	$('.'+thisclass).addClass('selected');
	cur_font=$('span.selected').css('font-family');
	cur_color=$('span.selected').css('color');
	$('#color_color').css('background-color',cur_color);
	$('select#font').val(cur_font);
}
function clear_select(){$('.selected').removeClass('selected');}

function change_font(){
	font_family=$('select#font').val();
	$('span.selected').css('font-family',font_family);
}

function font_size_webkit(inc){
	var currentFontSize = $('span.selected').css('font-size');
	//alert(currentFontSize);
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum*inc;
    $('span.selected').css('font-size', newFontSize+'em');
}
function font_size(inc){
	var currentFontSize = $('span.selected').css('font-size');
	//alert(currentFontSize);
    var currentFontSizeNum = parseFloat(currentFontSize, 10);
    var newFontSize = currentFontSizeNum*inc;
    $('span.selected').css('font-size', newFontSize);
}

function remove_span(){
	$('span.selected').remove();
}

function save(){
	email=$('#email').val();
	if(email==''){
			$('#email_warning').text('Please enter an email address to continue!');
	}else{
		random_hash = Math.random();
		old_save=$('#save_button').html();
		$('#save_button').html('<img src="../images/load.gif" />');
				main_offset=$('#text_stage').offset();
		m_off_l=main_offset.left;
		m_off_r=main_offset.right;
		m_off_t=main_offset.top;
		stylesheet='<style>';
		$('#text_stage').children().each(function(){
				me=$(this);
				offset=me.offset();
				thisclass=me.attr('class');
				thisclass=thisclass.substr(0,2);
				left=offset.left-m_off_l; top=offset.top-m_off_t;
				font=me.css('font-family');
				font_size=me.css('font-size');
				color=me.css('color');
				stylesheet+=' span.'+thisclass+'{left:'+left+'px; top:'+top+'px; font-family:'+font+'; font-size:'+font_size+'; font-color:'+color+';}';
				
		});
		stylesheet+='#text_stage{display:block;}</style>';
		markup=$('#text_stage').html();
		
		name=$('#text_stage').text();
		thoughts=$('textarea#thoughts').val();
		suggestions=$('textarea#suggestions').val();
		$.post("save.php",{random:random_hash, email:email, content:markup, style:stylesheet, name:name, thoughts:thoughts, suggestions:suggestions},function (data){
			$('#final').fadeOut();
			$('#finish').fadeIn();
			$('#step_4 h2').text('Thank you!');	
			$('#save_button').html(old_save);
		});
	}
}

function save_light(){
	email=$('#email').val();
	if(email==''){
			$('#email_warning').text('Please enter an email address to continue!');
	}else{
		random_hash = Math.random();
		thoughts=$('textarea#thoughts').val();
		suggestions=$('textarea#suggestions').val();
		$.post("save.php",{random:random_hash, email:email, thoughts:thoughts, suggestions:suggestions},function (data){
			$('#final').fadeOut();
			$('#finish').fadeIn();
			$('#step_4 h2').text('Thank you!');	
			$('#save_button').html(old_save);
		});
	}
}

function show_email(){
	$('#vote_instructions').css('display','none');
	$('#step_1').css('display','block');;

}

function move_on(){
	$('#image_10').css('display','none');
	$('#step_4').css('display','block');
}
function move_next(){
	$('#laugh').css('display','none');
	$('#image_11').css('display','block');
}

