// JavaScript Document



function MainController(option) {

	this.host = '';

	this.currentPage = '';

	this.mainmenu = option.mainmenu;

	this.mnu_option = option.mnu_option;

	this.mnu_pos_x = option.mnu_pos_x;

	this.mnu_pos_y = option.mnu_pos_y;

	this.prefix = option.prefix || '';

	this.temp_path = '';

//	this.temp_path = '/ceamv2';

	this.exception = option.exception || new Array();

	this.init();

}





MainController.prototype.init = function() {

	this.processHost();

	this.setCurrentPage();

}



MainController.prototype.getHost = function() {

	return this.host;

}



MainController.prototype.processHost = function() {

	var pathname = document.location.pathname;

	if(pathname.search(/\/$/) == -1 && pathname.search(/php$/) == -1)

		pathname = pathname + '/';

	this.host = document.location.protocol + '//' + document.location.host + pathname;

	this.host = this.host.replace(/\/[^\/]*.php$/,'/');

}



MainController.prototype.getCurrentPage = function() {

	return this.currentPage;

}



MainController.prototype.setCurrentPage = function() {

	var finalpath = '';

	var pathname = document.location.pathname;

	found = pathname.toLowerCase().match(/\/([^\/]*.php)/);



	if(found) {

		finalpath = found[1];

		for(var i = 0; i < this.exception.length; i++) {

			if(finalpath == this.exception[i].key + ".php")

				finalpath = this.exception[i].value + ".php";

		}

	}

	if(pathname == this.temp_path + '/') {

		finalpath = 'index.php';

	}



	this.currentPage = finalpath;

}



MainController.prototype.getCurrentId = function() {

	var finalId = '';

	var currentPage = this.getCurrentPage();

	var found = currentPage.match(/(.*).php/);



	if(found)

		finalId = found[1];

	

	return this.prefix + finalId;

}



MainController.prototype.processMenu = function() {

	var currentPage = this.getCurrentPage();

	var currentId = this.getCurrentId();

	var anchor_href = '';

	var content = '';

	var currentImg = '';

	var obj = this;



	$("#"+ this.mainmenu +" a").each(function(idx) {

		anchor_href = $(this).attr("href");

		if(currentPage == anchor_href) {

			tmp_position = obj.getPosition(currentPage) + 'px ' + obj.mnu_pos_y + 'px';

			$(this).css({'backgroundPosition' : tmp_position});

		}

/*		anchor_href = $(this).attr("href");

		if( currentPage == anchor_href ) {

			currentImg = $("#" + currentId + " img").attr("src");

			if(currentImg.search(/_on/) == -1) {

				currentImg = currentImg.replace(/\.jpg/,'_on.jpg');

				$("#" + currentId + " img").attr("src",currentImg);

			}

		}*/

	});



}



MainController.prototype.getPosition = function(id) {

	var tmp_pos = 0;

	var local_id = id.substring(0,(id.length - 4));

	var count = 0;

	while(count < this.mnu_option.length) {

		if(local_id == this.mnu_option[count]) {

			tmp_pos = this.mnu_pos_x[count];

			break;

		}

		count++;

	}

	return tmp_pos;

}



MainController.prototype.manageSubMenu = function(id) {

	$("#" + this.prefix + id).mouseover(function() {

//		$("#sub_" + id).slideDown('fast');

		$("#sub_" + id).css('display','block');

	});

	$("#" + this.prefix + id).mouseleave(function() {

//		$("#sub_" + id).slideUp('fast');

		$("#sub_" + id).css('display','none');

	});

	$("#sub_" + id).mouseleave(function() {

//		$(this).slideUp('fast');

		$(this).css('display','none');

	});

	$("#" + this.mainmenu).mouseleave(function() {

//		$("#sub_" + id).slideUp('fast');

		$("#sub_" + id).css('display','none');

	});

}



MainController.prototype.changeLinks = function() {

	var tmp_url = '';

	var re = /http/;

	var local_host = this.getHost();



	$("a").each(function(idx) {

		if($(this).attr('href').search(re) == -1) {

			tmp_url = local_host + $(this).attr('href');

			$(this).attr('href', tmp_url);

		}

	});

}



MainController.prototype.contactUs = function(winpopup, content, btn_close, form_f) {

	var form  = document.getElementById(form_f);

	var send  = Spry.Widget.Form.validate(form);

	var data = '';

	

	if(send) {

		data += $("#" + form_f).serialize();

		data += "&action=contactUs";

		$.post("_task.php", data,function(data){

			if(data.status) {

				$("#" + winpopup).fadeIn('fast');

				form.reset();

			}

			else {

				$("#" + content).html(data.msg)

				$("#" + winpopup).fadeIn('fast');

			}

		}, "json");

	}

	

	$("#" + btn_close).click(function() {

		$("#" + winpopup).fadeOut('fast');

	});

}



MainController.prototype.admissionCard = function(winpopup, content, btn_close, form_f) {

	var form  = document.getElementById(form_f);

	var send  = Spry.Widget.Form.validate(form);

	var data = '';

	

	if(send) {

		data += $("#" + form_f).serialize();

		data += "&action=admissionCard";

		$.post("_task.php", data,function(data){

			if(data.status) {

				$("#" + winpopup).fadeIn('fast');

				form.reset();

			}

			else {

				$("#" + content).html(data.msg)

				$("#" + winpopup).fadeIn('fast');

			}

		}, "json");

	}

	

	$("#" + btn_close).click(function() {

		$("#" + winpopup).fadeOut('fast');

	});

}



MainController.prototype.cardFreeCourse = function(winpopup, content, btn_close, form_f) {

	var form  = document.getElementById(form_f);

	var send  = Spry.Widget.Form.validate(form);

	var data = '';

	

	if(send) {

		data += $("#" + form_f).serialize();

		data += "&action=cardFreeCourse";

		$.post("_task.php", data,function(data){

			if(data.status) {

				$("#" + winpopup).fadeIn('fast');

				form.reset();

			}

			else {

				$("#" + content).html(data.msg)

				$("#" + winpopup).fadeIn('fast');

			}

		}, "json");

	}

	

	$("#" + btn_close).click(function() {

		$("#" + winpopup).fadeOut('fast');

	});

}





/***Modificado por Alex 06/05/2011  11:10am**/





MainController.prototype.sendAdmision = function() {



	

	

	var form  = document.getElementById('form_inscrip2');

	var send  = Spry.Widget.Form.validate(form);

	var data = '';

	

	if(send) {

	

		data += $("#form_inscrip2").serialize();

		

		$.post("email.php", data,function(data){

			

			

				$("#p_msj").fadeIn('fast');

				form.reset();

			

			

		}, "json");

	}

	

		

	$("#b_msj").click(function() {

		$("#p_msj").fadeOut('fast');

	});	

		

}






MainController.prototype.sendDiplomado = function() {



	

	

	var form  = document.getElementById('form_inscrip2');

	var send  = Spry.Widget.Form.validate(form);

	var data = '';

	

	if(send) {

	

		data += $("#form_inscrip2").serialize();

		

		$.post("email_diplomado.php", data,function(data){

			

			

				$("#p_msj").fadeIn('fast');

				form.reset();

			

			

		}, "json");

	}

	

		

	$("#b_msj").click(function() {

		$("#p_msj").fadeOut('fast');

	});	

		

}




app.MainController = MainController;
