function renderClickProducts() {
	$("#product .jquery-selectbox .jquery-selectbox-list span").click(function(){
		var product = $('#product #produto').val();
		var many_select = $('#listagem');

		if (product == null || product == "") {
			alert('tente novamente!');
		} else {
			$.get('products.ajax.php?search=' + product, function(data){
				many_select.html(data);
			}, "html");
		}
	});
}

function renderClickCategory() {
	$("#cat .jquery-selectbox .jquery-selectbox-list span").click(function(){
		var category = $('#category').val();
		var prod_select = $('#product #produto');

		if (category == null || category == "") {
			prod_select.html('<option value="">Selecione um produto...</option>');
			prod_select.parents('.jquery-selectbox').unselectbox();
			prod_select.selectbox();
			renderClickProducts();
		} else {
			$.get('product.ajax.php?search=' + category, function(data){
				prod_select.html(data);
				prod_select.parents('.jquery-selectbox').unselectbox();
				prod_select.selectbox();
				renderClickProducts();
			}, "html");
		}
	});
	
}
$(document).ready(function(){
	
	$('a.fancybox').fancybox({
		'width':753,
		'height':248,
		'autoScale':false,
		'transitionIn':'none',
		'transitionOut':'none',
		'type':'iframe',
		'onStart': function() {
			$("#video iframe").hide();
		},
		'onClosed': function() {
			$("#video iframe").show();
		}
	});
					
	var limg = $('#page div img').attr('src');
	var linka = $('#page div img.fancybox').parent().attr('href');	
	
	if (limg = linka){
		$('#page div img.fancybox').parent().addClass('fancybox');
	};		

	$('#banner').after('<div id="nav"></div>').cycle({ 
    fx:     'fade', 
    speed:   1000, 
    timeout: 6000, 
    pager: '#nav',
		cleartype: true, 
		cleartypeNoBg: true  
	});
	$('#nav a').text('');
	$('#sidebar ul li ul').append('<li class="bottom"></li>');
	
	$('#sidebar ul>li a').click(function() {
		$('ul.sub-menu', $(this).parent()).slideToggle('fast', function() {
			$(this).parent().find('a:first').toggleClass('aberto');
		});
	});
	
	$('#thumb').jcarousel({
	 	vertical: true,
		scroll: 2
	});
	var nThumb = $('#thumb li').length;
	if (nThumb <= 4 ){
		$('.jcarousel-next, .jcarousel-prev').hide();
	};
	if (nThumb <= 0 ){
		$('.jcarousel-skin-tango').hide();
	};
	var nVideos = $('#box_page #video ul li').length;
	if (nVideos < 1 ){
		$('#box_page #video ul').hide();
	};	
	$('#thumb li:first-child').addClass('active');
	$('#thumb li a').click(function(){
		$("#big").attr("href", $(this).attr("big"));
		$('#thumb li.active').removeClass('active');
		$(this).parent().addClass('active');
	});	
	
	$('#video ul a').click(function(){
		$("#big-video").attr("src", $(this).attr("big-video"));
	});	

	$('.cloud-zoom, .cloud-zoom-gallery').CloudZoom();
	
	$("#list_tabs a.tab").click(function () {
		$("#list_tabs a.active").removeClass("active");
		$(this).addClass("active");
		$(".tabs").show();
		var content_show = $(this).attr("rel");
		$("#"+content_show).hide();
	});
	$('input#curriculo').change(function(){
	  $('span#legenda').text($('input#curriculo').val());
		$('input#curriculo').attr('value', $('span#legenda').text );
	});	
	$('.select').selectbox();
	$('#guias .baixar').append('<img src="images/bt-seta.png" width="11 height="13"/>');
	
	$('#formNews').validate();
	$('#formFaq').validate();
	$('#formEnvio').validate();
	
	$("#linha .jquery-selectbox .jquery-selectbox-list span").click(function(){
		var line = $('#line').val();
		var cat_select = $('#cat #category');

		if (line == null || line == "") {
			cat_select.html('<option value="">Selecione uma linha...</option>');
			cat_select.parents('.jquery-selectbox').unselectbox();
			cat_select.selectbox();
			renderClickCategory();
		} else {
			$.get('category.ajax.php?search=' + line, function(data){
				cat_select.html(data);
				cat_select.parents('.jquery-selectbox').unselectbox();
				cat_select.selectbox();
				renderClickCategory();
			}, "html");
		}
	});

	renderClickCategory();
	renderClickProducts();

	$("#uf .jquery-selectbox-list span").click(function(){
		var values = $('#estado').val();
		var city_select = $('#box_city #city');

		if (values == null || values == "") {
			city_select.html('<option value="">Estado...</option>');
			city_select.parents('.jquery-selectbox').unselectbox();
			city_select.selectbox();
		} else {
		//alert($('#estado').val());
			$.get('cidade.ajax.php?search=' + values, function(data){
				city_select.html(data);
				city_select.parents('.jquery-selectbox').unselectbox();
				city_select.selectbox();
			}, "html");
		}
	});
	
});
