// It's good to define all of our stuff within a namespace (CAD) to keep it
// organized and so it never conflicts with other libraries. Here, we define
// the name space if it isn't already.
if (typeof(CAD) == 'undefined') { CAD = {}; }

// This code will get run immediately when the page loads. What we are doing here
// is making a function and then immediately calling that function. The reason we
// do that is that it keeps any 'global' variables we need on this page limited to
// the scope of this function.
(function(){
	// Within the CAD namespace, we make a namespace specifically for this
	// page, again, just for organization and to avoid name conflicts.
	CAD.portfolio = {
		// Keeps track of the current selected client
		client: '',
		client_num: undefined,

		// Keeps track of the current selected case study
		case_num: 1,

		setVert: function(client_num) {
			var client = $('#hidden_content').find('.client').eq(client_num-1);
			if (client) {
				var vert_image = client.find('.vert_image');
				$('#vert_image').css('display', 'block').attr('src', vert_image.attr('src'));
				return true;
			} else {
				return false;
			}
		},

		clearVert: function() {
			$('#vert_image').css('display', 'none');
		},

		setHomeStage: function(show) {
			if (show) {
				$('#stage').css('display', 'none');
				$('#home_stage').css('display', 'block');
			} else {
				$('#stage').css('display', 'block');
				$('#home_stage').css('display', 'none');
			}
		},

		// Sets the current client (which is passed in as a string) and loads
		// data for that client such as their name, first case study and image.
		setClient: function(client_num) {
			// Don't reset everything if this client is already selected
			if (CAD.portfolio.client_num == client_num) {
				return false; 
			}
			var client = $('#hidden_content').find('.client').eq(client_num-1);
			if (client) {
				CAD.portfolio.client_num = client_num;
				CAD.portfolio.client = client;
				var client_name = client.find('.client_name').text();
				$('#client_name').text(client_name);
				CAD.portfolio.setCaseStudyNum(1);
				return true;
			} else {
				return false;
			}
		},

		// Sets the case study number to display and causes it to be displayed.
		// Case study nums start with 1 (not 0). If the case study trying to be
		// loaded does not exist, no change is made and the function returns
		// false.
		setCaseStudyNum: function(num) {
			if (!CAD.portfolio.client) {
				// the client must be loaded prior to calling this 
				return false;
			}
			if (num < 1) {
				// case nums start at 1
				return false;
			}
			CAD.portfolio.setHomeStage(false);
			var case_study = CAD.portfolio.client.find('.case_study').eq(num-1);
			if (case_study.length > 0) {
				CAD.portfolio.case_num = num;
				var stage_image = case_study.find('.stage_image');
				var content = case_study.find('.content');
				var tag_block = case_study.find('.tags').eq(0);
				$('#more_info').html(content.html());
				$('#stage_image').css('display', 'block').attr('src', stage_image.attr('src'));
				if (tag_block.length > 0) {
					$('#tags').html(tag_block.html());
				} else {
					$('#tags').html('');
				}
				return true;
			} else {
				return false;
			}
		},

		// Automatically figures out what the next case study is and loads it.
		// Returns false and no changes are made if the last case study is already
		// loaded.
		nextCaseStudy: function() {
			return CAD.portfolio.setCaseStudyNum(CAD.portfolio.case_num + 1);
		},

		// Automatically figures ou what the previous case study is and loads it.
		// Returns false and no changes are made if the first case study is already
		// loaded.
		prevCaseStudy: function() {
			return CAD.portfolio.setCaseStudyNum(CAD.portfolio.case_num - 1);
		}
	};
})();

// Here we are getting the document object and giving it some code to run when
// the document is finished loading. We put code in here that must not run until
// the document is loaded, such as anything that needs to access the DOM.
$(document).ready(function() {
	$('#header_image').click(function(evt) {
		CAD.portfolio.setHomeStage(true);
	});

	$('#blocks li').click(function(evt) {
		if (this.attributes.client) {
			CAD.portfolio.setClient(this.attributes.client.value);
		}
	}).mouseover(function(evt) {
		if (this.attributes.client) {
			CAD.portfolio.setVert(this.attributes.client.value);
		}
	}).mouseout(function(evt) {
		CAD.portfolio.clearVert();
	});

	$('#more_info_prev').click(function(evt) {
		CAD.portfolio.prevCaseStudy();
	});

	$('#more_info_next').click(function(evt) {
		CAD.portfolio.nextCaseStudy();
	});
});

