/* ------------------------------------------------------------------------
 * Case Studies Randomizer
 * ------------------------------------------------------------------------
 * Infrascture to randomizes case studies for the side icons.
 * ------------------------------------------------------------------------
 * TELUS
 * Copyright 2006
 * Vancouver, BC
 * 604.801.5758
 * esolutions@telus.com
 * ------------------------------------------------------------------------
 */


// ------------------------------------------------------------------------
// DataTypes
// ------------------------------------------------------------------------

	// Case Study Sections
	function csSection(id, name, image, image2) {
		this.id = id;
		this.name = name;
		this.image = image;
		this.image2 = image2;
	}


	// Case Study Items
	function csItem(id, section, link, name) {
		this.id = id;
		this.section = section;
		this.link = link;
		this.name = name;
	}
	
	// Bucket
	// ------
	// Used to hold info about case studies for randomizing.
	function csBucket(level) {
		this.level = level;
		this.items = new Array();
	}

	// Filter
	// ------
	// Defines a filter for excluding or including something.
	// If section or id is 0, then that part will be ignored during filtering.
	// Having both of the 0 will mean "default" for undefined or unmatching 
	// selections. id takes precedence over section.
	// Levels are defined as follows:
	// 0     -- excluded (no exceptions)
	// 1 - 8 -- preference levels with 8 taking priority over 7 and so on.
	// 9     -- included (no exceptions)
	// ------
	function csFilter(section, id, level) {
		this.section = section;
		this.id = id;
		this.level = level;
	}


// ------------------------------------------------------------------------
// Data
// ------------------------------------------------------------------------

	// Case Study Sections
	var g_CSSections = new Array();
	g_CSSections[0] = new csSection(1, "contact center operation", "/images/banners/ftr_cc_operation.gif", "/images/banners/ftr_cc_operation2.gif");
	g_CSSections[1] = new csSection(2, "contact center consolidation", "/images/banners/ftr_cc_consolidation.gif");
	g_CSSections[2] = new csSection(3, "contact center re-engineering", "/images/banners/ftr_cc_re-engineer.gif");
	g_CSSections[3] = new csSection(4, "offshore application development and maintenance services", "/images/banners/ftr_offshore_app_devmaint.gif");
	g_CSSections[4] = new csSection(5, "IT outsourcing", "/images/banners/ftr_it_outsourc.gif", "/images/banners/ftr_it_outsourc2.gif");
	g_CSSections[5] = new csSection(6, "IT service management re-engineering", "/images/banners/ftr_it_servmanreeng.gif");
	g_CSSections[6] = new csSection(7, "IT consulting", "/images/banners/ftr_it_consult.gif");
	g_CSSections[7] = new csSection(8, "e-business consulting", "/images/banners/ftr_e_busconsult.gif");
	g_CSSections[8] = new csSection(9, "IT business process re-engineering", "/images/banners/ftr_it_busprocreeng.gif");
	g_CSSections[9] = new csSection(10, "business process outsourcing", "/images/banners/ftr_busprocout.gif", "/images/banners/ftr_busprocout2.gif");

	// Case Studies
	var g_CaseStudies = new Array();
	g_CaseStudies[0] = new csItem(1, 1, "/case_studies/case_study_twelve.shtml", "Meeting the customer service needs of the largest computer manufacturer of the world.");
	g_CaseStudies[1] = new csItem(2, 1, "/case_studies/case_study_thirteen.shtml", "Support the growth of a leading American Credit Card service provider.");
	g_CaseStudies[2] = new csItem(3, 1, "/case_studies/case_study_fourteen.shtml", "Support peripheral sales for a leading computer manufacturer.");
	g_CaseStudies[3] = new csItem(4, 1, "/case_studies/case_study_fifteen.shtml", "Support additional needs of a leading computer manufacturer.");
	g_CaseStudies[4] = new csItem(5, 2, "/case_studies/case_study_six.shtml", "Saudi Arabia's largest oil company improved delivery and reporting capabilities with our implementation of a fully integrated contact center.");
	g_CaseStudies[5] = new csItem(6, 3, "/case_studies/case_study_seventeen.shtml", "We re-engineered an information call center to meet the demands of an expanding tourism industry in a major Asian country.");
	g_CaseStudies[6] = new csItem(7, 4, "/case_studies/case_study_two.shtml", "We helped one of Canada's largest Telecommunications companies substantially reduce operating costs.");
	g_CaseStudies[7] = new csItem(8, 5, "/case_studies/case_study_five.shtml", "Premium one stop innovative technical support for 45,000 desktops across Korea.");
	g_CaseStudies[8] = new csItem(9, 5, "/case_studies/case_study_eight.shtml", "Our IT support and service enabled this global company to concentrate on effectively driving its business forward.");
	g_CaseStudies[9] = new csItem(10, 5, "/case_studies/case_study_nine.shtml", "We enabled this family owned organization to keep pace with technology advancements in its industry without the major expenditures.");
	g_CaseStudies[10] = new csItem(11, 5, "/case_studies/case_study_sixteen.shtml", "We designed a web-based remote tool for IT support that enabled a Korean foreign studies university to become a cyber university.");
	g_CaseStudies[11] = new csItem(12, 5, "/case_studies/case_study_eighteen.shtml", "We developed a computerised ticketing system to improve the efficiency of a major Korean association.");
	g_CaseStudies[12] = new csItem(13, 6, "/case_studies/case_study_three.shtml", "A leading emerging markets bank improved service and enhanced shareholder value with our innovative contact centers solution.");
	g_CaseStudies[13] = new csItem(14, 7, "/case_studies/case_study_ten.shtml", "We helped a large Caribbean telecommunications company establish a world-class data center facility in one of the world's more challenging infrastructure environments.");
	g_CaseStudies[14] = new csItem(15, 8, "/case_studies/case_study_four.shtml", "A dedicated team from TELUS International assisted this organization in its transformation into one of Asia's top IT service companies.");
	g_CaseStudies[15] = new csItem(16, 9, "/case_studies/case_study_seven.shtml", "We helped this service provider enhance its customer facing capabilities in order to stay ahead of the competition.");
	g_CaseStudies[16] = new csItem(17, 10, "/case_studies/case_study_one.shtml", "One of the largest hospitals in the Philippines improved operating efficiency by adopting our highly efficient integrated management system.");
	g_CaseStudies[17] = new csItem(18, 10, "/case_studies/case_study_eleven.shtml", "We delivered a complete and comprehensive functional outsourcing solution designed to simplify the way a major global underwriter and brokerage does business in Canada.");
	
	// Solutions
	var g_Solutions = new Array();
	g_Solutions["/solutions/contact_center_solutions.shtml"] = [[1, 0], [2, 0], [3,0]];
	g_Solutions["/solutions/application_mgmt.shtml"] =[[4, 0], [8, 0]];
	g_Solutions["/solutions/technology_mgmt.shtml"] = [[5, 0], [6, 0], [7,0], [9,0]];
	g_Solutions["/solutions/bpo.shtml"] = [[10, 0]];


