var concerto = {
	rootUrl: '',
	includesUrl:'',
	go:function(url, includesUrl) {
		concerto.rootUrl = url;
		concerto.includesUrl = includesUrl;
	},
	form:{
		initialize: function() {
			$(document).ready(function() {
				$('form').submit(function(e) {
					e.preventDefault();
					
					
					var validated = concerto.form.validator.validate();
					if(validated) {
						var submitButton = $('form').find('input[type="submit"]');
						$('form').append('<input type="hidden" name="' + submitButton.attr('name') + '" value="' + submitButton.attr('value') + '"/>');
						$('form')[0].submit();
					}
					
					return false;
				});
				$('#mask').click(function() {
					concerto.ui.dialog.close();
					
				});
				
			});
		},
		validator: {
			checks: [],
			validate: function() {
				var validated = true;
				if(concerto.form.validator.checks.length) {			
					var check;
					for(var i=0;i<concerto.form.validator.checks.length;i++) {
						check = concerto.form.validator.checks[i];
						validated = check();
					}
				}
				return validated;
			}
		}
	},
	ui: {
		tooltip: {
			open: function(params) {
				$('#tooltip').prependTo('#'+params.id).fadeIn();
			}
		},
		dialog: {
			opener:'',
			load: function(params) {
				concerto.ui.dialog.open({mode:'loading'});
				if(!params.data) params.data = '';
				$.ajax({
					data:params.data,
					url:params.url,
					success:function(results) {
						concerto.ui.dialog.open({
							mode:'load-content',
							size:params.size,
							html:results,
							success:params.success
						});
						concerto.ui.dialog.opener = params.opener;
					}
				});
			},
			close:function() {
				$('#modal').fadeOut(function() {
					$(this).html('<div id="modal-contents"></div>');
				});
				$('#mask').fadeOut();
			},
			changeSize: function(size) {
				$('#modal-contents').hide();
				var height = $(window).height();
				var width = $(window).width();
				var top;
				var left;
				var modalHeight;
				var modalWidth;
				switch(size) {
					case 'large': // 800 x 500
						top = (height/2) - 251;
						left = (width/2) - 401;
						modalHeight = 500;
						modalWidth = 800;
						break;
					case 'medium': // 500 x 300
						top = (height/2) - 151;
						left = (width/2) - 251;
						modalHeight = 300;
						modalWidth = 500;
						break;
					case 'small': // 300 x 200
						top = (height/2) - 101;
						left = (width/2) - 151;
						modalHeight = 200;
						modalWidth = 300;
						break;
					case 'medium-large': //800 x 400
						top = (height/2) - 201;
						left = (width/2) - 401;
						modalHeight = 400;
						modalWidth = 800;
						break;
						
				}
				$('#modal').animate({
					'top':top + 'px',
					'left':left + 'px',
					'height':modalHeight + 'px',
					'width':modalWidth + 'px'
					
				}, function() {
					$('#modal-contents').fadeIn();
				});
			},
			open: function(options) {
				
				var height = $(window).height();
				var width = $(window).width();
				var top;
				var left;
				var modalHeight;
				var modalWidth;
				switch(options.mode) {
					case 'loading':
						$('#mask').fadeIn();
						
						top = (height/2) - 51;
						left = (width/2) - 151;
						
						$('#modal').css('height', '100px').css('width', '300px').css('top', top + 'px').css('left', left + 'px');
						$('#modal-contents').html('<div class="ajax-loader">Loading...</div>');
						$('#modal').fadeIn();
						break;
					case 'display-content':
						$('#mask').fadeIn();
						top = (height/2) - 51;
						left = (width/2) - 151;
						
						$('#modal').css('height', '100px').css('width', '300px').css('top', top + 'px').css('left', left + 'px');
						switch(options.size) {
							case 'large': // 700 x 400
								top = (height/2) - 201;
								left = (width/2) - 251;
								modalHeight = 400;
								modalWidth = 700;
								break;
							case 'medium': // 500 x 300
								top = (height/2) - 151;
								left = (width/2) - 251;
								modalHeight = 300;
								modalWidth = 500;
								break;
							case 'small': // 300 x 200
								top = (height/2) - 101;
								left = (width/2) - 151;
								modalHeight = 200;
								modalWidth = 300;
								break;
							case 'smallest': // 300 x 100
								top = (height/2) - 51;
								left = (width/2) - 151;
								modalHeight = 100;
								modalWidth = 300;
								break;
								
						}
						if(options.size != 'smallest') {
							$('#modal').animate({
								'top':top + 'px',
								'left':left + 'px',
								'height':modalHeight + 'px',
								'width':modalWidth + 'px'
								
							}, function() {
								$('#modal').fadeIn();
								$('#modal-contents').html(options.html).fadeIn();
								if(options.success) {
									options.success();
								}
							});
						}
						else {
							$('#modal').fadeIn();
							$('#modal-contents').html(options.html).fadeIn();
							if(options.success) {
								options.success();
							}
							
						}
						concerto.ui.dialog.opener = options.opener;
						break;
					case 'load-content':
						$('#modal-contents').fadeOut();
						switch(options.size) {
							case 'large': // 800 x 500
								top = (height/2) - 251;
								left = (width/2) - 401;
								modalHeight = 500;
								modalWidth = 800;
								break;
							case 'medium': // 500 x 300
								top = (height/2) - 151;
								left = (width/2) - 251;
								modalHeight = 300;
								modalWidth = 500;
								break;
							case 'small': // 300 x 200
								top = (height/2) - 101;
								left = (width/2) - 151;
								modalHeight = 200;
								modalWidth = 300;
								break;
							case 'medium-large': //800 x 400
								top = (height/2) - 201;
								left = (width/2) - 401;
								modalHeight = 400;
								modalWidth = 800;
								break;
							case 'smallest': // 300 x 100
								top = (height/2) - 51;
								left = (width/2) - 151;
								modalHeight = 100;
								modalWidth = 300;
								break;
								
						}
						if(options.size != 'smallest') {
							$('#modal').animate({
								'top':top + 'px',
								'left':left + 'px',
								'height':modalHeight + 'px',
								'width':modalWidth + 'px'
								
							}, function() {
								$('#modal-contents').html(options.html).fadeIn();
								if(options.success) {
									options.success();
								}
							});
						}
						else {
							$('#modal-contents').html(options.html).fadeIn();
							if(options.success) {
								options.success();
							}
						}
						break;
				}
			}
		}
	}


};
