/* Randomize home page navigation block images
Author: Maggie Costello, Filament Group Inc.
Date: September 2007
Notes: based on the jQuery library (jquery.com), version 1.2.1
*/

function preloadImgs(){
	var imgs = [		
		'images/home_nav/thumb_acton_barn.jpg',
		'images/home_nav/thumb_brookline_conversion.jpg',
		'images/home_nav/thumb_brookline_eaves.jpg',
		'images/home_nav/thumb_brookline_indooroutdoor.jpg',
		'images/home_nav/thumb_building_science.jpg',
		'images/home_nav/thumb_carpentry.jpg',
		'images/home_nav/thumb_construction.jpg',
		'images/home_nav/thumb_crew_anita.jpg',
		'images/home_nav/thumb_crew_cador.jpg',
		'images/home_nav/thumb_crew_catherine.jpg',
		'images/home_nav/thumb_crew_corky.jpg',
		'images/home_nav/thumb_crew_doug.jpg',
		'images/home_nav/thumb_crew_geoff.jpg',
		'images/home_nav/thumb_crew_jeffj.jpg',
		'images/home_nav/thumb_crew_karin.jpg',
		'images/home_nav/thumb_crew_karla.jpg',
		'images/home_nav/thumb_crew_kerry.jpg',
		'images/home_nav/thumb_crew_maria.jpg',
		'images/home_nav/thumb_crew_merlyn.jpg',
		'images/home_nav/thumb_crew_nick.jpg',
		'images/home_nav/thumb_crew_paul.jpg',
		'images/home_nav/thumb_crew_rick.jpg',
		'images/home_nav/thumb_crew_steveb.jpg',		
		'images/home_nav/thumb_crew_josh.jpg',
		'images/home_nav/thumb_crew_mike_duclos.jpg',
		'images/home_nav/thumb_crew_mcgann.jpg',		
		'images/home_nav/thumb_decks.jpg',
		'images/home_nav/thumb_design_planning.jpg',
		'images/home_nav/thumb_energy.jpg',
		'images/home_nav/thumb_followup.jpg',
		'images/home_nav/thumb_landscape_renovation.jpg',
		'images/home_nav/thumb_landscaping.jpg',
		'images/home_nav/thumb_masonry.jpg',
		'images/home_nav/thumb_moroccan_bath.jpg',
		'images/home_nav/thumb_newton_renovation.jpg',
		'images/home_nav/thumb_newton_scandinavia.jpg',
		'images/home_nav/thumb_victorian.jpg',
		'images/home_nav/thumb_wellesley_dreamhome.jpg',
		'images/home_nav/thumb_wellesley_wholehouse.jpg',
		'images/home_nav/thumb_weston_addition.jpg'
	];
	var loadImgs = new Array();
	$(imgs).each(function(i){
		loadImgs[i] = new Image();
		loadImgs[i].src = imgs[i];	
	});
}

preloadImgs();


function inDateRange(startDate, endDate) {
	var now = new Date();
	var today = new Date(now.getFullYear(), now.getMonth(), now.getDate());					
	if (today >= startDate && today < endDate) { return true; }
	else { return false; }	
};


/*
 * Email Defuscator - jQuery plugin 1.0 alpha
 *
 * Copyright (c) 2007 Joakim Stai
 *
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * Revision: $Id$
 *
 */

/**
 * Converts obfuscated email addresses into normal, working email addresses.
 *
 * @name defuscate
 * @param Boolean link If true, all defuscated email addresses will be turned into links, defaults to true (optional)
 * @descr Converts obfuscated email addresses into normal email addresses
 */

jQuery.fn.defuscate = function( settings ) {
    settings = jQuery.extend({
        link: true
    }, settings);
    var regex = /\b([A-Z0-9._%-]+)\([^)]+\)((?:[A-Z0-9-]+\.)+[A-Z]{2,6})\b/gi;
    return this.each(function() {
        if ( $(this).is('a[@href]') ) {
            // If it's an <a> element, defuscate the href attribute
            $(this).attr('href', $(this).attr('href').replace(regex, '$1@$2'));
            // Make sure that the element's contents is not made into a link
            var is_link = true;
            //alert($(this).attr('href'));
        }
        // Defuscate the element's contents
        $(this).html($(this).html().replace(regex, (settings.link && !is_link ? '<a href="mailto:$1@$2">$1@$2</a>' : '$1@$2')));
  });
};


