/****************************************************************
* PROPRIETARY INFO
* The content contained in this JavaScript file is the property 
* of The Fashion Institute of Design & Merchandising. 
* You may not, in any way, shape, or form, copy or reproduce 
* any portion of this code without express written consent.  
****************************************************************/

/****************************************************************
* LINK SORTER
* The purpose of this link sorter is to take a series of links, 
* store them in a JavaScript array, and give the user the ability 
* to sort the links by date, last name, grad year or major. 
* 
* The basic concept is that all information that we want to write, 
* including the sort buttons and the actual links, gets stored 
* into one large string. And this info has to be in a separate 
* frame just for continuity. Also because the page has to be 
* reloaded or refreshed each time we want to show a new page. 
* Then the page that gets reloaded just writes out the string!
****************************************************************/
var business = ["business",
"Activewear",
"Blogs",
"Bridal",
"Consultant",
"Costume Designer",
"DIY",
"Extended Education",
"Footwear",
"Gifts",
"Graphic Designer",
"Handbags and Accessories",
"Home",
"Illustrator",
"Interior Designer",
"Intimate Apparel",
"Jewelry",
"Kids",
"Make Up Artist/Skin Care",
"Men's",
"Patternmaker",
"Pets",
"Photographer/Artist",
"PR/Marketing/Special Events",
"Retailer",
"Screen Printing/Embroidery",
"Showroom Sales",
"Software",
"Stylist",
"Swimwear",
"Textile Designer",
"Visual Merchandiser",
"Web Designer",
"Women's"];

var majors = ["001-majors",
"Apparel Manufacturing Management",
"Beauty Industry Merchandising &amp; Marketing",
"Fashion Design",
"Film and TV Costume Design",
"Footwear Design",
"Graphic  Design",
"Interior Design",
"Jewelry Design",
"Merchandise Marketing",
"Product Development",
"Textile Design",
"Theater Costume Design",
"Visual Communications"
];



/****************************************************************
* VARIABLES
****************************************************************/
var whichsort = 1; // determines which type of sort we will perform
var itemsperpage = 10; // sets number of items to display per page.
var maxURLlength = 40; // maximum possible length for a URL before we strip the 'www'
var thepage = 'links.shtml'; // where to refresh the bottom page. 
var thememberpage = 'links.shtml'; // where to refresh the bottom page if you're already logged in
var thepublicpage = 'links.shtml'; // where to refresh the bottom page if you're not logged in yet
var currentpage = 1; // current page

var thecontent = ''; // store all content here before it is printed out
var totalpages = links.length/itemsperpage; // number of complete pages
var remainder = totalpages % 1; // number of leftover items that didn't fit on complete pages
if (remainder > 0) {totalpages = totalpages - remainder + 1;} // new number of pages, total.
var majorslist = ''; // stores all the content for writing the majors dropdown list
//var thelist = new Array(); // used to check for duplicates when building the majors dropdown menu
var x, y, holder; // variables used for sorting
var whichmajor; // stores the current major - only used for the sort by major
var majorflag = 0; // 1 = sort by major;  0 = sort by anything else;
var businesslist = ''; // stores all the content for writing the business dropdown list
var whichbusiness; // stores the current business - only used for the sort by business
var businessflag = 0; // 1 = sort by business;  0 = sort by anything else;
var gradyear = 0; // placeholder to convert 4-digit year into 2-digit year (i.e. 1998 -> '98)
var showall = 0; // 1 = show all records; 0 = break up into pages like normal


/****************************************************************
* WHICH REFRESH PAGE?
* Determines which page to refresh on the bottom, 
* the public or the member version, based on whether you're 
* logged in or not. 
****************************************************************/
function whichRefreshPage() {
  thepage = (window.location.href.indexOf('login')!=-1) ? thememberpage : thepublicpage;
}

