var barcode = new Object();
var legend = new Object();
var caption = new Object();
var windowWidth, windowHeight;


// barcode /////////////////////////////////////////////////////////////////////////////////
// config parameters
barcode.allSets = new Array();
barcode.allImages = new Array();
barcode.step = 400;
barcode.isAnimating = false;
barcode.canOpen = true;
barcode.backgroundColor = '#000000';
barcode.activeColor = '#999999';
barcode.inactiveColor = '#000000';
barcode.lineThickness = 1;
barcode.lineSpacing = 2;
barcode.imageSpacing = 2;
//barcode.activeTags = ['professional', 'advertising'];
barcode.activeTags = ['experimental'];
barcode.correction = 300;
barcode.canOpen = true;

barcode.reset = function()
{
    barcode.canOpen = false;
    // unbind all click events
    $('#barcode li img').hide();

    $('li', barcode.el).removeAttr('style').removeClass('active').unbind().addClass('notActive').each(function(i)
    {
        if (barcode.allSets.length > 0)
        {
            $(this).width(barcode.allSets[i].smallWidth);
            $(this).stop(true, true);

        }

    });
    //end rev14

    // show/hide scroll arrows
    $('#scrollRight, #scrollLeft').fadeOut();

}
barcode.ini = function()
{

    barcode.reset();
    $('#barcodeContainer img').css('display', 'inline');
   
    // reset caption
    $('#caption span').text('');
    //reset legend nav
    $('#legend li').removeClass('active opened');
    // initialise useful internal globals
    barcode.allSets = new Array();
    barcode.allImages = new Array();
    barcode.newIndex = 0;
    barcode.mouseX = 0;
    barcode.startX = 0;
    barcode.startLength = 0;
    barcode.myInterval = 0;
    barcode.fullWidth = 0;
    barcode.currentLength = 0;
    barcode.imageIndexInSet = 0;
    barcode.middleX = Math.round(windowWidth / 2);
    barcode.stageRight = windowWidth;
    barcode.CurrentSet = 0;
    barcode.currentImageIndex = 0;
    barcode.el.css({
        left: barcode.startX
        //,width: windowWidth
    });
}
barcode.move = function(targetPos)
{
    var currentPosX = barcode.el.offset().left;
    //$.log('current pos x before move: ' + currentPosX);
    var modifier = 0;

    if (typeof targetPos == 'string')
    {
        switch (targetPos)
        {
            case 'left':
                //modifier = Number(barcode.step);
                barcode.imageIndexInSet -= 1;
                //$.log('left called');
                break;

            case 'right':
                //modifier = Number(0 - barcode.step);
                barcode.imageIndexInSet += 1;
                //$.log('right called');
                break;
        }

        barcode.imageIndexInSet = (barcode.imageIndexInSet >= barcode.allSets[barcode.currentIndex].images.length) ? barcode.allSets[barcode.currentIndex].images.length - 1 : barcode.imageIndexInSet;
        barcode.imageIndexInSet = (barcode.imageIndexInSet <= 0) ? 0 : barcode.imageIndexInSet;
        //$.log('barcode.imageIndexInSet= ' + barcode.imageIndexInSet);
        modifier = barcode.allSets[barcode.currentIndex].images[barcode.imageIndexInSet]._width;
        //$.log('modifier= ' + modifier);

        var newPosX = (targetPos == 'left') ? currentPosX + modifier : currentPosX - modifier;
        newPosX = (newPosX < (barcode.startX - barcode.currentLength)) ? (barcode.startX - barcode.currentLength) : newPosX;
        newPosX = (newPosX > barcode.startX) ? barcode.startX : newPosX;
    }
    else
    {
        // targetPos is an Int, corresponding to the imageSet index
        //makes sure we do not have a negative index
        targetPos = (targetPos < 0) ? 0 : targetPos;
        //makes sure we do not have a higher value than max index
        targetPos = (targetPos > (barcode.allSets.length - 1)) ? (barcode.allSets.length - 1) : targetPos;
        var newPosX = barcode.startX - (barcode.allSets[targetPos]._x + (barcode.correction));
    }

    // ACTUALLY MOVES THE STRIP
    if (barcode.isAnimating == false)
    {
        barcode.isAnimating = true;
        //$.log('Barcode is now being sent to left = ' + newPosX);
        barcode.el.animate({
            left: newPosX + 'px'
        }, 1000, function()
        {
            barcode.isAnimating = false;

            barcode.showArrows();

        });
    } else
    {
        //$.log('Barcode is animating, cannot interfere! ');
    }
    //$debugging.html(message);
}
barcode.showArrows = function()
{
    // show/hide scroll arrows
    var barPosition = barcode.currentLength + barcode.el.offset().left;
    //$.log('arrow should hide? barPosition= ' + barPosition);
    if (barPosition > barcode.stageRight)
    {
        $('#scrollRight').fadeIn();
        //$.log('arrow right should appear');
    }
    else
    {
        $('#scrollRight').fadeOut();
        //$.log('arrow right should NOT appear');
    }
    if (barcode.el.offset().left < 0)
    {
        $('#scrollLeft').fadeIn();
        //$.log('arrow left should appear');
    }
    else
    {
        $('#scrollLeft').fadeOut();
        //$.log('arrow left should NOT appear');
    }
}


