(function($){
	var vimeo = 'http://player.vimeo.com/video/'; //base of the video inserts
	var base = '/'; //base of the api
	var path= 'index.php/api/work'; //relative to base
	
	if($.Drive === undefined){
		$.Drive = { 
			projects : [],
			project : null,
			video : function(){
				var $quotes = $('#media .quotes'),
				$photo = $('#media .frame'),
				$video = $('#media .video'), 
				$play = $video.find('.play'),
				$iframe = $video.find('iframe');
				$iframe.hide();
				
				if (navigator.appVersion.indexOf("Win")!=-1) {
					$iframe.attr('src', vimeo+$.Drive.project.video + '?autoplay=1');
				}
				if (navigator.appVersion.indexOf("Mac")!=-1) {
					$iframe.attr('src', vimeo+$.Drive.project.video);
				}
//				$iframe.attr('src', vimeo+$.Drive.project.video + '?autoplay=1');
//				$iframe.attr('src', vimeo+$.Drive.project.video);


				$('.btn-video').hide();
				if($.Drive.project.photos){
					$('.btn-photo').show();
				}
				if($quotes.css('display')!=='none'){
					$quotes.hide();
				}
				$photo.hide();
				$video.show();
				$play.show();
			},
			photo : function(){
				var $quotes = $('#media .quotes');
				var $frame = $('#media .frame');
				var $photo = $('#media .photos');
				var $video = $('#media .video');
				//clear out nivoslider
				if($photo.data('nivoslider')){
					$photo.data('nivoslider').stop();
				}
				$photo.remove();
				
				$('.btn-photo').hide();
				if($.Drive.project.video){
					$('.btn-video').show();
				}
				if($quotes.css('display')!=='none'){
					$quotes.hide();
				}
				$video.hide();
				$frame.show();
				$.Drive.getPhotos($.Drive.project);
			},
			select: function(project){
				$.Drive.project = project;
				
				var $container = $('#work'),
				$content = $('#content'),
				$quotes = $('#media .quotes'),
				$video = $content.find('.video'),
				$photos = $content.find('.photos'),
				$title = $content.find('h3'),
				$subtitle = $content.find('h4'),
				$description = $content.find('.description'),
				$logo = $content.find('.client-logo'),
				$impact = $content.find('.right p'),
				$links = $content.find('.links');
								
				function render(){
					$container.css('background-image', 'url('+project.wallpaper+')');
					
					//swap out the elements
					$title.text(project.title);
					$subtitle.text(project.subtitle);
					$description.html(project.description);
					$impact.html(project.impact);

					if(project.links){
						$links.children().remove();
						for(var i = 0; i < project.links.length; i++){
							var $li = $('<li>')
							var $a = $('<a>').text(project.links[i].title).attr('href', project.links[i].url).attr('target','_blank');
							$li.append($a);
							$links.append($li);
						}
						$links.show();
					}else{
						$links.hide();
					}
	
					//show the video first by default
					if(project.video && project.photos){
						$.Drive.video();
					}else if(project.video){
						$.Drive.video();
					}else if (project.photos){
						$quotes.hide();
						$.Drive.photo();
					}else{
						//console.error('No media');
					}
					
					$logo.children().remove();
					var logo = new Image();
					logo.onload = function() {
						$logo.append(logo);
					};
					logo.src = project.logo;
					$content.show();
					$container.fadeIn(500,'swing');
				}
				//get a nice transition on the go
				$container.fadeOut(500,'swing',render);
			},
			getPhotos: function(project){
				$('#work .frame').append("<div class='photos nivoSlider' style='display:none'>");
				if(project.photos){
					var images = [];
					$.each(project.photos, function(i,photo){
						var img = new Image();
						img.onload = function(){	
							images.push(img);
							if(images.length == project.photos.length){
								for(var j= 0; j< images.length; j++){
									$('#work .photos').append(images[j]);
								}
								$('#work .photos').nivoSlider({
									directionNav: true,
							        directionNavHide: true,
									controlNav:false,
									directionNavHide:false,
									effect:'slideInLeft'
								});
								$('#work .photos').show();
								$('#work .photos').data('nivoslider').start();
							}
						};
						img.src = photo.url;
					});
				}
			},
			load: function(project){
				var project = project;
				//add a menu marker
				var $container = $('#work .submenu ul');
				var $menuitem = $('<li><a href=\'#\'></a></li>');
				var $a = $menuitem.find('a');
				$menuitem.append($a);
				//bind an onclick to render this project
				$a.text(project.menu);
				$a.click(function(e){
					e.preventDefault();
					$.Drive.select(project);
				});
				//get the photos for the project if there's no video then present the menu tag						
				$container.append($menuitem);
			},
			start : function(){
				//grab data from the api
				var success = function(result){
					var projects = $.parseJSON(result);
					$.Drive.projects = projects
					for(var i = 0; i < $.Drive.projects.length; i++){
						$.Drive.load(projects[i]);
					}
				}
				var error = function(result){
					//console.error(result);
				}
				$.ajax({
					url: base+path,
					method: 'GET',
					success: success,
					error: error
				});
				
				//bind a click to the testimonial button too
				function testimonials()
				{
                    //hide the projects and show the tetimonials
                    function render()
                    {
                    	//	hide what we need to hide
                    	$( '#work .video, #work .frame, #work #content' ).hide();
                    	//	show the quotes again
                    	$( '.quotes' ).show();
                    	//	Set the correct background
                    	$( '#work' ).css('background-image', 'url(' + base + 'assets/images/wallpapers/work.jpg)');
                    	//	Fade it all back in
                    	$('#work').fadeIn( 500 );
                    }
                    $('#work').fadeOut( 500, render );
                    return false;
				}
				$( '#testimonials' ).click( testimonials );
				$( 'li[data-id=work] a' ).click( testimonials );
			}
		}
	
		$(document).ready(function(){
			$.Drive.start();
		});
	}//if
	
})(jQuery);
