// Partners.js

$(document).ready(function() {
	$('body').addClass('js');
	units.handle();
	//pagelist.handle();
	apply.handle();
	video.handle();
	
	//$('#partners .partner').quicklist({items: 3, autoAdvance: true, autoTime: 3500});
	
	$('#partners .partner').quicklist({
		items: 3,
		autoAdvance: true,
		autoTime: 7000
	});
});

var units = {
	handle: function() {
		$('#units ul p').hide();
		$('#units .current p').show();
		$('#units a').each(function(i) {
			$(this).click(function(e){
				e.preventDefault();
				
				var last = $('#units .current');
				last.removeClass('current');
				
				$(this).parent().addClass('current');

				if (last.attr('class') != 'current') {
					$('#units ul p').hide('fast');
					$('#units .current p').show('fast');
				}
				
				//$('#unitImages').css('backgroundPosition', '-'+i*237+'px 0');
				$('#unitImages').animate({
					backgroundPosition: '-'+i*238+'px 0'
				}, 500, 'swing');
			});
		});
	}
}

var apply = {
	handle: function() {
		$('a:contains(Apply now), a:contains(Apply to become a partner)').click(function(e) {
			$('#apply-now').fadeOut('medium', function() {
				$('#partners-apply').fadeIn('fast');
				var targetOffset = $('#partners-apply').offset().top;
				$('html,body').animate({ scrollTop: targetOffset}, 1000);
			});
		});
		
		if ($('.errors').length == 0 && $('#success').length == 0) {
			$('#partners-apply').hide();
		} else {
			$('#apply-now').hide();
			if ($('#success').length > 0) {
				$('a:contains(Apply now)').hide();
			}
		}
	}
}

var video = {
	lastTitle: '',
	lastUser: '',
	handle: function() {
		$('#partners a.thumbnail, #partners a.play-video').click(video.click);
		$('#pbottom > *').wrapAll('<div id="video-container"></div>');
		
		if ($('object').length == 0) $('#partners a.play-video').hide();
	},
	click: function(e) {
		var url = 'http://www.viddler.com/explore/'+$(this).attr('href').replace('#', '');
		video.lastTitle = $(this).attr('alt');
		video.lastUser = $(this).parent().children('h4').text(); 
		
		if ($('object').length > 0) {
			$.ajax({
			  url: '_php/ajax.php?oembed='+url,
			  cache: false,
			  success: video.swap
			});
		}
	},
	swap: function(data) {
		if (data) {
			$('#video-container').fadeOut('slow', function(){
				$('#pbottom').addClass('loading');
				
				$('object').after(data);
				$('object:eq(0)').remove();
				$('#pbottom h3').text(video.lastTitle);
				$('#pbottom b').text(video.lastUser);
				
				// $('#pbottom').removeClass('loading');
				$('#video-container').fadeIn();
			});
		}
	}
}

/* var pagelist = {
	number: false,
	selector: false,
	list: false,
	timer: false,
	
	handle: function() {
		var source = $('ul.pagelist:eq(0)');
		if (source.length > 0) {
			pagelist.number = source.attr('class').replace(/[^0-9]/g, '');
			pagelist.selector = source.attr('rel');
			pagelist.auto = source.attr('class').match(/plauto/);
			pagelist.list = source;
			
			var t = this; // Save the reference to the object, because jQuery takes over "this" (I think)
			
			$(t.selector+':gt('+(t.number-1)+')').hide();
			var length = $(t.selector).length;
			var pages = Math.ceil(length / t.number);
			
			this.pages = pages;
			this.length = length;
			
			for(var i=0;i<pages;i++) {
				t.list.append('<li><a href="#">'+(i+1)+'</a></li>');
			}
			t.list.append('<li class="prev"><a href="#">Previous</a></li><li class="next"><a href="#">Next</a></li>');
			t.list.children(':first-child').addClass('current');
			
			t.list.find('a').click(function(e) { // .click(t.change) takes over "this", so I am doing it this way
				t.change(e);
			});	
			
			if (t.auto) {
				t.timer = setTimeout(pagelist.autoswitch, 5000);
			}
		}
	},
	change: function(e, index) {
		var t = this;
		
		if (t.auto) {
			clearTimeout(t.timer);
		}
		
		if ($(t.selector+':animated').length == 0) {
			if (! index && index != 0) {
				e.preventDefault();
				
				var e = $(e.target);
				var index = 0;
					
				if (e.parent().attr('class') != 'next' && e.parent().attr('class') != 'prev') {
					t.list.find('.current').removeClass('current');
					e.parent().addClass('current');
				} else {
					if (e.parent().attr('class') == 'next') {
						t.list.find('.current').removeClass('current').next().addClass('current');
					} else {
						t.list.find('.current').removeClass('current').prev().addClass('current');
					}
				}
				
				t.list.children('li').each(function(i) {
					if ($(this).is('.current')) {
						index = i+1;
					}
				});
			}
			
			if (index <= 0 || index > t.pages) index = 1;
			
			t.list.children('.current').removeClass('current');
			t.list.children('li:eq('+(index-1)+')').addClass('current');
			
			var max = t.number * index;
			var min = (t.number * index) - t.number;
			
			// alert(max+' '+min);
			
			var show = [];
			
			for(var i=0;i<max;i++) {
				if (i >= min) {
					show.push(t.selector+':eq('+i+')');
				}
			}
			
			$(t.selector+':visible').fadeOut('fast', function() {
				$(show.join(',')).fadeIn('fast');
			});
		}
		
		if (t.auto) {
			t.timer = setTimeout(pagelist.autoswitch, 5000);
		}
	},
	autoswitch: function() {
		var index = 0;
		pagelist.list.children('li').each(function(i) {
			if ($(this).is('.current')) {
				index = i+2;
			}
		});
		pagelist.change(null, index);
	}
} */
