function findPos(obj)
{
	var curleft = curtop = 0;
	if(obj.offsetParent)
	{
		do
		{
			curleft += obj.offsetLeft;
			curtop += obj.offsetTop;
		}
		while(obj = obj.offsetParent);
	}
	return [curleft, curtop];
}

function changeState(obj, state1, state2)
{
	// if the entire string of one state exists within another (e.g. over & overRed) then the longer string should occur first in the state list sent as a parameter to this function
	if(obj.src.indexOf(state1) > -1)
	{
		obj.src = obj.src.replace(state1, state2);
	}
	else
	{
		obj.src = obj.src.replace(state2, state1);
	}
	return true;
}

function showImage(obj, image)
{
	dlObj = obj.parentNode;
	jobTitle = "";

	dlLinks = dlObj.getElementsByTagName("a");
	for(i=0;i<dlLinks.length; i++)
	{
		if(dlLinks[i].className == 'job_title')
		{
			jobTitle = dlLinks[i].firstChild.nodeValue;
			break;
		}
	}
	if(jobTitle == "")
	{
		jobTitle = dlObj.firstChild.nodeValue;
	}

// 	document.getElementById("thumb_viewer_image").src = '/images/blank.png';
	document.getElementById("thumb_viewer_image").src = '/images/portfolio/' + image;
	document.getElementById("thumb_viewer_title").innerHTML = jobTitle;
	
	position = findPos(obj);
	//document.getElementById("thumb_viewer").style.top = (position[1] - 200) + 'px';
	document.getElementById("thumb_viewer").style.display = 'block';
	document.getElementById("thumb_viewer_back").style.display = 'block';
}
function hideImage()
{
	document.getElementById("thumb_viewer").style.display = 'none';
	document.getElementById("thumb_viewer_back").style.display = 'none';
}

window.onload = checks;

function checks()
{
	// check for portfolio thumbnail links on images and change them to lightboxes
	allLinks = document.links;
	count = 0;
	for(i=0; i<allLinks.length; i++)
	{
		if(allLinks[i].className == 'portfolio_thumb')
		{
			count ++;
			allLinks[i].href = '#';
			allLinks[i].target = '_self';
		}
	}
	
	// check for code excerpts and add buttons to them
	allOLs = document.getElementsByTagName("ol");

	for(i=0; i<allOLs.length; i++)
	{
		if(allOLs[i].className == 'code')
		{
			excerptCopyButton = document.createElement('img');
			excerptCopyButton.setAttribute('src', '/images/get_excerpt.png');
			excerptCopyButton.setAttribute('alt', 'Get Code Excerpt ' + (i + 1));
			excerptCopyButton.setAttribute('title', 'Get Code Excerpt ' + (i + 1));
			
			excerptNo = i + 'wtf';	// have to assign it here, otherwise it attempts to use the global i value, which will be the total number of ol lists on the page
			// add onclick listener to all compliant browsers and then ie
			/*if(excerptCopyButton.addEventListener)
			{
				excerptCopyButton.addEventListener("click",	function() {showExcerpt(excerptNo);}, false);
			}
			else if(excerptCopyButton.attachEvent)
			{
				excerptCopyButton.attachEvent("onclick",	function() {showExcerpt(excerptNo);});
			}*/
			if(excerptCopyButton.addEventListener)
			{
				excerptCopyButton.addEventListener("click",	function(e){showExcerpt(e.target)}, false);
			}
			else if(excerptCopyButton.attachEvent)
			{
				excerptCopyButton.attachEvent("onclick",showExcerpt);
			}

			excerptBlock = document.createElement('div');
			excerptBlock.className = 'excerptPanel';
			excerptBlock.appendChild(excerptCopyButton);
			
			// append the excerpt block to the top of the list
			currentOL = document.getElementsByTagName("ol")[i];
			allOLs[i].parentNode.insertBefore(excerptBlock, currentOL);
		}
	}
}

function showExcerpt(obj)
{
	id = parseInt(obj.title.substring(obj.title.lastIndexOf(' '), obj.title.length)) - 1;

	olBlock = document.getElementsByTagName('ol')[parseInt(id)];
	lines = olBlock.getElementsByTagName('li');
	codeHead = '<html><head><title>Code Excerpt</title></head><body><textarea style="width: 100%; height: 100%;">';
	code = '';
	for(i=0; i<lines.length; i++)
	{
		code += StripTags(lines[i].innerHTML) + "\n";
	}
	code = html_entity_decode(code)
	codeFoot = '</textarea></body></html>';
	
	excerpt = codeHead + code + codeFoot;

	codeWindow = window.open('about:blank', 'codeWindow', 'width=700,height=500,scrollbars=yes');
	codeWindow.document.write(excerpt);
	codeWindow.document.close();
}



// StripTags function not my own
//CREATOR: http://www.georgehernandez.com
function StripTags(strMod){
    if(arguments.length<3) strMod=strMod.replace(/<\/?(?!\!)[^>]*>/gi, '');
    else{
        var IsAllowed=arguments[1];
        var Specified=eval("["+arguments[2]+"]");
        if(IsAllowed){
            var strRegExp='</?(?!(' + Specified.join('|') + '))\b[^>]*>';
            strMod=strMod.replace(new RegExp(strRegExp, 'gi'), '');
        }else{
            var strRegExp='</?(' + Specified.join('|') + ')\b[^>]*>';
            strMod=strMod.replace(new RegExp(strRegExp, 'gi'), '');
        }
    }
    return strMod;
}

// html_entity_decode found on http://www.daniweb.com/forums/thread137235.html#
function html_entity_decode(str)
{
	//jd-tech.net
	var tarea=document.createElement('textarea');
	tarea.innerHTML = str; return tarea.value;
	tarea.parentNode.removeChild(tarea);
}