﻿/* ================================ */
/*	Plugin:		WiselySlider 0.2    */
/* 	Date: 		2011-02-23		    */
/*	Company: 	Wisely AB		    */
/*	Author: 	Chris Lind		    */
/* ================================ */
/* 	   No license or copyright.     */
/* 	  Screw around all you want!    */
/* ================================ */

(function ($) {

    $.fn.extend({

        slider: function (settings) {
            var defaults = {
                active: 0,
                width: this.width(),
                speed: 800, //Animation speed
                auto_play: false,
                auto_play_interval: 4000, //Slideshow time interval
                slideshow_wait_interval: 7000, //Time for slideshow to start up again
                use_arrows: false
            }

            settings = $.extend(defaults, settings);

            var active = settings.active;
            var total_length = $(this).children('#images').children().length;


            $(this).children('#images').css({ 'width': ($(this).children('#images').children().length * settings.width) + 'px', 'position': 'absolute' });

            $('#descriptions').append('<ul id="toggles"></ul>');

            if (settings.use_arrows)
                $('#descriptions').children('#toggles').append('<li id="prev"><a href="javascript:void(0);">' + i + '</a></li>');

            for (var i = 0; i < $(this).children('#images').children().length; i++) {
                $('#descriptions').children('#toggles').append('<li id="toggle' + i + '"><a href="javascript:void(0);">' + i + '</a></li>');
                if (i == 0) {
                    $('#description').children('#toggles li:last-child a').addClass('active');
                } else if (i == $(this).children('#images').children().length - 1) {
                    $('#descriptions').children('#toggles li:last-child').css({ 'padding-right': '0' });
                }
            }

            if (settings.use_arrows)
                $('#descriptions').children('#toggles').append('<li id="next"><a href="javascript:void(0);">' + i + '</a></li>');

            $('#toggles li').click(function () {

                clearInterval(slideshow_interval);
                clearInterval(wait_interval);
                wait_interval = setInterval(startSlideShow, settings.slideshow_wait_interval);

                if (this.id != 'next' && this.id != 'prev') {
                    var li_id = this.id.split('toggle')[1];

                    if (active != li_id) {
                        showImage(li_id);
                    }
                } else {
                    if (this.id == 'next')
                        nextslide()
                    else
                        prevslide()
                }
            });

            // Set active item
            var start_num = active + 1;

            if (settings.use_arrows)
                start_num++;

            var start_left = -(active * settings.width);

            $('#toggles li:nth-child(' + start_num + ') a').addClass('active');

            $('#images').css({ left: start_left + "px" }, settings.speed);

            // var left_offset = ($(this).children('#toggles').children().outerWidth() - $(this).children('#toggles').children().width()) / 2; //Because of the last items padding, to center #toggles
            // $(this).children('#toggles').css({ 'width': (($(this).children('#toggles').children().length * $(this).children('#toggles').children().outerWidth())) + 'px', 'left': ($(this).outerWidth() / 2 - $('#toggles').outerWidth() / 2) + left_offset });

            function showImage(rawID) {

                active = parseInt(rawID);

                var _id = parseInt(rawID);

                var amountLeft = 0;

                if (_id != 0)
                    amountLeft = -(_id * settings.width);

                $('#toggles li a').removeClass('active');

                var child_num = _id + 1;

                if (settings.use_arrows) //If we have a previous arrow in front of everything
                    child_num++;

                $('#toggles li:nth-child(' + child_num + ') a').addClass('active');

                $('#images').stop().animate({ left: amountLeft + "px" }, settings.speed);
            }

            wait_interval = null;
            slideshow_interval = null;

            if (settings.auto_play == true)
                startSlideShow();


            function startSlideShow() {

                clearInterval(wait_interval);
                clearInterval(slideshow_interval);

                slideshow_interval = setInterval(nextslide, settings.auto_play_interval);
            }

            function nextslide() {
                if (active == total_length - 1)
                    active = 0;
                else
                    active++;

                showImage(active);
            }

            function prevslide() {
                if (active == 0)
                    active = total_length - 1;
                else
                    active--;

                showImage(active);
            }

        }
    });
})(jQuery);


function initAccordion(cls, im) {
    var element = "ul." + cls;
    $(element + ' ul').hide();
    $.each($(element), function () {
        $('#' + this.id + '.expandfirst ul:first').show();
        if (im)
            SetImage(this.getElementsByTagName("li")[0]);
    });
    $(element + ' li a').click(
function () {
    var checkElement = $(this).next();
    var parent = this.parentNode.parentNode.id;
    if (im)
        SetImage(this.parentNode);
    if ($('#' + parent).hasClass('noaccordion')) {
        $(this).next().slideToggle('normal');
        return false;
    }
    if ((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        if ($('#' + parent).hasClass('collapsible')) {
            $('#' + parent + ' ul:visible').slideUp('normal');
        }
        return false;
    }
    if ((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('#' + parent + ' ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
    }
}
);
}

function SetImage(el) {
    var details = el.getElementsByTagName("li")[0];
    if (details.getElementsByTagName("img").length == 0) {
        var img = new Image();
        if (details.id)
            img.src = "images/logos/" + details.id.replace("_", "");
        else
            img.src = "images/logos/anonymous.png";
        details.insertBefore(img, details.firstChild);
        var options = GetItemsByClassName(el, "p", "vc");
        if (options.length != 0) {
            var qrlink = default_New("a", options[0], "", "Scanna till mobiltelefon");
            qrlink.href = "#";
            qrlink.onclick = function () { var parent = this.parentNode; if (parent.getElementsByTagName("img").length == 0) { GenerateQR(parent, el.id); }; return false; };
        }
    }
}

function GetItemsByClassName(root, tag, css) {
    var children = root.getElementsByTagName(tag);
    var foundCss = new Array();
    for (var i = 0; i < children.length; ++i) {
        var current = children[i];
        var cssName = current.className;
        if ((cssName) && (cssName == css))
            foundCss.push(current);
    }
    return foundCss;
}

