var i=0;


/*
//REQUIRED FILES IN YOUR HEADER TO USE THIS FILE:
<!-- JQ Videobox Files -->
 <script type="text/javascript" src="/js/jquery-latest.pack.js"></script>
 <script type="text/javascript" src="/js/jquery.jwbox.js"></script>
 <script type="text/javascript" src="/js/swfobject.js"></script>
 <link rel="stylesheet" type="text/css" href="/css/jwbox.css" />
 <!-- JQ Videobox Files End -->
*/

 
 
 
 /*
//EACH OF THESE VIDEOS WILL BE ADDED TO THE GALLERY, TOP VIDEO STARTS IN TOP LEFT, AND THEY FALL IN NEXT TO THE RIGHT
//AT THE END OF A LINE (if it doesn't fit), it will drop down to the next line, far left.

//USAGE EXAMPLE FOR willsJWBox:
//willsJWBox(vidID, quote, thumbImageNum, popupImgPath, popupW, popupH, gallerWrapperID)
//willsJWBox("m-JC-H4-Bz4", "Lionel Hampton Jazz Festival", 1, "http://i.ytimg.com/vi/m9UKg8_SEv0/2.jpg", 400, 300, "jwboxesWrapper");
*/


function willsJWBox(vidID, quote, thumbImageNum, popupImgPath, popupW, popupH, gallerWrapperID)
{
//THIS FUNCTINO WRITES OUT A JWBOX PLAYER
//DEFAULTS
		var _gallerWrapperID = "jwboxesWrapper";
        var _urlBase = "http://www.youtube.com/watch?v=";
        var _thumbBase = "http://img.youtube.com/vi/";
        var _thumbImageNum = 1;
        var _imgCss = "width:150px;"; 
        var _popupW = '800';
        var _popupH = '600';

		//CHECK FOR VALUES COMING IN,
		//if they're not there, use the defaults
		
		if(!gallerWrapperID)
		{
		  gallerWrapperID = _gallerWrapperID;
		}
		if(!popupW)
		{
		  popupW = _popupW;
		}
		if(!popupH)
		{
		  popupH = _popupH;
		}		
		if(!thumbImageNum)
		{
		  thumbImageNum = _thumbImageNum;
		}
		if(popupImgPath)
		{
			img = popupImgPath;
		}
		else
		{
		    img = _thumbBase + vidID + "/" + thumbImageNum + ".jpg";
		}
		if(vidID.indexOf("http") > -1)
		{
		  img2 = img;
		  vidUrl = vidID;
		}
		else
		{
		  img2 = _thumbBase + vidID + "/" + 2 + ".jpg";
          vidUrl = _urlBase + vidID;
		}

		//HANDLE i counter
		try
		{
			if(i == null)
			{
			  i=0;
			}
		}
		catch(ex)
		{
		    document.write("error:" + ex);
			i=0;
		}
		i+=1;
		
		
		//HANDLE so array
		try
		{
			if(!s)
			{
			  so = new Array();
			}
		}
		catch(ex)
		{
			var so = new Array();
		}
		
		
        //BEGIN jwbox
        
        
        jQuery("#" + gallerWrapperID).append("<div id='jwbox" + i + "' class='jwbox'>");
		
		jQuery("#jwbox" + i).append("<a class='play'></a>");
		
		
        jQuery("#jwbox" + i).append("<img class='thumbnail' alt='JW Box Image' title='JW Box Image' src='" + img + "' onmouseover='this.src=\"" + img2 + "\"; jQuery(\"#jwbox" + i + " .play\").css(\"visibility\", \"visible\");' onmouseout='this.src= \"" + img + "\"; 	jQuery(\"#jwbox" + i + " .play\").css(\"visibility\", \"hidden\");' />");
		
		if(quote)
		{
			jQuery("#jwbox" + i).append("<p>" + quote + "</p>");
		}
		

            jQuery("#jwbox" + i).append("<div class='jwbox_hidden'>");

                jQuery("#jwbox" + i + " .jwbox_hidden").append("<div class='rightcorner'>&nbsp;</div>");
                jQuery("#jwbox" + i + " .jwbox_hidden").append("<div class='leftcorner'>&nbsp;</div>");
                jQuery("#jwbox" + i + " .jwbox_hidden").append("<div class='topmiddle'>&nbsp;</div>");

                jQuery("#jwbox" + i + " .jwbox_hidden").append("<div class='jwbox_wrap'>");
                    

                jQuery("#jwbox" + i + " .jwbox_wrap").append("<div id='player" + i + "' class='spacer'></div>");

					  var flashvars = 
					  {
                          file: vidUrl + '&enablejsapi=1&version=3&playerapiid=ytplayer&autoplay=true&image=' + popupImgPath,
                          autostart:"true"
					  }
					  var params = 
					  {
							  allowfullscreen:"true",
							  allowscriptaccess:"always"
					  }
					  var attributes = 
					  {
							  id: 'player' + i, 
							  name: 'player' + i
					  }
					  swfobject.embedSWF('http://player.longtailvideo.com/player.swf', 'player' + i, popupW, popupH, "9.0.115", false, flashvars, params, attributes);
 
					 if(quote)
					 {
                        jQuery("#jwbox" + i + " .jwbox_wrap").append("<p>" + quote + "</p>");
					 }
					/*
                 jQuery("#" + gallerWrapperID).append("</div>");
             jQuery("#" + gallerWrapperID).append("</div>");
             jQuery("#" + gallerWrapperID).append("</div>");
        jQuery("#" + gallerWrapperID).append("</div>");
		*/
}





jQuery.fn.center = function () {
	this.css("position","fixed");
	this.css("top", ( jQuery(window).height() - this.outerHeight() ) / 2 + "px");
	this.css("left", ( jQuery(window).width() - this.outerWidth() ) / 2 + "px");
    return this;
}

jQuery.jwbox = {
	lightbox	:	null,
	player	: null,
	toggle	: function(context) {
		if (!jQuery.jwbox.lightbox) {
				jQuery.jwbox.lightbox = jQuery(".jwbox_hidden", context);
				jQuery.jwbox.center();
				jQuery("#jwbox_background").fadeIn("fast");
				jQuery.jwbox.lightbox.css("display","block")
				jQuery.jwbox.center();
				jQuery("#jwbox_background").fadeTo(0, 0.8);
				jQuery("object", context).each(function(){
					jQuery.jwbox.player = document.getElementById(this.id);
				});
		} else if ((context.className == 'jwbox_content')) {
		} else {
			try {
				jQuery.jwbox.player.sendEvent("STOP");
				jQuery.jwbox.player = null;
			} catch (err) {
			}
			jQuery.jwbox.lightbox.css("display","none");
			jQuery.jwbox.lightbox = null;
			jQuery("#jwbox_background").fadeOut("fast");
		}
	},
	center	: function() {
		if (jQuery.jwbox.lightbox) {
			jQuery.jwbox.lightbox.center();
		}
	}
}

jQuery(document).keyup(function(event){
    if (event.keyCode == 27 && jQuery.jwbox.lightbox) {
		jQuery.jwbox.toggle(jQuery("#jwbox_background"));
    }
});

jQuery(document).ready(function () {
	jQuery("body").append('<div id="jwbox_background">&nbsp;</div>');
	jQuery(".jwbox").click(function () {jQuery.jwbox.toggle(this); return false;});
	jQuery("#jwbox_background").click(function () {jQuery.jwbox.toggle(this); return false;});
	jQuery(".jwbox_content").click(function () {jQuery.jwbox.toggle(this); return false;});
	jQuery(window).resize(function() {jQuery.jwbox.center();});
});
