// JavaScript Document
var aInverno2011 = 	Array([0,"retrato"],
						[1,"retrato"],
						[2, "paisagem"],
						[3, "paisagem"],
						[4, "paisagem"],
						[5, "retrato"],
						[6, "paisagem"],
						[7, "retrato"],
						[8, "paisagem"],
						[9, "retrato"],
						[10, "paisagem"]);

var aVerao2011 = Array(	[0, "retrato"],
						[1, "retrato"],
						[2, "paisagem"],
						[3, "retrato"],
						[4, "retrato"],
						[5, "paisagem"],
						[6, "retrato"],
						[7, "retrato"]);

var aInverno2010 = 	Array(	[0, "retrato"],
							[1, "retrato"],
							[2, "paisagem"],
							[3, "paisagem"],
							[4, "retrato"],
							[5, "paisagem"],
							[6, "paisagem"],
							[7, "paisagem"]);


var fotoAtual = 1;
var totalFotosInverno2011 = 11;
var totalFotosVerao2011 = 8;
var totalFotosInverno2010 = 8;

var currCamp = -1;
var areasAnteriores = ["inverno2011", "verao2011", "inverno2010"];
var areasAnterioresn = [];
areasAnterioresn["inverno2011"] = 0;
areasAnterioresn["verao2011"] = 1;
areasAnterioresn["inverno2010"] = 2;
var totalimagesC = 0;
var AnterioresInit = false;

function initanteriores(){
	
	//console.log('initanteriores');
	
	var imgAtual = ["", 0, "paisagem", 400];
	
	$('#twittershare').click(function() {
		
		url = "http://www.vida-bela.com/" + "#" + $.address.value();
		//url = document.location.href.substr(0, document.location.href.indexOf('#')).toString() + "#" + $.address.value();
		url = encodeURIComponent(url);
		abreLinkPopUp('http://twitter.com/share?url=' + url + '&via=vidabelawear&text=Vida Bela',600,360);
		   
	});
	
	$('#facebookshare').click(function() {
		
		url = "http://www.vida-bela.com/" + "#" + $.address.value();
		//url = document.location.href.substr(0, document.location.href.indexOf('#')).toString() + "#" + $.address.value();
		url = encodeURIComponent(url);
		abreLinkPopUp('http://www.facebook.com/sharer.php?u=' + url + '&t=Vida Bela',500,360);
		   
    });

	$('.anteriores_rede').hover(
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": .5}, timeOver);
			
		},
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": 1}, timeOver);
			
		}
	
	);
	
	$('.anteriores_seta_anterior, .anteriores_seta_proxima').hover(
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": .8}, timeOver);
			
		},
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": 1}, timeOver);
			
		}
	
	);
	
	$('.anteriores_menuItem').hover(
	
		function() {
			
			if(currCamp!=$(this).attr("name")){
				
				$(this).children('.anteriores_menuItemseta').stop(true, true).animate({"left": "3"}, timeOver);
				$(this).stop(true, true).animate({"color": "#e9f1cb"}, timeOver);
				
			}
			
		},
	
		function() {
			
			if(currCamp!=$(this).attr("name")){
				
				$(this).children('.anteriores_menuItemseta').stop(true, true).animate({"left": "0"}, timeOver);
				$(this).stop(true, true).animate({"color": "#F5F0F0"}, timeOver);
				
			}
			
		}
	
	);
	
	$('.botao_zoom_anteriores').hover(
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": .8}, timeOver);
			
		},
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": 1}, timeOver);
			
		}
	
	);
	
	anterioresInit = true;
	toggle_anterioresInit(0);
	
}

