(function() {
	var $ = jQuery.noConflict();
	$(function() {
	
		var playVideo = function() {
			if (flowplayer == null) {			  
				flowplayer = flashembed("player",  
				   {src : "/flowplayer/FlowPlayerLight.swf", bgcolor : "#6F7485", height: 390, width : 512},  
				   {config : playerConfig}
				);				 
			} else {
				flowplayer.setConfig(playerConfig);
			};
		};
		
		var setActiveBttn = function(obj, isFirst) {
			$("a", "#playlist").removeClass("activeVideo");
			if (isFirst) {
					$("a", "#playlist").first().addClass("activeVideo");
			} else {				
				$(obj).addClass("activeVideo");
			}
			// MSIE Fix
			if ( $.browser.msie ) { $(".videoNavBttns").css({"display" : "block", "float" : "left", "height" : "15px", "text-align" : "center", "width" : "10px"}); }
		};
		
		var setActiveInfoBox = function(rel, isFirst) {
			$(".videoInfo").hide();
			if (isFirst) {
				$(".videoInfo[rel='1']").fadeIn("slow");
			} else {
				$(".videoInfo[rel='" + rel + "']").fadeIn("slow");
			}
		};
		
		var playerConfig = {
			loop: false,
			useNativeFullScreen: true,
			autoBuffering: false,
			controlBarBackgroundColor: 0xffffff,
			menuItems: [true, true, true, true, true, false, false],
			autoPlay: true
		} 
		
		var flowplayer = null;
		var player = $("#player");
		var links = $("a", "#playlist");
		var playlist = $("#playlist");
		
		var isMSIE = $.browser.msie;
		
		// Get info
		var titles = [], descriptions = [], hrefs = [], // hrefs[] - IE
			i = 1;
		if ( !isMSIE ) {
			links.each(function(key, value) {
				i % 2 != 0 ? titles.push(value) : descriptions.push(value);
				i++;
			});
		} else { // M$ IE :@
			links.each(function() {
				var v = $(this).text();
				var h = $(this).attr("href");
				if ( i % 2 != 0 ) { titles.push(v); hrefs.push(h); } else { descriptions.push(v); }
				i++;
			});
		}
		
		// Remove HTML navigation
		playlist.html("");
		// Create the new one, dyniamic navigation, non-IE browsers
		if ( !isMSIE ) {
			i = 0;
			$(titles).each(function(key, value) {			
				var trigger = $("<a/>", {
					"rel" : i + 1,
					"href" : titles[i].href,
					"title" : titles[i].text.trim(),
					"class" : "videoNavBttns",
					text : i + 1
				});
				var info = $("<div/>", {
					"rel" : i + 1,
					"class" : "videoInfo",
					text : descriptions[i].text.trim()
				});				
				i++;				
				playlist.append(trigger).fadeIn("slow");
				playlist.after(info);
			});
		} else { // IE
			var c = titles.length;
			for (var i = 0; i < c; i++) {
				var trigger = '<a rel="' + (i + 1) + '" href="' + hrefs[i] + '" title="' + titles[i] + '" class="videoNavBttns">' + (i + 1) + '</a>';
				var info = '<div rel="' + (i + 1) + '" class="videoInfo">' + descriptions[i] + '</div>';
				playlist.append(trigger).fadeIn("slow");
				playlist.after(info);
			}
		}
		
		// Events handling	
		player.live("click", function(e) {			
			setActiveBttn(false, true);
			setActiveInfoBox(false, true);			
			playerConfig.videoFile = links.get(0).href;	
			playVideo();
			e.preventDefault();
		});		
		links.live("click", function(e) {
			setActiveBttn(this, false);
			setActiveInfoBox($(this).attr("rel"), false);			
			playerConfig.videoFile = $(this).attr("href");
			playVideo();
			e.preventDefault();
		});			

		// Autostart, the first one
		//~ $("#player").trigger("click");
		
		// Creating the Nivo Slider
		var slideshow = $('#pictures');		
		if ( slideshow.length ) {
			$('#pictures').nivoSlider({
				effect: 'fade',
				pauseTime: 3000,
				directionNav: false,
				controlNav: false	
			});
		}

	});
})(jQuery);


 
