//standards
//all variables:                        words are seperated by "_"
//global variables:                     camel cased
//local variables:                      lower case
//function parameters:                  lower cased and prefixed with an "_"
//function names:                       words not seperated, first word is lower case, subsequent words are camel cased.

var Video_ID = "";
var Show_Title = 0;                     // 0 = false; 1 = true
var Show_Byline = 0;                    // 0 = false; 1 = true
var Show_Portrait = 0;                  // 0 = false; 1 = true
var Color = "009fd4";                   // default cyan
var Fullscreen = 1;                     // 0 = false; 1 = true
var Default_Native_Width = 478;
var Default_Native_Height = 269;
var Current_Video_Native_Width = 0;     // set by the embedVideo function
var Current_Video_Native_Height = 0;    // set by the embedVideo function

embedVideo = function(_video_id,_native_width,_native_height)
{    
	Video_ID = _video_id;
	
	if (Video_ID != "")
    {
    	var video_source = "http://player.vimeo.com/video/"+Video_ID+"?api=1&amp;player_id=player&amp;title=0&amp;byline=0&amp;portrait=0&amp;color=009fd4;&amp;autoplay=0";
    			
		//get the native width, either from a passed in value, or from the default
		var video_native_width;
		
		if (_native_width == undefined)
		{
			video_native_width = Default_Native_Width;
		}
		else
		{
		    video_native_width = _native_width;
		}
		
		//get the native height, either from a passed in value, or from the default
		var video_native_height;
		if (_native_height == undefined)
		{		
		    video_native_height = Default_Native_Height;
		}
		else
		{
		    video_native_height = _native_height;
		}
		
		//set the native width and height for this video - these values are reused in the sizeVideo function on resize
		Current_Video_Native_Width = video_native_width;
		Current_Video_Native_Height = video_native_height;

        //get the ratio of the width of the container on the page to the native width
	    var ratio = getSizeRatio(Current_Video_Native_Width);
        
        //set the width and height values according to the width of the container on the page, maintaining aspect ratio
	    var video_height = Current_Video_Native_Height * ratio; // Reset height to match scaled image
	    var video_width = Current_Video_Native_Width * ratio; // Reset width to match scaled image
	    
		$("#video-outer").css(
		{
			"height": video_height
		});
	    $("#video-outer").addClass("loading");
    	
    	$("#video-outer").append
		(
			'<iframe id="video-player" src="'+video_source+'" height="'+video_height+'" width="'+video_width+'" frameborder="0"></iframe>'
		);
	}
}

sizeVideo = function()
{    
    $("#video").css(
	{
		"visibility": "hidden"
	});
    
    // get the ratio of the native width to the width of the container on the page
    var ratio = getSizeRatio(Current_Video_Native_Width);
	
	// set the width and height values according to the width of the container on the page, maintaining aspect ratio	
    var video_height = Current_Video_Native_Height * ratio; // Reset height to match scaled image
    var video_width = Current_Video_Native_Width * ratio; // Reset width to match scaled image
	
	$("#video-outer").css(
	{
		"height": video_height
	});
    $("#video-outer").addClass("loading");
    
    $("#video").css(
	{
		"height": video_height,
		"width": video_width
	});
	
	$("#video").css(
	{
		"visibility": "visibile"
	});
}

getSizeRatio = function(_native_video_width)
{
    var container_width = $("#video-outer").width();
    var max_width = container_width;    	
    var ratio = 0;  // Used for aspect ratio

    ratio = max_width / _native_video_width; // get ratio for scaling the selector
    
    return ratio;
}

$(window).resize(function()
{
	//sizeVideo();
});

updateVideo = function(_selector_id, _video_id, _native_width, _native_height)
{
	$("#video-player").remove();
	
	$("ul.video-list li#" + _selector_id).addClass("selected");
	$("ul.video-list li").not("ul.video-list li#" + _selector_id).removeClass("selected");
	
	embedVideo(_video_id, _native_width, _native_height);
	scrollToSelector("#video-outer");
}
