var href = window.location.href;

var hash_pos = href.indexOf('#');

var hash_data = href.substr(hash_pos);

$(function()
{
	if (hash_data == '#/cinema')
	{
		$('html').addClass('cinema');
	}
	
	Site.init();
});

var Site =
{
	init:function()
	{
		for (obj in Site)
		{
			if (obj != 'init')
			{	
				if (Site[obj].init)
				{
					Site[obj].init();
				}
			}
		}
	},
	
	Vimeo :
	{
		init:function()
		{
			$('#loader').show();
			
			this.init_toggle_video_list();
		},
		
		init_toggle_video_list:function()
		{
			$.getJSON('http://vimeo.com/api/v2/galejproduktion/videos.json?format=json&callback=?',function(data)
			{
				var videos = data;
				
				var html = '<p class="close"><a href="#">Close</a></p>';
				html = html + '<div class="categories">';
				html = html + '<p><strong>Show:</strong></p>';
				html = html + '<ul>';
				html = html + '<li class="selected"><a href="#">All</a></li>';
				html = html + '<li><a href="#">Music Videos</a></li>';
				html = html + '<li><a href="#">Commercial</a></li>';
				html = html + '<li><a href="#">Corporate</a></li>';
				html = html + '<li><a href="#">Other</a></li>';
				
				html = html + '</ul>';
				html = html + '</div>';
				
				
				html = html + '<div class="vids"><ul>';

				for(var video in videos)
				{
					var tags		= videos[video].tags;
					var title 		= videos[video].title;
					var vimeo_url 	= videos[video].url;
					var thumb 		= videos[video].thumbnail_small;
					

					html = html + '<li class="' + tags + '">';
					html = html + '<a href="' + vimeo_url + '">';
					html = html + '<img src="' + thumb + '" alt="" /><span>Play</span></a>';
					html = html + '<p class="details">' + title + '</p>';
					html = html + '</li>';
				}				

				html = html + '</ul></div>';
				
				//alert(html);
				
				var $container = $('#work.dd div');
				
				$container.append(html).find('div.vids a').click(function()
				{
					var url = $(this).attr('href');
					Site.Vimeo.show_video(url);
					
					return false;
				}
				).filter(':first').trigger('click');
				
				$container.find('p.close a').click(function()
				{
					$('li.work a').trigger('click');
					return false;
				});
				
				var $video_items = $container.find('div.vids li');
				
				$container.find('div.categories ul a').click(function()
				{
					var $this = $(this);
					
					$this.parents('div.categories').find('li').removeClass('selected');
					$this.parent('li').addClass('selected');
					
					var category = $this.text();
					
					$video_items.show();
					
					if (category != 'All')
					{
						$video_items.each(function()
						{
							var $this = $(this);

							var classes = $this.attr('class');

							if (classes.indexOf(category) == -1)
							{
								$this.hide();
							}
						});	
					}
					
					return false;
				});
				
				$('#wrapper').prepend('<p class="work toggle"><a href="#">Browse videos</a></p>');			
			});
			
			$('.work a').live('click',function()
			{
				var $target = $('#work');
				
				var cinema_mode = ($('html').hasClass('cinema')) ? true : false;
				
				var is_ie = ($.support.opacity) ? false : true;
				
				if ($target.is(':visible'))
				{
					$('#work').slideUp();
					
					if ($.browser.msie)
					{
						$('#video div.clip object').css('visibility','visible');
					}
				}
				else
				{
					$('#work').slideDown();
					
					if ($.browser.msie)
					{
						$('#video div.clip object').css('visibility','hidden');
					}
				}
			
				return false;
			});
			
			//$('nav .work a').trigger('click');
			
		},
		
		show_video:function(url)
		{
			var cinema_mode = ($('html').hasClass('cinema')) ? true : false;
			
			if (cinema_mode)
			{
				vimeo_prefs = {
					maxwidth: 1024,
					portrait:false,
					byline:false,
					title:false,
					color: '74957d'
				};
			}
			else
			{
				vimeo_prefs = {
					maxwidth: 640,
					portrait:false,
					byline:false,
					title:false,
					color: '74957d'
				};
			}
			
			//console.log(vimeo_prefs)
						
			$('#loader').show();
			
			if ($('#work').is(':visible'))
			{
				$('li.work a').trigger('click');
			}
			
			var $container = $('#video');
			
			$container.empty();
			
			$container.oembed(url,
			{
				embedMethod: "fill",
				vimeo: vimeo_prefs			
			},
			function(container, oembed)
			{
				var url_split = url.split('/');
				var video_id = url_split[url_split.length -1];

				$.getJSON('http://vimeo.com/api/v2/video/' + video_id + '.json?format=json&callback=?',function(data)
				{
					var title 		= data[0].title;
					var description = data[0].description;
										
					$container.append('<div class="clip"></div>').find('div.clip').append(oembed.code);
					
					if ( ! $.browser.msie)
					{
						$('#video div.clip object').append('<param name="wmode" value="opaque" />');
					}
					
					$container.append('<p class="more"><a href="#">Show details</a></p>').find('p.more a').click(function()
					{
						var $details = $container.find('div.details');
						
						if ($details.is(':visible'))
						{
							$details.slideUp();
							$(this).text('Show details');
						}
						else
						{
							$details.slideDown();
							$(this).text('Hide details');
						}
						
						return false;
					});
					
					$container.append('<h3>' + title + '</h3>');
					
					$container.append('<div class="details">' + description + '</div>');
										
					var toggle_text = ($('html').hasClass('cinema')) ? 'Leave cinema' : 'Enter cinema';
					
					$container.append('<p id="cinema"><a href="#/cinema">' + toggle_text + '</a></p>').find('p#cinema a').click(function()
					{
						var cinema_mode = ($('html').hasClass('cinema')) ? true : false;
						
						if ( ! cinema_mode)
						{	
							$(this).attr('href','#/cinema').text('Leave cinema');
							$('html').addClass('cinema');
						}
						else
						{
							$(this).attr('href','#').text('Enter cinema');
							$('html').removeClass('cinema');
						}
						
						Site.Vimeo.show_video(url);
						
					});
					
					$('#loader').hide();
				});
			});			
		}
	},
	
	NewWindow :
	{
		init:function()
		{
			var $links = $('a[href^="http://"], a[href^="https://"]');

			$links.click(function()
			{	
				var $this = $(this);
				var href = $this.attr('href');

				if (href.indexOf('galejproduktion.se') == -1)
				{
					window.open(href);
					return false;
				}
			});
		}
	}
};


