/*PLUGINS VARS*/
var bpos = "";
var perc = 0 ;
var minperc = 0 ;

/*FUNCTIONS*/
function isInt(x) {
	var y = parseInt(x);
	if (isNaN(y)) return false;
	return x==y && x.toString()==y.toString();
}

/*GET ADDRESS*/  
function getEndereco() {
	if($.trim($("#address_zip").val()) != "" ) {
		//$("#address_message").html( '<img src="/skin/img/icons/loading.gif" alt="Loading" />' );
		$("#address_message").css( 'visibility', 'visible' );
		$.getScript("http://cep.republicavirtual.com.br/web_cep.php?formato=javascript&cep="+$("#address_zip").val(), function(){
			if(resultadoCEP["resultado"]){
				$("#address_street").val(unescape(resultadoCEP["tipo_logradouro"])+": "+unescape(resultadoCEP["logradouro"]));
				$("#address_district").val(unescape(resultadoCEP["bairro"]));
				$("#address_city").val(unescape(resultadoCEP["cidade"]));
				$("#address_state").val(unescape(resultadoCEP["uf"]));
				//$("#address_message").html( '' );
			}else{  
				//$("#address_message").html( 'Endereço inválido.' );
			}
			$("#address_message").css( 'visibility', 'hidden' );
		});
	}
}

function updatePasswordBar() {
	//$('#result').html(passwordStrength($('#password').val(),$('#username').val())) ;
	perc = passwordStrengthPercent($('#password').val(),$('#username').val());

	bpos=" $('#colorbar').css( {backgroundPosition: \"0px -" ;
	bpos = bpos + perc + "px";
	bpos = bpos + "\" } );";
	bpos=bpos +" $('#colorbar').css( {width: \"" ;
	bpos = bpos + ((perc * 2)/1.5) + "px";
	bpos = bpos + "\" } );";
	eval(bpos);
	//$('#percent').html(" " + perc  + "% ");
}

$(document).ready(function() {
	/*TOP*/
	$('.top .community img').each(function(){
		$(this).mouseover(function() {
			$('.top .community .message').text( $(this).attr( 'title' ) );
		});
		$(this).mouseout(function() {
			$('.top .community .message').text( '' );
		});
	});
	
	/*MENU*/
	$('.menu .menuItem img').each(function(){
		var str_filename  = $(this).attr( 'src' );
		var str_name      = str_filename.substring( 0, str_filename.length - 4 );
		var str_extension = str_filename.substring( str_filename.length - 4, str_filename.length );
		
		$(this).mouseover(function() {
			$(this).attr( 'src', str_name + '_on' + str_extension );
		});
		$(this).mouseout(function() {
			$(this).attr( 'src', str_name + str_extension );
		});
	});
	
	/*INSTALLED WEBPACKS HOME*/
	$('#portfolioItems img').fadeTo(1,0.4);
	
	$('#portfolioItems img').mouseover(function() {
		$(this).fadeTo(200,1);
	});
	
	$('#portfolioItems img').mouseout(function() {
		$(this).fadeTo(200,0.4);
	});
	
	$('#portfolioItems img').click(function() {
		var str_filename  = $(this).attr( 'src' );
		var str_name      = str_filename.substring( 0, str_filename.length - 4 );
		var str_extension = str_filename.substring( str_filename.length - 4, str_filename.length );
		
		$('#portfolioEnlarged').html( '<img src="' + str_name + '_enlarged' + str_extension + '" />' );
		
		$('#portfolioItems').fadeOut(500);
		$('#portfolioEnlarged').fadeIn(1000);
		$('#portfolioEnlarged').click(function() {
			$('#portfolioEnlarged').fadeOut(500);
			$('#portfolioItems').fadeIn(1000);
		});
	});
	
	/*PORTFOLIO*/
	
	/*ORDERING*/
	$('#products .productSelection img').each(function(){
		var obj_image = $(this).parent().children('img.imageSelect');
		var obj_check = $(this).parent().children(':input[type="checkbox"]');
		if ( obj_check.attr( 'checked' ) ) {
			var str_src = obj_image.attr('src').replace('_unselected','_selected');
			obj_image.attr('src', str_src);
		}
	});
	
	$('#products .productSelection img').click(function(){
		var obj_image = $(this).parent().children('img.imageSelect');
		var obj_check = $(this).parent().children(':input[type="checkbox"]');
		if ( ! obj_check.attr( 'checked' ) ) {
			var str_src = obj_image.attr('src').replace('_unselected','_selected');
			obj_check.attr( 'checked', true );
		} else {
			var str_src = obj_image.attr('src').replace('_selected','_unselected');
			obj_check.attr( 'checked', false );
		}
		obj_image.attr('src', str_src);
	});

	$('.skin').click(function(){
		$(this).children('.skinSelection').children( 'input[type="radio"]' ).attr( 'checked', true );
	});
	
	var boo_skin = false;
	
	$('#skinOptions input[type="radio"]').each(function(){
		if ( $(this).attr( 'checked' ) ) {
			boo_skin = true;
		}
	});
	
	if ( ! boo_skin && $('#skinOptions input[type="radio"]').length > 0 ) {
		//$('#skinOptions input[type="radio"]').eq(0).attr('checked', true);
	}
	
	$('#skinOptions .demo:last').hide();
	$('#skinOptions .money:last').hide();
	
	if ( $("#username").attr( 'name' ) ) {
		$("#username").before( 'http://' );
		$("#username").after( '.mpwaf.com.br' );
		$("#username").tooltip({ 
			top: 3, 
			left: 3 
		});
	}
	
	$(".moduleDescription").tooltip({ 
		top: 3, 
		left: 3 
	});
	/*FORMS*/
	$('input[type="text"],input[type="password"],textarea,select').addClass('idleField');
	$('input[type="text"],input[type="password"],textarea,select').focus(function() {
		$(this).removeClass('idleField').addClass('focusField');
	});
	$('input[type="text"],input[type="password"],textarea,select').blur(function() {
		$(this).removeClass('focusField').addClass('idleField');
	});
	
	$('#password').after( '<br /><h1 class="colorbar" style="height: 3px;font-size:1px;" id="colorbar"></h1>' );
	$('#password').css( {backgroundPosition: "0 0"} );
	$('#username').keyup(function(){
		updatePasswordBar();
	})
	$('#password').keyup(function(){
		updatePasswordBar();
	})
	
	/*PRODUCTS*/
	var int_product = 1;
	$('#products table.productInfo').each(function(){
		if ( int_product == 1 || ! isInt( int_product / 3 ) ) {
			$(this).addClass( 'productAreaSeparated' );
		} else {
			$(this).addClass( 'productArea' );
			$(this).after( '<div style="clear: both; height: 20px;">&nbsp;</div>' );
		}
		int_product++;
	});
});