(function($) {
	
	$.fn.wcitgallery = function (vars) {
		
		
		//Variables you can control outside this plugin
		settings = $.extend({
			indexStart	: 0,
			skipCount		: 5,
			controlOpacity	: 1
		}, vars);
		
		var x = 0;
		var elementWidth = 124;
		var stepCount = elementWidth * settings.skipCount;
		var element = $("#wcitGallery li");
		var numOfElements = $("#wcitGallery li").length;
		var totalWidth = numOfElements * elementWidth;
		var last = "-" + (totalWidth - (5 * elementWidth));
		var transSpeed = 750;
		var eIndex = numOfElements;
		$("#wcitGallery").css("width", totalWidth)
		$("#gallery").hide().fadeIn("slow");
		
		//	alert(last);
		
		if (numOfElements > 5) {
			$(".control-right").addClass("hover-right").animate({"opacity": 1});
		} else {
			$("#gallery").addClass("gridbackground");
			$(".control-right").removeClass("hover-right").animate({"opacity": settings.controlOpacity});
		}
		
		$(".control-left").removeClass("hover-left").animate({"opacity": settings.controlOpacity});
		
		
		$("#wcitGallery li a").click(function (ev) {
			ev.preventDefault();
			imgSrc = $(this).attr("href");
			var screenShot = $(this).attr("title");
			if ($(this).attr("alt") == "photo") {
				setImg(imgSrc);
			} else {
				setVideo(imgSrc, screenShot);
			}
		});
		
		
		function setImg (imgSrc) {
			$("#img-holder").fadeOut("fast", function () {
				$(this).html("<img width='671px' src='"+imgSrc+"' alt='' />").fadeIn("slow");
				$(this).find("img").load(function () {
					$("#img-holder").fadeIn("slow");
				});
			});
		}
		
		function setVideo (imgSrc, screen) {
			$("#img-holder").fadeOut("fast", function () {
			
				var flashvars = false;
				var params = {
					flashvars: "autoplay=true&file=" + imgSrc + "&image="+screen,
					allowFullScreen: true
				};

				swfobject.embedSWF("/flash/galleryplayer.swf", "img-holder", "678", "453", "9.0.0", "expressInstall.swf", flashvars, params);
				
				$(this).fadeIn("slow");
			});
		}
		//flashvars="file=video.flv&image=preview.jpg" 
		
		$(".control-left").click(function () {
			if (eIndex >= numOfElements) {
				eIndex = numOfElements;
			} else {
				$(".control-right").addClass("hover-right").animate({"opacity": 1});
				numLeft = (parseFloat(numOfElements) - (parseFloat(eIndex)));
				if (numLeft >= settings.skipCount) {
					eIndex = parseFloat(eIndex) + settings.skipCount;
					$("#wcitGallery").animate({
						marginLeft: "+=" + stepCount + "px"
					}, transSpeed, "swing", function () {
						if (eIndex >= numOfElements) {
							$(".control-left").removeClass("hover-left").animate({"opacity": settings.controlOpacity});
						}
					});
				} else {
					numLeft = parseFloat(numOfElements) - parseFloat(eIndex);
					eIndex = numOfElements;
					$("#wcitGallery").animate({
						marginLeft: "+=" + (numLeft * elementWidth) + "px"
					}, transSpeed, "swing", function () {
						$(".control-left").removeClass("hover-left").animate({"opacity": settings.controlOpacity});
					});
				}
			}
		});
		
	
		
		
		$(".control-right").click(function () {
			if (eIndex <= 5) {
				eIndex = eIndex;
			} else {
				$(".control-left").addClass("hover-left").animate({"opacity": 1});
				if ((eIndex - settings.skipCount) >= settings.skipCount) {
					eIndex = parseFloat(eIndex) - settings.skipCount;
					$("#wcitGallery").animate({
						marginLeft: "-=" + stepCount + "px"
					}, transSpeed, "swing", function () {
						if (eIndex < 5 || numOfElements < 6) {
							$(".control-right").removeClass("hover-right").animate({"opacity": settings.controlOpacity});
						}
					});
				} else {
					numLeft = parseFloat(eIndex) - settings.skipCount;
					eIndex = 5;
					$("#wcitGallery").animate({
						marginLeft: "-=" + (numLeft * elementWidth) + "px"
					}, transSpeed, "swing", function () {
						$(".control-right").removeClass("hover-right").animate({"opacity": settings.controlOpacity});
					});
				}
			}
		});
		
		if ($(element[x]).find("a").attr("alt") == "photo") {
			setImg($(element[x]).find("a").attr("href"));
		} else {
			setVideo($(element[x]).find("a").attr("href"), $(element[x]).find("a").attr("title"));
		}
		
	}
	
})(jQuery);

$("#wcitGallery").wcitgallery();