function toggle_anteriores(idNew, time, sld) {
					
	var otherElement1;
	var otherElement2;
	var newElement;
	var array;
	var totalFotos;
	anterioresInit = true;
	
	if (idNew == 1){
		newElement = "div #imagens_inverno2011";
		array = aInverno2011;
		totalFotos = totalFotosInverno2011;
		
		otherElement1 = "div #imagens_verao2011";
		otherElement2 = "div #imagens_inverno2010";
	} else if (idNew == 2){
		newElement = "div #imagens_verao2011";
		array = aVerao2011;
		totalFotos = totalFotosVerao2011;
		
		otherElement1 = "div #imagens_inverno2011";
		otherElement2 = "div #imagens_inverno2010";
	} else if (idNew == 3){
		newElement = "div #imagens_inverno2010";
		array = aInverno2010;
		totalFotos = totalFotosInverno2010;
		
		otherElement1 = "div #imagens_inverno2011";
		otherElement2 = "div #imagens_verao2011";
	}
	
	currCamp = idNew;
	totalimagesC = totalFotos;
	tm = (time == 0 ) ? 300 : 300;
	
	$(otherElement1).fadeOut(tm);
	$(otherElement1).cycle('destroy');

	$(otherElement2).fadeOut(tm);
	$(otherElement2).cycle('destroy');
	
	if (array[sld][1] == "paisagem"){
		expand_anteriores(newElement, 300);
	} else {
		contract_anteriores(newElement, 300);		
	}
	
	$(newElement).delay(300).fadeIn(tm);
	$(newElement).cycle('destroy');
	$(newElement).cycle({ 
		fx:     'fade', 
		speed:  'fast', 
		timeout: 0,
		before: onBefore,
		after: onAfter,
		next:   '.anteriores_seta_proxima', 
		prev:   '.anteriores_seta_anterior',
		startingSlide: sld,
		sync: 0
	});
	
	function onBefore(curr, next, opts) {    
		var index = 0;
		
		if(anterioresInit==true){
			
			index = sld;
			anterioresInit = false;
			
		} else { 
			
			index = opts.nextSlide;
			anterioresInit = false;
			
		}
		
		tm = (time == 0 ) ? 300 : 300;
		
		if (array[index][1] == "paisagem"){
			expand_anteriores(newElement, tm);
		} else {
			contract_anteriores(newElement, tm);		
		}
	}
	
	function onAfter (curr, next, opts){
		
		var idd = (opts.currSlide<9) ? "0" + (opts.currSlide + 1) : (opts.currSlide + 1);
		var tt = (totalFotos<=9) ? "0" + totalFotos : totalFotos;
		var ano = areasAnteriores[currCamp-1] + "_";
		
		var url = "";
		var array = [];
		
		if (currCamp == 1){
			array = aInverno2011;
			url = "content/anteriores/inverno2011/zoom/";
		} else if (currCamp == 2){
			array = aVerao2011;
			url = "content/anteriores/verao2011/zoom/";
		} else if (currCamp == 3){
			array = aInverno2010;
			url = "content/anteriores/inverno2010/zoom/";
		}
		
		var wdt = (array[opts.currSlide][1] == "paisagem") ? 900 : 400;
		
		imgAtual = [url + (opts.currSlide + 1) + ".jpg", opts.currSlide, array[opts.currSlide][1], wdt];
		
		$('.anteriores_compartilhar').html(idd + "<span>/" + tt + "</span>");
		$.address.value("colecao/anteriores/" + ano + (opts.currSlide + 1));
			
	}
	
	for(var i=1 ; i<=3; i++){
		
		if(i==idNew){
			
			$('#anteriores_menuItem' + i).children('.anteriores_menuItemseta').stop(true, true).animate({"left": "3"}, timeOver);
			$('#anteriores_menuItem' + i).stop(true, true).animate({"color": "#e9f1cb"}, timeOver);
			
		}else{
			
			$('#anteriores_menuItem' + i).children('.anteriores_menuItemseta').stop(true, true).animate({"left": "0"}, timeOver);
			$('#anteriores_menuItem' + i).stop(true, true).animate({"color": "#F5F0F0"}, timeOver);
			
		}
		
	}
	
}

function expand_anteriores(div_images, time){
	$(div_images).animate({'width': 445, 'height': 306}, time);
	$('.imagem_anteriores').stop(true, true).animate({'width': 455}, time);
	$('.compartilhar').stop(true, true).animate({'left': 252 }, time);
	$('.container_anteriores').stop(true, true).animate({'margin-left': 194, 'width':615 }, time);
}

function contract_anteriores(div_images, time){
	$(div_images).animate({'width': 196, 'height': 294}, time);
	$('.imagem_anteriores').stop(true, true).animate({'width': 206}, time);
	$('.compartilhar').stop(true, true).animate({'left': 378 }, time);
	$('.container_anteriores').stop(true, true).animate({'margin-left': 318, 'width':366 }, time);
}

function gotoSlide(id){
	
	var cc = (currCamp==1) ? "div #imagens_inverno2011" : (currCamp==2) ? "div #imagens_verao2011" : "div #imagens_inverno2010";
	$(cc).cycle(id);
	
}

function toggle_anterioresInit(time){
	
	var value = $.address.value();
	mvalue = value.split("/");
	
	if(mvalue.length>3){
		
		var str = mvalue[3];
		var strL = str.substring(0);
		
	}else{
		
		strL = "";
		
	}
	
	strL = strL.substring(0, strL.indexOf("_"));
		
	if(strL=="inverno2011" || strL=="verao2011" || strL=="inverno2010"){
		
		var strN = str.split("_");
		var nr = (strN[1]) - 1;
		totalimagesC = (areasAnterioresn[strN[0]]==0) ? totalFotosInverno2011 : (areasAnterioresn[strN[0]]==0) ? totalFotosVerao2011 : totalFotosInverno2010;
		
		if(nr>totalimagesC){
			
			toggle_anteriores(areasAnterioresn[strN[0]] + 1, time, 0);
			
		}else{
			
			toggle_anteriores(areasAnterioresn[strN[0]] + 1, time, nr);
			
		}
	
	}else{
		
		toggle_anteriores(areasAnterioresn["inverno2011"] + 1, time, 0);
		
	}	
	
}
