/*
*	Custom javascript functions.
*	@author David Tym <davidtym@greensprocket.com>
*	@copyright Copyright (c) 2010, Visual Odyssey Inc.
*   @date Sunday, November 29, 2009 2:32:52 PM
*/

/*
*	JavaScript settings for variables.
*/

	var error_js001 = 'An unexpected connection error has occurred. Please try again in a few moments. If the problem persists please make note of the following code and contact your system administrator.  Code: js001.';


/*
*	Initialize JQuery tab and form validation.
*/

$(document).ready(function(){
	$('#cms-tabs').tabs();
	$("#cms-data-form").validate();
});


/*
*	Submit function for WYSIWYG editor
*	Function Details:
*		When sumitting through AJAX window the normal submit button is not used.  This confuses the editor so the following function bypasses the problem.  This function is called from the Ajax call "'Save': function()".
*/

function doSubmit()
{
//	document.result.cms-data-form.cms-editor.onsubmit();
//	document.result.cms-data-form.cms-editor.submit()
};


/*
*	Ajax calls to populate pages with content, load editing form and posting data via ajax to database.
*
*	Function Details:
*		Instance, a unique number so each ajax instance doesn't collide with another and cause errors.
*		Page, editing PHP page to load and handle data requests.
*		Name, a tag's name <!cms ... name="about" !> so it can be used in the database allowing duplicate tags to pull unique data.
*/

	function ajax(instance,page,name,id) {

		$(document).ready(function(){ 

			// AJAX call to populate page
			$.ajax({
				type: 'POST',
				url: '/plugins/'+page+'.php?name='+name+'&id='+id,
				cache: false,
				data: 'state=read',
				success: function(html){
					$('#result'+instance).html(html);
				},
				error: function(){
					$('#result'+instance).html(error_js001);
				}
			});

			//  Create pop-up dialog box
			$('#dialog'+instance).dialog({
				autoOpen: false,
				width: 840,
				modal: true, 
				position: ['center',150],
				buttons: {
					'Save': function() { 

						//doSubmit(); // Call WYSIWYG editor submit function.

						var str = $('form').serialize();

						// AJAX call to post data
						$.ajax({
							type: 'POST',
							url: '/plugins/'+page+'.php?name='+name+'&id='+id,
							data: 'state=post&' + str,
							success: function(html){
								$('#result'+instance).html(html);
							},
							error: function(){
								$('#result'+instance).html(error_js001);
							}
						});
						$(this).dialog('close');

					}, 
					'Cancel': function() { 
						$(this).dialog('close'); 
					} 
				}
			});
			
			// Create pop-up dialog	box
			$('#dialog_link'+instance).click(function(){

				// AJAX call to build form
				$.ajax({
					type: 'POST',
					url: '/plugins/'+page+'.php?name='+name+'&id='+id,
					data: 'state=form',
					success: function(html){
						$('#form'+instance).html(html);
					},
					error: function(){
						$('#form'+instance).html(error_js001);
					}
				});

				// Open dialog box
				$('#dialog'+instance).dialog('open');
				return false;
			});				

		}); 

	};


/*
*	Ajax call to populate template thumbnail section.
*/

	function templateThumbnail(file) {

		$(document).ready(function(){ 

			$.ajax({
				type: 'POST',
				url: '/cms.inc.php',
				cache: false,
				data: 'state=thumbnail&file='+file,
				success: function(html){
					$('#cms-thumbnail').html(html);
				},
				error: function(){
					$('#cms-thumbnail').html(error_js001);
				}
			});
		});
	};


/*
*	Ajax call to populate template thumbnail section.
*/

	function toolTip(code) {

		$(document).ready(function(){ 

			$.ajax({
				type: 'POST',
				url: '/cms.inc.php',
				cache: false,
				data: 'state=tooltip&code='+code,
				success: function(html){
					$('#cms-tooltip').html(html);
				},
				error: function(){
					$('#cms-tooltip').html(error_js001);
				}
			});
		});
	};


/*
*	URLRewrite script to populate input field from page name field.
*/

	function createURL(raw) {
		raw = raw.toLowerCase();
		raw = raw.replace(/^\s+|\s+$/g, "");			// trim leading and trailing spaces		
		raw = raw.replace(/[_|\s]+/g, "-");				// change all spaces and underscores to a hyphen
		raw = raw.replace(/[^a-z0-9-]+/g, "");			// remove all non-alphanumeric characters except the hyphen
		raw = raw.replace(/[-]+/g, "-");				// replace multiple instances of the hyphen with a single instance
		raw = raw.replace(/^-+|-+$/g, "");				// trim leading and trailing hyphens	

		document.getElementById('cms-data-form').URLRewrite.value = raw;
	};

	function Duplicate(raw) {
		document.getElementById('cms-data-form').MetaTitle.value = raw;
	};

/*
*	Form date picker
*/
	$(document).ready(function(){ 
		$('#Date').datepicker({
			changeMonth: true,
			changeYear: true,
			altField: '#Date', altFormat: 'yy-mm-dd',
			yearRange: '-10:+10'
		});
	});
