/*
* FEATURES v1.1
* Copyright (c) 2011 Paul Woods
* Dual licensed under the MIT and GPL licenses.
*/
var Features = {

	// Public Vars
	start		: 1, // Video to launch on init()
	delay		: 7000, // Default delay between transition
	offset		: 0, // Set the height of your list item before calling ini()
	speed		: 500, // Default scroll speed
	carousel	: false, // The Carousel object name as Jquery string
	container	: false, // The Carousel container object name as Jquery string
	loop		: false, // Loop the start and end points (true/false)
	
	// Private Vars
	scroll		: true, // Scroll enabled
	current 	: 0, // Do not change first feature index
	interval	: false, // The object timer interval
	minwdith	: 0, // The minimum scroll width
	maxwidth	: 0, // The maximum scroll offset
	max			: 0, // Count the list items calculated on ini()
	pos			: 0, // Scroll position in px
	selected	: false, // The selected video
	
	ini : function ()
	{
		// Setup
		
		var self = this;
		this.max = Number($(".feature-item").length); // Count the total tweets
		// Set the width of the carousel
		var carousel_width = this.offset*this.max;
		$(this.carousel).css ('width',carousel_width);
		var container_width = $(this.container).css('width');
		container_width = Number(container_width.substr(0,container_width.length-2));
		this.maxwidth = (carousel_width-container_width);
		// Reset Carousel to 0 position
		$(this.carousel).css('left',0);
		// Click launch Video(s)
		$('.feature-item').click ( function () {
			self.load($(this).find("a").attr("rel"));
			self.select($(this).find("a").attr("id"));	
		});
		// Load Default Video
		this.select(this.find_anchor_id(this.start));
		this.load(this.find_video_id(this.start));
		// Only bind behaviours if we have enough features
		if (carousel_width < container_width) {
			this.scroll = false;
			return;
		}
		// Bind user interface
		$('#feature-ui-next').click ( function () {
			self.next();
		});
		$('#feature-ui-prev').click ( function () {
			self.prev();
		});
		$('#feature-ui-first').click ( function () {
			self.first();
		});
		$('#feature-ui-last').click ( function () {
			self.last();
		});
	},
	find_anchor_id : function (i)
	{
		// Find anchor ID
		return $("#video-"+i).attr("id");
	},
	find_video_id : function (i)
	{
		// Find video ID
		return $("#video-"+i).attr("rel");
	},
	debug : function (theText)
	{
		$("#debug").html(theText);	
	},
	first : function ()
	{
		// First item
		this.move(+this.pos);
	},
	last : function ()
	{
		// Last item
		this.move(+this.maxwidth,true);
	},
	prev : function ()
	{
		// Previous item
		this.move(+this.offset);
	},
	next : function ()
	{
		// Next item
		this.move(-this.offset);
	},
	move : function (pos,jump)
	{
		// Move Carousel
		if (!jump) {
			this.pos = (this.pos-=pos);
		} else {
			this.pos = pos;
		}
		// Loop Start
		if (this.pos < 0) {
			this.pos = 0;
			if (this.loop) {
				this.last();
			}
		}
		// Loop End
		if (this.pos > this.maxwidth) {
			this.pos = this.maxwidth;
			if (this.loop) {
				this.first();
			}
		}
		$(this.carousel).stop(true,false);
		var now = $(this.carousel).css('left');
		now = Math.abs(Number(now.substr(0,now.length-2)));
		var move = this.pos-now;
		this.debug("left:"+now+" go:"+move);
		$(this.carousel).animate ({"left" : "-="+move},this.speed,false);
	},
	select : function (theId)
	{
		// Toggle Selected Class
		if (!theId) {
			theId = "video-1";
		}
		var obj; var name; var overlay;
		for (i=0;i<=this.max;i++) {
			name = "video-"+i;
			obj = $("#"+name);
			overlay = $("#feature-overlay-"+i);
			if (name == theId) {
				overlay.css("display","none");
			} else {
				overlay.css({
				'display' : 'block',
				'border-left':'3px solid black',
				'border-right':'3px solid black',
				'border-top':'10px solid black',
				'border-bottom':'10px solid black',
				'width' : '94px',
				'height' : '60px'
				});
			}
		}
	},
	load : function (theID)
	{
		// Load Youtube SWF
		if (!theID) return;
		var flashvars = {};
		var params = {
			wmode: "opaque",
			bgcolor: "000000"
		};
		var attributes = {};
		swfobject.embedSWF("http://www.youtube.com/v/"+theID+"?version=3&modestbranding=1", "feature-video", "630", "450", "9.0.124", false, flashvars, params);
	}
};

$(document).ready ( function () {
	
	Features.container = "#feature-scroller-container";
	Features.carousel = "#feature-scroller-carousel";
	Features.offset = 100;
	Features.loop = false;
	Features.ini();
});
