/*
*	Custom javascript functions.
*	@author David Tym <davidtym@greensprocket.com>
*	@copyright Copyright (c) 2010, Visual Odyssey Inc.
*   @date Thursday, September 08, 2011 - 9:15:48 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({
		invalidHandler: function(form, validator) {
			var errors = validator.numberOfInvalids();

			if (errors) {
				var message = '<div class="cms-doc-title">Oops!</div><p style="color: red;">You missed ' + errors + ' field(s) which have been highlighted. If no highlighted fields are visible and tabs are present, please check the other tabs for missed fields.</p>';
				$('#cms-error').html(message);
				$('#cms-error').show();
			} else {
				$('#cms-error').hide();
			}
			
		},
		rules: {
			Password: "required",
			VerifyPassword: {
				equalTo: "#Password"
			}
		}

	 })
})


/*
*	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);
				}
			});
		});
	};



/*
*	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'
		});
	});

/*
*	Calendar date picker
*/
	$(document).ready(function(){ 
		$('#StartDateTime').datepicker({
			changeMonth: true,
			changeYear: true,
			altField: '#Date', altFormat: 'yy-mm-dd',
			yearRange: '-10:+10'
		});
		$('#EndDateTime').datepicker({
			changeMonth: true,
			changeYear: true,
			altField: '#Date', altFormat: 'yy-mm-dd',
			yearRange: '-10:+10'
		});
	});
