$(document).ready(function() {
	
	/****** ADD THE DATE CONTROL TO ALL THE CORRECT FIELDS *******/
  $('.datefield').datepicker({numberOfMonths: 2, showButtonPanel: true});
  /****** MAKE ALL TEXT AREAS ELASTIC *******/
	//$('textarea').elastic();
	/****** MAKE ANYTHING WITH THE ID OF TABS A TABBED INTERFACE.. *******/
	$("#tabs").tabs();	
	
	/****** IF THE DELETE DIALOG IS RENDERED, GO AHEAD AND MAKE IT A DIALOG *******/
	$('#delete-dialog').dialog({autoOpen:false, bgiframe:true, modal:true, draggable:false, resizable:false, width:450, height:150});
	
	/** START THE FORMS SWAPPING **/
		var forms = $(".tabs-form");
		jQuery.each(forms, function() {
		  id = $(this).attr("id");
		  // preset the selected tab
		  var seltab = jQuery.Replace($('#' + id + ' #sub-nav li.active').attr("id"), '-tab', '');
		  $('#' + id).data("selectedtab", seltab);
	      //console.log($(this).attr("id"));
	      
	      // attach the click event to each item in the form
	      //console.log($('#' + id + ' #sub-nav li').click());
	      
	      // loop all the menu items attaching the click event to each to handle the navigations
	      $('#' + id + ' #sub-nav li').click(function(){
	      	var _tab = jQuery.Replace($(this).attr("id"), '-tab', '');
	      	 // swap the forms
	      	 $('#' + $('#' + id).data("selectedtab")).attr("class", "inactive");
	      	 $('#' + _tab).attr("class", "active");
	      	 // swap the tabs
	      	 $('#' + $('#' + id).data("selectedtab") + '-tab').attr("class", "inactive");
	      	 $('#' + $(this).attr("id")).attr("class", "active");
	      	 // update the data structure so we know where we are!
	      	 $('#' + id).data("selectedtab", _tab);
	      });
	    });
		//console.log($('#' + id).data("selectedtab"));
	
	/** END THE FORMS SWAPPING **/
	
	/** START THE GRID FUNCTIONS **/
		// Check all checkboxes if the select 
		if($('#select-all') != null){
			$('#select-all').click(function(){
				checked_status = this.checked;
				$("input[name='select_row']").each(function()
				{
					this.checked = checked_status;
				});

			});		
		}
	/** END THE GRID FUNCTIONS **/
	
	
	
		
		/** START THE INLINE CUSTOM TEXT EDITOR - USED IN THE CONFIGURATION SECTION AS WELL **/
		
		$(".customTextEditor").click(function(){
				// we need to make a call out to get all the custom text content so we can build the dialog....
				_positionid = $(this).attr("id");
				_addlFields = new Array(0);
				_returnValues = new Array(0);
				$.getJSON("/index.cfm/do/customize.currentCustomText", {positionid:_positionid},function(data){
				 // change the title of the dialog window
				 $('#customText').dialog('option', 'title', 'Editing: ' + data.position);
				 
				 $(data.fields).each(function(x, obj){
				 	
						// if the max length is 0, then use the WYSIWYG editor, otherwise use a text box.....
						if (obj.maxlength == 0) {
							$('#customText').append('<div><div>' + obj.usertype + '</div><textarea name="' + obj.fieldname + '" ></textarea></div>')
							// create an instance of the editor
							editor = CKEDITOR.replace(obj.fieldname, {toolbox: 'PostCourseDefault', CustomConfigurationsPath:'/thirdparty/ckeditor/config.js'});
							//set the current value of the editor
							editor.setData(obj.currentvalue);
						}else{
							$('#customText').append('<div><div>' + obj.usertype + '</div><input type="text" id="' + obj.fieldname + '" name="' + obj.fieldname + '" value="' + obj.currentvalue + '" /></div>')
							_addlFields.push(obj.fieldname)
						}
					});
					
			  	$('#customText').prepend('<div>' + data.details + '</div>');
					// store the positionid so it can be used when we submit the form...
					$('#customText').data("positionid", _positionid);
				});
				
				// open the dialog box...
				$('#customText').dialog('open');
				
			});
			
			
			$('#customText').bind('dialogclose', function(event, ui) {
				// clear all the HTML
				$('#customText').html('');
				// remove the positionid from the data store...
				$('#customText').removeData("positionid");
			  // loop all the instances removing each...
				 jQuery.each(CKEDITOR.instances, function(i, val) {
			      CKEDITOR.remove(val);
			    });
			});
			
			
			$(".custom-text").hover(
      function () {
        $(this).find('.custom-text-controls').fadeIn();
        //$(this).css({'background-color' : '#eeeeee'});
      }, 
      function () {
        $(this).find('.custom-text-controls').fadeOut();
        //$(this).css({'background-color' : 'none'});
      }
    );
	
		$("#customText").dialog({
					bgiframe: true,
					height: 500,
					width: 700,
					modal: true,
					autoOpen:false,
					draggable:true,
					resizable:false,
					buttons: {
						Save: function() {							
							jQuery.each(CKEDITOR.instances, function(i, val) {
							 	$.post("/index.cfm/do/customize.saveCustomText", { positionid:val.name, textValue:val.getData() }, function(data){}, "json");
							 	// if the inline text is rendered on the page..update it
							 	_data = val.getData(); 
							 	if(_data.length == 0)
							 		_data = '&nbsp;'
							 	$('.inline-' + val.name).find('.custom-text-value').html(_data);
					    });
							
						  jQuery.each(_addlFields, function(i, val) {
							 	$.post("/index.cfm/do/customize.saveCustomText", { positionid:val, textValue:$('#' + val).val() }, function(data){}, "json");
							 	// if the inline text is rendered on the page..update it
							 	_data = $('#' + val).val(); 
							 	if(_data.length == 0)
							 		_data = '&nbsp;'
							 	$('.inline-' + val.name).find('.custom-text-value').html(_data);
					    });
							
							$(this).dialog('close');
						},
						Cancel: function() {
							$(this).dialog('close');
						}
					}
				});
				
	/** END THE INLINE CUSTOM TEXT EDITOR - USED IN THE CONFIGURATION SECTION AS WELL **/
	
	
});

/****** QUICK DELETE FUNCTION *******/
function quickDelete(objectid, url){
	$('#delete-dialog').dialog('option', 'buttons', { "Cancel": function() { $(this).dialog("close"); }, "Ok": function() { location.href=url } });
	
	$('#delete-dialog-message').html($('#delete_' + objectid).children('.delete-link').attr('title'));
	$('#delete-dialog').dialog('open');
}

/** Include the standard form submit function **/
function submitForm(formid){$('#' + formid).submit();}	
