/// <reference path="jquery-1.3.2-vsdoc.js" />

/** 
 * Toggle the visibility of a field
 * 
 * Note: css must contain the following selectors:
 *
 * .visible {
 *     display: block;
 * }
 * .invisible {
 *     display: none;
 * }
 *
 * Example:
 *
 * <a href="#" onclick="toggleVisibility('fieldId')">toggle</a>
 * <div id="fieldId" class="invisible">hello world</div>
 */
function toggleVisibility(myfieldname) 
{
    var myfield = document.getElementById(myfieldname);
    
    if (myfield.className == "invisible") 
	{
        // myfield.className = "visible"; 
        // NOTE: using class "visible" instead of empty quotes will cause table rows to grow horizontally
        myfield.className = "";
    } 
	else 
	{
        myfield.className = "invisible";
    }
    
    return false;
}

/** 
 * Make a hidden field visible
 * 
 * Example:
 *
 * <a href="#" onclick="makeVisible('fieldId')">show</a>
 * <div id="fieldId" class="invisible">hello world</div>
 */
function makeVisible(myfieldname) 
{
    var myfield = document.getElementById(myfieldname);
    
    if (myfield !== null) 
	{ 
        // NOTE: using class "visible" instead of empty quotes will cause table rows to grow horizontally
        myfield.className = "";
    }

    return false;
} 

/** 
 * Make a field invisible
 * 
 * Example:
 *
 * <a href="#" onclick="makeInvisible('fieldId')">hide</a>
 * <div id="fieldId" class="visible">hello world</div>
 */
function makeInvisible(myfieldname) 
{
    var myfield = document.getElementById(myfieldname);
    
    if (myfield !== null) 
	{ 
        myfield.className = "invisible";
    }
    
    return false;
} 

function deleteTag(tagId) 
{
    var myfield = document.getElementById('delete_tag_id');
    
    if (myfield !== null) 
	{ 
		myfield.value = tagId;
		document.delete_tag_form.submit();
    }
    return false;
} 

function selectTag(tagId) 
{
    var myfield = document.getElementById('search_tag_id');
    
    if (myfield !== null) 
	{ 
		myfield.value = tagId;
		document.search_tag_form.submit();
    }
    return false;
} 



/** 
 * Hide the peek section on page load
 */
$(document).ready(
function() 
{
	$('#config').hide();
	$('#config').css("height", "300px");

	$('a#slideout').click(function() 
	{
		$('#config').slideToggle("normal");
		return false;
	});
});



