/* 
   Author: Mark Brightenfield
   Date  : 11/05/06

   Function List:
   xCoord(id)
      Returns the x-coordinate of the object with id with the value id

   yCoord(id)
      Returns the y-coordinate of the object with id with the value id

   placeIt(id, x, y)
      Places the id object at the coordinates (x,y)

   shiftIt(id, dx, dy)
      Moves the id object dx pixels to the right and dy pixels down

   hideIt(id)
      Hides the id object by setting its visibility style to "hidden"

   showIt(id)
      Shows the id object by setting its visibility style to "visible"

   winWidth()
      Returns the width of the interior browser window in pixels

   winHeight()
      Returns the height of the interior browser window in pixels
   
   openNewWin(url)
      opens link in new window

*/

var w = winWidth()-215;
var h = winHeight()-50;
var menuVisible = false;

function xCoord(id)
{
	var obj = document.getElementById(id);
	var xc = parseInt(obj.style.left);
	return xc;
}

function yCoord(id)
{
	var obj = document.getElementById(id);
	var yc = parseInt(obj.style.top);
	return yc;
}

function placeIt(id, x, y)
{
	object = document.getElementById(id);
	object.style.position = "absolute";
	object.style.left=x+"px";
	object.style.top=y+"px";
}

function shiftIt(id, dx, dy)
{
	object=document.getElementById(id);
	object.style.left=xCoord(id)+dx+"px";
	object.style.top=yCoord(id)+dy+"px";
}

function hideIt(id)
{
	object = document.getElementById(id);
	object.style.visibility="hidden";
}

function showIt(id)
{
	object = document.getElementById(id);
	object.style.visibility="visible";
}

function slide(id, x, y){
	var dx = xCoord(id);
	var dy = yCoord(id);
	placeIt(id, 100, 100);
	if(x != dx &&  y != dy)
	{
		if(x > dx)
			shiftIt(id, dx+1, dy);
		if(x < dx && x != dx)
			shiftIt(id, dx-1, dy);
		if(y > dy)
			shiftIt(id, dx, dy+1);
		if(y < dy && y != dy)
			shiftIt(id, dx, dy-1);
			
		setTimeout("slide('"+id+"', "+x+", "+y+")", 50);
	}
}

function winWidth()
{
	if(window.innerWidth) return window.innerWidth;
	else if(document.documentElement) return document.documentElement.offsetWidth;
	else if(document.body.clientWidth) return document.body.clientWidth;
}

function winHeight()
{
	if(window.innerHeight) return window.innerHeight;
	else if(document.documentElement) return document.documentElement.offsetHeight;
	else if(document.body.clientHeight) return document.body.clientHeight;
}

//Opens a new browser window
function openNewWin(url)
{
	var defWin = window.open(url,"","width=800,height=600,screenX=100,screenY=100,resizable=yes");
	var notPop = (defWin==null || typeof(defWin)=="undefined") ? true: false;
	return notPop;
}

//Calls the moveUp or moveDown function
function setMenu()
{
	if(!menuVisible)
	{
		hideIt("menu");
		placeIt("menu2", 0 , -250);
		showIt("menu2");
		menuDown();
		menuVisible=true;
	}
	else
	{
		menuUp();
		menuVisible=false;
	}
}

//Slides the menu down
function menuDown()
{
	var y = yCoord("menu2");
	if(y <= 0)
	{
		shiftIt("menu2", 0, 10);
		setTimeout("menuDown()", 30);
	}
}

//Slides the menu up
function menuUp()
{
	var y = yCoord("menu2");
	if(y >= -250)
	{
		shiftIt("menu2", 0, -10);
		setTimeout("menuUp()", 30);
	}
	else
	{
		hideIt("menu2");
		showIt("menu");
	}
}

//Embeds video into document
function embedVideo(url)
{
	openWin = window.open("", "newwin", "height=500,width=500,toolbar=no,scrollbars="+scroll+",menubar=no");
	openWin.document.write("<html>");
	openWin.document.write("<body style='background-color: #1B1B1B'>");
	openWin.document.write("<embed src='"+url+"' height='500' width='500' autostart='true' /></br>");
	openWin.document.write("</body>");
	openWin.document.write("<html>");
	openWin.document.close();
}

function windowStatus(status)
{
	window.status = status;	
}

function validate(thisForm)
{
	for(var i = 0; i <= thisForm.elements.length; i++){
		if(thisForm.elements[i].type == "text" || thisForm.elements[i].type == "password"){
			if (thisForm.elements[i].value == ""){
				thisForm.elements[i].focus();
				thisForm.elements[i].style.background = "#61B2DF";
				return false;
			}
		}
	}
	return true;
}
function validateContact(thisForm)
{
	for(var i = 0; i <= thisForm.elements.length; i++){
		if(thisForm.elements[i].type == "text" || thisForm.elements[i].type == "password" || thisForm.elements[i].type == "textarea"){
			if (thisForm.elements[i].value == ""){
				thisForm.elements[i].focus();
				thisForm.elements[i].style.background = "#61B2DF";
				return false;
			}
			if(thisForm.elements[i].value.search(/mail.ru/) >= 0){
				alert("Stop Spamming Fucker!");
				return false;
			}
		}
	}
	return true;
}

var url = "http://www.brightenfield.com";
var title = "Brightenfield.com - Games, Videos, and more.";
function bookmarkSite(){
	if (window.sidebar) // firefox
		window.sidebar.addPanel(title, url, "");
	else if(window.opera && window.print){ // opera
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	}
	else if(document.all)// ie
		window.external.AddFavorite(url, title);
	else
		alert("Browser doesn't support this function.");
}

