var minvote = 0;
var maxvote = 10;
var maxbarwidth = 146;
var aktpage = 1;

$q(document).ready(function(){
	initsubbox();
	//initcomments();
	initusermenu();
	//initrating();
});

function initsubbox(){
	$q('.subBoxIcon').click(function(){
		if ($q(this).attr('class').indexOf('opened', 0) >= 0){
			$q(this).parent().parent().find('.subBoxContent').slideUp('fast');
			$q(this).removeClass('opened');
			$q(this).addClass('closed');
		}else{
			$q(this).parent().parent().find('.subBoxContent').slideDown('fast');
			$q(this).removeClass('closed');
			$q(this).addClass('opened');
		}
	});
}

function initrating(){
	$q('.tRatingBar .btnAudio').click(function(){
		var index = $q('.tRatingBar .btnAudio').index(this);
		voteA(index);
	});
	$q('.tRatingBar .btnAudio').mouseover(function(){
		var index = $q('.tRatingBar .btnAudio').index(this);
		setBarA(index);
	});
	$q('.tRatingBar .btnAudio').mouseout(function(){
		if (currentRatingA < 0){
			$q(this).parent().parent().children('.tRatingArrow').hide();
		}else{
			setBarA(currentRatingA);
		}
	});
	$q('.tRatingBar .btnVideo').click(function(){
		var index = $q('.tRatingBar .btnVideo').index(this);
		voteV(index);
	});
	$q('.tRatingBar .btnVideo').mouseover(function(){
		var index = $q('.tRatingBar .btnVideo').index(this);
		setBarV(index);
	});
	$q('.tRatingBar .btnVideo').mouseout(function(){
		if (currentRatingV < 0){
			$q(this).parent().parent().children('.tRatingArrow').hide();
		}else{
			setBarV(currentRatingV);
		}
	});
}

function setBarV(newvote){
	if (currentRatingV < 0){ $q('.vVideo .tRatingArrow').show(); }
	$q('.vVideo .tRatingArrow').css('margin-left',13 * newvote);
} 

function setBarA(newvote){
	if (currentRatingA < 0){ $q('.vAudio .tRatingArrow').show(); }
	$q('.vAudio .tRatingArrow').css('margin-left',13 * newvote);
} 

function voteV(newvote){
	$q('.vVideo .tRatingBar').hide();
	$q('.vVideo .tRatingLoader').fadeIn('fast');
	$q.post(pageroot + "/ajax/vote.php",
			{ 
				value: newvote,
				torrent: torrentid,
				type: "v"
			},
			function(ret){
				var state = $q('msg',ret).attr('state');
				var msg = unescape($q('msg',ret).text());
				$q('.vVideo .tVoteLoader').hide();
				if (state == "1"){
					var newvote = $q('msg',ret).attr('newvote');
					currentRatingV = newvote;
					$q('.vVideo .tRatingArrow').show();
					setBarV(newvote);
					alert(msg);
				}else{
					alert(msg);
				}
				$q('.vVideo .tRatingLoader').hide();
				$q('.vVideo .tRatingBar').fadeIn('fast');
			}
	);
}
function voteA(newvote){
	$q('.vAudio .tRatingBar').hide();
	$q('.vAudio .tRatingLoader').fadeIn('fast');
	$q.post(pageroot + "/ajax/vote.php",
			{ 
				value: newvote,
				torrent: torrentid,
				type: "a"
			},
			function(ret){
				var state = $q('msg',ret).attr('state');
				var msg = unescape($q('msg',ret).text());
				$q('.vAudio .tVoteLoader').hide();
				if (state == "1"){
					var newvote = $q('msg',ret).attr('newvote');
					currentRatingA = newvote;
					$q('.vAudio .tRatingArrow').show();
					setBarA(newvote);
					alert(msg);
				}else{
					alert(msg);
				}
				$q('.vAudio .tRatingLoader').hide();
				$q('.vAudio .tRatingBar').fadeIn('fast');
			}
	);
}
function initcomments(){
	loadcomments(torrentid,aktpage);
	$q('#btnSaveComment').click(function(){
		$q(this).hide();
		$q('#lblCommenterror').hide();
		$q('#lblCommentmsg').hide();
		var text = $q('#txtComment').val();
		$q.post(pageroot + "/ajax/writecomment.php",
			{ 
				comment: text,
				torrentid: torrentid
			},
			function(ret){
				var state = $q('msg',ret).attr('state');
				var msg = unescape($q('msg',ret).text());
				$q('.vVideo .tVoteLoader').hide();
				if (state == "1"){
					$q('#lblCommentmsg').text(msg);
					$q('#lblCommentmsg').fadeIn('fast');
					$q('#txtComment').val('');
					aktpage = 1;
					loadcomments(torrentid,aktpage);
				}else{
					$q('#lblCommenterror').text(msg);
					$q('#lblCommenterror').fadeIn('fast');
				}
				$q('#btnSaveComment').show();
			}
		);
	});
}
function loadcomments(torrentid,page){
	var commenturl = pageroot + "/comments/" + torrentid + "/" + page + "&" + Math.random();
	$q.get(commenturl,null,
			function(data){
				$q('#commentsbox').html(data);
			}
		);
}
function gotoPage(newpage){
	aktpage = newpage;
	loadcomments(torrentid,newpage);
}

function initusermenu(){
	$q('#btnUsermenu').click(function(){
		$q('#boxUsermenu').slideDown('fast');
		$q.post(pageroot + "/ajax/detailsusermenu.php", 
			{ 
				torrentid: torrentid
			},
			function(data){
				$q('#boxUsermenu .boxContent').html(data);
			}
		);
	});
}