var ascii;
var ratingInfo = new Array(2);

// initialize the fluid XMLHttpRequest Sub System
fluidXMLHttpRequestInit();

function rate(id) {
        var rating = document.forms[0].rating.value;
        var filter = document.forms[0].filter.value;
	var status = 0;
	var url = 'http://tv.case.edu/actions/tv/rate?rating=' + rating + '&id=' + id + '&return=fluid&filter=' + filter;
				
	status = fluidXMLHttpRequest(url,true);

	if(status == 0) {
		setTimeout('updateImage(' + id + ')', 500);
	}
	else {
		alert('Could not connect to server to send rating. Please try again later.');
	}
}

function updateImage(id) {
	if(fluidXMLHttpRequestWait() > 0) {
		setTimeout('updateImage(' + id + ')', 500);
	}
	else {
		ascii = fluidXMLHttpRequestResponseText();
		ratingInfo[0] = fluidGetXMLValue('trating', ascii);
                ratingInfo[1] = fluidGetXMLValue('nrating', ascii);
					
                ratingInfo[0] = (Math.floor(ratingInfo[0] * 10)) / 10;

		document.getElementById('rating' + id).src = 'http://tv.case.edu/actions/tv/rating?id=' + id + '&refresh=true';
		document.getElementById('nrating' + id).childNodes[0].data = 'Number of Ratings: ' + ratingInfo[1];
		document.getElementById('trating' + id).childNodes[0].data = 'Current Rating: ' + ratingInfo[0] + '/5';
	}
}


function getRandomIndex() {
     return Math.floor((Math.random()*5) + 1);
}

function getUniqueImage(l) {
     var path = '';

     if(l == 'studentstuff_home') {
           path = '/tv/default/thumbs/student/student' + getRandomIndex() + '.jpg';
     }
     else if(l == 'soapbox_home') {
           path = '/tv/default/thumbs/soapbox/soapbox' + getRandomIndex() + '.jpg';
     }
     else if(l == 'sports_home') {
           path = '/tv/default/thumbs/sports/sports' + getRandomIndex() + '.jpg';
     }
     else if(l == 'academics_home') {
           path = '/tv/default/thumbs/academics/academics' + getRandomIndex() + '.jpg';
     }
     else if(l == 'talkingheads_home') {
           path = '/tv/default/thumbs/talking/talking' + getRandomIndex() + '.jpg';
     }
     else if(l == 'stories_home') {
           path = '/tv/default/thumbs/stories/stories' + getRandomIndex() + '.jpg';
     }
     else if(l == 'inthecircle_home') {
           path = '/tv/default/thumbs/circle/circle' + getRandomIndex() + '.jpg';
     }
     else if(l == 'working_home') {
           path = '/tv/default/thumbs/working/working' + getRandomIndex() + '.jpg';
     }
     else {
          // do nothing
     }
     return path;
}


function setLiImage(element) {
     if(document.getElementById(element)) {
         document.getElementById(element).style.backgroundImage = 'url(' + getUniqueImage(element) + ')';
     }
     else {
         // do nothing
     }
}

function runLoadHandler() {
     setLiImage('studentstuff_home');
     setLiImage('soapbox_home');
     setLiImage('sports_home');
     setLiImage('academics_home');
     setLiImage('talkingheads_home');
     setLiImage('stories_home');
     setLiImage('inthecircle_home');
     setLiImage('working_home');
}
