function setCookie(c_name,value,expiredays) {
	var expiration_date=new Date();
	expiration_date.setDate(expiration_date.getDate()+expiredays);
	document.cookie=c_name+ "=" +escape(value)+ ((expiredays==null) ? "" : ";expires="+expiration_date.toGMTString());
}

function getCookie(c_name) {
	if (document.cookie.length>0) {
  		c_start=document.cookie.indexOf(c_name + "=");
	  	if (c_start!=-1) { 
	    	c_start=c_start + c_name.length+1; 
	    	c_end=document.cookie.indexOf(";",c_start);
	    	if (c_end==-1) {
				c_end=document.cookie.length;
			}
	    	return unescape(document.cookie.substring(c_start,c_end));
	    }
  	}
	return "";
}

function initPortfolioView() {
	var buttons = (document.getElementById("views")) ? document.getElementById("views").getElementsByTagName("a") : null;
	if(buttons!=null) {
		if(view_buttons!=null) {
			for(var i=0;i<buttons.length;i++) {
				view_buttons.push(buttons[i].id);
			}
		}
	}
	var containers = (document.getElementById("slider")) ? document.getElementById("slider").getElementsByTagName("div") : null;
	if(containers!=null) {
		for(var i=0;i<containers.length;i++) {
			if(containers[i].id.indexOf("project_")!=-1) {
				project_containers.push(containers[i]);
			}
		}
	}
}

function initGalleries() {
	//var bucket = "";
	for(var i=2; i<projects_in_gallery.length; i=i+3) {
		var bucket = projects_in_gallery[i];
		//alert(projects_in_gallery[i]);
	}
}

function setPortfolioView(obj) {
	alert("YO!");
	if(isPortfolio) {
		if(obj!=null) { // if a view button is clicked
			setCookie('view', obj.id, 30);

		} else { // if no view button is being clicked (we go from memory)
			
			if(getCookie("view")==null || getCookie("view")=="null" || getCookie("view")==undefined || getCookie("view")=="") {
				setCookie('view', "thumb", 30);
			}

		}
		
		// select appropriate view button (if it's a project then the selection is hardcoded in the template)
		if(!isProject) {
			if(view_buttons!=null) {
				for(var i=0;i<view_buttons.length;i++) {
					if(view_buttons[i] == getCookie("view")) {
						document.getElementById(view_buttons[i]).className="selected";
					} else {
						document.getElementById(view_buttons[i]).className="";
					}
				}
			}
		}
		
		// switch layout
		if(getCookie("view")=="list") {
			if(!isProjectsByClient) {
				$('div.thumb-module').removeClass().addClass('list-module');
				viewCorrector();
			}
		} else if(getCookie("view")=="thumb") {
			$('div.list-module').removeClass().addClass('thumb-module');
			viewCorrector();
		} else {
			// load project
		}
	}
}


function AjaxBeforeSend(){
	jQuery('.maincontent').block({  
		message: jQuery('div#spinner'),
		css: {
			border: 'none', 
			padding: '0px', 
			backgroundColor: 'transparent'
		}  
	});
	jQuery('.blockUI').css({'display':'none'}).fadeIn(500);
	jQuery('.blockUI embed').css({'visibility':'hidden'});
	setTimeout("jQuery('.blockUI embed').css({'visibility':'visible'});", 510);
	return;
}

function AjaxOnComplete(){
	setTimeout("jQuery('.maincontent').unblock();", 1000);
	setTimeout("jQuery('.blockUI embed').css({'visibility':'hidden'});", 1000);
	return;
}

function loadGallery(url, title){
	AjaxBeforeSend();
	var regExp = /\s+/g;
	url = url.replace(regExp,'%20');
	$('#portfolio').load(url + ' .portfolio-bg', function(){
		isProject = false;
		isProjectsByClient = false;
		var parent_title = "";
		initPortfolioView();
		$('div#slider').codaSlider();
		do_sIFR();
		setTitle(null, title);
		AjaxOnComplete();
		pageTracker._trackPageview("http://www.horngroup.com/"+url);
	});
	return false;
}

