// default.js

$(function(){
	
	// slideshows
	$('#slideshow').cycle({ 
    	fx:     'fade'
	});
	
	$('#stroll,#gallery').cycle({ 
    	fx:     'fade',
		activePagerClass: 'sel',
		pager:	'#thumbnails',
		pagerAnchorBuilder: thumbnailBuilder,
		updateActivePagerLink: thumbnailHighlight
	});
	
	// map
	$('#map').each(function(){
		if (GBrowserIsCompatible()) {
			
			var building = $('#map>.bld').html();
			var sTaddress = $('#map>.st').html();
			var sFaddress = $('#map>.sf').html();
			var sTdirections = 'http://www.google.com/maps?f=d&hl=en&saddr=' + '' + '&daddr=' + sTaddress;
			var sFdirections = 'http://www.google.com/maps?f=d&hl=en&saddr=' + sTaddress + '&daddr=';

			var sTinfo = '<div style="text-align: left;"><a href="http://www.craftsalliance.com/">' + building + '</a><br>' + sFaddress + '<br><br>Get directions: <a href="' + sTdirections + '">To here</a> - <a href="' + sFdirections + '">From here</a><br><br></div>';
			$(this).empty();
			
        	mapA = new GMap2(this);
			mapA.addControl(new GSmallMapControl());
			//mapA.addControl(new GMapTypeControl());
			//mapA.addMapType(G_PHYSICAL_MAP) ; 

        	geocoder = new GClientGeocoder();

			showAddress(sTaddress, sTinfo);
		}	
	});
	
	// email list form
	$('#email_list').ajaxForm({
		dataType		:	'json',
		beforeSubmit	:	function(){
								$('#email').css({background:'#ffffff'});
								if(checkEmail($('#email'))){
									return true;
								} else {
									$('#email').css({background:'#b3b2a0'});
									return false;
								}
							},  
        success			:	function(response, status){
								if(response.success == true){
									$('#email_list').fadeOut();
								} else {
									$('#email').css({background:'#b3b2a0'});
								}
							}
	});
	
	// email list input 
	$('#email_list input').focus(function(){														 
		if($(this).val() == $(this).attr('title')){
			$(this).val('');
		}
	}).blur(function(){
		if($(this).val() == ''){
			$(this).val($(this).attr('title'));
		}
	});
	
	$('#participants>tbody>tr:odd').addClass('odd');
	
	$(".gallery").fancybox({
		'titlePosition'	: 'over',
		'titleFormat'		: formatTitle
	});
	
});

//
function checkRegexp(obj,regexp) {
	if (!(regexp.test(obj.val()))){
		return false;
	} else {
		return true;
	}
}

//
function checkEmail(obj){
	return checkRegexp(obj, /^((([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+(\.([a-z]|\d|[!#\$%&'\*\+\-\/=\?\^_`{\|}~]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])+)*)|((\x22)((((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(([\x01-\x08\x0b\x0c\x0e-\x1f\x7f]|\x21|[\x23-\x5b]|[\x5d-\x7e]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(\\([\x01-\x09\x0b\x0c\x0d-\x7f]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF]))))*(((\x20|\x09)*(\x0d\x0a))?(\x20|\x09)+)?(\x22)))@((([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|\d|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.)+(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])|(([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])([a-z]|\d|-|\.|_|~|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])*([a-z]|[\u00A0-\uD7FF\uF900-\uFDCF\uFDF0-\uFFEF])))\.?$/i);
}

//
function showAddress(address, info) {
	if (geocoder) {
		geocoder.getLatLng( address, function(point) {
			if (!point) {
				alert(address + " not found");
            } else {
				mapA.setCenter(point, 10, G_PHYSICAL_MAP);
				var marker = new GMarker(point);
				mapA.addOverlay(marker);
			  
				GEvent.addListener(marker, "click", function() {
					marker.openInfoWindowHtml(info);
				});
				
				marker.openInfoWindowHtml(info);
            }
		});
	}
}

//
function thumbnailBuilder(idx, slide) {
	var imageLocation	= slide.src;
	var domainName		= document.domain;
	
	imageLocation = imageLocation.substring(domainName.length + 7);
	
	$('#thumbnails').width(((idx+1) * 160) + 'px');
	
	return '<a href="#" id="thumb_'+idx+'"><img src="/image.php?action=resize&m_w=120&m_h=70&q=80&p=top_left&path=' + imageLocation + '" /></a>';
};

//
function thumbnailHighlight(container, slide) {
	$(container+'>a').removeClass('sel');
	$('#thumb_'+slide).addClass('sel');
	
	var leftPosition = 0;
	
	if(slide > 0){ leftPosition = -((slide - 1) * 159); }
	
	$(container).animate({left: leftPosition+'px'});
}

function formatTitle(title, currentArray, currentIndex, currentOpts){
	return '<span id="fancybox-title-over">' + $('.gallery:eq(' + currentIndex + ')').text() + '</span>';
}