function tag(user, title, url)
{
	document.location = "?content=tag.php&username=" + user + "&title=" + title + "&url=" + url;
}

function deleteTag(user, url)
{
	var conf = confirm("Are you sure you want to delete this?");
	if(conf)
		document.location = "/deleteTag.php?username=" + user + "&url=" + url;
}

var Website = {
	Version: "1.0",
	Name: "Brightenfield.com",
	Print: function(){
		alert(this.Version + " " + this.Name);
	}
};
var test = "mark";

//	String Extensions
String.prototype.IN = function(array){
	for(var x in array)
		if(this == array[x])
			return true;
	return false;
};

//	Array Extentions
Array.prototype.contains = function(obj){
	for(var x in this)
		if(this[x] == obj)
			return true;
	return false;
};

//BEGIN Scramble
function Scramble(word){
	//Private Members, Constructor
	var letters = word.split("").sort();

	//Private Methods
	function factorial(n){
		if(n == 0)
			return 1;
		else
			return (factorial(n-1) * n);
	}
	//Number of Permutations
	function P(n,r){
		return factorial(n) / factorial (n-r);
	}
	
	//Number of Combinations
	function C(n,r){
		return factorial(n) / (factorial(r) * factorial (n-r));
	}
	function hasDupes(){
		var retVal = false;
		for(var x = 0; x<letters.length-1; x++){
			for(var y = x+1; y<letters.length; y++){
				if(letters[x] == letters[y]){
					retVal = true;
					break;
				}	
			}				
		}
		return retVal;
	}
	
	function calcForDupes(){
		var comb = 1;
		var positions = word.length;
		var letterCount = new Array();
		letterCount.push(new Array());
		letterCount[0][0] = letters[0];
		letterCount[0][1] = 1;
		
		for(var x = 1; x<word.length; x++){
			var inLetterCount = false;
			
			for(var y = 0; y<letterCount.length; y++){
				if(letters[x] == letterCount[y][0]){
					letterCount[y][1]++;
					inLetterCount = true;
					break;
				}
			}
			
			if(!inLetterCount){
				letterCount.push(new Array());
				letterCount[letterCount.length-1][0] = letters[x];
				letterCount[letterCount.length-1][1] = 1;
			}
		}
		
		for(var x = 0; x<letterCount.length; x++){
			comb *= C(positions, letterCount[x][1]);
			positions -= letterCount[x][1];
		}
		
		return comb;
	}
	
	//Public Methods
	this.value = function(){
		return word;
	}
	
	this.combinations = function(){
		var numCombinations;
		if(hasDupes()){
			numCombinations = calcForDupes();
		}			
		else
			numCombinations = factorial(word.length);
		return numCombinations;
	}
	
	this.possibleValues = function(){
		var possible = letters;
		
	}
	
};
//End Scramble

var temp = new Scramble("mississippi");

//BEGIN ControlPanel
function ControlPanel(){
	//Private Members, Contstructor
	var buttons = new Array();    //Stores button values and functions
	var containers = new Array(); //DOM Object IDs that the panel will display in
	var btns = new Array(); 	  //The button objects that are created
	
	//Private Methods
	
	//Public Methods
	this.add = function(value, func){
		var buttonVal = new Array()
		buttonVal.push(value);
		buttonVal.push(func);
		buttons.push(buttonVal);
	}
	this.display = function(parent){
		if (containers.indexOf(parent) == -1){
			containers.push(parent);
			var container = document.createElement("div");
			container.setAttribute("id", "ControlPanel");
			for(var x = 0; x<buttons.length; x++){
				var cur = document.createElement("input");
				cur.setAttribute("type", "button");
				cur.setAttribute("value", buttons[x][0]);
				cur.onclick = buttons[x][1];
				
				btns.push(cur);
				container.appendChild(cur);
			}
			document.getElementById(parent).appendChild(container);
		}
			
	}
	this.toggleButton = function(value){
		for(var x = 0; x<btns.length; x++){
			if(btns[x].value == value){
				if(btns[x].style.display == "")
					btns[x].style.display = "none";
				else
					btns[x].style.display = "";
			}
		}
	}
	this.count = function(){
		alert(buttons.length);
	}
};
//END ControlPanel

var panel = new ControlPanel();
panel.add("test", function(){alert("testing")});
panel.add("test2", function(){alert("2tset")});


function Validator(){
	//Private Members, Constructor
	var invalidFields = new Array();	

	//Private Methods
	
	//Public Methods
	this.filled = function(inputs) {
		for(var i = 0; i<inputs.length; i++)
			if(inputs[i].value == "")
				invalidFields.push(new Array("Please fill in required fields.", inputs[i]));
	}
	
	this.email = function(email) {
		var emailEx = new RegExp(/^([a-zA-Z0-9_\.\-])+\@(([a-zA-Z0-9\-])+\.)+([a-zA-Z0-9]{2,4})+$/);
		var validate = emailEx.test(email.value);
		if(!validate)
			invalidFields.push(new Array("Please enter valid email address.", email));
	}
	
	this.getInvalid = function() {
		return invalidFields;
	}
	
	this.reset = function() {
		invalidFields = new Array();
	}
}

var validate = new Validator();
validate.filled(document.getElementsByTagName("INPUT"));
