document.onmousemove=get_mouse;
var popup = document.getElementById('pop_up_box');

function get_mouse(e) {
	if (popup) {
		popup.style.left = event.clientX + 'px';
		popup.style.top  = (event.clientY+20) + 'px';
	} else {
		popup = document.getElementById('pop_up_box');
	}
}

function sendImage() {
	var file = document.getElementById('uploadedfile');
	var url  = document.getElementById('urlfile');
	var titl = document.getElementById('title');
	var tags = document.getElementById('tags');
	var desc = document.getElementById('descript');
	
	var error_sec = document.getElementById('error_section');
	
	var valid = true;
	
	var tag_count = tags.value.split(",");
	var new_tag_line = '';
	for (var i = 0; i < tag_count.length; i++) {
		if ((tag_count[i] == '')|| (tag_count[i] == null)) continue;
		if (i != (tag_count.length-1)) {new_tag_line += tag_count[i].trim() + ','; } else { new_tag_line += tag_count[i].trim(); }
	}
	if (new_tag_line.charAt(new_tag_line.length-1) == ",") { new_tag_line = new_tag_line.substring(0, new_tag_line.length-2);}
	var error = '';

	if ((file.value == '') && (url.value == '')) {
		//#alert('You must select a file to upload.');
		if (error != '') error += '<br />';
		error += "You must select a file or URL to upload!";
		valid = false;
		file.style.backgroundColor='red';
		url.style.backgroundColor='red';
	}
	
	if ((file.value != '') && (url.value != '')) {
		if (error != '') error += '<br />';
		error += "Please only select a file or URL to upload, not both.";
		valid = false;
		url.style.backgroundColor='red';
		file.style.backgroundColor='red';
	}
	
	if (titl.value.length > 40) {
		if (error != '') error += '<br />';
		error += "Your title is too long! Place some of the text in the description, please.";
		valid = false;
		titl.style.backgroundColor='red';
	}
	
	if (titl.value == '') {		
		if (error != '') error += '<br />';
		error += "You must enter a title for your item!";
		valid = false;
		titl.style.backgroundColor='red';
	}
	
	/*
	if (desc.value == '') {
		//alert('You must enter a title for your image.');
		if (error != '') error += '<br />';
		error += "Please enter a short description for your item!";
		valid = false;
		desc.style.backgroundColor='red';
	}
	*/
	
	if (new_tag_line.split(",").length < 2) {
		//alert('You must enter at least two tags.');
		if (error != '') error += '<br />';
		error += "You must enter at least two tags for your item!";
		valid = false;
		tags.style.backgroundColor='red';
	}
	
	
	if (!valid) { error_sec.innerHTML = error; }
	
	if (valid) {
		var text = document.getElementById('submit_sec');
		tags.value = new_tag_line;		
		text.innerHTML = 'Uploading item... please wait...';
		document.forms["uploadForm"].submit();
		
	}
	 
	
}

function ajaxObject(url, callbackFunction) {
  var that=this;      
  this.updating = false;
  this.abort = function() {
    if (that.updating) {
      that.updating=false;
      that.AJAX.abort();
      that.AJAX=null;
    }
  }
  this.update = function(passData,postMethod) { 
    if (that.updating) { return false; }
    that.AJAX = null;                          
    if (window.XMLHttpRequest) {              
      that.AJAX=new XMLHttpRequest();              
    } else {                                  
      that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
    }                                             
    if (that.AJAX==null) {                             
      return false;                               
    } else {
      that.AJAX.onreadystatechange = function() {  
        if (that.AJAX.readyState==4) {             
          that.updating=false;                
          that.callback(that.AJAX.responseText,that.AJAX.status,that.AJAX.responseXML);        
          that.AJAX=null;                                         
        }                                                      
      }                                                        
      that.updating = new Date();                              
      if (/post/i.test(postMethod)) {
        var uri=urlCall+'?'+that.updating.getTime();
        that.AJAX.open("POST", uri, true);
        that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        that.AJAX.setRequestHeader("Content-Length", passData.length);
        that.AJAX.send(passData);
      } else {
        var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
        that.AJAX.open("GET", uri, true);                             
        that.AJAX.send(null);                                         
      }              
      return true;                                             
    }                                                                           
  }
  var urlCall = url;        
  this.callback = callbackFunction || function () { };
}

var imgstr = null;
var imgdex = 0;



function update_message() {
	var messageRequest = new ajaxObject('/_lib/_ajax/front.message.php', processData);
	messageRequest.update('test=1');
	//setTimeout("update_message()", 60000);
}

