var voteflag = null;

function updatePreview( element )
{
	var source = element.src.replace('/thumbnails/','/');
	var full = element.src.replace('/thumbnails/','/full/');
	
	document.getElementById('preview').src = source;
	
	$('a.prime').attr('href',full);
	
	/* Reinitialize gallery */
	
	var images = $('a.simple').map(function () { 
	                        return $(this).attr('id');
	                      }).get();
	                      
	images = images.slice(2,images.length);
}
function updateLargePreview( element) 
{
	var source = element.src.replace('/normal/thumbnails/','/330px/');
	
	document.getElementById('largePreview').src = source;
}
function toggleUpload()
{
	$('#uploadavatar').toggle();
}
function login()
{
	document.forms.login.submit();
}

function vote(element,id)
{
	var v = element.title.substring(element.title.indexOf('-')+1);
	
	/* Disable further clicks, not sexy way to do it but... who cares :) */
	if (voteflag === null)
	{
		voteflag = 1;
	}
	else 
	{
		return false;
	}
	
	/* Load series with AJAX */
	$.ajax(
	{
	   type: "GET",
	   url: 'http://' + document.domain + '/server/vote.php',
	   data: 'id=' + id + '&vote=' + v,
	   dataType: "text",
	   success: function(response)
	   {
	   		var rank = response.split(/\:/);
	   		
	   		var stars = rank[2];
	   		var fullstars = Math.floor(stars);
	   		
	   		
	   		$('img[@title]').each(function()
	   		{
	   			var t = $(this).attr('title');
	   			
	   			if (t.indexOf('rank') !== -1)
	   			{
	   				$(this).fadeTo(300,0.5);
	   			}
	   		});
	   			   		
	   		for (i=1;i<=fullstars;i++)
	   		{
	   			$('img[@title="rank-' + i + '"]').attr('src','http://testdrive.bg/sm_templates/images/star.gif');
	   		}
	   		/* Append half star */
	   		if (0 !== stars - fullstars)
	   		{
	   			/* We have to append */
	   			
	   			$('img[@title="rank-' + i + '"]').attr('src','http://testdrive.bg/sm_templates/images/star_half.gif');
	   			i++;
	   			
	   		}
	   		
	   		for(c=i;c<=5;c++)
	   		{
	   			$('img[@title="rank-' + c + '"]').attr('src','http://testdrive.bg/sm_templates/images/star_empty.gif');
	   		}
	   		
	   		$('img[@title]').each(function()
	   		{
	   			var t = $(this).attr('title');
	   			
	   			if (t.indexOf('rank') !== -1)
	   			{
	   				$(this).fadeTo(300,1);
	   			}
	   		});
	   			
	   }
	 });
}

function initVoting()
{
	var initialState = new Array();
	
	$('div.voting').mouseout(function(event)
	{
		if (event.target.tagName.toUpperCase().indexOf('DIV') != -1)
		{
			restoreInitialVotingImages(initialState);
		}
	});
			
	/* Locate initial state of images */
	$('img[@title]').each(function()
	{
		var t = $(this).attr('title');
		
		if (t.indexOf('rank') != -1)
		{
			initialState.push($(this).attr('src'));
		}
	});
	
	/* Bind mouseover and mouseout events */
	$('img[@title]').each(function()
	{
		var t = $(this).attr('title');
		
		if (t.indexOf('rank') != -1)
		{
			$(this).mouseover(function()
			{
				var data = t.split('-');
				
				disableVotingImages();
				
				for (i=1;i<=data[1];i++)
				{
					$('img[@title="rank-' + i + '"]').attr('src','http://testdrive.bg/sm_templates/images/star.gif');
				}
			});
		}
	});
}
function disableVotingImages()
{
	for	(i=1;i<=5;i++)
	{
		$('img[@title="rank-' + i + '"]').attr('src','http://testdrive.bg/sm_templates/images/star_empty.gif');
	}
}
function restoreInitialVotingImages( states )
{
	for(var i in states)
	{
		var _num = parseInt(i) + 1;
		
		$('img[@title="rank-' + _num + '"]').attr('src',states[i]);
	}
}

function addInputField(type)
{
	var inputs = $('td[@id="' + type + '"]').children('input');
	
	var new_id = inputs.length + 1;
	
	/* Create new input and append it to the table cell */
	$('<br />').appendTo('#' + type);
	$('<input type="text" />').attr('name',type + '_' + new_id).addClass('basic').css({ margin:1, width:170 }).appendTo('#' + type);
	
}

/* Analysis font size */
var font_min=10;
var font_max=16;

function zoomin() 
{
	var fontSize = parseInt($('#articleText').css('fontSize'));
	
	var newFont = ++fontSize;
	
	if (font_max >= newFont) $('#articleText').css('fontSize',newFont);
}
function zoomout() 
{
   var fontSize = parseInt($('#articleText').css('fontSize'));
	
	var newFont = --fontSize;
	
	if (font_min <= newFont) $('#articleText').css('fontSize',newFont);
}
