$(document).ready(function() {
	colocaflashs();
	loadsubmenu();
	equalHeight($('.column'));
});
function equalHeight(group) {
	tallest = 0;
	group.each(function() {
		thisHeight = $(this).height();
		if(thisHeight > tallest) {
			tallest = thisHeight;
		}
	});
	group.height(tallest);
}
function colocaflashs(){
	var param = {};
	param.wmode = 'transparent';
	$('.flash').each(function (j) {
		var flashvars = {txt : ($(this).attr('title')?$(this).attr('title'):$(this).html())};
		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);
		}
		$(this).attr('id','flash'+j);
		swfobject.embedSWF('images/flash/titulo.swf', 'flash'+j, wid, hei, '9.0.0', '', flashvars, param);
	})
	$('.texto').show();
	$('#banner').each(function (j) {
		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);
		}
		$(this).attr('id','flashbanner'+j);
		swfobject.embedSWF('images/flash/databanner.swf?v=1', 'flashbanner'+j, wid, hei, '9.0.0', '', {}, param);
	})
	$('#banner_big').each(function (j) {
		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);
		}
		$(this).attr('id','flashbannerbig'+j);
		swfobject.embedSWF('images/flash/databanner.swf?v=1', 'flashbannerbig'+j, wid, hei, '9.0.0', '', {}, param);
	})

}

function mudaestado(obj_est, id_sel_cid, url){
	estado = $(obj_est).val();
	if(isNaN(estado)){
		$('#'+id_sel_cid).html('<option value="">--- selecione o estado ---</option>');
	}else{
		$('#'+id_sel_cid).html('<option value="">carregando....</option>');
		$.get(url,function(data){
			$('#'+id_sel_cid).html('<option value="">--- selecione ---</option>'+data);
		})
	}
}

function loadsubmenu(){
	$.ajax({
		url: "produtos.xml",
		dataType: "xml",
		success: function(xml){
			$tipos = '';
			$(xml).find('tipo').each(function(i){
				$link = "javascript:;";
				if($(this).children("descricao").text() != ''){
					$link = 'prod.php?t='+i;
				}
				$tipos += '<span class="classcategoria"><a href="'+$link+'">'+$(this).children("titulo").text()+'</a>';
				if($(this).find('produto')){
					$tipos += '<div class="listaprod">';
					$(this).find('produto').each(function(j){
						$tipos += '<span><a href="prod.php?t='+i+'&p='+j+'">'+$(this).children('prod_titulo').text()+'</a></span>';
					})
					$tipos += '</div>';
				}
				$tipos += '</span>';
			});
			$("#submenuprod").html($tipos);
			comandosmenu();
		}	
	});
}

function comandosmenu(){
	$("div#mainmenu span a.princ").each(function(){
		$(this).click(function(){
			$(this).parent().children('div.submenu').toggle();
			$('div#mainmenu span').removeClass('selected');
			$(this).parent().addClass('selected');
			$("div#mainmenu span div.submenu").not($(this).parent().children('div.submenu')).each(function(){
				$(this).hide();
			});
		});
	});
	$("div#mainmenu span div span").each(function(){ 
		var elshow = $(this).find('div:first');
		var elhide = $(this).find('div');
		$(this).hover(function(){ 
			var pos = $(this).position();
			elshow.css('top',pos.top+'px')
				.css('left',(pos.left+$(this).width())+'px');		
			elshow.show();
		}, function(){
			if(elhide.parent().attr('class') != 'selected'){
				elhide.hide();
			}
		});
	});
}
function abretipo(idtipo){
	$.ajax({
		url: "produtos.xml",
		dataType: "xml",
		success: function(xml){
			$(xml).find('tipo:eq('+idtipo+')').each(function(){
				$('div#titulodetalhe').html('<div class="flash" title="'+$(this).children('titulo').text()+'"></div>');
				$('div.texto').html($(this).children('descricao').text());
				$('div#foto').html($(this).children('foto').text());
				if($(this).children('folder').length>0){
					$('div.folder').html($(this).children('folder').text());
				}else{
					$('div.folder').remove();
				}
				if($(this).children('manual').length>0){
					$('div.manual').html($(this).children('manual').text());
				}else{
					$('div.manual').remove();
				}
				if($(this).children('datashit').length>0){
					$('div.datashit').html($(this).children('datashit').text());
				}else{
					$('div.datashit').remove();
				}
				//$('div.folder').html($(this).children('folder').text());
				//$('div.manual').html($(this).children('manual').text());
				//$('div.datashit').html($(this).children('datashit').text());
				colocaflashs()				
			});
		}
	});
}
function abreprod(idtipo,idprod){
	$.ajax({
		url: "produtos.xml",
		dataType: "xml",
		success: function(xml){
			$(xml).find('tipo:eq('+idtipo+') produto:eq('+idprod+')').each(function(){
				$('div#titulodetalhe').html('<div class="flash" title="'+$(this).children('prod_titulo').text()+'"></div>');
				$('div.texto').html($(this).children('prod_descricao').text());
				$('div#foto').html($(this).children('prod_foto').text());
				if($(this).children('prod_folder').length>0){
					$('div.folder').html($(this).children('prod_folder').text());
				}else{
					$('div.folder').remove();
				}
				if($(this).children('prod_manual').length>0){
					$('div.manual').html($(this).children('prod_manual').text());
				}else{
					$('div.manual').remove();
				}
				if($(this).children('prod_datashit').length>0){
					$('div.datashit').html($(this).children('prod_datashit').text());
				}else{
					$('div.datashit').remove();
				}
				//$('div.folder').html($(this).children('prod_folder').text());
				//$('div.manual').html($(this).children('prod_manual').text());
				//$('div.datashit').html($(this).children('prod_datashit').text());
				colocaflashs()
			});
		}
	});
}

function MM_findObj(n, d) { //v4.01
  var p,i,x;  if(!d) d=document; if((p=n.indexOf("?"))>0&&parent.frames.length) {
    d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
  if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++) x=d.forms[i][n];
  for(i=0;!x&&d.layers&&i<d.layers.length;i++) x=MM_findObj(n,d.layers[i].document);
  if(!x && d.getElementById) x=d.getElementById(n); return x;
}
function MM_swapImage() { //v3.0
  var i,j=0,x,a=MM_swapImage.arguments; document.MM_sr=new Array; for(i=0;i<(a.length-2);i+=3)
   if ((x=MM_findObj(a[i]))!=null){document.MM_sr[j++]=x; if(!x.oSrc) x.oSrc=x.src; x.src=a[i+2];}
}
function MM_swapImgRestore() { //v3.0
  var i,x,a=document.MM_sr; for(i=0;a&&i<a.length&&(x=a[i])&&x.oSrc;i++) x.src=x.oSrc;
}

function MM_preloadImages() { //v3.0
  var d=document; if(d.images){ if(!d.MM_p) d.MM_p=new Array();
    var i,j=d.MM_p.length,a=MM_preloadImages.arguments; for(i=0; i<a.length; i++)
    if (a[i].indexOf("#")!=0){ d.MM_p[j]=new Image; d.MM_p[j++].src=a[i];}}
} 
