/**
function writeCookie(name, value, time_exp){
 if(time_exp != "" ){
  var exp = new Date();
  var expira = exp.getTime() + (time_exp * 3600000);
  exp.setTime(expira);
  document.cookie = "" + name + "=" + value + "; expires=" + exp.toGMTString();
 }
}

function readCookie(name){
 var cookieValue = "";
 var search = name + "=";
 if(document.cookie.length > 0){
  offset = document.cookie.indexOf(search);
  if (offset != -1){
   offset += search.length;
   end = document.cookie.indexOf(";", offset);
   if (end == -1) end = document.cookie.length;
   cookieValue = unescape(document.cookie.substring(offset, end))
  }
 }
 return cookieValue;
}
 **/

function slideUl (fromUL, toUL, back){
    var from = (back ? 330 : -330);
    fromUL.morph({left: from});
    toUL.morph({left: 0});
}
	
function listMenu (json){
    var boxFotos = $('boxFotos');
    boxFotos.empty();
    //boxFoto
    $$('.toolNavStatus').set('html','Bem-vindo');
    var divAccount = new Element('div', {'class': 'div_account_index', 'id': 'div_account_index'}).inject(boxFotos);
    var nameUser = json.nameUser.split(' ');
    //var h2Account = new Element('h2', {'html': 'Bem-vindo(a), ' + nameUser[0] + '.' }).inject(divAccount);
    var h2Account = new Element('h2', {'html': nameUser[0] + ',' }).inject(divAccount);
    new Element('p', {'html': 'Bem-vindo(a) ao exclusivo sistema PhotoBook LPJ. Uma forma inovadora para escolher suas fotos de maneira fácil, rápida e segura.<br /> Para começar basta clicar no link <b>Escolha das fotos</b>. Divirta-se!' }).inject(divAccount);
    //subnav
    var subnav = $('subnav');
    var eventoStore = new Element('input', {'type': 'hidden', 'id': 'eventoStore'}).inject(subnav);
    var ulEventos = $('ulEventos');
    var ulFotos = $('ulFotos');

    //json eventos
    var subnavTitle = $$('.subnavTitle');

    json.eventos.each(function(item, index) {
        var liEvento = new Element('li', {'class':'slideMenu',
		
		//adiciona o evento click para listar as fotos
		'events': {
            'click': function(e){
                e.stop();
                //[Pasta 0,Nome 1,Qtd de Fotos 2,Keyword 3]
                var dataEvento = eventoStore.retrieve('evento', {});
                dataEvento.pasta = item[0];
                dataEvento.nome = item[1];
                dataEvento.qtdFotos = item[2];
                dataEvento.keyword = item[3];
										
                var evento = item[0];
                var keyword = item[3];
				var pagina = 0;
										
                //Se for noKeyword item[3] == 0 aparece o link All
                var fAll = $('f-all-0');
                if (item[3] == 1){fAll.setStyle('display', 'none'); var tamanho = 'sel';}
                else {fAll.setStyle('display', 'block'); var tamanho = 'all'; }
							
                requestBoxFoto(evento, keyword, tamanho, pagina);
                navegacao(evento, keyword, tamanho);
																
                this.addClass('selected');
                subnavTitle.set({'html':item[1]});
																
                var back = $('backButton');
                back.set('href', ulEventos.title);
										
                var dataBack = back.retrieve('slide', {});
                dataBack.from = ulFotos;
                dataBack.to = ulEventos;
										
                slideUl (ulEventos, ulFotos, 0);
                ulEventos.removeClass('ulAtual');
                ulFotos.addClass('ulAtual');
										
										
                $('div_account_index').setStyle('display', 'none');
                $$(['.toolNavStatus']).setStyle('display', 'block');
            }
        }

		}).inject(ulEventos);
			
        liEvento.store('keyword', item[3]);
        var aEvento = new Element('a', {'href': '#ulFotos', 'html':item[1]
        }).inject(liEvento);	
        var pEvento = new Element('p', {'html': item[2] + ' Fotos'}).inject(liEvento);						
    });//fecha json eventos each
			
    //backButton
    $('backButton').addEvent('click', function(e) {
        e.stop();
					
        var ulFrom = $$('.ulAtual');
        var ulTo = this.href.split('#');
        var tituloSubNav = ulTo[1].split('ul');
        ulTo = $(ulTo[1]);
        slideUl (ulFrom,ulTo,1);
        ulFrom.removeClass('ulAtual');
        ulTo.addClass('ulAtual');

	var menu = $('ulIndex');
	menu.set('class', 'ulSlide');
					
        $$('#subnav li').removeClass('selected');
        var remove = $$('.boxFoto').dispose();
        var div_account = $('div_account_index');
        div_account.setStyle('display', 'block');
        div_account.empty();
					
        pag = 0;
        var next = $$('.next');
        var prev = $$('.prev');
        next.setStyle('display', 'none');
        prev.setStyle('display', 'none');
        $$(['h2.toolNavStatus', 'h3.toolNavStatus']).setStyle('display', 'none');
        var boxFotos = $('boxFotos');
        boxFotos.set('class' , 'account_index');
        this.set('href', '#ulIndex');
							
        var subnavTitle = $$('.subnavTitle');
        subnavTitle.set({'html':tituloSubNav[1]});
							
        //seta o nome
        //var cookieLogin = new Hash.Cookie('Account', {duration: 360000});
        //var username = cookieLogin.get('username');	
        //var password = cookieLogin.get('password');
        //var json = cookieLogin.get('json');

        var request = new Request.JSON({
            method: 'get',
            url: '../infuser',
            onComplete: function(json2) {
									
                var nameUser = json2.nome;	
                 //var h2Account = new Element('h2', {'html': 'Bem-vindo(a), ' + nameUser + '.' }).inject(divAccount);
    var h2Account = new Element('h2', {'html': nameUser + ',' }).inject(divAccount);
    new Element('p', {'html': 'Bem-vindo(a) ao exclusivo sistema PhotoBook LPJ. Uma forma inovadora para escolher suas fotos de maneira fácil, rápida e segura.<br /> Para começar basta clicar no link <b>Escolha das fotos</b>. Divirta-se!' }).inject(divAccount);
    
				
            }//fecha function on complete
        }).send(this);

        //var nameUser = readCookie('nameuser');	
        //var h2Account = new Element('h2', {'html': 'Bem-vindo(a), ' + nameUser + '.' }).inject(div_account);
							
							
    });
			
    //link para as fotos
    $$('#subnav li.fotosLink').each(function(el){	
        el.addEvent('click', function(e) {
            var evento = eventoStore.retrieve('evento').pasta;
            var qtdFotos = eventoStore.retrieve('evento').qtdFotos;
            e.stop();
            $$('#subnav li').removeClass('selected');
            this.addClass('selected');
            var liId = el.id.split ("-");
            var tamanho = liId[1];
            var keyword = liId[2];
            //requestBoxFoto(evento, keyword, tamanho, pag);
            requestBoxFoto(evento, keyword, tamanho, 0);
            navegacao(evento, keyword, tamanho);	
        });
    });
			
			
    $$('.slideMenu').addEvent('click', function(e) {
        e.stop();
        $$('.ulSlide').set('morph', {duration: 'short', transition: Fx.Transitions.Circ.easeOut});
        var ulFrom = this.getParent('ul');
        var a = this.getChildren('a');
        var href = a[0].href;
        var ulTo = href.split('#');
        ulTo = $(ulTo[1]);
        slideUl (ulFrom, ulTo, 0);
						
        ulFrom.removeClass('ulAtual');
        ulTo.addClass('ulAtual');
						
        var back = $('backButton');
        back.set('href', ulFrom.title);
						
        var menuTitulo = a[0].get('html');
        $('menuTitulo').set('html', menuTitulo);
						
    });
			
			
			
}
		
