var offset = 900;

var nbPhotos = 0;
var currentPhoto = 1;
var nbToLoad = 10;
var fam = 0;
var month = null;

jQuery.preloadImages = function()
{
  for(var i = 0; i<arguments.length; i++)
  {
    jQuery("<img>").attr("src", arguments[i]);
  }
}

function handleNext(link)
{	
	if( currentPhoto == nbPhotos )
	{
		loadPhotos(link);
	}
	else
	{
		anim();
	}
}

function anim()
{
	$("#anim").animate({"left": "+=" + offset + "px"}, "slow");
	document.body.focus();
	
	currentPhoto ++;
	
	handleLinks();
	
	$('.votes').show();
	
}

function setPhoto(p)
{
	var parts = p.split("-");
	
	$('#photo' + currentPhoto).attr('src', root + 'demoiselle/'+parts[0]);
	
	var td = $('#img' + currentPhoto);
	
	td.find('.date').html(parts[1]);
	

}

function handleLinks()
{
	var tdi = $('#img' + currentPhoto);
	var tdr = $('#linkr' + currentPhoto);
	var tdl = $('#linkl' + currentPhoto);
	var linksr = tdr.find('.links');
	var linksl = tdl.find('.links');
	
	
	if( currentPhoto == 1 )
	{
		linksr.find('.previous').hide();
		linksr.find('.return').hide();
	}
	
	if( currentPhoto == fam )
	{
		linksl.find('.next').hide();
	}
	
}

function goStart()
{
	//$("#anim").animate({"left": "0px"}, "slow");
	
	var l = (nbPhotos-1) * offset;
	
	$("#anim").animate({"left": "-"+l+"px"}, "slow");

	document.body.focus();
	
	currentPhoto = 1;
}

function goBack()
{
	$("#anim").animate({"left": "-=" + offset + "px"}, "slow");
	document.body.focus();
	
	currentPhoto--;
}

function cont()
{
	var s = "mailto:daily.demoiselle@gmail.com";
	
	self.location.href = String.fromCharCode(109,97,105,108,116,111,58,100,97,105,108,121,46,100,101,109,111,105,115,101,108,108,101,64,103,109,97,105,108,46,99,111,109);
	
}

$(function()
{
	$('#previewContainer').find('.image').each(function()
	{
		//$(this).click(function() {showText($(this));});
	});
	
	$('#previewContainer').find('.close').each(function()
	{
		var i = $(this).parents('td').find('.image');
		$(this).click(function() { hideText(i); } );
	
	});
	
	$('#previewContainer').find('.details').each(function()
	{
		var i = $(this).parents('td').find('.image');
		$(this).click(function() {showText(i);});
	
	});
	
	handleLinks();
	
	
	applyRating();
	
	$('#anim').css('left', '-' + ((nbPhotos-1) * offset) + 'px');
	
	$('.votes').show();
	
});

function applyRating()
{
	$('input[type=radio].star').rating({callback: function(note, h, input)
	{
		var code = input.parent().attr('name');
		
		input.rating('readOnly',true, true);

		$.post(root+"a/vote.php", { p: code, v: note },
	    function(data)
	    {
	    	var parts = data.split(";");
	    	
	    	if( parts.length  == 4)
	    	{
	    		var score = parts[0];
		    	var nb    = parts[1];
		    	var rank  = parts[2];
		    	var scoreexact  = parts[3];
		    	input.rating('readOnly', false, false);
		    	input.rating('select',score);	
		    	input.rating('readOnly', true, true);
		    	
		    	var pluriel = parseInt(nb, 10) > 1 ? 's' : '';
		    	var s = nb + " vote"+ pluriel + '&nbsp;&nbsp;';
		    	
		    	input.parents("td").find('.nb').html(s);
		    	input.parents("td").find('.ranking').html(rank); 
		    	input.parents("td").find('.score').html(scoreexact);  	
	    	}
	    });
	
		}
	});

					
	
	$('input[type=radio].star').each(function()
	{
		
		
		if( $(this).parent().hasClass('yes'))
		{
			$(this).rating('readOnly',true, true);
		}		
	});
	

	
	

}


function substr_count( haystack, needle, offset, length )
{
 
    var pos = 0, cnt = 0;
 
    if(isNaN(offset)) offset = 0;
    if(isNaN(length)) length = 0;
    offset--;
 
    while( (offset = haystack.indexOf(needle, offset+1)) != -1 ){
        if(length > 0 && (offset+needle.length) > length){
            return false;
        } else{
            cnt++;
        }
    }
 
    return cnt;
}


function loadPhotos(link)
{
	link = $(link);

	var father = link.parent();
	
	var loader = father.find('.loader');
	
	link.hide();
	loader.show();
	
	$.get(root+"photos.php", { start: nbPhotos, nb: nbToLoad, month: month },
  	function(data)
  	{
    	$('#thetr').prepend(data);
    	
    	
    	var nb = substr_count(data, 'handleNext(this);');
		
    	var left = nb * offset;
    	
    	var c = parseInt($("#anim").css('left'));
    	
    	$("#anim").css('left', (c-left) + 'px');
    	
		nbPhotos += nb;
    	
    	handleLinks();
    	applyRating();
    	
    	
    	anim();
    	
    	loader.hide();
    	link.show();
    	//link.unbind('click').click(anim);
    	$('a[rel=lightbox]').lightBox({fixedNavigation:false});
    	
    	
    	
  	});
}

function showText(i)
{
	var div = i.parents('td').find('.description');
	div.parents('.textContainer').css('margin-top', '0px');
	div.animate({"width": "+=600px"}, "slow");
	
	var details = i.parents('td').find('.details');
	
	i.unbind('click').click(function() {hideText(i) });
	details.unbind('click').click(function() {hideText(i) });
}

function hideText(i)
{
	var div = i.parents('td').find('.description');	
	div.animate({"width": "0px"}, "slow", "linear", function() { div.parents('.textContainer').css('margin-top', '-1000px') } );

	var details = i.parents('td').find('.details');

	i.unbind('click').click(function() {showText(i) });
	details.unbind('click').click(function() {showText(i) });
}