var fade_speed = 200;
$(function(){
	
	/*
		Hardcoding...
	*/
		$('a[title=vintage]').parent().addClass('vintage').css('display','none').clone().appendTo('#gallery_nav > ul');
		$('.vintage:last').css('display','block');
		
		$('a[title=petroleum]').parent().addClass('petroleum').css('display','none').clone().prependTo('#gallery_nav > ul');
		$('.petroleum:first').css('display','block')
	/*
		...end
	*/
	
	$('ul li ul').each(function(){ //IE6/7 Fix... Need more logic in PHP to see if there is no sub directories, not to make an empty UL...
		if($(this).children('li').length < 1){
			$(this).remove();
		}
	});
	
	function activate_gallery(){
		//Settings and functions
		function kill(){$('.kill').css({display:'block'});} //Blocks the entire gallery
		function unkill(){$('.kill').css({display:'none'});} //Unblocks the entire gallery
		function replace_image(lrg_img,active){
			kill();
			$('.active_image').removeClass('active_image');
			$(active).addClass('active_image');
			$('#img_holder').stop().fadeOut(fade_speed,function(){
				$(this).empty().append('<img src="'+lrg_img+'" alt="" />').children('img').load(function(){
					$(this).parent().fadeIn(fade_speed,function(){$(this).css({opacity:'1'});unkill();});
				});
			});
			var page_number = $('#paginator p').text().split('/');
			$('#paginator p').empty().append($("img").index( $('.active_image') )+'/'+page_number[1]);
		}
		$('#carousel_wrapper ul').css({marginLeft:'0px'});
		function c_slide(the_this){
			if($(the_this).attr('href') == '#prev'){
				if($('#carousel_wrapper ul').css('margin-left') !== '0px'){
					kill();
					//width of wrapper plus 9px of margin!
					$('#carousel_wrapper ul').animate({marginLeft:'+=520px'},1000,'easeOutCirc',function(){
						unkill();
					});
				}
			}
			else{
				if($('#carousel_wrapper ul li').size() > 5){
					if($('#carousel_wrapper ul').css('margin-left')!==('-'+$('.set').size()*520)+'px'){
						kill();
						$('#carousel_wrapper ul').animate({marginLeft:'-=520px'},1000,'easeOutCirc',function(){
							unkill();
						});
					}
				}
			}
		}
		
		//Boot up stuff (only needs to run 1 time)
		$('#gallery_wrapper a').click(function(){return false});
		$('#carousel_wrapper img:first').addClass('active_image');
		$('#paginator p').empty().append('1/'+$('#carousel_wrapper img').size());
		var first_image = $('#carousel_wrapper img:first');
		replace_image($(first_image).attr('src').replace('thumbs','lrg'),$(first_image));
		//alert($(first_image).attr('src').replace('thumbs','lrg'));
			//Get the large image
			$('#carousel_wrapper ul li img').click(function(){
				var lrg_img = $(this).attr('src').replace('thumbs','lrg');
				if(lrg_img !== $('#lrg_image_container img').attr('src')){
					replace_image(lrg_img,$(this));
				}
			});
			


			$('#arrow_wrapper a').click(function(){
				if($(this).attr('href') == '#prev'){
					var the_selector = $('.active_image').parent().prev().find('img');
					var lrg_img = $(the_selector).attr('src').replace('thumbs','lrg');
				}
				else{
						var the_selector = $('.active_image').parent().next().find('img');
						var lrg_img = $(the_selector).attr('src').replace('thumbs','lrg');
				}
				replace_image(lrg_img,$(the_selector));

				if($(this).attr('href') == '#next' && $('.active_image').hasClass('set')){
					c_slide($(this));
				}
				else if($(this).attr('href') == '#prev' && $('.active_image').hasClass('back_set')){
					c_slide($(this));
				}
				this.event.stopPropagation();
			});


			//C Nav
			var i=-1;
			$('#carousel_wrapper img').each(function(){
				if(i == 3){$(this).addClass('back_set');i++}
				else if(i == 4){$(this).addClass('set');i=0}
				else {i++;}
			});
			$('#carousel_nav a').click(function(){c_slide($(this));this.event.stopPropagation();});
	}
	
	$('#gallery_nav ul a').click(function(){
		if(!$(this).parent().children('ul').children('li').length>0){
			var new_set = $(this).attr('rel');
			var new_prefix = $(this).attr('title');
			if($('.active_gallery').attr('rel') !== new_set){
				$('.active_gallery').removeClass('active_gallery');
				$(this).addClass('active_gallery');
				$.get("js/get_images.php", { set:new_set,prefix:new_prefix }, function(data){
						$('#carousel_wrapper ul').fadeOut(750,function(){
							$(this).empty().append(data).fadeIn(750);
							activate_gallery();
						});
				});
			}
		}
	});
	
		var default_gallery = 'images/gallery/thumbs/petroleum/truck+&+trailer';
		$.get("js/get_images.php", { set:default_gallery,prefix:'truck+&+trailer' }, function(data){
				$('#carousel_wrapper ul').empty().append(data);
				$('a[rel='+default_gallery+']').addClass('active_gallery');
				activate_gallery();
		});
	
});
