// document ready

$(document).ready(function() {

	// External links
	
	$('a').each(function() {
		if($(this).attr('rel') == 'external') {
			$(this).click(function() {
				window.open($(this).attr('href'));
				this.blur();
				return false;
			});
		}
	});
	
	// Print
	
	$('#print').click(function() {
		window.print();
		this.blur();
		return false;
	});

	// search box

	$('#search-button').click(function() {
		$('#search-form').submit();
		return false;
	});
	
	// homepage featured articles
	
	features($('#latest-articles .article:eq(0)'));
	feature.timer = setTimeout('nextFeature()', 5000);
	
	$('#feature-next').click(function() {
		clearTimeout(feature.timer);
		nextFeature(true);
		this.blur();
		return false;
	});
	
	// calendar listings

	
	if($('#calendar-map')) {
		map.stored = $('#calendar-map img').attr('src');
	}
	
	$('#calendar-sort-city').click(function() {
		$('.menu').hide();
		$(document.body).unbind('click');
		$(document.body).click(function() {
			$('.menu').hide();
		});
		$('#calendar-sort-city-menu').show();	
		this.blur();
		return false;
	});

	$('#calendar-sort-event').click(function() {
		$('.menu').hide();
		$(document.body).unbind('click');
		$(document.body).click(function() {
			$('.menu').hide();
		});
		$('#calendar-sort-event-menu').show();
		this.blur();
		return false;
	});
	
	$('#calendar-listings .more').click(function() {
		$('#calendar-listings .full').hide();
		$('#calendar-listings .mini').show();
		$(this).parent().parent().find('.full').slideDown('fast');
		$(this).parent().parent().find('.mini').hide();
		return false;
	});

	$('#calendar-listings .expand').click(function() {
		$('#calendar-listings .full').hide();
		$('#calendar-listings .mini').show();
		$(this).parent().parent().find('.full').slideDown('fast');
		$(this).parent().parent().find('.mini').hide();
		return false;
	});

	$('#calendar-listings .collapse').click(function() {
		$('#calendar-listings .full').hide();
		$('#calendar-listings .mini').show();
		$(this).parent().parent().find('.full').slideUp('fast');
		$(this).parent().parent().find('.mini').show();
		return false;
	});
	
	// articles index page

	$('#articles-by-date .year').click(function() {
		$('#articles-by-date .year').parent().removeClass('selected');
		$(this).parent().addClass('selected');
		this.blur();
		return false;
	});
	
	// article page

	$('#article-assets .arrow-right').click(function() {
		shiftAsset();
		this.blur();
		return false;
	});

	$('#font-large').click(function() {
		$('#article-text').css({'fontSize': '18px', 'lineHeight': '24px'});
		this.blur();
		return false;
	});

	$('#font-medium').click(function() {
		$('#article-text').css({'fontSize': '13px', 'lineHeight': '21px'});
		this.blur();
		return false;
	});

	$('#font-small').click(function() {
		$('#article-text').css({'fontSize': '12px', 'lineHeight': '20px'});
		this.blur();
		return false;
	});
	
});



// calendar image map swap 


map = {
	stored: null,
	swap: function(m) {
		$('#calendar-map img').attr('src', '/images/calendar-map-' + m + '.gif');
	},
	restore: function() {
		$('#calendar-map img').attr('src', map.stored);	
	}
}



// homepage features

var feature = {
	index: 0,
	timer: null
};

function features(e) {
	$('#feature-name').html($(e).find('.link').html());
	$('#feature-image').css('background', 'url(' + $(e).find('.image').attr('src') + ') center center no-repeat');
	$('#feature-text').html($(e).find('.text').html());
	$('#feature-link').attr('href', $(e).find('.link').attr('href'));
	$('#latest-articles .article').css('background', 'none');
	$(e).css('background', '#ECEEEF');
	//$('#feature-overlay a').attr('href', $(e).find('.link').attr('href'));
}

function nextFeature(killTimeout) {
	feature.index ++;
	if(feature.index > 3) feature.index = 0;
	features($('#latest-articles .article:eq(' + feature.index + ')'));
	if(!killTimeout) feature.timer = setTimeout('nextFeature()', 5000);
}


// shift

var aItems = 0;
var aOffset = 0;
var aWidth = 0;
function shiftAsset() {
	aOffset ++;
	$('#article-media').animate({'marginLeft': '-=' + aWidth + 'px'}, function() {
		if(aOffset >= aItems) {
			$('#article-media').css({'marginLeft': 0});
			aOffset = 0;
		}																			   
	});
}


$(document).ready(function() {
	var copy = $('#article-media').html();
	aItems = $('#article-media .asset').length;
	aWidth = $('#article-media .asset').width();
	if(aItems == 0) {
		$('#article-assets').css('height', 'auto');
		$('#article-assets .arrow-right').hide();
	}
	else if(aItems > 3) {
		$('#article-media').append(copy);
	}
	else {
		$('#article-assets .arrow-right').hide();	
	}

});


// lightbox

