/**
    *
    *
    *
    *
    *
    *
    **/
	
var buildlog = prototype = {
	author: "Niclas Larsson",
	version: "1.0",
	
	buildlog: function()	{
		alert(this.author);
		alert(this.version);
	},
	
	/* Ajax object */
	getXMLHttpObject: function()	{
		try	{
			this.xmlHttp = new ActiveXObject("Msxml2.XMLHTTP");
		}
		catch(e)	{
			try	{
				this.xmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
			}
			catch(e)	{
				this.xmlHttp = new XMLHttpRequest();
			}
		}
		
		if(this.xmlHttp)
			return this.xmlHttp;
		
		alert("Couldn´t create AJAX instance.");
	},
	
	toggle: function(id)	{
		this.obj = this.getObject(id);
		
		if(this.obj.style.display != "none")
			this.obj.style.display = "none";
		else
			this.obj.style.display = "block";			
	},
	
	is_numeric: function(num)	{
		return !isNaN(num);
	},
	
	getObject: function(id)	{
		if(this.obj = document.getElementById(id))
			return this.obj;
			
		if(!this.is_object(id))
			alert("getObject didn´t get a valid id or a object, returning the given id.");
			
		return id;
	},
	
	is_object: function(obj)	{
		if(typeof(obj) != 'object')
			return false;
			
		return true;
	},
	
	toShort: function(text, length)	{
		if(text.length < length)
			return true;
			
		return false;
	},
	
	toLong: function(text, length)	{
		if(text.length > length)
			return true;
			
		return false;
	},
	
	textCounter: function(field, counter, maxlimit, linecounter)	{
		this.fieldWidth	=  parseInt(field.offsetWidth);
		this.charcnt	= field.value.length;        

		if(this.charcnt > maxlimit)
			field.value = field.value.substring(0, maxlimit);
		else { 
			this.percentage = parseInt(100 - (( maxlimit - this.charcnt) * 100)/maxlimit) ;
			document.getElementById(counter).style.width =  parseInt((this.fieldWidth*this.percentage)/102)+"px";
			document.getElementById(counter).innerHTML=""+this.percentage+"%"
			//setcolor(document.getElementById(counter),this.percentage,"background-color");
		}
	}
}

var addBookmark = function(name, book_id, type, book_type, object)	{
	this.bookmarkparams = "name=" + escape(name) + "&book_id=" + escape(book_id) + "&type=" + escape(type) + "&book_type=" + escape(book_type);
	var self			= this;
	this.xmlHttp		= buildlog.getXMLHttpObject();
	this.xmlHttp.onreadystatechange = function()	{
		if(self.xmlHttp.readyState == 4)	{
			if(self.xmlHttp.responseText)
				alert(self.xmlHttp.responseText);
				
			if(object)	{
				
			}
			else if(book_type == "favorite")
				favorites.toggle();
			else if(book_type = "watched")
				watched.toggle();
				
		}
	}
	this.xmlHttp.open("POST", "ajax_bookmarks.php?INSERT", true);
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		this.xmlHttp.setRequestHeader("Content-length", this.bookmarkparams.length);
		this.xmlHttp.setRequestHeader("Connection", "close");
	this.xmlHttp.send(this.bookmarkparams);
}

var removePicture = function(id, type)	{
	this.params		= "id=" + id;
	this.xmlHttp		= buildlog.getXMLHttpObject();
	this.url 		= 'ajax_projectRemoveImage.php';
	var self		= this;
	
	this.xmlHttp.onreadystatechange = function()	{
		if(self.xmlHttp.readyState == 4)	{
			this.obj = document.getElementById("photo" + id);
			while(this.obj.hasChildNodes())
				this.obj.removeChild(this.obj.firstChild);
				
			this.obj.parentNode.removeChild(this.obj);
		}
	}
	
	if(type == 'build')
		this.url = 'ajax_buildRemoveImage.php';
	
	this.xmlHttp.open("POST", this.url , true);
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		this.xmlHttp.setRequestHeader("Content-length", this.params.length);
		this.xmlHttp.setRequestHeader("Connection", "close");
	this.xmlHttp.send(this.params);
}

var voteHandler = function(bg)	{
	this.bg			= document.getElementById(bg);
	this.startWidth	= this.bg.style.left;
	this.setWidth	= null;
	this.vote		= null;
	
	this.cBg = function(obj)	{
		this.bg.style.left = Math.round((parseInt(obj.style.left)) / 120 * 100) + "%";
	}
	
	this.mouseout = function()	{
		this.bg.style.left = (this.setWidth ? this.setWidth : this.startWidth);
	}
	
	this.onclick = function(obj, vote)	{
		this.vote = vote;
		this.setWidth = this.bg.style.left = Math.round((parseInt(obj.style.left)) / 120 * 100) + "%";
	}
}

