var loginDivDisplayed = false;

var ua = navigator.userAgent.toLowerCase();
var isW3C = (document.getElementById) ? true : false
var isAll = (document.all) ? true : false
var isSafari = false;
if (ua.indexOf("safari") != -1) { isSafari = true; }


/*
===================================================================================================
GENERIC UI/BROWSER STUFF
===================================================================================================
*/

function unblur() {
	this.blur();
}

function getLinksToBlur() {
	if ((isW3C) || (isAll)) {
		if (isW3C) {
			links = document.getElementsByTagName("a");
		} else {
			links = document.all.tags("a");
		}
		for(i=0; i<links.length; i++) {
			links[i].onfocus = unblur
		}
	}
}

/*
===================================================================================================
INDEX (LIST) Page
===================================================================================================
*/

function doDelete(cat, id){
	if(confirm("Really Delete?\nThis action is not un-doable...")){
		$.ajax({
			type : "GET",
			url : "/cms/fusebox.php",
			dataType: "script",
			data : "f=deleteRecord&t="+cat+"&s="+id,
			success : function(msg){
				if(msg == "DELETE_SUCCESS"){
					window.location.reload();	
				} else {
					alert("Bad Delete Request: " + msg);
				}
			} 
		})
	
	}
}

/*
function doDelete(cat, id){
	if(confirm("Really Delete?\nThis action is not un-doable...")){
		new Ajax.Request('/cms/fusebox.php',
		{
	    	method:'get',
			parameters: {f: "deleteRecord", t:cat, s: id},
	    	onSuccess: function(transport){
	      		var response = transport.responseText || "no response text";
				alert(response)
	      		if(response == "DELETE_SUCCESS"){
					window.location.reload();	
				} else {
					alert("Bad Delete Request");
				}
	    	},
			onFailure: function(){ alert('Something went wrong?..') }
		});
	}
}
*/

/*
===================================================================================================
EDIT page
===================================================================================================
*/

function getForm(){
	return document.forms.updateform;
}

function checkEditForm(){
	
	f = getForm();
	
	selectedArray = new Array()
	
	for(i=0; i<f.length; i++){
	
		// make sure we have values in any required fields
		// by keying off the className (is it "required"?)
		if(f[i].className.indexOf("required") != -1 && f[i].value == ""){
				alert("Please include all required information");
				f[i].focus();
				return false;
		}
		
		
		// if we find any multi-select fields, get the selected values, 
		// create a new hidden input with a comma delimited value, 
		// set the name of the new input to the old input's name
		// set the old select name to "GARBAGE"
		// attach the new input to the form
		
		if(f[i].type == "select-multiple"){
			selector = f[i];
			selectName = f[i].name;
			
			for(j=0; j<selector.length; j++){
				if(selector[j].selected) selectedArray.push(selector[j].value);
			}
						
			n = document.createElement('input');
			n.setAttribute("name", selectName);
			n.setAttribute("type", "hidden");
			n.setAttribute("value", selectedArray.join(","));
			
			selector.name = "GARBAGE";
			
			f.appendChild(n);
					
		}
	}
	
	return true;
}


function submitEditForm(){
	f = getForm();
	if(checkEditForm()){
		f.submit();
	}
}


/*
// made this an inline link w/ jQuery
function editSelect(divId){
	$("#" + divId).slideDown();
	
	var d = document.getElementById(divId).style.display;
	if(d == "block"){
		cancelNew(divId);
	} else {
		document.getElementById(divId).style.display = "block";
		f = getForm();
	}
	
};
*/


function cancelNew(divId){
	document.getElementById(divId).style.display="none";
}	


function saveNew(divId){
	f = getForm();
	
	if(!f.new_brand.value || f.new_brand.value == ""){
		return false 
	} else {
		
		$.ajax({
			type : 'GET',
			url: '/cms/fusebox.php',
			data :'f=insertNewBrand&d='+f.new_brand.value+'&=projects_brands&id=0',
			success: function(msg){
				if(msg == "UPDATE_SUCCESS"){
					window.location.reload();	
				} else {
					alert(msg);
				}
			}
						
		})
	}

}


/*
===================================================================================================
LOGIN UI
===================================================================================================
*/


var loginDivDisplayed = false;

function toggleLogin(){
	if(!loginDivDisplayed){
		$("#loginDiv").slideDown("slow");
		loginDivDisplayed = true;
	} else {
		$("loginDiv").slideUp("slow");
		loginDivDisplayed = false;
	}
}



function checkFocus(o){

	if(o.name == "username"){
		if(o.value == "username"){
			o.value = "";
		} else if (o.value == ""){
			o.value = "username";
		}
	}
	
	if(o.name == "password"){
		if(o.value == "password"){
			o.value = "";
			// 	o.type = "password"; // not in ie!
			
			var input = document.getElementById('upw');
			var input2 = input.cloneNode(false);
		
			input2.type='password';
			input.parentNode.replaceChild(input2,input);
			input2.focus();
			document.getElementById('upw').focus();
			
			
		} else if (o.value == ""){
			//var input = document.getElementById('upw');
			//var input2 = input.cloneNode(false);
	 		//input2.type='text';
			//input.parentNode.replaceChild(input2,input);
			//o.value = "password";
		}
	}

}	

function doLogin(s){
	$.ajax({
		type : 'GET',
		url: '/cms/fusebox.php',
		data :'f=login&str='+s,
		success: function(msg){
			if(msg == "LOGIN_SUCCESS"){
				window.location.reload();	
			} else {
				alert(msg);
			}
		}
						
	})

	
	
}


