/*
	functions.site.js
	JQuery / DOM Interaction Functions
	Creator: Matt Kircher
*/

/* GENERAL */
function setupPage(){
	translateEmails();		//changes unlinked email address to usable ones (spam protection)	
	
	//homepage nav
	$(".home #main-nav > ul").addClass('sf-vertical')
	
	//all navs
	$("#main-nav > ul").addClass('sf-menu')
	.supersubs({
		minWidth:    12,
		maxWidth:    36,
		extraWidth:  1    
	})
	.superfish({
		delay: 500,
		speed: 'fast',
		autoArrows: false,
		dropShadows: true
	});
	
	$('#main-nav').css({ visibility:'visible' });	//for IE, so superfish doesn't shutter/flicker
	
	$('#content, #footer-nav ul, #footer .hcard').addClass('clearfix');
	
	$('#main-content :header:eq(0)').addClass('lined_header');
	
	$('.previous_page_link').click(function(){ history.go(-1); return false; });
	
	//representative projects side links
	if($('.about #representive_projects').length){
		$('#sub-content').append('<hr /><h4 class="subhead">Representive Projects:</h4><ul class="small_project_list"></ul>');
		$('#representive_projects dt').each(function(index){
			$(this).attr('id', 'project'+index);
			$('#sub-content .small_project_list').append('<li><a href="#project'+index+'">'+$(this).html()+'</a></li>');
		});
		$('#representive_projects dd').append('<p><a href="#content">Back to the top</a></p>');
	}
	
	//professional experience side links
	if($('.about #professional-experience').length){
		$('#sub-content').append('<hr /><h4 class="subhead">Professional Experience:</h4><ul class="small_project_list"></ul>');
		$('#professional-experience dt').each(function(index){
			$(this).attr('id', 'project'+index);
			$('#sub-content .small_project_list').append('<li><a href="#project'+index+'">'+$(this).html()+'</a></li>');
			$('#sub-content .small_project_list li:last strong').remove();
		});
		$('#professional-experience dd').append('<p><a href="#content">Back to the top</a></p>');
	}
	
	//format project lists
	if($('#project-listing').length){
		$('#project-listing tbody tr')
		.find('> td:eq(0)').addClass('project_name').end()
		.find('> td:eq(1)').addClass('project_amount').end()
		.find('> td:eq(2)').addClass('project_complete').end()
		.hover(
			function(){ $(this).css({ backgroundColor:'#F6F6F6' }); },
			function(){ $(this).css({ background:'none' }); }
		);
		
		var value = 0;
		$('.project_amount').each(function(){
			n = $(this).text();
			n = parseFloat(n.replace(/,/gi,'').substring(1));
			value += n;
		});
		
		total = '$'+addCommas(value.toFixed(2));
		$('<tr><th class="project_total" colspan="3"><strong>Total Value:</strong> '+total+'</td></th>').insertAfter($('#project_list thead tr:last'));
	}
	
	//client side links
	if($('.clients #client-listing').length){
		
		var sector_count = 1;
		$('#client-listing .sector').each(function(index){
			$(this).attr('id', 'sector-'+sector_count+'-'+index);
			$('#sub-content').append('<h4 class="subhead">'+$(this).find(' > h3').text()+'</h4><ul class="small_project_list"></ul><br /><br />');
			
			$(this).find('.category').each(function(index){
				$(this).attr('id', 'category'+sector_count+'-'+index);
				$('#sub-content .small_project_list:last').append('<li><a href="#category'+sector_count+'-'+index+'">'+$(this).find('h4').text()+'</a></li>');
			});
			
			sector_count++;
		});
		
		$('.small_project_list a').click(function(){
			var source = $('#client-listing .category'+$(this).attr('href'))
			$(source).show().siblings().hide(); 
			$(source).parent().siblings().find(' > .category').hide();
			
			$('#client-listing > p').remove();
			var link_top 	  = $('<p class="emphasized_txt"><a href="#content">Back to the top</a> | <a class="showall_link" href="#content">Show all clients</a></p>')
			var link_bottom = $('<p class="emphasized_txt"><a class="showall_link" href="#content">Show all clients</a></p>')
			$(link_top).insertAfter($(source).parent());
			$(link_bottom).insertBefore($(source).parent());
			
			$('.showall_link').click(function(){	
				$('#client-listing .category').show();
				$('#client-listing > p').remove();
				resizeContentDivs();
			});
			resizeContentDivs();
			
			return false;
		});
		
	}
	
	$('#content > div').equalHeights();
}

