var currentNews = 0;
var newsCount = 0;

var images = [];
var currentImageID = 0;
var currentImageSrc = '';
var counter = 0;

$(document).ready(function () {

    makeAutoNews('sliderElements', 'sliderBar');

    $("a[rel^='prettyPhoto']").prettyPhoto();
    $.galleryUtility.zoomIn = function () {

        ///*
        images = [];
        currentImageID = 0;
        currentImageSrc = $(this).attr('src');
        counter = 0;

        $('div.fabricGallery_Box img').each(function () {
            var tmpSrc = $(this).attr('src');

            if (tmpSrc == currentImageSrc) {
                currentImageID = counter;
            }

            tmpSrc = tmpSrc.replace("/image,200,200/", "/");
            images.push(tmpSrc);
            counter++;
        });

        $.prettyPhoto.open(images);
        $.prettyPhoto.changePage(currentImageID);
        //*/

        /*
        var src = $(this).attr('src');
        src = src.replace("/image,200,200/", "/");
        $.prettyPhoto.open(src);
        */
    };

    $('div.fabricGallery_Box img').slidingGallery({
        container: $('div.fabricGallery_Box'),
        Lwidth: 120,
        Lheight: 120,
        gutterWidth: 5
    });

    /* FB Widget */
    $('#facebookIcon').toggle(
        function () {
            $('#facebookWidget').animate({ right: '0px' }, 200);
        },
        function () {
            $('#facebookWidget').animate({ right: '-240px' }, 200);
        }
    );
    $('#facebookIcon').tipsy({ title: function () { return this.getAttribute('fb-title'); }, gravity: 'se' });
    $('#facebookWidget').stop().animate({ top: ($(window).scrollTop() + 55) + 'px' }, 400);
    $(window).scroll(function () {
        //$(window).scrollTop()
        $('#facebookWidget').stop().animate({ top: ($(window).scrollTop() + 55) + 'px' }, 400);
    });

    //Newsletter comment
    //$('#newsletter_mail, #newsletter_submit').tipsy({ title: function () { return 'Zapraszamy wkrótce!'; }, gravity: 'se' });
    

});

function makeAutoNews(containerID, barID) {
    var element_id = 0;
    var is_first = true;

    //var size = 0;

    $('#' + containerID + ' img').each(function () {
        $(this).attr('id', 'ano_' + element_id);

        if ($(this).attr('url') != '') {
            $(this).css('cursor', 'pointer');
            $(this).click(function () { document.location.href = $(this).attr('url'); });
        }

        //size = size + $(this).width();

        if (is_first == true) {
            insertNewsButton(containerID, barID, 'ano_' + element_id, 1, element_id);
            is_first = false;
            $('#ano_' + element_id).addClass('act');
        }
        else {
            insertNewsButton(containerID, barID, 'ano_' + element_id, 0, element_id);
            $('#ano_' + element_id).addClass('act');
        }
        element_id++;
        newsCount++;
    });

    $('#' + containerID).scrollTo($('#ano_0'), 0);
    $('#ano_0').addClass('act');


    setInterval(function () {
        autoSetNews(containerID, barID);
    }, 8000);
}

function autoSetNews(containerID, barID) {
    if (newsCount > 1) {
        if ((currentNews + 1) == newsCount) {
            currentNews = 0;
            clearSel(barID);
            $('#sel_ano_0').addClass('act');
            $('.opis').html($('#ano_0').attr('desc'));
            $('#polecamy_title').html($('#ano_0').attr('title'));
            $('#polecamy_title2').html($('#ano_0').attr('title2'));
            $('#' + containerID).scrollTo($('#ano_0'), 500);
        }
        else {
            currentNews++;
            clearSel(barID);
            $('#sel_ano_' + currentNews).addClass('act');
            $('.opis').html($('#ano_' + currentNews).attr('desc'));
            $('#polecamy_title').html($('#ano_' + currentNews).attr('title'));
            $('#polecamy_title2').html($('#ano_' + currentNews).attr('title2'));
            $('#' + containerID).scrollTo($('#ano_' + currentNews), 500);
        }
    }
}

function insertNewsButton(containerID, barID, imageID, isSelected, objID) {
    if (isSelected) {
        $('#' + barID).append('<a id="sel_' + imageID + '" class="act" onfocus="blur();" onclick="return false;" href="javasrcipt:;" ></a>');
        $('#sel_' + imageID).click(function () {
            clearSel(barID);
            currentNews = objID;
            $(this).addClass('act');
            $('.opis').html($('#' + imageID).attr('desc'));
            $('#polecamy_title').html($('#' + imageID).attr('title'));
            $('#polecamy_title2').html($('#' + imageID).attr('title2'));
            $('#' + containerID).scrollTo($('#' + imageID), 500);
        });
    }
    else {
        $('#' + barID).append('<a id="sel_' + imageID + '" onfocus="blur();" onclick="return false;" href="javasrcipt:;" ></a>');
        $('#sel_' + imageID).click(function () {
            currentNews = objID;
            clearSel(barID);
            $('.opis').html($('#' + imageID).attr('desc'));
            $('#polecamy_title2').html($('#' + imageID).attr('title2'));
            $('#polecamy_title').html($('#' + imageID).attr('title'));
            $(this).addClass('act');
            $('#' + containerID).scrollTo($('#' + imageID), 500);
        });
    }
}

function clearSel(barID) {
    $('#' + barID + ' a').each(function () {
        $(this).removeClass('act');
    });
}
