$(document).ready(function() {
	$('a.delete').click(function() {
		if (jQuery(this).attr('title').indexOf("el ") > -1)
			return confirm('\u00BFDesea borrar '+jQuery(this).attr('title')+' seleccionado?');
		else
			return confirm('\u00BFDesea borrar '+jQuery(this).attr('title')+' seleccionada?');
	}); 
	if($.fn.fancybox) {	
		$("a.fancybox").fancybox({ 'overlayShow': true, 'zoomSpeedIn': 300, 'zoomSpeedOut': 300 });
	}
	$('a[class="external"]').click( function() {
		this.target = "_blank";
	});
	
	if ($('#securedAmount').length > 0) {
		$('#securedAmount').change(function() {
			amountOfSecured = $(this).val();
			$('#secured').html('');
			for(i=1;i<=amountOfSecured;i++) {
				if ($('#lang').val() == 'es') {
					$('#secured').append(
						'<div id="secured'+i+'">'+
						'<h4>Edad del asegurado '+i+'</h4>'+
						'<div class="slider"></div>'+
						'<p class="slider-text"><input type="text" id="amount'+i+'" name="amount'+i+'" value="" /> <span class="years">años</span> <span class="help"></span></p>'+
						'</div>'
					);					
				} else if ($('#lang').val() == 'en') {
					$('#secured').append(
						'<div id="secured'+i+'">'+
						'<h4>Age of Insured '+i+'</h4>'+
						'<div class="slider"></div>'+
						'<p class="slider-text"><input type="text" id="amount'+i+'" name="amount'+i+'" value="" /> <span class="years">años</span> <span class="help"></span></p>'+
						'</div>'
					);							
				} else {
					$('#secured').append(
						'<div id="secured'+i+'">'+
						'<h4>Alter des Versicherten '+i+'</h4>'+
						'<div class="slider"></div>'+
						'<p class="slider-text"><input type="text" id="amount'+i+'" name="amount'+i+'" value="" /> <span class="years">años</span> <span class="help"></span></p>'+
						'</div>'
					);	
				}

				$('#secured'+i+' .slider').slider({
					value:40,
					min:0,
					max:100,
					step: 1,
					slide: function(event, ui) {
						$(this).parent().find('input').val(ui.value);
					}
				});
				$('#secured'+i+' #amount'+i).val($('#secured'+i+' .slider').slider('value'));
			}
		});
	}	
	if ($("#calculadora").length > 0) {
		$("#calculadora").submit(function() { 
			if ($('#securedProduct').val().length == 0) {
				return false;
			}
			var agesArray = new Array();
			if($('#option-eps-trafico').is(':checked')) {
				traficoCheck = 1;
			} else {
				traficoCheck = 0;
			}
			if($('#option-eps-dental').is(':checked') ) {
				dentalCheck = 1;
			} else {
				dentalCheck = 0;
			}

			i = 0;
			$('#secured input').each(function() {
				agesArray[i] = $(this).val();
				i++;
			});
			$.ajax({
				url: "/includes/paymentbyproduct.php",
				async:true,
				contentType: "application/x-www-form-urlencoded",
		        dataType: "html",
				data: ({
					securedAmount : $('#securedAmount').val(),
					securedProduct : $('#securedProduct').val(), 
					ages : agesArray,
					dental : dentalCheck,
					trafico : traficoCheck,
					lang : $('#lang').val()					
				}),
		        global: true,
		        ifModified: false,
		        processData:true,
		        timeout: 3000,
		        type: "POST",
		       	success: function(datos){
					$("#results").html(datos);
				}
			});
			return false;
		});
	}
	if ($('form#form-cuadro-medico').length > 0) {
		$("#form-cuadro-medico").submit(function() { 
			$.ajax({
				url: "/includes/medic-chart.php",
				async:true,
				contentType: "application/x-www-form-urlencoded",
		        dataType: "html",
				data: ({
					str_medic_chart_name : $('#str_medic_chart_name').val(),
					str_medic_type : $('#str_medic_type').val(), 
					str_medic_population : $('#str_medic_population').val(),
					f_page : 0,
					lang : $('#lang').val()
				}),
		        global: true,
		        ifModified: false,
		        processData:true,
		        timeout: 3000,
		        type: "POST",
		       	success: function(datos){
					$("#medic-chart-results").html(datos);
				}
			});
			return false;
		});
		$('#medic-chart-results p.paginacion a').live('click', function(event) {
			tempURL = $(this).attr('href');
			page = tempURL.split('/');
			event.preventDefault();
			$("#medic-chart-results div.loader").show();
			$.ajax({
				url: "/includes/medic-chart.php",
				async:true,
				contentType: "application/x-www-form-urlencoded",
		        dataType: "html",
				data: ({
					str_medic_chart_name : $('#str_medic_chart_name').val(),
					str_medic_type : $('#str_medic_type').val(), 
					str_medic_population : $('#str_medic_population').val(),
					f_page : page[3],
					lang : $('#lang').val()
				}),
		        global: true,
		        ifModified: false,
		        processData:true,
		        timeout: 3000,
		        type: "POST",
		       	success: function(datos){
					$("#medic-chart-results").html(datos);
					$("#medic-chart-results div.loader").hide();
				}
			});
			return false;		
		});
	}
});

function showPreview(coords) {	
	if (parseInt(coords.w) > 0) {
		var rx = 470 / coords.w;
		var ry = 352 / coords.h;

		if ($("#shirt_image_name").length) {
			$('#preview').css({
				width: Math.round(rx * $('#shirt_image_name').width()) + 'px',
				height: Math.round(ry * $('#shirt_image_name').height()) + 'px',
				marginLeft: '-' + Math.round(rx * coords.x) + 'px',
				marginTop: '-' + Math.round(ry * coords.y) + 'px'
			});
		}	
		jQuery('#x').val(coords.x);
		jQuery('#y').val(coords.y);
		jQuery('#w').val(coords.w);
		jQuery('#h').val(coords.h);		
	}
};