ui = {
	initialise : function() {
		debugger;
		Cufon.replace('.header', { fontFamily: 'Comic sans'});
	},
	requiredField : function(id) {
		var el = this.getEl(id);
		if(el.value.length == 0){
			el.style.backgroundColor = "#BFBFBF";
			return false;	
		}
		el.style.backgroundColor = "#ffffff";
		return true;
	},
	getEl : function(id) {
		return document.getElementById(id);
	},
	matchFields : function(id1, id2) {
		return this.getEl(id1).value == this.getEl(id2).value;
	},
	writeError : function(msg, id) {
		id = (id==undefined ? "errorMessage" : id);
		this.getEl(id).innerHTML = msg;
	}
}

user = {
	register : function() {
		try {
			if(!ui.requiredField("firstName")){ throw "First name is a required field"; }	
			if(!ui.requiredField("lastName")){ throw "Last name is a required field"; }	
			if(!ui.requiredField("email")){ throw "Email is a required field"; }	
			if(!ui.requiredField("password")){ throw "Password is a required field"; }
			if(!ui.matchFields("password", "reTypedPassword")) {throw "Passwords don't match";}
		}
		catch(ex) {
			ui.writeError(ex);
			return false;
		}
	},
	login : function() {
		try {
			if(!ui.requiredField("userName")) throw "Email is a required field";
			if(!ui.requiredField("password")) throw "Password is a required field";
		}catch(ex) {
			ui.writeError(ex, "errorMessage");
			return false;
		}
		return true;
	},
	passwordReminder : function(frmId) {
		try {
			if(!ui.requiredField("userName")) throw "Email is a required field";
			//Get the form
			frm = ui.getEl(frmId);
			//Change the action
			frm.action += "&passwordReminder=1";
			//Submit
			frm.submit();
		}catch(ex) {
			ui.writeError(ex, "errorMessage");
			return false;
		}
		return true;
	},
	toggleCourses : function(id) {
		$("#"+id).toggle("slide");
		return false;
	}
}

menu = {
	animating:false,
	init : function() {
		var _fadeSpeed = 400;
		var isChrome = (navigator.userAgent.toLowerCase().indexOf("chrome") > -1 ? true : false) ;
		
		$("#menuItems > ul > li").each( function() { 
			var _opener = $(this);
			var _drop = _opener.find(' > div');
			if(_drop.length) {
				if(isChrome) {
					_drop.css({'margin' : '-22px 0 0 0'});
				}
				_opener.hover(function() {					
						_opener.find(' > a').removeClass('no-hover');
						_drop.fadeIn(_fadeSpeed);
					},
					function() {
						_opener.find(' > a').addClass('no-hover');
						_drop.fadeOut(_fadeSpeed);
					}
				);	
			}
		});
	},
	initTabs : function() {
		var _active = "";
		var _width = 500;
		$("#panelContainer > a").each(function() {
			$(this).hover(function() {
				if(menu.animating)return true;
				
				if(_active != this.id) {
					menu.animating = true;
					if(_active.length > 0) {
						menu.animate(_active, 100, 250, 'easeOutCubic', false);
					}
					
					_active = this.id;
					menu.animate(_active, _width, 1000, 'easeInQuad', true);
				}
			  }
			);
		});
		_active = "first";
		menu.animate(_active, _width, 1000, 'easeInQuad', true);
	},
	animate : function(ctl, width, duration, easing, imgShow, complete) {
//		$("#"+ctl+ " > div").animate({width:width+"px"}, {queue:false, duration: duration, easing: easing}, null, function() {alert(complete);});	
		var currentImg = $("#"+ctl+" > div > .panelImg");
		if(currentImg!=null) {
			(!imgShow ? currentImg.fadeIn(200) :  currentImg.hide(200));
		}
		$("#"+ctl+ " > div").animate({width:width+"px"},duration, easing, function(){
			var img = $("#"+ctl+" > div > .mainImg");
			if(img!=undefined) {
				(imgShow ? img.fadeIn(200) :  img.hide());
			}
			menu.animating = !imgShow;
		});
	}
}

/* Google MAPS
 **************************************************/
//javascript:void(prompt('',gApplication.getMap().getCenter()));
var MapUtil = {
    loaded : false,
    initialize : function (canvas, latLng) {
		try {
			  var myLatlng = new google.maps.LatLng(-26.08663386488555, 27.958756685256958);
			  var myOptions = {
				zoom: 12,
				center: latLng,
				mapTypeId: google.maps.MapTypeId.ROADMAP,
			  }
			  var map = new google.maps.Map(canvas, myOptions);
				
			  var marker = new google.maps.Marker({
				  position: latLng, 
				  map: map, 
				  title:"Compulink"
			  }); 
			  var infoWindow = this.contentWindow();
			  google.maps.event.addListener(marker, 'click', function() {
				  infoWindow.open(map,marker);
				});
			  this.loaded = true;
		}
		catch(e) {
			alert("initializemap: " + e);
		}
   },
   contentWindow : function() {
		var contentString = '<div id="mapContent">'+
			'<h3">Compulink</h3><br/>'+
			'Unit S043C, 1st Floor<br/>'+
			'The Boskruin Village Shopping Centre<br/>'+
			'Cnr Pres. Fouche and Hawken<br/>'+
			'Boskruin<br/>'+
			'Randburg<br/>'+
		'</div>';
		return new google.maps.InfoWindow({content: contentString});
	}
};
