$.fn.extend({
			
	getNextElem: function(elem) {

		if(this.next().length > 0)
			return this.next();
		else
			return this.parent().find(elem + ":first");
		
	}
	
});

(function($){

	$.fn.selectList = function() {
	
		return this.each(function() {
	
			var that = $(this),
				height = that.height() - 32; // start pos

			that.hoverIntent({
				interval: 0, sensitivity: 4, timeout: 200, 
				over: function() { that.stop().animate({top: 0}); }, 
				out: function() { that.stop().animate({top: -height}); }
				});
			
			/* 
			if(that.attr('id') == 'country-picker') {
			
				that.find('a').bind('click', function(e) {
						
					e.preventDefault();
						
					var destination = this.href;
					
					// _gaq.push(['_trackEvent', 'Navigate to Country', 'click', destination]);
					window.location = destination;
						
				});
			}
			*/
	
		});
		
	}	
	
	// bind to lists
	$('.top-select-list').selectList();

	// slider fn
	$.fn.itemSlider = function(options) {
		
		var defaults = {
			auto: true,
			speed: 5000
			};
		
		var options = $.extend(defaults, options); 
					  
		var container 	= $(this),
			nav 		= container.find('#slider-nav'),
			anchors		= nav.find('a'),
			first		= nav.find('li:first-child a'),
			current 	= first.attr('rel'),
			content		= container.find('#slider-content'),
			items 		= content.children('div'),
			slidetimer	= null;

		$.nextSlide = function() { nav.find('li:has(a.current)').getNextElem('li').find('a').trigger('click', [true]); }

		return container.each(function() {

			anchors.each(function() {
								  
				var a = $(this), rel = a.attr('rel');
				
				a.bind('click', function(e, auto) {

					e.preventDefault();
		
					if(rel == current) return false;
		
					items
						.removeClass('current')
						.hide()
						.filter(function(index) { return $(this).attr('rel') == rel; })
						.addClass('current')
						.show();

					anchors
						.removeClass('current')
						.filter(function(index) { return $(this).attr('rel') == rel; })
						.addClass('current');

					current = rel;
					
					// when event gets triggered manually, stop the slider
					if( auto != true ) { clearInterval(slidetimer); }

				});
			
			});
			
			if(options.auto) { slidetimer = setInterval("$.nextSlide()", options.speed); }

		});

	}

	$('#slider').itemSlider({auto: false});

	// default values for input fields
	$('input[type=text], textarea').focus(function(){ if(this.value == this.placeholder) { this.value = ''; } else { this.select(); }});

	// add external fn
	$('a:external, a[rel=external]').attr({target: '_blank'});

	// position icons
	$('#slider-nav a').each(function(){
									 
		var that = $(this), rel = that.attr('rel'), icon = that.find('.icon'), pos = ((rel - 1 ) * 29) * -1; 
		icon.css({backgroundPosition: '0 ' + pos + 'px'});
			
	});

	$('form#country-select select').bind('change', function() {
		
		var destination = this.value; 
		
		// log to GA
		// _gaq.push(['_trackEvent', 'Navigate to Country', 'click', destination]);
		
		// redirect user
		window.location = destination;
		
	});

})(this.jQuery);
