$(document).ready(function(){

	parseFormNoLabels( 'ProdutoBuscaForm' );

	// preloading images
	document.createElement('img').src = './img/globBgDropMenu.png';
	document.createElement('img').src = './img/globalBtnCadastrar.gif';

	$('#CadastroCadastrarForm').each(
		function() {
			$(this).before('<a href="#" id="newsopen">Assine nossa newsletter</a>');
			$(this).hide();
			$('#newsopen').click( function(evt) {
				evt.preventDefault();
				$('#CadastroCadastrarForm').toggle();
			} );
		}
	)

	$('#cadastroMessage').each(
		function() {
			$( this ).appendTo( '#topo' );
			if( $( this ).hasClass( 'error' ) ) $('#CadastroCadastrarForm').toggle();
		}
	);

	$('#navegacao li ul').each(
		function() {
			var li = $(this).parent();

			li.hover(
				function() { $(this).addClass('ativo'); $('ul', this).stop(true,true).fadeIn('fast'); },
				function() { $(this).removeClass('ativo'); $('ul', this).fadeOut('fast'); }
			);

		}
	);



	// so poe os titulos quando todos os css estiverem carregados
	FLASH_TEXT_REPLACEMENT = $('<div id="FLASH_TEXT_REPLACEMENT">').appendTo( 'body' ).css( 'display', 'none' );
	_flashLoadInterval = setInterval( _loadFlashTextReplacement, 30 );
	$('#conteudo').append('<div style="clear:both"></div>');




	/**/
	if( typeof $.prettyPhoto != 'undefined' ) $("#conteudo a[rel^=prettyPhoto]").prettyPhoto({theme:'light_square'});


});



var _loadFlashTextReplacement = function()
{
	if( $('#FLASH_TEXT_REPLACEMENT').height() != 100 ) return; // se mudar no css, mude aqui
	clearInterval( _flashLoadInterval );

	$('h2.realTitle, big.realTitle').css( 'visibility', 'visible' ).each(
		function(){

			$(this).addClass('flash-replaced');

			var txt = this.innerHTML;

			this.innerHTML = '<span class="alt">'+this.innerHTML+'</span>';
			var $alt = $('.alt', this);

			var cor = $(this).css('color');
			if( cor.substring( 0, 3 )=='rgb' ) cor = rgb2hex( cor );

			var fs = $(this).css('fontSize');

			var htmlOptions = {
				'height': $alt.height(),
				'width': $alt.width(),
				'flashvars': {
					'css': [
						'* { color: '+ cor +'; letter-spacing: 0; }',
						'a { color: #000000; text-decoration: none; }',
						'a:hover { text-decoration: underline; }'
					].join(' '),
					'txt': txt,
					'fontSize': fs
				 },

				'version': 7,
				'src': './swf/jfr.swf'
			};

			$(this).flash( htmlOptions );
		}
	)



	$('h3.realTitle').css( 'visibility', 'visible' ).each(
		function() {

			$(this).addClass('flash-replaced');

			var txt = this.innerHTML;

			this.innerHTML = '<span class="alt">'+this.innerHTML+'</span>';
			var $alt = $('.alt', this);


			var cor = $(this).css('color');
			if( cor.substring( 0, 3 )=='rgb' ) cor = rgb2hex( cor );

			var fs = $(this).css('fontSize');

			var htmlOptions = {
				'height': $alt.height(),
				'width': $alt.width(),
				'flashvars': {
					'css': [
						'* { color: '+ cor +'; letter-spacing: 0; }',
						'a { color: #000000; text-decoration: none; }',
						'a:hover { text-decoration: underline; }'
					].join(' '),
					'txt': txt,
					'fontSize': fs
				 },

				'version': 7,
				'src': './swf/jfr.swf'
			};

			$(this).flash( htmlOptions );

		}
	);
}



var parseFormNoLabels = function( $tgt )
{
	$( '#' + $tgt ).addClass('parsed');
	jQuery.each( $( '#' + $tgt + ' label' ), function()
	{
		$d = this.innerHTML.replace(/:?(<([^>]+)>)/ig,"");
		var $inputTgt = $( 'input#'+ this.attributes['for'].value );

		if( $inputTgt.length==0 ) return;

		$inputTgt[0].originalValue = $d;
		if( $inputTgt[0].value=='' ) $inputTgt[0].value = $d;

		$inputTgt.bind("focus", function(e){ if(this.value==this.originalValue) this.value=''; });
		$inputTgt.bind("blur", function(e){ if(this.value=='') this.value=this.originalValue; });

		this.style.display = 'none';
	});
	$( '#' + $tgt ).submit(
		function() {
			$( '#' + $tgt + ' label' ).each(
				function(){
					var el = $( 'input#'+ $(this).attr( 'for' ) )[0];
					if( el ) {
						if( el.value==el.originalValue ) el.value = '';
					}
				});
		}
	);

}

function rgb2hex(rgbString) {

	var parts = rgbString
	        .match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/)
	;

	delete (parts[0]);
	for (var i = 1; i <= 3; ++i) {
	    parts[i] = parseInt(parts[i]).toString(16);
	    if (parts[i].length == 1) parts[i] = '0' + parts[i];
	}
	var hexString = parts.join('');

	return "#" + hexString;
}

