UNCO = {};
UNCO.general = {};
UNCO.wireup = {};

$(document).ready(function(){
	UNCO.wireup.main_menu();
	$("#message_close").click(function(){
		UNCO.general.close_msg();
	});
	 $.beautyOfCode.init({  
      brushes: ['Xml', 'JScript','Plain', 'Php', 'Python']  
    });
	UNCO.general.load_logo();
});


UNCO.general.load_logo = function() {
	var logo_units = "u,u2,u,n,n,n,c,c,c,o,o,o,u,u2,u,n,n2,n,c,c2,c2,o,o2,o,u,u,u,n,n2,n,c,c,c,o,o,o";
	logo_array = logo_units.split(",");
	for(i in logo_array) {
		$("#logo").append("<div class='" + logo_array[i] + "' style='opacity:0'></div>");
	}
	$("#logo div").each(function(){
		$(this).queue(function () {
			$(this).animate( { opacity:"1" }, { duration: ((Math.floor(Math.random()*4)+3)*1000) });
		});
	});
	$("#logo div").dequeue();
};

UNCO.wireup.main_menu = function() {
	$("#main_menu a").not('direct_link').click(function(){
		var href = $(this).attr('href');
		href = href.substring(href.indexOf("#"));
		href = href.replace(/\//g,"");
		$("#foot").fadeOut("slow");
		$("#modules > div:visible").fadeOut("slow", function(){
			//check if module already loaded
			if($("#modules #"+href+"_module").size() < 1 ) {
				//need to create the module
				$("#modules").append("<div id='"+href+"_module'></div>");
				//ajax content
				$.ajax({
					type: "GET",
					url: "http://www.unco.co.nz/motors/"+href+".php",
					data: "mode=ajax",
					success: function(data){
						$("#modules #"+href+"_module").html(data).fadeIn("slow");
						$("#foot").fadeIn("slow");
						//do wireups here
						UNCO.wireup[href]();
					},
					error: function(data){
						console.log(data);
					}
				});
			} else {
				//just show div
				$("#modules #"+href+"_module").fadeIn("slow");
				$("#foot").fadeIn("slow");
			}
			
		});
		
		return false;
	});
	
	$("#main_menu a.direct_link").click(function(){
		alert('spanish coming soon');
		return false;
	});
};

UNCO.wireup.cv = function() {
	//console.log('wireup cv');
};
UNCO.wireup.contact = function() {
	//console.log('wireup contact');
	//get validation script
	$.getScript("/includes/jquery.validate.js", function(){
			//do form validation
		$("#contact_form").validate({
			rules: {
				email: {
					required: true,
					email: true
				},
				subject: "required",
				message: "required"
			},
			messages: {
				email: "&uarr; Please enter a valid email address",
				subject: "&uarr; Please enter a subject",
				message: "&uarr; Please enter a message"
			},
			submitHandler: function(form) {
				dataString = $("#contact_form").serialize();
				$.ajax({
					type: "POST",
					url: "http://www.unco.co.nz/tools/mail.php",
					data: dataString,
					success: function(data){
						$("#contact_form").wrap("<span id='message_sent'></span>");
						$("#message_sent").fadeOut("slow", function(){
							$(this).html("Thanks, your message has been sent!").fadeIn("slow");
						});;
					},
					error: function(data){
						console.log("something went wrong: " + data);
					}
				});
				return false;
   			}
		});

	});
	
	
};
UNCO.wireup.portfolio = function() {
	//console.log('wireup portfolio');
	$(".menu_type_images a").click(function(){
		var href = $(this).attr('href') + "?mode=ajax";
		UNCO.general.msg({"src":href, "success":"UNCO.wireup.project()", "data": "mode=ajax"});
		return false;
	});
};
UNCO.wireup.project = function() {
	//console.log('wireup project');
	$("a.project_imagebox").click(function(){
		var href = $(this).attr('href') + "?mode=ajax";
		UNCO.general.msg({"src":href, "success":"UNCO.wireup.project()", "data": "mode=ajax"});
		return false;
	});

};

UNCO.wireup.greypapers = function() {
	//console.log('wireup greypapers');
};
UNCO.general.msg = function(obj) {
	/*
	obj is a javascript obj
	- html(optional): a string to message eg. html:"hello world"
	- src (optional): a url string to load content with ajax call eg {"src":"http://www.google.com"}
	- success (optional): success function to be called if src is provided
	- time (optional): length of time window should stay open for before closing automatically default is forever
	- width (optional): width of message box default is 300px
	*/
	//$("#message_window").hide();
	$("#message_content").empty();
	if (obj.src) {
		data = obj.data || '';
		$.ajax({
			type: "GET",
			url: obj.src,
			data: data,
			success: function(data) {
				$("#message_content").html(data);
				if (obj.success) {
					eval(obj.success + ";");
				}
			},
			error: function(data) {
				console.log('error from msg:' + data);
			}
		});
	} else if (obj.html) {
		$("#message_content").html(obj.html);
		if (obj.success) {
			eval(obj.success + ";");
		}
	} else {
		$("#message_content").html("I need html or a src");
	}
	if (obj.time > 0) {
		setTimeout('TYVEN.common.close_message()', obj.time);
	}
	if (obj.width) {
		$("#message_box").css("width", obj.width + "px");
	}
	$("#message_window").fadeIn("fast");
};

UNCO.general.close_msg = function() {
	$("#message_window").fadeOut("fast");
	$("#contents").height('100%');
};