// Carasquillo Project Scripts



// GENERATE SMALL FEATURE
function smallFeature(number) {
	 document.write('<div class=\"project-small\">');
     document.write('<a href=\"projects.html?p='+number+'\"><img src=\"images/projects/project_'+project[number]+'_main.jpg\" width=\"115\" height=\"85\" border=\"0\" /></a>');
     document.write('<p class=\"project-title\">'+project_name[number]+'</p>');
     document.write('<p class=\"project-location\">'+project_location[number]+'</p>');
     document.write('<a href=\"projects.html?p='+number+'\">LEARN MORE &raquo;</a><br />');
     document.write('<br /></div>');
}

// GENERATE HOME PAGE FEATURE
function homeFeature(number) {

     document.write('<a href=\"projects.html?p='+number+'\"><img src=\"images/projects/project_'+project[number]+'_main.jpg\" width=\"115\" height=\"85\" border=\"0\" " /></a>');
     document.write('<p class=\"project-title\">'+project_name[number]+'</p>');
     document.write('<p class=\"project-location\">'+project_location[number]+'</p>');
     document.write('<a href=\"projects.html?p='+number+'\">LEARN MORE &raquo;</a>');
     
}


// GENERATE LARGE PROJECT FOR PROJECT PAGE
function projectDisplay(number) {
	document.write('<h1>'+project_name[number]+'</h1>');
   	document.write('<p>'+project_description[number]+'</p></div>');
    document.write('<div id=\"project-photos\">');
    document.write('<img src=\"images/projects/project_'+project[number]+'_main.jpg\" width=\"445\" /><br /><br />');
    document.write('<script type=\"text/javascript\">hs.graphicsDir = \"scripts/graphics/\";</script>');
	
	for (i  = 1; i <= project_photonumber[number]; i++) {
		document.write('<a href=\"images/projects/project_'+project[number]+i+'.jpg\" class=\"highslide\" onClick=\"return hs.expand(this)\"><img hspace=\"1\" src=\"images/projects/project_'+project[number]+i+'.jpg\" alt=\"'+project_name[number]+'\" title=\"Click to enlarge\" width=\"105\"></a><div class=\"highslide-caption\">'+project_name[number]+'</div>');
	}
}

// GET QUERY STRING
function getQueryString() {
	fullURL = parent.document.URL 
	projectnumber = fullURL.substring(fullURL.indexOf('?')+3, fullURL.length)
	if (fullURL.indexOf('?') == -1) {projectnumber = 0;}
	return parseInt(projectnumber);
}

//GET SEARCH STRING
function getSearchString() {
	fullURL = parent.document.URL 
	searchstring = fullURL.substring(fullURL.indexOf('?')+3, fullURL.length)
	if (fullURL.indexOf('?') == -1) {searchstring = "";}
	return searchstring;
}

//GET CATEGORY
function getCategory() {
	fullURL = parent.document.URL;
	category = fullURL.substring(fullURL.indexOf('?')+3, fullURL.length)
	if (fullURL.indexOf('?') == -1) {category = "";}
	return category;
}


// BUILD LIST OF FEATURED PROJECTS
function buildFeatured(featuredNumber) {
	rand = Math.round(Math.random()*project.length);

	if ((rand + featuredNumber) > project.length) {
		rand -= featuredNumber;
		
	}
	for (i = 0 ; i < featuredNumber; i++) {
		smallFeature(rand+i);	
		
	}
	
}

// BUILD LIST OF HOME FEATURED PROJECTS
function buildHomeFeatured(featuredNumber) {
	rand = Math.round(Math.random()*project.length);

	if ((rand + featuredNumber) > project.length) {
		rand -= featuredNumber;
		
	}
	for (i = 0 ; i < featuredNumber; i++) {
		homeFeature(rand+i);	
		if (i < featuredNumber-1) {
			document.write('<br clear=\"left\" /><br /><br />');
		}
		else { document.write('<br clear=\"left\" />');}
	}
	
}

