

$(document).ready(function() {
	
	/*
	 * Ventanas
	 */
	createDialogs();
	 
	centerDialogs();
	$(window).bind('resize', function () {
		centerDialogs();
	});
	
	$('#menu_1').bind('click', function () {
		showIndex();
	});
	
	$('#menu_2').bind('click', function () {
		showKnow();
	});	
	
	$('#menu_3, #btn_buy').bind('click', function () {
		showBuy();
	});
	
	$('#menu_4').bind('click', function () {
		showProducts();
	});
	
	$('#promo, #participa').bind('click', function () {
		showSurvey();
	});
	
	$('#btn_kms, #survey_kms').bind('click', function () {
		showKms();
	});
	
	$('#btn_rusia, #survey_rusia').bind('click', function () {
		showRusia();
	});
	
	$('#survey_bases').bind('click', function () {
		showBases();
	});
	
	$('.dlg_header_close,.dlg_close').bind('click', function () {
		hideDialog(this);
	});
	
	$('#legalBtn').bind('click', function () {
		showDialog('#dlg_legal');
	});
	
	$('.buy_type_box').bind('click', function () {
		checkOption('box');
	});
	
	$('.buy_type_online').bind('click', function () {
		checkOption('online');
	});
	
});

/*
 * TAB
 */

function showIndex() {
	$("#tab_2:visible, #tab_4:visible").hide();
	$("#tab_1").fadeIn("slow");
}

function showKnow() {
	$("#tab_1:visible, #tab_4:visible").hide();
	$("#tab_2").fadeIn("slow");
}

function showBuy() {
	showDialog('#dlg_buy');
}

function showProducts() {
	$("#tab_1:visible, #tab_2:visible").hide();
	$("#tab_4").fadeIn("slow");
}

/*
 * Encuesta
 */

function showSurvey() {
	showDialog('#dlg_survey');
	
	$.ajax({
		url: "_survey.php",
		cache: false,
		success: function(html){
			$("#dlg_survey #survey_quest").html(html);
		}
	});
}

function sendSurvey() {
	$.ajax({
		url: "_survey.php",
		type: "post",
		cache: false,
		data: $("#surveyF").serialize(),
		success: function(html){
			$("#dlg_survey #survey_quest").html(html);
		}
	});
}

/*
 * Compra
 */

function checkOption(value) {
	$('#type').val(value);

	if ($('#type').val() == "box") {
		$('.buy_type_box').addClass('selected');
	} else {
		$('.buy_type_box').removeClass('selected');
	}
	
	if ($('#type').val() == 'online') {
		$('.buy_type_online').addClass('selected');
	} else {
		$('.buy_type_online').removeClass('selected');
	}
}
 

/*
 * VENTANAS VARIAS
 */

function showKms() {
	showDialog("#dlg_kms");
}

function showRusia() {
	showDialog("#dlg_rusia");
}

function showBases() {
	showDialog("#dlg_bases");
}









function centerDialogs() {
	$('.center').each(function() {
		centerDialog(this);
	});
}

function centerDialog(id) {
	var wnd = $(window), obj = $(id);
	var top  = $(document).scrollTop()  + ((wnd.height() / 2) - (obj.height() / 2));
	var left = $(document).scrollLeft() + ((wnd.width()  / 2) - (obj.width()  / 2));
	top = top < $(document).scrollTop() ? $(document).scrollTop() : top;
	obj.css({top: top, left: left});
}

function showMiniDialog(id) {
	$("#cortina2").fadeTo(0, 0.70, function() {
		$(this).show();
		centerDialog(id);
		$(id).fadeIn("fast");
	});
}

/*
 * Dialogos
 */

function createDialogs() {
	$('.dialog').each(function() {
		$(this)
			.wrapInner(
					"<table class='dialog_container'>" + 
					"<tr><td align='center'>" +
					"<div class='dialog_box'></div>" +
					"</td></tr></table>"
				)
			.prepend("<div class='cortina'></div>");
	});
	
	/*
	$('.dialog h2').prepend(
			"<button class='close' type='button' onclick='hideDialog(this)'>cerrar</button>"
		);
	*/
}

function showDialog(id) {
	$(id).fadeIn("fast");
}

function hideDialog(id) {
	$(id).parents('.dialog').hide();
}









function hideMiniDialog(id) {
	$(id).fadeOut("slow", function() {
		$("#cortina2").hide();
	});
}