// JavaScript Document
var aNuvens = 	Array([0,"paisagem"],
					[1,"retrato"],
					[2, "retrato"],
					[3, "retrato"],
					[4, "retrato"],
					[5, "retrato"],
					[6, "retrato"],
					[7, "retrato"],
					[8, "retrato"],
					[9, "paisagem"]);

var aFlores = Array(	[0, "retrato"],
						[1, "paisagem"],
						[2, "paisagem"],
						[3, "retrato"],
						[4, "paisagem"],
						[5, "retrato"],
						[6, "paisagem"],
						[7, "paisagem"],
						[8, "retrato"],
						[9, "paisagem"],
						[10, "retrato"],
						[11, "paisagem"],
						[12, "retrato"],
						[13, "retrato"],
						[14, "retrato"],
						[15, "retrato"]);

var aCor = Array(		[0, "paisagem"],
						[1, "retrato"],
						[2, "retrato"],
						[3, "retrato"],
						[4, "retrato"],
						[5, "retrato"],
						[6, "retrato"],
						[7, "retrato"],
						[8, "retrato"]);


var fotoAtual = 1;
var totalFotosNuvens = 10;
var totalFotosFlores = 16;
var totalFotosCor = 9;

var currCamp = -1;
var areasCampanha = ["nuvens", "flores", "cor"];
var areasCampanhan = [];
areasCampanhan["nuvens"] = 0;
areasCampanhan["flores"] = 1;
areasCampanhan["cor"] = 2;
var totalimagesC = 0;
var campanhaInit = false;

function initcampanha(){
	
	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);
		   
    });

	$('.campanha_rede').hover(
	
		function() {
			
			$(this).stop(true, true).animate({"backgroundColor": "#fbf2ef"}, timeOver);
			
		},
	
		function() {
			
			$(this).stop(true, true).animate({"backgroundColor": "#fdd2c4"}, timeOver);
			
		}
	
	);
	
	$('.campanha_seta_anterior, .campanha_seta_proxima').hover(
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": .8}, timeOver);
			
		},
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": 1}, timeOver);
			
		}
	
	);
	
	$('.campanha_menuItem').hover(
	
		function() {
			
			if(currCamp!=$(this).attr("name")){
				
				$(this).children('.campanha_menuItemseta').stop(true, true).animate({"left": "3"}, timeOver);
				$(this).stop(true, true).animate({"color": "#e9f1cb"}, timeOver);
				
			}
			
		},
	
		function() {
			
			if(currCamp!=$(this).attr("name")){
				
				$(this).children('.campanha_menuItemseta').stop(true, true).animate({"left": "0"}, timeOver);
				$(this).stop(true, true).animate({"color": "#F5F0F0"}, timeOver);
				
			}
			
		}
	
	);
	
	$('.botao_zoom_campanha').hover(
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": .8}, timeOver);
			
		},
	
		function() {
			
			$(this).stop(true, true).animate({"opacity": 1}, timeOver);
			
		}
	
	);
	
	campanhaInit = true;
	toggle_campanhaInit(0);
	
}

