var iframe;
var IE = 0;
var src = 'main.php';
var hash;
var inter = 0;
var aboutEditor;
var contactSelectId = 0;
function init(){
	$(window).bind("hashchange", function (){
		checkHash();
	});
	checkHash();
}
function disableHTML(object){
	var htmlString = object;
	htmlString = htmlString.replace(/"/gim,"&#34;");
	htmlString = htmlString.replace(/'/gim,"&#39;");
	return htmlString;
}
function checkHash(){
	if(location.hash.replace("#", "") != hash){
		if(location.hash != ""){
			oldHash = src;
			src = location.hash.replace("#", "");
			hash = src;
		}else{
			src = "main.php";
			hash = "";
		}
		setMiddleAreaHTML(src);
	}
}
function setMiddleAreaHTML(src){
	xmlHttp = GetXmlHttpObject();
	if( xmlHttp == null){
		alert("Browser does not support Browser!");
		return false;
	}
	if(IE){
		iframe.src = "PcChangeLocation.php?location="+src;
	}
	url = src;
	xmlHttp.onreadystatechange = statechange;
	xmlHttp.open("GET", url, true);	
	xmlHttp.send();
}
function statechange(){
	if(xmlHttp.readyState == 1){
		
	}
	if(xmlHttp.readyState == 4 || xmlHttp.readyState =="complete"){
		$("#middleArea").fadeOut("fast", function(){
			$("#middleArea").fadeIn("fast");
			document.getElementById("middleArea").innerHTML = xmlHttp.responseText;
		});
	}
}
function closePop(){
	$(".pop").fadeOut("slow");
}
function closecom(){
	document.getElementById("popup").innerHTML = "";
}
function closeEdit(callback){
	$(".editarea").fadeOut("fast");
	callback();
}




//About functions
function aboutLoad(){
	$.ajax({
		url: "aboutLoad.php",
		type: "get",
		success: function (data){
			$("#abouts").html(data);
			$("#abouts").fadeIn("fast");
		}
	});
}
function aboutAdd(){
	$.ajax({
		url: "add_about.php",
		type: "get",
		success: function(data){
			$(".editarea").fadeIn("fast");
			$("#abouts").fadeOut("fast");
			document.getElementById("aboutEditId").value = data;
			if(!aboutEditor){
				aboutEditor = new text_Editor("editor", "aboutEditBody", "");
				aboutEditor.create();
			}
			frame = document.getElementById("editortextarea").contentWindow || document.getElementById("editortextarea").contentDocument;
			frame.document.body.innerHTML = "";
			document.getElementById("aboutEditHead").value = "";
		}
	});
}
function aboutLoadEdit(id){
	$.ajax({
		url: "aboutLoadEdit.php",
		type: "get",
		data: "id="+id,
		success: function(data){
			try{
				result = eval(data);
				message = result[0]["message"];
				header = result[0]["header"];
				document.getElementById("aboutEditId").value = id;
				$("#abouts").fadeOut("fast", function(){
					$(".editarea").fadeIn("fast");
					document.getElementById("aboutEditHead").value = header;
					if(!aboutEditor){
						aboutEditor = new text_Editor("editor", "aboutEditBody", message);
						aboutEditor.create();
					}
					frame = document.getElementById("editortextarea").contentWindow || document.getElementById("editortextarea").contentDocument;
					frame.document.body.innerHTML = message;
				});
			}catch(err){
				$("#abouts").fadeOut("fast", function(){
					$("#abouts").fadeIn("fast");
					$("#abouts").html(err.description);
				});
			}
		}
	});
	$("#abouts").html("");
}
function aboutSave(){
	$.ajax({
		url: "aboutsave.php",
		type: "post",
		data: "message="+aboutEditor.getHTML()+"&header="+document.getElementById("aboutEditHead").value+"&id="+document.getElementById("aboutEditId").value,
		success: function (data){
			closeEdit(aboutLoad);
		}
	});
}
function aboutDelete(id){
	$.ajax({
		url: "aboutDelete.php",
		type: "post",
		data: "id="+id,
		success: function (data){
			aboutLoad();
		}
	});
}


//Contact Function
function contactSelect(id){
	if(id != contactSelectId){
		if(contactSelectId!=0){
			try{
				contactUnSelect(contactSelectId);
			}catch(e){
				alert(e);
			}
		}
		contactSelectId = id;
		$("#contactName"+id).html("<input value='"+$("#contactName"+id).html()+"' onkeyup='contactSave("+id+", \"name\")' id='contactNameInput' />");
		$("#contactPos"+id).html("<input value='"+$("#contactPos"+id).html()+"' onkeyup='contactSave("+id+", \"what\")'  id='contactPosInput' />");
		$("#contactPhone"+id).html("<input value='"+$("#contactPhone"+id).html()+"' onkeyup='contactSave("+id+", \"PhoneNumber\")' id='contactPhoneInput' />");
		$("#contactEmail"+id).html("<input value='"+$("#contactEmail"+id).html()+"' onkeyup='contactSave("+id+", \"email\")' id='contactEmailInput' />");
	}
}
function contactUnSelect(id){
	$("#contactName"+id).html(document.getElementById("contactNameInput").value);
	$("#contactPos"+id).html(document.getElementById("contactPosInput").value);
	$("#contactPhone"+id).html(document.getElementById("contactPhoneInput").value);
	$("#contactEmail"+id).html(document.getElementById("contactEmailInput").value);
	contactSelectId = 0;
}
function contactSave(id, what){
	var value = "";
	if(what == "name"){
		value = document.getElementById("contactNameInput").value;
	}else if(what == 'what'){
		value = document.getElementById("contactPosInput").value;
	}else if(what == 'PhoneNumber'){
		value = document.getElementById("contactPhoneInput").value;
	}else{
		value = document.getElementById("contactEmailInput").value;
	}
	$.ajax({
		url: "contactSave.php",
		data: "id="+id+"&value="+value+"&what="+what,
		type: "post",
		success: function (data){
			if(data.match("error")){
				alert(data)
			}
		}
	});
}
function contactAddGroup(){
	name = prompt("Group Name:");
	$.ajax({
		url: "contactAddGroup.php",
		data: "name="+name,
		type: "get",
		success: function (data){
			alert(data);
			$("#contacts").append("<div class='contactGroup' id='contactGroupMain"+data+"'>"+
					"<div class='aboutHead'><div class='aboutHeadText'>"+name+"</div>"+
						"<div class='aboutHeadButton'>"+
							"<button class='aboutheaderButton' onclick='contactDeleteGroup("+data+")'>Delete Group</button>"+
							"<button class='aboutheaderButton' onclick='contactDeleteContact("+data+")'>Delete Contact</button>"+
							"<button class='aboutheaderButton' onclick='contactAddContact("+data+")'>Add Contact</button>"+
						"</div>"+
					"</div>"+
					"<div class='aboutBody' id='contactGroup"+data+"'></div>"+
				"</div>");
		}
	});
}
function contactDeleteGroup(id){
	if(confirm("Are you sure that you want to delete this group?")){
		$.ajax({
			url: "contactDeleteGroup.php",
			type: "get",
			data: "id="+id,
			success: function (data){
				$("#contactGroupMain"+id).fadeOut("fast");
			}
		});
	}
}
function contactAddContact(groupId){
	$.ajax({
		url: "contactAdd.php",
		type: "get",
		data: "groupId="+groupId,
		success: function(data){
			if(data!="Error"){
				$("#contactGroup"+groupId).append("<div class='contact' onclick='contactSelect("+data+")'>"+
											"<div class='contactImage'>"+
												"<img class='contactImg'/>"+
											"</div>"+
											"<div class='contactInfos'>"+
												"<div class='contactInfo' id='contactName"+data+"'></div>"+
												"<div class='contactInfo' id='contactPos"+data+"'></div>"+
												"<div class='contactInfo' id='contactPhone"+data+"'></div>"+
												"<div class='contactInfo' id='contactEmail"+data+"'></div>"+
											"</div>"+
											"<div class='contactChangePic'></div>"+
											"</div>");
			}
		}
	});
}
function contactUploadPic(form){
	fileUploadForm(form, "./images/contact/contactUpload.php", "status", contactUpload);
}
function contactUpload(data){
	alert(data);
}
//resources
function resourceAdd(){
	$.ajax({
		url: "resourcesAdd.php",
		type: "get",
		success: function(data){
			$(".resources").prepend("<div class='resource' id='resource"+data+"'>"+
										"<div class='resourceHead'>[<span class='clickText' onclick='resourceDelete("+data+")'>Delete</span>] [<span class='clickText' onclick='resourceUpdate("+data+")'>Save</span>]<input type='checkbox' name='featuered' onclick='resourceFatured("+data+", this.value)'/></div>"+
											"<div class='resourcePic'>"+
											"<div class='resourcePicImg'>"+
												"<div class='resourcePicImgChange' onclick='resourceShowPop("+data+", this)' id='resourceChangeImg"+data+"'>Change Pic</div>"+
												"<img height='100%' class='resourcePicImgimg' id='resourcePicImgimg"+data+"' src='./images/resources/"+data+".png' />"+
											"</div><div class='resourcePicUrl'>Get it on <a target='_blank' href='http://'>amazon</a></div>"+
											"</div>"+
										"<div class='resourceText' id='resourceText"+data+"' contenteditable='true'></div>"+
										"<div class='resourceUrl'>Amazon Link: http://<input onchange='resourceUpdateUrl(this.value, "+data+")' name='url'/></div></div>"+
									"</div>");
		}
	});
}
function resourceDelete(id){
	$.ajax({
		url: "resourcesDelete.php",
		type: "post",
		data: "id="+id,
		success: function (data){
			$("#resource"+id).slideUp("fast", function(){$("#resource"+id).remove();});
		}
	});
}
function resourceUpdateUrl(value, id){
	$.ajax({
		url: "resourceUpdateUrl.php",
		type: "post",
		data: "value="+escape(value)+"&id="+id,
		success: function(data){
			if(!data.match("error")){
				alert("Updated");
			}
		}
	});
}
function resourceUpdate(id){
	$.ajax({
		url: "resourceUpdateText.php",
		type: "post",
		data: "id="+id+"&text="+escape(disableHTML($("#resourceText"+id).html())),
		success: function (data){
			alert("Saved");
		}
	});
}
function resourceUploadPic(form){
	fileUploadForm(form, "./images/resources/resourcePic.php", "popUpload", function(data){
		alert(data);
	})
}
function resourceShowPop(id, object){
	$(".pop").fadeIn("fast");
	top = $(object).position().top;
	$(".pop").css({"top":top-200});
	document.getElementById("resourceImgUpId").value = id;
}
function resourceFatured(id, value){
	if(value == "on"){
		value = 1;
	}else{
		value = 0;
	}
	$.ajax({
		url: "resourceUpdateFeatured.php",
		type: "post",
		data: "id="+id+"&value="+value,
		success: function (data){
			alert(data);
		}
	});
}

//calendar
function popup(number, inLog){
	if(inLog){
		document.getElementById("popup").innerHTML = "<div class='pope' id='pop' align='center'><table border='0' width='600px'><tr><td align='right' width='560px'><iframe frameborder='0' width='500' height='400' src='calender_event_show.php?number=" + number + "'></iframe></td></tr><tr><td colspan='2' align='right'><button onclick='closepop();'>close</button></td></tr></table></div>";
		document.getElementById("pop").style.top = 150 + document.body.scrollTop;
		document.getElementById("pop").className = "pope"; 
	}else{
		document.getElementById("popup").innerHTML = "<div class='pop' id='pop' align='center'><table border='0' width='350px'><tr><td align='right' width='100px'><iframe frameborder='0' height='220' src='calender_event_show.php?number=" + number + "'></iframe></td></tr><tr><td colspan='2' align='right'><button onclick='closepop();'>close</button></td></tr></table></div>";
		document.getElementById("pop").style.top = 150 + document.body.scrollTop;
	}
}

//closes the popup
function closepop(){
	document.getElementById("popup").innerHTML = "";
}

//changes the calender
function changecal(plusOrMinus){
	
	con = 0;
	
	date = document.getElementById("Date").innerHTML;
	date = date.split(" ");
	//define varibles
	lettermonth = date[0];  //letter repersentation of the month
	year = date[1];			//the year
	
	var month=new Array(12);
	month[0]="January";
	month[1]="February";
	month[2]="March";
	month[3]="April";
	month[4]="May";
	month[5]="June";
	month[6]="July";
	month[7]="August";
	month[8]="September";
	month[9]="October";
	month[10]="November";
	month[11]="December";
	
	
	if ((lettermonth != month[0] && lettermonth != month[11]) || (lettermonth == month[11] && plusOrMinus == -1) || (lettermonth == month[0] && plusOrMinus == 1)) {
		while (lettermonth != month[con] && con < 11) {
			con++;
		}
		con = con + plusOrMinus;
	}else if(lettermonth == month[0]){
		year --;
		con = 11;
		document.getElementById("Date").innerHTML = con;
	}else if(lettermonth == month[11]){
		year ++;
		con = 0;
	}
	
	document.getElementById("Date").innerHTML = month[con] + " "  + year;
	con ++;
	calender(con, year)
}

//makes the calender using ajax from ajax_calender.php
function calender(con, year)
{

	xmlHttp=GetXmlHttpObject()
	if (xmlHttp==null)
		{
			alert ("Browser does not support HTTP Request")
			return
		}
	
	
	var url="ajax_calender.php";
	url = url + "?year=";
	url = url + year;
	url = url + "&month=";
	url = url + con;
	url = url + "&sid=" + Math.random();
	
	
	xmlHttp.onreadystatechange= function()
	{
		if(xmlHttp.readyState==0){
			document.getElementById("calendertable").innerHTML = "The request is not initialized";
		}
		
		if(xmlHttp.readyState == 1){
			document.getElementById("calendertable").innerHTML = "The request has been set up";
		}
		
		if(xmlHttp.readyState == 2){
			document.getElementById("calendertable").innerHTML = "The request has been sent";
		}
		
		if(xmlHttp.readyState == 3){
			document.getElementById("calendertable").innerHTML = "The request is in process";
		}
		
		if (xmlHttp.readyState == 4 || xmlHttp.readyState == "complete"){
			$("#calendertable").html(xmlHttp.responseText);
		} 
	}
	xmlHttp.open("GET",url,true)
	xmlHttp.send(null)
}
function addevent(){
	document.getElementById("popup").innerHTML = "<div id='pop' class='pope' height='950' width='950'  align='center'><table border='0' width='350px'><tr><td><iframe frameborder='0' src='calendar_addevent.php?' height='500' width='900'></iframe></td></tr><tr><td align='right'><button onclick='closepop();'>close</button></td></tr></table></div>";
	document.getElementById("pop").style.top = 150 + document.body.scrollTop;
}

/*
 * Goes with rLinks.php
 */
function addLinkClick(){
	$(".pop").fadeIn("fast");
}
function rLinksClosePop(){
	$(".pop").fadeOut("fast");
}
function rLinksDeleteLink(id){
	if(confirm("Are you sure you want to delete?")){
		$.ajax({
			url: "rLinksDelete.php",
			type: "get",
			data: "id="+id,
			success: function(data){
				location.reload();
			}
		});
	}
}
function linkPopAddClick(){
	$.ajax({
		url: "rLinksAdd.php",
		type: "post",
		data: "name="+escape(document.getElementById("rLinksName").value)+ "&href="+escape(document.getElementById("rLinksHref").value),
		success: function(data){
			location.reload();
		}
	});
	rLinksClosePop();
}
