var log = function(){ log.history = log.history || []; log.history.push(arguments); arguments.callee = arguments.callee.caller; if(this.console) console.log( Array.prototype.slice.call(arguments) ); };


var LMG_SHOW = function(){

	/* private methods start */
	var show = {
		
		timer : "",
		
		prepare : function(){
			
			$('.flexcroll').jScrollPane();
			
			/* load slideshow pictures */
			$.ajax({
				url: "xml_pics.php",
			  	type: "GET",
			  	dataType: "text",
			  
			   	complete: function(txt, txtStatus) {

				    $('body').append(txt.responseText);
				    
				    $('.gallery').each(function(index) {
				    	
						$(this).find('a').attr('rel', 'gallery[' + $(this).attr('name') + ']');
						
					});
					
					$("a[rel^='gallery']").prettyPhoto({
						deeplinking: true,
						animation_speed : 'normal'
					});
					
					try{
						
						$("a[rel='milkbox:']").each(function(index) {
						
							if( $(this).attr('href').substring(0, 5) != "javas" ){
								
								$(this).prettyPhoto({
									
									deeplinking: true,
									animation_speed : 'normal'
									
								});
							}
						});
						
					}catch(e){
						
					}
			  	}
			});
			
		},
		
		slideshow : function(d){
			
			try{ log(d.length); }catch(e){ return; }
			
			var data = d;
			var container = $('#show');
			var controller = '<div class="slideshow-controller"><ul><li class="first"><a title="First [Shift + Leftwards Arrow]"></a></li><li class="prev" ><a title="Prev [Leftwards Arrow]" class=""></a></li><li class="pause"><a title="Play / Pause [P]"></a></li><li class="next" ><a title="Next [Rightwards Arrow]"></a></li><li class="last" ><a title="Last [Shift + Rightwards Arrow]"></a></li></ul></div>';
			var preloader, bar;
			var t = 400;
			var actual = 0;
			var btn_next, btn_prev, btn_pause;
			var is_busy = false;
			var is_paused = false;
			
			container
				.append('<div class="image-container"></div>')
				.append('<div class="ajax-load"></div>')
				.append('<div class="image-container-bar"><div class="slide-title"></div><div class="slide-description"></div></div>')
				.find('.image-container-bar')
				.append(controller);
			
			controller = $('.slideshow-controller');
			
			var handle_clicks = function(el){
				
				if (!is_busy) {
					
					clearTimeout(show.timer);
					
					btn_pause.find('a').css('background-position', 'top');
					
					switch(el){
					
						case 'prev' : 
							
							actual-- != 0 ? actual-- : actual = data.length-2; 
							is_paused = false;
							change(); 
							
							break;
							
						case 'next' : 
						
							is_paused = false;
							change(); 
							
							break;
							
						case 'pause':
						
							if(is_paused){
								
								is_paused = false;
								change();
								
							}else{
								
								btn_pause.find('a').css('background-position', '0 10px');
								
								is_paused = true;
							}
													
							break;
					}
				}
			}; // handle_clicks end
			
			btn_prev  = controller.find('.prev' ).click( function() { handle_clicks("prev");  });
			btn_next  = controller.find('.next' ).click( function() { handle_clicks("next");  });
			btn_pause = controller.find('.pause').click( function() { handle_clicks("pause"); });
			
			preloader = $('.ajax-load').fadeOut(0);
			
			controller.fadeOut(0);
			
			container.bind({
				mouseenter 	: function(){ controller.fadeIn(t*0.25); },
				mouseleave 	: function(){ controller.fadeOut(t*0.25); }
			});
			
			data[0].href == "" ? container.css('cursor', 'initial') : container.css('cursor', 'pointer');
			
			bar = $('.image-container-bar').fadeTo(0,0);

		  	preloader.delay(t).fadeIn(t);
						
			var load_picture = function(id){
				
				data[id].href == "" ? container.css('cursor', 'initial') : container.css('cursor', 'pointer');
				
				is_busy = true;
				
				clearTimeout(show.timer);
				
				preloader.fadeIn(t);
				
				bar = $('.image-container-bar').fadeTo(t,0, function(){
					
					container.find('.slide-title').empty().append(data[id].caption).fadeTo(0,0).delay(t).fadeTo(t,1.1);
					container.find('.slide-description').empty().append(data[id].description).fadeTo(0,0).delay(t*1.1).fadeTo(t,1);
				
					bar.fadeTo(t,1);
				});
			
				var img = $("<img />").attr('src', data[id].path + "?rev=" + Math.random()).fadeTo(0,0).load(function() {

      				$('.image-container')
      					.find('img')
      					.fadeOut(t, function(){ 
							$(this).remove(); 
						})
						.end()
						.append(img);
          				
					img.fadeTo(t*3,1, function(){
						
						container.css('background', '#000000');
						
						is_busy = false;
						
						img = null;
					}).click(function(){
						if( data[id].href != "" ) document.location = data[id].href;
					});
					
				  	preloader.fadeOut(t*0.5);
					
				  	if(data.length > 1){
				  		
				  		show.timer = setTimeout(change, 10000);
				  		
			  		}else{

			  			clearTimeout(show.timer);
			  			
			  			controller.empty();
			  			
			  			container.unbind("mouseenter");
			  			container.unbind("mouseleave");
			  			container.unbind("click");
				 	  			
			  		}
										
        		});
            	
			}// load_picture() end
			
			var change = function(){
				
				if(!is_paused){
					
					actual < data.length-1 ? actual++ : actual = 0;
				
					load_picture(actual);
				}
				
			};
			
			if(data.length > 0){
				
				load_picture(actual);
				
				clearTimeout(show.timer);

			}
			
		}, // slideshow end
		
		
		flash_static_image : function(d){
			
			$('#show').empty();
			
			clearTimeout(show.timer);
			
			show.slideshow(d)
			
		}// flash_static_image end
		
		
	}
	/* private methods end */
	
	


	
	/* public methods start */
	return {
		
		init : function(){
			show.prepare();
		},
		
		slideshow : function(d){
			show.slideshow(d);
		},
		
		set_flash_image : function(d){
			show.flash_static_image(d);
		}
		
	}
	/* public methods end */
	
	
	
	
}();


jQuery.fn.ready( function(){
	LMG_SHOW.init();
});

/* flash interfase */
function loadthumb(p, h, c, d){

	LMG_SHOW.set_flash_image([{ path : p, href : h, caption : c, description : d }]);
	
}

/* gallery start */
function loadGall(id){
	
	var opened = false;
	
	$("a[rel^='gallery']").each(function(index) {
				
		if( $(this).attr('rel') == "gallery[" + id + "]" ){
						
			if(!opened){
				
				$(this).click();
				
				opened = true;
				
				setTimeout(function(){
					
					opened = false;
					
				}, 500);
			}			
		}
	});
}
/* gallery end */
