var Utils = {
	Popup: function(url, height, width) {
		var win = window.open(url, 'janela', 'height=' + height + ', width=' + width);
	}
}

var User = {
	Management: {
		updateUserForm: function() {
			if($('#NewUserIsCompany').attr('checked') == true)
				$('#companyDetails').show();
			else
				$('#companyDetails').hide();
		}
	}
}

var Ad = {
  Listing: {
	showRecommendFriend: function() {
		$('div#recommendFriend').show("slow");
	}
  },

  SearchForm: {
  	updateSpecial: function(category_id) {
  		if(category_id == 7 || category_id == "7") {
  			$('div#automobile').show();
  			$('div#realestate').hide();
  			Ad.CreationForm.updateAutomobileDescription();
  		}
  		else if(category_id == 6 || category_id == "6") {
  			$('div#automobile').hide();
  			$('div#realestate').show();
  			Ad.CreationForm.updateRealestateDescription();
  			
  		}
  		else {
  			$('div#automobile').hide();
  			$('div#realestate').hide();
  			$('#AdExtras').val('');
  		}
  	
  	}
  
  },

  CreationForm: {
  	currentPaperOption: 0,
  	currentOption: 0,
  	updateForm: function(selectedOption) {
  		switch(selectedOption) {
  			case '1':
  			case '2':
  				$('div#photos').hide();
  				break;
  			case '3':
  			case '4':
  				$('div#photos').show();
  				break;
  		}
  		Ad.CreationForm.currentOption = selectedOption;
  		Ad.CreationForm.updatePrice();
  	},
  	updatePaperForm: function(selectedOption) {
  		if (selectedOption < 5) $('div#photos').hide();
  		else $('div#photos').show();
  		Ad.CreationForm.currentPaperOption = selectedOption;
  		Ad.CreationForm.updatePrice();
  	},
  	updatePrice: function() {
  		var basePrice = 0;
  		switch(Ad.CreationForm.currentPaperOption) {
  			case '1':
  				basePrice = 6;
  				break;
  			case '2':
  				basePrice = 8;
  				break;
  			case '3':
  				basePrice = 10;
  				break;
  			case '4':
  				basePrice = 12;
  				break;
  			case '5':
  				basePrice = 15;
  				break;
  			default: basePrice = 6;
  		}
  		var duration = $('select#AdDuration').val();
  		if(duration == '') duration = 1;
  		
  		var size =  $.trim($('#AdDescription').val()).length;
  		var extraLines = 0;
  		var extra = 0;
		if (size > 128) extraLines = parseInt(Math.floor((size - 128) / 16.0), 10);
		switch(Ad.CreationForm.currentPaperOption) {
			case '1': extra = 0.5; break;
			case '2': extra = 0.75; break;
			case '3': extra = 1; break;
			case '4': extra = 1.5; break;
			case '5': extra = 2; break;
			default: extra = 0.5;
		}
		if (Ad.CreationForm.currentPaperOption == 1 && size <= 32) finalPrice = 0;
		else finalPrice = duration * basePrice + extraLines * extra;

  		$('span#price-value').text(finalPrice + '');
  	},
  	
  	updateSpecial: function(category_id) {
  		if(category_id == 7 || category_id == "7") {
  			$('div#automobile').show();
  			$('div#realestate').hide();
  			Ad.CreationForm.updateAutomobileDescription();
  		}
  		else if(category_id == 6 || category_id == "6") {
  			$('div#automobile').hide();
  			$('div#realestate').show();
  			Ad.CreationForm.updateRealestateDescription();
  			
  		}
  		else {
  			$('div#automobile').hide();
  			$('div#realestate').hide();
  			$('#AdExtras').val('');
  		}
  	
  	},
  	updateAutomobileDescription: function() {
  		var html = '<table id="ad-special-details"><tr><th colspan="2">Detalhes</th></tr>';
  		if($('#AutomobileBrand').val() != null && $('#AutomobileBrand').val() != '') html += '<tr><td class="title">Marca / Modelo:</td><td class="data">' + $('#AutomobileBrand').val() + '</td></tr>';
		
		if($('#AutomobileYear').val() != '' && $('#AutomobileMonth').val() != '') html += '<tr><td class="title">Ano/Mes:</td><td class="data">' + $('#AutomobileYear').val() + '/' + $('#AutomobileMonth').val() + '</td></tr>';
		else if($('#AutomobileYear').val() != '' && $('#AutomobileMonth').val() == '') html += '<tr><td class="title">Ano:</td><td class="data">' + $('#AutomobileYear').val() + '</td></tr>';
		
		if($('#AutomobileMileage').val() != '') html += '<tr><td class="title">Quilometragem:</td><td class="data">' + $('#AutomobileMileage').val() + '</td></tr>';
		if($('#AutomobileFuelType').val() != null && $('#AutomobileFuelType').val() != '') html += '<tr><td class="title">Tipo de Combust&iacute;vel:</td><td class="data">' + $('#AutomobileFuelType').val() + '</td></tr>';
  		if($('#AutomobileColor').val() != null && $('#AutomobileColor').val() != '') html += '<tr><td class="title">Cor:</td><td class="data">' + $('#AutomobileColor').val() + '</td></tr>';
  		if($('#AutomobileGearBox').val() != null && $('#AutomobileGearBox').val() != '') html += '<tr><td class="title">Transmiss&atilde;o:</td><td class="data"> ' + $('#AutomobileGearBox').val() + '</td></tr>';
		if($('#AutomobilePrice').val() != '') html += '<tr><td class="title">Preco:</td><td class="data">' + $('#AutomobilePrice').val() + '&euro;</td></tr>';
		if($('#AutomobilePotency').val() != '') html += '<tr><td class="title">Potencia:</td><td class="data">' + $('#AutomobilePotency').val() + ' cv.</td></tr>';
		if($('#AutomobileCilinders').val() != '') html += '<tr><td class="title">Cilindrada:</td><td class="data">' + $('#AutomobileCilinders').val() + ' cm<sup>3</sup></td></tr>';
		html += '<tr><th colspan="2">Equipamento</th></tr><tr><td class="data" colspan="2">';
		var extras = 0;
		if($('#AutomobileExtras1').attr('checked') == true) { extras++; html += 'Ar Condicionado';}
		if($('#AutomobileExtras2').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Fecho Centralizado';}
		if($('#AutomobileExtras3').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Estofos em Pele';}
		if($('#AutomobileExtras4').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Leitor CD';}
		if($('#AutomobileExtras5').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Bancos Ajustaveis';}
		if($('#AutomobileExtras6').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'ABS';}
		if($('#AutomobileExtras7').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Vidros Electricos'; }
		if($('#AutomobileExtras8').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Far&oacute;is de Xenon';}
		if($('#AutomobileExtras9').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Sistema de Navega&ccedil;&atilde;o (GPS)';}
		if($('#AutomobileExtras10').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Pintura Metalizada'; }
		if($('#AutomobileExtras11').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Garantia'; }
		if($('#AutomobileExtras12').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Jantes Liga Leve'; }
		if($('#AutomobileExtras13').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Farois de Nevoeiro'; }
		if($('#AutomobileExtras14').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Volante Regulavel'; }
		if($('#AutomobileExtras15').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Volante Multifuncoes'; }
		if($('#AutomobileExtras16').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Estofos em Pele Aquecidos'; }
		if($('#AutomobileExtras17').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Sensores de Parqueamento'; }
		if($('#AutomobileExtras18').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Cruise Control'; }
		if($('#AutomobileExtras19').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Direccao Assistida'; }
		if($('#AutomobileExtras20').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Espelhos ElÃ©ctricos'; }
		if($('#AutomobileExtras21').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Pre&ccedil;o sem retoma'; }
		if($('#AutomobileExtras22').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Espelhos Aquecidos'; }
		if($('#AutomobileExtras23').attr('checked') == true) { if(extras > 0) html += ', '; extras++; html += 'Airbags Frontais'; }
		if(extras == 0) {
			html += '- Sem extras';
		}
		html += '</td></tr></table>';
		$('#AdExtras').val(html);
  	},
  	
  	updateRealestateDescription: function() {
  		
  		
  		var html = '';
  		if($('#RealestateType').val() != null && $('#RealestateType').val() != '') html += '<strong>Tipo de Imovel:</strong> ' + $('#RealestateType').val();
  		if($('#RealestateTopology').val() != null && $('#RealestateTopology').val() != '') html += ' (' + $('#RealestateTopology').val() + ')';
  		html += '<br />'
  		if($('#RealestateBussiness').val() != null && $('#RealestateBussiness').val() != '') html += '<strong>Tipo de Negocio:</strong> ' + $('#RealestateBussiness').val() + '<br />';
		if($('#RealestatePrice').val() != '') html += '<strong>Preco:</strong> ' + $('#RealestatePrice').val() + '&euro;<br />';
		if($('#RealestateArea').val() != '') html += '<strong>Area coberta:</strong> ' + $('#RealestateArea').val() + 'm<sup>2</sup><br />';
		if($('#RealestateAreaUncovered').val() != '') html += '<strong>Area descoberta:</strong> ' + $('#RealestateAreaUncovered').val() + 'm<sup>2</sup><br />';
		html += '<br />';
		html += '<strong>Extras:</strong><br />';
		var extras = 0;
		if($('#RealestateExtras1').attr('checked') == true) { extras++; html += '- Condominio Fechado<br />'; }
		if($('#RealestateExtras2').attr('checked') == true) { extras++; html += '- Seguranca<br />'; }
		if($('#RealestateExtras3').attr('checked') == true) { extras++; html += '- Area Comum<br />'; }
		if($('#RealestateExtras4').attr('checked') == true) { extras++; html += '- Transportes Publicos<br />'; }
		if($('#RealestateExtras5').attr('checked') == true) { extras++; html += '- Estacionamento<br />'; }
		if($('#RealestateExtras6').attr('checked') == true) { extras++; html += '- Vista para o Mar<br />'; }
		if($('#RealestateExtras7').attr('checked') == true) { extras++; html += '- Aquecimento Central<br />'; }
		if($('#RealestateExtras8').attr('checked') == true) { extras++; html += '- Piscina<br />'; }
		if($('#RealestateExtras9').attr('checked') == true) { extras++; html += '- Quintal<br />'; }
		if(extras == 0) {
			html += '- Sem extras';
		}
		$('#AdExtras').val(html);
		
		var realestateType = $('select#RealestateType').val();
		if(realestateType != "Terreno" && realestateType != "Lote" && realestateType != "Pastagem" && realestateType != "Armazéns") {
			$('.tipologiaHolder').show();
		}
		else {
			$('.tipologiaHolder').hide();	
		}
  	}  	
  }
};

