$(document).ready(function(){

	/* Save/Remove Favourite */
	$(".btn_stf").click(function(){
		var rel = $(this).attr("rel").split('|');
		var type = rel[0];
		var type_id = rel[1];
		$.getJSON("/inc/ajax.php?action=saveFave&id="+type_id+"&type="+type, function(json){
			if(json.success==1){
				$("#show_stf").hide();
				$("#save_date").text(json.save_date);
				$("#show_rff").fadeIn("slow");
			}else{
				alert(json.msg);
			}
		});
		return false;
	});
	$(".btn_rff").click(function(){
		var rel = $(this).attr("rel").split('|');
		var type = rel[0];
		var type_id = rel[1];
		$.getJSON("/inc/ajax.php?action=removeFave&id="+type_id+"&type="+type, function(json){
			if(json.success==1){
				$("#show_stf").fadeIn("slow");
				$("#show_rff").hide();
			}else{
				alert(json.msg);
			}
		});
		return false;
	});




	/* Delete/Edit comments */
	$("[@rel='comment_action']").click(function(){
		var id = $(this).attr("id");
		var type = $(this).attr("type");
		$(this).parent().parent().parent().append("<p class='highlight' style='display:none;'><strong>Are you sure you want to delete this comment?</strong> | <a href='' class='delcom' name='"+id+"'>Yes</a> | <a href='' class='nodel'>No</a></p>");
		$("p.highlight").fadeIn(100);
		$(".delcom").click(function(){
			dc(this.name,type);
			return false;
		});
		$(".nodel").click(function(){
			$("p.highlight").fadeOut(500,function(){
				$(this).remove();
			});
			return false;
		});
		return false;
	});
	var dc = function(id,type){
		id = id.replace("dc_","");
		var uid = $("#c_"+id).attr("rel");
		$.getJSON("/inc/ajax.php?action=deleteComment&id="+id+"&uid="+uid+"&type="+type, function(json){
			if(json.success==1){
				$("#c_"+id).slideUp(750,function(){ $(this).remove(); });
			}else{
				alert("Could not delete comment: "+json.message);
			}
		});

	}


});


	function updateVote(type,divID,rating){
		$.getJSON("/inc/ajax.php?action=vote&carID="+divID+"&type="+type+"&rating="+rating, function(json){
			if(json.success=="1"){
				$('#rate'+json.divID).hide();
				$('#ratingHide').css("width",json.newWidth);
				$('#ulHide').show();
				$('#rateThanks').css('display','block');
			}
		});
		//new ajax('/inc/ajax/favourites.php', {postBody: 'action=vote&carID='+divID+'&type='+type+'&rating='+rating, onComplete: updateVote_cb});
	}

	/* News Ticker */
	var rotate_speed = 7500;
	var animate_speed = 1000;
	var current_headline = 0;
	var old_headline = 0;
	
	var headline_count = $("div.headline").size();
	$("div.headline:eq(" + current_headline + ")").css('top', '5px');

	var headline_interval = setInterval(headline_rotate,rotate_speed);
	$('#network').hover(function() {
		clearInterval(headline_interval);
	}, function() {
		headline_interval = setInterval(headline_rotate,rotate_speed);
		//headline_rotate();
	});

	function headline_rotate() {
		current_headline = (old_headline + 1) % headline_count;
		$("div.headline:eq(" + old_headline + ")").animate({top: -205}, animate_speed, function() {
			$(this).css('top', '110px');
		});
		$("div.headline:eq(" + current_headline + ")").animate({top: 5}, animate_speed);
		old_headline = current_headline;
	}



	/* Top Download toggle */
	$("#topdl-boxes a").click(function(){
		var selected = $(this).attr("name");
		$("#tdl_week,#tdl_month").hide();
		$("#tdl_"+selected).show();
		$(this).parent().children("a").removeClass("current");
		$(this).addClass("current");
		return false;
	});

	/* Hover for list table rows */
	$("table.list_table:not(.no_hover)").each(function(){
		$(this).find("tr:gt(1)").hover(function(){
			$(this).children("td").css("background-color","#EFEFEF").addClass("handHover");
		},function(){
			$(this).children("td").css("background-color","#FFFFFF").removeClass("handHover");
		});
	});
	$("table.list_table tr:gt(1)").click(function(){
		window.location = $(this).find("a").eq(0).attr("href");
	});

	$("a.infolink").click(function(){
		$(".infolink").removeClass("active");
		$("div[@rel='info']").hide();
		$(this).addClass("active");
		$("#file_"+this.name).show();
		return false;
	});
	$("table.perf_stats tr:nth-child(odd)").addClass("odd");

	$("a.full_image").click(function(){
		var attrs = $(this).attr("rel").split("|");
		var width = parseInt(attrs[1])+20;
		var height = parseInt(attrs[2])+20;
		window.open(attrs[0], "fullImage", "status=1,width="+width+",height="+height+",resizable=1,scrollbars=1,top=5,left=5")
	});