
$(document).ready(function(){

	msie_warning();

	clickable_media_thumbs();
	check_url_hash();
	attachments_note();

	$('a[href$=".jpg"], a[href$=".png"], a[href$=".gif"]').has('img').fancybox({ overlayColor: '#000', centerOnScroll: true });

});

function msie_warning() {

	if ($('body').hasClass('ie7') || $('body').hasClass('ie6')) {
		$('body').html('<div id="content" class="hentry" style="text-align: center;">Internet Explorer is missing updates required to view this site. <a href="http://www.microsoft.com/windows/internet-explorer/default.aspx">Click here to update...</a></div>');
	}
	
}

function check_url_hash() {
	if ((urlhash = $.trim(window.location.hash.substring(1))) != '') {
		for (var id in pltn.media) {
			if (pltn.media[id].slug == urlhash) {
				$('#'+id).click();
			}
		}
	}
}

function attachments_note() {

	if ($('body').hasClass('safari')) {
		download_text = 'Download Linked File As...';
	} else if ($('body').hasClass('msie')) {
		download_text = 'Save Target As...';
	} else {
		download_text = 'Save Link As...';
	}

	$('.file-meta a').click(function() {
		$.fancybox(
			'<p class="font-proxima">Please right-click the download link below and choose: <em>'+download_text+'</em></p><p class="font-proxima" style="text-align: center;"><a href="'+$(this).attr('href')+'">Download</a></p>',
			{ 'autoDimensions' : false, 'width' : 350, 'height' : 'auto'}
		);
		return false;
	});

}

function clickable_media_thumbs() {

	$mediaspace = $('#content > div.media-space');
	$thumbs = $('#content > div.media-thumb');

	if ($thumbs.length && $mediaspace.length) {

		$photo = $('#entry-photo', $mediaspace);
		$video = $('#entry-video', $mediaspace);
		$vimeo = $('#entry-vimeo-video', $mediaspace);

		$thumbs.each(function() {

			$id = $(this).children('div.entry-thumb').attr('id');

			if (pltn.media[$id]) {

				if (pltn.media[$id].type == 'video' && !jwplayer('video-player')) {
					load_jw_player($video);
				}

				$(this).css('cursor', 'pointer').click(function() {

					$id = $(this).children('div.entry-thumb').attr('id');
					window.location.hash = pltn.media[$id].slug;

					$mediaspace.children('.entry-title').html($(this).children('.entry-title').html());
					$mediaspace.children('.entry-content').html($(this).children('.entry-content').html());

					if (pltn.media[$id].type == 'photo') {

						$vimeo.empty();
						$photo.html('<a href="'+pltn.media[$id].large+'"><img src="'+pltn.media[$id].medium+'" /></a>');
						$photo.parent().removeClass('is-vimeo is-video').addClass('is-photo');

					} else if (pltn.media[$id].type == 'video') {

						isHTML5 = jwplayer('video-player').renderingMode == 'html5';

						$vimeo.empty();
						jwplayer('video-player').stop().load({
							file: isHTML5 ? 'http://videos.pollutioncreative.com.s3-website-us-west-1.amazonaws.com/'+window.pltn.media[$id].file : window.pltn.media[$id].file,
							image: window.pltn.media[$id].image,
							provider: isHTML5 ? 'video' : 'rtmp'
						});
						setTimeout(function() { jwplayer('video-player').play(); }, 500);
						$photo.parent().removeClass('is-vimeo is-photo').addClass('is-video');

					} else if (pltn.media[$id].type == 'vimeo-video') {

						$vimeo.html('<iframe src="http://player.vimeo.com/video/'+pltn.media[$id].id+'?autoplay=1&amp;title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff" width="620" height="349" frameborder="0" webkitAllowFullScreen allowFullScreen></iframe>');
						$photo.parent().removeClass('is-video is-photo').addClass('is-vimeo');

					}

					$('html, body').animate({ scrollTop: 0 });

					return false;

				});

			}
			
		});
	}

}

function load_jw_player($object) {

	$object.append('<div id="video-player"></div>');
	jwplayer('video-player').setup({
		skin: '/wp-content/themes/pollution/player/glow/glow.xml',
		screencolor: 'ffffff',
		width: 620,
		height: 349,
		provider: 'rtmp',
		streamer: 'rtmp://videos.pollutioncreative.com/cfx/st',
		modes: [
			{ type: 'flash', src: '/wp-content/themes/pollution/player/player.swf' },
			{ type: 'html5', config: { provider: 'video' } }
       	]
	});

}