/****************************************************************
* ALPHABETICAL SORT
* Take an array and sort it into alphabetical order
* Sorts to the array to find the best item for the first slot, 
* then the second slot, etc...
****************************************************************/
function alphaSort(mytype) {
  for (x=0; x<majors.length; x++) {
    for (y=x+1; y<majors.length; y++) {
      if (majors[x] > majors[y]) {
	    holder = majors[y];
	    majors[y] = majors[x];
	    majors[x] = holder;
	  }
    }
  }
}

/****************************************************************
* YEAR CONVERT
* Converts a 4-digit year into a 2-digit year
* e.g. 1998 -> '98, 2004 -> '04
****************************************************************/
function yearConvert(which) {
  gradyear = which + ''; // convert to string
  return '\'' + gradyear.substring(2,4); // returns last 2 digits
}

/****************************************************************
* URL CONVERT
* Strips the 'http://' out of the URL
* e.g. http://www.google.com -> wwww.google.com
* if it's too long, then only show google.com
****************************************************************/
function urlConvert(which) {
  if (which.length > maxURLlength) {
    return which.substring(4,which.length);
  }
  return which;
}

/****************************************************************
* FORMAT IT
* Takes the content in the array and formats it to look like 
* a nice organized list. Also adds a second alum if necessary.
****************************************************************/
function formatIt(which) {
  thecontent += '<tr><td>';
  thecontent += '<a href="http://' + links[which][5] + '" target="_blank">';
  thecontent += '<img src="thumbnails/' + links[which][0] + '.' + links[which][6] + '" width="138" height="87" border="0"></a></td>';
  thecontent += '<td valign="top"><span class="name">' + links[which][1] + ' ' + links[which][2] + '</span>' + ' ' + '<span class="grad-year">' + yearConvert(links[which][3]) + '</span>' + '<br>' + '<span class="major">' + majors[links[which][4]] + '</span>';
  if (links[which][8] == 1) {thecontent += '<br>' + links[which][9] + ' ' + links[which][10] + ' ' + yearConvert(links[which][11]) + '<BR>' + links[which][14];}
  thecontent += '</span><span class="url"><br>';
  thecontent += '<a href="http://' + links[which][5] + '" target="_blank">' + urlConvert(links[which][5]) + '</a></span></td></tr>';
}

/****************************************************************
* SORT IT
* A series of Sort functions
* type 0 = sort by date entered
* type 1 = sort by date entered - reverse
* type 2 = sort by alphabetical
* type 3 = sort by grad year
* type 4 = sort by grad year - reverse
****************************************************************/
function sortIt(type) {
	if (type == 0) {	// DATE ENTERED
	  for(x = 0; x < links.length; x++) {
		for(y = 0; y < (links.length-1); y++) {
		  if(links[y][0] > links[y+1][0]) {
		    holder = links[y+1];
		    links[y+1] = links[y];
		    links[y] = holder;
		  }
		}
	  }
	}
	if (type == 1) {	// DATE ENTERED - REVERSE
	  for(x = 0; x < links.length; x++) {
		for(y = 0; y < (links.length-1); y++) {
		  if(links[y][0] < links[y+1][0]) {
		    holder = links[y+1];
		    links[y+1] = links[y];
		    links[y] = holder;
		  }
		}
	  }
	}
	if (type == 2){ 	// ALPHABETICAL ORDER
	  for(x = 0; x < links.length; x++) {
		for(y = 0; y < (links.length-1); y++) {
		  if(links[y][2] > links[y+1][2]) {
		    holder = links[y+1];
		    links[y+1] = links[y];
		    links[y] = holder;
		  }
		}
	  }
	}
	if (type == 3){ 	// GRAD YEAR
	  for(x = 0; x < links.length; x++) {
		for(y = 0; y < (links.length-1); y++) {
		  if(links[y][3] > links[y+1][3]) {
		    holder = links[y+1];
		    links[y+1] = links[y];
		    links[y] = holder;
		  }
		}
	  }
	}
	if (type == 4){ 	// GRAD YEAR - reverse
	  var x, y, holder;
	  for(x = 0; x < links.length; x++) {
		for(y = 0; y < (links.length-1); y++) {
		  if(links[y][3] < links[y+1][3]) {
		    holder = links[y+1];
		    links[y+1] = links[y];
		    links[y] = holder;
		  }
		}
	  }
	}
}

