var hexDigits = new Array
        ("0","1","2","3","4","5","6","7","8","9","A","B","C","D","E","F"); 
	
	
	function colorToHex(rgb) {
	 rgb = rgb.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
	 return "#" + hex(rgb[1]) + hex(rgb[2]) + hex(rgb[3]);
	}
	
	function hex(x) {
	  return isNaN(x) ? "00" : hexDigits[(x - x % 16) / 16] + hexDigits[x % 16];
	 }
	
	function changeColors(point)
	{
		var i=1;
		for (i=0;i<=10;i++)
		{
			if (point >= i)
			{
				$('.'+i+' div').css('background-color', $('.'+i).attr('hcolor'));
				$('.'+i+' div').css('color', '#fff');
			}
			else
			{
				$('.'+i+' div').css('background-color', '#D4D6D3');
				$('.'+i+' div').css('color', '#000');
			}
		}
	}
	
    var vote = 0;

	function addList(id, type, mid)
   	{
   		$('#loader-'+mid).show();
   		if (type == 1)
   		{
   		$('#overlay').show();
   		$('#rtb').show();
	    $('.point').click(function () {
	    	var vote = $(this).attr('point');
	    	$('#rtb').hide();
   			$('#overlay').hide();
	    	callAddList(id, type, vote, mid)
	    });
	    $('#cancelrating').click(function () {
    		$('#rtb').hide();
   			$('#overlay').hide();
    		var vote = 0;
	    	callAddList(id, type, vote, mid);
    	}); 
		}
		else
		{
			callAddList(id, type, 0, mid);
		}
   		
   		
   	}
   	
   	function callAddList(id, type, vote, mid) {
   		$.get("http://sinebot.com/action/", { id: id, t: type, v: vote, c: "addlist" },
		   	function(data){
		     if (data == 1)
		     {
		     	if (type == 1)
		     	{
		     		temp = '<span id="infow-'+mid+'" class="info"> (izlediklerimde,</span> <span id="removew-'+mid+'" class="info"> <a href="javascript:void(0);" onclick="removeFromList('+mid+', 1, '+id+');">kaldır</a>)</span>';
						
			     	$('#feedactions-'+mid).html(temp);
	   				$('#loader-'+mid).hide();
		     	}
		     	else if (type == 2)
		     	{
		     		temp = '<span id="infow-'+mid+'" class="info"> (izleyeceklerimde,</span> <span id="removew-'+mid+'" class="info"> <a href="javascript:void(0);" onclick="removeFromList('+mid+', 2, '+id+');">kaldır</a> ya da</span> <span id="addw-'+mid+'" class="info"><a href="javascript:void(0);" onclick="addList('+id+', 1, '+mid+');">izlediklerime ekle</a>)</span>';
						
		     		$('#feedactions-'+mid).html(temp);
	   				$('#loader-'+mid).hide();
		     	}
		     }
		     else if(data == 2)
		     {
		     	//$('#approvespan-'+id).html('bir hata oluştu. tekrar deneyin.');
		     }
		   	});
   	}
   	
   	function removeFromList(id, type, mid)
   	{
   		$('#loader-'+id).show();
   		$.get("http://sinebot.com/action/", { id: id, t: type, c: "removefromlist" },
	   	function(data){
	     if (data == 1)
	     {
	     	temp = '<span id="addw-'+id+'" class="info">(<a href="javascript:void(0);" onclick="addList('+mid+', 1, '+id+');">izlediklerime ekle</a> ya da </span><span id="addww-'+id+'" class="info"><a href="javascript:void(0);" onclick="addList('+mid+', 2, '+id+');">izleyeceklerime ekle</a>)</span>';
	     	
	     	$('#feedactions-'+id).html(temp);
   			$('#loader-'+id).hide();
	     }
	     else if(data == 2)
	     {
	     	//$('#approvespan-'+id).html('bir hata oluştu. tekrar deneyin.');
	     }
	   	});
   	}
   	
   	function getTrailer(url, id)
   	{
   		$.get("http://sinebot.com/embed/", { url: url },
	   	function(data){
	     if (data)
	     {
	     	$('#tthumb-'+id).hide();
	     	$('#trailer-'+id).html(data);
   			$('#ctrailer-'+id).show();
	     }
	   	});
   	}
   	
   	function closeTrailer(id)
   	{
   		$('#trailer-'+id).html('');
   		$('#ctrailer-'+id).hide();
   		$('#tthumb-'+id).show();
   	}
   	
   	function follow(id, type)
   	{
   		$.get("http://sinebot.com/follow/", { id: id, type: type },
	   	function(data){
	     if (data == 1)
	     {
	     	if (type == 1)
	     	{
	     		temp = '<span><a href="javascript:void(0);" onclick="follow('+id+', 2);">Takibi bırak</a></span>';
	     		$('#follow-'+id).removeClass('follow');
	     		$('#follow-'+id).addClass('unfollow');
     		}
	     	else if (type == 2)
	     	{
	     		temp = '<span><a href="javascript:void(0);" onclick="follow('+id+', 1);">Takip et</a></span>';
	     		$('#follow-'+id).removeClass('unfollow');
	     		$('#follow-'+id).addClass('follow');
     		}
	     	
	     	$('#follow-'+id).html(temp);
	     }
	     else if(data == 2)
	     {
	     	if (type == 1)
	     	temp = '<span id="follow-'+id+'" class="info">zaten takip ediyorsun, <a href="javascript:void(0);" onclick="follow('+id+', 2);">Takibi bırak</a></span>';
	     	else if (type == 2)
	     	temp = '<span id="follow-'+id+'" class="info">zaten takip etmiyorsun, <a href="javascript:void(0);" onclick="follow('+id+', 1);">Takip et</a></span>';
	     	
	     	$('#follow-'+id).html(temp);
	     	//$('#approvespan-'+id).html('bir hata oluştu. tekrar deneyin.');
	     }
	   	});
   	}
   	
   	function more(id, type, username)
   	{
   		$('#loader-more').show();
   		$('#more').html('yükleniyor...');
   		$.get("http://sinebot.com/action/", { id: id, t: type, u: username, c: "more" },
	   	function(data){
	     if (data)
	     {
	     	if (type == 66 || type == 67 || type == 68)
	     	{
				$('#popularmovies').append(data);
	   			$('#loader-more').hide(); 
	     	}
	     	else
	     	{
				$('#results').append(data);
	   			$('#loader-more').hide(); 
   			}
	     }
	     else
	     {
	     	//$('#approvespan-'+id).html('bir hata oluştu. tekrar deneyin.');
	     }
	   	});
   	}
   	
   	function addFavs(id)
   	{
   		$.get("http://sinebot.com/action/", { id: id, c: "addfavs" },
	   	function(data){
	     if (data == 1)
	     {
	     	temp = '<a class="favactive" title="Beğenini kaldır" href="javascript:void(0);" onclick="removeFavs('+id+');"></a>';
					
		   	$('#fav-'+id).html(temp);
	     }
	     else if(data == 2)
	     {
	     	//$('#approvespan-'+id).html('bir hata oluştu. tekrar deneyin.');
	     }
	   	});
   	}
   	
   	function removeFavs(id)
   	{
   		$.get("http://sinebot.com/action/", { id: id, c: "removefavs" },
	   	function(data){
	     if (data == 1)
	     {
	     	temp = '<a class="favdeactive" title="Beğen" href="javascript:void(0);" onclick="addFavs('+id+');"></a>';
					
		   	$('#fav-'+id).html(temp);
	     }
	     else if(data == 2)
	     {
	     	//$('#approvespan-'+id).html('bir hata oluştu. tekrar deneyin.');
	     }
	   	});
   	}
   	
   	function checkUsername(name)
   	{
   		$.get("http://sinebot.com/action/", { id: name, c: "checkusername" },
	   	function(data){
	     return data;
	   	});
   	}
   	
   	function showAll(classname, thisid)
   	{
   		$('.'+classname).show();
   		$('#'+thisid).remove();
   	}
   	
   	function rate(id)
   	{
   		temp = 'Puan ver: <select name="rating" class="rating" id="r-'+id+'" onChange="rateMovie('+id+');"><option>1</option><option>2</option><option>3</option><option>4</option><option>5</option><option>6</option><option>7</option><option>8</option><option>9</option><option>10</option></select>';
								
			$('#rating-'+id).html(temp);
   	}
   	
   	function showNote(id)
   	{
   		if ($('#notes-'+id).css('display') == 'none')
   		{
   			$('#notes-'+id).css('display', 'block');
   		}
   		else
   		{
   			$('#notes-'+id).css('display', 'none');   			
   		}
   	}
   	
   	function editNote(id)
   	{
   		$.get("http://sinebot.com/action/", { id: id, c:"editnote" },
	   	function(data){
		   	$('#notes-'+id).html(data);
	   	});
   	}
   	
   	function saveNote(id)
   	{
   		$.post("http://sinebot.com/form/", { id: id, note: $('#notetext-'+id).val(), formname: "addnote" },
	   	function(data){
	     if (data != '')
	     {
	     	temp = "<p>"+data+"</p> <a href=\"javascript:void(0)\" onClick=\"editNote("+id+")\">düzenle</a>";
					
		   	$('#notes-'+id).html(temp);
	     }
	     else
	     {
	     	//$('#approvespan-'+id).html('bir hata oluştu. tekrar deneyin.');
	     }
	   	});
   	}
   	
   	function rateMovie(movieId)
   	{
   		$.get("http://sinebot.com/action/", { id: movieId, v: $('#r-'+movieId).val(),c:"ratemovie" },
	   	function(data){
	     if (data == 1)
	     {
	     	temp = 'Puanınız: '+$('#r-'+movieId).val()+'/10 <a href="javascript:rate('+movieId+');">değiştir</a>';
					
		   	$('#rating-'+movieId).html(temp);
	     }
	     else if(data == 2 || data == 3)
	     {
	     	$('#rating-'+movieId).html('');
	     }
	   	});
   	}
   	
   	function sendComment(mid, comment, cid)
   	{
   		$.post("http://sinebot.com/addcomment/", { formname: 'addcomment', mid: mid, comment: comment, cid: cid},
	   	function(data){
	     if (data == 1)
	     {
	     	refreshComments(mid);
	     	$('#addcomment').val('');
	     }
	     else if(data == 2 || data == 3)
	     {
	     }
	   	});
   	}
   	
   	function refreshComments(mid)
   	{
   		$.get("http://sinebot.com/_getcomments.php", { mid: mid},
	   	function(data){
	     if (data)
	     {
		   	$('#commentsformovie').html(data);
	     }
	   	});
   	}
   	
   	
   	function getMostWatched()
   	{
   		$.get("http://sinebot.com/action/", { c: 'mostwatched'},
	   	function(data){
	     if (data)
	     {
		   	$('#popularmovies').html(data);
		   	$('.popmenu').removeClass('popmenuactive');
   			$('#pop1').addClass('popmenuactive');
	     }
	   	});
   	}
   	
   	function getMostWillWatch()
   	{
   		$.get("http://sinebot.com/action/", { c: 'mostwillwatch'},
	   	function(data){
	     if (data)
	     {
		   	$('#popularmovies').html(data);
		   	$('.popmenu').removeClass('popmenuactive');
   			$('#pop2').addClass('popmenuactive');
	     }
	   	});
   	}
   	
   	function getMostCommented()
   	{
   		$.get("http://sinebot.com/action/", { c: 'mostcommented'},
	   	function(data){
	     if (data)
	     {
		   	$('#popularmovies').html(data);
		   	$('.popmenu').removeClass('popmenuactive');
   			$('#pop3').addClass('popmenuactive');
	     }
	   	});
   	}
   	
   	function getLastCommented()
   	{
   		$.get("http://sinebot.com/action/", { c: 'lastcommented'},
	   	function(data){
	     if (data)
	     {
		   	$('#popularmovies').html(data);
		   	$('.popmenu').removeClass('popmenuactive');
   			$('#pop4').addClass('popmenuactive');
	     }
	   	});
   	}
   	function getLastAdded()
   	{
   		$.get("http://sinebot.com/action/", { c: 'lastadded'},
	   	function(data){
	     if (data)
	     {
		   	$('#popularmovies').html(data);
		   	$('.popmenu').removeClass('popmenuactive');
   			$('#pop4').addClass('popmenuactive');
	     }
	   	});
   	}
   	
   	function editComment(id, mid)
   	{
   		aval = $('#addcomment-'+id).val();
   		sendComment(mid, aval, id);
   		
   	}
   	
   	function deleteComment(id, mid)
   	{
   		$.post("http://sinebot.com/addcomment/", { formname: 'removecomment', mid: mid, cid: id},
	   	function(data){
	     if (data == 1)
	     {
	     	refreshComments(mid);
	     	$('#addcomment').val('');
	     }
	     else if(data == 2 || data == 3)
	     {
	     }
	   	});
   		
   	}
   	
   	function getCommentEdit(id, mid)
   	{	
   		chtml = $('#comment-'+id).html();
   		ahtml = $('#addcomment-'+id).val();
   		temp = '<div class="addcommenttext"><textarea id="addcomment-'+id+'">'+chtml+'</textarea><input id="sendcomment" type="button" value="Gönder" onClick="javascript:editComment('+id+', '+mid+');"/></div>';
   		$('#commentbody-'+id).html(temp);
   	}
   	
   	function preparePopMenu()
   	{
   		$('.popmenu').mouseover(function() {
   			$(this).addClass('popmenuhover');
   		});
   		$('.popmenu').mouseout(function() {
   			$(this).removeClass('popmenuhover');
   		});
   	}
