$(document).ready(function() {
	// Fixes pngs on IE 6 but only if the image has width/height in its attributes
	$(document).pngFix();		   
	
	$('.date-pick').datePicker();
	
	
	$('.rhrList li:last').css('border-bottom','none');	
	$('.rhr').corner("bottom");
	$('.rhr_sub').corner();	   
		   
		   
	//Facebox lightbox effect
	$('a[rel="facebox"]').facebox();	   
	
	// Will alt Row Colors for the list tables
	$('#sort tr').filter(':odd').addClass('odd');
	$('#sort th:last').css('border-right','none');
	$('#sort th:first').css('border-left','none');
    $('#sort tr:even').addClass('even');
	
	
	// Will alt Row Colors for the form tables
	$('.formInfo tr').filter(':odd').addClass('odd_form');
    $('.formInfo tr:even').addClass('even_form');
	
	
	// Table inside a table has no border bottom.
	$('table table tr td').css('border-bottom','none');
	
	// Any External Link will open up a new window.
	$('a[rel="external"]').click( function(){
        window.open( $(this).attr('href') );
        return false;
    });
	
	// Will handle any PDF to open up without sending you to a new page	
	$("a[href*=.pdf]").click(function()	{
		$(this).attr({"target":"_self"});
		return false;
	});
	
	
	// Show - Hide the child page row in the add-edit content page
	$("#isParentPage").click(function(){
		$("#sh_childPage").toggle();
		$("#sh_GrandChildPage").toggle();
 	});
	// This will deal with the change of the parent page dropdown in the contact page
	$("#parentPage").change(function() {
		var selected = $("#parentPage option:selected").val(); 
		//alert(selected);
		$("#sh_GrandChildPage").show();
		$.post("get-parent-child.php", { parentPage: selected}, 
		function(data){
			$('#showGrandChildren').html(data);
		});
		return true;
	});	
	
	
	// This will show the JS Confirm Delete using the rel="confirmdelete"
	$('A[rel="confirmDelete"]').click(confirmTheClick); 
	
	// Brings up the information from the help icon for items in the form so users will know what each item is.
	$('.help').cluetip({splitTitle: '|'});
	
	
	
	

	
	
	
	
	/****************************************************
		FORM VALIDATION
	****************************************************/
	$("#companyState").mask("aa");
	$("#companyPhone").mask("(999) 999-9999");
	$("#companyFax").mask("(999) 999-9999");
	$("#companyTollFree").mask("(999) 999-9999");
	$("#companyZipCode").mask("99999");
	
	// VALIDATE : NEWS ADD FORM
	$("#news-add").validate({
		rules: {
			categoryID: "required",
			articleTitle: "required",
			articleDescription: "required",
			pageTitle: "required",
			metaKeywords: "required",
			metaDescription: "required"
		},
		messages: {
			categoryID: " [Required]",
			articleTitle: "<br/>[Required]",
			articleDescription: "<br/>[Required]",
			pageTitle: "<br/>[Required]",
			metaKeywords: "<br/>[Required]",
			metaDescription: "<br/>[Required]"
		}
	});
	// VALIDATE : PAGE ADD FORM
	$("#pageAdd").validate({
		rules: {
			contentName: "required",
			contentHeader: "required",
			pageTitle: "required",
			pageKeyword: "required",
			pageDesription: "required"
		},
		messages: {
			contentName: "<br/>[Required]",
			contentHeader: "<br/>[Required]",
			pageTitle: "<br/>[Required]",
			pageKeyword: "<br/>[Required]",
			pageDesription: "<br/>[Required]"
		}
	});
	
	// VALIDATE : PAGE ADD FORM
	$("#catAdd").validate({
		rules: {
			categoryName: "required",
			title: "required",
			keywords: "required",
			description: "required"
		},
		messages: {
			categoryName: "<br/>[Required]",
			title: "<br/>[Required]",
			keywords: "<br/>[Required]",
			description: "<br/>[Required]"
		}
	});
	
	// VALIDATE : EDIT USERS
	$("#user-edit").validate({
		rules: {
			roleID: "required",
			firstName: "required",
			lastName: "required",
			emailAddress: "required",
			displayName: "required",
			userName: "required",
			password: "required"
		},
		messages: {
			roleID: "<br/>[Required:]",
			firstName: "<br/>[Required:]",
			lastName: "<br/>[Required:]",
			emailAddress: "<br/>[Required:]",
			displayName: "<br/>[Required:]",
			userName: "<br/>[Required:]",
			password: "<br/>[Required:]"
		}
	});
	
	// VALIDATE : ADD File
	$("#file-add").validate({
		rules: {
			txtFileName: "required",
			file: "required"
		},
		messages: {
			txtFileName: "<br/>[Required:]",
			file: "<br/>[Required:]"
		}
	});
	
	// VALIDATE : Edit File
	$("#file-edit").validate({
		rules: {
			txtFileName: "required"
		},
		messages: {
			txtFileName: "<br/>[Required:]"
		}
	});
	
	
	
	
	/****************************************************
		SETTINGS FUNCTIONS
	****************************************************/
	
	
	// Making Settings Section more manageable with toggles
	$("#companyInformationBox").click(function(){
		$("#companyBox").slideToggle();	
		$("#companyTable").css("marginBottom",0);
		var el = $("#companyInfoShow");  
		var state = $("#companyInfoShow").html();
		state = (state == 'Hide' ? '<span id="shText">Show Panel</span>' : '<span id="shText">Hide Panel</span>');					
		el.replaceWith(state); 
		return false;
	});
	
	// Making Settings Section more manageable with toggles
	$("#analyticsInformationBox").click(function(){
		$("#analyticsBox").slideToggle();	
		$("#analyticsTable").css("marginBottom",0);
		var el = $("#analyticsShow");  
		var state = $("#analyticsShow").html();
		state = (state == 'Hide' ? '<span id="shText">Show Panel</span>' : '<span id="shText">Hide Panel</span>');					
		el.replaceWith(state); 
		return false;
	});

	$('#featuresInformationBox').click(function(){
		$("#featuresBox").slideToggle();	
		$("#featuresTable").css("marginBottom",0);
		return false;
	});
	$('#additionalInformationBox').click(function(){
		$("#additionalBox").slideToggle();	
		$("#additionalTable").css("marginBottom",0);
		return false;
	});
	
	
	
	$('#projectList').sortable({
		opacity: '0.5',
		update: function(e, ui){
			serial = $(this).sortable("serialize");	
			$.ajax({
				url: "project-list-sort.php",
				type: "POST",
				data: serial,
				// complete: function(){},
				success: function(feedback){
					$('#data').html(feedback);
				}
				// error: function(){}
			});
		}
	}).css("cursor","pointer");
	
	
	
	
	// Dealing with Sorting the values
	$('#myList').sortable({
		opacity: '0.5',
		update: function(e, ui){
			serial = $(this).sortable("serialize");	
			$.ajax({
				url: "content-sort.php",
				type: "POST",
				data: serial,
				// complete: function(){},
				success: function(feedback){
					$('#data').html(feedback);
				}
				// error: function(){}
			});
		}
	}).css("cursor","pointer");
	
	
	
	$("#projectShowcase").click(function()	{
		var toClone = $("#projectImageUpload");							  
		$(toClone).clone(true).insertAfter(toClone);
		return false;
	});
	
	
	
}); // END DOM

// Confirm the click...to make sure that there are no mistakes (used for add or edit submits)
function confirmTheClick()
{
	var agree="Are you sure you wish to perform this action?";
	if ( confirm( agree ) )
		return true;
	else
		return false;
}
/*
For masked input 
    a - Represents an alpha character (A-Z,a-z)
    9 - Represents a numeric character (0-9)
    * - Represents an alphanumeric character (A-Z,a-z,0-9) 
*/
