var video_width = 624;
var video_height = 367;
function loadVideo(newvid, newjpg, idcontainer){
	var mwFO = new FlashObject("Newvision_Video_Interface_Widescreen3.swf", "nv", video_width, video_height, "8", "#ffffff");
	mwFO.addVariable("flv_url", newvid);
	mwFO.addVariable("jpg_url", newjpg);
	mwFO.addVariable("percenttobuffer", 5);
	mwFO.addVariable("autostart", true);
	mwFO.addParam("menu", "false");
	mwFO.addParam("wmode", "transparent");
	mwFO.write(idcontainer);
}

function get_baseurl() {
	var baseurl = $('script[src*=javascripts/]:first').attr('src');
	baseurl = baseurl.substring(0, baseurl.indexOf("javascripts/"));
	return baseurl;
}

$.fn.videoSlides = function(options) {
	var isMethodCall = (typeof options == "string") || false;
	var args = arguments;
	
	$(this).each(function() {
		var videoSlides = $(this).data("videoSlides");
		if (videoSlides && isMethodCall) {
			if (videoSlides[options] && $.isFunction(videoSlides[options])) {
				videoSlides[options].apply(videoSlides, $.makeArray(args).slice(1));
			}
		} else if ( !isMethodCall && !videoSlides ) {
			videoSlides = new $.videoSlides(this, options);
			$(this).data("videoSlides", videoSlides);
		}
	});
	
	return this;
};

$.videoSlides = function(elem, options) {
	this.elem = elem;
	this.options = $.extend( {}, $.videoSlides.defaults, options );
	this.init();
};

$.extend( $.videoSlides, {
	defaults: {
	},
	prototype: {
		init: function(){
			this.slides = $("ul li", this.elem);
			this.slides.css({display:"none", opacity:""});
			this.current = 0;
			this.slideTimer = 0;
			
			$(this.slides[0]).css({display:"block",opacity:""});
			
			
		},
		slideStart: function(){
			var toHide = this.slides[this.current];
			
			this.current ++;
			if (this.current > this.slides.length-1) {
				this.current = 0;
			}
			
			var toShow = this.slides[this.current];
			
			$(toHide).stop();
			$(toHide).animate({opacity:0},{queue:false, duration:750, complete:function(){
				$(this).hide().css({opacity:""});
			}});
			$(toShow).stop();
			$(toShow).css({opacity:0}).show().animate({opacity:1},{queue:false, duration:1000, complete:function(){
				$(this).show().css({opacity:""});
			}});
			
			var self = this;
			this.slideTimer = window.setTimeout(function(){
				self.slideStart();
			}, 7000);
		},
		slideStop: function(){
			if (this.slideTimer > 0) {
				window.clearTimeout(this.slideTimer);
			}
			this.slideTimer = 0;
		},
		slideReset: function() {
			if (this.slideTimer > 0) {
				window.clearTimeout(this.slideTimer);
			}
			this.slides.css({display:"none",opacity:""});
			this.current = 0;
			this.slideTimer = 0;
			
			$(this.slides[0]).css({display:"block",opacity:""});
			
			var self = this;
			this.slideTimer = window.setTimeout(function(){
				self.slideStart();
			}, 7000);
		}
	}
});

