/*--------------------------------------------------------------------
======================================================================

JOHN BALLANCE PHOTOGRAPHY

Parka Design
Baton Rouge, LA 70816
+1 225 686 5211

http://www.parkadesign.com

Contains functions to control site-wide UI elements

* Requires jQuery 1.3 or higher

Contents:

	1. Global variables
	2. Global functions
	3. Home page
	4. Samples page
	5. Contact page
	6. Cart page

======================================================================
--------------------------------------------------------------------*/

$(document).ready(function() {


/* 1. Global variables
--------------------------------------------------------------------*/
var page = $('body').attr('class');
var orderCookie = $.cookie('order');
var gPath = 'http://picasaweb.google.com/data/feed/api/user/';
var gUser = 'jballance1';


/* 2. Global functions
--------------------------------------------------------------------*/
function getDataURL(gMethod, gObject) {
	var request = gPath + gUser + '/' + gMethod + '/' + gObject + '?thumbsize=72c&imgmax=800&alt=json&callback=?';
	return request;
}

function showCartQuantity() {
	var orderCookie = $.cookie('order');
	var items = orderCookie.split(';');
	$('#pre-nav li:first-child a span').text(items.length - 1);
	if ($('#pre-nav li:first-child a span').text() == '0') {
		$('#pre-nav li:first-child').css({ display: 'none' });
	}
}

if (orderCookie ==  null || orderCookie ==  '') {
	$('#pre-nav li:first-child').css({ display: 'none' });
} else {
	showCartQuantity();
}


/* 3. Home page
--------------------------------------------------------------------*/
if (page.indexOf('home') != -1) {

	$.getJSON(getDataURL('album', 'Featured'), function(data) {
		var samples = [];
		$.each(data.feed.entry, function(i, item) {
			var photo = [item.media$group.media$thumbnail[0].url, item.media$group.media$content[0].url];
			var html = '<a href="#" rel="' + photo[1] + '">';
			html += '<img src="' + photo[0] + '" alt="#">';
			html += '</a>';
			var sample = new Image;
			sample.setAttribute('id', 'sample-' + (i + 1));
			sample.setAttribute('src', photo[1]);
			sample.setAttribute('alt', '');
			if (i != 0) {
				sample.style.display = 'none';
			}
			samples.push(sample);
			$('#featured-sample').append(sample);
		});
	});
	
	var interval = 10000;
	var counter = 1;
	
	$('#featured-sample').everyTime(interval, function() {
		if (counter == 7) {
			counter = 1;
		} else {
			counter++;
		}
		$('#featured-sample img').fadeOut(950);
		$('#sample-' + (counter)).fadeIn(900);
	});
	
}


/* 4. Samples page
--------------------------------------------------------------------*/
if (page.indexOf('samples') != -1) {

	function loadAlbum(albumName) {
		$.getJSON(getDataURL('album', albumName), function(data) {
			$('.slideshow .holder').fadeOut(500);
			$('.controls li').remove();
			var samples = [];
			$.each(data.feed.entry, function(i, item) {
				if (i < 10) {
					var photo = [item.media$group.media$thumbnail[0].url, item.media$group.media$content[0].url];
					var html = '<a href="#" rel="' + photo[1] + '">';
					html += '<img src="' + photo[0] + '" alt="">';
					html += '</a>';
					var sample = new Image;
					sample.setAttribute('src', photo[1]);
					sample.setAttribute('alt', 'Alternate text');
					var display = 'none';
					if (i == 0) {
						display = 'block';
					}
					$('.slideshow').append(
						$('<div/>')
							.attr('id', albumName + '-' + (i + 1))
							.addClass('holder')
							.css({ display: display })
							.html(sample)
					);		
					$('<li/>')
						.html(html)
						.click(function() {
							$('.slideshow .holder').fadeOut(500);
							$('#' + albumName + '-' + (i + 1)).fadeIn(500);
							return false;
						})
						.appendTo('.controls ul');
				}
			});
		});
	}
		
	$('.controls select').change(function() {
		loadAlbum($(this).val());
	});
	
	loadAlbum('featured');
	
	
}


/* 5. Contact page
--------------------------------------------------------------------*/
if (page.indexOf('contact') != -1) {

	if (location.href.indexOf('?message=sent') != -1) {
		alert('Your message has been sent.');
	}	

}


/* 6. Cart page
--------------------------------------------------------------------*/
if (page.indexOf('cart') != -1) {

	if ($.cookie('order') !=  null && $.cookie('order') !=  '') {
		var items = $.cookie('order').split(';');
		$.each(items, function(i, item) {
			if (i < items.length - 1) {
				var details = item.split(',');
				var html = '<td><span>' + details[0].match(/\d/g).join('') + '</span></td>';
				html += '<td><a class="enlarge" href="' + details[4] + '"><img src="http://johnballance.net/wp-content/themes/johnballance/assets/images/site/picture.png" alt="photo icon" /><a/></td>'
				html += '<td>' + details[1] + '</td>'
				html += '<td>' + details[2] + '</td>';
				html += '<td>' + details[3] + '</td>';
				html += '<td><a class="remove" href="#"><img src="http://johnballance.net/wp-content/themes/johnballance/assets/images/site/delete.png" alt="delete button" /></a></td>';
				$('<tr/>').attr('id', i).html(html).appendTo('table tbody');
				var emailTxt = '';
				var previewUrl = details[4].replace('s800', 's512');
				emailTxt += (i + 1) + '. ' + details[0] + ', ' + details[1] + ' (' + details[2] + ') [' + previewUrl + ']\n';  
				$('input[name=items]').val($('input[name=items]').val() + emailTxt);
			}
		});
	} else {
		$('table tbody').html('Your cart is empty.');
	}
	
	$('table td a.enlarge').fancybox({ imageScale: true, overlayOpacity: .75 });
	
	if (location.href.indexOf('?order=submitted') != -1) {
		alert('Your order has been submitted.');
	}
	
	$('.remove').click(function() {
		var items = $.cookie('order').split(';');
		items.splice($(this).attr('id'), 1);
		$.cookie('order', '', { path: '/' });
		$.each(items, function(i, item) {
			if (i < (items.length - 1)) {
				$.cookie('order', $.cookie('order') + item + ';', { path: '/' });
			}
		});
		$(this).closest('tr').remove();
		showCartQuantity();
		return false;
	});

}


});