function toggle_campanha(idNew, time, sld) {
					
	var otherElement1;
	var otherElement2;
	var newElement;
	var array;
	var totalFotos;
	campanhaInit = true;
	
	if (idNew == 1){
		newElement = "div #imagens_nuvens";
		array = aNuvens;
		totalFotos = totalFotosNuvens;
		
		otherElement1 = "div #imagens_flores";
		otherElement2 = "div #imagens_cor";
	} else if (idNew == 2){
		newElement = "div #imagens_flores";
		array = aFlores;
		totalFotos = totalFotosFlores;
		
		otherElement1 = "div #imagens_nuvens";
		otherElement2 = "div #imagens_cor";
	} else if (idNew == 3){
		newElement = "div #imagens_cor";
		array = aCor;
		totalFotos = totalFotosCor;
		
		otherElement1 = "div #imagens_nuvens";
		otherElement2 = "div #imagens_flores";
	}
	
	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_campanha(newElement, 300);
	} else {
		contract_campanha(newElement, 300);		
	}
	
	$(newElement).delay(300).fadeIn(tm);
	$(newElement).cycle('destroy');
	$(newElement).cycle({ 
		fx:     'fade', 
		speed:  'fast', 
		timeout: 0,
		before: onBefore,
		after: onAfter,
		next:   '.campanha_seta_proxima', 
		prev:   '.campanha_seta_anterior',
		startingSlide: sld,
		sync: 0
	});
	
	function onBefore(curr, next, opts) {    
		var index = 0;
		
		if(campanhaInit==true){
			
			index = sld;
			campanhaInit = false;
			
		} else { 
			
			index = opts.nextSlide;
			campanhaInit = false;
			
		}
		
		tm = (time == 0 ) ? 300 : 300;
		
		if (array[index][1] == "paisagem"){
			expand_campanha(newElement, tm);
		} else {
			contract_campanha(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 = areasCampanha[currCamp-1] + "_";
		
		var url = "";
		var array = [];
		
		if (currCamp == 1){
			array = aNuvens;
			url = "content/campanha/nuvens/zoom/";
		} else if (currCamp == 2){
			array = aFlores;
			url = "content/campanha/flores/zoom/";
		} else if (currCamp == 3){
			array = aCor;
			url = "content/campanha/cor/zoom/";
		}
		
		var wdt = (array[opts.currSlide][1] == "paisagem") ? 900 : 400;
		
		imgAtual = [url + (opts.currSlide + 1) + ".jpg", opts.currSlide, array[opts.currSlide][1], wdt];
		
		$('.campanha_compartilhar').html(idd + "<span>/" + tt + "</span>");
		$.address.value("colecao/campanha/" + ano + (opts.currSlide + 1));
			
	}
	
	for(var i=1 ; i<=3; i++){
		
		if(i==idNew){
			
			$('#campanha_menuItem' + i).children('.campanha_menuItemseta').stop(true, true).animate({"left": "3"}, timeOver);
			$('#campanha_menuItem' + i).stop(true, true).animate({"color": "#e9f1cb"}, timeOver);
			
		}else{
			
			$('#campanha_menuItem' + i).children('.campanha_menuItemseta').stop(true, true).animate({"left": "0"}, timeOver);
			$('#campanha_menuItem' + i).stop(true, true).animate({"color": "#F5F0F0"}, timeOver);
			
		}
		
	}
	
}

function expand_campanha(div_images, time){
	$(div_images).animate({'width': 445, 'height': 306}, time);
	$('.imagem_campanha').stop(true, true).animate({'width': 455}, time);
	$('.compartilhar').stop(true, true).animate({'left': 252 }, time);
	$('.container_campanha').stop(true, true).animate({'margin-left': 194, 'width':615 }, time);
}

function contract_campanha(div_images, time){
	$(div_images).animate({'width': 196, 'height': 294}, time);
	$('.imagem_campanha').stop(true, true).animate({'width': 206}, time);
	$('.compartilhar').stop(true, true).animate({'left': 378 }, time);
	$('.container_campanha').stop(true, true).animate({'margin-left': 318, 'width':366 }, time);
}

function gotoSlide(id){
	
	var cc = (currCamp==1) ? "div #imagens_nuvens" : (currCamp==2) ? "div #imagens_flores" : "div #imagens_cor";
	$(cc).cycle(id);
	
}

function toggle_campanhaInit(time){
	
	var value = $.address.value();
	mvalue = value.split("/");
	
	if(mvalue.length>3){
		
		var str = mvalue[3];
		var strL = str.substring(0, 3);
		
	}else{
		
		strL = "";
		
	}
	
	if(strL=="nuv" || strL=="flo" || strL=="cor"){
		
		var strN = str.split("_");
		var nr = (strN[1]) - 1;
		totalimagesC = (areasCampanhan[strN[0]]==0) ? totalFotosNuvens : (areasCampanhan[strN[0]]==0) ? totalFotosFlores : totalFotosCor;
		
		if(nr>totalimagesC){
			
			toggle_campanha(areasCampanhan[strN[0]] + 1, time, 0);
			
		}else{
			
			toggle_campanha(areasCampanhan[strN[0]] + 1, time, nr);
			
		}
	
	}else{
		
		toggle_campanha(areasCampanhan["nuvens"] + 1, time, 0);
		
	}	
	
}
