////////////
// rollover nav
var home = new Image();
var homeOver = new Image();
var pics = new Image();
var picsOver = new Image();
var upload = new Image();
var uploadOver = new Image();
var vids = new Image();
var vidsOver = new Image();

home.src = 'images/nav-home.gif';
homeOver.src = 'images/nav-home-over.gif';
pics.src = 'images/nav-pictures.gif';
picsOver.src = 'images/nav-pictures-over.gif';
upload.src = 'images/nav-upload.gif';
uploadOver.src = 'images/nav-upload-over.gif';
vids.src = 'images/nav-videos.gif';
vidsOver.src = 'images/nav-videos-over.gif';
// end rollover nav
//////////////

function pop_upload(destination){
	var upwin = window.open(destination,"upwin","width=400,height=450,scrollbars=0,menubar=0,location=0");
	upwin.focus();
}

function com(){
    	createRequest();
		var class_id = document.getElementById('class_id').value;
		var ncom = document.getElementById("cominfo");
		var media_id = document.getElementById("media_id").value;
		// alert('id: ' + media_id);
		replaceText(ncom, "Posting comment...");
		var new_comment = document.getElementById("new_comment").value;
		// alert(": " + new_comment);
		var the_user = document.getElementById("user_name").value;
		// alert("this username: " + the_user);
		var com_url = "post_comment.php?target_id=";
		com_url += media_id;
		com_url += "&class_id=" + class_id;
		com_url += "&user_name=" + the_user;
		com_url += "&comment=" + new_comment;
		// alert("com: " + new_comment);
		// alert("url: " + com_url);
		request.open("GET",com_url,true);
    	// request.onreadystatechange = update_comments;
    	request.onreadystatechange = update_comments;
		request.send(null);
}
/*
////////
// enter key search submission
function checkEnter(e){
/*
if(window.event) // IE
  { keynum = e.keyCode;}
else if(e.which) // Netscape/Firefox/Opera
  { keynum = e.which; }
// alert("keynum: " + keynum);
// var bob = document.getElementById('search_box').value
// alert('search this: ' + bob)
if(keynum == '13'){
	get_search(1);
	}
}
*/
////////
//////////////////////////
/// updating comments //
/*
function get_search(class_id) {
		createRequest();
		var search_box = document.getElementById("search_box").value;
		alert('s: ' + search_box);
		//var queryurl = "get_search_results.php?query=bark&class_id=1";
		var queryurl = "get_search_results.php?query="
		queryurl += search_box;
		queryurl += "&class_id=";
		queryurl += class_id;
		document.getElementById("search_box").value = "searching...";
		alert("queryurl: " + queryurl);
		queryurl += search_box;
		request.open("GET",queryurl,true);
    	request.onreadystatechange = display_search_results;
		request.send(null);
	}
function display_search_results(){
    	if (request.readyState == 4) {
			document.getElementById("search_box").value = "";
			var srdiv = document.getElementById("display-media");
			var srresponse = request.responseText;
			srdiv.innerHTML = srresponse;
		}	
	}
	
// end updating comments
////////////////////////
*/

//////////////////////////
/// updating comments //

function update_comments() {
    	if (request.readyState == 4) {
			// alert('success');
	//		var ncom = document.getElementById("cominfo");
      		var got_comments = request.responseText;
			if (got_comments == 1){ l_com();}
		}	
	}

function l_com(){
		createRequest();
		var ncom = document.getElementById("cominfo");
		replaceText(ncom, "Loading comments now...");
		
		var class_id = document.getElementById('class_id').value;
		var media_id = document.getElementById("media_id").value;
		l_com_url = "get_comments.php?target_id=";
		l_com_url += media_id;
		l_com_url += "&limit=10";
		l_com_url += "&classi_id=" + class_id;
		request.open("GET",l_com_url,true);
    	request.onreadystatechange = update_l_comments;
		request.send(null);
	}	
	
function update_l_comments(){
    	if (request.readyState == 4) {
			var ncom = document.getElementById("cominfo");
			replaceText(ncom, "Add another comment?");
			var l_com_div = document.getElementById("com");
			var l_com_response = request.responseText;
			clearText(l_com_div);
			l_com_div.innerHTML = l_com_response;
			// var clean_up = document.getElementById("com_form");
			// clean_up.innerHTML = "<br><br><br>";
		}	
	}
	
// end updating comments
////////////////////////
	function fetchData(url,dataRequested,objectID){
		if (dataRequested) var dataRequest = "dataRequest="+ dataRequested;
		var pageRequest = false
		if (window.XMLHttpRequest) {
			pageRequest = new XMLHttpRequest()
		}
		else if (window.ActiveXObject){ 
			try {
				pageRequest = new ActiveXObject("Msxml2.XMLHTTP")
			} 
			catch (e) {
			try{
				pageRequest = new ActiveXObject("Microsoft.XMLHTTP")
			}
			catch (e){}
		}
	}
	else return false
	pageRequest.onreadystatechange=function() {
		filterData(pageRequest,objectID)
	}
	if (dataRequested) {		
		pageRequest.open('POST', url, true);
  	  	pageRequest.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    	pageRequest.send(dataRequest);
	}
	else {
		pageRequest.open('GET', url, true)
		pageRequest.send(null)	
	}
}
var objPopUp = null;

