
// Cufon font replacement
Cufon.replace('#left h1, #right .box h3')
     .replace('#nav .nav a, .nltr .butn a, .links a', { hover: true })
     .replace('.gallery .nav a', { hover: true });


jQuery.fn.reverse = function() {
    return this.pushStack(this.get().reverse(), arguments);
};


// DOM READY
$(function(){



	// Fancybox links
	$('a[rel=fancybox]').live('click', function(e){
		var url = $(this).attr('href');
		$.fancybox({
			type: 'ajax',
			href: url,
			padding: 0,
			autoScale: false,
			overlayOpacity: 0.5,
			overlayColor: '#000',
			showCloseButton: false
		});
		e.preventDefault();
	});



	// Galleries
	$('.gallery').gallery({
		nav: '.nav a'
	});



	// Left column ajaxlist
	$('.list.ajax').ajaxList({
		speed: 400
	});



	// Home lists
	// Load new polls when other video is selected
	$('#homevids .link a').click(function(e){
		$link = $(this);
		$('#homepolls').slideUp(300, function(){
			$(this).load( $link.attr('rel'), function(){
				$(this).ajaxList({ speed: 400 })
				$(this).slideDown(300);
			});
		});
	});



	// Cycle Plugin:
	// Right column image slider
	$('.imgslider ul').cycle({
		fx: 'scrollHorz',
		speed: 500,
		timeout: 6000,
		pause: 1,
		random: 1,
		prev: '.imgslider .prev a',
		next: '.imgslider .next a'
	});



	// Right column person slider
	$('.persons').personSlider({
		speed: 500,
		timeout: 6000,
		showAll: '.personsShowAll',
		showAllContext: '.box',
		showLoop: '.personsLoop',
		showLoopContext: '.box'
	});



	// Submit poll through ajax
	$('.ajaxpoll').live('submit', function(e){
		var $form = $(this),
		    $load = $form.parents('div.load'),
		    $li = $form.parents('li.active');
		
		$load.load($form.attr('action'), $form.serialize());
		$li.animate({ height: $load.height() + $li.find('.link').height() + 'px' }, 250);
		e.preventDefault();
	});



	// Submit reply form through ajax
	$('.fancy form.reply').live('submit', function(e){
		var $form = $(this);
		$form.parents('.fancy').parent()
			.load($form.attr('action'), $form.serializeArray());
		e.preventDefault();
	});



	// Submit form when selectbox item is chosen
	$('.submitonchange').change(function(){
		$(this).parents('form').submit();
	});



	// Nice select boxes
	$('.niceselect select').niceSelect();



	// Tagcloud
	$('.tagcloud').tagcloud();



});
// END DOM READY



// Ajax list
$.fn.ajaxList = function(opts){

	var speed = opts.speed;

	$('.list')
		.find('> li:first').addClass('first').end()
		.find('> li:last').addClass('last');
	$('.box').each(function(){
		if ($(this).find('.list').length > 1) {
			$(this).find('.list:last').css('margin-bottom', '50px');
		}
	});

	this.each(function(){

		var $ul = $(this),
		    $anchors = $ul.find('> li > .link a');

		$ul.find('.load').hide();

		$active = $ul.find('> li.active');
		$active.css('height', 'auto')
			.find('.load').show().end()
			.next('li').addClass('afteractive');

		if (!$ul.hasClass('showlink')) {
			$active.find('.link').hide();
		}

		$(window).load(function(){
			$active.height($(this).find('.load').outerHeight());
		});

		$anchors.click(function(e){

			e.preventDefault();

			var $li = $(this).parents('li'),
			    $old = $ul.find('> li.active');

			if ($ul.hasClass('showlink')) {

				if ($li.hasClass('active')) {

					$li.removeClass('active')
						.animate({ height: '40px' }, speed)
						.find('.load').fadeOut(speed, function(){
							$ul.find('.afteractive').removeClass('afteractive');
							$(this).html('');
						});
					return;

				} else {

					$old.removeClass('active')
						.animate({ height: '40px' }, speed)
						.find('.load').fadeOut(speed, function(){
							$(this).html('');
						});

					$ul.find('.afteractive').removeClass('afteractive');

					$li.addClass('active')
						.find('.load').load($li.find('.link a.item').attr('href'), function(){
							$li.animate({ height: $li.find('.load').outerHeight() + 40 + 'px' }, speed)
								.next('li').addClass('afteractive').end()
								.find('.load').fadeIn(speed);
						});

				}

			} else {

				$old.removeClass('active')
					.animate({ height: '40px' }, speed)
					.find('.load').fadeOut(speed, function(){
						$old.find('.link').fadeIn(speed);
						$ul.find('.afteractive').removeClass('afteractive');
						$(this).html('');
					});

				$li.addClass('active')
					.find('.load').load($li.find('.link a.item').attr('href'), function(){
						$li.find('.link').fadeOut(speed, function(){
							$li.animate({ height: $li.find('.load').outerHeight() + 'px' }, speed)
								.find('.load').fadeIn(speed).end()
								.next('li').addClass('afteractive');
						});
					});

			}

		});

	});

}