/*
function editUser(id)
{
	$("#newUserbutton").hide();
	$("#editUserDialog").dialog('option', 'title', 'Edit User');
	$("#userDialogSubmitButton").val("Save");
	$("#formAction").val("edit");

    $(document).ready(function(){
	    $.getJSON("/index.php?q=users&action=user&userId=" + id,
	        function(data)
	        {
	    		$("#dialogUserId").val(data.userId);
	    		$("#loginId").val(data.loginId);
	    		$("#userNameFirst").val(data.nameFirst);
	    		$("#userNameLast").val(data.nameLast);
	    		$("#userEmail").val(data.email);
	    		$("#userStatus").val(data.status);
	    		$("#userCompany").val(data.company);
	    		$("#userPhone").val(data.phone);
	    		$("#userAddress1").val(data.address1);
	    		$("#userAddress2").val(data.address2);
	    		$("#userCity").val(data.city);
	    		$("#userState").val(data.state);
	    		$("#userZip").val(data.zip);
        	});
	});

    
	
	if ($(".ui-state-error").length > 0)
	{
		$("#editUserDialog").dialog('option', 'position', 'bottom');
	}
	
	$("#editUserDialog").dialog('open');
}

function saveUser(id)
{
	$("#userId").val(id);
	$("#userAction").val("save");
	
	$("#loginIdHidden").val($("#loginIdInline").val());
	$("#nameFirstHidden").val($("#nameFirstInline").val());
	$("#nameLastHidden").val($("#nameLastInline").val());
	$("#statusHidden").val($("#statusInline").val());
	
	$("#userForm").submit();
}

function deleteUser(id)
{
	if (confirm("Are you sure you would like to delete this user?")) 
	{
		$("#userId").val(id);
		$("#userAction").val("delete");
		$("#userForm").submit();
	}
}

function cancelUser(id)
{
	$("#userId").val("");
	$("#userAction").val("");
	$("#userForm").submit();
}



$(document).ready(function(){
	$("#passwordChange").validate({
		rules: 
		{
			password1: "required",
			password2: 
			{
				equalTo: "#password1"
			}
		}
	});
	
	$("#passwordKeyGen").validate(
	{
		rules: 
		{
			email: "required",
			email: true
		}
	});
	
	$("#newUserForm").validate(
	{
		rules: 
		{
			loginId: "required",
			nameFirst: "required",
			nameLast: "required"
		}
	});
	
	$("#dialogUserForm").validate(
	{
		rules: 
		{
			loginId: "required",
			userNameFirst: "required",
			userNameLast: "required",
			userEmail: "required"
		}
	});

});


$(document).ready(function(){
	$("#editUserDialog").dialog({ autoOpen: false, width: 320 }); 
	
	$("#userDialogCancelButton").click(
		function () 
		{
			// if error is displayed, refresh page
			if ($(".ui-state-error").length > 0)
			{
				cancelUser(0);
			}
			
			// otherwise, just close the dialog without the roundtrip
			$("#editUserDialog").dialog('close');
		}
	);
	
	// Create a new user
	$("#userDialogSubmitButton").click(
		function () 
		{
			if ($("#dialogUserForm").validate().form())
			{
				$("#dialogUserForm").submit();
				$("#editUserDialog").dialog('close');
			}
		}
	);
	
	
	$("#newUserbutton").click(
		function () 
		{
			$("#newUserbutton").hide();
			$("#editUserDialog").dialog('option', 'title', 'Create New User');
			$("#userDialogSubmitButton").val("Create");
			$("#formAction").val("create");
			
			// blank all values
    		$("#dialogUserId").val("");
    		$("#loginId").val("");
    		$("#userNameFirst").val("");
    		$("#userNameLast").val("");
    		$("#userEmail").val("");
    		$("#userStatus").val("");
    		$("#userCompany").val("");
    		$("#userPhone").val("");
    		$("#userAddress1").val("");
    		$("#userAddress2").val("");
    		$("#userCity").val("");
    		$("#userState").val("");
    		$("#userZip").val("");
			
			$("#editUserDialog").dialog('open');
		}
	);
	
	$("#editUserDialog").bind('dialogclose', function(event, ui) {
		$("#newUserbutton").show();
		$("#dialogUserForm").validate().resetForm();
	});


});
*/

$(document).ready(function(){
	$('#tabs').tabs();
	$('#tabs').show();
});

//$(document).ready(function(){
//	console.log("in doc ready");
//});

$(document).ready(function(){
	$("#contentAccessType").change(
		function () 
		{
			var contentType = $("#contentAccessType").val();
			
			if (contentType == 0)
			{
				// switching to single mode, check multiple editors before roundtripping.
				//var content_0_isDirty = CKEDITOR.instances.page_content.checkDirty();
				//if (content_0_isDirty)
				//{
					//console.log("Content 0 is dirty");
				//}
				
				$("#page_content_form_save").val("Edit");
				$("#page_content_form_save").click();
			}
			else 
			{
				// switching to multiple mode, check single editor before roundtripping.
				//var content_0_isDirty = CKEDITOR.instances.page_content.checkDirty();
				//if (content_0_isDirty)
				//{
					//console.log("Content is dirty");
				//}
				
				$("#page_content_form_save").val("Edit");
				$("#page_content_form_save").click();
			}
		}
	);
});


$(document).ready(function()
{
	$("#adminEditPageContentButton").click(function() 
	{
		$("#editPageContentButton").click();
	});
	
	//$("#addProductForm #submitbutton").click(function() 
	//{
		//if ($("#addProductForm").validate().form())
		//{	
			//$("#addProductForm #submitbutton").attr('disabled', 'disabled');
			//$("#addProductForm").submit();
		//}
	//});
});


