var totWidth = 0;
var marginWidth = 20;
var activeClient = 1;
var activePos = 0;
var clients = 0;
var clientsIds = new Array();
var initPos = "";
var meses = new Array("", "Enero", "Febrero", "Marzo", "Abril", "Mayo", "Junio", "Julio", "Agosto", "Septiembre", "Octubre", "Noviembre", "Diciembre");

$(document).ready(function(){
	$('div#menu div.item').mouseover(function(){
		$(this).addClass('on');
	});
	$('div#menu div.item').mouseout(function(){
		$(this).removeClass('on');
	});

	$('div#menu div.item').click(function(){
		self.location.href=$(this).attr('href');
	});

	initPos = $('#content').offset();
	
	$('#content div.client').each(function(){
		clientsIds[clients] = $(this).attr('id');
		clients++;
	});

	$('div.thumb').livequery('mouseover', function(event) {
		$(this).addClass('over');
	});
	$('div.thumb').livequery('mouseout', function(event) {
		$(this).removeClass('over');
	});
	$('#content div.thumb').click(function(){
		var theid = $(this).attr('id').replace('thumb_', '');
		var thcliid = $(this).parent().parent().attr('id');
		var scrpt = self.location.href+'action/getImg/id/'+theid+'/cliid/'+thcliid+'/';
		$.getScript(scrpt);
	});

	
	$('#homecontent div.thumb').livequery('click', function(event) { 
		var theid = $(this).attr('id').replace('thumb_', '');
		var scrpt = self.location.href+'main/action/getFront/id/'+theid+'/';
		$.getScript(scrpt);
	});


	$('#homecontent a.getMonth').livequery('click', function(event) {
		var theurl = $(this).attr('href');
		$.getScript(theurl);
		return false;
	});

	if(clients > 0){
		initClients();
	}

	$('.goToClient').click(function(){
		var client = $(this).attr('id').replace('tdclient_', '');
		goto(client);
	});

	$('#arrow_left').click(function(){
		var prevClient = (activeClient*1)-1;
		$(this).hide();
		$('#arrow_left').show();
		goto(prevClient);
	});
	$('#arrow_right').click(function(){
		var nextClient = (activeClient*1)+1;
		$(this).hide();
		$('#arrow_right').show();
		goto(nextClient);
	});

	$(window).resize(function(){
		initClients();
	});

	$('#over_1').mouseover(function(){
		$('#client_'+(activeClient-1)).css('filter', 'alpha(opacity=100)');
		$('#client_'+(activeClient-1)).css('-moz-opacity', '1');
		$('#client_'+(activeClient-1)).css('opacity', '1');
	});
	$('#over_1').mouseout(function(){
		$('#client_'+(activeClient-1)).css('filter', 'alpha(opacity=30)');
		$('#client_'+(activeClient-1)).css('-moz-opacity', '0.3');
		$('#client_'+(activeClient-1)).css('opacity', '0.3');
	});
	$('#over_1').click(function(){
		$(this).hide();
		$('#arrow_left').trigger('click');
	});
	$('#over_2').mouseover(function(){
		$('#client_'+(activeClient+1)).css('filter', 'alpha(opacity=100)');
		$('#client_'+(activeClient+1)).css('-moz-opacity', '1');
		$('#client_'+(activeClient+1)).css('opacity', '1');
	});
	$('#over_2').mouseout(function(){
		$('#client_'+(activeClient+1)).css('filter', 'alpha(opacity=30)');
		$('#client_'+(activeClient+1)).css('-moz-opacity', '0.3');
		$('#client_'+(activeClient+1)).css('opacity', '0.3');
	});
	$('#over_2').click(function(){
		$(this).hide();
		$('#arrow_right').trigger('click');
	});

	$('#mprev').livequery('click', function(event) {
		page++;
		var newhtml = '<a href="#" id="mnext">siguientes...</a><br />';
		for(i=((page-1)*3), j=0; i<months.length && j<3; i++, j++){
			newhtml += '<a href="main/action/getMonths/month/'+months[i][1]+'/year/'+months[i][0]+'/" class="getMonth">'+meses[months[i][1]]+'</a><br />';
		}
		if(page < pages)
			newhtml += '<a href="#" id="mprev">anteriores...</a>';
		$('#themonths').html(newhtml);
		return false;
	});
	$('#mnext').livequery('click', function(event) {
		page--;
		var newhtml = '<a href="#" id="mnext">siguientes...</a><br />';
		if(page==1)
			newhtml = "";
		for(i=((page-1)*3), j=0; i<months.length && j<3; i++, j++){
			newhtml += '<a href="main/action/getMonths/month/'+months[i][1]+'/year/'+months[i][0]+'/" class="getMonth">'+meses[months[i][1]]+'</a><br />';
		}
		newhtml += '<a href="#" id="mprev">anteriores...</a>';
		$('#themonths').html(newhtml);
		return false;
	});
});

