$('html,body').ready(function() {

    //configuracoes iniciais
    var section = ["inicio", "contato", "lojas", "divirta-se", "inicio", "colecao", "representantes", "ykz-moda", "blog"];
    var lojas = ["local-estado", "local-cidade", "local-bairro"];
    var representantes = ["regiao-estado", "regiao-regiao"];
    var menu = ["divirta-se", "contato", "colecao", "ykz-moda"];
    var submenu = ["brincando-de-colorir", "vestir", "chat", "localizacao", "catalogo-online", "ykz"];

    var url = 'http://www.ykzmoda.com.br';
    var hash = window.location.hash.split('/');


    //verifica a hash inicial e anima.	
    if (hash == "") {
        $('html, body').animate({scrollTop: $('#' + section[4]).offset().top}, 1000);
        now = 4;
    } else {
        if (hash[1] == "inicio" || hash[1] == "lojas" || hash[1] == "representantes"){
            $('html, body').animate({scrollTop: $('#' + hash[1]).offset().top}, 1000);
			_gaq.push(['_trackPageview', '/' + hash[1] + '/']);
        }
        
		if(hash[1] == "divirta-se" || hash[1] == "contato" || hash[1] == "colecao" || hash[1] == "ykz-moda" ) {
            $('html, body').animate({scrollTop: $('#' + hash[1]).offset().top}, 1000);
            $("#" + hash[1] + " div.section-content").load("/" + hash[1] + "/" + hash[2] + "/?ajax=1 div.ajax-content");
			_gaq.push(['_trackPageview', '/' + hash[1] + '/' + hash[2] + '/']);
        }
        now = jQuery.inArray(hash[1], section);
        if (now <= 0) { now = 1; }
    } 

	if(hash[1] == "blog" && hash[2] == ""){
		$('html, body').animate({scrollTop: $('#' + hash[1]).offset().top}, 1000);
		_gaq.push(['_trackPageview', '/' + hash[1] + '/']);
	}
	
	if(hash[1] == "blog" && hash[2] != "") {
		$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
		$('html,body').animate({scrollTop: $('#blog').offset().top},1000);
		hash = hash.join('/');
		hash = hash.replace("#!/blog/","blog/");
		$('#blogcontent').load('/' + hash + '?ajax=1 , div.post-item');
		_gaq.push(['_trackPageview', '/' + hash]);
	}
		

    //aplica as funcoes de animacao no formulario de busca	
	$('#formb').submit(function() { return false; });
	$('.btnbusca').click(function(){
		$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
		$('html,body').animate({scrollTop: $('#blog').offset().top},1000);
		var valores = $('#formb').serialize();
    	$('#blogcontent').load('/?ajax=1&'+ valores +' , div.post-item');
	})
	
	$('#busca-blog').submit(function() { return false; });
	$('.btnbusca-blog').click(function(){
		$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
		$('html,body').animate({scrollTop: $('#blog').offset().top},1000);
		var valores = $('#busca-blog').serialize();
    	$('#blogcontent').load('/?ajax=1&'+ valores +' , div.post-item');
	})


	//aplica as funcoes de animacao no formulario de comentarios	
	//$('form#commentform').submit(function() { return false; });
	$('#commentform').live('submit', function() { 
		
		
		//$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
		return false; 
	});
	
	$('#submit-comment').live('click', function() { 
		
		$('#author').attr('name', 'comment_author');
		$('#email').attr('name', 'comment_author_email');
		$('#url').attr('name', 'comment_author_url');
		$('#comment').attr('name', 'comment_content');
		
		var alerta = ''
		if (!$('#author').val()) { alerta = alerta + '\n - seu nome '; }	
		if ((!$('#email').val()) || ($('#email').val().indexOf("@") < 1) || ($('#email').val().indexOf('.') < 4)) { alerta = alerta + '\n - email válido '; }	
		if (!$('#comment').val()) { alerta = alerta + '\n - seu comentário '; }	
		
		if (!alerta) {
		
			var valores = $('#commentform').serialize();
			var permalink = $('#comment_post_permalink').val();
			//alert($('#comment_post_permalink').val()+'?ajax=1&'+valores);
			//$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
			$('#blogcontent').load(permalink+'?ajax=1&'+valores+' , div.post-item');
			
			$('html,body').animate({scrollTop: $('#blog').offset().top},1000);
			
		} else {
			
			alert('Ops. Para prosseguir é necessário preencher:' + alerta);
			
		}
		
/*
		$('#blogcontent').load(
			$.post($('#comment_post_permalink').val(), { 'comment_post_ID': '134', 'author': 'Nome do Autor', 'email': 'aaa@bbb.com', 'url': '', 'comment': 'teste de comentário!' },
			return data;
		), 'div.post-item');
	
		$.ajax({
		  type: 'POST',
		  url: function(){$('#comment_post_permalink').val()+'?ajax=1';},
		  data: valores,
		  success: function(){ alert( "Data Saved"); }

		});


		var url = $('#comment_post_permalink').val()+'?ajax=1';
		$.post( url, { 'comment_post_ID': '134', 'author': 'Nome do Autor', 'email': 'aaa@bbb.com', 'url': '', 'comment': 'teste de comentário!' },
			function(data) { alert(data); }
		);


		
*/
		
	});
	
	
/*
	$('#submit').click(function(){
		$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
		$('html,body').animate({scrollTop: $('#blog').offset().top},1000);
		//var valores = $('#formb').serialize();
		$.post( '/wp-comments-post.php', { 'author': 'Nome do Autor', 'email': 'aaa@bbb.com', 'url': '', 'comment': 'teste de comentário!' } );
    	//$('#blogcontent').load('/?ajax=1&'+ valores +' , div.post-item');
	})*/


    //aplica as funcoes de animacao nos links	
    $('a').live('click' , function() {	
		hreff = this.hash; 
		if (hreff == "#!/blog/") {
			$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
			$('html,body').animate({scrollTop: $('#blog').offset().top},1000);
			$('#blogcontent').load('/blog/?ajax=1 , div.post-item');
			_gaq.push(['_trackPageview', '/blog/']);
		} else if(hreff.indexOf("blog") != -1) { 
			str = hreff.replace("#!/blog/","blog/"); 
			$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
			$('html,body').animate({scrollTop: $('#blog').offset().top},1000);
			$('#blogcontent').load('/' + str + '?ajax=1 , div.post-item');
			_gaq.push(['_trackPageview', '/' + str]);
		}
	})			
	$.each(section, function(index, value) {
    	$('a[href="' + url + '\\/\\#\\!\\/' + value + '\\/"]').click(function() {		
        	if (value == "inicio" || value == "lojas" || value == "blog" || value == "representantes") {
				$('html,body').animate({ scrollTop: $('#' + value).offset().top }, 1000); 
				_gaq.push(['_trackPageview', '/' + value]);
			} else {
				if (window.location.hash != "#!/" + value + "/") {
					$('#' + value + ' div.section-content').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
                	$('html,body').animate({ scrollTop: $('#' + value).offset().top }, 1000);
                	$('#' + value + ' div.section-content').load('/' + value + '/?ajax=1 , div.ajax-content');
					_gaq.push(['_trackPageview', '/' + value + '/']);
            	}
        	}
        	now = index;
    	});
    });

	$.each(menu, function(index, value) {
		$.each(submenu, function(index, val) {
			$('a[href*="\\#\\!\\/' + value + '\\/' + val + '"]').click(function() {
				if (val == "divirta-se") { val = ["brincando-de-colorir", "vestir"]; }
				if (val == "contato") { val = ["chat", "localizacao"]; }
				if (val == "colecao") { val = ["catalogo-online"]; }
				if (val == "ykz-moda") { val = ["ykz"]; }

				if (window.location.hash != "#!/" + value + "/" + val + "/") {
					$("#" + value + " div.section-content").html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
					$('html,body').animate({ scrollTop: $("#" + value + "").offset().top }, 1000);
					$("#" + value + " div.section-content").load("/" + value + "/" + val + "/?ajax=1 , div.ajax-content");
					_gaq.push(['_trackPageview', '/' + value + '/' + val + '/']);
				}
			});
		});
	});

/*    // anima a partir das setas do teclado

	$(document).keyup(function(e) { 
	
		var valor = $('#formb').serialize();
	
	    switch (e.keyCode) {
        case 37:
		if(valor != "s=Digite+o+que+procura"){} else {
            now--;
            if (now <= 0) { now = 1; }
			$('html,body').animate({scrollTop: $('#' + section[now]).offset().top}, 1000);
			_gaq.push(['_trackPageview', '/' + section[now] + '/']);
        	$("html:animated,body:animated").clearQueue(); }
            break;
			
        case 39:
		if(valor != "s=Digite+o+que+procura"){} else {
            now++;
            if (now >= 9) { now = 8; }
			$('html,body').animate({ scrollTop: $('#' + section[now]).offset().top }, 1000);
			_gaq.push(['_trackPageview', '/' + section[now] + '/']);
        	$("html:animated,body:animated").clearQueue(); }
            break;
        }
 
    });
*/
	//blog - categorias
	$('#cat').change(function() {
		if($('#cat').val()>0) {	
			$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
			$('html,body').animate({scrollTop: $('#blog').offset().top},1000);
	    	$('#blogcontent').load('/?ajax=1&cat='+ $('#cat').val() +' , div.post-item');
		}
	});

	//arquivo - arquivo
	$('#archive').change(function() {
		if($('#archive').val()) {
			$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
			$('html,body').animate({scrollTop: $('#blog').offset().top},1000);	
			$('#blogcontent').load($('#archive').val().replace('/#!/', '/') + '?ajax=1 , div.post-item');
			document.location.href=$('#archive').val();
		}
	});

	//arquivo - ultimos posts
	$('#lastposts').change(function() {
		if($('#lastposts').val()) {
			$('#blogcontent').html('<div id="loading"><img src="/wp-content/themes/ykzmoda/images/loading.gif" /><br />Carregando</div>');
			$('html,body').animate({scrollTop: $('#blog').offset().top},1000);	
			$('#blogcontent').load($('#lastposts').val().replace('/#!/', '/') + '?ajax=1 , div.post-item');
			document.location.href=$('#lastposts').val();
		}
	});

	

    //desabilita o select
    $.each(lojas,
    function(index, value) {
        $("#" + value).change(
        function() {
            if ($(this).val() == 0) {
                alert('Você precisa informar um(a) "+value+"!');
                $(this).focus();
            } else {
                if (value == "local-estado")
                {
                    $('#ajax-loja').html('');
                    $('#local-cidade').attr("disabled", true);
                    $('#local-bairro').attr("disabled", true);
                    $("#local-cidade").load("" + $(this).val() + " , #local-cidade>option",
                    function() {
                        $("#local-cidade").removeAttr("disabled");
                    });
					if ($('#local-estado option:selected').text() != 'Selecione') {
						_gaq.push(['_trackPageview', '/lojas/' + $('#local-estado option:selected').text()]);
					}
                }
                if (value == "local-cidade")
                {
                    $('#ajax-loja').html('');
                    $('#local-bairro').attr("disabled", true);
                    $("#local-bairro").load("" + $(this).val() + " , #local-bairro>option",
                    function() {
                        $('#local-bairro').removeAttr("disabled");
                    });
					if ($('#local-cidade option:selected').text() != 'Selecione') {
						_gaq.push(['_trackPageview', '/lojas/' + $('#local-estado option:selected').text() + '/' + $('#local-cidade option:selected').text()]);
					}
                } else {
                    $('#ajax-loja').html('');
                    $("#ajax-loja").load("" + $(this).val() + " , #ajax-loja div.loja-item");
					if ($('#local-bairro option:selected').text() != 'Selecione') {
						_gaq.push(['_trackPageview', '/lojas/' + $('#local-estado option:selected').text() + '/' + $('#local-cidade option:selected').text() + '/' + $('#local-bairro option:selected').text()]);
					}
                }
            }
        }
        );
    });


    $.each(representantes,
    function(index, value) {
        $("#" + value + "").change(
        function() {
            if ($(this).val() == 0) {
                alert('Você precisa informar um(a) "+value+"!');
                $(this).focus();
            } else {
                if (value == "regiao-estado") {
                    $('#ajax-representante').html('');
                    $('#regiao-regiao').attr("disabled", true);
                    $("#regiao-regiao").load("" + $(this).val() + " , #regiao-regiao>option",
                    function() {
                        $("#regiao-regiao").removeAttr("disabled");
                    });
				} else {
                    $('#ajax-representante').html('');
                    $("#ajax-representante").load("" + $(this).val() + " , #ajax-representante div.representante-item");
                }
            }
        });
    });
});