(function($){
	var video_list=[];
	var current_index = -1;
		
	$().ready(function() {	
		var baseurl = get_baseurl();
		var img = new Image();
		img.src = baseurl + "images/thumb-over.png";
		
		$(".video_slides").each(function(){
			$(this).videoSlides();
		});
		
		$(".videoListing ul li").each(function(){
			var a = $(this).find("a")[0];
			var href = a.hash || "";
			var rel = $(a).attr("rel");
			var rels = rel.split("|");
			if (rels.length > 3 && rels[1] != "") {
				var video_file = rels[1];
				var video_image = rels[2];
				var video_slides = rels[3] || "";
				if (video_slides != "" && $("#"+video_slides).length) {
					video_slides = $("#" + video_slides);
				} else {
					video_slides = undefined;
				}
				
				video_list.push({
					video_file: video_file,
					video_image: video_image,
					video_slides: video_slides,
					video_desc: ( $(href).length ? $(href) : undefined ),
					elem: this
				});
				
				$(this).wrapInner( $("<div></div>").addClass("inner").css({opacity:0.6}) );
				$(this).append($("<img src='" + img.src + "' class='hoverimg' />"));
				$(this).bind("mouseover", function(){
					if (!$(this).is(".current")) {
						$(this).addClass("hover");
						$(".inner", this).css({opacity:""});
						$(".hoverimg", this).hide();
						
						$($(this).find("a")[0]).trigger("showdetail");
					}
				});
				$(this).bind("mouseout", function(){
					if (!$(this).is(".current")) {
						$(this).removeClass("hover");
						$(".inner", this).css({opacity:0.6});
						$(".hoverimg", this).show();
					}
					
					$(".text_preview .video_caption").removeClass("current");
					$(".image_preview .video_slides").removeClass("current");
					
					if (video_list[current_index].video_desc) {
						$(video_list[current_index].video_desc).addClass("current");
					}
					if (video_list[current_index].video_slides) {
						$(video_list[current_index].video_slides).addClass("current");
					}
				});
				$(this).bind("click", function(){
					$($(this).find("a")[0]).trigger("click");
					return false;
				});
				
				$(a).bind("showdetail", function(){
					var li = $(this).parents("li")[0];
					var idx = 0;
					
					var c = $("li.current", $(this).parents("ul"));
					if ( c.length ) {
						idx = $("li", $(this).parents("ul")).index(c[0]);
						if (idx >= 0) {
							if (video_list[idx].video_desc) {
								$(video_list[idx].video_desc).removeClass("current");
								$(video_list[idx].video_slides).removeClass("current");
							}
							
							if (idx != current_index) {
								$(li).parents("ul").find("li.current")
									.find(".inner").css({opacity:0.6}).end()
									.find(".hoverimg").show().end()
									.removeClass("current");
							}
						}
					}
					
					idx = $("li", $(this).parents("ul")).index(li);
					
					$(".text_preview .video_caption").removeClass("current");
					$(".image_preview .video_slides").removeClass("current");
					
					if (video_list[idx].video_desc) {
						$(video_list[idx].video_desc).addClass("current");
					}
					if (video_list[idx].video_slides) {
						$(video_list[idx].video_slides).addClass("current");
					}
				});
				
				$(a).bind("click", function(){
					var li = $(this).parents("li")[0];
					
					if (!$(li).is(".current")) {
					
						if (current_index >= 0) {
							if (video_list[current_index].video_desc) {
								$(video_list[current_index].video_desc).removeClass("current");
							}
							if (video_list[current_index].video_slides) {
								$(video_list[current_index].video_slides).removeClass("current");
								$(video_list[current_index].video_slides).videoSlides("slideStop");
							}
						}
						
						$(li).parents("ul").find("li.current")
							.find(".inner").css({opacity:0.6}).end()
							.find(".hoverimg").show().end()
							.removeClass("current");
			
						$(li).addClass("current");
						
						var idx = $("li", $(this).parents("ul")).index(li);
						
						current_index = idx;
						
						if (video_list[current_index].video_desc) {
							$(video_list[current_index].video_desc).addClass("current");
						}
						if (video_list[current_index].video_slides) {
							$(video_list[current_index].video_slides).addClass("current");
							$(video_list[current_index].video_slides).videoSlides("slideReset");
						}
					}
					
					loadVideo(video_file, video_image, "video_player");
					
					if (!$(".nav a[href='#home']").parents("li").is(".current")) {
						$(".nav a[href='#home']").triggerHandler("click");
					}
						
					return false;
				});
				
				if ($(href).length) {
					$("a.play", href).bind("click", function(){
						loadVideo(video_file, video_image, "video_player");
						return false;
					});
				}
			}
		});
		
		$(".text_preview .video_caption").removeClass("current");
		
		if ($(".videoListing ul li a.current").length) {
			current_index = $(".videoListing ul li").index($($(".videoListing ul li a.current")[0]).parents("li")[0])
		} else {
			current_index = 0;
		}
		
		$(".videoListing ul li").removeClass("current");
		
		$($(".videoListing ul li a")[current_index]).parents("li").addClass("current");
		
		$($(".videoListing ul li a")[current_index]).parents("li")
			.find(".inner").css({opacity:""}).end()
			.find(".hoverimg").hide();
		
		$(".videoListing ul li a").removeClass("current");
		$($(".videoListing ul li a")[current_index]).addClass("current");
		$($(".text_preview .video_caption")[current_index]).addClass("current");
		$($(".image_preview .video_slides")[current_index]).addClass("current");
		
		$($(".image_preview .video_slides")[current_index]).videoSlides("slideReset");
		
		
		$("#video_nav a.prev").bind("click", function(){
			this.blur();
			
			if (current_index >= 0) {
				if (video_list[current_index].video_desc) {
					$(video_list[current_index].video_desc).removeClass("current");
				}
			}
			
			$(".videoListing ul").find("li.current")
				.find(".inner").css({opacity:0.6}).end()
				.find(".hoverimg").show().end()
				.removeClass("current");
			
			current_index --;
			if (current_index < 0) {
				current_index = video_list.length-1;
			}
			
			var li = video_list[current_index].elem;
			$(li).addClass("current").find(".inner").css({opacity:""}).end()
				.find(".hoverimg").hide();
			
			
			if (video_list[current_index].video_desc) {
				$(video_list[current_index].video_desc).addClass("current");
			}
			if (video_list[current_index].video_slides) {
				$(video_list[current_index].video_slides).addClass("current");
			}
			
			$(li).find("a").triggerHandler("click");
			
			return false;
		});
		
		$("#video_nav a.next").bind("click", function(){
			this.blur();
			
			if (current_index >= 0) {
				if (video_list[current_index].video_desc) {
					$(video_list[current_index].video_desc).removeClass("current");
				}
			}
			
			$(".videoListing ul").find("li.current")
				.find(".inner").css({opacity:0.6}).end()
				.find(".hoverimg").show().end()
				.removeClass("current");
			
			current_index ++;
			if (current_index > video_list.length-1) {
				current_index = 0;
			}
			
			var li = video_list[current_index].elem;
			$(li).addClass("current").find(".inner").css({opacity:""}).end()
				.find(".hoverimg").hide();
			
			if (video_list[current_index].video_desc) {
				$(video_list[current_index].video_desc).addClass("current");
			}
			if (video_list[current_index].video_slides) {
				$(video_list[current_index].video_slides).addClass("current");
			}
			
			$(li).find("a").triggerHandler("click");
			
			return false;
		});
		
		
		if ($.browser.msie && $.browser.version < 7) {
			$("img[src$=png], .rbox1 span.l, .rbox1 span.r").pngfix({sizingMethod:"scale"});
		}
	});
	
})(jQuery);

