/**
* handle premium content behaviors
*/

/*--- Initialization ---*/
//object initialized in calling page on domready

//PremiumMedia Constructor, extend Class
var PremiumMedia = Class.extend({
	init: function(type){
		switch(type){
			case 'results':
				this.nodes = $('li.document.premium a.content');//premium link elements in the search results
				break;
			case 'confirm':
				this.nodes = $('a.premium');
				break;
			default:
				this.nodes = $('li.document.premium a.content');//premium link elements in the search results
				break;
		}
		
		this.container = $('#container');
		this.documentId = null;
		this.documentLink = null;
		this.documentLinkText = null;
		this.type = type;//from what type of page is this called - 'results','confirm'
		
		//Dialog object initialization
		this.container.append("<div id='dialog'></div>");
		$('#dialog').dialog({
		    autoOpen: false,
		    bgiframe: true,
		    resizable: false,
		    height: 240,
		    width: 300,
		    modal: false,
		    overlay:
		            {
		                backgroundColor: '#000',
		                opacity: 0.5
		            }
		        ,
		    title: 'Default title...'
		});
		//signup form nodes
		this.createAccountFormNodes = this.createAccountForm();
				
		this.assign_behaviors();
	},
	
	assign_behaviors: function(){
		var lex = this;
		switch(this.type){
			case 'results':
				this.nodes.bind(
				'click' , function(e){
					lex.documentId 			= $(this).parent().attr("id");
					lex.documentLink		= $(this).attr("href");
					lex.documentLinkText	= $(this).text();
					if(FT_Globals.user){
						
						var userId		= FT_Globals.user.id || 0;
						var page		= document.location.pathname;
						var search		= document.location.search;//todo:url encode
						
						var json_url = "http://www.familytales.org/trackPremiumMediaClick_json.php?document_id=" + lex.documentId + "&user_id=" + userId + "&page=" + page + "&search=" + search;
					
						$.getJSON(json_url, function(json){
					  		//Allowed to see content or not?
					 		if(!json.error){
								//console.log('redirect to content: '+e.target);
								document.location.href = e.target;
								
							}else{
								var shortCredits = (parseFloat(json.cost) - parseFloat(json.credit)) * 100;
								var creditString = (shortCredits = 1) ? 'credit' : 'credits';
								
								$('#dialog').dialog('option', 'title', 'Dagnabit! not enough credit left.');
								$("#dialog").html("You are short " + shortCredits + " " + creditString + ".<p>Soon you will be able to earn credit through participation. For now please write hans.brough@yahoo.com for a bunch o\' free credit.</p>");
								$('#dialog').dialog('open'); 
							}
						});
					}else{//no user object - not logged in
						$('#dialog').dialog('option', 'title', 'Please log in.');
						$('#dialog').dialog('option', 'width', '400px');
						//$("#dialog").html("Don\'t have an account? It\'s easy to sign up!");
						$("#dialog").append(lex.createAccountFormNodes);
						$('#dialog').dialog('open');
						//now assign behaviors to form elements in dialog (if they do not already exist)
						//if( !FT_Globals.account || FT_Globals.account.type != 'add-inline' ){
							FT_Globals.account = new Account('add-inline');
						//}
					}
					return false;//to stop default behavior of link
				}
			);
			break;
			case 'confirm':
				this.nodes.bind(
					'click' , function(e){
						if(FT_Globals.user){
							lex.documentId 	= jQuery.url.setUrl( FT_Globals.confirmation['link'] ).param("id");
							var userId		= FT_Globals.user.id || 0;
							var page		= lex.type;
							var get_params	= FT_Globals.confirmation['cid'];

							var json_url = "http://www.familytales.org/trackPremiumMediaClick_json.php?document_id=" + lex.documentId + "&user_id=" + userId + "&page=" + page + "&search=" + get_params;
							
							$.getJSON(json_url, function(json){
						  		//Allowed to see content or not?
						 		if(!json.error){
									//console.log('redirect to content: '+e.target);
									document.location.href = FT_Globals.confirmation['link'];
								}else{
									//console.log('error returned');
								}
							});
						}
					}
				);
				break;
		}//end switch
	},
	
	createAccountForm: function(){
		var inlineForm = $('<form id="inline_create_account"></form>').append(
				'<p class="message">Don\'t have an account? It\'s easy to sign up!</p><fieldset><input type="text" id="email"><label for="email">email:</label></fieldset><fieldset><input type="password" id="password"><label for="password">password:</label></fieldset><fieldset><input type="password" id="confirm"><label for="confirm">confirm password:</label></fieldset><input type="button" id="add_user" value="send me an account confirmation email">');
				
		return inlineForm;
	}
	
	
});