var saveRate = function(vote, msg, project_id, type)	{
	if(!vote)	{
		alert("You have to leave a vote!");
		return;
	}
	else if(!msg)	{
		alert("You have to type a message!");
		return;
	}
	
	this.vote	= vote;
	this.msg	= msg;
	this.params = "message=" + escape(this.msg) + "&vote=" + this.vote + "&" + (type ? "build_id=" : "project_id=") + project_id;
	var self	= this;
	
	this.xmlHttp = buildlog.getXMLHttpObject();
	this.xmlHttp.onreadystatechange = function()	{
		if(self.xmlHttp.readyState == 4)	{	
			this.response	= self.xmlHttp.responseText;
			this.responseL	= this.response.length;
			this.responseS	= this.response.indexOf("R");
			this.totRate	= this.response.substr(0, this.responseS);
			this.newiHTML 	= this.response.substr(this.responseS + 1, this.responseL);
			
			document.getElementById("projectRate").style.width	= (100 - Math.min(100, Math.round(this.totRate * 16,66))) + "%"; 
			document.getElementById("slideContainer").innerHTML = this.newiHTML;	
				
			slider.down(document.getElementById('slideContainer').offsetHeight, 4000, null, true);
			leaverate.toggle();
		}
	}
	
	this.date = new Date().getTime();
	this.xmlHttp.open("POST", (type ? "ajax_buildgetStatusRate.php?" : "ajax_projectgetStatusRate.php?") + this.date, true);
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		this.xmlHttp.setRequestHeader("Content-length", this.params.length);
		this.xmlHttp.setRequestHeader("Connection", "close");
	this.xmlHttp.send(this.params);
}

var reportFunc = function(url, msg)	{
	this.xmlHttp = buildlog.getXMLHttpObject();
	this.params	 = "url=" + escape(url) + "&msg=" + escape(msg);
	var self	 = this;
	
	this.xmlHttp.onreadystatechange = function()	{
		if(self.xmlHttp.readyState == 4)	{
			report.toggle();
		}
	}
	
	this.xmlHttp.open("POST", "ajax_report.php", true);
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		this.xmlHttp.setRequestHeader("Content-length", this.params.length);
		this.xmlHttp.setRequestHeader("Connection", "close");
	this.xmlHttp.send(this.params);
}

var changePicture = function(id /*, pictures*/)	{
	this.obj 		= document.getElementById(id);
	this.pictures	= arguments;
	this.counter	= 1;
	this.size		= this.pictures.length;
	this.preload	= new Array();
	var self		= this;
	
	self.preload[1]	= new Image().src	= self.pictures[1] + "_thumb.jpg";
	
	this.change = function()	{
		if(self.counter >= this.size)
			self.counter = 1;
		else if(self.size < 2 || self.counter < 1)
			return;
			
		self.preload[this.counter+1]	= new Image().src = self.pictures[self.counter+1] + "_thumb.jpg";
		self.obj.src					= self.preload[self.counter];
		
		self.counter++;
		setTimeout(function() { self.change(); }, 5000);
	}
}

var smallBookmarks = function(title, name, book_id, type, book_type, object)	{
	return '<div class="popUp"><h5 style="margin-bottom:0;"><span style="float:left;">' + title + '</span><a href="#" onclick="' + (object) + '.toggle();" style="margin:5px 5px 5px 5px;float:right;font-size:12px;padding-right:5px;">Stäng</a></h5><span style="padding:0 5px 0 5px;"><form id="signupform" method="get" action="" style="padding:0 10px 10px 10px;"><h4 class="header">Namn</h4><input name="name" id="fav_name" value="' + name + '" style="border:1px solid #999999;width:319px;padding:5px;" /><br /><input name="signupsubmit2" type="button" class="save" id="signupsubmit3" value="Lägg till" style="margin:20px 0 0 0;color:#333333;" onclick="addBookmark(document.getElementById(\'fav_name\').value, \'' + book_id + '\', \'' + (type == "build" || type == "forum" ? (type == "build" ? "build" : "forum") :"project") + '\', \'' + (book_type == "favorite" ? "favorite" : "watched") + '\', \'' + object + '\');' + (object) + '.toggle();" /></form></span></div>';
}