function popUp(whichLink) {
	fetchData('dataPage.php',whichLink,'popUpMessage')
	objPopTrig = document.getElementById(whichLink);
	objPopUp = document.getElementById('popUpMessage');

	objPopUp.innerHTML='<b>Loading</b>';

	xPos = objPopTrig.offsetLeft;
	yPos = (objPopTrig.offsetTop + objPopTrig.offsetHeight);
	if (xPos + objPopUp.offsetWidth >  document.body.clientWidth) xPos = xPos - objPopUp.offsetWidth;
	if (yPos + objPopUp.offsetHeight >  document.body.clientHeight) yPos = yPos - objPopUp.offsetHeight - objPopTrig.offsetHeight;
	// alert("xpos: " + xPos + " ypos: " + yPos);
	objPopUp.style.left = xPos + 'px';
	objPopUp.style.top = yPos + 'px';
	objPopUp.style.visibility = 'visible';
}

function popHide() {
	objPopUp.style.visibility = 'hidden';
	objPopUp = null;
}

function filterData(pageRequest,objectID){
	if (pageRequest.readyState == 4 && (pageRequest.status==200 || window.location.href.indexOf("http")==-1))
		document.getElementById(objectID).innerHTML=pageRequest.responseText
}

function print_ad(whichLink){
	objToPrint = document.getElementById(whichLink);
	objToPrint = whichLink + ".jpg";
	alert("objToPring: " + objToPrint);
	objToPrint.print();  
}
function replaceText(el, text) {
  if (el != null) {
    clearText(el);
    var newNode = document.createTextNode(text);
    el.appendChild(newNode);
  }
}
function changeHTML(e1, newHTML){
  if (e1 != null) {
    clearText(e1);
	document.getElementById('rate').innerHTML=newHTML;
	}
}
function clearText(el) {
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        el.removeChild(childNode);
      }
    }
  }
}

function getText(el) {
  var text = "";
  if (el != null) {
    if (el.childNodes) {
      for (var i = 0; i < el.childNodes.length; i++) {
        var childNode = el.childNodes[i];
        if (childNode.nodeValue != null) {
          text = text + childNode.nodeValue;
        }
      }
    }
  }
  return text;
}


   var request = null;

function createRequest() {
     try {
       request = new XMLHttpRequest();
     } catch (trymicrosoft) {
       try {
         request = new ActiveXObject("Msxml2.XMLHTTP");
       } catch (othermicrosoft) {
         try {
           request = new ActiveXObject("Microsoft.XMLHTTP");
         } catch (failed) {
           request = null;
         }
       }
     }

     if (request == null)
       alert("Error creating request object!");
}

function set_script(value, id){
	// alert('' + id + ' ' + value);
	set_ratings(value, id);
	var empty_old_overall = document.getElementById("new_rating");
	replaceText(empty_old_overall, "Total Votes: ");
    
}
function new_rating(){
    createRequest();
	var url2 = "get_ratings_average.php?id=";
	thisid = document.getElementById('thisid').value;
	url2 += thisid;
	// alert('url2: ' + url2);
	request.open("GET",url2,true);
    request.onreadystatechange = updatePage2;
	request.send(null);
}

function updatePage2() {
    if (request.readyState == 4) {
    	var got_rating = request.responseText;
   		var new_rating_div = document.getElementById("star");
	  	if((got_rating >= 0)&&(got_rating < 2)){ new_rating_div.className = "star1";}
	  	if((got_rating > 2)&&(got_rating < 3)){ new_rating_div.className = "star2";}
	  	if((got_rating > 3)&&(got_rating < 4)){ new_rating_div.className = "star3";}
	  	if((got_rating > 4)&&(got_rating < 4.5)){ new_rating_div.className = "star4";}
	  	if(got_rating >= 4.5){ new_rating_div.className = "star5";}
		show_total_ratings();
    }
}

function show_total_ratings(){
    createRequest();
	var url3 = "get_ratings_total.php?id=";
	thisid = document.getElementById('thisid').value;
	url3 += thisid;
	request.open("GET",url3,true);
    request.onreadystatechange = updatePage3;
	request.send(null);
}

function updatePage3() {
    if (request.readyState == 4) {
      var got_total_ratings = request.responseText;
   	  var new_total_ratings = document.getElementById("new_rating");
	  replaceText(new_total_ratings, "Total Votes: " + got_total_ratings);
    }
}

function set_ratings(value, id){
    createRequest();
    var url = "post_ratings.php?id=" + id + "&value=" + value;
	// alert("url: "+ url );
	request.open("GET",url,true);
    request.onreadystatechange = updatePage;
	request.send(null);
}
function updatePage() {
    if (request.readyState == 4) {
      var is_success = request.responseText;
      var show_success = document.getElementById("success");
      replaceText(show_success, is_success); 
	  new_rating();
	 youvoted();
    }
}
