/*
 Shadow animation jQuery-plugin 1.7
 http://www.bitstorm.org/jquery/shadow-animation/
 Copyright 2011 Edwin Martin <edwin@bitstorm.org>
 Contributors: Mark Carver, Xavier Lepretre
 Released under the MIT and GPL licenses.
*/
jQuery(function(e,i){function j(){var a=e("script:first"),b=a.css("color"),c=false;if(/^rgba/.test(b))c=true;else try{c=b!=a.css("color","rgba(0, 0, 0, 0.5)").css("color");a.css("color",b)}catch(d){}return c}function k(a,b,c){var d=[];a.c&&d.push("inset");typeof b.left!="undefined"&&d.push(parseInt(a.left+c*(b.left-a.left),10)+"px "+parseInt(a.top+c*(b.top-a.top),10)+"px");typeof b.blur!="undefined"&&d.push(parseInt(a.blur+c*(b.blur-a.blur),10)+"px");typeof b.a!="undefined"&&d.push(parseInt(a.a+c*
(b.a-a.a),10)+"px");if(typeof b.color!="undefined"){var g="rgb"+(e.support.rgba?"a":"")+"("+parseInt(a.color[0]+c*(b.color[0]-a.color[0]),10)+","+parseInt(a.color[1]+c*(b.color[1]-a.color[1]),10)+","+parseInt(a.color[2]+c*(b.color[2]-a.color[2]),10);if(e.support.rgba)g+=","+parseFloat(a.color[3]+c*(b.color[3]-a.color[3]));g+=")";d.push(g)}return d.join(" ")}function h(a){var b,c,d={};if(b=/#([0-9a-fA-F]{2})([0-9a-fA-F]{2})([0-9a-fA-F]{2})/.exec(a))c=[parseInt(b[1],16),parseInt(b[2],16),parseInt(b[3],
16),1];else if(b=/#([0-9a-fA-F])([0-9a-fA-F])([0-9a-fA-F])/.exec(a))c=[parseInt(b[1],16)*17,parseInt(b[2],16)*17,parseInt(b[3],16)*17,1];else if(b=/rgb\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*\)/.exec(a))c=[parseInt(b[1],10),parseInt(b[2],10),parseInt(b[3],10),1];else if(b=/rgba\(\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9]{1,3})\s*,\s*([0-9\.]*)\s*\)/.exec(a))c=[parseInt(b[1],10),parseInt(b[2],10),parseInt(b[3],10),parseFloat(b[4])];d=(b=/(-?[0-9]+)(?:px)?\s+(-?[0-9]+)(?:px)?(?:\s+(-?[0-9]+)(?:px)?)?(?:\s+(-?[0-9]+)(?:px)?)?/.exec(a))?
{left:parseInt(b[1],10),top:parseInt(b[2],10),blur:b[3]?parseInt(b[3],10):0,a:b[4]?parseInt(b[4],10):0}:{left:0,top:0,blur:0,a:0};d.c=/inset/.test(a);d.color=c;return d}e.extend(true,e,{support:{rgba:j()}});var f;e.each(["boxShadow","MozBoxShadow","WebkitBoxShadow"],function(a,b){a=e("html").css(b);if(typeof a=="string"&&a!=""){f=b;return false}});if(f)e.fx.step.boxShadow=function(a){if(!a.init){a.b=h(e(a.elem).get(0).style[f]||e(a.elem).css(f));a.end=e.extend({},a.b,h(a.end));if(a.b.color==i)a.b.color=
a.end.color||[0,0,0];a.init=true}a.elem.style[f]=k(a.b,a.end,a.pos)}});
/*
 jQuery hashchange event - v1.3 - 7/21/2010
 http://benalman.com/projects/jquery-hashchange-plugin/
 Copyright (c) 2010 "Cowboy" Ben Alman
 Dual licensed under the MIT and GPL licenses.
 http://benalman.com/about/license/
*/
(function($,e,b){var c="hashchange",h=document,f,g=$.event.special,i=h.documentMode,d="on"+c in e&&(i===b||i>7);function a(j){j=j||location.href;return"#"+j.replace(/^[^#]*#?(.*)$/,"$1")}$.fn[c]=function(j){return j?this.bind(c,j):this.trigger(c)};$.fn[c].delay=50;g[c]=$.extend(g[c],{setup:function(){if(d){return false}$(f.start)},teardown:function(){if(d){return false}$(f.stop)}});f=(function(){var j={},p,m=a(),k=function(q){return q},l=k,o=k;j.start=function(){p||n()};j.stop=function(){p&&clearTimeout(p);p=b};function n(){var r=a(),q=o(m);if(r!==m){l(m=r,q);$(e).trigger(c)}else{if(q!==m){location.href=location.href.replace(/#.*/,"")+q}}p=setTimeout(n,$.fn[c].delay)}$.browser.msie&&!d&&(function(){var q,r;j.start=function(){if(!q){r=$.fn[c].src;r=r&&r+a();q=$('<iframe tabindex="-1" title="empty"/>').hide().one("load",function(){r||l(a());n()}).attr("src",r||"javascript:0").insertAfter("body")[0].contentWindow;h.onpropertychange=function(){try{if(event.propertyName==="title"){q.document.title=h.title}}catch(s){}}}};j.stop=k;o=function(){return a(q.location.href)};l=function(v,s){var u=q.document,t=$.fn[c].domain;if(v!==s){u.title=h.title;u.open();t&&u.write('<script>document.domain="'+t+'"<\/script>');u.close();q.location.hash=v}}})();return j})()})(jQuery,this);
/*
 Based on Anchor Slider by Cedric Dugas
 http://www.position-absolute.com
*/

var playlist = {};

$.getJSON('js/playlist.json', function(data){
	$.each(data.clips, function(nr){
		var obj = {}, c=data.clips[nr], f=data.formats;
		$.each(f, function(i){
			obj[i] = {src:data.dir+'/'+c.file+'.'+f[i].ext,type:f[i].type};
		});
		playlist[c.title] = [[obj],c.meta];
	});
	console.log(playlist);
});

$.fn.anchorAnimate = function(settings){
	return this.each(function(){
		var page = $('html:not(:animated),body:not(:animated)');
		$(this).click(function(e){	
			e.preventDefault();
			var hash = $(this).attr('href');
			var new_y = $(hash).offset().top;
	
			if(page.scrollTop()!=new_y){
				page.animate({scrollTop: new_y}, 1100, function(){
					location.hash = hash;
				});
			}else {
				//alert('No scrolling needed')
			}
			return false;
		});
	});
};

/*
$('.wi_block').live({mouseenter: function(e){
	console.log('hover!');
},mouseleave: function(e){
	console.log('leave.. :(');
}});
*/
var playClip = function(title) {
	
	var clip = playlist[title][0], meta = playlist[title][1];

	if(!$('#prjkktr').length){
		var video = '<video id="prjkktr" class="projekktor" title="" width="736" height="414" controls="controls" autoplay="autoplay">';
		for(i in clip[0]) {
			video += '<source src="'+clip[0][i].src+'" type="'+clip[0][i].type+'"></source>';
		}
		video += '</video><div id="meta"><h3>'+title+'</h3><div class="content"></div></div>';
		$('#show').html(video);
		//projekktor('#prjkktr');
		
		$('#show').css('height', '1px').animate({height:'414px'}, 600, function(){
			//projekktor('prjkktr').setPlay();
		});
	}else{
		var video = '<video id="prjkktr" class="projekktor" title="" width="736" height="414" controls="controls" autoplay="autoplay">';
		for(i in clip[0]) {
			video += '<source src="'+clip[0][i].src+'" type="'+clip[0][i].type+'"></source>';
		}
		video += '</video><div id="meta"><h3>'+title+'</h3><div class="content"></div></div>';
		$('#show').html(video);
		
		$('#meta>h3').html(title);
		//projekktor('prjkktr').setFile(clip).setPlay();
	}
	var m ='';
	if(meta){
		$.each(meta, function(key, val){
			m += '<p>'+key+': '+val+'</p>';
		});	
	}
	$('#meta .content').html(m);
};




$(function(){
	
	$(window).hashchange(function(){
		var page = $('html:not(:animated),body:not(:animated)');
		if(location.hash != ''){
			var hash = $(location.hash);
			var new_y = hash.offset().top;
			document.title = 'Draai Media \u2014 '+hash.html();
		}else {
			var new_y = 0;
		}
		if(page.scrollTop()!=new_y){
			page.scrollTop(new_y);
		}
		/*
		$('nav a').each(function(){
			var that = $(this);
			that[ that.attr( 'href' ) === hash ? 'addClass' : 'removeClass' ]( 'selected' );
		});
		*/
	});

	$('.wi_block').hover(function(){
		$(this).children('.wi_img').stop().animate({boxShadow:'0 -5px 14px rgba(23, 106, 107, 0.6)'},250).children('.wi_play').css('display','block'); 
		$(this).addClass('on').css('zIndex',1000).stop().animate({top:'-20px',boxShadow:'2px 2px 1px rgba(0, 0, 0, 0.3)'}, 250).children('.wi_bar').stop().animate({marginTop:'0px'}, 300);
	},function(){
		$(this).children('.wi_img').stop().animate({boxShadow:'0 0px 0px rgba(23, 106, 107, 0.6)'}, 150).children('.wi_play').css('display','none'); 
		$(this).removeClass('on').css('zIndex',500).stop().animate({top:'0px',boxShadow:'2px 2px 1px rgba(0, 0, 0, 0)'},150,function(){$(this).css('zIndex','auto')}).children('.wi_bar').stop().animate({marginTop:'-41px'}, 150);
	});

	$('.wi_play').click(function(e){
		e.preventDefault();
		playClip(this.title);
	});

	$('a.anchorLink').anchorAnimate();
	$(window).hashchange();
});
