// preload function
jQuery.fn.onImagesLoaded = function(_cb) { 
	return this.each(function() {
		var $imgs = (this.tagName.toLowerCase()==='img')?$(this):$('img',this),
		_cont = this,
		i = 0,
		_done=function() {
			if( typeof _cb === 'function' ) _cb(_cont);
		};
		if( $imgs.length ) {
			$imgs.each(function() {
			var _img = this,
			_checki=function(e) {
			if((_img.complete) || (_img.readyState=='complete'&&e.type=='readystatechange') )
			{
				if( ++i===$imgs.length ) _done();
			}
			else if( _img.readyState === undefined ) // dont for IE
			{
				$(_img).attr('src',$(_img).attr('src'));
			}
		};
		$(_img).bind('load readystatechange', function(e){_checki(e);});
		_checki({type:'readystatechange'});
		});
		} else _done();
	});
};
// delay function
jQuery.fn.delay = function(time,callback) {
	this.each(function() {
		setTimeout(callback,time);
	})
	return this;
};

	//Decline variables 
	var slide_values = []; 
	var total_slides = 0;
	var active_slide = 0;
	
	function random_direction(){
		var random_number=Math.floor(Math.random()*8);
		switch (random_number) {
			case 0: return 'tl';
			break;
			case 1: return 'tr';
			break;
			case 2: return 'bl';
			break;
			case 3: return 'br';
			break;
			case 4: return 't';
			break;
			case 5: return 'r';
			break;
			case 6: return 'b';
			break;
			case 7: return 'l';
			break;
			default: return 'tl'; 
		}
	}
	function slide_to_pos(opposite){
		switch (opposite) {
			case 'tl': return 'br';
			break;
			case 'tr': return 'bl';
			break;
			case 'bl': return 'tr';
			break;
			case 'br': return 'tl';
			break;
			case 't': return 'b';
			break;
			case 'r': return 'l';
			break;
			case 'b': return 't';
			break;
			case 'l': return 'r';
			break;
			default: return 'tl'; 
		}
	}
	function set_slide_position(slide_obj){
		var the_slide_id = $('#'+slide_obj.id);

		var slide_width = parseInt(slide_obj.width);
		var slide_height = parseInt(slide_obj.height);
		
		switch (slide_obj.start_pos) {
			case 'tl': the_slide_id.css({'top':0,'left':0});
			break;
			case 'tr': the_slide_id.css({'top':0,'left':eval(960-slide_width)});
			break;
			case 'bl': the_slide_id.css({'top':eval(372-slide_height),'left':0});
			break;
			case 'br': the_slide_id.css({'top':eval(372-slide_height),'left':eval(960-slide_width)});
			break;
			case 't': the_slide_id.css({'top':0,'left':eval((960-slide_width)/2)});
			break;
			case 'r': the_slide_id.css({'top':eval((372-slide_height)/2),'left':eval(960-slide_width)});
			break;
			case 'b': the_slide_id.css({'top':eval(372-slide_height),'left':eval((960-slide_width)/2)});
			break;
			case 'l': the_slide_id.css({'top':eval((372-slide_height)/2),'left':0});
			break;
			default: the_slide_id.css({'top':0,'left':0});
		}
	}
	function slideshow(){
		if(total_slides > active_slide){
			var slide = slide_values[active_slide];
			$('#'+slide.id).show().animate({'opacity':1},slide_transition_speed,function(){
				$('#'+slide.id).animate({'top':eval(slide.slide_pos_top),'left':eval(slide.slide_pos_left)},slide_move_speed,function(){
					$('#'+slide.id).animate({'opacity':0},slide_transition_speed,function(){
						$('#'+slide.id).css({'top':eval(slide.start_pos_top),'left':eval(slide.start_pos_left)});														  
						$('#'+slide.id).hide();
						active_slide++;
						slideshow()
					});
				});
			});

		}else{
			$('#slider_images li').animate({'opacity':0},1).hide();
			active_slide=0;
			slideshow();
		};
	}
	function return_position(the_width, the_height, the_position, x_or_y){
		var width = parseInt(the_width);
		var height = parseInt(the_height);
		if(x_or_y == 'top'){
			switch (the_position) {
				case 'tl': var output = 0;
				break;
				case 'tr': var output = 0;
				break;
				case 'bl': var output = eval(372-height);
				break;
				case 'br': var output = eval(372-height);
				break;
				case 't': var output = 0;
				break;
				case 'r': var output = eval((372-height)/2);
				break;
				case 'b': var output = eval(372-height);
				break;
				case 'l': var output = eval((372-height)/2);
				break;
				default: var output = 0;
			}
		}else if(x_or_y == 'left'){
			switch (the_position) {
				case 'tl': var output = 0;
				break;
				case 'tr': var output = eval(960-width);
				break;
				case 'bl': var output = 0;
				break;
				case 'br': var output = eval(960-width);
				break;
				case 't': var output = eval((960-width)/2);
				break;
				case 'r': var output = eval(960-width);
				break;
				case 'b': var output = eval((960-width)/2);
				break;
				case 'l': var output = 0;
				break;
				default: var output = 0;
			}
		}
		return output;
	}
	function initalize_slider(directions){
		//Create the navigation on the left side

		//Check if a arrays was submitted or not
		if(directions[0] == 'random'){
		}else{
		};
		//loop through ech image
		$('#slider ul li').each(function(i){
			//Give everey image an ID and append a dot to the slidenavigation
			$(this).attr('id','slide_'+i);

			//build new slideobject
			var slide_object = new Object;
			slide_object.id = 'slide_'+i;
			slide_object.width = $(this).children('img').attr('width');
			slide_object.height = $(this).children('img').attr('height');
			if ($.browser.msie) {
				slide_object.width = $(this).children('img').css('width');
				slide_object.height = $(this).children('img').css('height');
			}else{
				slide_object.width = $(this).children('img').attr('width');
				slide_object.height = $(this).children('img').attr('height');
			}
			// if the direction was given through an array
			if(directions[0] == 'random'){
				slide_object.start_pos = random_direction();
			}else{
				slide_object.start_pos = directions[i];
			};
			slide_object.slide_pos = slide_to_pos(slide_object.start_pos);
			
			slide_object.start_pos_top = return_position(slide_object.width, slide_object.height, slide_object.start_pos,'top');
			slide_object.start_pos_left = return_position(slide_object.width, slide_object.height, slide_object.start_pos,'left');
			slide_object.slide_pos_top = return_position(slide_object.width, slide_object.height, slide_object.slide_pos,'top');	
			slide_object.slide_pos_left = return_position(slide_object.width, slide_object.height, slide_object.slide_pos,'left');	
			//add to array
			slide_values.push(slide_object);
		});
		//count all slides
		total_slides = slide_values.length;
		//set the slide position for all slides
		for (var i = 0; i < total_slides; i++) {
			set_slide_position(slide_values[i]);
		}
		//hide all slides
		$('#slider_images li').animate({'opacity':0},1).hide(function(){$('#slider_images').show();});
		//hide the preloader gif
		$('#slider').css({'background-image':'none'});
		//when everything is loaded start the slideshow
		slideshow();
	}//initalize end

$(document).ready(function(){
	$('#slider_images').onImagesLoaded(function(_img){initalize_slider(direction)});
});

