$(document).ready(function(){
	var last_img_hover = false;
	var img_width = 140;
	var hover_origs = [];
	var sprite_timer;

	$(".rotate img").mouseenter(function(){
		var r = this;
		var this_img = $(this);//.find("img");
		last_img_hover = this_img.attr("id");
		var c_img = this_img.attr("src").split("?")[0];
		var c_full_suffix = c_img.substring(c_img.length-11);
		var c_suffix = c_full_suffix.split("-")[1];
		var c_id = this_img.attr("id").substr(4);
		// console.log("suffix: " + c_suffix);
		var sprite_img = c_img.substring(0, c_img.length - c_suffix.length) + "sprite.jpg";
		// console.log("sprite img: " + sprite_img);
		if (sprite_timer){
			clearTimeout(sprite_timer);
			sprite_timer = false;
		}
		if (c_suffix == "sprite.jpg" || typeof(hover_origs[last_img_hover]) != "undefined"){
			this_img.attr("src", sprite_img);
			rotate_thumb(this_img.attr("id"));
		}
		else {
			hover_origs[last_img_hover] = c_img;
			// var sprite_img = "http://www.newsfilter.org/web_images/videos/thumbs/" + c_id + "/" + c_id + "-sprite.jpg";
			// console.log("Going to fetch: " + sprite_img);
			var img = new Image();
			$(img).attr('src', sprite_img).load(function(){
				// console.log("FETCHED: " + sprite_img);
				this_img.attr("src", sprite_img).css("width", "auto");
				rotate_thumb(this_img.attr("id"));
			});
		}
	});
	$(".rotate").mouseout(function(){
		last_img_hover = false;
		var this_img = $(this).find("img");
		if (last_img_hover == this_img.attr("id"))
			last_img_hover = false;
		//this_img.attr("src", hover_origs[this_img.attr("id")]).css({"margin-left":"0", "min-width":"140px"});
	});
	
	rotate_thumb = function(to_rotate){
		// console.log("ROTATE THUMB..." + to_rotate);
		if (to_rotate != last_img_hover)
			return false;
		var sprite = $("#" + to_rotate);
		var sprite_width = sprite.width();
		sprite_width = 1400;
		var sprite_left = parseInt(sprite.css("margin-left"));
		if ((-1)*(sprite_left) + img_width >= sprite_width){
			sprite.css({"margin-left":"0", "width":"auto"});
		}
		else {
			sprite.css({"margin-left":(sprite_left-img_width)});
		}
			
		sprite_timer = window.setTimeout("rotate_thumb('" + to_rotate + "')", 800);
	};
	
	
	var pre_stars = Array();
	$(".empty_stars").mousemove(function(e){
		if ($(this).hasClass("done"))
			return;
		if (typeof(pre_stars[this.id]) == "undefined")
			pre_stars[this.id] = $(this).children().width();
		var mx = e.pageX -  $(this).offset().left;
		var stars_width = Math.ceil(mx/18)*18;
		// console.log("mx: " + mx + " | stars_width: " + stars_width);
		// console.log("px: " + e.pageX + " | l offset: " + this.offsetLeft);
		$(this).children(".full_stars").width(stars_width + "px").addClass("full_stars_hover");
	});
	
	$(".empty_stars").mouseout(function(e){
		if ($(this).hasClass("done"))
			return;
		var stars_width = pre_stars[this.id];
		$(this).children(".full_stars").width(stars_width + "px").removeClass("full_stars_hover");
	});

	$(".full_stars").click(function(e){
		if ($(".full_stars").hasClass("done"))
			return;
		var mx = e.pageX - $(".full_stars").offset().left;
		var rating = Math.ceil(mx/18);
		post_rating();
	});

	$(".video_btns .fav a").click(function(){
		add_to_favorites();
		return false;	
	});


	$("#comment_form").submit(function(){
		submit_comment();
		return false;
	});

});

var rating;



function submit_comment(){
	var form = $("#comment_form");
	 $.ajax(window.location.pathname, {type: "POST", dataType: "json", data: {comment: $("#comment").val(), ajax: true, PHPSESSID:ajax_sess}, success: function(response){
		if (response.userid == 0){
			load_notloggedin('submit_comment');
			return false;
		}
		if (response.status != "ok"){
			alert("Error with your comment: " + response.msg);
			return false;
		}
		$("#comment_form").slideUp("slow");
		$(".comment_wrap .add").after(response.comment);
	}
	});
}



function add_to_favorites(){
	$.post(window.location.pathname, {action: 'add_to_favorites', PHPSESSID: ajax_sess, ajax: 'true'}, function(response){
		 if (response.userid == 0){
			load_notloggedin("add_to_favorites");
			return false;
		}
		//console.log(response);
		if (typeof(response["msg"]) !== "undefined")
			alert(response.msg);
		else
			alert(response);
	}, 'json');
}

function post_rating(){
	if ($(".full_stars").hasClass("done"))
		return;
	$(".full_stars").parent().addClass("done");
	//var mx = e.pageX - $(".full_stars").offset().left;
	//var rating = Math.ceil(mx/18);
	var rating_id = $(".full_stars").parent().attr("id");
	$.ajax("/rate.php", {
		type: "POST",
		dataType: "json",
		data: {ajax:true, PHPSESSID:ajax_sess, rating:rating, rating_id:rating_id},
		success: function(response){
			if (response.userid == 0){
				load_notloggedin("post_rating");
				return false;
			}
			else if (typeof(response.msg) !== "undefined" && response.msg != "Ok")
				alert(response.msg);
		}
	});
}


function load_notloggedin(a){
	var iframe_url = $("#notloggedin_iframe_anchor").attr("href")+"?a=" + a;
	//console.log("url: " + iframe_url);
	$.fancybox($("#notloggedin_iframe_anchor").attr("href")+"?a=" + a, {
		"transitionIn": "elastic",
		"transitionOut": "elastic",
		"speedIn"       :       600,
		"speedOut"      :       200,
		"type"          :       "iframe",
		"width"         :       420,
		"height"        :       700,
		"onComplete": function(){
			//console.log("complete");
		}
	});
	//alert("You must be logged in to use this feature!");
	
}

