var versaoXML = 1.2;

$(function(){
	colocaflashs();
});

function colocaflashobj(id_target, swf, id_obj, width, height, flash_vars){
	var param = {};
	param.wmode = 'transparent';
	if(width == 0){
		width = $('#'+id_target).width();
		if(isNaN(width)){
			width = width.substring(0,width.length-2);
		}
	}
	if(height == 0){
		height = $('#'+id_target).height();
		if(isNaN(height)){
			height = height.substring(0,height.length-2);
		}
	}
	swfobject.embedSWF(swf, id_target, width, height, '9.0.0', '', flash_vars, param);
}

function colocaflashs(){
	var param = {};
	param.wmode = 'transparent';
	$('[flash]').each(function (j) {
		var flashvars = {txt : $(this).attr('flash')};
		wid = $(this).width();
		if(isNaN(wid)){
			wid = wid.substring(0,wid.length-2);
		}
		hei = $(this).height();
		if(isNaN(hei)){
			hei = hei.substring(0,hei.length-2);
		}
		swfobject.embedSWF('images/flash/titulo.swf', $(this).attr('id'), wid, hei, '9.0.0', '', flashvars, param);
	})
}

$(document).ready(function() {
	colocaflashs();
});

function comandosmenu(){
	$("div#mainmenu span").each(function(){ 
		var elshow = $(this).find('div:first');
		var elhide = $(this).find('div');
		$(this).click(function(){ 
			var pos = $(this).position();
			//elshow.css('top',pos.top+'px')
			//	.css('left',(pos.left+$(this).width())+'px');
			elhide.hide();
			elshow.show();
		},function(){
			if(!$(this).hasClass("selected")){
			elshow.hide();
			}
		});
	});
}



function loadsubmenu(){
	$.ajax({
		url: "produtos.xml?v="+versaoXML,
		dataType: "xml",
		success: function(xml){
			$tipos = '';
			$(xml).find('tipo').each(function(i){
				$tipos += '<span><a href="'+($tipos='produtos.php?id='+i+'')+'">'+$(this).children("titulo").text()+'</a></span>';
			});
			$(".submenuprod").html($tipos)
			comandosmenu();
		}	
	});
}




function loadprodutos(index_tipo,index_subtipo){
	var $primeiro = '';
	$.ajax({
		url: "produtos.xml?v="+versaoXML,
		dataType: "xml",
		success: function(xml){
			var busca = 'tipo:eq('+index_tipo+')';
			if(!isNaN(index_subtipo)){
				busca = 'subtipo:eq('+index_subtipo+')';
			}
			$(xml).find(busca).each(function(){
				colocaflashobj('flash', 'images/flash/titulo.swf', 'titulo1111', 0, 0, {txt : $(this).children("titulo").text().replace('&', "%26")});
				
				if(!isNaN(index_subtipo)){
					$('div#foto').html($(this).parent().parent().children("foto").text());
					$('div#texto').html($(this).parent().parent().children("texto").text());
				}else{
					$('div#foto').html($(this).children("foto").text());
					$('div#texto').html($(this).children("texto").text());
				}
				
				if($(xml).find('tipo:eq('+index_tipo+') produto').length > 0){
					var $lista = '<ul id="lista">';
					$(this).find('produto').each(function(i){
						$lista += '<li><a href="#" onClick="abreproduto('+index_tipo+','+i+');">'+$(this).children("nome").text()+'</a></li>';
						if($primeiro == ''){
							abreproduto(index_tipo,0);
						}
					});
					$lista += '</ul>';
					$('div#leftmiolo').html($lista);
					$('div#inferior').show();
				}else{
					$('div#inferior, div.linha').hide();
				}
			});
		}
	});
}


function abreproduto(index_tipo,index_prod){
	$('div#right').slideUp("normal",function(){
		$.ajax({
			url: "produtos.xml?v="+versaoXML,
			dataType: "xml",
			success: function(xml){
				if($(xml).find('tipo:eq('+index_tipo+') produto:eq('+index_prod+') subtipos').length > 0){
					var $detalhes = '';
					$(xml).find('tipo:eq('+index_tipo+') produto:eq('+index_prod+') subtipos subtipo').each(function(){
						if($(this).children("img") && $(this).children("img").text() != ''){
							$detalhes += '<img class="imgspace2" src="'+$(this).children("img").text()+'" />';
						}
						$detalhes += '<h2>'+$(this).children("subtitulo").text()+'</h2>';
						$detalhes += $(this).children("subdescricao").text();
						$(this).find('catalogo').each(function(){
							$detalhes += '<div id="load"><ul id="down"><li><a href="download/'+$(this).children("arquivo").text()+'">'+$(this).children("nomecatalogo").text()+'</a></li></div><div id="arq"><ul id="icon"><li><a href="'+$(this).children("arquivo").text()+'"><img src="images/'+$(this).children("tipoarquivo").text()+'" ></a></li></ul></div><div class="brclear"></div>'
						 });
						$detalhes += '<div class="brclear"></div><br /><div class="linha"></div><br />';
					});
					$('div#rightcont').html($detalhes);
					$('div#right').slideDown("normal");
				}else{
					$(xml).find('tipo:eq('+index_tipo+') produto:eq('+index_prod+')').each(function(){
						var $detalhes = '';
						if($(this).children("imagem") && $(this).children("imagem").text() != ''){
							$detalhes += '<img class="imgspace2" src="'+$(this).children("imagem").text()+'" />';
						}
						$detalhes += '<h2>'+$(this).children("nome").text()+'</h2>';
						$detalhes += $(this).children("descricao").text();
						$(this).find('catalogo').each(function(){
							$detalhes += '<div id="load"><ul id="down"><li><a href="download/'+$(this).children("arquivo").text()+'">'+$(this).children("nomecatalogo").text()+'</a></li></div><div id="arq"><ul id="icon"><li><a href="'+$(this).children("arquivo").text()+'"><img src="images/'+$(this).children("tipoarquivo").text()+'" ></a></li></ul></div><div class="brclear"></div>'
						 });
						$('div#rightcont').html($detalhes);
						$('div#right').slideDown("normal");
					});
				}
			}
		});
	});
}