// BUILD ALL PROJECTS
function buildAll() {
	
	for (i = 0 ; i < project.length; i++) {
		if ((i!=0) && (i/5 == (parseInt(i/5)))) {
			document.write('<br clear="left" />');
		}
	
	smallFeature(i);
	}	
		
	
}


// BACK AND FORTH PROJECT NAVIGATION

function goPreviousProject(prevnumber) {
	/*prevprojectnumber = prevnumber-1;
	if (prevprojectnumber < 0) {prevprojectnumber = project.length-1}
	prevname = project_name[prevprojectnumber];
	if( prevname.length > 22 ) {
		prevname = prevname.substring(0,22) + "...";
	}*/
	
	document.write('<a href=\"javascript:history.back()\">');
	document.write('&laquo; Previous Page</a></div>');
}

function goNextProject(nextnumber) {
	nextprojectnumber = parseInt(nextnumber)+1;
	
	if (nextprojectnumber >= project.length) {nextprojectnumber = 0}
	nextname = project_name[nextprojectnumber];
	if( nextname.length > 22 ) {
		nextname = nextname.substring(0,22) + "...";
	}
	document.write('<a href=\"projects.html?p='+nextprojectnumber+'\">');
	document.write('Next: '+nextname+' &raquo;</a></div>');
}


// BUILD DROP DOWN LIST OF PROJECTS
function projectNameList() {
	for(i = 0;i < project.length; i++) {
	document.write('<option value="projects.html?p='+i+'\">'+project_name[i]+'</option>');
	}
}


//SEARCH
function keywords(searchstring) {
searchstring = searchstring.toLowerCase();
var searchArray = [];
var j = 0;

	
	for (i = 0; i < project.length; i++) {
		//name = project_name[i].toLowerCase();
		//description = project_description[i].toLowerCase();
		//loc = project_location[i].toLowerCase();
		kw = project_keywords[i].toLowerCase();
		
		if ( (kw.indexOf(searchstring) != -1) ) {
			searchArray[j] = i;
			j++;
			}
		}
		

	if (searchArray.length == 0) {document.write('No Projects Found that Match Keywords');}
	else {
		for (i = 0 ; i < searchArray.length; i++) {
			if ((i!=0) && (i/5 == (parseInt(i/5)))) {
				document.write('<br clear="left" />');
			}
		
		smallFeature(searchArray[i]);
		}
	}

}

//SERVICE
function categoryList(category) {
category = category.toLowerCase();
var searchArray = [];
var j = 0;
		
	for (i = 0; i < project.length; i++) {
		cat = project_category[i].toLowerCase();
		if ( (cat.indexOf(category) != -1)  ) {
			searchArray[j] = i;
			j++;
			}
		}
		
	
		for (i = 0 ; i < searchArray.length; i++) {
			if ((i!=0) && (i/5 == (parseInt(i/5)))) {
				document.write('<br clear="left" />');
			}
		
		smallFeature(searchArray[i]);
		}
	
}


// SWAP HEADER IMAGES
function swapBackground() {
	if (document.getElementById('mainimagesub')) {
	rand = Math.floor(Math.random( ) * (5))+1;
	document.getElementById('mainimagesub').style.backgroundImage = 'url(images/header_'+rand+'.jpg)';
								}
}

// SORT SELECT LISTS
function sortSelect() { 
  // Sort ALL select elements 
  var selectArr = document.getElementsByTagName('select'); 
  for (var i = 0; i < selectArr.length; i++) { 
    var oArr = []; 
    // Get the options for the select element 
    for (var j = 1; j < selectArr[i].options.length-1; j++) { 
      // Store this as an object that has an option object member, and 
      // a toString function (which will be used for sorting) 
      oArr[oArr.length] = { 
        option : selectArr[i].options[j], 
        toString : function() { 
          // Return the text of the option, not the value 
          return this.option.text; 
        } 
      } 
    } 
    // Sort the array of options for this select 
    oArr.sort(); 
    // Remove all options from the select 
    selectArr[i].options.length = 1; 
    // Rebuild the select using our sorted array 
    for (var j = 1; j < oArr.length; j++) { 
      selectArr[i].options[j] = oArr[j].option; 
    } 
  } 
}
