var currentPulldown = "";
var windowCount = 0;

/* Find Current Position Of Specified Object */
function findPos(obj) {
	
	/* Get Current Position */
	this.x = obj.offsetLeft;
	this.y = obj.offsetTop;
	
	/* Cycle Through Parents */
	while(obj.offsetParent) {
		
		/* Calculate Offset */
		this.x = this.x + obj.offsetParent.offsetLeft;
		this.y = this.y + obj.offsetParent.offsetTop;
		
		/* Top of the Chain */
		if (obj == document.getElementsByTagName('body')[0]){
			
			/* Stop */
			break
			
		} else {
			
			/* Set obj For Next Iteration */
			obj = obj.offsetParent;
			
		}
		
	}
	
	/* Return Position Object (x, y) */
	return this
	
}

/* Get Coordinates */
function getCoordinates(e) {

	/* No Event Specified */
	if (!e) {
	
		/* Get Event from Window.Event (Internet Explorer) */
    if(window.event) {
      
			/* Define Event */
      e = window.event;
			
    } else {
			
      /* Total Failure. Return */
      return;
			
    }
		
  }
	
	/* Initialize Coordinates Object */
	coordinates = new Object();

	/* Most Browsers */
  if (typeof(e.pageX) == "number") {
    
		/* Get X and Y Coordinates */
    coordinates.x = e.pageX;
    coordinates.y = e.pageY;
		
		/* Return Object */
		return coordinates;
		
  } 
		
	/* Interent Explorer */
	if (typeof(e.clientX) == "number") {
	
		/* Get X and Y Coordinates */
		coordinates.x = e.clientX;
		coordinates.y = e.clientY;
		
		/* Check If Browser is Old Browser */
		var badOldBrowser = (window.navigator.userAgent.indexOf("Opera") + 1) ||
		(window.ScriptEngine && ScriptEngine().indexOf("InScript") + 1) ||
		(navigator.vendor == "KDE")
		
		/* Not Old Browser */
    if (!badOldBrowser) {
			
			/* Internet Explorer 4, 5 and 6 (Non Standards Compliant Mode) */
      if (document.body && (document.body.scrollLeft || document.body.scrollTop)) {
				
				/* Append Additional Positions */
        coordinates.x += document.body.scrollLeft;
        coordinates.y += document.body.scrollTop;
				
				/* Return Object */
				return coordinates;
				
      }
			
			/* Internet Explorer 6 (Standards Compliant Mode) */
			if (document.documentElement && (document.documentElement.scrollLeft || document.documentElement.scrollTop)) {
				
				/* Append Additional Positions */
        coordinates.x += document.documentElement.scrollLeft;
        coordinates.y += document.documentElement.scrollTop;
				
      }
			
    }
		
  } 
    
	/* Unreadable. Return Blank Object */
	return coordinates;

}

function getResolution() {
	
	resolution = new Object();
	resolution.width = screen.width;
	//resolution.height = screen.height;
	resolution.height = document.getElementsByTagName('body')[0].offsetHeight;
	return resolution;
	
}

function cluck(object) {
	
	/* Get Current Status */
	if (object.tagName == "DIV") {
		
		/* Get Text */
		text = object.innerHTML;
		
		/* Clear InnerHTML */
		object.innerHTML = "";
		
		/* Create Textarea */
		textarea = document.createElement('textarea');
		textarea.onblur = function() { save(textarea); }
		textarea.onchange = function() { save(textarea) }
		textarea.id = object.id + 'Textarea';
		textarea.className = "rightAppTextArea";
		textarea.innerHTML = text.replace(/(<br>|<br \/>)/gi, "\n");
		
		/* Add Textarea To Div */
		object.appendChild(textarea);
			
		/* Get Rid of Event Handlers */
		object.onclick = function() { }
		object.onmouseover = function() { }
		textarea.onchange = function() {
			save(object, textarea.value);
		}
		textarea.onblur = function() {
			save(object, textarea.value);
		}
		
	}
	
}

function setFontSize(fontSize) {
	fontSize = parseInt(fontSize);

	/* No Stylesheets Exist */
	if (!document.styleSheets) {
		
		/* Return */
		return;
		
	}

	/* Initialize CSS Array */
	var css = new Array();

	/* Standards Compliant */
	if (document.styleSheets[1].cssRules)  {
		
		/* Get CSS Stylesheets */
		css = document.styleSheets[1].cssRules;
		browser = "non-ie";
		
	} else {         
		
		/* Get CSS Stylesheets (IE) */
		css = document.styleSheets[1].rules;
		browser = "ie";
		
	}
		
	/* Loop Through Styles */
	for (i=0; i < css.length; i++) {
		
		/* Main Content */
		if (css[i].selectorText.toLowerCase() == "body") {
				
				/* Change Colour */
				css[i].style.fontSize = fontSize + "px";
				
		}
		
	}
	
	/* Initialize Expiry of Cookie */
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
		
	/* Save to Cookie */
	document.cookie = "baseFontSize" + "=" + fontSize + expires + "; path=/";

}

