//**********************
//custom jquery code

var _global_photo_fader_count = 0;

$(document).ready(function() {
	$('.photo_fader').each(function(i, item) {
		photo_fader({
			div: item
		});
	});

	$('.popup').each(function(i, item) {
		$(this).find('dd').hide();

		$(this).find('dt').each(function() {
			var content = $(this).next('dd');
			if ($(content).length == 0) {return;}

			$(this).addClass('link');

			$(this).click(function() {
				popup_show({
					html: $(content).html()
				});
			});
		});
	});

	$('#popup .popup_background').click(function() {
		$(this).parent().hide();
	});
});

//**********************
function popup_show(properties) {
	var div = (properties['div']) ? properties['div'] : '#popup';
	var html = (properties['html']) ? properties['html'] : '';

	if (!div || !html || $(div).length == 0) {return false;}

	var content_div = $(div).find('.popup_content');
	if ($(content_div).length == 0) {content_div = $('<div>').addClass('popup_content').appendTo(div);}

	$(content_div).empty().html(html);

	var button = $('<div>').addClass('popup_close').html('X').prependTo(content_div);

	$(button).click(function() {
		$(div).hide();
	});

	$(div).show();
}

//**********************
function photo_fader(properties) {
	var div = (properties['div']) ? properties['div'] : null;
	var pause_time = (properties['pause_time']) ? properties['pause_time'] : 4500;

	if (!div) {return false;}

	var list = $(div).children('ul');

	$(list).children('li').eq(0).addClass('selected');

	//photo_fader_controls
	var count = $(list).children('li').length;
	var number_div = $('<div>').addClass('photo_fader_controls').appendTo(div);

	if (count > 1) {
		var number_list = $('<ul>').appendTo(number_div);

		for (var i=0; i<count; i++) {
			var number = $('<li>').attr('data-index', i).appendTo(number_list);

			$(number).click(function() {
				$(list).trigger('stop');
				$(list).trigger('change', $(this).attr('data-index'));
			});
		}

		$(number_list).children('li').eq(0).addClass('selected');
	}

	//taglines
	$(list).children('li').each(function(i, item) {
		var url = $(this).find('a').attr('href');
		var alt = $(this).find('img').attr('alt');

		$(this).attr('data-index', i);
	});

	$(list).bind('change', function(event, num) {
		var selected = $(list).children('.selected');

		//no need to do anything if already showing this item
		if (num != undefined && num == $(selected).attr('data-index')) {
			$(this).trigger('stop');
			return false;
		}

		if (num != undefined) {
			var next = $(list).children('li').eq(num);
		} else {
			var next = $(selected).next();
			if ($(next).length == 0) {next = $(list).children('li').eq(0);}
		}

		//fade out currently showing item
		$(selected).fadeOut(500);
		$(selected).removeClass('selected');

		//change selected number
		$(number_div).find('li.selected').removeClass('selected');
		$(number_div).find('li').eq($(next).attr('data-index')).addClass('selected');

		$(next).fadeIn(500);
		$(next).addClass('selected');
	});

	$(list).bind('stop', function(event) {
		$(this).stopTime();
	});

	$(list).bind('start', function(event) {
		$(this).trigger('stop');
		$(this).everyTime(pause_time, function() {
			$(this).trigger('change');
		});
	});

	//only start the animation if there's more than 1 item
	if (count > 1) {
		var delay = (_global_photo_fader_count * Math.floor(pause_time / 2)) + 50;  //need to adjust pause_time if there are > 2 photo faders running

		$(list).oneTime(delay, function() {
			$(this).trigger('start');
		});
	}

	//update item count
	_global_photo_fader_count++;
}