$(document).ready(function()
{
	$("#adminEditPageContentButton").hover(function()
	{
        makeVisible("editPageContentForm");
        makeInvisible("editPageContentButton");

        //$("#editPageContentForm").addclass("visible");
        $("#editPageContentForm").css(
            {
                "border": "2px dashed #D8CFC1"
            }
        );
	},
    function()
    {
        makeInvisible("editPageContentForm");

        //$("#editPageContentForm").addclass("visible");
        $("#editPageContentForm").css(
            {
                "border": "none"
            }
        );
	}
    );
        
    // Toggle hidden spaces
    jQuery(".togglecontent").hide();
    jQuery(".toggleheading").click(function()
    {
        jQuery(this).next(".togglecontent").slideToggle(300);
    });

});


function indicateAjaxRequest(obj)
{
    obj.addClass("busy");
    obj.removeClass("neutral");
}
function indicateAjaxComplete(obj)
{
    obj.addClass("neutral");
    obj.removeClass("busy");
}

function indicateAjaxRequestDialog(obj)
{
    obj.addClass("busyDialog");
    obj.removeClass("neutral");
}
function indicateAjaxCompleteDialog(obj)
{
    obj.addClass("neutralDialog");
    obj.removeClass("busy");
}




/*
function getWys()
{
    var editor = CKEDITOR.replace('page_content');
    
    // fetch data from the editor
    alert(CKEDITOR.instances.page_content.getData());

    // update the data in the editor
    CKEDITOR.instances.page_content.setData("Larry Was Here");
}


$('.jquery_ckeditor').ckeditor(config);
var editor = $('.jquery_ckeditor').ckeditorGet();
CKFinder.SetupCKEditor(editor, '../../');
*/

function getWys()
{
    CKEDITOR.config.startupMode = 'source';

    var editor = CKEDITOR.replace('page_content');

    // fetch data from the editor
    //alert(CKEDITOR.instances.page_content.getData());

    // update the data in the editor
    //CKEDITOR.instances.page_content.setData("Larry Was Here");

    CKEDITOR.editorConfig = function(config)
    {
        config.toolbarStartupExpanded = false;
        config.height = 400;
        config.FormatSource = false;
        config.FormatOutput = false;
        config.enterMode = 2;

        config.skin = 'v2';

        config.filebrowserBrowseUrl = '/ckfinder/ckfinder.html';
        config.filebrowserImageBrowseUrl = '/ckfinder/ckfinder.html?Type=Images';
        config.filebrowserFlashBrowseUrl = '/ckfinder/ckfinder.html?Type=Flash';
        config.filebrowserUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Files';
        config.filebrowserImageUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Images';
        config.filebrowserFlashUploadUrl = '/ckfinder/core/connector/php/connector.php?command=QuickUpload&type=Flash';

        config.contentsCss = '/style/master_syntive.css';

        config.fullPage = true;

        //config.toolbar = 'Basic';
        /*
        CKEDITOR.config.toolbar = [
           ['Styles','Format','Font','FontSize'],
           '/',
           ['Bold','Italic','Underline','StrikeThrough','-','Undo','Redo','-','Cut','Copy','Paste','Find','Replace','-','Outdent','Indent','-','Print'],
           '/',
           ['NumberedList','BulletedList','-','JustifyLeft','JustifyCenter','JustifyRight','JustifyBlock'],
           ['Image','Table','-','Link','Flash','Smiley','TextColor','BGColor','Source']
        ];
        */

    };


}


function destroyWys()
{
    // remove editor from the page
    //$('#page_content').ckeditor(function()
    //{
    //    this.destroy();
    //});

    CKEDITOR.instances.page_content.destroy();
}

$(document).ready(function()
{
    $(".draggable").draggable(
    {
        //	use a helper-clone that is append to 'body' so is not 'contained' by a pane
        helper: function() { return $(this).clone().appendTo('body').css('zIndex', 5).show(); },
        cursor: 'move'
    });
});