function showPulldown(pulldownElement) {

	if (currentPulldown != "") {
		hidePulldown(currentPulldown);
	}
	
	parent = pulldownElement.parentNode.parentNode;
	divList = parent.getElementsByTagName('div');
	
	/* Get Values List */
	for (i = 0; i < divList.length; i++) {
		if (divList[i].id == "valuesList") {
			valueList = divList[i];
		}
	}
	
	valueList.style.display = "block";
	valueList.style.width = (parent.offsetWidth - 2) + "px";
	valueList.style.left= (findPos(parent).x) + "px";
	valueList.style.marginTop = "21px";
	currentPulldown = valueList;
	
}

function setButton(buttonDiv, style) {
	switch (style.toLowerCase()) {
	
		case "selected":
			buttonDiv.style.backgroundImage = "url(graphx/pulldown_selected.png);";
			break;
			
		case "normal":
			buttonDiv.style.backgroundImage = "url(graphx/pulldown_normal.png);";
			break;
			
		case "hover":
			buttonDiv.style.backgroundImage = "url(graphx/pulldown_hover.png);";
			break;
			
	}
}

window.onclick = function clickHandler(e) {
	
	/* Pulldown Value */
	if (e.target.id == "pulldown") {
		showPulldown(e.target.childNodes[3].childNodes[0]);
		return;
	}
	
	/* Not attached to the pulldown */
	if (!isChildOf(e.target, "pulldown")) {
		hidePulldown(currentPulldown);
		return;
	}
	
	/* Value Selected */
	if (e.target.className == "pulldownValue") {
		selectValue(e.target);
		hidePulldown(currentPulldown);
		return;
	}
	
	if (e.target.id == "value") {
		showPulldown(e.target.parentNode.childNodes[2]);
		return;
	}
	
}

function hidePulldown(pulldownElement) {
	if (pulldownElement) {
		pulldownElement.style.display = "none";
	}
		
	currentPulldown = "";
}

function isChildOf(element, parentID) {
	
	while (element.parentNode) {
		if (element.parentNode.id == parentID) {
			return true;
		}
		element = element.parentNode;
	}
	
	return false;
}

function getParent(element, parentID) {
	
	while (element.parentNode) {
		if (element.parentNode.id == parentID) {
			return element.parentNode;
		}
		element = element.parentNode;
	}
	
	return null;
}

function selectValue(target) {
	pullDown = target.parentNode.parentNode;
	valueDiv = pullDown.getElementsByTagName('div')[0].getElementsByTagName('div')[0];
	valueDiv.setAttribute('value', target.getAttribute('value'));
	valueDiv.innerHTML = target.innerHTML;
	fieldName = target.parentNode.parentNode.getAttribute("name");
	if (getParent(target, "pulldown") != null) {
		getParent(target, "pulldown").getElementsByTagName('input')[0].value = target.getAttribute('value');
	}
}

function updateWhatsHot(value) {
		
	/* Section Exists */
	if (document.getElementById('whatsHotSection_' + value)) {
		
		/* Get Div */
		div = document.getElementById('whatsHotSection_' + value);
		
		/* Get Sections */
		sections = document.getElementById('whatsHotSections').getElementsByTagName('div')
		
		/* Cycle Through And Hide Current Div */
		for (i = 0; i < sections.length; i++) {
			
			/* Hide Section */
			if (sections[i].className == "whatsHotSection") {
				sections[i].style.display = "none";
			}
			
		}
		
		/* Show Specified Section */
		div.style.display = "block";
		
	}
	
}

function getResolution(element) {
	resolution = new Object();
	resolution.x = screen.width;
	resolution.y = screen.height;
	
	/* Create Width and Height Elements */
	widthElement = document.createElement('input');
	widthElement.type = "hidden";
	widthElement.name = "width";
	widthElement.value = screen.width;
	
	heightElement = document.createElement('input');
	heightElement.type = "hidden";
	heightElement.name = "height";
	heightElement.value = screen.height;
	
	formParent = getParent(element, "loginFormData");
	if (formParent) {
		formParent.appendChild(widthElement);
		formParent.appendChild(heightElement);
	}
	
}