/****************************************************************
* DISPLAY THE SORT BUTTONS
* Display the sorting links, but only make it a link if the array 
* is not already sorted by that method
****************************************************************/
function writeSortButtons(whichnumber, whichtext) {
  if (whichnumber == whichsort) { // the number we want to sort by is the current sort method
	thecontent += '| ' + whichtext + ' |';
  } else {
    thecontent += '| <a href="javascript:top.whichsort = ' + whichnumber + '; top.sortIt(top.whichsort); top.currentpage=1; top.refreshContent(1); document.location=top.thepage">' + whichtext + '</a> |';
  }
}

/****************************************************************
* DISPLAY THE MAJORS LIST (dropdown)
* Creates a dropdown menu that displays the list of majors, 
* but it removes all duplicates. When you click on the major, 
* it should generate a list of all records with that major. 
* step 1 - get the records into an array
* step 2 - sort the records
* step 3 - build the dropdown menu
* To identify unique majors, as we find new majors, we stick them
* in an array called 'thelist'. If it's already in 'thelist', 
* we move on. 
****************************************************************/
function displayMajors() {
/*
// step 1
  for (i=0; i<links.length; i++) { // cycle through all records
    var found = 0;
    for (j=0; j<thelist.length; j++) { // cycle through thelist of majors we've already found
	  if (thelist[j].indexOf(links[i][4])!=-1) {
	    found = 1; // found it!
	  }
	}
	if (found != 1) {// if it's not a duplicate
	  thelist[thelist.length] = links[i][4]; // add to array
	}
  }  */
// step 2
  alphaSort(); 
// step 3
  majorslist += '<form name="myform01"><select name="happy" onChange="top.majorflag=1; top.whichmajor=document.myform01.happy.options[document.myform01.happy.selectedIndex].value; top.refreshContent(1); document.location=top.thepage"><option>Please select a major</option>';  
  for (k=1; k<majors.length; k++) {
    majorslist += '<option value="' + majors[k] + '">' + majors[k] + '</option>'; // write the option
  }
  majorslist += '</select>';
  businesslist += '<select name="biz" onChange="top.businessflag=1; top.whichbusiness=document.myform01.biz.options[document.myform01.biz.selectedIndex].value; top.refreshContent(1); document.location=top.thepage"><option>Please select a business</option>';  
  for (k=1; k<business.length; k++) {
    businesslist += '<option value="' + business[k] + '">' + business[k] + '</option>'; // write the option
  }
  businesslist += '</select></form><HR>';
  majorslist += businesslist;
}

