$(document).ready(function(){
	$('#close_for_ringtone').bind("click", hide_ringtone_download_box );
	$('#close_for_wallpaper').bind("click", hide_wallpaper_download_box );
	$('#submit_for_wallpaper').bind("click", confirm_wallpaper_download);
	$('#submit_for_ringtone').bind("click", confirm_ringtone_download);
	$('#submit_for_alerts').bind("click", register_alerts);

	//mask phone number boxes.	
	$('#mobile_number_for_alerts').mask("(999) 999-9999");
	$('#mobile_number_for_ringtone').mask("(999) 999-9999");
	$('#mobile_number_for_wallpaper').mask("(999) 999-9999");         
	
	//add hover to wallpapers   
	$('#wallpapers img').fadeTo('fast', 0.6);      
	$('#wallpapers img').hover(function() {       
		$(this).fadeTo('fast', 1.0);
		    }, 
		function() {
			$(this).fadeTo('slow', 0.6);   
	});       
	
	$('.ringtone_row_item').each( function() {
		id = this.id;
		AssignRingtoneToPlayer('player' + id, id, 'play' + id, 'pause' + id);      		
	});

});    


function show_message_box(section, message, text_color, fadeout) {     
	$('#' + section + '_message_box').slideDown('slow', function() { 
		$(this).html(message); 
		$(this).css('color', text_color);
	});
	
	if(fadeout > 0){
		setTimeout( function () {
			$('#' + section + '_message_box').fadeOut(5000);
		}, fadeout);     
	}  
}    

function register_alerts() {
	if (checkInternationalPhone(unformatPhone($('#mobile_number_for_alerts').val())) == false) {          
		$('#mobile_number_for_alerts').focus();          
		show_message_box('alerts', 'Invalid phone number.', 'red', 0);          
		return false;      
	}        
	
	if ($('#check_terms_for_alerts:checked').val() == undefined) {          
		$('#check_terms_for_alerts').focus();          
		show_message_box('alerts', 'Please agree to terms and conditions.', 'red', 0);          
		return false;      
	}        
	
	RegisterForAlerts(1604, unformatPhone($('#mobile_number_for_alerts').val()));        
	show_message_box('alerts', 'Successfully registered for alerts.You should now receive a message on your phone. To confirm, please reply YES to the message.', 'green', 7000);    
	
}    

function AssignRingtoneToPlayer(player, mp3name, play, pause) {   
	$('#' + player).jPlayer({        
		ready: function () {            
			$(this).setFile("/services/render_preview/" + mp3name);        
			},        
			oggSupport: false,        
			swfPath: '/js/'     
	});    
	$('#' + player).jPlayerId('play', play);  
	$('#' + player).jPlayerId('pause', pause);  
}    

function show_ringtone_download_box(id, msgtext, price){   
	$('#ringtone_download_box').slideDown('slow', function () { 
		$('#mobile_number_for_ringtone').focus(); 
	});
	$('#ringtone_price').html("Cost: $" + price);
	$('#selected_ringtone').html('Download ringtone: ' + msgtext);   
	$('#selected_ringtone_id').val(id);   
	$('#check_terms_ringtone').attr('checked', false);  
}    

function show_wallpaper_download_box(id, msgtext, price){   
	$('#wallpaper_download_box').slideDown('slow', function () { 
		$('#mobile_number_for_wallpaper').focus(); 
		});
	$('#wallpaper_price').html("Cost: $" + price);
	$('#selected_wallpaper').html('Download wallpaper: ' + msgtext);   
	$('#selected_wallpaper_id').val(id);   
	$('#check_terms_wallpaper').attr('checked', false);    
}    

function hide_wallpaper_download_box(){   
	$('#wallpaper_download_box').slideUp('slow');   
	$('#wallpaper_result').slideUp('slow', function() { 
		$('#wallpaper_result').html(''); 
	});   
	$('#wallpaper_message_box').html('');   
	$('#check_terms_for_wallpaper').checked = false;  
}    

function hide_ringtone_download_box(){   
	$('#ringtone_download_box').slideUp('slow');   
	$('#ringtone_message_box').html('');   
	$('#check_terms_for_ringtone').checked = false;  
}    

function confirm_ringtone_download(){   
	if(checkInternationalPhone(unformatPhone($('#mobile_number_for_ringtone').val())) == false) {    
		$('#mobile_number_for_ringtone').focus();    
		show_message_box('ringtone', 'Invalid phone number ', 'red', 0);    
		return false;   
	}   
	if($('#check_terms_for_ringtone:checked').val() == undefined ) {    
		$('#check_terms_for_ringtone').focus();    
		show_message_box('ringtone', 'Please agree to terms and conditions.', 'red', 0);    
		return false;   
	}      
	RequestContent($('#selected_ringtone_id').val(), unformatPhone($('#mobile_number_for_ringtone').val()));      
	hide_ringtone_download_box();   
	show_message_box('ringtone_result', 'You have successfully requested the ringtone. You should now receive a message on your phone. To confirm, please reply YES to the message.', 'green', 7000);       
	$('#ringtone_message_box').fadeOut(5000);  
}    

function confirm_wallpaper_download(){   
	if(checkInternationalPhone(unformatPhone($('#mobile_number_for_wallpaper').val())) == false) {    
		$('#mobile_number_for_wallpaper').focus();    
		show_message_box('wallpaper', 'Invalid phone number', 'red', 0);    
		return false;   
	}   
	if($('#check_terms_wallpaper:checked ').val() == undefined){    
		$('#check_terms_wallpaper').focus();    
		show_message_box('wallpaper', 'Please agree to terms and conditions.', 'red', 0);    
		return false;   
	}      
	RequestContent($('#selected_wallpaper_id').val(), unformatPhone($('#mobile_number_for_wallpaper').val()));      
	hide_wallpaper_download_box();   
	show_message_box('wallpaper_result', 'You have successfully requested the wallpaper. You should receive a message on your phone. To confirm, please reply YES to the message.', 'green', 7000);   
	$('#wallpaper_message_box').fadeOut(5000);  
}
		
		
		
		
		
		