function initClients(){
	initPos = $('#container').offset();
	initPos.top += $('#clients').height()+20;
	totWidth = (785+marginWidth) * clients;
	$('#content').width(totWidth+'px');
	$('#content').css('position', 'absolute');
	$('#content').css('top', initPos.top+'px');
	$('#content').css('left', initPos.left+'px');
	$('#content div.client').css('margin-right', '20px');
	$('#content div.client').each(function(){
		if($(this).attr('id') != clientsIds[0]){
			$(this).css('filter', 'alpha(opacity=30)');
			$(this).css('-moz-opacity', '0.3');
			$(this).css('opacity', '0.3');
		}
	});
	$('#over_1').css('left', (initPos.left-$('#over_1').width()-marginWidth)+'px');
	$('#over_2').css('left', (initPos.left+$('#over_2').width()+marginWidth)+'px');
	$('#over_1').css('top', initPos.top+'px');
	$('#over_2').css('top', initPos.top+'px');

	activePos = initPos.left;
	
	activePos = activePos - ((785+marginWidth)*(activeClient-1));
	$('#content').css('left', activePos+'px');

	if($('#arrow_left').length > 0){
		var pos = $('#container').offset();
		$('#arrow_left').css('left', (pos.left));
		$('#arrow_right').css('left', (pos.left+780));
	}

}

function goto(cliId){
	if(cliId > activeClient){
		prevClient = activeClient;
		activeClient = cliId;
		difCli = activeClient - prevClient;
		activePos = activePos - ((785 + marginWidth)*difCli);

		$('#client_'+prevClient).animate({
			opacity: '0.3'
			}, 'fast', 'swing', function(){
				$('#content').animate({
					left: activePos+'px'
					}, 'fast', 'swing', function(){
						$('#client_'+activeClient).animate({
							opacity: '1.0'
							}, 'fast', 'swing', function(){
								if(activeClient == clients){
									$('#over_2').hide();
								} else{
									$('#over_2').show();
								}
								if(activeClient > 1){
									$('#over_1').show();
								} else{
									$('#over_1').hide();
								}
							}
						);
				});
		});
	} else if(cliId < activeClient){
		prevClient = activeClient;
		activeClient = cliId;
		difCli = prevClient - activeClient;
		activePos = activePos + ((785 + marginWidth)*difCli);

		$('#client_'+prevClient).animate({
			opacity: '0.3'
			}, 'fast', 'swing', function(){
				$('#content').animate({
					left: activePos+'px'
					}, 'fast', 'swing', function(){
						$('#client_'+activeClient).animate({
							opacity: '1.0'
							}, 'fast', 'swing', function(){
								if(activeClient == clients){
									$('#over_2').hide();
								} else{
									$('#over_2').show();
								}
								if(activeClient > 1){
									$('#over_1').show();
								} else{
									$('#over_1').hide();
								}
							}
						);
				});
		});
		if(activeClient == 1){
			$('#over_1').hide();
		}
		if(activeClient < clients){
			$('#over_2').show();
		}
	}
	
	if(activeClient < clients)
		$('#arrow_right').show();
	else
		$('#arrow_right').hide();
	if(activeClient > 1)
		$('#arrow_left').show();
	else
		$('#arrow_left').hide();


}