lightbox = {
	opened: false,
	get: function(id, type, articleId) {
		if(!this.opened) {
			this.url = null;
			switch(type) {
				case 'gallery': 
					this.url = '/manage_images/get_image_set_as_json/';
					break;
				case 'still': 
					this.url = '/manage_images/get_single_image_json/';
					break;
				case 'video': 
					this.url = '/videos/get_single_video_json/';
					break;
				case 'audio': 
					this.url = '/audios/get_single_audio_json/';
					break;
			}
			if(this.url && id) {
				$(document.body).prepend('<div id="lightbox-load"><img src="/images/global-loading.gif"'
					+ 'width="30" height="30" alt="" /></div>');
				lightbox.center($('#lightbox-load'));
				$.getJSON(this.url + id, function(data) {
					$('#lightbox-load').remove();
					$(document.body).prepend('<div id="lightbox-block"></div>'
						+ '<div id="lightbox-bg"></div><div id="lightbox-contents"></div>');
					var lb = $('#lightbox-contents');
					var bg = $('#lightbox-bg');
					lb.html('<div class="top"><h2></h2><a href="#" id="lightbox-close"></a></div>');
					$('#lightbox-close').click(function() { lightbox.close(); return false; });
					lb.append('<div class="media"></div><div class="credit"></div>'
						+ '<div class="caption"></div>');
					lb.find('h2').html();
					switch(type) {
						case 'gallery':
							lb.find('h2').html(data.title || '');
							lightbox.index = 0;
							lightbox.images = data.images;
							lightbox.galleryImage();
							break;
						case 'still':
							lb.find('h2').html(data.title || '');
							lb.find('.media').html('<img src="' + data.url + '" width="' +
								data.width + '" height="' + data.height + '" alt="" />');
							var credPad = (600 - data.width) / 2;
							lb.find('.credit').css({paddingRight: credPad});
							lb.find('.credit').html(data.credit);
							lb.find('.caption').html(data.caption);
							break;
						case 'video':
						case 'audio':
							lb.find('h2').html(data.title || '');
							lb.find('.media').html(lightbox.flash(id + '/' + articleId, data.width, parseInt(data.height) + 36, true, true));
							lb.find('.caption').html(data.description);
							break;
					}
					bg.width(lb.width());
					bg.height(lb.height());
					lightbox.center(bg);
					lightbox.center(lb);
					$('#lightbox-block').height($('#wrapper').height()).css('opacity', 0).animate({'opacity': .3});
					bg.css('opacity', 0).animate({'opacity': .8}, function(){ $('#lightbox-contents').show(); });
					$(window).unbind('scroll');
					$(window).scroll(function() {
						lightbox.center('#lightbox-load');
						lightbox.center('#lightbox-bg');
						lightbox.center('#lightbox-contents');
					});
					$(window).unbind('resize');
					$(window).resize(function() {
						lightbox.center('#lightbox-load');
						lightbox.center('#lightbox-bg');
						lightbox.center('#lightbox-contents');
					});
					$('#lightbox-block').click(function() {
						lightbox.close();					 
					});
				});
				lightbox.opened = true;
			}
			else alert('Error opening lightbox. Check id and type.');
		}
	},
	galleryImage: function() {
		var img = this.images[this.index];
		var lb = $('#lightbox-contents');
		lb.find('.media').css('background', 'url(' + img.url + ') center top no-repeat');
		if(this.images.length > 1) {
			lb.find('.media').html('<a href="#" id="go-back"></a><a href="#" id="go-next"></a>');
		}
		var pad = (600 - img.width) / 2;
		if($.browser.msie && $.browser.version < 7) pad = pad / 2;
		$('#go-back').css({
			width: img.width / 2,
			height: img.height,
			marginLeft: pad
		}).click(function() {
			lightbox.galleryBack();
			this.blur();
			return false;
		});
		$('#go-next').css({
			width: img.width / 2,
			height: img.height
		}).click(function() {
			lightbox.galleryNext();
			this.blur();
			return false;
		});
		lb.find('.media').height(img.height + 10);
		lb.find('.credit').css({paddingRight: pad});
		lb.find('.credit').html(img.credit);
		lb.find('.caption').html(img.caption);
	},
	galleryBack: function() {
		if(!this.index) this.index = this.images.length - 1;
		else this.index --;
		this.galleryImage();
	},
	galleryNext: function() {
		if(this.index + 1 > this.images.length - 1) this.index = 0;
		else this.index ++;
		this.galleryImage();		
	},
	center: function(e) {
		var t = ($(window).height() / 2) - ($(e).height() / 2) - 50 + $(window).scrollTop();
		var l = ($(window).width() / 2) - ($(e).width() / 2);
		if(t < 10) t = 10;
		if(l < 10) l = 10;
		$(e).css({top: t, left: l});	
	},
	close: function() {
		$('#lightbox-block').remove();
		$('#lightbox-bg').remove();
		$('#lightbox-contents').remove();
		lightbox.opened = false;
	},
	flash: function(i, w, h, a, l) {
		a = a ? '&auto=true' : '';
		l = l ? '&local=true' : '';
		return '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" width="' + w + '" height="' + h + '"><param name="movie" value="/flash/mediaplayer.swf?id=' + i + a + l + '"></param><param name="allowfullscreen" value="true"></param><param name="wmode" value="transparent"></param><param name="allowscriptaccess" value="always"></param><embed src="/flash/mediaplayer.swf?id=' + i + a + l + '" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" allowfullscreen="true" wmode="transparent" width="' + w + '" height="' + h + '"></embed></object>';
		
	}
}
