//
// Common JavaScript Functions
//

//
// jQuery Functions
//
$(document).ready(function(){
	var pathname = window.location.pathname;
	
	//
	// Featured Games - Homepage
	//
	if(pathname == '/') {
		featuredGames(0);
		$(function() {
			var fTimer = setInterval(fGame, 7000);
			var game = 1;
			
			function fGame() {
				if(game < 5) {
					featuredGames(game);
					game++;
				}
				else {
					featuredGames(0);
					game = 1;
				}
			}
			
			$("div.featured ul.side li").hover(function() {
				if(!$(this).is(".selected")) {
					clearInterval(fTimer);
					featuredGames($(this).index());
				}
			}, function() {
				fTimer = setInterval(fGame, 7000);
				game = $(this).index()+1;
			});
		});
	}
	function featuredGames(id) {
		var featured = $("div.featured");
		var info = featured.find("div.info");
		var sideLink = featured.find("ul.side li");
		sideLink.find("a").removeClass("selected");
		info.hide();

		sideLink.eq(id).find("a").addClass("selected");
		
		featured.find("a.image").attr({
			'href': sideLink.eq(id).find("a").attr('href'),
			'title': sideLink.eq(id).find("a").attr('title')}).html(sideLink.eq(id).find("a").html());
		
		featured.find("img").stop(true, true).fadeOut("fast", function() {
			featured.find("img").attr('src', info.eq(id).find("p.img").html()).fadeIn("slow");
		});
		
		info.eq(id).fadeIn("slow");
	}
	
	//
	// Game Rating
	//
	$(".rating").live('hover', function() {
		var rating = $(this).find(".fill").css('width');
		if($(this).find(".fill.rated").length == 0) {
			var width = 0;
			var rate = 0;
			
			$(this).mousemove(function(e) {
			if($(this).find(".fill.rated").length == 0) {
				var xPos = e.pageX - this.offsetLeft;
	
				if(xPos <= 10) {
					width = 10;
					rate = .5;
				}
				else if(xPos <= 20) {
					width = 20;
					rate = 1;
				}
				else if(xPos <= 31) {
					width = 31;
					rate = 1.5;
				}
				else if(xPos <= 41) {
					width = 41;
					rate = 2;
				}
				else if(xPos <= 52) {
					width = 52;
					rate = 2.5;
				}
				else if(xPos <= 62) {
					width = 62;
					rate = 3;
				}
				else if(xPos <= 73) {
					width = 73;
					rate = 3.5;
				}
				else if(xPos <= 83) {
					width = 83;
					rate = 4;
				}
				else if(xPos <= 94) {
					width = 94;
					rate = 4.5;
				}
				else {
					width = 104;
					rate = 5;
				}
				
				$(this).find(".fill").css({'background-color': '#e1d11c', 'width': width+'px'});
			}
			});
			
			$(this).click(function() {
				if($(this).find(".fill.rated").length == 0) {
				$.ajax({
					type: "GET",
					url: "/ganv2/includes/functions.php",
					data: "f=rating&id="+ $(".game").attr("id").substr(0, $(".game").attr("id").indexOf("-")) +"&rating="+rate+"&type="+ $(".game").attr("id").substr($(".game").attr("id").indexOf("-")+1),
					success: function(msg) {
						if(msg == "success") {
							$(this).find(".fill").attr("class", "fill rated");
							$(this).find(".fill").css('width', width+'px').fadeIn();
							alert(rate);
						}
						else {
							alert(msg);
						}
					}
				});
				}
			});
		}
	}, function() {
		if($(this).find(".fill.rated").length == 0) {
			$(this).find(".fill").css({'background-color': '#990000', 'width': rating});
		}
	})
});