barcode.draw = function()
{
    // generate barcode ////////////////////
    var imageIndexCounter = 0;
    barcode.el.startWidth = 0;
    $('#barcode li').each(function(z)
    {
        var $thisLI = $(this);
        var $thisImages = $('img', $thisLI);
        var smallWidth = $thisLI.attr('width');

        // activate/disactivate this image set according to currently activated tags
        var tags = $thisLI.attr('tags').split(',');
        for (var y = 0; y < tags.length; y++)
        {
            if (jQuery.inArray(tags[y], barcode.activeTags) !== -1)
            {
                $thisLI.removeClass('notActive').addClass('active');
                break;
            }
        }

        var setObj = new Object();
        setObj.ID = z;
        setObj.isOpen = false;
        setObj.smallWidth = smallWidth * barcode.lineThickness;

        $thisLI.css({
            width: setObj.smallWidth,
            marginRight: barcode.lineSpacing
        });

        setObj._x = Number($thisLI.offset().left);
        // //$.log('setObj' + z + '._x= ' + setObj._x);
        setObj.fullWidth = Number(0);
        setObj.description = $thisLI.attr('description');
        setObj.images = new Array();
        var tempX = 0;
        $thisImages.each(function(f)
        {
            var $thisImg = $(this);
            $thisImg.css({
                left: tempX,
                display: 'none',
                borderLeft: barcode.imageSpacing + 'px solid ' + barcode.backgroundColor
            }).attr('title', '(' + $thisLI.attr('title') + ' ( picture ' + (f + 1) + ' )');

            var imgObj = new Object();
            imgObj._width = Number($thisImg.attr('width'));
            imgObj._x = Number(tempX);

            tempX += Number(imgObj._width + barcode.imageSpacing);

            imgObj.ID = z;
            imgObj.globalIndex = imageIndexCounter;

            setObj.fullWidth += Number(imgObj._width + barcode.imageSpacing);
            setObj.images.push(imgObj);

            barcode.allImages.push(imgObj);

            barcode.fullWidth += setObj.fullWidth;

            imageIndexCounter++;
        });
        barcode.currentLength += Number(setObj.smallWidth + barcode.lineSpacing);
        barcode.allSets.push(setObj);
        //$this.append('<span class="digit">' + setObj.images.length + '</span>');

        // END LOOPING THROUGH LIs / IMAGESETS
    }).not('notActive').toggle(
    function()
    {
        var $thisLI = $(this);
        var thisIndex = $('#barcode li').index(this);
        if ($thisLI.hasClass('notActive') === false)
        {
            // //$.log("$thisLI.hasClass('notActive') = " + $thisLI.hasClass('notActive').toString());

            caption.defaultText = $thisLI.attr('description');
            caption.refresh();

            // var thisIndex = $('#barcode li').index(this);
            barcode.currentIndex = thisIndex;
            barcode.imageIndexInSet = 0;
            openImageSet(thisIndex);
            $('#legend li.opened').removeClass('active');
            $('#legend li').eq(thisIndex).addClass('opened active');

        }
    },
    function()
    {
        var $thisLI = $(this);
        var thisIndex = $('#barcode li').index(this);
        closeImageSet(thisIndex);
        barcode.currentIndex = thisIndex;
        barcode.imageIndexInSet = 0;

        $('#legend li').eq(thisIndex).removeClass('active');

    }).hoverIntent({
        sensitivity: 1,
        interval: 200,
        over: function()
        {
            var thisIndex = $('#barcode li').index(this);
            if ($(this).hasClass('notActive') == false)
            {
                if (barcode.allSets[thisIndex].isOpen == false)
                {

                    $(this).animate({
                        width: '+=40px'
                    }, 1000);

                    var tempBackgroundImg = $('img', $(this)).eq(0).attr('src');
                    $(this).css({
                        backgroundColor: 'white'
                        /*						,backgroundImage: 'url(' + tempBackgroundImg + ')',
                        backgroundPosition: 'top left',
                        backgroundRepeat: 'no-repeat'*/

                    });
                }
                caption.refresh($(this).attr('description'));
            }
        },
        timeout: 500,
        out: function()
        {
            var thisIndex = $('#barcode li').index(this);
            if ($(this).hasClass('notActive') == false)
            {
                if (barcode.allSets[thisIndex].isOpen == false)
                {

                    $(this).animate({
                        width: barcode.allSets[thisIndex].smallWidth
                    }, 1000).css({
                        backgroundColor: barcode.activeColor,
                        backgroundImage: 'none'
                    });

                }
                caption.refresh();
            }
        }
    });
    barcode.currentLength += barcode.correction;
    barcode.startX = barcode.middleX - Math.round(barcode.currentLength / 2) + (barcode.correction / 2);
    barcode.el.css({
        left: barcode.startX
        , width: barcode.currentLength
    });
    // caption object  /////////////////////////////////////////////
    $('#caption').fadeIn('slow');
    barcode.canOpen = true;
    legend.ini();
    /*
    $('body').append('<div id="trueMiddle" style="width:1px;position:absolute;z-index:1000;background:red;height:100%"></div>');
    $('#trueMiddle').css({ left: barcode.middleX });
    //*/
}



