// JavaScript Document
function lightbox(portId,fav) {
	if (!$('lightboxCover')) {
		try { 
			Element.insert($('body'),{bottom:'<div id="lightboxCover" style="position:absolute;top:0px;left:0px;overflow:hidden;background-color:#fff;opacity:0.6;filter:alpha(opacity=60);text-align:center;display:none;" onclick="closelightbox();">&nbsp;</div>'}); 
			Element.insert($('body'),{bottom:'<div align="center" style="position:absolute;top:0px;width:100%;" id="lightboxWrapper"><div id="portContainer" style=""><table width="100%" cellspacing="0" cellpadding="0" border="0"><tr><td id="portContainer2"><div id="portLogo"><img src="/images/logo-splash.gif"></div><div id="lightboxItem" align="left" style="position:relative;top:0px;line-height:10px;overflow:hidden;display:none;background-color:#bc813a;border:solid 1px #000000;"><table cellspacing="0" cellpadding="0" border="0"><tr><td valign="top"><img src="/images/spacer.gif" id="lightboxImg" onclick="closelightbox();" title="CLICK TO RETURN TO PORTFOLIO"></td><td valign="top" width="150"><div style="padding:25px 10px 10px 10px;font-size:11px;line-height:15px;" id="lightboxContent"></div></td></tr></table></div></td></tr></table></div></div>'}); 
			Event.observe(window, 'resize', function() { positionLightbox(true); });
			Event.observe('lightboxImg', 'load', function() {
				if (/.*spacer\.gif/.test($('lightboxImg').src) == false) { 
					positionLightbox();
					Effect.Appear('lightboxItem', { duration: 0.5 });

					//$('lightboxItem').style.display='block';
					
				} //end if  
			});
		} catch(e) { alert(e.description); }
	}
	$('lightboxWrapper').style.display='block';
	
	//$('lightboxCover').style.display='block';
	Effect.Appear('lightboxCover', { duration: 0.5, from:0.0, to:0.6 });
	$('body').style.overflow='hidden';
	$('html').style.overflow='hidden';
	positionLightbox();
	
	new Ajax.Request('/includes/portfolio.cfm?' + Math.random() * 999999, {
		method:'post',
		postBody:'portfolio_id=' + portId + '&fav=' + fav,  
		onSuccess: function(transport) { 
			var portfolioItem = transport.responseText.split('|');
			if (portfolioItem.length > 1) {
				$('lightboxImg').src = portfolioItem[0];
				$('lightboxContent').innerHTML = portfolioItem[1];
			} else {
				closelightbox();
			}
		},
		onException: function(req,exception) { alert('Error: ' + exception.description); closelightbox(); return true; },
		onFailure: function(transport) { alert('Error'); closelightbox(); } 
	});	
	
	//setTimeout("if($('lightboxItem').style.display=='none') { $('lightboxImg').src='/images/portImages/159full.jpg'; }",2000);
} //end function

function closelightbox(portId,fav) {
	if ($('lightboxCover')) {
		Effect.Fade('lightboxItem', { duration: 0.3, from:1.0, to:0.0 });		
		Effect.Fade('lightboxCover', { duration: 0.3, from:0.6, to:0.0, afterFinish: function() { 
			$('body').style.overflow='visible';
			$('html').style.overflow='visible';
			$('lightboxImg').src = '/images/spacer.gif';
			$('lightboxContent').innerHTML = '';
			$('lightboxWrapper').style.display='none';
			if (portId) { lightbox(portId,fav); }
		} });
	}
} //end function

function positionLightbox(resize) {
	if ($('lightboxCover')) {
		var dimensions = screenSize();
		$('lightboxCover').style.width=dimensions[0] + 'px';
		$('lightboxCover').style.height=dimensions[1] + 'px';
		
		if (resize != true) {
			$('portContainer').style.width='';
			var lbdims = $('lightboxItem').getDimensions();
			$('portContainer').style.width=lbdims['width']+'px';
		} else {
			var lbdims = $('lightboxItem').getDimensions();	
		}
		$('portContainer2').style.height=dimensions[1]+'px';

		//$('lightboxItem').style.left = Math.floor((dimensions[0]/2)-(lbdims['width']/2)) + 'px';
		//$('lightboxItem').style.top = Math.floor((dimensions[1]/2)-(lbdims['height']/2)) + 'px';
	} //end if
}


