// consertar flicker de bg image:hover no IE6
try {document.execCommand("BackgroundImageCache", false, true);} catch(err) {}

$().ready(function(){
	
	var _lang = location.pathname.match(/$\/(\w\w)/);
	_lang = _lang && _lang[1] ? _lang[1] : 'pt';
	
	$.browser.IE6 = $.browser.msie && $.browser.version <= 6;
	
	// fix tamanho do drop-down do select no topo
	if ($.browser.IE6){
		var clicked = false;
		$('#principal select')
			.bind('mouseover', function(){
				$(this).css('width', 'auto');
			}).bind('click', function(){
				clicked = true;
			}).bind('blur change', function(){
				$(this).css('width','');
				clicked = false;
			}).bind('mouseout', function(){
				if (!clicked)
					$(this).css('width', '');
			});
	} else {
		$('#principal select')
			.bind('mousedown', function(){
				$(this).css('width', 'auto');
			}).bind('change blur', function(){
				$(this).css('width','');
			});
	}
	
	
	//links selects
	$('.selects > select').change(function(){
		if(this.value != '')
			window.location=(lang == 'pt' ? '' : '/'+_lang+'/')+"associado.php?id="+this.value;
	});
	
	// galeria de fotos sidebar
	if (jQuery.fn.slimbox){
		$('.col-side.galeria a').slimbox({
			previousKeys: [37,65],
			nextKeys: [39,80],
			counterText: "Imagem {x} de {y}"
		});
	};

	// documentos
	var $docs = $('.documentos .categoria ');
	var cmd =  {
		pt: ['+ Abrir', '- Fechar'],
		en: ['+ Open', '- Close'],
		es: ['+ Abrir', '- Cerrar'],
		it: ['+ Aprire', '- Chiudere']
	}[_lang];
	$docs.each(function(){
		var $p = $(this).find('ul');
		$(this)
			.append( $('<span class="control"/>').text(cmd[0]) )
			.find('.control')
				.click(function(){
					var self = $(this);
					$p.toggle();
					self.text( $p.is(':visible') ? cmd[1] : cmd[0]);
				});
	});
	
	$docs.filter(':gt(0)').find('ul').hide()
	$docs.filter(':first').find('.control').text(cmd[1]);

	// associados
	var $ass = $('.associados .categoria');
	
	$ass.each(function(){
		var $p = $(this).find('table');
		$(this)
			.append( $('<span class="control"/>').text(cmd[0]) )
			.find('.control')
				.click(function(){
					var self = $(this);
					$p.toggle();
					self.text( $p.is(':visible') ? cmd[1] : cmd[0]);
				});
	});
	
	$ass.filter(':gt(0)').find('table').hide()
	$ass.filter(':first').find('.control').text(cmd[1]);

	// estilizar input de arquivo no form contato/associe-se, exceto IE6
	if (!$.browser.IE6)
	{
		var
			$ia = $('#input-arquivo'),
			$file = $ia.find('.file'),
			txt = $file.attr('title'),
			$replace = $('<input type="text" class="text arq-replace" />');
		
		$ia
			.append( $replace )
			.append('<button class="enviar arq-replace2">'+txt+'</button>')
			
		$file
			.css({ opacity:0, position:'absolute', top:0, right:0, zIndex:3 })
			.change(function(){
				$replace.val( $(this).val() );
			});
	}
	else
	{
		// esconder noticias sem imagem do IE6
		$('.noticias-capa img[src$="img/noticias/home_thumb_"]').hide();
	};
	

});

$(window).load(function(){
	
	$.get('imagens.xml', function(data){
		
		var imagens = $(data).find('imagem').get();
		
		var
			i = 1,
			ln = imagens.length,
			$fotos = $('div.slideshow'),
			$info = $fotos.find('.info'),
			$info_cidade = $info.find('strong'),
			$info_local = $info.find('span'),
			$info_texto = $info.find('p');
		
		function slideshow(){
			$('<img />')
				.hide()
				.prependTo( $fotos )
				.load(function(){
					var $t = $(this);
					
					// substituir imagem
					$t.next('img').stop().fadeOut(400, function(){
						$(this).remove();
						$t.fadeIn(600, function(){
							i++;
							if (i==ln) i = 0;
						});
						if ($fotos.is('#fotos-topo'))
							$t.animate({
								top: 0
						}, 12000, 'linear');
					});
						
					// substituir texto
					$info.fadeOut(300, function(){
						$info_cidade.text( $(imagens[i]).find('cidade').text() );
						$info_local.text( $(imagens[i]).find('local').text() );
						$info_texto.text( '> ' + $(imagens[i]).find('texto').text() );
					}).fadeIn(300);
					
					// proximo slide
					setTimeout(slideshow, 6000);
				})
				.attr('src', $(imagens[i]).attr('url') );
		};
		
		setTimeout(slideshow, 6000);
		
	}, 'xml');

});