/****************************************************************
* REFRESH CONTENT
* This is where all the content goes! We store all written data 
* into this array because (1) It's quicker than multiple 
* document.write lines, and so that we can just make the call 
* from the page when it's ready instead of writing to a certain 
* page within a certain frame. 
* 1- display the sort buttons
****************************************************************/
function refreshContent(mynumber) {

  // VARIABLES
  thecontent=''; // erases the old version (sets it to blank)
  var currentitem = (currentpage-1)*itemsperpage; // defines the 1st item of the page. (i.e. p3= item#9)
  var end = (currentitem+itemsperpage < links.length) ? currentitem+itemsperpage : links.length; // make sure there are enough items to fill the page

  // SORT BUTTONS
  thecontent += '<span class="Verdana11pxBlack">Sort By: ';
  writeSortButtons(1,'Date Added'); // the number is the sort method
  writeSortButtons(2,'Last Name'); // the text is self explanatory
  writeSortButtons(4,'Grad Year');
  thecontent += '</span><HR>';
  
  // SHOW ALL LINKS (if requested)
  if (showall == 1) {
    thecontent += '<table width="352" border="0" cellpadding="0" cellspacing="5">';
    for (i=0; i<links.length; i++) {
	  formatIt(i);
	}
	thecontent += '</table>';
	showall = 0; // reset flag so you can sort by anything else
	
  // SHOW ALL RECORDS WITH A SPECIFIC MAJOR (if requested)
  } else if (majorflag == 1) {
	//alert("major flag");
    thecontent += '<table width="352" border="0" cellpadding="0" cellspacing="5">';
    for (i=0; i<links.length; i++) { // check all records
	  if (majors[links[i][4]].indexOf(whichmajor) != -1) { // check if the record matches
	    formatIt(i);
	  }
    }
	thecontent += '</table>';
	majorflag = 0; // reset flag so you can sort by anything else
  } else if (businessflag == 1) {
	//alert("major flag");
    thecontent += '<table width="352" border="0" cellpadding="0" cellspacing="5">';
    for (i=0; i<links.length; i++) { // check all records
/*		if (links[i][7].indexOf("|") != -1) {
			tempBiz = links[i][7].split("|");
			for ( l=0; l<=tempBiz.length; l++ ) {
				tempBiz[l] = parseInt(links[i][7],10);
//			console.log(tempBiz[l]);
			};
		} else {
			tempBiz[0] = parseInt(links[i][7],10);
		};*/
		switch (true) {
			case links[i][7][1] != null:
				if (business[links[i][7][1]].indexOf(whichbusiness) != -1) { // check if the record matches
					formatIt(i);
					break;
				};
			case links[i][7][0] != null:
				if (business[links[i][7][1]].indexOf(whichbusiness) != -1) { // check if the record matches
					formatIt(i);
				};
			break;
			default:
				if (business[links[i][7]].indexOf(whichbusiness) != -1) { // check if the record matches
					formatIt(i);
				};
			break;
		};

/*		if (tempBiz[1] && business[tempBiz[1]].indexOf(whichbusiness) != -1) {
			formatIt(i);
		}*/
	}
	thecontent += '</table>';
	businessflag = 0; // reset flag so you can sort by anything else
  } else {

	  // PAGE NUMBERS
	  // display 'prev' and 'next' when appropriate. 
	  // Each link will (1) set the new current page, 
	  // (2) refresh the content by calling this function, and 
	  // (3) reload the new page. The link will only be written 
	  // when appropriate (i.e. we're not already on the current page.)
	  thecontent += '<span class="Verdana11pxBlack">';
	  if (currentpage > 1) {thecontent += '<a href="javascript:top.currentpage--; top.refreshContent(1); document.location=top.thepage">prev</a> ';} // prev
	  for (i=1; i<=totalpages; i++) {
		if (i != currentpage) {thecontent += '<a href="javascript:top.currentpage=' + i + '; top.refreshContent(1); document.location=top.thepage">';} 
		thecontent += i; // write the number
		if (i != currentpage) {thecontent += '</a>';}
		thecontent += ' '; // add a space in between numbers
	  }
	  if (currentpage < totalpages) {thecontent += '<a href="javascript:top.currentpage++; top.refreshContent(1); document.location=top.thepage">next</a>';} // next
	  thecontent += ' || <a href="javascript:top.showall=1; top.refreshContent(1); document.location=top.thepage">show all</a></span><HR>';
	  
	  // DISPLAY ITEMS
	  thecontent += '<table width="352" border="0" cellpadding="0" cellspacing="5">';
	  for (i=currentitem; i<end; i++) {
		formatIt(i);
	  }
	  thecontent += '</table>';
  }
}

/****************************************************************
* ACTIONS
****************************************************************/
whichRefreshPage();
sortIt(whichsort); // sort through the array
displayMajors(); // display the majors dropdown menu
refreshContent(0); // initialize 'thecontent'