// ------------------------------------------------------------------------
// Functions
// ------------------------------------------------------------------------

	// buildCSFilter
	// -------------
	// Builds a filter array based on the current Case Studies page.
	// It will do a lookup and exclude the current case studies page.
	function buildCSFilter(url) {
		var filterResults = new Array();
		
		// Defaults everyone to 5.
		filterResults.push(new csFilter(0, 0, 5));
	
		// First check to see if it's a case studies page.
		var csPageIndex = lookupCSIndexURI(url);
		
		if (csPageIndex >= 0) {
			filterResults.push(new csFilter(0, csPageIndex, 0));

			// Get section.
			var section = g_CaseStudies[csPageIndex].section;

			// Now prioritize it so that current section is chosen last.
			filterResults.push(new csFilter(section, 0, 3));
		} // if

		return filterResults;
		
	} // buildCSFilter


	// buildSolutionsFilter
	// --------------------
	// Builds a filter array based on the selected solutions page.
	// It will do a lookup and exclude the current case studies page.
	function buildSolutionsFilter(url) {
		var filterResults = new Array();
		
		// Defaults everyone to exclude (0).
		filterResults.push(new csFilter(0, 0, 0));
		
		// Find the solutions page.
		var name = findURI(url, -1);
		
		if (g_Solutions[name] != undefined) {
			var list = g_Solutions[name];
			for (var i = 0; i < list.length; i++) {
				filterResults.push(new csFilter(list[i][0], list[i][1], 5));
			} // for
			
		} // if
	
		return filterResults;
		
	} // buildSolutionsFilter
	
	
	// checkMatch
	// ----------
	// Checks the match between the filter and case study.  Returns a number:
	// < 0 -- no match.
	// 0   -- default match.
	// 1   -- section match.
	// 2   -- item match.
	function checkMatch(filter, item) {
	
		// basic check.
		if ((filter == null) || (filter == undefined) 
				|| (item == null) || (item == undefined)) {
			return -1;
		}
		
		if ((filter.section == 0) && (filter.id == 0)) {
			return 0;
		}
		
		if (filter.id == item.id) {
			return 2;
		}
		
		if ((filter.section == item.section) && (filter.id == 0)) {
			return 1;
		}
		
		return -1;
	
	} // checkMatch


	// getCaseStudy
	// ------------------
	// Returns the section with that ID.
	function getCaseStudy(id) {
		var index = lookupCSIndexID(id);
		if (index >= 0) return g_CaseStudies[index];
		return null;
	} // getCaseStudy


	// getCSSection
	// ------------------
	// Returns the section with that ID.
	function getCSSection(id) {
		var index = lookupCSSectionIndexID(id);
		if (index >= 0) return g_CSSections[index];
		return null;
	} // getCSSection


	// getFilteredChoices
	// ------------------
	// Builds a table for the filtered choices.  Returns a list of levels 
	// and choices.  Not for public consumption anyways.
	function getFilteredChoices(filters) {
	
		// If there are no filters, just make it a default of level 5.
		if ((filters == null) || (filters == undefined)) {
			filterResults.push(new csFilter(0, 0, 5));
		} // if
		
		var buckets = new Array();
	
		// For each item in the list, iterate through the filter list to
		// figure out where it goes.
		for (var i = 0; i < g_CaseStudies.length; i++) {
			var bestFilter = 0;
			var bestFilterScore = -1;

			for (var j = 0; j < filters.length; j++) {
			
				var filterScore = checkMatch(filter[j], g_CaseStudies[i]);
				if (filterScore > bestFilterScore) {
					bestFilter = j;
					bestFilterScore = filterScore;
				} // if
			} // for
			
			if (bestFilterScore >= 0) {
				var level = filters[bestFilter].level;
				if (buckets[level] == null) {
					buckets[level] = new csBucket(level);
				}
				
				buckets[level].items.push(g_CaseStudies[i]);
			} // if
		} // for
		
		// compact the list.
		var i = buckets.length - 1;
		while (i >= 0) {
			if (buckets[i] == null) {
				buckets.splice(i, 1);
			}
			i--;
		} // while
	
		return buckets.reverse();
	
	} // getFilteredChoices


	// getRandomCaseStudies
	// ------------------
	// Finds "x" number of random case studies in priority order.
	// Returns an array of csItems.
	function getRandomCaseStudies(number, filter) {
		var results = new Array();
	
		// Get buckets from filters.
		var buckets = getFilteredChoices(filter);
		
		// Randomly select things.
		var resultIndex = 0;
		while ((buckets.length > 0) && (resultIndex < number)) {
		
			// If level 0, don't display.
			if (buckets[0].level == 0) {
				buckets.shift();
				continue;
			}
			
			var index = getRandomIndex(buckets[0].items.length);
			results[resultIndex] = buckets[0].items[index];
			resultIndex++;
			
			buckets[0].items.splice(index, 1);
			if (buckets[0].items.length <= 0) {
				buckets.shift();
			}
			
		} // while
		
		return results;
	
	} // getRandomCaseStudies
	
	
	// getRandomIndex
	// ------------------
	// Generate a random index from a maxsize (assuming dense indexes).
	function getRandomIndex(maxsize) {
		var idxUnround = 0;
		var idx = -99;

		idxUnround = Math.random() * maxsize;
		idx = Math.floor(idxUnround);

		return idx;
	} // getRandomIndex
	

	// lookupCSIndexID
	// ----------------
	// Brute-force convenience lookup for the CS Index based on the ID.
	function lookupCSIndexID(id) {
		for (var i = 0;	i < g_CaseStudies.length; i++) {
			if (g_CaseStudies[i].id == id) {
				return i;
			}
		} // for
		
		return -1;

	} // lookupCSIndexID


	// lookupCSIndexURI
	// ----------------
	// Looks up the CS Index based on the URL.  Relies on g_CaseStudies[i].link 
	// being in a /path/to/page.ext format.
	function lookupCSIndexURI(uri) {
		var meURI = findURI(uri, -1);
		
		for (var i = 0;	i < g_CaseStudies.length; i++) {
			var testURI = g_CaseStudies[i].link;
			
			if (meURI.toLowerCase() == testURI.toLowerCase()) {
				return i;
			}

		} // for
		
		return -1;

	} // lookupCSIndexURI


	// lookupCSSectionIndexID
	// ----------------------
	// Brute-force convenience lookup for the CS Section Index based on 
	// the ID.
	function lookupCSSectionIndexID(id) {
		for (var i = 0;	i < g_CSSections.length; i++) {
			if (g_CSSections[i].id == id) {
				return i;
			}
		} // for
		
		return -1;

	} // lookupCSSectionIndexID