// legend /////////////////////////////////////

legend.ini = function()
{
    // $('#legendContainer').width($(document).width()).fadeIn(3000);
    $('#legendContainer h2').css({ color: barcode.activeColor });
    var legendHtml = '';
    var legendWidth = 700;
    var percentInPixel = Math.round(legendWidth / $('#barcode img').length);
    $('#barcode li').each(function(l)
    {
        var numOfImg = $('img', $(this)).length;
        var thisWidth = Number(numOfImg * percentInPixel);
        var tags = $(this).attr('tags').split(',');
        var liClass = 'notActive';
        for (var y = 0; y < tags.length; y++)
        {
            if (jQuery.inArray(tags[y], barcode.activeTags) !== -1)
            {
                liClass = 'activated';

                break;
            }
        }
        legendHtml += '<li class="' + liClass + '" style="width:' + thisWidth + 'px"><span>' + $(this).attr('description') + '</span></li>';

    });

    $('#legend').html(legendHtml); //.css({

    //      borderBottom: '1px solid #353535'
    // });
    $('#legend li').hoverIntent(function()
    {
        caption.refresh($('span', $(this)).text());
        //$(this).css('cursor', 'pointer');
    }, function()
    {
        caption.refresh();
    }).bind('click', function()
    {
        var $thisLI = $(this);
        caption.defaultText = $('span', $(this)).text();
        caption.refresh();
        $('#legend li.opened').removeClass('active');
        $thisLI.addClass('opened active');

        var thisIndex = $('#legend li').index(this);
        barcode.currentIndex = thisIndex;
        barcode.imageIndexInSet = 0;
        openImageSet(thisIndex);

    });
}


// CAPTION //////////////////////////:


caption.defaultText = '';
caption.refresh = function(txt)
{
    txt = (txt == '' || typeof txt == 'undefined') ? caption.defaultText : txt;
    $('#caption span').fadeOut(100, function()
    {
        $('#caption span').text(txt).fadeIn(300);
    });
}