// Person Slider
$.fn.personSlider = function(opts){

	var speed = opts.speed,
	    timeout = opts.timeout;

	this.each(function(){

		var $el = $(this),
		    $cntr = $el.find('.cntr'),
		    $showall = $el.closest(opts.showAllContext).find(opts.showAll),
	   	 $showloop = $el.closest(opts.showLoopContext).find(opts.showLoop),
		    ulWidth = $el.find('ul').width(),
		    cntrWidth = $el.find('ul').length * ulWidth;

		if ($cntr.hasClass('loop'))
			$cntr.width(cntrWidth).find('ul:not(:first)').fadeTo(0, 0.3);

		setInterval(function(){
			if ($cntr.hasClass('loop')) {
				var currentPos = $cntr.css('left').replace('px',''),
				    currentItem = (currentPos * -1) / ulWidth + 1;
				if (currentPos - ulWidth == -1 * cntrWidth) {
					$cntr
						.animate({ left: 0 }, speed)
						.find('ul:not(:eq(0))').fadeTo(speed, 0.3).end()
						.find('ul:eq(0)').fadeTo(speed, 1).end()
					;
				} else {
					$cntr
						.animate({ left: currentPos - ulWidth + 'px' }, speed)
						.find('ul:not(:eq('+currentItem+'))').fadeTo(speed, 0.3).end()
						.find('ul:eq('+currentItem+')').fadeTo(speed, 1).end()
					;
				}
			}
		}, timeout);

		$showall.click(function(e){
			$(this).hide();
			$showloop.show();
			$cntr
				.removeClass('loop')
				.addClass('all')
				.css({ left: 0, width: 'auto' })
				.find('ul').fadeTo(0, 1)
			;
			$el.animate({ height: '372px' }, speed);
			e.preventDefault();
		});

		$showloop.click(function(e){
			$(this).hide();
			$showall.show();
			$cntr.removeClass('all')
				.addClass('loop')
				.css({ left: 0, width: cntrWidth })
				.find('ul:not(:first)').fadeTo(0, 0.3);
			$el.animate({ height: '192px' }, speed);
			e.preventDefault();
		});

	});

}



// Nice select box
$.fn.niceSelect = function(){

	this.each(function(){

		var $select = $(this),
		    $replaced = $select.after('<div class="replaced"></div>').parent().find('.replaced'),
		    $selected = ($select.find('option[selected]').length == 1) ? $select.find('option[selected]') : $select.find('option:first');

		$select.addClass('initiated');

		$replaced.html($selected.html())
			.click(function(){
				$select.click();
			});

		$select.change(function(){
			$replaced.html( $(this).find('option:selected').html() );
		});

	});

}



// Tagcloud
$.fn.tagcloud = function(){

	this.each(function(){

		var $cloud = $(this);

		$cloud.find('li').each(function(){
			var height = Math.random()*15+10;
			$(this).css('height', height + 'px').find('a').css('line-height', height + 'px');
		});

	});

}



// Gallery
$.fn.gallery = function(opts){

	this.each(function(){

		var $gallery = $(this);

		$gallery.find('ul a').live('click', function(e){
			e.preventDefault();
			var url = $(this).attr('href');
			$.fancybox({
				href: url,
				overlayOpacity: 0.5,
				overlayColor: '#000'
			});
		});

		$gallery.find('.nav a').live('click', function(e){

			e.preventDefault();
			$clicked = $(this);
			$gallery.height($gallery.height());

			$.get($clicked.attr('href'), function(data){
			
				$gallery.find('li').reverse().each(function(i){
					var $li = $(this);
					setTimeout(function(){
						$li.fadeOut(150);
					}, 40 * i);
				});

				setTimeout(function(){
					$gallery
						.find('.nav').hide().remove().end()
						.find('ul').hide().after(data).remove().end()
						.find('li').hide().each(function(i){
							var $li = $(this);
							setTimeout(function(){
								$li.fadeIn(150);
							}, 40 * i);
						})
					;
					Cufon.refresh('.gallery .nav a');
				}, 50 * $gallery.find('li').length);

			});

		});

	});

}