function addFavorite(portId) {
	$('favoriteLink').innerHTML = '<a href="#" onclick="removeFavorite(' + portId + ');return false;" style="color:#ffffff;">FAVORITE (REMOVE)</a>';

	new Ajax.Request('/includes/portfolio.cfm?' + Math.random() * 999999, {
		method:'post',
		postBody:'favorite=add&portfolio_id=' + portId,  
		onSuccess: function(transport) { },
		onException: function(req,exception) { alert('Error: ' + exception.description); return true; },
		onFailure: function(transport) { alert('Error'); } 
	});	

}

function removeFavorite(portId) {
	$('favoriteLink').innerHTML = '<a href="#" onclick="addFavorite(' + portId + ');return false;" style="color:#ffffff;">ADD TO FAVORITES</a>';

	new Ajax.Request('/includes/portfolio.cfm?' + Math.random() * 999999, {
		method:'post',
		postBody:'favorite=remove&portfolio_id=' + portId,  
		onSuccess: function(transport) { },
		onException: function(req,exception) { alert('Error: ' + exception.description); return true; },
		onFailure: function(transport) { alert('Error'); } 
	});	
}

function screenSize() {
	if (typeof( window.innerWidth ) == 'number') {
	//Non-IE
		var screenWidth = window.innerWidth;
		var screenHeight = window.innerHeight;
	} else {
		if(document.documentElement && (document.documentElement.clientWidth || document.documentElement.clientHeight)) {
			//IE 6+ in 'standards compliant mode'
			var screenWidth = document.documentElement.clientWidth;
			var screenHeight = document.documentElement.clientHeight;
		} else {
			if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
				//IE 4 compatible
				var screenWidth = document.body.clientWidth;
				var screenHeight = document.body.clientHeight;
			} //end if
		} // end if-else
	} //end if-else
	
	return new Array(screenWidth,screenHeight);
} //end function

function fadeImage(img) {
	//var URL = unescape(window.document.location);
	//if (URL.indexOf("?") > -1) {
		var imgs = $(img).readAttribute("images").split(',');
		if (imgs.length > 1) {
			var imgIndex = $(img).readAttribute("imgIndex");
			if (imgIndex == null) {
				$(img).setAttribute("imgIndex",1);
			} else if (parseInt(imgIndex) >= imgs.length-1) {
				$(img).setAttribute("imgIndex",0);
			} else {
				$(img).setAttribute("imgIndex",(parseInt(imgIndex)+1));
			}
			
			$(img).width = $(img).getWidth();
			$(img).height = $(img).getHeight();
			//$(img).style.opacity = '0.5';
			//$(img).src='/images/spacer.gif';
			//$(img).style.background = 'url(' + imgs[$(img).readAttribute("imgIndex")] +')';
			//alert($(img).readAttribute("imgIndex") + ' test ' + imgs.length);
			$(img).src=imgs[$(img).readAttribute("imgIndex")];
			
		} //end if
	//}
}

function autoGrow(minHeight,maxHeight) {
	var minHgt = minHeight ? minHeight:15;
	var maxHgt = maxHeight ? maxHeight:150;

	if ($('textarea_holder')) {
		$('textarea_holder').style.width=($('comments').getWidth()-5) + 'px';
		var html = $('comments').value; /*.replace(/(<|>)/g, '');*/
		html = html.replace(/\n/g, '<br>');
		$('textarea_holder').innerHTML = html;
		if (html == '') {
			$('comments').style.height=minHgt+ 'px';
		} else {
			var textHeight = $('textarea_holder').getHeight()+15;
			if (textHeight < minHgt) { textHeight = minHgt; }
			if (textHeight > maxHgt) { textHeight = maxHgt; $('comments').style.overflow='auto'; } else { $('comments').style.overflow='hidden'; }
			$('comments').morph('height:' + (textHeight)+ 'px', { duration:0.3 }); 
		}
	}
}

