/*
 * rdai.slideshow.js
 *
 * All code/functions/classes
 * Author(s): Matt Robenolt, Kevin Krpicak
 *
 * Copyright © 2010 RDA International & YDEK Productions LLC
 * http://rdai.com & http://ydekproductions.com
 * hey@ydekproductions.com
 *
 * $Id: rdai.slideshow.js 113 2010-03-26 14:42:33Z rdai_paulc $
 *
 * No, don't use this if you somehow get ahold of it.  We will hunt you down
 * and probably inflict massive amounts of pain on your family.
 */
rdai.slideshow = {
	
	cache: {
		scrollbarWidth: 625,
		scrollerWidth: 89
	},
	
	current: 0,
	slide: null,
	total: 0,
	slideTotal: 0,
	slideAmount : 0,
	ids: [],
	LEFT_KEYCODE: 37,
	RIGHT_KEYCODE: 39,
	animationQueue: [],
	isQueueRunning: false,
	isShown: false,
	
	init: function()
	{
		var s = rdai.slideshow;
		s.total = $('#slideshow').children('li').length;
		s.slideTotal= Math.floor(625/s.total);
		s.slideAmount = 0;
		
		$('#right_arrow').click(s.clickRightArrow);
		$('#left_arrow').click(s.clickLeftArrow);
		$('#close').click(function()
		{
			s.resetVids();
			$.address.value('/work');
		});
		
		$('#overlay > h1').click(function()
		{
			s.resetVids();
			$.address.value('/work');
		});
		
	
		$('#slideshow').children('li').each(function(i, li)
		{
			s.ids.push($(li).attr('id'));
		});
		
		$('div[rel^="youtube"]').each(rdai.swfobjectsettings.renderSwf);
		
		$.address.change(s.changeWork);
		
		s.jumpTo(s.ids[0]); //show initial project
	},
	
	changeWork: function()
	{
		if($.address.pathNames().length < 2)
		{
			rdai.slideshow.hide();
		}
		else
		{
			var page = $.address.pathNames()[1];
			
			if(!rdai.slideshow.isShown)
			{
				rdai.slideshow.jumpTo(page);
				rdai.slideshow.show();
			}
			else
			{
				rdai.slideshow.navigate(page);
			}
		}
	},
	
	hide: function()
	{
		if(!rdai.slideshow.isShown) return;
		
		if(rdai.ie6) rdai.slideshow.killResizeListener();
		
		if(rdai.ie)	$('#overlay').hide(); 
		else		$('#overlay').fadeOut(); 
		
		//rdai.slideshow.killArrowKeys();
		rdai.slideshow.isShown = false;
		
		if(rdai.ie6) $('body, html').css({'overflowX': 'hidden', 'overflowY': 'scroll'});
		else $('body').css('overflow', 'visible');
		
		rdai.setUpScrollListener();
		
	},
	
	show: function()
	{
		if(rdai.slideshow.isShown) return;
		
		if(rdai.ie6) rdai.slideshow.setUpResizeListener();
		
		if(rdai.ie6) $('body, html').css({'overflowX': 'hidden', 'overflowY': 'hidden'});
		else $('body').css('overflow', 'hidden');

		if(rdai.ie)	$('#overlay').show(); 
		else		$('#overlay').fadeIn(); 
		
		
		rdai.killScrollListener();
		//rdai.slideshow.activateArrowKeys();
		rdai.slideshow.isShown = true;
	},
	
	setUpResizeListener: function()
	{
		//this is only for IE6
		$(window).unbind('resize.slideshow').bind('resize.slideshow', function()
		{
			$('#overlay, #slideshow').css({
				'height': $(window).height()+'px',
				'width': $(window).width()+'px'
			});
			$('#overlay').css('top', $(window).scrollTop()+'px');
		}).resize();
	},
	
	killResizeListener: function()
	{
		$(window).unbind('resize.slideshow');
	},
	
	/*activateArrowKeys: function()
	{
		$(document).unbind('keydown.slideshow').bind('keydown.slideshow', function(e)
		{
			if(e.which == rdai.slideshow.LEFT_KEYCODE) rdai.slideshow.clickLeftArrow();
			else if(e.which == rdai.slideshow.RIGHT_KEYCODE) rdai.slideshow.clickRightArrow();
		});
	},
	
	killArrowKeys: function()
	{
		$(document).unbind('keydown.slideshow');
	},*/
	
	jumpTo: function(id)
	{
		var s = rdai.slideshow;
		
		var i = $.inArray(id, s.ids);
		s.animationQueue = [];
		
		var $el_current = $('#slideshow > li:eq('+s.current+')');
		var $el_next = $('#slideshow > li:eq('+i+')');
		var attrs = s.parseRelAttributes($el_next.attr('rel'));
		
		$el_current.stop().css({'left': '1000px'});
		$el_current.css({'opacity': 0});
		$el_next.stop().css({'left': 0});
		$el_next.css({'opacity': 1});
		$('#slideshow').css({'backgroundColor': attrs.bg});
		//$('#slideshow').stop().css({'backgroundColor': attrs.bg});
		//$('#slideshow_scroller > p').text(attrs.cat || '');
		//$('#scroller').stop().css({'left': (i/(s.total-1))*(s.cache.scrollbarWidth-s.cache.scrollerWidth)+'px'});
		
		s.current = i;
		
		s.resetArrows();
	},
	
	navigate: function(id)
	{
		var s = rdai.slideshow;
		
		var i =  $.inArray(id, s.ids);
		if(i == -1 || i == s.current) return;
		
		var $el_current = $('#slideshow > li:eq('+s.current+')');
		var $el_next = $('#slideshow > li:eq('+i+')');
		var attrs = s.parseRelAttributes($el_next.attr('rel'));
		
		var dir = i > s.current;
		
		$('#'+s.ids.join(', #')).each(function(j, el)
		{
			if(j == i) return;
			if(j == s.current) return;
			$(el).stop().css({'left': '1000px'});
			$(el).css({'opacity': 0});
		});
		$el_current.stop().animate({'left': dir?'-1000px':'1000px'}, 300, '', function()
		{
			$el_current.css({'opacity': 0});
			$('#slideshow').css({'backgroundColor': attrs.bg});
			$el_next.stop().css({'left': dir?'1000px':'-1000px'}).animate({'left': 0}, 300);
			$el_next.css({'opacity': 1});
			/*console.log(dir);
			if(dir == true) $el_next.css({'opacity': 1});
			else $el_next.css({'opacity': 0});*/
			/*
			$('#slideshow').stop().animate({'backgroundColor': attrs.bg}, 300, function()
			{
				//$('#slideshow_scroller > p').text(attrs.cat || '');
				$el_next.stop().css({'left': dir?'1000px':'-1000px', 'opacity': 0}).animate({'left': 0, 'opacity': 1}, 300);
			});*/
		});
		
		//$('#scroller').stop().animate({'left': (i/(s.total-1))*(s.cache.scrollbarWidth-s.cache.scrollerWidth)+'px'}, 900);
		
		s.current = i;
		
		s.resetArrows();
	},
	
	clickRightArrow: function()
	{
		var s = rdai.slideshow;
		if(!s.ids[s.current+1]) return;
		$.address.value('/work/'+s.ids[s.current+1]);
		s.resetVids();
	},
	
	clickLeftArrow: function()
	{
		var s = rdai.slideshow;
		if(!s.ids[s.current-1]) return;
		$.address.value('/work/'+s.ids[s.current-1]);
		s.resetVids();
	},
	
	resetArrows: function()
	{
		var s = rdai.slideshow;
		$('#left_arrow, #right_arrow').show();
		if(s.current == 0) $('#left_arrow').hide();
		if(s.current == s.total-1) $('#right_arrow').hide();
	},
	
	parseRelAttributes: function(rel)
	{
		var response = {};
		var o = rel.split('|');
		for(var i=0; i<o.length; i++)
		{
			var parts = o[i].split(':');
			response[parts[0]] = parts[1];
		}
		return response;
	},
	
	resetVids: function(){
		var sos = rdai.swfobjectsettings;
		for(var i = 0; i < sos.allVidIds.length; i++){
			try{
				$("#"+sos.allVidIds[i])[0].stopPlaying();
			}catch(e){
				//console.log(e.message);
			}

		}
	 }
};