// oembed.js

(function(d){d.fn.oembed=function(g,f,h){f=d.extend(true,d.fn.oembed.defaults,f);return this.each(function(){var i=d(this),j=(g!=null)?g:i.attr("href"),k;if(!h){h=function(m,l){d.fn.oembed.insertCode(m,f.embedMethod,l)}}if(j!=null){k=e(j);if(k!=null){k.params=c(f[k.name])||{};k.maxWidth=f.maxWidth;k.maxHeight=f.maxHeight;k.embedCode(i,j,h);return}}h(i,null)})};d.fn.oembed.defaults={maxWidth:null,maxHeight:null,embedMethod:"replace"};d.fn.oembed.insertCode=function(g,h,f){if(f==null){return}switch(h){case"auto":if(g.attr("href")!=null){d.fn.oembed.insertCode(g,"append",f)}else{d.fn.oembed.insertCode(g,"replace",f)}break;case"replace":g.replaceWith(f.code);break;case"fill":g.html(f.code);break;case"append":var i=g.next();if(i==null||!i.hasClass("oembed-container")){i=g.after('<div class="oembed-container"></div>').next(".oembed-container");if(f!=null&&f.provider_name!=null){i.toggleClass("oembed-container-"+f.provider_name)}}i.html(f.code);break}};d.fn.oembed.getPhotoCode=function(f,h){var i=h.title?h.title:"";i+=h.author_name?" - "+h.author_name:"";i+=h.provider_name?" - "+h.provider_name:"";var g='<div><a href="'+f+'" target="_blank"><img src="'+h.url+'" alt="'+i+'"/></a></div>';if(h.html){g+="<div>"+h.html+"</div>"}return g};d.fn.oembed.getVideoCode=function(f,h){var g=h.html;return g};d.fn.oembed.getRichCode=function(f,h){var g=h.html;return g};d.fn.oembed.getGenericCode=function(f,h){var i=(h.title!=null)?h.title:f,g='<a href="'+f+'">'+i+"</a>";if(h.html){g+="<div>"+h.html+"</div>"}return g};d.fn.oembed.isAvailable=function(f){var g=e(f);return(g!=null)};function e(f){for(var g=0;g<b.length;g++){if(b[g].matches(f)){return b[g]}}return null}function c(h){if(h==null){return null}var g={};for(var f in h){if(f!=null){g[f.toLowerCase()]=h[f]}}return g}var b=[new a("fivemin","5min.com"),new a("amazon","amazon.com"),new a("flickr","flickr","http://flickr.com/services/oembed","jsoncallback"),new a("googlevideo","video.google."),new a("hulu","hulu.com"),new a("imdb","imdb.com"),new a("metacafe","metacafe.com"),new a("qik","qik.com"),new a("revision3","revision3.com"),new a("slideshare","slideshare.net"),new a("twitpic","twitpic.com"),new a("viddler","viddler.com"),new a("vimeo","vimeo.com","http://vimeo.com/api/oembed.json"),new a("wikipedia","wikipedia.org"),new a("wordpress","wordpress.com"),new a("youtube","youtube.com"),new a("vids.myspace.com","vids.myspace.com","http://vids.myspace.com/index.cfm?fuseaction=oembed"),new a("screenr","screenr.com","http://screenr.com/api/oembed.json")];function a(f,g,i,h){this.name=f;this.urlPattern=g;this.oEmbedUrl=(i!=null)?i:"http://oohembed.com/oohembed/";this.callbackparameter=(h!=null)?h:"callback";this.maxWidth=500;this.maxHeight=400;this.matches=function(j){return j.indexOf(this.urlPattern)>=0};this.getRequestUrl=function(m){var k=this.oEmbedUrl;if(k.indexOf("?")<=0){k=k+"?"}else{k=k+"&"}var j="";if(this.maxWidth!=null&&this.params.maxwidth==null){this.params.maxwidth=this.maxWidth}if(this.maxHeight!=null&&this.params.maxheight==null){this.params.maxheight=this.maxHeight}for(var l in this.params){if(l==this.callbackparameter){continue}if(this.params[l]!=null){j+="&"+escape(l)+"="+this.params[l]}}k+="format=json&url="+escape(m)+j+"&"+this.callbackparameter+"=?";return k};this.embedCode=function(j,l,m){var k=this.getRequestUrl(l);d.getJSON(k,function(q){var n=d.extend(q);var p,o=q.type;switch(o){case"photo":n.code=d.fn.oembed.getPhotoCode(l,q);break;case"video":n.code=d.fn.oembed.getVideoCode(l,q);break;case"rich":n.code=d.fn.oembed.getRichCode(l,q);break;default:n.code=d.fn.oembed.getGenericCode(l,q);break}m(j,n)})}}})(jQuery);