function processData(responseText, responseStatus) {
  if (responseStatus==200) {
    //alert(responseText);
	//document.getElementById('front_message').innerHTML = responseText;
	imgstr = responseText;
	cycle_message();
  } else {
    //alert(responseStatus);
  }
}

function cycle_message() {
	if (imgstr == null) return;	
	if (window.DOMParser)
	{
		parser=new DOMParser();
		xmlDoc=parser.parseFromString(imgstr,"text/xml");
	}
	else // Internet Explorer
	{
		xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
		xmlDoc.async="false";
		xmlDoc.loadXML(imgstr); 
	}
	
	var dexArray1 = xmlDoc.getElementsByTagName("Adex");
	var titArray1 = xmlDoc.getElementsByTagName("Atitle");
	var urlArray1 = xmlDoc.getElementsByTagName("Aurl");
	
	var dexArray2 = xmlDoc.getElementsByTagName("Bdex");
	var titArray2 = xmlDoc.getElementsByTagName("Btitle");
	var urlArray2 = xmlDoc.getElementsByTagName("Burl");
	var viwArray2 = xmlDoc.getElementsByTagName("Bviews");
	
	var dexArray3 = xmlDoc.getElementsByTagName("Cdex");
	var titArray3 = xmlDoc.getElementsByTagName("Ctitle");
	var urlArray3 = xmlDoc.getElementsByTagName("Curl");
	var votArray3 = xmlDoc.getElementsByTagName("Cvotes");
	
	//console.dir(imgstr);
	//alert(dexArray[0].textContent);
	
	var htmlStr1  = '<span style="font-size: 10px;"><a href="view/' + dexArray1[imgdex].textContent +'">' + titArray1[imgdex].textContent + '<br /><img border="0" onerror="this.src=\'/uploads/thumbs/none.jpg\';" src="/uploads/thumbs/' + dexArray1[imgdex].textContent + '_thumb.' + urlArray1[imgdex].textContent.split(".")[urlArray1[imgdex].textContent.split(".").length-1] + '"></a></span>';
	var htmlStr2  = '<span style="font-size: 10px;"><a href="view/' + dexArray2[imgdex].textContent +'">' + titArray2[imgdex].textContent + '<br /><img border="0" onerror="this.src=\'/uploads/thumbs/none.jpg\';" src="/uploads/thumbs/' + dexArray2[imgdex].textContent + '_thumb.' + urlArray2[imgdex].textContent.split(".")[urlArray2[imgdex].textContent.split(".").length-1] + '"><br />' + viwArray2[imgdex].textContent + ' views.</a></span>';
	var htmlStr3  = '<span style="font-size: 10px;"><a href="view/' + dexArray3[imgdex].textContent +'">' + titArray3[imgdex].textContent + '<br /><img border="0" onerror="this.src=\'/uploads/thumbs/none.jpg\';" src="/uploads/thumbs/' + dexArray3[imgdex].textContent + '_thumb.' + urlArray3[imgdex].textContent.split(".")[urlArray3[imgdex].textContent.split(".").length-1] + '"><br />' + parseFloat(votArray3[imgdex].textContent).toFixed(1) + ' rating.</a></span>';
	
	if (dexArray1[imgdex].textContent != null)
		document.getElementById('recent_message').innerHTML = htmlStr1;
	
	if (dexArray2[imgdex].textContent != null)
		document.getElementById('vote_message').innerHTML = htmlStr3;
		
	if (dexArray3[imgdex].textContent != null)
		document.getElementById('view_message').innerHTML = htmlStr2;
	
	imgdex++;
	if (imgdex == 10) imgdex = 0;
	setTimeout("cycle_message()", 3500);
}


function send_new_tag(tag, page) {
	var messageRequest = new ajaxObject('/_lib/_ajax/suggest.tag.php', processTag);
	messageRequest.update('tag=' + tag + '&dex=' + page);
	//setTimeout("update_message()", 60000);
}

function add_comment() {
	var ref = document.getElementById('page_id_comment').value;
	var com = document.getElementById('comment_comment').value;
	if (com == "Leave a comment!") return;
	document.getElementById('comment_comment').disabled = true;
	document.getElementById('comment_submit_button').innerHTML = 'Sending comment...';
	
	var messageRequest = new ajaxObject('/_lib/_ajax/add.comment.php', processComment);
	if ((com != "") && (ref != "")) {
		messageRequest.update('comment=' + com + '&ref=' + ref);
	}
	setTimeout("refresh_page()", 3000);
	//setTimeout("update_message()", 60000);
}

