if (typeof $ !== 'undefined')
{
	var vidoq = {
		menuPageBarAnimDuration: 1000,
		menuLoadSubAnimDuration: 1000,
		menuLoadSubFadeDuration: 1000,

		menuPageBarStepIE: function(now, fx)
		{
			if(now == 0)
			{
				$(fx.elem).css({position: '', 'background-image': 'none'});
				$('span.vidoq-active', fx.elem).css({
					position: '',
					height: '',
					width: '',
					top: '',
					left: ''
				});
			}
			else if(now == 100)
			{
				$(fx.elem).css({position: '', 'background-image': ''});
				$('span.vidoq-active', fx.elem).css({
					position: '',
					height: '',
					width: '',
					top: '',
					left: ''
				});
			}
			else
			{
				$(fx.elem).css({position: 'relative', 'background-image': 'none'});
				$('span.vidoq-active', fx.elem).css({
					position: 'absolute',
					height: '100%',
					top: '0px',
					width: now + '%',
					left: ((100 - now) / 2) + '%'
				});
			}
		},

		menuPageBarStepCSS: function(now, fx)
		{
			$(fx.elem).css({
				'background-repeat': 'no-repeat',
				'background-size': now + '% 3px'
			});
		},
		
		menuPageBarStep: function () {},
		
		menuPageLoad: function (completion)
		{
			var activePage = $('#vidoq-header a.vidoq-active');
			
			if(activePage.length < 1)
			{
				completion();
				return;
			}

			activePage.prop({vidoqBarSize: 0}).animate({vidoqBarSize: 100}, {
				duration: vidoq.menuPageBarAnimDuration,
				step: vidoq.menuPageBarStep,
				complete: completion
			});
		},
		
		menuPageUnload: function(completion)
		{
			var activePage = $('#vidoq-header a.vidoq-active');

			if(activePage.length < 1)
			{
				completion();
				return;
			}

			activePage.prop({vidoqBarSize: 100}).animate({vidoqBarSize: 0}, {
				duration: vidoq.menuPageBarAnimDuration,
				step: vidoq.menuPageBarStep,
				complete: completion
			});
		},
		
		menuLoad: function (completion)
		{
			var activePage = $('#vidoq-header a.vidoq-active');
			var subMenu;
			var parentMenu = false;
			
			if(activePage.parents('ul').length == 2)
				subMenu = activePage.parent().parent(); 
			else if (activePage.parent().next().is('ul'))
			{
				subMenu = activePage.parent().next();
				parentMenu = true;
			}
			else
			{
				vidoq.menuPageLoad(completion);
				return;
			}
			
			if(subMenu.is(':visible'))
			{
				vidoq.menuPageLoad(completion);
				return;
			}
			else if(parentMenu)
				vidoq.menuPageLoad();
				
			var w = $(subMenu).prop('vidoqMenuWidth');
			if(w <= $(subMenu).width())
				w = 'toggle';
			
			$(subMenu).animate({width: w}, vidoq.menuLoadSubAnimDuration, function () {
				$(subMenu).css({width: ''});
				$('li', subMenu).vidoqFadeIn(vidoq.menuLoadSubFadeDuration);
				if(!parentMenu)
				{
					$('li', subMenu).first().queue(function () {
						vidoq.menuPageLoad(completion);
						$(this).dequeue();
					});
				}
				else
				{
					$('li', subMenu).first().queue(function () {
						completion();
						$(this).dequeue();
					});
				}
			});
		},
		
		menuUnload: function (page, completion)
		{
			var activePage = $('#vidoq-header a.vidoq-active');
			var subMenu = null;
			var parentMenu = false;
			
			if(activePage.parents('ul').length == 2)
				subMenu = activePage.parent().parent(); 
			else if (activePage.parent().next().is('ul'))
			{
				subMenu = activePage.parent().next();
				parentMenu = true;
			}
			else
			{
				vidoq.menuPageUnload(completion);
				return;
			}
			
			var newPage = $('#vidoq-header a').filter(function () { return page.indexOf(this.href) == 0; });
			var newMenu = null;
			
			if(newPage.parents('ul').length == 2)
				newMenu = newPage.parent().parent(); 
			else if (newPage.parent().next().is('ul'))
				newMenu = newPage.parent().next();
				
			if(newMenu != null && newMenu.get(0) === subMenu.get(0))
			{
				vidoq.menuPageUnload(completion);
				return;
			}

			var func = function () {
				$('li', subMenu).vidoqFadeOut(vidoq.menuLoadSubFadeDuration);
				$($('li', subMenu).get(0)).queue(function () {
					$(this).dequeue();
					$(subMenu).animate({width: 'toggle'}, vidoq.menuLoadSubAnimDuration, completion);
				});
			};
			
			if(!parentMenu)
				vidoq.menuPageUnload(func);
			else
			{
				vidoq.menuPageUnload(function () {});
				func();
			}
		},

		galleryFadeDuration: 1000,
		galleryChangeDelay: 8000,
		galleryChangeActive: false,
		
		galleryChangeSetup: function(target) {
			var galleryData = $.parseJSON($(target).parents('.vidoq-image').contents().filter(function () { return this.nodeType == 8; }).get(0).data);

			for(var i = 0; i < galleryData.length; i++)
			{
				if(target.src.indexOf(galleryData[i]['thumb']) == target.src.length - galleryData[i]['thumb'].length)
					break;
			}
			
			$(target).prop({
				vidoqGalleryData: galleryData,
				vidoqGalleryIndex: i
			});

			var pt = $(target).parent().parent();

			var ctrl = $('.vidoq-controls', pt);

			var social_fb = $('.vidoq-social-fb', ctrl);
			$(social_fb).empty();
			
			$('a.vidoq-control-left,a.vidoq-control-right', ctrl).each(function (i, elem) { $(elem).attr({href: '#'}); });
			
			if(galleryData.length > 1)
			{
				$('a.vidoq-control-left,a.vidoq-control-right', ctrl).click(function (e) {
					if(vidoq.galleryChangeActive)
					{
						if($(target).next().is('iframe'))
							$(target).next().stop(false, true);
						else
							$(target).stop(false, true);
						e.preventDefault();
						return;
					}

					var timer = $(target).prop('vidoqGalleryTimer');
					
					if(timer != null)
						clearTimeout(timer);

					$(target).prop({vidoqGalleryTimer: null});
					
					var i = $(target).prop('vidoqGalleryIndex');
					var data = $(target).prop('vidoqGalleryData');

					if($(this).hasClass('vidoq-control-left'))
					{
						i = (i + data.length - 2) % data.length;
						
						$(target).prop({vidoqGalleryIndex: i});
					}
					else
						i = (i + 1) % data.length;
						
					if($(target).next().is('iframe'))
					{
						var social_fb = $('.vidoq-social-fb', $(target).parent().parent());
						$(social_fb).empty();

						vidoq.galleryChangeActive = true;
						$(target).next().vidoqFadeOut(vidoq.galleryFadeDuration, 'linear', function () {
							$(this).remove();

							$(target).show();
							$(target).prop({
								vidoqGalleryIndex: i,
								src: data[i]['thumb']
							});
						});
					}
					else
						vidoq.galleryChange(target);
					
					e.preventDefault();
				});
			}
			
			if($(target).prev().is('a'))
				$(target).prev().remove();
			
			vidoq.galleryChangeActive = true;
				
			$(target).bind('load.vidoq-galleryChangeSetup', function () {
				if($(target).css('visibility') != 'hidden')
					return;
					
				vidoq.galleryChange(target);
			});
			if(target.complete)
				$(target).trigger('load.vidoq-galleryChangeSetup');
		},
		
		galleryChange: function (target) {
			if($(target).css('visibility') == 'hidden')
			{
				$(target).vidoqFadeIn(vidoq.galleryFadeDuration, 'linear', function () {
				
					var i = $(target).prop('vidoqGalleryIndex');
					var data = $(target).prop('vidoqGalleryData');
					
					var social_fb = $('.vidoq-social-fb', $(target).parent().parent());
					$(social_fb).empty();

					var wnd = ('' + window.location);
					var fq = wnd.substring(0, wnd.length - window.location.pathname.length);
					
					$(social_fb).append('<iframe src="http://www.facebook.com/plugins/like.php?href=' + fq + data[i]['gallery_link'] + '&layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');

					if(data[i]['link'].length > 0)
					{
						$(target).before('<a href="' + data[i]['link'] + '"><div class="vidoq-movie vidoq-gallery"></div></a>');

						$(target).prev().click(function (e) {
							$(target).prev().remove();
							$(target).vidoqFadeOut(vidoq.galleryFadeDuration, 'linear', function () {
								var timer = $(target).prop('vidoqGalleryTimer');
								
								if(timer != null)
									clearTimeout(timer);

								$(target).prop({vidoqGalleryTimer: null});
								
								$(target).hide().after('<iframe frameborder="0" src="" class="vidoq-gallery"></iframe>');
								$(target).next().vidoqFadeOut(0).bind('load.vidoq-galleryChange', function () {
									$(this).vidoqFadeIn(vidoq.galleryFadeDuration, 'linear');
								}).prop({
									src: data[i]['link']
								});
							});
							e.preventDefault();
						});
					}

					vidoq.galleryChangeActive = false;
				
					$(target).prop({vidoqGalleryTimer: setTimeout(function () {
						$(target).prop({vidoqGalleryTimer: null});
						
						vidoq.galleryChange(target);
					}, vidoq.galleryChangeDelay)});
				});
			}
			else
			{
				var data = $(target).prop('vidoqGalleryData');
				
				if(data.length > 1)
				{
					vidoq.galleryChangeActive = true;
				
					if($(target).prev().is('a'))
					{
						$(target).prev().remove();
					}

					var social_fb = $('.vidoq-social-fb', $(target).parent().parent());
					$(social_fb).empty();
					
					$(target).vidoqFadeOut(vidoq.galleryFadeDuration, 'linear', function () {
						var i = $(target).prop('vidoqGalleryIndex');
						var j = (i + 1) % data.length;
						
						$(target).prop({
							vidoqGalleryIndex: j,
							src: data[j]['thumb']
						});
					});
				}
			}
		},
		
		artistAnimDuration: 500,
		artistScaleFactor: 0.8,
		
		artistSetup: function (target) {
			$('.vidoq-artist:not(.vidoq-artist-active)', target).click(function (e) {
				var link = $('h1 a', this);
				var doc = $(link).attr('href');
				
					if($.browser.msie)
						$(link).get(0).click();
					else
						window.location = doc;
			});
			
			var pane = $('.vidoq-artist-scroll .vidoq-scroll-pane');
			var panels = $('.vidoq-scroll-artist', pane);
			
			if(panels.length < 2)
				return;
			
			pane.prepend('<div class="vidoq-scroll-artist" />');
			pane.append('<div class="vidoq-scroll-artist" />');

			var panels = $('.vidoq-scroll-artist', pane);

			panels.wrap('<div class="vidoq-artist-pane" />')

			var pos = Array();
			
			pane.scrollLeft(0);
			
			for(var i = 0; i < panels.length; i++)
				pos[i] = $(panels[i]).parent().position().left;
			
			var i = 1;

			pane.scrollLeft($(panels[i]).position().left + pane.scrollLeft());
			
			pane.parent().prepend('<div class="vidoq-scroll-left"><a href="#"><div></div></a></div>').append('<div class="vidoq-scroll-right"><a href="#"><div></div></a></div>');
			
			var setCtrl = function () {
				$('.vidoq-scroll-left div,.vidoq-scroll-right div', pane.parent()).removeClass('vidoq-disabled');
				
				if(i == 0)
					$('.vidoq-scroll-left div', pane.parent()).addClass('vidoq-disabled');
				else if(i >= panels.length - 3)
					$('.vidoq-scroll-right div', pane.parent()).addClass('vidoq-disabled');
			};
			
			setCtrl();
			
			for(var j = 0; j < panels.length; j++)
				if(j != i + 1)
					vidoq.artistScale(panels[j], vidoq.artistScaleFactor, (j > i) ? 'left' : 'right');
			
			$('.vidoq-scroll-left a,.vidoq-scroll-right a', pane.parent()).click(function (e) {
				e.preventDefault();
				
				if($('div', this).hasClass('vidoq-disabled'))
					return;
					
				var j;
				
				if($(this).parent().hasClass('vidoq-scroll-left'))
				{
					j = -1;
					i = i + j;
					
					if(i < 0)
					{
						i = 0;
					}
				}
				else
				{
					j = 1;
					i = i + j;
					
					if(i > panels.length - 3)
					{
						i = panels.length - 3;
					}
				}
					
				pane.queue((function(i,j) { return function () {				
				if(j < 0)
				{
					$(panels[i + 1]).prop({vidoqArtistScale: vidoq.artistScaleFactor}).animate({vidoqArtistScale: 1}, {duration: vidoq.artistAnimDuration, easing: 'linear', step: function (now, fx) {
						vidoq.artistScale(fx.elem, now, 'right');
					}});
					$(panels[i + 2]).prop({vidoqArtistScale: 1}).animate({vidoqArtistScale: vidoq.artistScaleFactor}, {duration: vidoq.artistAnimDuration, easing: 'linear', step: function (now, fx) {
						vidoq.artistScale(fx.elem, now, 'left');
					}});
				}
				else
				{
					$(panels[i + 1]).prop({vidoqArtistScale: vidoq.artistScaleFactor}).animate({vidoqArtistScale: 1}, {duration: vidoq.artistAnimDuration, easing: 'linear', step: function (now, fx) {
						vidoq.artistScale(fx.elem, now, 'left');
					}});
					$(panels[i]).prop({vidoqArtistScale: 1}).animate({vidoqArtistScale: vidoq.artistScaleFactor}, {duration: vidoq.artistAnimDuration, easing: 'linear', step: function (now, fx) {
						vidoq.artistScale(fx.elem, now, 'right');
					}});
				}
				
				$(this).dequeue();
				}})(i,j));
				pane.animate({scrollLeft: pos[i]}, vidoq.artistAnimDuration, 'linear');
				setCtrl();
			});
		},
		
		artistScale: function() {},
		
		artistScaleCSS: function(target, percent, side) {
			if(percent == 1)
			{
				target.style[vidoq.transformPrefix] = '';
				target.style[vidoq.transformPrefix + 'Origin'] = '';
			}
			else
			{
				target.style[vidoq.transformPrefix] = 'scale(' + percent + ')';
				target.style[vidoq.transformPrefix + 'Origin'] = side + ' center';
			}
		},
		
		artistScaleIE: function(target, percent, side) {
			if(percent == 1)
			{
				target.style.filter = '';
			}
			else
			{
				var x = 0;
				var y = 0;

				var h = $(target).height();
				y = (h - (h * percent)) / 2;
				
				if(side == 'right')
				{
					var w = $(target).width();
					x = (w - (w * percent));
				}
				
				target.style.filter = 'progid:DXImageTransform.Microsoft.Matrix(M11=' + percent + ',M12=0,M21=0,M22=' + percent + ',dX=' + x + ',dY=' + y + ')';
			}
		},
		
		artistGalleryFadeDuration: 125,
		artistGalleryAnimDuration: 250,
		
		artistGallerySetup: function (target) {
			$('.vidoq-artist-thumbnail', target).wrapAll('<div class="vidoq-thumbnail-list">');
			$(target).prepend('<div class="vidoq-image vidoq-artist-large"><a href="#"><img src=""></a></div>');
			
			var large = $('.vidoq-artist-large', target);
			var inanim = false;
			var fb_link;
			
			$('img', large).bind('load.vidoq-artistGallerySetup', function () {
				$(this).vidoqFadeIn(vidoq.artistGalleryFadeDuration, 'linear', function () {
					$('.vidoq-social-fb', $(target).parent()).append('<iframe src="http://www.facebook.com/plugins/like.php?href=' + fb_link + '&layout=button_count" scrolling="no" frameborder="0" allowtransparency="true"></iframe>');
					
					inanim = false;
				});
			}).vidoqFadeOut(0);

			large.css({
				position: 'absolute',
				width: 187,
				height: 117
			}).hide().click(function (e) {
				e.preventDefault();

				if(inanim)
					return;
				inanim = true;
				
				large.css({'z-index': ''});
				$('.vidoq-social-fb', $(target).parent()).empty();
				
				$('img', large).vidoqFadeOut(vidoq.artistGalleryFadeDuration, 'linear', function () {
					large.animate({
						left: large.prop('vidoqPos').left,
						top: large.prop('vidoqPos').top,
						width: 187,
						height: 117
					}, vidoq.artistGalleryAnimDuration, 'linear', function () {
						$('.vidoq-artist-thumbnail', target).vidoqFadeIn(vidoq.artistGalleryFadeDuration, 'linear').first().queue(function() {
							large.hide();

							inanim = false;
							$(this).dequeue();
						});
					});
				});
			});
			
			$(target).parent().append('<div class="vidoq-controls"><div class="vidoq-social-fb">');
			
			$('.vidoq-thumbnail-list a', target).click(function (e) {
				e.preventDefault();
				
				if(inanim)
					return;
				inanim = true;
				
				fb_link = this.href;
				
				var img_link = $('img', this).attr('vidoqlargesrc');
				
				large.prop({vidoqPos: $(this).parent().offset()});

				large.css({
					left: large.prop('vidoqPos').left,
					top: large.prop('vidoqPos').top
				}).show();
				$('.vidoq-artist-thumbnail', target).vidoqFadeOut(vidoq.artistGalleryFadeDuration, 'linear').first().queue(function () {
					large.css({'z-index': 1});
					
					large.animate({
						left: $(target).offset().left,
						top: $(target).offset().top,
						width: 832,
						height: 554
					}, vidoq.artistGalleryAnimDuration, 'linear', function () {
						$('img', large).prop({src: img_link}); 
					});
					
					$(this).dequeue();
				});
			});
		},
		
		docImageFadeDuration: 1000,
		
		docIsMenuLink: function(target) {
			var retval = false;
			
			$('#vidoq-header a:not(.vidoq-active)').each(function(i, elem) {
				if(!retval && elem.href.indexOf(target) == 0)
					retval = true;
			});
			
			return retval;
		},
		
		docFix: function(target) {
			if($('#vidoq-header a.vidoq-active').length > 0 && document.referrer.indexOf($('#vidoq-header a.vidoq-active').get(0).href) != 0)
				vidoq.menuLoad(function () {});

			$('img[vidoqanim]', target).vidoqFadeOut(0).each(function (i, elem) {
				vidoq.galleryChangeSetup(elem);
			});

			$('.vidoq-image iframe,.vidoq-image img:not([vidoqanim])', target).vidoqFadeOut(0).bind('load.vidoq-docFix', function () {
					$(this).unbind('load.vidoq-docFix');
					$(this).vidoqFadeIn(vidoq.docImageFadeDuration);
			}).each(function(i, elem) {
				if('complete' in elem && elem.complete)
					$(elem).trigger('load.vidoq-docFix');
			});
			
			$('a', target).filter(function() { return vidoq.docIsMenuLink(this.href); }).bind('click.vidoq-docFix', function () {
				$('a', target).unbind('click.vidoq-docFix');

				var link = this;

				vidoq.menuUnload(link.href, function () {
					if($.browser.msie)
						link.click();
					else
						window.location = link.href;
				});
				
				return false;
			});
			
			vidoq.artistSetup(target);
			
			$('.vidoq-artist-thumbnails', target).each(function (i, elem) { vidoq.artistGallerySetup(elem); });
		},
		
		transformPrefix: '',
		
		domSupport: function (propList) {
			var testElem = $('body').get(0);
			
			for (var i = 0; i < propList.length; i++)
				if(propList[i] in testElem.style)
					return propList[i];
			
			return false;
		},

		domReady: function () {
			$('#vidoq-header > ul > ul').each(function (i, elem) {
				$(elem).prop({vidoqMenuWidth: $(elem).width()});

				if($('a', elem).add($('a', $(elem).prev())).filter(function () { return document.referrer.indexOf(this.href) == 0; }).length > 0 &&
					$('a', elem).add($('a', $(elem).prev())).filter('.vidoq-active').length > 0)
					return;

				$('li', elem).vidoqFadeOut(0);
				$(elem).animate({width: 'toggle'}, 0);
			});
			
			var barEnd = $('#vidoq-header a.vidoq-active').length > 0 ? (document.referrer.indexOf($('#vidoq-header a.vidoq-active').get(0).href) == 0) : false;
			
			if(vidoq.domSupport('backgroundSize'))
			{
				vidoq.menuPageBarStep = vidoq.menuPageBarStepCSS;
				$('#vidoq-header a').each(function (i, elem) {
					vidoq.menuPageBarStep((barEnd && $(elem).hasClass('vidoq-active')) ? 100 : 0, {elem: elem});
				});
			}
			else
			{
				vidoq.menuPageBarStep = vidoq.menuPageBarStepIE;
				$('#vidoq-header a').each(function (i, elem) {
					$(elem).prepend('<span class="vidoq-active"></span>');
					vidoq.menuPageBarStep((barEnd && $(elem).hasClass('vidoq-active')) ? 100 : 0, {elem: elem});
				});
			}

			if((vidoq.transformPrefix = vidoq.domSupport(['webkitTransform', 'MozTransform', 'OTransform', 'msTransform', 'transform'])))
				vidoq.artistScale = vidoq.artistScaleCSS;
			else if(vidoq.domSupport(['filter']))
				vidoq.artistScale = vidoq.artistScaleIE;

			vidoq.docFix(document);
			$('html').css({visibility: ''});
		}
	};
	
	(function ($) {
		$.fn.vidoqFadeIn = function(speed, easing, callback) {
			return this.css({visibility: '', opacity: 0}).animate({opacity: 1}, speed, easing, function() {
				$(this).css({opacity: ''});
				$(this).css({filter: '', '-ms-filter': ''});
				if($.isFunction(callback))
					callback.call(this);
			});
		};
		$.fn.vidoqFadeOut = function (speed, easing, callback) {
			return this.css({opacity: 1}).animate({opacity: 0}, speed, easing, function() {
				$(this).css({visibility: 'hidden', opacity: ''});
				$(this).css({filter: '', '-ms-filter': ''});
				if($.isFunction(callback))
					callback.call(this);
			});
		};
	})(jQuery);

	$('html').css({visibility: 'hidden'});
	$(document).ready(vidoq.domReady);
}