function postComment(type) {
	var type = type ? type: 'newsletter';
	var errors = false;
	var errorList = '';
	
	if ($('email').value != '' && valEmail($('email').value) == false) {
		$('email').style.borderColor = '#cc0000';
		errors = true;
		errorList = errorList + '<li>Please enter a valid email address.</li>';
	} else { $('email').style.borderColor = ''; }

	if (trim($('username').value) == '') {
		$('username').style.borderColor = '#cc0000';
		errors = true;
		errorList = errorList + '<li>Please enter your name.</li>';
	} else { $('username').style.borderColor = '';}
	
	if (trim($('comments').value) == '') {
		$('comments').style.borderColor = '#cc0000';
		errors = true;
		errorList = errorList + '<li>Please enter a comment.</li>';
	} else { $('comments').style.borderColor = '';}

	// Return errors
	if (errors == true) {
		$('errorBox').innerHTML = errorList;
	} else {
		$('postingComment').style.height = $('commentInput').getHeight() + 'px';
		$('commentInput').style.display='none';
		$('postingComment').style.display='';
		$('errorBox').innerHTML = '';
		
		// Post comment to server.
		
		if (type == 'newsletter') {
			
			new Ajax.Request('/includes/newsletterMain.cfm?' + Math.random() * 999999, {
				method:'post',
				postBody:'comments=add&nid=' + $('nid').value + '&aid=' + $('aid').value + '&name=' + $('username').value + '&email=' + $('email').value +  '&comment=' + $('comments').value,  //'&notify=' + $('notify').checked +
				onSuccess: function(transport) { 
					$('postingComment').insert({ before:transport.responseText });
					$('postingComment').style.display='none';$('commentInput').style.display=''; $('comments').value=''; autoGrow(30,300); },
				onException: function(req,exception) { alert('Error: ' + exception.description); $('postingComment').style.display='none';$('commentInput').style.display=''; return true; },
				onFailure: function(transport) { alert('Error'); $('postingComment').style.display='none';$('commentInput').style.display=''; } 
			});
			
		} else {
			new Ajax.Request('/includes/blog.cfm?' + Math.random() * 999999, {
				method:'post',
				postBody:'comments=add&aid=' + $('aid').value + '&name=' + $('username').value + '&email=' + $('email').value + '&comment=' + $('comments').value,  //'&notify=' + $('notify').checked +
				onSuccess: function(transport) { 
					$('postingComment').insert({ before:transport.responseText });
					$('postingComment').style.display='none';$('commentInput').style.display=''; $('comments').value=''; autoGrow(30,300); },
				onException: function(req,exception) { alert('Error: ' + exception.description); $('postingComment').style.display='none';$('commentInput').style.display=''; return true; },
				onFailure: function(transport) { alert('Error'); $('postingComment').style.display='none';$('commentInput').style.display=''; } 
			});			
		}
	
	} //end if else
}


function showAllComments() {
	$('showMoreComments').morph('opacity:0; filter:alpha(opacity=0)', { duration:0.3, 
		afterFinish: function() { 
			$('showMoreComments').style.overflow = 'hidden';
			$('showMoreComments').morph('height:1px', { duration:0.2 ,afterFinish: function() { $('showMoreComments').style.display ='none'; }});
		}
	});
	$('hiddenCommentsWrapper').morph('height:' + $('hiddenComments').getHeight() + 'px', { duration:0.5, 
		afterFinish: function() { 
			$('hiddenCommentsWrapper').style.height=''; 		
		}
	});
}


function valEmail(email) {
	invalidChars = " /:,;"
	if (email == "Required") {
		return false
	}
	for (i=0; i<invalidChars.length; i++) {
		badChar = invalidChars.charAt(i)
		if (email.indexOf(badChar,0) != -1) {
			return false
		}
	}
	atPos = email.indexOf("@",1)
	if (atPos == -1) {
		return false
	}
	if (email.indexOf("@",atPos+1) != -1) {
		return false
	}
	periodPos = email.indexOf(".",atPos)
	if (periodPos == -1) {
		return false
	}
	if (periodPos+3 > email.length)	{
		return false
	}

	return true
}


function ltrim(str) {
	while (1) {
		if (str.substring(0, 1) != " " && str.substring(0, 1) != "\n" && str.substring(0, 1) != "\r")
		break;
		str = str.substring(1, str.length);
	} //end while
	
	return str;
} //end function


function rtrim(str) {
	while (1) {
		if (str.substring(str.length - 1, str.length) != " " && str.substring(str.length - 1, str.length) != "\n" && str.substring(str.length - 1, str.length) != "\r")
		break;
		str = str.substring(0, str.length - 1);
	} //end while
	
	return str;
} //end function


function trim(str) {
	return ltrim(rtrim(str));
} //end function