function doLogout(){
	
	$.ajax({
		type : 'GET',
		url: '/cms/fusebox.php',
		data :'f=logout',
		success: function(msg){
			if(msg == "LOGOUT_SUCCESS"){
				window.location.reload();	
			} else {
				alert(msg);
			}
		}
	})
}









/*
===================================================================================================
Project Page
===================================================================================================
*/



		/*
	function goToTheEnd() {
		var inst = tinyMCE.getInstanceById('desc_long');
		
		var root = tinyMCE.DOM.getRoot();  // This gets the root node of the editor window
		var lastnode = root.childNodes[root.childNodes.length-1]; // And this gets the last node inside of it, so the last <p>...</p> tag
		if (tinymce.isGecko) {
			// But firefox places the selection outside of that tag, so we need to go one level deeper:
			lastnode = lastnode.childNodes[lastnode.childNodes.length-1];
		}
		// Now, we select the node
		inst.selection.select(lastnode);
		// And collapse the selection to the end to put the caret there:
		inst.selection.collapse(false);
	}
*/
	
	var editBool = false;
	
	function doEdit(){
		
		if(!editBool){
			$("#editwindow").slideDown("slow");
			tinyMCE.execInstanceCommand("mce_editor_0", "mceFocus");
			editBool = true;	
		} else {
			$("#editwindow").slideUp(1000);
			editBool = false;
		}
		
		/*if($("editwindow").style.height == "0pt" || $("editwindow").style.display == "none"){
			$("editwindow").style.height = 550
			$("editwindow").style.display = "block";
			tinyMCE.execInstanceCommand("mce_editor_0", "mceFocus");
		} else {
			$("editwindow").style.height = 0
			$("editwindow").style.display = "none";
		}
		*/
		
		// this screws up tinyMCE for some reason
		/*if($("editwindow").style.display == "block"){
			$("editwindow").style.display = "none";
		} else {
			$("editwindow").style.display = "block";
		}
		*/
	}

	function insertAsset(s, filepath){
		if(document.forms.editForm){
			//a = s.split("/");
			//var myFile = a[a.length-1];
			
			//var html = '<br><a href="' + s + '">' +  s + "</a>";
			var html = '<br><a href="'+filepath+'" >'+s+'</a>';
			
			var inst = tinyMCE.getInstanceById('desc_long');
			
			//goToTheEnd();
			inst.execCommand('SelectAll');
			inst.selection.collapse(false);
			
			//tinyMCE.execCommand('mceInsertContent', false, html);
			
			 tinyMCE.execCommand('mceInsertRawHTML', false, html, true); 

			
			
		}
	}
	
	function addURLtoUI(s, divName){
		divName = "#" + ((divName) ? divName : "previewDiv");
		if($(divName)){
			string = "<img src='/i/check.gif' valign='bottom' style='margin-top:-3px;'><b>File Uploaded:</b> " + s;
			$(divName).html(string);
			$(divName).fadeIn("slow");
		}
	}
	
	function addValueToForm(s, fName){
		if(document.forms[0][fName]) document.forms[0][fName].value = s
	}
	
	function onAssetUploaded(s, divName, filepath){
		insertAsset(s, filepath);
		addURLtoUI(s, divName);
		addValueToForm(s, divName.replace("preview_", ""));
	}
	
	function getEditForm(){
		return document.forms.editForm;
	}
	
	function init(){
		f = getEditForm();
		for(i=0; i<f.uploadType.length; i++){
			var c = f.uploadType[i];
			c.onclick = function(){
				if(this.checked){
					checkBoxErrors(this.value)
				}
			}
		}
	}

	function getCheckedValue(){
		if(document.forms.editForm){
			f = getEditForm();
			for(i=0; i<f.uploadType.length; i++){
				var c = f.uploadType[i];
				if(c.checked) return c.value;
			}
		} else {
			return false;
		}
	}
	
	function getOverwriteValue(){
		if(document.forms.editForm.dontOverwrite != undefined){
			f = getEditForm();
			return f.dontOverwrite.checked
		} else {
			return false;
		}
	}
	
	function checkBoxErrors(checkValue){
		/*
		1 Add Link
		2 Embed Image (JPG, GIF, PNG Only)
		3 Create HTML for a SWF
		4 Create SKIN demo page
		*/
		
		/*if(checkValue == 3 || checkValue == 4){
			setCheckboxByValue(1, true);
		}*/
		
		if(checkValue == 1){
			//setCheckboxByValue(1, true);
			setCheckboxByValue(3, false);
			setCheckboxByValue(4, false);
		}
		
		if(checkValue == 3){
			//setCheckboxByValue(1, true);
			//setCheckboxByValue(2, false);
			setCheckboxByValue(1, false);
			setCheckboxByValue(4, false);
		}
		
		if(checkValue == 4){
			//setCheckboxByValue(1, true);
			//setCheckboxByValue(2, false);
			setCheckboxByValue(1, false);
			setCheckboxByValue(3, false);
		}
	}
	
		
	function setCheckboxByValue(n, mode){
		f = getEditForm();
		for(i=0; i<f.uploadType.length; i++){
			var c = f.uploadType[i];
			if(c.value == n){

				if(mode && !c.checked){
						c.checked = true;
				}
				
				if(!mode && c.checked){
						c.checked = false;
				}
				
			}
		}
	}	
	

/*
===================================================================================================
INITs
===================================================================================================
*/


function init(){
	getLinksToBlur();
}


window.onload = init;