// on DOM ready
$(function(){
	$('.homeNavBlock img').css({ opacity:0 });
	var filepath = 'images/home_nav/';
	
	var serviceImages = [		
		'thumb_building_science.jpg',
		'thumb_carpentry.jpg',
		'thumb_construction.jpg',
		'thumb_decks.jpg',
		'thumb_design_planning.jpg',
		'thumb_energy.jpg',
		'thumb_followup.jpg',
		'thumb_landscaping.jpg',
		'thumb_masonry.jpg'
	];
	
	var workImages = [
		'thumb_acton_barn.jpg',
		'thumb_brookline_conversion.jpg',
		'thumb_brookline_eaves.jpg',
		'thumb_brookline_indooroutdoor.jpg',
		'thumb_landscape_renovation.jpg',
		'thumb_landscaping.jpg',
		'thumb_moroccan_bath.jpg',
		'thumb_newton_renovation.jpg',
		'thumb_newton_scandinavia.jpg',
		'thumb_victorian.jpg',
		'thumb_wellesley_dreamhome.jpg',
		'thumb_wellesley_wholehouse.jpg',
		'thumb_weston_addition.jpg'
	];
	
	var crewImages = [
		'thumb_crew_anita.jpg',
		'thumb_crew_cador.jpg',
		'thumb_crew_catherine.jpg',
		'thumb_crew_corky.jpg',
		'thumb_crew_doug.jpg',
		'thumb_crew_geoff.jpg',
		'thumb_crew_jeffj.jpg',
		'thumb_crew_karin.jpg',
		'thumb_crew_karla.jpg',
		'thumb_crew_kerry.jpg',
		'thumb_crew_maria.jpg',
		'thumb_crew_nick.jpg',
		'thumb_crew_paul.jpg',
		'thumb_crew_rick.jpg',
		'thumb_crew_steveb.jpg',
		'thumb_crew_josh.jpg',
		'thumb_crew_mike_duclos.jpg',
		'thumb_crew_mcgann.jpg'	
	];	

	var serviceNum = Math.floor(Math.random()*serviceImages.length+1);	
	$('#servicePhoto').attr('src', filepath + serviceImages[serviceNum-1]).fadeTo(1000, 0.99);
	
	var workNum = Math.floor(Math.random()*workImages.length+1);	
	$('#workPhoto').attr('src', filepath + workImages[workNum-1]).fadeTo(1000, 0.99);
	
	var crewNum = Math.floor(Math.random()*crewImages.length+1);	
	$('#crewPhoto').attr('src', filepath + crewImages[crewNum-1]).fadeTo(1000, 0.99);
	


	// home page What's New rotating content 
	$('#welcome-whats-new div').hide();
	
	/* NOTE: start and end dates must be specified in the following format:
		- 4-digit year
		- 1 or 2 digit month, where the numbers range from 0 (January) to 11 (December)
		- 1 or 2 digit day
		ex: new Date(yyyy, mm, dd);
	*/
	
	// march 18 - 31
	/*
	//var startDate1 = new Date(2010, 2, 18);
	//var endDate1 = new Date(2010, 2, 31);
	//if (inDateRange(startDate1, endDate1)) { $('#welcome-march18').show(); }
	
	// april 1 - 14
	//var startDate2 = new Date(2010, 3, 1);
	//var endDate2 = new Date(2010, 3, 14);
	//if (inDateRange(startDate2, endDate2)) { $('#welcome-april1').show(); }
	
	// april 15 - 28
	//var startDate3 = new Date(2010, 3, 15);
	//var endDate3 = new Date(2010, 3, 28);
	//if (inDateRange(startDate3, endDate3)) { $('#welcome-april15').show(); }
	
	// april 29 - may 12
	var startDate4 = new Date(2010, 3, 29);
	var endDate4 = new Date(2010, 4, 12);
	//if (inDateRange(startDate4, endDate4)) { $('#welcome-april29').show(); }
	
	// may 13 - 26
	//if (inDateRange(new Date(2010, 4, 13), new Date(2010, 4, 26))) { $('#welcome-may13').show(); }
	*/
	
	// may 27 - june 16
	//if (inDateRange(new Date(2010, 4, 27), new Date(2010, 5, 16))) { $('#welcome-may27').show(); }
	
	//june 17 - July 7
	//if (inDateRange(new Date(2010, 5, 10), new Date(2010, 6, 7))) { $('#welcome-june17').show(); }
	
	//july 8 - july 28
	$('#welcome-july8').show(); 
	
	/* future date ranges for home page rotation:

	july 29 - aug 18
	aug 19 - sept 8
	sept 9 - sept 29
	sept 30 - oct 21	
	*/
	
	// decrypt email addresses
	$('.email-encrypt').defuscate();

});



