// =========
// = Cufon = 
// =========
Cufon.replace('.clarendon, h1.pgtitle, .blogright h2, #follow-promo h2', { fontFamily: 'clarendon' } );
Cufon.replace('.hpbottom h1', { fontFamily: 'gotham' } );

// =================
// = Current State =
// =================

var url = window.location;

// Homepage
	if (url.pathname == '/') {
		$('a[href=/]').addClass('selected');
	}

// Secondary Nav 
	$('a[href='+ url.href +']').addClass("current");


// =====================
// = Flash Replacement =
// =====================
$flash = $("#flash");

if ($flash.length > 0) {
	$flash.flash({
		src: "/_flash/home.swf",
		width:  610,
		height: 372,
		wmode:"transparent"
	});	
};


// =================
// = Comments Form =
// =================

var $commentform = $('#comments-form'),
	$ajaxmessage = $('#ajax-comment').html('<img src="/_images/ajax-loader.gif" alt="" class="loading"/>');

	$ajaxmessage.find('img.loading').hide();
	$commentform.parent().wrap('<div style="min-height:355px"/>');
	successful = false;

$('#comment-submit').click(function(){
	$commentform.find("input[name=preview]").attr('value', '');
});

$('#comment-preview').click(function(){
	$commentform.find("input[name=preview]").attr('value', '1');
});

if ($commentform.length > 0) {
	  $commentform.ajaxForm({
		target: $ajaxmessage,
		beforeSubmit: function(data, $form, opt){
			$ajaxmessage.html('<img src="/_images/ajax-loader.gif" alt="" class="loading"/>');
			$ajaxmessage.find('img.loading').show();
			$commentform.parent().fadeOut(500);
		},
		success: function(){
			if (successful != true ){
				$commentform.parent().fadeIn(400);
			}
		}
	});
}; //end of if


// ===================
// = Blog Images Fix =
// ===================

$('.blogentry img').each(function(){
	var img = $(this);
	if (img.width() > 300) {
		img.css( { 'float': 'none', display: 'block' } );
	}
});

// ================
// = Blog Gallery =
// ================

var customcall = $.galleria.customcallback,
	$galleryimage = $('#gallery').prepend('<div class="galleryimage"/>').find('div:first');
	opacityfade = 0.5;
	fadelength = 800;
	
$("#gallery ul").galleria({
	history: false,
	insert: 'div.galleryimage',
	onImage   : function(image,caption,thumb) { // let's add some image effects for demonstration purposes

		// fade in the image & caption
		image.css('display','none').fadeIn(fadelength, function(){
			$galleryimage.find('.oldreplaced').not(":animated").remove();
		});
		caption.css('display','none').fadeIn(fadelength);

		// fetch the thumbnail container
		var _li = thumb.parents('li');

		// fade out inactive thumbnail
		_li.siblings().children('img.selected').fadeTo(500,opacityfade);

		// fade in active thumbnail
		thumb.fadeTo('fast',1).addClass('selected');

		// add a title for the clickable image
		image.attr('title','Next image >>');
	},
	onThumb : function(thumb) { // thumbnail effects goes here

		// fetch the thumbnail container
		var _li = thumb.parents('li');

		// if thumbnail is active, fade all the way.
		var _fadeTo = _li.is('.active') ? '1' : opacityfade;

		// fade in the thumbnail when finnished loading
		thumb.css({display:'none',opacity:_fadeTo}).fadeIn(fadelength);

		// hover effects
		thumb.hover(
			function() { _li.children('img:animated').stop(); thumb.fadeTo('fast',1); },
			function() { _li.children('img:animated').stop(); _li.not('.active').children('img').fadeTo('fast',opacityfade); } // don't fade out if the parent is active
		);
	}
}).removeClass('loading').find('li:first').addClass('active');


// ===============
// = Google Maps =
// ===============

// API Keys
// Staging
// ABQIAAAAaT3ZKHjRWtZOoYxO8gTAuRTXuEQwU_GPfqaGmPCGyB4TPTMTOBR6s9Pj2jozP1CZhq1wgocc90DkdA

// Live
// ABQIAAAAaT3ZKHjRWtZOoYxO8gTAuRTX_tpEPYkSI3Ehuk8d1mcuZfVEHhQVzyWPFg_xdgcHHvkYxE4oH2LC0w


function createMarker(point,html) {
    var marker = new GMarker(point);
    GEvent.addListener(marker, "click", function() {
        marker.openInfoWindowHtml(html);
    });
    return marker;
}

function mapLoad() {
    if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map"));
        map.addControl(new GSmallMapControl());
        map.setCenter(new GLatLng(41.887135,-87.644205), 15);
        var point = new GLatLng(41.885894, -87.644924);
        var marker = createMarker(point,'<div style="width:150px;height:55px"><h4>Urban Initiatives</h4><p style="line-height:1.5">\
		650 W Lake St<br/>\
		Chicago, IL 60661-1000</p></div>');
        map.addOverlay(marker);
    }
}

var $map = $('#map');

if ($map.length > 0){
	mapLoad(); // Google Maps
}
