var ajaxURL = '16';

$j(document).ready(function(){

	initUpdateTVSelect();
	initCloseParents();
	initEditableTVs();
	initEditableTools();
	
	//initCourseReordering();
	
	initPrompt();
	initSchedule();
	initQMFields();
});

function isSet( variable ){
	return( typeof( variable ) != 'undefined' );
}

function initQMFields(){


	var editButton = $j("#QM_edit");
	var addButton = $j("#QM_add");
	var deleteButton = $j("#QM_delete");
	
	if($j("#editable_tvs").length == 1){
		if(editableTVs != ""){
			var editableTVsEdit = "&tvs=" + editableTVs + "&a=27";
			var currentHref = $j(editButton).attr("href").replace(/a=27/, editableTVsEdit);
			$j(editButton).attr("href", currentHref);
		}
	}
	
	if($j("#editable_child_tvs").length == 1){
		if(editableChildTVs != ""){
			var editableTVsAdd = "&tvs=" + editableChildTVs + "&a=4";
			var currentHref = $j(addButton).attr("href").replace(/a=4/, editableTVsAdd);
			$j(addButton).attr("href", currentHref);
		}
	} else {
		$j(addButton).remove();
	}
	
	if($j("#allow_deletion").length == 1){
		
	} else {
		$j(deleteButton).remove();
	}
	
	$j("#editable_tvs").remove();
	$j("#editable_child_tvs").remove();
	$j("#allow_deletion").remove();
}

function initSchedule(){

	var add_button = $j(".add_to_schedule");
	var save_button = $j(".save_to_schedule").hide();
	var delete_button = $j(".delete_from_schedule");
	
	$j(delete_button).click(function(e){
		e.preventDefault();
		
		var table = $j(this).parents('tr');
		var schedule_id = $j(table).find('input.input_schedule_id').val();
		
			$j.post("index.php?id=" + ajaxURL, 
			    {
			    	action: "removeFromSchedule",
			    	schedule_id: schedule_id
			    },
  			    function(data){
  			    	console.log(data);
    		    	window.location.href = window.location.href;
  			    }
  			);

		
	});
	
	$j(add_button).click(function(e){
	
		e.preventDefault();
		var age = "<td><input type='hidden' name='course_id' class='input_course_id' value='" + document_id + "'/><input type='text' name='age' value='' class='input_age' /></td>";
		var days = "<td><input type='text' name='days' value='' class='input_days' /></td>";
		var time = "<td><input type='text' name='time' value='' class='input_time' /></td>";
		var date = "<td><input type='text' name='date' value='' class='input_date' /></td>";
		var sessions = "<td><input type='text' name='sessions' value='10 sessions' class='input_sessions' /></td>";
		var fees = "<td><input type='text' name='fees' value='HK$ 3,900' class='input_fees' /></td><td></td>";
		
		$j(this).parents('table').find('tbody tr:last').after("<tr class='unsaved'>" + age + days + time + date + sessions + fees + "</tr>");
		
		$j(this).hide();
		$j(save_button).show();
	
	});
	
	$j(save_button).click(function(e){
		e.preventDefault();
		var table = $j(this).parents('table');
		var course_id = $j(table).find('input.input_course_id').val();
		var age = $j(table).find('input.input_age').val();
		var days = $j(table).find('input.input_days').val();
		var time = $j(table).find('input.input_time').val();
		var date = $j(table).find('input.input_date').val();
		var sessions = $j(table).find('input.input_sessions').val();
		var fees = $j(table).find('input.input_fees').val();
		
		if(age == '' || days == '' || time == '' || date == '' || sessions == '' || fees == ''){
		
			alert('Make sure to fill out all fields.');
			
		} else {
	
			$j.post("index.php?id=" + ajaxURL, 
			    {
			    	action: "saveToSchedule",
			    	course_id: course_id,
			    	age: age,
			    	days: days,
			    	time: time,
			    	date: date,
			    	sessions: sessions,
			    	fees: fees
			    },
  			    function(data){
    		    	window.location.href = window.location.href;
  			    }
  			);
	
		}
		
	});

}

