﻿var bGalleryFx, bGalleryBtnFx, bGalleryPause, bGalleryFxSpeed, bGalleryAutoSlide;
var bGalleryObj, bGalleryActive, bGalleryCNext, bGalleryCPrev, bGalleryNext, bGalleryTimer;
var imagePathStart = "<img src=\"";
var imagePathEnd = "\" alt=\"\" />";

/* INIT ****************************/
function bGalleryshowActivate(galleryID) {
    // find
    bGalleryObj = j("#" + galleryID);
	 bGalleryObj.find(".bControls").css('display','block');
    bGalleryCNext = bGalleryObj.find(".bControls .controlNext");
    bGalleryCPrev = bGalleryObj.find(".bControls .controlPrev");

    // init
    bGalleryObj.find(".bContent li").css({ 'z-index': 1 }).find("img").css({ 'opacity': 0 });
    bGalleryObj.find(".bContent li:first").addClass("first");
    bGalleryObj.find(".bContent li:last").addClass("last");
    bGalleryObj.find(".bControls a").removeAttr("href");
    bGalleryCNext.find("a").click(function () { bGalleryshowNext(); });
    bGalleryCPrev.find("a").click(function () { bGalleryshowPrev(); });

    // first slide
    bGalleryActive = bGalleryObj.find(".bContent li.first");
    bGalleryshowLoadSlide(bGalleryActive);
};

/* FUNCTIONS ****************************/
function bGalleryshowNext() {
    if (bGalleryObj.find(".bContent li:animated").length == 0) {

        if (bGalleryActive.hasClass("last")) {
            bGalleryNext = bGalleryObj.find(".bContent li:first");
        } else {
            bGalleryNext = bGalleryActive.next();
        }
        bGalleryshowLoadNextSlide(bGalleryNext, 'next');

    }
}

function bGalleryshowPrev() {
    if (bGalleryObj.find(".bContent li:animated").length == 0) {

        if (bGalleryActive.hasClass("first")) {
            bGalleryNext = bGalleryObj.find(".bContent li:last");
        } else {
            bGalleryNext = bGalleryActive.prev();
        }
        bGalleryshowLoadNextSlide(bGalleryNext, 'prev');

    }
}

function bGalleryshowLoadSlide(nextSlideObj) {
    var slideImgPath = nextSlideObj.find("a").attr("rel");
    nextSlideObj.find("a").html(imagePathStart + slideImgPath + imagePathEnd);
	switch (bGalleryFx) {
		case 'slide': // SLIDE ---------------------------- 
		    //break;
		case 'fade': // FADE ---------------------------- 
		default:
		    // set NEXT to invisible
		    nextSlideObj.css({ opacity: 0 })
			.animate({
				opacity: 1,
				'z-index': 2
			}, bGalleryFxSpeed,
				function () {
					// set NEXT as ACTIVE
					bGalleryshowSetSlide(nextSlideObj);
				});
		    break;
	} // end switch

}

function bGalleryshowLoadNextSlide(nextSlideObj, direction) {
    var slideImgPath = nextSlideObj.find("a").attr("rel");
    nextSlideObj.find("a").html(imagePathStart + slideImgPath + imagePathEnd);
	switch (bGalleryFx) {
		case 'slide': // SLIDE ---------------------------- 
		    var slideWidth, activePos, newActivePos, lastActivePos;
		    slideWidth = 1280;

		    activePos = (slideWidth - 960) / 2;
		    // set NEXT to direction
		    if (direction == 'prev') {
		        newActivePos = '-' + slideWidth + 'px';
		        lastActivePos = slideWidth + 'px';
		    } else {
		        newActivePos = slideWidth + 'px';
		        lastActivePos = '-' + (slideWidth + activePos) + 'px';
		    }
		    activePos = '-' + activePos + 'px';
		    nextSlideObj.css({ left: newActivePos, opacity: 0 });

		    // slide out ACTIVE slide
		    bGalleryActive.animate({
		        left: lastActivePos,
		        opacity: 0,
		        'z-index': 1
		    }, bGalleryFxSpeed,
				function () {
				});
		    // fade in NEXT slide
		    nextSlideObj.animate({
		        left: activePos,
		        opacity: 1,
		        'z-index': 2
		    }, bGalleryFxSpeed,
					function () {
						// set NEXT as ACTIVE
						bGalleryshowSetSlide(nextSlideObj);
					});
		    break;
		case 'fade': // FADE ---------------------------- 
		default:
		    // set NEXT to invisible
		    nextSlideObj.css({ opacity: 0 });
		    // fade out ACTIVE slide
		    bGalleryActive.animate({
		        opacity: 0,
		        'z-index': 1
		    }, bGalleryFxSpeed,
				function () {
					// fade in NEXT slide
					nextSlideObj.animate({
					    opacity: 1,
					    'z-index': 2
					}, bGalleryFxSpeed,
							function () {
								// set NEXT as ACTIVE
								bGalleryshowSetSlide(nextSlideObj);
							});
				});
		    break;
	} // end switch

}

function bGalleryshowSetSlide(slideObj) {
    bGalleryActive.css({ 'z-index': 2 }).removeClass("active");
    slideObj.css({ 'z-index': 3 }).addClass("active");
    bGalleryActive = slideObj;
    // set timer
    bGalleryshowStart();
}

function bGalleryshowStart() {
    bGalleryshowStop();
    if (bGalleryAutoSlide == true) bGalleryTimer = setInterval("bGalleryshowNext()", bGalleryPause);
}

function bGalleryshowStop() {
    clearInterval(bGalleryTimer);
}

