var _gallery = {uid:"gallery", width:980, height:725, currentImage:1, length:0, loaded:0, interval:''};

$().ready(function() {
    $("#gallery #images div.hoveredCursor").each(function() {
        $(this).css('display', 'none');
        $(this).bind('click', _navigation.projectHandler);
	});
    
    $("#container > #gallery").bind('mouseenter', _gallery.mouseHandler);
    $("#container > #gallery").bind('mouseleave', _gallery.mouseHandler);

});

_gallery.mouseHandler = function(e) {
    var el = $(this).find('> #images > #image_'+_gallery.currentImage+' > div.hoveredCursor');
    var bg = el.find('> .gallery_background');
    var elp = el.find('> p');

    switch(e.type) {
        case 'mouseleave':
            _gallery.slideshow('reset');
            elp.animate({'opacity': '0'}, 250, "easeOutExpo", function() {elp.css('filter', '');});
            bg.animate({'opacity': '0'}, 250, "easeOutExpo", function(){
                el.css("display", 'none');
                /*IE7*/
                bg.css('filter', '');
            });
            break;
        case 'mouseenter':
            _gallery.slideshow('pause');
            /*IE7*/
            bg.css('opacity', '0');
            elp.css('opacity', '0');
            el.css('display', "block");
            elp.animate({'opacity': '1'}, 250, "easeOutExpo", function() {elp.css('filter', '');});
            bg.animate({'opacity': '0.65'}, 250, "easeOutExpo");
            break;
    }
};

_gallery.slideshow = function(command) {
	switch(command) {
		case "start":
		case "reset":
			clearInterval(_gallery.interval);
            _gallery.interval = setInterval(function() {_gallery.slideImage({data:{dir:'right', src:'interval'}});}, 4000);
			break;
		case "pause":
			clearInterval(_gallery.interval);
			break;
	}
};

_gallery.loadContent = function(url) {
	var src, title;
	var imageURLArray = [];
	
	_gallery.currentImage = -1;
	
	_gallery.length = 0;

	$("#container > #gallery > #images div.image").each(function() {
		_gallery.length++;

		src = $(this).find('img').attr('alt');
		title = this.getAttribute('title');

		if(title === url) {
			_gallery.currentImage = _gallery.length-1;
		}

		imageURLArray.push(src);
	});
	
	_gallery.currentImage = (url==="" || _gallery.currentImage === -1)? 1: _gallery.currentImage;

	$("#gallery").bind("IMAGEPRELOADER_LOADED"+"_"+_gallery.uid, _gallery.imagesLoaded);
    $("#gallery").imagePreloader(_gallery.uid, $("#container"), imageURLArray, false, "MODE_QUEUED");

    if(_gallery.length === 0) {
        $("#container > #gallery > #galleryRight").css('visibility', 'hidden');
    }
};

_gallery.slideImage = function(e) {
	var next;
	var img = $("#container > #gallery > #images div#image_"+_gallery.currentImage);

    switch(e.data.dir) {
        case 'left':
            if(_gallery.currentImage-1>=0) {
                _gallery.currentImage--;
            } else {
                _gallery.currentImage = _gallery.loaded;
            }
            break;
        case 'right':
            if(_gallery.currentImage+1<_gallery.loaded) {
                _gallery.currentImage++;
            } else {
                _gallery.currentImage = 1;
            }
            break;
    }

	next = $("#container > #gallery > #images div#image_"+_gallery.currentImage);

    img.stop(true).animate({"opacity": 0}, 1000, function() {$(this).css('display', 'none');});
    img.css('z-index', '6');
    next.css('display', '');
    next.css('z-index', '7');
    next.css('opacity', '0');
    next.stop(true).animate({"opacity": 1}, 1000, function() {/*IE7*/$(this).css('filter', '');});
};

// @param e: Event object
// @param id: id of the image that was just loaded
// @param src: the source of the Image that was just loaded
// @param img: the Image object that was just loaded
// function imagesLoaded(e, id, img) {
_gallery.imagesLoaded = function(e, id, src) {
    _gallery.loaded++;

	var imgclass = $("#container > #gallery > #images div.image").has("img[alt=\""+src+"\"]");
	var img = imgclass.find('img');
	
	img.attr('src', src);
	img.removeClass('loadingGif');
	imgclass.attr('id', 'image_'+id);
	
    if(id !== _gallery.currentImage) {
        imgclass.animate({'opacity': '0'}, 0, function() {$(this).css('display', 'none');});
	} else {
	    imgclass.css('display', 'block');
        imgclass.css('z-index', '7');
	}

    if(_gallery.loaded == 2) {
        _gallery.slideshow('start');
    }
};