function initCourseReordering(){
	/*
$j("#course_list").sortable({
		handle: '.handle',
		update: function(){
			var serialized = $j("#course_list").sortable('serialize');
			
			
			$j.post("index.php?id=" + ajaxURL, 
		    {
		    	action: "reOrder",
		    	parent_id: document_id,
		    	serialized: serialized
		    },
  		    function(data){
  		    	console.log(data);
    	    	//window.location.href = "index.php?id=" + data;
  		    }
  		);
		
		}
	
	});

*/}

function initPrompt(){

	var prompts = $j("#cms_panel .prompt");
	var empty = "<span class='warning'>!</span> Some required fields are still empty!";
	if(prompts.length > 0){
		$j("#cms_status").prepend(empty);
	}

}

function createDocument(newTemplate, newParent, flashdataKey){
	
	var title_user = prompt("Choose a title, click ok and then wait a few seconds for your new document to load.", default_title);
	if(title_user != "" && title_user != null){
		$j.post("index.php?id=" + ajaxURL, 
		    {
		    	action: "newDocument",
		    	parent_id: newParent,
		    	title: title_user,
		    	flashdata_key: flashdataKey,
		    	template_id: newTemplate
		    },
  		    function(data){
  		    	console.log(data);
    	    	window.location.href = "index.php?id=" + data;
  		    }
  		);
  	}
  	
  	return false;
	
}

function publishDocument(doc_id, publish_action, flashdataKey, redirectTo){
	
	$j.post("index.php?id=" + ajaxURL, 
	    {
	    	action: "publishAction",
	    	flashdata_key: flashdataKey,
	    	doc_id: doc_id,
	    	publish_action: publish_action
	    },
  	    function(data){
  	    	console.log(data);
        	window.location.href = "index.php?id=" + redirectTo;
  	    }
  	);
  	
  	
  	return false;
}

function deleteDocument(doc_id, delete_action, flashdataKey, redirectTo){
	
	if(confirm("Are you sure you want to delete this document?")){
	
		$j.post("index.php?id=" + ajaxURL, 
		    {
		    	action: "deleteAction",
		    	flashdata_key: flashdataKey,
		    	doc_id: doc_id,
		    	delete_action: delete_action
		    },
  		    function(data){
  		    	console.log(data);
    	    	window.location.href = "index.php?id=" + redirectTo;
  		    }
  		);
  	
  	}
  	
  	return false;
}

function initEditableTools(){

	$j("#tools ul").prepend($j("#editable_tools ul").html());
	$j("#editable_tools").remove();

}

function initEditableTVs(){

	/*
$j("#template_variables").append($j("#editable_tvs").html());
	$j("#editable_tvs").remove();
*/

}

function initCloseParents(){
	
	var closers = $j(".closeParent");
	closers.each(function(){
		$j(this).click(function(e){
		
			e.preventDefault();
			if($j(this).hasClass('open')){
				$j(this).parent().animate({
			
					left: '-190px'	
			
				});
				$j(this).removeClass('open');
				$j(this).addClass('closed');
			} else {
				$j(this).parent().animate({
			
					left: '0px'	
			
				});
				$j(this).removeClass('closed');
				$j(this).addClass('open');
			}
			
			//$j(this).parent().find('.openParent').show(0);
			
		
		});
		
	
	});


}


function initUpdateTVSelect(){

	var selects = $j("select.updateTV");
	
	selects.each(function(){
	
		$j(this).change(function(e){
			var doc_id = $j(this).val();
			var value = $j(this).parents('form').find("input[name='value']").val();
			var tv_name = $j(this).parents('form').find("input[name='tv_name']").val();
			var flashdata_key = $j(this).attr("id");
			
			
			$j.post("index.php?id=" + ajaxURL, 
				{
					action: "updateTV",
					doc_id: doc_id,
					value: value,
					flashdata_key: flashdata_key,
					tv_name: tv_name
				},
  				function(data){
  					console.log(data);
    				window.location.href = window.location.href;
  				}
  			);

			
		
		});
	
	});

}


function toggleCMSLinks(){

	$j("#container .authorised").toggle('slow');

}
