//= require <jquery>

var RR = RR || {};

RR.work = {
	init: function(open) {

	$.ajax({
			type: 'get',
			dataType: 'xml',
			url: 'gallery.xml',
			processData: 'false',
			success: function(result) {
				var $result = $(result);
				$result.find('item').each(function(){
					var id = $(this).attr('id');
					var type = $(this).attr('type');
					var link = $(this).attr('link');
					var info = $(this).attr('info');
					var date = $(this).attr('date');
					var thumb = $(this).attr('thumb');
					var photo = $(this).attr('photo');
					var thumbnails = '<p id="photo_'+id+'" class="thumb"><img src="'+thumb+'" alt="'+date+' | '+info+' | '+link+'" /></p>';
					$('<li></li>').html(thumbnails).appendTo('#gallery ul#thumbs');
					
				});
			RR.work.thumbClick(open);
			RR.work.filters();
			}
		});
		$('#Content #container .title').text('portfolio');
	},
	
	thumbClick: function(open) {
		var index = 1;
		var tcount = 1;
		var count = $('#thumbs li:first').find('.thumb').attr('id');
		count = count.substr(6);
		var offset = 0;
		var ulWidth = $('#gallery ul#thumbs li').size();
		$('#gallery ul#thumbs').css('left','0');
		$('#gallery ul#thumbs').width(ulWidth*96);
		$('#gallery ul#thumbs li').first().css('background','#0012ff');
		var photo = 'img/gallery/photo/photo_'+count+'.jpg';
		$('#photo_container img').attr('src',photo);
		
		var alt = $('#thumbs li:first').find('.thumb').children().attr('alt');
		alt = alt.toString();
		alt = alt.split(' | ');
		var date = alt[0];
		var info = alt[1]; 
		var link = alt[2];
		var html = date+' : '+info+' '+'<a id="link" href="" target="_blank"></a>';
		$('#info').html(html);
		if(link != '') {
			$('#info #link').attr('href',link).text(': launch site >');
		}
		
		$('#prev_big').unbind('click').bind('click',function(evt) {
			if(index > 1) {
				count--;
				index--;
				$('#gallery ul#thumbs li').css('background','#fff');
				$(".thumb[id='photo_" + count + "']").parent().css('background','#0012ff');
				$('#photo_container img').attr('src','');
				var photo = 'img/gallery/photo/photo_'+count+'.jpg';
				$('#photo_container img').attr('src',photo);
				var alt = $(".thumb[id='photo_" + count + "']").children().attr('alt');
				alt = alt.toString();
				alt = alt.split(' | ');
				var date = alt[0];
				var info = alt[1]; 
				var link = alt[2];
				var html = date+' : '+info+' '+'<a id="link" href="" target="_blank"></a>';
				$('#info').html(html);
				if(link != '') {
					$('#info #link').attr('href',link).text(': launch site >');
				}
			}
			return false;
		});
		
		$('#next_big').unbind('click').bind('click',function(evt) {
			if(index < $('#gallery ul#thumbs li').size()) {
				count++;
				index++;
				$('#gallery ul#thumbs li').css('background','#fff');
				$(".thumb[id='photo_" + count + "']").parent().css('background','#0012ff');
				$('#photo_container img').attr('src','');
				var photo = 'img/gallery/photo/photo_'+count+'.jpg';
				$('#photo_container img').attr('src',photo);
				var alt = $(".thumb[id='photo_" + count + "']").children().attr('alt');
				alt = alt.toString();
				alt = alt.split(' | ');
				var date = alt[0];
				var info = alt[1]; 
				var link = alt[2];
				var html = date+' : '+info+' '+'<a id="link" href="" target="_blank"></a>';
				$('#info').html(html);
				if(link != '') {
					$('#info #link').attr('href',link).text(': launch site >');
				}
			}
			return false;
		});
		
		$('#prev').unbind('click').bind('click',function(evt) {
			if(tcount > 1) {
				tcount--;
				offset += 96;
				RR.work.animate(offset);
			}
			return false;
		});
		
		$('#next').unbind('click').bind('click',function(evt) {
			if(tcount < $('#gallery ul#thumbs li').size()) {
				tcount++;
				offset -= 96;
				RR.work.animate(offset);
			}
			return false;
		});
		
		$('.thumb').css('cursor','pointer');
		$('.thumb').unbind('click').bind('click',function(evt) {
			count = $(this).attr('id');
			count = count.substr(6);
			index = $(this).parent().index()+1;
			if($('#buttons li a').hasClass('selected')) {
				$('#buttons li a').removeClass('selected');
			}
			$('#Work').css('display','block');
			$('#Reel').css('display','none');
			$('#About').css('display','none');
			$('#Music').css('display','none');
			$('#Twitter').css('display','none');
			$('#gallery ul#thumbs li').css('background','#fff');
			$(this).parent().css('background','#0012ff');
			$('#photo_container img').attr('src','');
			var photo = 'img/gallery/photo/'+$(this).attr('id')+'.jpg';
			$('#photo_container img').attr('src',photo);
			$("#Nav").animate({top: "15px"}, 500, function() {
				$('#buttons').fadeIn('fast');
				$('#container').fadeIn('fast');
			});
			RR.nav.open(open=true);
			$('#Content #container .title').text('portfolio');
			var alt = $(this).children().attr('alt');
			alt = alt.toString();
			alt = alt.split(' | ');
			var date = alt[0];
			var info = alt[1]; 
			var link = alt[2];
			var html = date+' : '+info+' '+'<a id="link" href="" target="_blank"></a>';
			$('#info').html(html);
			if(link != '') {
				$('#info #link').attr('href',link).text(': launch site >');
			}
			return false;
		});
	},
	
	animate: function(offset) {
		
		$("#Nav #gallery ul#thumbs").animate({left: offset}, 250, function() {
			//callback
		});
	},
	
	filters: function() {
		$('#filters li#all a').addClass('selected');
		
		var selection;
		$('#filters li a').click(function(evt) {
			selection = $(this).text();
			RR.work.select(selection);
			return false;
		});
	},
	
	select: function(selection) {
		if($('#filters li a').hasClass('selected')) {
			$('#filters li a').removeClass('selected');
		}
		var type;
		switch(selection) {
			case '+ALL':
				$('#filters li#all a').addClass('selected');
				type = 'all';
				RR.work.filter(type);
				break;
			case 'CP+B':
				$('#filters li#cpb a').addClass('selected');
				type = 'cpb';
				RR.work.filter(type);
				break;
			case 'C+3D':
				$('#filters li#c3d a').addClass('selected');
				type = '3d';
				RR.work.filter(type);
				break;
			case 'C+2D':
				$('#filters li#c2d a').addClass('selected');
				type = '2d';
				RR.work.filter(type);
				break;
		}
	},
	
	filter: function(type) {
		
		$('#gallery ul#thumbs').children().remove();
		
		var filter = type;
		
		$.ajax({
			type: 'get',
			dataType: 'xml',
			url: 'gallery.xml',
			processData: 'false',
			success: function(result) {
				var $result = $(result);
				$result.find('item').each(function(){
					var id = $(this).attr('id');
					var type = $(this).attr('type');
					var link = $(this).attr('link');
					var info = $(this).attr('info');
					var date = $(this).attr('date');
					var thumb = $(this).attr('thumb');
					var photo = $(this).attr('photo');
					var thumbnails = '<p id="photo_'+id+'" class="thumb"><img src="'+thumb+'" alt="'+date+' | '+info+' | '+link+'" /></p>';
					if(filter == 'all') {
						$('<li></li>').html(thumbnails).appendTo('#gallery ul#thumbs');
					} else if(filter == type) {
						$('<li></li>').html(thumbnails).appendTo('#gallery ul#thumbs');
					}
				});
			RR.work.thumbClick(open);
			}
		});
	
	}
}