var changeImageOrder = function(image_id, type_id, action, type)	{
	this.params		= "image_id=" + image_id + "&type_id=" + type_id + "&action=" + action + "&type=" + type;
	this.xmlHttp	= buildlog.getXMLHttpObject();
	var self		= this;
	
	this.xmlHttp.onreadystatechange = function()	{
		if(self.xmlHttp.readyState == 4)	{
			/*
			if(!isNaN(self.xmlHttp.responseText * 1))	{
				this.newer = document.getElementById("image" + image_id);
				this.older = document.getElementById("image" + self.xmlHttp.responseText);
				
				this.newSrc = this.newer.src;
				this.oldSrc = this.older.src;
				this.newId	= this.newer.id;
				this.oldId	= this.older.id;
				
				this.newer.src	= this.oldSrc;
				this.older.src	= this.newSrc;
				this.newer.id	= this.oldId;
				this.older.id	= this.newId; 
			}
			*/
			
			if(!isNaN(self.xmlHttp.responseText * 1))	{
				this.newer = document.getElementById("photo" + image_id);
				this.older = document.getElementById("photo" + self.xmlHttp.responseText);
				
				this.newiHTML = this.newer.innerHTML;
				this.oldiHTML = this.older.innerHTML;
				this.newID = this.newer.id;
				this.oldID = this.older.id;
				
				this.newer.innerHTML = this.oldiHTML;
				this.older.innerHTML = this.newiHTML;
				this.older.id = this.newID;
				this.newer.id = this.oldID;
				
			}
			
		//alert(self.xmlHttp.responseText);
		}
	}
	
	this.xmlHttp.open("POST", "ajax_changeimageorder.php", true);
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		this.xmlHttp.setRequestHeader("Content-length", this.params.length);
		this.xmlHttp.setRequestHeader("Connection", "close");
	this.xmlHttp.send(this.params);	
}

	var inviter = function(username, password)	{
		if((!username.match(/^[\w.-]+@([\w.-]+\.)+[a-z]{2,6}$/i)) || (!password))	{
			alert("Email or password is incorrect.");
			return;
		}
			
		this.params		= "u=" + escape(username) + "&p=" + escape(password) + "&get_contacts=1";
		var self		= this;
		
		this.xmlHttp = buildlog.getXMLHttpObject();
		this.xmlHttp.onreadystatechange = function()	{
			if(self.xmlHttp.readyState == 4)	{
				this.obj			= document.getElementById("inviterBox");
				this.obj.innerHTML	= self.xmlHttp.responseText;
			}
		}
		this.xmlHttp.open("POST", "ajax_inviter.php", true);
		this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
		this.xmlHttp.setRequestHeader("Content-length", this.params.length);
		this.xmlHttp.setRequestHeader("Connection", "close");
		this.xmlHttp.send(this.params);
	}
	
var getCheckedByParent = function(parent_id) {
	this.parent	= document.getElementById(parent_id);
	this.array	= parent.getElementsByTagName("input");
	this.length = this.array.length;
	this.params = "submit=1&mails=";
	var self	= this;

	for(var i=0;i<this.length;i++)	{
		if(this.array[i].checked)
			this.params += (this.params ? " " + escape(this.array[i].value) : escape(this.array[i].value));
	}
	
	this.xmlHttp = buildlog.getXMLHttpObject();
	this.xmlHttp.onreadystatechange = function()	{
		if(self.xmlHttp.readyState == 4)	{
			self.parent.innerHTML = self.xmlHttp.responseText;
		}
	}
	this.xmlHttp.open("POST", "ajax_inviter.php", true);
	this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
	this.xmlHttp.setRequestHeader("Content-length", this.params.length);
	this.xmlHttp.setRequestHeader("Connection", "close");
	this.xmlHttp.send(this.params);
	
	/*
			
	var collection = document.getElementById(aId).getElementsByTagName('INPUT');
	for (var x=0; x<collection.length; x++) {
		if (collection[x].type.toUpperCase()=='CHECKBOX')
			collection[x].checked = aChecked;
	}
	*/	
}

var handleImageComment = function(image_id, comment, type)	{
	if(type == 'project')
		type = 'project';
	else
		type = 'build';
		
	this.params = "image_id=" + image_id + "&comment=" + escape(comment) + "&type=" + type;
	var self = this;
	
	this.xmlHttp = buildlog.getXMLHttpObject();
	this.xmlHttp.onreadystatechange = function()	{
		if(self.xmlHttp.readyState == 4)	{
			//alert(self.xmlHttp.responseText);
		}
	}
	
	this.xmlHttp.open("POST", "ajax_savedescription.php", true);
	this.xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded;");
	this.xmlHttp.setRequestHeader("Content-length", this.params.length);
	this.xmlHttp.setRequestHeader("Connection", "close");
	this.xmlHttp.send(this.params);		
}

