/**
 * undocumented function
 *
 * @return void
 * @author Dan Bryant <db@leadingtone.org>
 * @package qd_common.js
 * @version 1.0
 * @perams:
 *	
 **/
function Video(){};
Video.prototype = {
	player:function(settings){
		// pull in configuration options
		var defaults = {
			vid: false // load this video into the video player
			,cid: false // load this category into the video player
		};

		if(settings) jQuery.extend(defaults, settings);

		var cid = defaults.cid;
		var vid = defaults.vid;

		// setup the palyer launcher options
		var opts = new Array();
		if(cid){ opts.push('category='+cid); }
		if(vid){ opts.push('nvideo='+vid); }
		var playeropts='';
		if(opts.length > 0){
			playeropts = '?' + opts.join('&');
		}

		// setup the window options
		var wopts = new Array();
		wopts.push('status=0'); 	 	// The status bar at the bottom of the window.
		wopts.push('toolbar=0');	 	// The standard browser toolbar, with buttons such as Back and Forward.
		wopts.push('location=0'); 		// The Location entry field where you enter the URL.
		wopts.push('menubar=0');		// The menu bar of the window
		wopts.push('directories=0');	// The standard browser directory buttons, such as What's New and What's Cool
		wopts.push('resizable=0'); 		// Allow/Disallow the user to resize the window.
		wopts.push('scrollbars=0'); 	// Enable the scrollbars if the document is bigger than the window
		wopts.push('height=610');		// Specifies the height of the window in pixels. (example: height='350')
		wopts.push('width=1000'); 		// Specifies the width of the window in pixels.

		var windowopts = '';
		if(wopts.length > 0){
			windowopts += wopts.join(',');		
		}
		var vidPlayer = window.open(GBL.base_path+"_qd.video_player.php"+playeropts,"vidPlayer",windowopts);
		vidPlayer.focus();
	}
	
	,categories:function(mode){

		switch(mode){
			case 'open':

				jQuery('.ie_container').animate({
					height:158
				},'slow','linear');

				jQuery('#video_container_bg').animate({
						height:112
					}
					,'slow'
					,'linear'
					,function(){
						jQuery('#close').fadeIn('fast');					
					}
				);

			break;

			default: // close

				jQuery('#close').fadeOut('fast',function(){

					jQuery('.ie_container').animate({
						height:451
					},'slow','linear');

					jQuery('#video_container_bg').animate({
						height:395
					},'fast','linear');
				});

		}

	}
	,loadVideo: function(vid){
		// console.log(GBL.base_path+'_qd.video_ajax.php'); 

		// docs: http://docs.jquery.com/Ajax/jQuery.ajax#options
		jQuery.ajax({
			url: GBL.base_path+'_qd.video_ajax.php'
			,dataType: 'json' // can be one of: xml, html, script, json, jsonp(see docs) or text.
			,type: 'POST' // POST or GET; default is get
			,data: 'op=loadVideo&vid='+vid
			,success: function(data){

				// console.log(data);

				if(!data.status){ // json request failed

					jQuery('#flv_container').html(data.rdata.player);
					jQuery('#advertisement').html(data.rdata.banner);
					jQuery('#description').html(data.rdata.description);

					jQuery.jGrowl(
						$P.sprintf('Invalid Video Id specified. Error: %s',json.message)
						,{ 
							sticky: true // make the notification "stick"
							,header: 'Video Load Error' // add a header to the message
						}
					);

				}else{

					jQuery('#flv_container').html(data.rdata.player);
					jQuery('#advertisement').html(data.rdata.banner);
					jQuery('#description').html(data.rdata.description);

				}
			}

		});

	}
	,loadVideoCategory:function(cid){

		var my = this;
		// docs: http://docs.jquery.com/Ajax/jQuery.ajax#options
		jQuery.ajax({
			url: GBL.base_path+'_qd.video_ajax.php'
			,dataType: 'json' // can be one of: xml, html, script, json, jsonp(see docs) or text.
			,type: 'POST' // POST or GET; default is get
			,data: 'op=loadVideoCategory&cid='+cid
			,success: function(data){

				if(!data.status){ // json request failed
	 				// TODO: growl here
				}else{
					jQuery('#category_title').html(data.rdata.category.name); // fill this with the category title
					jQuery('#video_thumbs_container').html(data.rdata.videolist); // fiull this with the markedup video list
					my.categories('close');
				}
			}

		});

	}


};
QDVideo = new Video();

jQuery(document).ready(function() {
	
	if(GBL.forceLoadVideo != 0){
		QDVideo.loadVideo(GBL.forceLoadVideo);
	}else{
		// figure out the first available video in the current video list and load that one 
		var loadID = jQuery('#video_thumbs_container > .video_thumb').attr('id');
		if(loadID){
			QDVideo.loadVideo(realID(loadID));			
		}
	}
	
	// hide the close button
	jQuery('#close').hide();
	
	var minHeight = 76;
	var maxHeight = 400;	

	// bind an mouse over action to the descrition
	jQuery('#description').bind('mouseenter',function(){
		var usedHeight = 0;
		jQuery('#description').removeClass('closed');
		jQuery('#description').addClass('open');
		jQuery('#description').children().each(function(){
			usedHeight += jQuery(this).height();
		});
		if(usedHeight > maxHeight) usedHeight = maxHeight;
		if(usedHeight > minHeight){ // only animate if we have to
			jQuery(this).animate({height:usedHeight},{
				duration:'fast'
				,easing:'linear'
				,queue: true
			});
		}
	});
	
	// bind a mouse leave action to the description
	jQuery('#description').bind('mouseleave',function(){
		jQuery('#description').removeClass('open');
		jQuery('#description').addClass('closed');
		jQuery(this).animate({height:minHeight},{
			duration:'slow'
			,easing:'swing'
			,queue: true
		});
	});
});

/**
 * Get a real id from a fake id; example: pull a real id from html dom element id attribute which cannot start with a number
 *
 * @return string
 * @author Dan Bryant <db@leadingtone.org>
 * @copyright LearningChange LLC - Dan Bryant (Shared Source Initiave/Dual Ownership), 17 March, 2007
 * @package strings.js
 * @version: 1.0
 * @perams:
 *	
 **/
function realID(id) { return id.substr(1,id.length); }
