$(function(){
	$('.update-button').click(function(){
		_gaq.push(['_trackEvent', 'Ajax', 'rndQuestionUpdate']);
		var question = $('.question-text .q-text');
		$.get('/widgets/api/questions?' + Math.random(), null, function(data){
			var title = $(data).find('item:eq(0) > title');
			var questionLink = $(data).find('item:eq(0) > href').text();
			
			if(questionLink) {
				question.replaceWith('<a class="q-text" href="'+questionLink+'">'+title.text()+'</a>');
			} else {
				question.replaceWith('<span class="q-text">' + title.text() + '</span>');
			}
		}, 'xml');
	});
	
	var dialog = $("#dialog");
	
	dialog.dialog({
		autoOpen: false,
		height: 560,
		//height: arr[2] + 40,
		modal: true,
		open: function(event, ui) {
			$('.ui-dialog').css('overflow', 'visible');
		},
		width: 682
		//width: arr[1] + 60
	});
	
	var oFlashMovie;
	
	$('.preview').click(function(){
		var arr = $(this).find('.video-data').text().split('|');
		
		var title = $(this.parentNode).find('.with-preview h3').text();
		_gaq.push(['_trackEvent', 'Video', 'Play', title]);
		
		//var flashvars = {};
		var flashvars = {
			file: htdocs + arr[0],
			image: htdocs + arr[4],			
			title: title,
			stretching: 'none',
			menu:false
		}
		 
		var params = {
			allowscriptaccess: 'always',
			wmode: 'opaque'
		}; 
		var attributes = {};
		
		var width = parseInt(arr[1]),
		    height = parseInt(arr[2]);

        swfobject.embedSWF(htdocs + "/swf/player.swf", "movie", width, height, "8.0.0", "/js/expressInstall.swf", flashvars, params, attributes);
		//swfobject.embedSWF(htdocs + "/swf/VideoPlayer.swf", "movie", width, height, "8.0.0", "/js/expressInstall.swf", flashvars, params, attributes);
		//swfobject.embedSWF('http://yapc.tv/swf/player.swf', 'movie', arr[1], arr[2], '9.0.0', htdocs + '/js/expressInstall.swf', flashvars, params, attributes);

		oFlashMovie = document.getElementById('movie');
		
		dialog.dialog('option', 'width', width + 60);
		dialog.dialog('option', 'height', height + 60);
		dialog.dialog('open');
	});

/*	
	$('.site-tour').click(function(){
		var arr = $(this).attr('rel').split('|');
		var flashvars = {}; 
		var params = {
			wmode: 'opaque'
		};
		var attributes = {}; 
		flashvars.sTitle = sTitle;
		flashvars.sURL = sURL;
		params.allowscriptaccess = "always";
		swfobject.embedSWF(htdocs + "/swf/VideoPlayer.swf", "movie", 620, 490, "8.0.0", htdocs + "/js/expressInstall.swf", flashvars, params, attributes);
		oFlashMovie = document.getElementById('movie');
		dialog.dialog('open');
	});
*/	
	var wCont = $('.widgets-container');
	var wIn = $('.widgets-inner');
	
	if (wCont.length) {
		var wParams = {
			allowScriptAccess: 'always',
			quality: 'high',
			wmode: 'transparent'
		};
		
		var slogan = '<span class="widget-slogan"><img alt="WhoYOUgle" src="' + htdocs + '/i/index/whoyougle-s.png" />' + widgetLocale.slogan + '</span>';
		
		var position = [];
		
		function getPosition(pos) {
			var result;
			for(var i in position) {
				if(typeof position[i] != 'undefined') {
					if(typeof result != 'undefined') {
						if(pos == 'min') {
							result = Math.min(result, position[i]);
						}
						if(pos == 'max') {
							result = Math.max(result, position[i]);
						}
					} else {
						result = position[i];
					}
				}
			}
			return result;
		}
		
		function appendWidget(i, prepend) {			
			addedWidgets++;
			animation = addedWidgets;
			if (prepend) {
				position[i] = -widgetWidth;
				wCont.prepend('<div id="w' + i + '" class="widget-container"><div class="widget-inner"><div class="widget" id="widget' + i + '"></div></div></div>');
			} else {
				var num = $('.widget-container').length;
				position[i] = num * widgetWidth;
				wCont.append('<div id="w' + i + '" class="widget-container" style="left: ' + position[i] + 'px"><div class="widget-inner"><div class="widget" id="widget' + i + '"></div></div></div>');
			}
			var flashvars = null;
			swfobject.embedSWF('http://' + widgetsDomain + '/2.0/' + widgetsArr[i].name + '.swf', 'widget' + i, 240, 300, '9.0.0', htdocs + '/js/expressInstall.swf', flashvars, wParams, null);
			
			$('#w' + i).hover(function(){
				widgetIn(this);
			}, function(){
				widgetOut();
			});			
		}
		
		var wContWidth;
		var widgetWidth = 1 + 20 + 240 + 20 + 1;
		var visibleCount;
		var widgetsCount = widgetsArr.length;
		var addedWidgets = 0;
		
		var startIndex = 0;
		
		function prevWidget(){
			if(animation < addedWidgets) return;
			
			animation = 0;
			var oldSIndex = startIndex;
			
			startIndex--;
			if(startIndex < 0) startIndex = widgetsCount - 1;
			var index = startIndex;
			
			var widget = $('#w' + index);
			
			if (widget.length == 0) {
				appendWidget(index, true);
			}
			
			setWidPos(oldSIndex);
			
			$('.widget-container').each(function(){
				var $this = $(this);
				$(this).animate({
					left: $this.position().left + widgetWidth
				}, 100, 'linear', function(){
					animation++;
				});				
			});
		}
		
		function nextWidget() {
			if(animation < addedWidgets) return;
			
			animation = 0;
			var oldSIndex = startIndex;
			
			startIndex++;
			if(startIndex >= widgetsCount) startIndex = 0;
			var index = (startIndex + visibleCount - 1) % widgetsCount;
			
			var widget = $('#w' + index);
			
			if (widget.length == 0) {
				appendWidget(index);
			}
			
			setWidPos(oldSIndex, 'next');
			
			$('.widget-container').each(function(){
				var $this = $(this);
				$this.animate({
					left: $this.position().left - widgetWidth
				}, 100, 'linear', function(){
					animation++;
					if(animation == addedWidgets) {
						var leftPos = widgetWidth;
						var id = -1;
						
						$('.widget-container').each(function(){
							if($(this).position().left < leftPos) {
								leftPos = $(this).position().left;
								id = this.id;
							}
							
						});
						
						if(id != -1) {
							$('#' + id).css({
								left: (addedWidgets - 1) * widgetWidth
							});
						}
					}
				});				
			});
		}
		
		function setWidPos(index, direction) {
			var j = index;
			for(var i = 0; i < widgetsCount; i++) {
				var curIndex = (j + i) % widgetsCount;
				
				var widget = $('#w' + curIndex);
				if(widget.length) {
					if (i == widgetsCount - 1 && direction != 'next') {
						widget.css({
							left: -widgetWidth
						})
					} else {
						widget.css({
							left: i * widgetWidth
						});
					}
				}
			}
		}
		
		function calcWidth() {
			wCont.width('auto');
			wContWidth = wCont.width();
			visibleCount = Math.floor(wContWidth / widgetWidth);
			if(visibleCount == 0) visibleCount = 1;
			
			if(visibleCount > widgetsCount) {
				visibleCount = widgetsCount;
			}
			
			wCont.width(widgetWidth * visibleCount);
			
			if(visibleCount < widgetsCount) {
				$('.w-lar, .w-rar').show();
			} else {
				$('.w-lar, .w-rar').hide();
			}
		}
		
		calcWidth();
		
		wCont.empty();
		for(var i = 0; i < visibleCount; i++) {
			appendWidget(i);
		}
		var animation = addedWidgets;
		
		$('.w-lar').click(function(){
			prevWidget();
		});
		
		$('.w-rar').click(function(){
			nextWidget();
		});
		
		$(window).resize(function(){
			var oldVisible = visibleCount;
			calcWidth();
			var num = $('.widget-container').length;
			if(oldVisible < visibleCount && num < visibleCount) {
				for(var i = 0; i < visibleCount; i++) {
					var index = (startIndex + i) % widgetsCount;
					
					var widget = $('#w' + index);
					if (widget.length == 0) {
						appendWidget(index);
					}
				}
			}
		});
		
		var bubbleHover = false;
		
		var insertBubble = $('.insert-widget').hover(function(){
			bubbleHover = true;
		}, function() {
			bubbleHover = false;
			setTimeout(hideWidgetBubble, 100);
		});
		
		var bubbleLink = insertBubble.find('a');
		
		function widgetIn(widget) {
			bubbleHover = true;
			var $this = $(widget);
			var index = parseInt(widget.id.replace('w', ''));
			bubbleLink.attr('href', widgetsArr[index].href);
			var leftMargin = parseInt($this.parent().css('margin-left').replace('auto', '0').replace('px', ''));
			insertBubble.show().css({
				left: $this.position().left + $this.parent().position().left + 110 + leftMargin
			});
		}
		
		function widgetOut() {
			bubbleHover = false;
			setTimeout(hideWidgetBubble, 100);
		}
		
		
		function hideWidgetBubble() {
			if (!bubbleHover) {
				insertBubble.hide();
			}
		}
	}

	
	
	
	$(document).ready( function(){
		$(".preview-block a").each( function()
				{
			$( this ).mouseover( function()
					{
						$( this ).parents("li").find("h3 a").addClass("hovered");
					})
					.mouseout( function()
					{
						$( this ).parents("li").find("h3 a").removeClass("hovered");
					});
				})
	});
	
});
