
var preloaded = new Array();
preload_images('/images/buttons/star_set.gif', '/images/buttons/star_on.gif', '/images/buttons/star_out.gif');
	
function preload_images() {
	for (var i = 0; i < arguments.length; i++) {
		preloaded[i] = document.createElement('img');
		preloaded[i].setAttribute('src', arguments[i]);
	}
}

function build_stars(star_ids) {
	for (var k in star_ids) {
		var content = '';
		for (var i = 1; i <= star_ids[k]; i++) {
			content += '<img src="/images/buttons/star_set.gif" />';
		}
		for (var j = star_ids[k] + 1; j <= 5; j++) {
			content += '<img src="/images/buttons/star_out.gif" />';
		}
		$('star_id_' + k).innerHTML = content;
	}
}
function build_star(star_id) {
	var content = '';
	for (var i = 1; i <= 5; i++) {
		content += '<img src="/images/buttons/star_out.gif" id="star_' + star_id + '_' + i + '" onclick="star_set(' + star_id + ', ' + i + ')" onmouseover="star_on(' + star_id + ', ' + i + ')" onmouseout="star_out(' + star_id + ')" />';
	}
	$('star_id_' + star_id).innerHTML = content;

	for (var j = 1; j <= star_ids[star_id]; j++) {
		$('star_' + star_id + '_' + j).src = '/images/buttons/star_set.gif';
	}
}
function star_on(id, star) {
	for (var j = 1; j <= star; j++) {
		$('star_' + id + '_' + j).src = '/images/buttons/star_on.gif';
	}
}
function star_out(id) {
	for (var j = 1; j <= star_ids[id]; j++) {
		$('star_' + id + '_' + j).src = '/images/buttons/star_set.gif';
	}

	for (var j = star_ids[id] + 1; j <= 5; j++) {
		$('star_' + id + '_' + j).src = '/images/buttons/star_out.gif';
	}
}

function set_star_out(id, star_rating) {
	var star;
	for (var j = 1; j <= star_rating; j++) {
		star = $('star_' + id + '_' + j);
		star.src = '/images/buttons/star_on.gif';
		star.onmouseover = star.onmouseout = star.onclick = function () {};
	}
	for (var j = star_rating + 1; j <= 5; j++) {
		star = $('star_' + id + '_' + j);
		star.src = '/images/buttons/star_out.gif';
		star.onmouseover = star.onmouseout = star.onclick = function () {};
	}
	$('star_id_' + id).title = 'Average Rating: ' + star_ids[id];
}

function star_set(id, star) {
	toggle_media_working('set_star_status');
	new ajax (
		'/actions.php?action=set_star&media_id=' + id + '&rating=' + star, {
			onComplete : function(req) {
				set_star_out(id, star);
				toggle_media_working('set_star_status');
				var num_votes = $('num_votes');
				var num_votes_count = Number((num_votes.innerHTML).replace(',', ''));
				num_votes.innerHTML = num_votes_count + 1;
			}
		}
	);
}
function toggle_media_working(id) {
	var loading_status = $(id);
	loading_status.style.display = loading_status.style.display == 'none' ? '' : 'none';
}

function top10_tabs_select(tab, type_id) {
	if (in_array(tab, top10_tab_cache)) { // cache found
		$(top10_tab_selected).className = ''; // deselect tab
		$(top10_tab_selected + '_tab').style.display = 'none'; // hide tabe content
		$(tab).className = 'top10_tabs_current'; // select new tab
		var tab_content = $(tab + '_tab');
		tab_content.style.display = '';
		top10_tab_selected = tab;
	}
	else { // no cache
		toggle_media_working('loading_status');
		top10_tab_cache.push(tab);
	
		$(top10_tab_selected).className = ''; // deselect tab
		$(top10_tab_selected + '_tab').style.display = 'none'; // hide tab content
		$(tab).className = 'top10_tabs_current'; // select new tab
		top10_tab_selected = tab; // set new selected tab
		new ajax (
			'/actions.php?action=media_top10_tab&top10=' + tab + '&type_id=' + type_id, {
				onComplete : function(req) {
					var tab_content = $(tab + '_tab');
					tab_content.innerHTML = req.responseText;
					tab_content.style.display = '';
					toggle_media_working('loading_status');
				}
			}
		);
	}
}

function show_video(file, width, height) {
	$('video_display').innerHTML = '<embed src="' + file + '" width="' + width + '" height="' + height + '" autoplay="true" controller="true" />';
	/*
	$('video_display').innerHTML = '<object type="video/x-ms-wmv" data="' + file + '" width="' + width + '" height="' + height + '">' +
	'<param name="src" value="' + file + '" />' + 
	'<param name="autostart" value="true" />' + 
	'<param name="controller" value="true" />' + 
	'<param name="AllowChangeDisplaySize" value="true" />' +
	'<param name="ShowDisplay" value="1" />' +
	'<param name="ShowStatusBar" value="1" />' +
'</object>';*/
}

function send_to_friend(media_id) {
	var your_name = $('your_name');
	var your_email = $('your_email');
	var friends_email = $('friends_email');
	if (your_name.value.length > 2 && your_email.value.length > 5 && friends_email.value.length > 5) {
		new ajax (
			'/actions.php?action=sendtofriend&media_id=' + media_id + '&your_name=' + escape(your_name.value) + '&your_email=' + escape(your_email.value) + '&friends_email=' + escape(friends_email.value), {
				onComplete : function(req) {
					var contents = req.responseText.split('|');
					var notice = $('sendtofriend_notice');
					notice.innerHTML = contents[1];
					notice.style.display = '';
					if (contents[0] > 0) { // submit successful
						your_name.value = your_email.value = friends_email.value = '';
					}
				}
			}
		);
	}
	else {
		var notice = $('sendtofriend_notice');
		notice.innerHTML = 'Gotta fill out the fields first Einstein.';
		notice.style.display = '';
	}

	return false;
}