function loadProject(url, parentTitle, title) {
	//alert("parentTitle = "+parentTitle+"\ntitle = "+title);
	AjaxBeforeSend();
	var regExp = /\s+/g;
	urlEnc = url.replace(regExp,'%20');
	$('#portfolio').load(urlEnc+' .portfolio-bg', function(){
		isProject = true;
		isProjectsByClient = false;
		var parent_title = "";
		setCookie('project',urlEnc,30);
		initPortfolioView();
		findProject(url);
		$('div#slider').codaSlider();
		//setBackToGalleryLink();
		do_sIFR();
		setTitle(null, title);
		AjaxOnComplete();
		pageTracker._trackPageview("http://www.horngroup.com/"+url);
	});
	return false;
}

function loadPortfolioTabs(url, title) {
	AjaxBeforeSend();
	var regExp = /\s+/g;
	url = url.replace(regExp,'%20');
	jQuery('#portfolio').load(url+', .portfolio-bg', function(){
		$('div#slider').codaSlider();
		portfolioLandingPrep();
		do_sIFR();
		setTitle(null, title);
		AjaxOnComplete();
		pageTracker._trackPageview("http://www.horngroup.com/"+url);
	});
	return false;
}

function loadCaseStudy(url, parentTitle, title) {
	AjaxBeforeSend();
	var regExp = /\s+/g;
	url = url.replace(regExp,'%20');
	$('#portfolio').load(url+', .portfolio-bg', function(){
		$('div#slider').codaSlider();
		do_sIFR();
		setTitle(parentTitle, title);
		AjaxOnComplete();
		pageTracker._trackPageview("http://www.horngroup.com/"+url);
	});
	return false;
}

function loadProjectsByClient(url) {
	AjaxBeforeSend();
	var regExp = /\s+/g;
	url = url.replace(regExp,'%20');
	$('#portfolio').load(url+', .portfolio-bg', function(){
		$('div#slider').codaSlider();
		do_sIFR();
		setTitle(parentTitle, title);
		AjaxOnComplete();
		pageTracker._trackPageview("http://www.horngroup.com/"+url);
	});
	return false;
}


/*function setBackToGalleryLink() {
	if(getCookie("view")=="list") {
		$('#back_to_gallery').text("Back to List");
	} else if(getCookie("view")=="thumb") {
		$('#back_to_gallery').text("Back to Thumbnails");
	} else { // project was browsed to directly (has not been to gallery)
		$('#back_to_gallery').text("Back");
	}
}*/

function checkLastProject(obj) {
	if(getCookie("project")!=null && getCookie("project")!="null" && getCookie("project")!="") {
		loadProject(getCookie("project"), null, null);
	} else {
		// load first one in gallery
		//obj.href=null;
		alert("You have not previously viewed a project.  To do so, please click on a project in the gallery.");
	}
}

var projectFoundIn;
var projectFoundAt;
	
/*function findProject(url) {
	var projectFound = false;
	for(var i=0;i<projects_in_gallery.length;i++) {
		if(projects_in_gallery[i].length>0) {
			for(var j=0;j<projects_in_gallery[i].length;j=j+3) {
				//alert(projects_in_gallery[i][j].substr(projects_in_gallery[i][j].indexOf("/horngroup/")+10));
				if(projects_in_gallery[i][j].indexOf(url)!=-1) {
					projectFound = true;
					projectFoundIn = i;
					projectFoundAt = j;
					//alert("projectFoundIn = "+projectFoundIn);
					//alert("projectFoundAt = "+projectFoundAt);
					break;
				}
			}
		}
		if(projectFound) break;
	}
}*/
	
function findProject(url) {
	var projectFound = false;
	for(var i=0;i<projects_in_gallery.length;i=i+3) {
		//alert(projects_in_gallery[i]);
		if(projects_in_gallery[i].length > 0) {
			if(projects_in_gallery[i].indexOf(url)!=-1) {
				//alert("url: "+url+"\nprojects_in_gallery[i]: "+projects_in_gallery[i]);
				projectFound = true;
				projectFoundAt = i;
				//alert(projects_in_gallery[i]);
				break;
			}
		}
		if(projectFound) break;
	}
}

/*function navigateProjects(obj) {
	var nextProject = parseInt(projectFoundAt+2);
	var prevProject = parseInt(projectFoundAt-2);
	//alert(projectFoundIn);
	if(obj.id=="next") {
		if(nextProject < projects_in_gallery[projectFoundIn].length) {
			loadProject(projects_in_gallery[projectFoundIn][nextProject], null, projects_in_gallery[projectFoundIn][nextProject+1]);
		} else {
			loadProject(projects_in_gallery[projectFoundIn][0], null, projects_in_gallery[projectFoundIn][1]);
		}
	} else if(obj.id=="prev") {
		if(prevProject >= 0) {
			loadProject(projects_in_gallery[projectFoundIn][prevProject], null, projects_in_gallery[projectFoundIn][prevProject+1]);
		} else {
			loadProject(projects_in_gallery[projectFoundIn][projects_in_gallery[projectFoundIn].length-2], null, projects_in_gallery[projectFoundIn][projects_in_gallery[projectFoundIn].length]);
		}
	}
}*/