function processTag(responseText, responseStatus) {
  if (responseStatus==200) {
    
  } else {
  }
}

function processComment(responseText, responseStatus) {
  if (responseStatus==200) {
  //
    if (responseText == "0") {
		//document.getElementById('comment_submit_span').innerHTML = 'Comment failed! Did you submit too early?';
		alert('You must wait at least 60 seconds before leaving another comment.');
	} else {
		//document.getElementById('comment_submit_span').innerHTML = 'Comment sent.';
		//document.getElementById('comment_submit_button').disabled = true;
		document.getElementById('whole_comment_section').innerHTML = 'Comment saved!';
	}
  } else {
  }
}

function send_vote(vote) {	
	var ref = document.getElementById('page_id_comment').value;	
	var button = document.getElementById('vote_' + vote);
	button.style.backgroundColor='#FF7700';
	button.id = 'voted';
	for (var c = 0; c < 11; c++) {
		if (c != vote) {
			document.getElementById('vote_' + c).disabled = true;
			document.getElementById('vote_' + c).style.color = '#4B4B4B';
		}
	}	
	var messageRequest = new ajaxObject('/_lib/_ajax/send.vote.php', processVote);
	
	if ((vote < 11) && (vote > -1) && (ref != "")) {
		messageRequest.update('vote=' + vote + '&ref=' + ref);
		
	}
	//setTimeout("update_message()", 60000);
}

function processVote(responseText, responseStatus) {
  if (responseStatus==200) {
    if (responseText != "-1") {
		var button = document.getElementById('voted');
		button.style.backgroundColor='#027700';
	} else {
		var button = document.getElementById('voted');
		button.style.backgroundColor='#770200';
		
	}	
  } else {
  }
}

function send_message(from) {
	sub = document.getElementById('subject').value;
	mes = document.getElementById('message').value;
	to  = document.getElementById('to').value;
	
	document.getElementById('send_btn').disabled = true;
	
	if (mes == "") {
		document.getElementById('error_message').innerHTML = "Please enter a message!";
		document.getElementById('send_btn').disabled = false;
	} else {
		
		if (sub == "") {
			if (confirm('Subject is blank, send anyway?')) {
				var messageRequest = new ajaxObject('/_lib/_ajax/send.message.php', processMessage);	
				messageRequest.update('from=' + from + '&sub=' + sub + '&mes=' + mes + '&to=' + to);
				document.getElementById('send_btn').disabled = true;
			} else {
				document.getElementById('send_btn').disabled = false;
			}
		} else {
			var messageRequest = new ajaxObject('/_lib/_ajax/send.message.php', processMessage);	
			messageRequest.update('from=' + from + '&sub=' + sub + '&mes=' + mes + '&to=' + to);
			document.getElementById('send_btn').disabled = true;
		}
	}
}

function processMessage(responseText, responseStatus) {
  if (responseStatus==200) {
	if (responseText == 1) {
		document.getElementById('final_msg').innerHTML = 'Message sent.</span><br /><span><a href=\"javascript:history.back();\">Return to Previous Page</a>';
	} else {
		document.getElementById('final_msg').style.color = 'red';
		document.getElementById('final_msg').innerHTML = 'Message was not sent.';
	}
  } else {
	document.getElementById('final_msg').style.color = 'red';
	document.getElementById('final_msg').innerHTML = 'Message was not sent.';
  }
}

function show_popup(titl, view) {
	if (popup) {
		popup.style.display = 'block';
		if (titl != "") {
			popup.innerHTML = "<div class=\"object_container\" style=\"opacity:0.8; filter:alpha(opacity=80); padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px;\"><b>" + titl + "</b><br />" + view + " views.</div>";
		} else {
			popup.innerHTML = "<div class=\"object_container\" style=\"opacity:0.8; filter:alpha(opacity=80); padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px;\">" + view + " views.</div>";
		}
	}
}

function default_popup(text) {
	if (popup) {
		popup.style.display = 'block';
		popup.innerHTML = "<div class=\"object_container\" style=\"opacity:0.8; filter:alpha(opacity=80); padding-left: 5px; padding-right: 5px; padding-top: 5px; padding-bottom: 5px;\">" + text + "</div>";
	}
}

function hide_popup() {
	if (popup) {
		popup.style.display = 'none';
		popup.innerHTML = '';
	}
}

function refresh_page() {
	location.reload(true);
}
