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(){
		$('#dlink').hide();
		var theid = $(this).attr('id').replace('thumb_', '');
		var thcliid = $(this).parent().parent().parent().parent().parent().attr('id');
		activeImg[$('#'+thcliid).attr('attr')] = theid;
		$('#'+thcliid+' div.imgbig').attr('attr', theid);
		var thehref = self.location.href.replace(urlstrip, '');
		var scrpt = thehref+'action/getImg/id/'+theid+'/cliid/'+thcliid+'/';
		$.getScript(scrpt);
	});

	
	$('#homecontent div.thumb').livequery('click', function(event) { 
		$('#dlink').hide();
		var theid = $(this).attr('id').replace('thumb_', '');
		var thehref = self.location.href.replace(urlstrip, '');
		var scrpt = thehref+'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(){
		$('#dlink').hide();
		var client = $(this).attr('id').replace('tdclient_', '');
		goto(client);
	});

	$('#arrow_left').click(function(){
		$('#dlink').hide();
		var prevClient = (activeClient*1)-1;
		$(this).hide();
		$('#arrow_left').show();
		goto(prevClient);
	});
	$('#arrow_right').click(function(){
		$('#dlink').hide();
		var nextClient = (activeClient*1)+1;
		$(this).hide();
		$('#arrow_right').show();
		goto(nextClient);
	});

	$(window).resize(function(){
		$('#dlink').hide();
		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(){
		$('#dlink').hide();
		$(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(){
		$('#dlink').hide();
		$(this).hide();
		$('#arrow_right').trigger('click');
	});

	$('#mprev').livequery('click', function(event) {
		$('#dlink').hide();
		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) {
		$('#dlink').hide();
		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;
	});

	$('img.mediumimg').livequery('click', function(event) {
		$('#fullimg').html('&nbsp;');
		$('#fullview').fadeIn();
		var thehref = self.location.href.replace(urlstrip, '');
		var scrpt = thehref+'action/getImgDetail/id/'+$(this).parent().attr('attr')+'/';
		$.getScript(scrpt);
/*
		$('#fullview').fadeIn();
		$("div#fullimg").html('<img src="'+$(this).attr('attr')+'" alt="" border="0" />');
		checkIfImgIsLoaded($(this).parent().attr('attr'));
*/
	});

	$('#fullview').click(function(){
		$('#imgdetail').fadeOut();
		$(this).fadeOut();
	});

	$('img.dlink').livequery('click', function(event) {
		var thehref = self.location.href.replace(urlstrip, '');
		var scrpt = thehref+'action/getDLink/id/'+activeImg[$(this).attr('attr')]+'/';
		$.getScript(scrpt);
	});
											   
	$('#dlink img').livequery('click', function(event) {
		$('#dlink').fadeOut();
	});
});

function checkIfImgIsLoaded(imgload){
	var jImg = $("div#fullimg img");
	if(jImg[ 0 ].complete)
		setImgSize(imgload);
	else
		t=setTimeout("checkIfImgIsLoaded(imgload)",500);
}
function setImgSize(imgload){
	var boxPadding = 10;
	var winwidth = $(window).width()-(boxPadding*4+2);
	var winheight = $(window).height()-(boxPadding*4+2);
	var imgwidth = $('div#fullimg img').width();
	var imgheight = $('div#fullimg img').height();
	if(imgwidth>winwidth){
		perc = winwidth * 100 / imgwidth;
		imgheight = perc * imgheight / 100;
		imgwidth = winwidth;
	}
	if(imgheight>winheight){
		perc = winheight * 100 / imgheight;
		imgwidth = perc * imgwidth / 100;
		imgheight = winheight;
	}
	$('div#fullimg img').css('width', imgwidth+"px");
	$('div#fullimg img').css('heigth', imgheight+"px");
	var newpositionTop = Math.ceil(($(document).height() / 2)) - Math.ceil((imgheight+4)/2) - boxPadding;
	$('#fullimg').animate({
			width: (imgwidth)+'px',
			height: (imgheight)+'px',
			marginTop: (newpositionTop)+'px'
		}, 500, 'swing', function(){
			$("div#fullimg img").fadeIn();
			var thehref = self.location.href.replace(urlstrip, '');
			var scrpt = thehref+'action/getImgDetail/id/'+imgload+'/';
			var winpos = $('#fullimg').offset();
			$('#imgdetail').css('width', (imgwidth+1)+'px');
			$('#imgdetail').css('left', (winpos.left+1)+'px');
			$('#imgdetail').css('top', (winpos.top + $('#fullimg').height() - $('#imgdetail').height() - 30)+'px');
			$.getScript(scrpt);
		});

}

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();


}