function rate(rating, type_id, item_id) {
	var par = 'type_id=' + type_id + '&item_id=' + item_id + '&rating=' + rating;
	$.post('/ajax/rating_action.php', par, function(data) {
		alert(data.message);
		if (data.code == 0) {
			var hot_perc = Math.round((data.avg_vote * 20 - 20) * 1.25);
			var cold_perc = 100 - hot_perc;
			$('#percent_hot').text(hot_perc + '%');
			$('#percent_cold').text(cold_perc + '%');
			$('#bar_hot').css("width", Math.round(hot_perc * 1.78) + 'px');
			$('#bar_cold').css("width", Math.round(cold_perc * 1.78) + 'px');
		}
	}, "json");
}


function addToFavorites(type_id, item_id) {
		var par = { action: "add", item_id: item_id, type_id: type_id };
        $.post('/ajax/favorite_action.php', par, function (data) {
        	if (data.code == 2) {
               	t_link = "";
				args = window.location.search.substring(1).split("&");
				for (i = 0; i < args.length; i++) {
					args[i] = args[i].split("=");
					if (args[i][0] == "t_link")
						t_link = "?t_link=" + args[i][1];
				}
				window.location = "/join.html" + t_link;
        	} else {
        		alert(data.message);
        	}
		}, "json");
}
     
    
// remove selected content from current members favorites
function removeFavorite( type_id, item_id ){
        if( confirm("This will delete this record from your favorites\nAre you sure you wish to continue?") ){
                var par = { action: "remove", type_id: type_id, item_id: item_id };
                $.post('/ajax/favorite_action.php', par, function(data) {
                    alert(data.message);
                }, "json");
        }
}
    
    
// clear favorites for current member
function clearFavorites(){
    if( confirm("This will remove *all* your favorites.\nAre you sure you wish to continue?") ){
            var par = 'action=clear';
            $.post( '/favorite_action.php', par, function (msg) { });
    }
}
    
    
    
function rateComment( comment_id, rating) {
	var par = { action: "bump", comment_id: comment_id, rating: rating };
	$.post('/ajax/comment_action.php', par, function(data) {
		alert(data.message);
	}, "json");
}
    
    
function addComment(scene) {
        var comment = $('#newComment').val();
        var par     = { action: "add", type_id: 5, item_id: scene, body: comment };
        if( comment == "" ) {
            alert("Unable to add your comment since it contains no text.");
            return;
        }
        $.post( '/ajax/comment_action.php', par, function(data) {
        	alert(data.message);
			$("#newCommentContainer").hide();
            $("#thankYouContainer").show();
		}, "json");
}

function initRolls () {
	$("ul.thumbs li a.image").each(function() {
		this.images = new Array();
		this.origimg = this.style.backgroundImage;
		if (this.origimg != '') {
			prefix = this.origimg.replace(/["']+/g, '').substring(4);
			prefix = prefix.substring(0, prefix.length - 1);
			for (y = 0; y < 5; y++) {
				this.images[y] = new Image();
				this.images[y].src = prefix.replace(/(sample|promo)_[0-9]+/, "promo_" + (2 + y * 2));
			}
			$(this).unbind().hover(
				function() {rollImages(this);},
				function() {clearRoll(this);}
			);
		}
	});
}
function rollImages(thumb, start) {
	if (start == undefined || start > thumb.images.length - 1)
		start = 0;
	thumb.style.backgroundImage = "url(" + thumb.images[start].src + ")";
	alertTimerId = setTimeout (function() {rollImages(thumb, start + 1);}, 652);
}
function clearRoll(thumb) {
	clearTimeout(alertTimerId);
	thumb.style.backgroundImage = thumb.origimg;
}


$(function() {
	initRolls();
});
