// JavaScript Document
function btn_overs(sel, newImage){
	var theImage=document.getElementById(sel);
	theImage.src=newImage;
}
function showImage(thePic){
	var picHTML='<img src="'+thePic+'" border="0">';
	document.getElementById("mainPic").innerHTML=picHTML;
}
function loadProject(thePic){
	var pn=parseProjectName(thePic);
	pn=ltrim(pn);
	var picHTML='<a href="?project='+pn+'"<img src="'+thePic+'" border="0"></a>';
	document.getElementById("mainPic").innerHTML=picHTML;
	var noun=getNoun(thePic);
	//document.getElementById("projectTitle").innerHTML=pn+' <span class="smaller"><a href="?project='+pn+'">view '+noun+'</a></span>';
	document.getElementById("projectTitle").innerHTML=pn;

}
function swapTitle(thePic){
	var pn=parseProjectName(thePic);
	pn=ltrim(pn);
	var noun=getNoun(thePic);
	//javascript:openArticle(\''.$sorter[$counter].'\')
	var thumbLink='';
	thumbLink='?'+noun+'='+pn;
	document.getElementById("projectTitle").innerHTML=pn+' <span class="smaller"><a href="'+thumbLink+'">view '+noun+'</a></span>';
	//document.getElementById("projectTitle").innerHTML=pn;
}
function swapProfileText(theText,thePic){
	thumbLink='<span class="smaller"><a href="javascript:openArticle(\''+parseArticleTitle(thePic)+'\')">read article</a></span>';
	document.getElementById("projectTitle").innerHTML=theText+thumbLink;
}
function swapDetails(theDetails){
	document.getElementById("picDetails").innerHTML=theDetails;
}

function parsef(thePic){
	var f=str_ireplace ( '../images/galleries/', '', thePic );
	f=str_ireplace ( '/1.jpg', '', f );
	return f;
}

function getNoun(thePic){
	var noun='project';
	if (thePic.search(/recognition/)!=-1){
		noun='article';		
	}
	if (thePic.search(/capabilities/)!=-1){
		noun='gallery';
	}
	return noun;
}

function parseProjectName(thePic){
	var projectName=str_ireplace ( '../images/galleries/', '', thePic );
	projectName=str_ireplace ( 'design/', '', projectName );
	projectName=str_ireplace ( 'capabilities/', '', projectName );
	projectName=str_ireplace ( 'construction/', '', projectName );
	projectName=str_ireplace ( 'recognition/', '', projectName );
	projectName=str_ireplace ( '/1.jpg', '', projectName );
	projectName=projectName.replace(/[0-9]/,"");
	projectName=projectName.replace(/[0-9]/,"");
	projectName=str_ireplace ( '_', ' ', projectName ); 
	projectName=str_ireplace ( '-', '', projectName ); 
	return projectName;
}

function parseArticleTitle(thePic){
	var artName=str_ireplace ( '../images/galleries/', '', thePic );
	artName=str_ireplace ( 'recognition/', '', artName );
	artName=str_ireplace ( '/1.jpg', '', artName );
	return artName;
}


function openArticle(art){	
	window.open('article.php?article='+art,'article','width=650,height=650,scrollbars=yes,resizable=yes,screenX=400,screenY=50,left=400,top=50');	
}

////utilities
// str_replace
function str_ireplace ( search, replace, subject ) {
    var i, k = '';
    var searchl = 0;

    search += '';
    searchl = search.length;
    if (!(replace instanceof Array)) {
        replace = new Array(replace);
        if (search instanceof Array) {
            // If search is an array and replace is a string,
            // then this replacement string is used for every value of search
            while (searchl > replace.length) {
                replace[replace.length] = replace[0];
            }
        }
    }

    if (!(search instanceof Array)) {
        search = new Array(search);
    }
    while (search.length>replace.length) {
        // If replace has fewer values than search,
        // then an empty string is used for the rest of replacement values
        replace[replace.length] = '';
    }

    if (subject instanceof Array) {
        // If subject is an array, then the search and replace is performed
        // with every entry of subject , and the return value is an array as well.
        for (k in subject) {
            subject[k] = str_ireplace(search, replace, subject[k]);
        }
        return subject;
    }

    searchl = search.length;
    for (i = 0; i < searchl; i++) {
        reg = new RegExp(search[i], 'gi');
        subject = subject.replace(reg, replace[i]);
    }

    return subject;
}

//trim
function trim(str, chars) {
	return ltrim(rtrim(str, chars), chars);
}
 
function ltrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("^[" + chars + "]+", "g"), "");
}
 
function rtrim(str, chars) {
	chars = chars || "\\s";
	return str.replace(new RegExp("[" + chars + "]+$", "g"), "");
}