function navigateProjects(obj) {
	var nextProject = parseInt(projectFoundAt+3);
	var prevProject = parseInt(projectFoundAt-3);
	if(obj.id=="next") {
		if(nextProject < projects_in_gallery.length) {
			loadProject(projects_in_gallery[nextProject], projects_in_gallery[nextProject+2], projects_in_gallery[nextProject+1]);
		} else {
			loadProject(projects_in_gallery[0], projects_in_gallery[2], projects_in_gallery[1]);
		}
	} else if(obj.id=="prev") {
		if(prevProject >= 0) {
			loadProject(projects_in_gallery[prevProject], projects_in_gallery[prevProject+2], projects_in_gallery[prevProject+1]);
		} else {
			loadProject(projects_in_gallery[projects_in_gallery.length-3], projects_in_gallery[projects_in_gallery.length-2], projects_in_gallery[projects_in_gallery.length-1]);
		}
	}
}

function viewCorrector() {
	// corrects _module width
	$('.stripViewer:has(.list-module)').css({'width':'915px','padding':'0px 0px 0px 0px'});
	$('.stripViewer:has(.thumb-module)').css({'width':'855px','padding':'15px 30px 15px 30px'});
	// styles list-module borders
	$('.list-module:even').addClass(' right-border');
	$('.list-module:nth-child(12n+1)').addClass(' no-top-border');
	$('.list-module:nth-child(12n+2)').addClass(' no-top-border');

	$('.thumb-module a span').css({opacity: 0});
		
}

function portfolioLandingPrep(){
	jQuery('.nav-box dd a').css({'background':'url(themes/horngroup/images/portfolio_landing_nav_bg_arrow.png) 0px 0px repeat-x'});
	jQuery('.nav-box dd em').css('opacity',0);
	jQuery('.nav-box dd a img').css({'opacity': '0'});
		
	var configPLN = {    
		sensitivity: 10,
		interval: 150,   
		over: arrowIn,
		timeout: 0,
		out: arrowOut
	};
	
	jQuery('.nav-box dd a').hoverIntent(configPLN);
			
	function arrowIn(){
		jQuery(this).prev().fadeTo(250, 1);
		jQuery(this).animate({backgroundPosition: '(260px 0px)'}, 750, 'easeOutExpo');
		jQuery(this).children('img').animate({'opacity':'0'}, 250).animate({'left':'+=18px', 'opacity':'1'}, 350);
	}
	
	function arrowOut(){
		jQuery(this).prev().fadeTo(250, 0);
		jQuery(this).animate({backgroundPosition: '(0px 0px)'}, 350);
		jQuery(this).children('img').animate({'left':'-=18px', 'opacity':'0'}, 250);
	}
	
	jQuery('.nav-box dd a').hover(function(){
		jQuery(this).prev().css('opacity', 0.2);
	},function(){
		jQuery(this).prev().css('opacity', 0);
	});
}

function setTitle(parentTitle, title) {
	document.title = "Horn Group, Inc. > " + "Portfolio > ";
	document.title += (parentTitle!=null && parentTitle != undefined && parentTitle != "null") ? parentTitle + " > " : "";
	document.title += (title!=null && title != undefined && title != "null") ? title : "";
}

