// JavaScript Document

function MainController(option) {
	this.host = '';
	this.currentPage = '';
	this.mainmenu = option.mainmenu;
	this.submenu = option.submenu;
	this.prefix = option.prefix || '';
	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 = '';

	$("#"+ this.mainmenu +" a").each(function(idx) {
		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.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');
	});
}

app.MainController = MainController;