function joinNewsletter() {

	var http = new XMLHttpRequest();
	var url = "community_dev.php5?joinNewsletter";
	params = "";
	
	http.open("POST", url, true);
	
	//Send the proper header information along with the request
	http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	http.setRequestHeader("Content-length", params.length);
	http.setRequestHeader("Connection", "close");

	http.onreadystatechange = function() {
		if(http.readyState == 4 && http.status == 200) {
			document.getElementById('joinMessage').innerHTML = http.responseText;
		}
	}
		
	http.send(params);
	
	return true;
	
}

function newWindow(url) {
	window.open (url, "window" + windowCount,"location=1,status=1,scrollbars=1"); 
}

function setAnswer(element, answer) {
	
	confirmAnswerElement = document.createElement('input');
	confirmAnswerElement.type = "hidden";
	confirmAnswerElement.name = "confirmAnswer";
	confirmAnswerElement.value = answer;
	
	formParent = getParent(element, "form");
	if (formParent) {
		formParent.appendChild(confirmAnswerElement);
	}
	
	formParent.submit();
	
}
	
function show(element, showImage) {
	
	/* Set Value */
	newStatus = (element.style.display == "block") ? "none" : "block";
	element.style.display = newStatus;
	
	/* Fix Show Image */
	showImage.src = (showImage.src == "graphx/hideshowHidden.png") ? "graphx/hideshowVisible.png" : "graphx/hideshowHidden.png";
	
	/* Initialize Expiry of Cookie */
	var date = new Date();
	date.setTime(date.getTime()+(365*24*60*60*1000));
	var expires = "; expires="+date.toGMTString();
		
	/* Save to Cookie */
	document.cookie = "show_" + element.id + "=" + newStatus + expires + "; path=/";
}

function addTag(tag) {
	
	/* Split Tag */
	if (tag.search(",") != -1) {
		splitTags = tag.split(",");
	} else {
		splitTags = new Array();
		splitTags[0] = tag;
	}
	
	for (i = 0; i < splitTags.length; i++) {
		
		realTag = trim(splitTags[i]);
		
		if (!tagExists(realTag)) {
			
			/* Create Element */
			option = document.createElement('option');
			option.value = realTag;
			option.innerHTML = realTag;
			option.selected = true;
			
			document.getElementById('tagList').appendChild(option);
			
		}
		
	}
	
}

function tagExists(tag) {

	options = document.getElementById('tagList').getElementsByTagName('option');
	for (i = 0; i < options.length; i++) {
		if (options[i].value == tag) {
			options[i].selected = true;
			return true;
		} else {
			options[i].selected = false;
		}
	}
	
	return false;
}

function deleteTags() {

	options = document.getElementById('tagList').getElementsByTagName('option');
	
	for (i = 0; i < options.length; i++) {
		if (options[i].selected == true) {
			document.getElementById('tagList').removeChild(options[i]);
		}
	}
	
}

function hideshow() {
	
	/* Initialize Count */
	count = 0;
	
	/* Seperate Lines */
	var cookieLines = document.cookie.split(';');
	
	/* Cycle Through Lines */
	for (i=0; i < cookieLines.length; i++) {
		
		/* Get Line */
		var line = cookieLines[i];
		
		/* Empty */
		while (line.charAt(0) == ' ') {
			
			/* Reread Line */
			line = line.substring(1, line.length);
			
		}
		
		/* Show/Hide Element Entry */
		if (line.search(/show/) != -1) {
			
			/* Read Data */
			var dataArray = line.split('=');
			
			/* Get Name */ 
			name = dataArray[0].substring(5);
				
			/* Get Value */
			value = dataArray[1];
			
			if (document.getElementById(name)) {
				document.getElementById(name).style.display = value;
			}
				
		}
		
	}
		
}

function saveTags() {
	
	options = document.getElementById('tagList').getElementsByTagName('option');
	formElement = new Array();
	
	for (i = 0; i < options.length; i++) {
		formElement[i] = document.createElement('input');
		formElement[i].value = options[i].value;
		formElement[i].name = "tags[]";
		formElement[i].type = "hidden";
		document.getElementById('tagsForm').appendChild(formElement[i]);
	}
	
	document.getElementById('tagsForm').submit();
	
}

function trim(str) { 
	
	if (typeof(str) != "undefined") {
		str = str.replace("/\s+/", " ");
		if (str.charAt(0) == " ") {
			str = str.substr(1);
		}
		if (str.charAt(str.length - 1) == " ") {
			str = str.substr(0, str.length - 2);
		}
		
		return (str == " ") ? "" : str;
	} else {
		return "";
	}

}

function authenticatedFeature() {
	errorDiv = document.getElementById('errorMessage');
	errorDiv.innerHTML = "You have to be logged in to access this feature.";
	errorDiv.style.display = "block";
	errorDiv.onclick = function() { this.style.display = "none"; }
}
	
