(function(){

	var defaultOptions = {
		onAfter: null,
		onBefore: null,
		timeout: 8000
	}

	var pkCarousel = function( element, options ) {

		var self = this;
		var index = 0;
		var $elements = $(element).children();
		var totalSize = $elements.size();

		var timer = setTimeout( function() { self.advance(); }, options.timeout );

		this.advance = function( idx ) {

			clearTimeout( timer );

			if ( idx != 0 ) {
				idx = idx || ( index + 1 ) % totalSize;
			}

			if ( options.onBefore ) {
				options.onBefore( idx, $($elements[ index ]) );
			}

			$($elements[ index ]).fadeOut('slow',function(){
				index = idx;
				$($elements[ index ]).fadeIn('slow',function(){
					if ( options.onAfter ) {
						options.onAfter( idx, $($elements[ index ]) );
					}
					timer = setTimeout( function() { self.advance(); }, options.timeout );
				});
			});
		};

		this.next = function() {
			this.advance( ( index + 1 ) % totalSize );
		};

		this.previous = function() {
			var idx = index == 0 ? totalSize - 1 : index - 1;
			this.advance( idx );
		};
	};

	var methods = {
		init : function( options ) {
			var settings = $.extend( {}, defaultOptions, options );

			return this.each(function() {
				var obj = new pkCarousel( this, settings );
				$.data( this, "pkCarousel", obj );
			});
		},
		advance: function( nextIdx ) {
			return this.each(function() {
				var obj = $(this).data( "pkCarousel" );
				if (!obj) {
					return this;
				}
				obj.advance( nextIdx );
			});
		},
		next: function() {
			return this.each(function() {
				var obj = $(this).data( "pkCarousel" );
				if (!obj) {
					return this;
				}
				obj.next();
			});
		},
		previous: function() {
			return this.each(function() {
				var obj = $(this).data( "pkCarousel" );
				if (!obj) {
					return this;
				}
				obj.previous();
			});
		}
	};

	$.fn.pkCarousel = function( method ){
		if ( methods[method] ) {
			return methods[method].apply( this, Array.prototype.slice.call( arguments, 1 ));
		} else if ( typeof method === 'object' || ! method ) {
			return methods.init.apply( this, arguments );
		} else {
			$.error( 'Method ' +  method + ' does not exist on jQuery.pkCarousel' );
		}
	};
})(jQuery);

function activateImage( $element, path ) {

	var src = $element.attr('src').split('/');
	
	src[ src.length - 1 ] = path;

	$element.attr('src', src.join('/'));
}

$(document).ready(function(){

	var $navButton = $('.navigation_button');
	$('#focus_box').children().each(function( k, v ){
		$clone = $navButton.clone();
		$clone.data('idx', k);
		$('.navigation_button:last').after( $clone );
	});
	$navButton.remove();
	activateImage( $('.navigation_button:first img'), 'btn_dot_active.png' );

	$('.navigation_button').click(function(){
		$('#focus_box').pkCarousel( 'advance', $(this).data('idx') );
		return false;
	});

	$('#focus_navigation .prev').click(function(){
		$('#focus_box').pkCarousel( 'previous' );
		return false;
	});
	$('#focus_navigation .next').click(function(){
		$('#focus_box').pkCarousel( 'next' );
		return false;
	});
	
	$('#focus_box').pkCarousel({
		timeout: 5000,
		onBefore: function( nextIdx, element ) {
			$('img', '.navigation_button').each(function( k, v ){
				if ( k == nextIdx ) {
					activateImage( $(this), 'btn_dot_active.png' );
				} else {
					activateImage( $(this), 'btn_dot_nonactive.png' );
				}
			});
		}
	});

	//Mainmenu
	$("#main_menu a").each(function(i){
		preimg = $(this).find("img:first").attr('src').replace('.jpg', '_hover.jpg');
		jQuery("<img>").attr("src", preimg);
		if($(this).hasClass("selected")){
			$(this).find("img:first").attr('src', $(this).find("img:first").attr('src').replace('.jpg', '_hover.jpg'));
		}
	});

	$("#main_menu a").mouseover(function(){
		if(!$(this).hasClass("selected")){
			$(this).find("img:first").attr('src', $(this).find("img:first").attr('src').replace('.jpg', '_hover.jpg'));
		}
	});

	$("#main_menu a").mouseout(function(){
		if(!$(this).hasClass("selected")){
			$(this).find("img:first").attr('src', $(this).find("img:first").attr('src').replace('_hover.jpg', '.jpg'));
		}
	});

	//Naviagtion footer menu
	$("#footer_navigation a").mouseover(function(){
		if(!$(this).hasClass("selected")){
			$(this).find("img:first").attr('src', $(this).find("img:first").attr('src').replace('.jpg', '_hover.jpg'));
		}
	});

	$("#footer_navigation a").mouseout(function(){
		if(!$(this).hasClass("selected")){
			$(this).find("img:first").attr('src', $(this).find("img:first").attr('src').replace('_hover.jpg', '.jpg'));
		}
	});

});
