$(document).ready(function() {

	var filePrefix = "";
	if(window.location.pathname.indexOf("work") > 0) {
		filePrefix = "../";		
	}

	// EDIT ///////////////////////////////////////////////

	// industry arrays (TODO: make into external XML)
	
	$("#industry").data("hospitality", ["Ritz_Carlton_Dove_Mountain.html","PGA_National_Resort_and_Spa.html","Double_Tree_By_Hilton.html","Radisson_Suites_Tucson.html","Mandarin_Oriental_Hotel_Group.html","Savvy_Navigator_Tours.html"]);
	$("#industry").data("energy", ["APS.html","Lasertel.html","IPAA.html","AZRISE.html","Pakedge_Device_And_Software.html",""]);
	$("#industry").data("finance", ["Commerce_Bank_Of_Arizona.html","NAIC.html","Syncom_Venture_Partners.html","Harbor_Equity_Partners.html","Hassett_Willis_Company.html","Carryl_Capital_Management.html"]);
	$("#industry").data("more", ["Etherton_Gallery.html","Skillfully_Aware.html","Paromi_Tea.html","Council_On_Foundations.html","Bridges_Jewelry.html","Olson_Communications.html"]);

	// initial industry
	var INITIAL_INDUSTRY = "hospitality"

	// DO NOT EDIT ////////////////////////////////////////

	// flag for industry clicking
	var industryclickable = true;
	
	// set initial links
	$(".industrypreviewbox").each(function(index) {	
		var fooC = $("#industry").data(INITIAL_INDUSTRY);
		$(this).attr("href", filePrefix + "work/" + fooC[index]);		
	});
	$(".industryselector").each(function(index) {	
		var fooC = $("#industry").data(INITIAL_INDUSTRY);
		$(this).attr("href", filePrefix + "work/" + fooC[index]);		
	});

	// General select colors (blue hover, red active) 

	var filename;

	$(".selectIndustryLink").hover(function() {
		if( $(this).attr("rel") != "active" ) {
			filename = $(this).children("img").attr("src").split(".png");
			$(this).children("img").attr("src", filename[0] + "_hover" + ".png");
		}
	}, function() {
		if( $(this).attr("rel") != "active" ) {
			filename = $(this).children("img").attr("src").split(".png");
			filename[0] = filename[0].replace("_hover", "");
			$(this).children("img").attr("src", filename[0] + ".png");
		}
	});

	// Inustry selector
	$(".selectIndustryLink").click(function() {
	
		if( $(this).attr("rel") != "active" && industryclickable == true) {
			clickable = false;

			// clear all active
			$(".selectIndustryLink").each(function() {
				$(this).attr("rel", "");
				filename = $(this).children("img").attr("src").split(".png");
				filename[0] = filename[0].replace("_hover", "");
				$(this).children("img").attr("src", filename[0] + ".png");			
			});

			// get new industry
			var newindustry = this.id;

			// new preview boxes
			$(".industrypreviewbox").each(function(index) {
				// set new image
				var imgsrc = $(this).children("img").attr("src");
				var fooA = imgsrc.split("industry_selectors/");
				var fooB = fooA[1].split("_");
				var newimgsrc = fooA[0] + "industry_selectors/" + newindustry + "_" + fooB[1] + "_" + fooB[2];
				$(this).children("img").attr("src", newimgsrc);

				// set new link
				var fooC = $("#industry").data(newindustry);
				$(this).attr("href", filePrefix + "work/" + fooC[index]);
			});

			// fade out previewboxes

			$(".previewbox").each(function() {
				var myPreviewBox = $(this);
				$(this).fadeOut(function() {

					// new selectors
					$(".industryselector").each(function(index) {
						// set new image
						var imgsrc = $(this).children("img").attr("src");
						var fooA = imgsrc.split("industry_selectors/");
						var fooB = fooA[1].split("_");
						var newimgsrc = fooA[0] + "industry_selectors/" + newindustry + "_" + fooB[1] + "_" + fooB[2];

						$(this).children("img").fadeOut(function() {
							$(this).load(function() { 
								$(this).fadeIn(function() {
									// load back up the selector
									myPreviewBox.fadeIn(function() {
										industryclickable = true;
									});
								}); 
							}); 
							$(this).attr("src", newimgsrc);
						});

						// set new link
						var fooC = $("#industry").data(newindustry);
						$(this).attr("href", filePrefix + "work/" + fooC[index]);

					});

				});
			});

			$(this).attr("rel", "active");
			filename = $(this).children("img").attr("src").split(".png");
			$(this).children("img").attr("src", filename[0] + "_hover" + ".png");
		}
		
			
	});

});



