//Braun Galleria, Version 1.0.6
//Copyright (c) Feb 1, 2010 adardesign.com
//Braun Galleria is freely distributable under the terms of an MIT-style license
//This means you are free to use the code, but please leave this copyright notice intact


var startInterval
$(document).ready(function () {
    var rV = {}
    // SETTING GLOBAL VAR'S
    rV.container = $(".adGalleryImg");
    rV.containerEl = rV.container.children();
    rV.containerElSize = rV.containerEl.length;
    rV.last = 0;
    rV.next = 1;
    rV.looped = false;
    rV.speed = 1500;
    rV.interval = 5000;
    rV.clickFade = 300;
    rV.isPaused = false;

    rV.setUpNav = function () {
        navStrip = $("<div/>").addClass("adGalleryStrip");
        navContainer = $("<span>").addClass("adGalleryNav");
        for (i = 1; i < rV.containerElSize + 1; i++) {
            navContainer.append($("<a href='#'>" + i + "</a>"))
        }

        rV.pauseButton = $("<span>")
				 						.addClass("adGalleryPause")
										.html("<a href='#' class='pause' />")
										.click(function () {
										    if (!rV.isPaused) {
										        clearInterval(startInterval);
										        rV.pauseButton.children().removeClass("pause").addClass("play");
										        rV.isPaused = true;
										    }
										    else {
										        rV.slideInterval();
										        startInterval = setInterval(rV.slideInterval, rV.interval);
										        rV.pauseButton.children().removeClass("play").addClass("pause");
										        rV.isPaused = false;
										    }
										    return false
										}).appendTo(navStrip);

        var navFunc = navContainer.children().eq(0).addClass("active").end().end().find("a").click(function () {
            clearInterval(startInterval)
            $(this).siblings().removeClass("active");
            $(this).addClass("active");
            indexOfClicked = navFunc.index($(this));

            //indexOfClicked = navFunc.index($(this));
            //changed this line for smoother transition
            rV.containerEl.eq(indexOfClicked === rV.last ? rV.containerElSize + 100 : rV.last).fadeOut(rV.clickFade);
            rV.containerEl.eq(indexOfClicked).fadeIn(rV.clickFade, function () { rV.containerEl.eq(indexOfClicked).css("opacity", 1) });

            rV.last = indexOfClicked;
            rV.next = indexOfClicked == rV.containerElSize - 1 ? 0 : indexOfClicked + 1;
            rV.pauseButton.children().removeClass("pause").addClass("play");
            rV.isPaused = true;
            return false

        })
        navStrip.append(navContainer);
        rV.container.append(navStrip);
        navContainer.children().last().addClass("last")
    }



    rV.slideInterval = function () {
        /*rV.container.find("img:gt(0)").each(function (i, e) {
            var el = $(e);
            el.attr("src", el.attr("rel"));
        })*/
        rV.containerEl.eq(rV.last).fadeOut(rV.speed);
        rV.containerEl.eq(rV.next).fadeIn(rV.speed);
        $(".adGalleryNav a").removeClass("active")
        $(".adGalleryNav a").eq(rV.next).addClass("active")
        rV.last++
        rV.next++

        if (rV.looped) {
            rV.looped = false
            rV.last = 0;
            rV.next = 1;

        }
        if (rV.last == rV.containerElSize - 1) {
            rV.last = rV.containerElSize - 1;
            rV.next = 0;
            rV.looped = true
        }
    }


    rV.init = function () {

        //rV.containerEl.filter(":first").css("display","block");
        //rV.containerEl.filter(":not(:first)").css("display","none");
        rV.containerEl.hide(0).first().fadeIn(500);
        rV.setUpNav()
        startInterval = setInterval(rV.slideInterval, rV.interval);

    }
    relToSrc = function () {
        rV.container.find("img:gt(0)").each(function (i, e) {
            var el = $(e);
            el.attr("src", el.attr("rel"));
        })
    }

    rV.init();
    setTimeout(relToSrc, rV.interval / 2);
});

