﻿$.fn.gallery = function(side, content, desc, defaults){
		$.gallery = this;
		$.extend(this, {
				_content: $(content),
				_desc: $(desc),
				_side: $(side),
				_list: $(side + " .list"),
				_up: $(side + " .up"),
				_down: $(side + " .down"),
				_left: $(side + " .left"),
				_right: $(side + " .right"),
				_speed: 20000
			}, defaults);
		this._content.html("<div></div>");
		
		this._scroll = true;
		
		if(this._list.children("a").length > 4){
			with($.gallery){
				_up.css("visibility", "visible");
				_down.css("visibility", "visible");
			}
		}
		this._list.bind("mousemove", function(e){
			return;
			var l = $(e.srcElement);
			var mouseOffset = {
				top: l.pageY - l.offset().top,
				left: l.pageX - l.offset().left
			};
			if(mouseOffset.top < l.height() < 2)
				$.gallery._up.trigger("mouseover");
			else
				$.gallery._down.trigger("mouseover");
		});
		this._oSpeed = this._speed;
		this._stop = function(){
			if(!$.gallery._scroll)
				return;
			$.gallery._speed = $.gallery._oSpeed;
			$.gallery._list.stop();
		};
		this._startMovement = function(horizontal){
			if(!horizontal){
				this._height = this._list.height();
				this._scrollHeight = this._list[0].scrollHeight;
			}else{
				this._width = this._list.width();
				this._scrollWidth = this._list[0].scrollWidth;
			}
		};
		this._down.bind("mouseover", function(e){
			
			var _ = $.gallery;
			_._startMovement();
			_._list.animate({scrollTop: _._scrollHeight + _._height}, {
				duration: _._speed
			});
		});
		
		
		
		
		
		
		this._up.bind("click", function(e){
			var _ = $.gallery;
			_._scroll = false;
			_._startMovement();
			_._list.stop();
			_._list.animate({scrollTop: 0}, {
				duration: 1000, easing: "easeOutBounce", complete: function(){
					_._scroll = true;
				}
			});
		});
		this._down.bind("click", function(e){
			var _ = $.gallery;
			_._scroll = false
			_._startMovement();
			_._list.stop();
			_._list.animate({scrollTop: _._scrollHeight - _._height}, {
				duration: 1000, easing: "easeOutBounce", complete: function(){
					_._scroll = true;
				}
			});
		});
		this._up.bind("mouseover", function(e){
			var _ = $.gallery;
			_._startMovement();
			_._list.animate({scrollTop: -1*_._height}, {
				duration: _._speed
			});
		});
		
		
		this._left.bind("mouseover", function(e){
			var _ = $.gallery;
			_._startMovement(true);
			_._list.animate({scrollLeft: -1*_._width}, {
				duration: _._speed
			});
		});
		
		this._left.bind("click", function(e){
			var _ = $.gallery;
			_._scroll = false;
			_._startMovement(true);
			_._list.stop();
			_._list.animate({scrollLeft: 0}, {
				duration: 1000, easing: "easeOutBounce", complete: function(){
					_._scroll = true;
				}
			});
		});
				
		this._right.bind("mouseover", function(e){
			
			var _ = $.gallery;
			_._startMovement(true);
			_._list.animate({scrollLeft: _._scrollWidth + _._width}, {
				duration: _._speed
			});
		});
		
		this._right.bind("click", function(e){
			var _ = $.gallery;
			_._scroll = false
			_._startMovement(true);
			_._list.stop();
			_._list.animate({scrollLeft: _._scrollWidth - _._width}, {
				duration: 1000, easing: "easeOutBounce", complete: function(){
					_._scroll = true;
				}
			});
		});
		
		this._list.bind("mousewheel", function(e, d){
			var _ = $.gallery._list;
			if(_.wheelscroll)
				return;
			_.wheelscroll = true;
			 $.gallery._startMovement();
			_.animate({scrollTop:  d < 0? "+=" +  $.gallery._height: "-=" + $.gallery._height},{
				duration: 500, easing: "easeInQuad", complete: function(){
					_.wheelscroll = false;
				}
			});
		});
		this._down.bind("mouseout", this._stop);
		this._up.bind("mouseout", this._stop);
		this._left.bind("mouseout", this._stop);
		this._right.bind("mouseout", this._stop);
		
		
		var $a = this._a = $(".list a");
		$a.bind("click", function(e){
			var url = this.href;
			var website = $a.attr("website");
			
			var temp = url.split(".");
			var extension = temp.length > 1? temp[temp.length-1]: "";
			var displayType = "";
			switch(extension.toLowerCase()){
				case "jpg":
				case "gif":
				case "png":
				case "bmp":
					displayType = "image";
					break;
				case "flv":
					displayType = "video";
					break;
				case "swf":
					displayType = "flash";
					break;
				default:
					break;
			}
			if(displayType == "")
				return true;
			
			var _ = $.gallery;
			_._content.addClass("loading");
			_._desc.html("");
			_._desc.html([
				$(this).children("p.desc").html().replace(/\(School Websites*\)/ig, "").replace(/\s*$/, ""),
				website? " | ": "",
				website? "<a title=\"" + this.title + "\" href=\"" + website + "\" target=_blank>": "",
				website? "<span class=web>" + website.replace("http://", "") + "</span>": "",
				website? "</a>": ""				
			].join(""));
			_._content.children("*").fadeOut("fast", function(){
				switch(displayType)
				{
					case "image":
						var realContent = [
							website? "<a title=\"" + this.title + "\" href=\"" + website + "\" target=_blank>": "",
							"<img src=" + url + " alt=\"" + this.title + "\" />",
							website? "</a>": ""
						].join("");
						_._content.html(realContent);
						var $img = _._content.children("img");
						if($img.length == 0)
							$img = _._content.children("").children("img");
							
						$img.bind("load", function(){
							_._content.removeClass("loading");
							$img.fadeIn("slow");
						});
						break;
					case "video":
						var width = 400, height = 300;
					var realContent = [
						'<object class=flashplayer classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" ',
						'width="' + width + '" ',
						'height="' + height + '" ',
						'title="' + this.title + '">',
		  				'<param name="movie" value="'+liteweb.vroot+'/flash/flvplayer.swf" />',
  						'<param name="quality" value="high" />',
  						'<param name="salign" value="TL" />',
  						'<param name="scale" value="noScale" />',
  						'<param name="FlashVars" value="path='+url+'" />',
  						'<embed class=flashplayer src="'+liteweb.vroot+'/flash/flvplayer.swf" ',
  						'quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" ',
		  				'width="' + width + '" ',
		  				'height="' + height + '"',
		  				'salign="TL" ',
  						'scale="noScale" ',
  						'FlashVars="path='+url+'"',
  						'></embed>',
						'</object>'].join("");
						_._content.removeClass("loading");
						_._content.html(realContent);
						break;
					case "flash":
					var width = 400, height = 300;
					var realContent = [
						'<object class=flashplayer classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,28,0" ',
						'width="' + width + '" ',
						'height="' + height + '" ',
						'title="' + this.title + '">',
		  				'<param name="movie" value="'+url+'" />',
  						'<param name="quality" value="high" />',
  						'<param name="salign" value="TL" />',
  						'<embed class=flashplayer src="'+url+'" ',
  						'quality="high" pluginspage="http://www.adobe.com/shockwave/download/download.cgi?P1_Prod_Version=ShockwaveFlash" type="application/x-shockwave-flash" ',
		  				'width="' + width + '" ',
		  				'height="' + height + '"',
		  				'salign="TL" ',
  						'></embed>',
						'</object>'].join("");
						_._content.removeClass("loading");
						_._content.html(realContent);
						break;
				}
			});
			return false;
		});
		if($a.length > 0)
			$($a[0]).trigger("click");
	};
