$().ready(function() {
	categorias();
	link_top();
	galeria();
	combobox();
});

function monta_button(par) {
	return '<span class="b"><span class="bl"></span><span class="ic"></span><span class="bc">' + par + '</span><span class="br"></span></span><span class="h"><span class="bl"></span><span class="ic"></span><span class="bc">' + par + '</span><span class="br"></span></span>';
}

function carrega_produtos(url, scrollg) {
	if(scrollg) {
		$('html, body').stop(false, true).animate({scrollTop : (document.getElementById('banner_nav') ? $('#banner_nav').offset().top - 180 : 0)}, 500);
	}

	var var_url = url.split("?");

	$('#right').fadeOut(250);
	$('#loading').fadeIn(250);

	$.ajax({
		url : config_url + 'web/modulos/produtos/carrega_produtos.php?' + var_url[1],
		dataType : 'html',
		success : function(res) {
			$('#loading').fadeOut(250);
			$('#right').html(res);
			$('#right').delay(250).fadeIn(250);

			$('div.pag>a', '#right').click(function() {
				carrega_produtos($(this).attr('href'), true);

				return false;
			});
		}
	});
}

function categorias() {
	$('>a', '#categoria>ul>li').click(function() {
		if(!$(this).hasClass('on')) {
			var hrefCat = $(this).attr('href') + ($("#palavra").val() ? "&palavra=" + $("#palavra").val() : "");
			var par = $(this).parent();

			$('#categoria>ul>li').each(function() {
				$('>a', this).removeClass('select on');
				$('>a>.b', this).stop(false, true).fadeIn(250);
				$('>a>.h', this).stop(false, true).fadeOut(250);
				$('>ul', this).stop(false, true).slideUp(500);
			});

			$('#categoria>ul>li>ul>li').each(function() {
				$('>a', this).removeClass('select on').stop(false, true).animate({backgroundPosition : "0% 0%", color : "#000000"}, 250);
			});

			$('>a', par).addClass('select on');
			$('>a>.b', par).stop(false, true).fadeOut(250);
			$('>a>.h', par).stop(false, true).fadeIn(250);
			$('>ul', par).stop(false, true).slideDown(500);

			carrega_produtos(hrefCat, true);
		}

		return false;
	});

	$('>a', '#categoria>ul>li>ul>li').click(function() {
		if(!$(this).hasClass('on')) {
			var hrefCat = $(this).attr('href') + ($("#palavra").val() ? "&palavra=" + $("#palavra").val() : "");
			var par = $(this).parent();

			$('>a', par.parent().parent()).removeClass('on');

			$('>li', par.parent()).each(function() {
				$('>a', this).removeClass('select on').stop(false, true).animate({color : "#000000"}, 250).css({backgroundPosition : "0% 0%"});
			});

			$('>a', par).addClass('select on').stop(false, true).animate({color : "#1685B9"}, 250).css({backgroundPosition : "0% 100%"});

			carrega_produtos(hrefCat, true);
		}

		return false;
	});

	$('>a', '#categoria>ul>li').each(function() {
		if(!$(this).hasClass('select')) {
			$('ul', $(this).parent()).hide();
		} else {
			$('ul', $(this).parent()).show();
		}
	});

	$('>li', '#categoria>ul').hover(function() {
		if(!$('>a', this).hasClass('select')) {
			$('>a>.b', this).stop(false, true).fadeOut(250);
			$('>a>.h', this).stop(false, true).fadeIn(250);
		}
	}, function() {
		if(!$('>a', this).hasClass('select')) {
			$('>a>.b', this).stop(false, true).fadeIn(250);
			$('>a>.h', this).stop(false, true).fadeOut(250);
		}
	});

	$('>a', '#categoria>ul>li>ul>li').hover(function() {
		if(!$(this).hasClass('select'))
			$(this).stop(false, true).animate({color : "#1685B9"}, 250).css({backgroundPosition : "0% 100%"});
	}, function() {
		if(!$(this).hasClass('select'))
			$(this).stop(false, true).animate({color : "#000000"}, 250).css({backgroundPosition : "0% 0%"});
	});
}

function link_top() {
	$('body').css('backgroundColor', '#EAEAEA');

	$('.button').hover(function() {
		if(!$(this).hasClass('select')) {
			$('>.b', this).stop(false, true).fadeOut(250);
			$('>.h', this).stop(false, true).fadeIn(250);
		}
	}, function() {
		if(!$(this).hasClass('select')) {
			$('>.b', this).stop(false, true).fadeIn(250);
			$('>.h', this).stop(false, true).fadeOut(250);
		}
	});

	$('.link_top').click(function() {
		$('html, body').stop(false, true).animate({scrollTop : 0}, 500);

		return false;
	});
}

function galeria() {
	Galleria.loadTheme(config_url + 'system/js/galleria/themes/classic/galleria.classic.js');

	$('.galleria').galleria({
		transition: 'fadeslide'
	});

	$("a[rel^='prettyPhoto']").prettyPhoto({
		animation_speed: 'fast',
		slideshow: 5000,
		autoplay_slideshow: false,
		opacity: 0.50,
		show_title: false,
		allow_resize: true,
		default_width: 640,
		default_height: 480,
		counter_separator_label: ' de ',
		theme: 'facebook',
		horizontal_padding: 20,
		hideflash: true,
		wmode: 'transparent',
		autoplay: true,
		modal: false,
		deeplinking: false,
		overlay_gallery: true,
		keyboard_shortcuts: true,
		ie6_fallback: true,
		social_tools: false
	});
}

function combobox() {
	var count = 0;

	$('select').each(function() {
		count++;

		var sel = $(this).parent();

		$(this).css({'opacity' : '0', 'position' : 'absolute', 'z-index' : '20'});

		$(this).after('<span class="sel" style="width:' + (parseInt($(this).innerWidth()) || 0) + 'px"><span class="val"></span></span>');

		$('.val', sel).html($('option:selected', this).text());

		$(this).change(function() {
			$('.val', sel).html($('option:selected', this).text());
		});
	});

	$('select.sel_estado').change(function(){
		var sel_cid = $("#" + $(this).attr('rel'));
		var sel = $(sel_cid).parent();

		$('.val', sel).html('Carregando...');

		$.getJSON(config_url + 'system/php/cidade.php?callback=?', {id: $(this).val()}, function(res){
			var options = '';

			for (var i = 0; i < res.length; i++) {
				options += '<option value="' + res[i].optVal + '">' + res[i].optDis + '</option>';
			}

			$(sel_cid).html(options);
			$('option:first', sel_cid).attr('selected', 'selected');

			$('.val', sel).html('');

			$(sel_cid).change(function() {
				$('.val', sel).html($('option:selected', this).text());
			});
		});
	});
}

