var BA = function() {

	var currentSlideShow = [];
	var currentItem = -1;
	var galleryCategory;
	var slideShowActive = false;
	var slideTimer;
	var slideTime = 6000;
	var portfolio = false;

	var getEventTarget = function(e) {  
        e = e || window.event;  
        return e.target || e.srcElement;
    }

	var addEventListeners = function() {
		console.log('adding addeventlisteners');
		$('.rightmenu').bind('click', function(event) {
            mainMenuHandler(event.originalEvent);
        });
        $('.leftmenu ul li').bind('click', function(event) {
            subMenuHandler(event.originalEvent);
        });
        
        $('#slideControle .left').bind('click', function(event) {
            playPreviousItem();
        });
        
        $('#slideControle .right').bind('click', function(event) {
            playNextItem();
        });
        
        $('#showGallery').bind('click', function(event) {
           showGalleryToggle();
        });
        
        $("img").lazyload({
         placeholder : "/assets/loader.png",
        	effect : "fadeIn" 
      	});
    }
	
	var mainMenuHandler = function(event) {
	
		$(".leftmenu").children().children().each(function(){
			var li = $(this);
			li.removeClass('selectedMenu');		
		});
		
		$(".rightmenu").children().children().each(function(){
			var li = $(this);
			li.removeClass('selectedMenu');		
		});
		
		$(".rightmenu").children().children().children().each(function(){
			var li = $(this);
			li.removeClass('selectedMenu');		
		});
	
	
		switch($(getEventTarget(event)).html()) {
			case "Portfolio":
				$('.leftmenu').show();
				$(getEventTarget(event)).addClass('selectedMenu');
				break;
			case "Home":
				porfolio = false;
				playSlideShow(false);
				$('.leftmenu').hide();
				$(getEventTarget(event)).addClass('selectedMenu');
				break;
		}
	}
		
	var subMenuHandler = function(event) {
		target = $(getEventTarget(event));
		if (target[0].nodeName != "LI") return;
		$(".leftmenu").children().children().each(function(){
			var li = $(this);
			li.removeClass('selectedMenu');		
		});	
		target.addClass('selectedMenu');	
		showGallery(target.attr('id'));		
	}
	
	
	var playSlideShow = function(play) {
	
		if (play != false) play = true;
	
		$('.gallery').hide();
        $('#slideshow').show();
       	$('#slideControle').hide();
       	$('#showGallery').hide();
	
		clearTimeout(slideTimer);
		currentSlideShow = [];	
		currentItem = -1;
		
		var wrapper = $('#wrapper');
		wrapper.empty();
		wrapper.addClass('gallery');
		$("#images").children().each(function(){
       		var div = $(this);
			var html = '<div id="' + div.attr('id') + '" class="galleryItem"><img source="/images/' + div.attr('category') + '/' + div.attr('id') + '/thumb.jpg"/></div>';
       		wrapper.append(html);
       		
       		var item = [];
       		item['id'] = div.attr('id');
       		item['category'] = div.attr('category');
       		item['name'] = div.attr('name');
       		currentSlideShow.push(item);
       	});
       	
       	
       	$(".gallery div").mouseover(function() {
       		$(this).addClass('over');       	
       	});
       	
       	$(".gallery div").mouseout(function() {
       		$(this).removeClass('over');       	
       	});
       	
       	if (play == true) slideShowActive = true;
       	playNextItem();
	}

	var showGallery = function(category) {
		var wrapper = $('#wrapper');
		wrapper.empty();
		wrapper.addClass('gallery');
		$("#images").children().each(function(){
       		var div = $(this);
       		
       		if ( div.attr('category') == category ) {
       			var html = '<div id="' + div.attr('id') + '" class="galleryItem"><img src="/images/' + category + '/' + div.attr('id') + '/thumb.jpg"/></div>';
       			wrapper.append(html);
       		} else {
       			div.empty();
       		}
       		
       	});
       	
       	$(".gallery div").mouseover(function() {
       		$(this).addClass('over');       	
       	});
       	
       	$(".gallery div").mouseout(function() {
       		$(this).removeClass('over');       	
       	});
       	
       	$('.galleryItem').bind('click', function(event) {
       		target = $(getEventTarget(event));
       		
      		
       		if ( target[0].nodeName != "DIV" ) {
       			target = target.parent();
       		}
       	
            playItem(target.attr('id'));
        });
       	
       	startGallery(category);
	}
		
	var startGallery = function(category) {
		currentSlideShow = [];	
		currentItem = -1;
		galleryCategory = category;
		$("#images").children().each(function(){
       		var div = $(this);
       		if ( div.attr('category') == category ) {
       			var item = [];
       			item['id'] = div.attr('id');
       			item['name'] = div.attr('name');
				item['category'] = category;
       			currentSlideShow.push(item);
       		} 
       	});
       	
       	porfolio = true;
       	
       	slideShowActive = false;
       	playNextItem();
       	
       	$('.gallery').hide();
        $('#slideshow').show();
       	$('#slideControle').show();
       	$('#showGallery').show();
	}
	
	var playItem = function(item) {
		
		var number = 0;
		$("#images").children().each(function(){
       		var div = $(this);
       		if ( div.attr('category') == galleryCategory) {
       			number++;
       			if(div.attr('id') == item) {
       				currentItem = number - 1;
       			}       		
       		}
       	});
       	
       	$('.gallery').hide();
        $('#slideshow').show();
       	$('#slideControle').show();
       	$('#showGallery').show();
	
		$('#slideshow').empty();
		var html = '<div class="slide" source="/images/' + galleryCategory + '/' + item + '/big.jpg"></div>';
		$('#slideshow').append(html);
		
      	$("#slideshow .slide").loadImage();	
      	
      	//$('#slide-text').html(currentSlideShow[currentItem]['name']);
	}
	
	var playNextItem = function() {
		if ( !slideShowActive ) {
			clearTimeout(slideTimer);
		}
		
		$('.gallery').hide();
		
		if ( currentItem == -1 ) {
			currentItem = 0;
		} else {
			if ( (currentItem + 1) == currentSlideShow.length ) {
				currentItem = 0;
			} else {
				currentItem++;
			}			
		}
		
		if ($('#slideshow').length > 1 ) {
			$('.slide').animate({ opacity: 0 }, 300, function() {
				$('#slideshow').empty();
				var html = '<div class="slide" source="/images/' + currentSlideShow[currentItem]['category'] + '/' + currentSlideShow[currentItem]['id'] + '/big.jpg"></div>';
				$('#slideshow').append(html);
				$("#slideshow .slide").loadImage();	
			});		
		} else {
			$('#slideshow').empty();
			var html = '<div class="slide" source="/images/' + currentSlideShow[currentItem]['category'] + '/' + currentSlideShow[currentItem]['id'] + '/big.jpg"></div>';
			$('#slideshow').append(html);
			$("#slideshow .slide").loadImage();	
		}
		
		if ( !slideShowActive ) $('#slide-text').html('');
		if ( slideShowActive || porfolio ) $('#slide-text').html(currentSlideShow[currentItem]['name']);
		if ( slideShowActive ) slideTimer=setTimeout("app.next()",slideTime);	
	}
	
	var playPreviousItem = function() {
				
		$('.gallery').hide();
		
		if ( currentItem == 0 ) {
			currentItem = currentSlideShow.length - 1;
		} else {
			currentItem--;
		}			
				
		if ($('#slideshow').length > 1 ) {
			$('.slide').animate({ opacity: 0 }, 300, function() {
				$('#slideshow').empty();
				var html = '<div class="slide" source="/images/' + currentSlideShow[currentItem]['category'] + '/' + currentSlideShow[currentItem]['id'] + '/big.jpg"></div>';
				$('#slideshow').append(html);
				
				$("#slideshow .slide").loadImage();	
			});		
		} else {
			$('#slideshow').empty();
			var html = '<div class="slide" source="/images/' + currentSlideShow[currentItem]['category'] + '/' + currentSlideShow[currentItem]['id'] + '/big.jpg"></div>';
			$('#slideshow').append(html);
			
			$("#slideshow .slide").loadImage();			
		}
		
		$('#slide-text').html(currentSlideShow[currentItem]['name']);
	}
	
	var showGalleryToggle = function () {
		$('#slideshow').hide();
       	$('#slideControle').hide();
       	$('#showGallery').hide();
   		$('.gallery').show();
	}

    var Ba = {
        init: function(page){ 
        	addEventListeners();
        	
        	if ( page != "contact" && page != "info") playSlideShow(false); 
        },
        next: function() {
        	playNextItem();
        }
    }
    return Ba;
}
