/*
 * Cumberland Museum scripts
 *
 * Targets jQuery 1.4.2.
 *
 * Copyright (c) 2010 Dialect Communications Group (dialect.ca)
 *
 * $Package: CM $
 */

/*jslint browser: true, white: true, plusplus: true, newcap: true,
    eqeqeq: true, evil: true, nomen: false,  regexp: true, undef: true,
    onevar: true */

/*global $,pageTracker,_gaq,window,console,Cufon */

Cufon.replace('h1');

var CM = {
	
	initSearch : function () {
		var prompt = 'Search';
		$('#keywords').val(prompt).bind('focus', function () {
			if ($(this).val() === prompt) {
				$(this).val('');
			}
		}).bind('blur', function () {
			if ($(this).val() === '') {
				$(this).val(prompt);
			}
		});	
	},
	
	gallery : null,
	gallery_idx : 0,
	gallery_slug : '',
	
	initGallery : function (data, slug) {
		CM.gallery_slug = slug;
		$.getJSON(data, function (d, s) {
			CM.gallery = d.slides;
			CM.preloadGallery();
		});
		
		
		
		$('#gallery .image img').css('cursor', 'pointer');
		
		$('#gallery .prev').click(function () {
			CM.updateGallery(CM.gallery_idx - 1);
			return false;
		});

		$('#gallery .next, #gallery .image img').click(function () {
			CM.updateGallery(CM.gallery_idx + 1);
			return false;
		});

	},
	
	updateGallery : function (idx) {
		if (idx < 0) {
			idx = CM.gallery.length - 1;
		} else if (idx > CM.gallery.length - 1) {
			idx = 0;
		}
		CM.gallery_idx = idx;
		
		CM.trackPageview('/galleries/' + CM.gallery_slug + '/' + idx);
		CM.trackEvent('Gallery', 'View Image', CM.gallery[idx].src);
		
		$('#gallery .cutline').html(CM.gallery[idx].cutline);
		$('#gallery .credit span').html(CM.gallery[idx].credit);
		$('#gallery .image img').attr('src', CM.gallery[idx].src);
	},
	
	preloadGallery : function () {
		var img = null, i;
		for (i = 0; i < CM.gallery.length; i += 1) {
			img = new Image();
			$(img).attr('src', CM.gallery[i].src);  
		} 
	},
	
	trackPageview : function (href) {
		try {
			_gaq.push(['_trackPageview', href]);
		} catch (e) { }
	},
	
	trackEvent : function (c, a, l, v) {
		try {
			_gaq.push(['_trackEvent', c, a, l, v]);
		} catch (e) { }
	}

};

$(document).bind('ready', function () {
	CM.initSearch();
	
/*	$(document).bind('keypress.cm', function (e) {
		if (e.which === 103) {
			$('body').toggleClass('debug');
		}
	});*/
});