function formatImageSinglePost() {
	var imgSrc     = jQuery("#blog-post-container img:first").attr("src");
	var origWidth  = jQuery("#blog-post-container img:first").width();
	var origHeight = jQuery("#blog-post-container img:first").height();
	var newHeight;
	var newWidth;
	var newPosition;
		
	jQuery("#blog-post-container img:first").remove();
	
	jQuery("#blog-post-container").prepend('<div id="blog-post-i"></div>');
	jQuery("#blog-post-i").prepend('<div id="blog-post-j"></div>');
	jQuery("#blog-post-j").prepend('<div id="blog-post-k"></div>');
	jQuery("#blog-post-j").append('<span></span>');
	jQuery("#blog-post-k").prepend('<img src="'+imgSrc+'"/>');
	
	jQuery("#blog-post-i").css({"float":"right","margin":"5px 0px 15px 15px"});
	jQuery("#blog-post-j").css({"position":"relative"});
	jQuery("#blog-post-j span").css({"position":"absolute" ,"top":"0px" ,"left":"0px" ,"display":"block"});

	var isVertical = (origHeight > origWidth) ? true : false;
	var isVert	   = (isVertical) ? "_vert" : "";

	if(isVertical) {
		newWidth   = 150;
		newHeight  = Math.round((newWidth*origHeight)/origWidth);
		jQuery("#blog-post-j span").css({"background":"transparent url(themes/horngroup/images/blog-img-big-vert.png) 0px 0px no-repeat" ,"width":"152px" ,"height":"197px"});
		jQuery("#blog-post-k").css({"width":"137px","height":"181px","overflow":"hidden","position":"relative","top":"5px","left":"5px"});
		jQuery("#blog-post-k img").css({"width":newWidth,"height":newHeight});
	} else {
		newHeight  = 139;
		newWidth   = Math.round((newHeight*origWidth)/origHeight);
		jQuery("#blog-post-j span").css({"background":"transparent url(themes/horngroup/images/blog-img-big-hor.png) 0px 0px no-repeat","width":"197px","height":"152px"});
		jQuery("#blog-post-k").css({"width":"181px","height":"137px","overflow":"hidden","position":"relative","top":"5px","left":"5px"});
		jQuery("#blog-post-k img").css({"width":newWidth,"height":newHeight,"position":"relative"});
	}
}

function formatImageAllPosts() {

	if(jQuery('.content h1').text() == "Posts by Author"){
		if(jQuery('.content h6').text() != ""){
			jQuery('.content img:first').addClass("author-image");
		}
	}
	
	jQuery('.content img').each(function(i){
		var origWidth  = jQuery(this).width();
		var origHeight = jQuery(this).height();
		
		var newHeight;
		var newWidth;

		jQuery(this).wrap('<div id="blog-post-i" style="float:left; margin:5px 15px 25px 0px;"></div>');
		jQuery(this).wrap('<div id="blog-post-j" style="position:relative;"></div>');
		jQuery(this).wrap('<div id="blog-post-k"></div>');
		jQuery(this).parent().parent().append('<span style="position:absolute; top:0px; left:0px; display:block;"></span>');
		
		var isVertical = (origHeight > origWidth) ? true : false;
		var isVert	   = (isVertical) ? "_vert" : "";
		
		// Author Image Exception
		if(jQuery(this).hasClass("author-image")){
			isVertical = true;
		}
		
		if(isVertical) {
			newWidth   = 87;
			if(jQuery(this).hasClass("author-image")){
				newHeight = 116;
			} else {
				newHeight = Math.round((newWidth*origHeight)/origWidth);
			}
			jQuery(this).parent().parent().children('span').css({"background":"transparent url(themes/horngroup/images/blog-img-sm-vert.png) 0px 0px no-repeat" ,"width":"97px" ,"height":"124px"});
			jQuery(this).parent().css({"width":"88px","height":"118px","overflow":"hidden","position":"relative","top":"1px","left":"2px"});
			jQuery(this).css({"width":newWidth,"height":newHeight,"margin":"2px 0px 0px 1px"});
			
			} else {
			
			newHeight = 87;
			newWidth = Math.round((newHeight*origWidth)/origHeight);
			jQuery(this).css({"width":newWidth,"height":newHeight});
			jQuery(this).parent().parent().children('span').css({"background":"transparent url(themes/horngroup/images/blog-img-sm-hor.png) 0px 0px no-repeat","width":"124px","height":"97px"});
			jQuery(this).parent().css({"width":"116px","height":"87px","overflow":"hidden","position":"relative","top":"3px","left":"3px"});
			jQuery(this).css({"width":newWidth,"height":newHeight,"margin":"0px 0px 0px 0px"});
		}
		
	});
	
	var isPostsByAuthor = jQuery('.content h1').text();
	if(isPostsByAuthor == "Posts by Author" && authorHasBio){
		jQuery('.content p:first').css({'margin':'0px 0px 70px 0px'});
		jQuery('#blog-post-i:first').css({'margin':'5px 15px 5px 0px'});
	}
}

function setPortfolioServiceActions(){
	jQuery('.nav-box dl dt ul li a').click(function(){
													
	});
}