function resizeContentDivs(){
	$('#content').find('> div').removeAttr('style').equalHeights();	
}

function addCommas(nStr)
{
	nStr += '';
	x = nStr.split('.');
	x1 = x[0];
	x2 = x.length > 1 ? '.' + x[1] : '';
	var rgx = /(\d+)(\d{3})/;
	while (rgx.test(x1)) {
		x1 = x1.replace(rgx, '$1' + ',' + '$2');
	}
	return x1 + x2;
}


//makes email tags invisible to spiders / spammers
function translateEmails(){
	$('span.email, address.email').each(function(){
		var spt = $(this);
		var at = / at /;
		var dot = / dot /g;		
		
		//EXAMPLE: <span class="email" title="link title | email address | email subject"> link content </span>		
		
		var inner_content = $(spt).html();						//inner HTML of span tag
		var t = $(spt).attr('title');						//email, link options from title attribute
		
		var title = t.substring(0, t.indexOf('|'));				//title for the link
		t = t.substring(t.indexOf('|')+1);
		
		var addr = t.substring(0, t.indexOf('|'));				//email address from id attribute
		addr = addr.replace(at,"@").replace(dot,".");				//replace words with chars
		
		var subject = t.substring(t.indexOf('|')+1);				//subject for email, if needed
		var fulladdr = ($.trim(subject) != "")?addr+'?subject='+subject:addr;	//full address formed with subject, if needed
		
		inner_content = ($.trim(inner_content) == "" || $.trim(inner_content) == "&nbsp;")?addr:inner_content;
		
		$(spt).after('<a href="mailto:'+fulladdr+'" title="'+title+'">'+ inner_content +'</a>')
		.hover(function(){window.status="Send an email!";}, function(){window.status="";});
		$(spt).remove();
	});
}

function formatBidRoomTable(){
	
	$('#current-bids-table tbody tr:even').addClass('shaded');
	$('#current-bids-table tbody tr').hover(
		function(){ $(this).addClass('hover'); },
		function(){ $(this).removeClass('hover'); }
	);
}

function setupBidRoomClickTrack(sessionVars){
	
	$('#content a').live('click', function(event){
		
			//date
			var d = new Date();
			var m = d.getMonth()+1;
			var h = (d.getHours() > 12)?eval(d.getHours()%12):d.getHours();
			var a = (d.getHours() > 12)?'pm':'am';
			var time = m+'/'+d.getDate()+'/'+d.getFullYear()+' at '+h+':'+d.getMinutes()+a;
			
			//title
			var project = $(this).attr('title');
			
			//link
			var linkURI = $(this).attr('href')
			var linkTitle = project.substr(project.indexOf('|')+1);
			
			project = project.substr(0, project.indexOf('|'));
			
			$.ajax({
				url: 'includes/forms.process.php?action=bidRoomTrack',
				data: {
					name:		sessionVars.name, 
					company:	sessionVars.company, 
					email:		sessionVars.email, 
					telephone:	sessionVars.telephone, 
					time: 		time, 
					link: 		linkURI, 
					file: 		linkTitle, 
					title: 	project,
					click_type:	(event.button != 0)?'clicked':'right-clicked'
				},
				success: function(){},
				dataType: JSON
			});
		}
	);
}




/* INITIALIZATION */
$(document).ready(function(){
	setupPage();
});

