/*
 * jQuery 'onImagesLoaded' plugin v1.1.1 (Updated January 27, 2010)
 * Fires callback functions when images have loaded within a particular selector.
 *
 * Copyright (c) Cirkuit Networks, Inc. (http://www.cirkuit.net), 2008-2010.
 * Dual licensed under the MIT and GPL licenses:
 *   http://www.opensource.org/licenses/mit-license.php
 *   http://www.gnu.org/licenses/gpl.html
 *
 * For documentation and usage, visit "http://includes.cirkuit.net/includes/js/jquery/plugins/onImagesLoad/1.1.1/documentation/"
 */
(function($){
    $.fn.onImagesLoad = function(options){
        var self = this;
        self.opts = $.extend({}, $.fn.onImagesLoad.defaults, options);

        self.bindEvents = function($imgs, container, callback){

            if ($imgs.length === 0){ //no images were in selection. callback based on options
                if (self.opts.callbackIfNoImagesExist && callback){
                    callback(container);
                }
            }
            else {
                var loadedImages = [];
                if (!$imgs.jquery){ $imgs = $($imgs); }
                $imgs.each(function(i){
                    //webkit fix inspiration thanks to bmsterling: http://plugins.jquery.com/node/10312
                    var orgSrc = this.src;
                    if (!$.browser.msie) {
                        this.src = ""; //ie will do funky things if this is here (show the image as an X, only show half of the image, etc)
                    }
                    $(this).bind('load', function(){
                        if (jQuery.inArray(i, loadedImages) < 0){ //don't double count images
                            loadedImages.push(i); //keep a record of images we've seen

                            if (loadedImages.length == $imgs.length){
                                if (callback){

                                    callback.call(container, container);

                                }
                            }
                        }
                    });
                    if (!$.browser.msie) {
                        this.src = orgSrc; //needed for potential cached images
                    }
                    else if (this.complete || this.complete === undefined){
                        this.src = orgSrc;
                        $(this).trigger('load');
                    } else {
                        $(this).trigger('load');
                    }
                });
            }
        };

        var imgAry = []; //only used if self.opts.selectorCallback exists
        self.each(function(){
            if (self.opts.itemCallback){
                var $imgs;
                if (this.tagName == "IMG"){ $imgs = this; } //is an image
                else { $imgs = $('img', this); } //contains image(s)
                self.bindEvents($imgs, this, self.opts.itemCallback);
            }
            if (self.opts.selectorCallback){
                if (this.tagName == "IMG"){ imgAry.push(this); } //is an image
                else { //contains image(s)
                    $('img', this).each(function(){ imgAry.push(this); });
                }
            }
        });
        if (self.opts.selectorCallback){
            self.bindEvents(imgAry, this, self.opts.selectorCallback);
        }

        return self.each(function(){}); //dont break the chain
    };

    //DEFAULT OPTIONS
    $.fn.onImagesLoad.defaults = {
        selectorCallback: null,        //the function to invoke when all images that $(yourSelector) encapsultaes have loaded (invoked only once per selector. see documentation)
        itemCallback: null,            //the function to invoke when each item that $(yourSelector) encapsultaes has loaded (invoked one or more times depending on selector. see documentation)
        callbackIfNoImagesExist: false //if true, the callbacks will be invoked even if no images exist within $(yourSelector).
                                       //if false, the callbacks will not be invoked if no images exist within $(yourSelector).
    };
})(jQuery);

// v0.6a

(function ($) {
	$.fn.bruendlSlider = function (options) {
		options = $.extend({
			visibleitems: 3,
			vertical: false,
			continuous: false,
			numbers: true,
			thumbs: true,
			fade: true,
			autoplay: false,
			autoplayspeed: 4000,
			animationspeed: 300,
			fadingspeed: 200,
			customeasing: "easeInOutExpo", // Choose Easing Method from JQuery Easing Plugin, "swing" and "linear" if no Easing Plugin available
			itemclass: ".sl_item",
			visiblecontainer: ".sl_visible",
			movingcontainer: ".sl_movingcontainer",
			leftarrow: ".sl_leftarrow a",
			rightarrow: ".sl_rightarrow a",
			numberscontainer: ".numbers",
			thumbscontainer: ".hiddenthumbs",
			bar: ".nav"
		}, options);
		return this.each(function () {

			// MAIN CONTAINERS

			var sl_item = $(options.itemclass, this);
			var sl_movingcontainer = $(options.movingcontainer, this);
			var sl_visiblecontainer = $(options.visiblecontainer, this);
			var sl_leftarrow = $(options.leftarrow, this);
			var sl_rightarrow = $(options.rightarrow, this);
			var sl_numberscontainer = $(options.numberscontainer, this);
			var sl_thumbscontainer = $(options.thumbscontainer, this);
			var sl_bar = $(options.bar, this);

			// FURTHER VARIABLES

			var sl_itemcount = $(sl_movingcontainer).children().length;
			var sl_itemwidth = $(sl_item).outerWidth();
			var sl_itemwidthpxpos = sl_itemwidth + "px";
			var sl_itemwidthpxneg = "-" + sl_itemwidth + "px";
			var sl_itemheight = $(sl_item).outerHeight();
			var sl_itemheightpxpos = sl_itemheight + "px";
			var sl_itemheightpxneg = "-" + sl_itemheight + "px";
			var sl_visibleitems = options.visibleitems;
			var sl_visiblewidth = sl_visibleitems * sl_itemwidth;

			var sl_currentpos = 0;
			var sl_arrowwidth = $(options.leftarrow, this).width();
			var sl_totalwidth = sl_visiblewidth;
			var sl_vertical = options.vertical;
			var sl_continuous = options.continuous;
			var sl_numbers = options.numbers;
			var sl_thumbs = options.thumbs;
			var sl_animationspeed = options.animationspeed;
			var sl_fadingspeed = options.fadingspeed;

			var arguments = new Object;
			var sl_cont_dir = "";
			var sl_aniarg_right = {};
			var sl_aniarg_left = {};

			var lastitem = false;
			var firstitem = false;

			// ************* DEFINE ARGUMENTS FOR ANIMATION *************** //

			function defineArgs(times) {
				if (sl_vertical == true) {
					$(sl_item).css({"float":"none"});
					$(sl_visiblecontainer).css({"height":sl_itemheight * sl_visibleitems});
					sl_aniarg_right["top"] = "-=" + sl_itemheight * times;
					sl_aniarg_right["queue"] = false;
					sl_aniarg_left["top"] = "+=" + sl_itemheight * times;
					sl_aniarg_left["queue"] = false;
					sl_cont_dir = "top";
				}
				else {
					sl_aniarg_right["left"] = "-=" + sl_itemwidth * times;
					sl_aniarg_right["queue"] = false;
					sl_aniarg_left["left"] = "+=" + sl_itemwidth * times;
					sl_aniarg_left["queue"] = false;
					sl_cont_dir = "left";
				}
			}

			defineArgs(1);

			// APPLY TOTAL WIDTH ON SLIDER AND VISIBLE WIDTH ON VISIBLECONTAINER,
			// HIDE ARROW IF CONTINUOUS IS FALSE

			$(sl_visiblecontainer).width(sl_visiblewidth);
			if (sl_continuous == false) {
				$(sl_leftarrow).addClass("ishidden");
			}

			// ***************** FADE FUNCTION ****************** //

			function Fade(direction, speed, times) {
				$(sl_movingcontainer).fadeOut(speed, function() {
					if (direction == "right") {
						FadeToRight(times);
					}
					else {
						FadeToLeft(times);
					}
				});
				$(sl_movingcontainer).fadeIn(speed);
			}

			/* BRUENDL */

			var heading = $(".heading span",sl_item);

			/* BRUENDL END */


			// *************** SLIDE FUNCTION ****************** //

			function Slide(direction, speed, times) {

				defineArgs(times);
				if (direction == "right") {
					arguments = sl_aniarg_right;
				}
				else {
					arguments = sl_aniarg_left;
				}
				if (firstitem == true) {
					if (sl_vertical == false) {
						$(sl_movingcontainer).css("left", sl_itemwidthpxneg);
					}
					else {
						$(sl_movingcontainer).css("top", sl_itemheightpxneg);
					}
				}

				var customeasing = options.customeasing;

				$(".stresslayer").show();

				/* BRUENDL */

				heading.hide();

				/* BRUENDL END */

				$(sl_movingcontainer).stop(false, false).animate(
						arguments, speed, customeasing, function() {

							//$(".stresslayer").hide();
							if (lastitem == true || firstitem == true) {
								if (direction == "right") {
									if (sl_vertical == false) {
										$(sl_movingcontainer).css("left", "0px");
									}
									else {
										$(sl_movingcontainer).css("top", 0);
									}
									lastitem = false;

									$(sl_item.eq(sl_itemcount)).remove();
								}
								if (direction == "left") {
									$(".sl_item:eq(0)", this).remove();
									if (sl_vertical == false) {
										var fromleft = (sl_itemwidth * (sl_itemcount - 1)) * -1;
										$(sl_movingcontainer).css("left", fromleft + "px");
									}
									else {
										var fromtop = (sl_itemheight * (sl_itemcount - 1)) * -1;
										$(sl_movingcontainer).css("top", fromtop + "px");
									}
									firstitem = false;
								}
							}

							/* BRUENDL */

							heading.fadeIn();

							/* BRUENDL END */

						}
						);
			}

			// ************** ANIMATE FUNCTION ************** // differs between slide and fade

			function Animation(direction, fade, times) {

				if (sl_continuous == false) {
					if (direction == "right") {
						if ($(sl_leftarrow).hasClass("ishidden")) {
							$(sl_leftarrow).removeClass("ishidden");
						}
					}
					else {
						if ($(sl_rightarrow).hasClass("ishidden")) {
							$(sl_rightarrow).removeClass("ishidden");
						}
					}
				}

				// Position Adjustments

				function AppendFirstItem() {
					lastitem = true;
					var firstitemtomove = $(sl_item.eq(0));
					$(firstitemtomove).clone().appendTo(sl_movingcontainer);
				}

				function PrependLastItem() {
					firstitem = true;
					var lastitemtomove = $(sl_item.eq(sl_itemcount - 1));
					$(lastitemtomove).clone().prependTo(sl_movingcontainer);

				}

				if (direction == "right") {
					if (sl_continuous == false) {
						sl_currentpos = sl_currentpos + (1 * times);
						if (sl_currentpos > 0) {
							$(sl_leftarrow).removeClass("ishidden");
						}
						if (sl_currentpos == (sl_itemcount - sl_visibleitems)) {
							$(sl_rightarrow).addClass("ishidden");
						}
					}
					else {
						if ((sl_currentpos + 1) == sl_itemcount) {
							AppendFirstItem();
						}
						sl_currentpos++;
						if (sl_currentpos == sl_itemcount) {
							sl_currentpos = 0;
						}

					}
				}
				else {

					if (sl_continuous == false) {
						sl_currentpos = sl_currentpos - (1 * times);
						if (sl_currentpos == 0) {
							$(sl_leftarrow).addClass("ishidden");
						}
					}
					else {

						if ((sl_currentpos) == 0) {
							PrependLastItem();
						}
						sl_currentpos--;
						if (sl_currentpos == -1) {
							sl_currentpos = (sl_itemcount - 1);
						}


					}
				}

				if (fade == true) {
					Fade(direction, sl_fadingspeed, times);
				}
				else {
					Slide(direction, sl_animationspeed, times);
				}

				// Thumb Adjustments

				if (sl_thumbs == true) {
					$(".thumb", sl_thumbscontainer).removeClass("current");
					$(".thumb:eq(" + (sl_currentpos) + ")").addClass("current");
				}

				// Nr Adjustments

				if (sl_numbers == true) {
					$(".nr a.current", sl_numberscontainer).removeClass("current");
					$(".nr a:eq(" + (sl_currentpos) + ")", sl_numberscontainer).addClass("current");
				}

				return false;

			}

			;

			// CLICK RIGHT ARROW

			$(sl_rightarrow).click(
					function() {
						StopInterval();
						Animation("right", false, 1);
						return false;
					}
					)

			// CLICK LEFT ARROW

			$(sl_leftarrow).click(
					function() {
						StopInterval();
						Animation("left", false, 1);
						return false;
					}
					)

			// APPEND NUMBERS

			if (sl_numbers == true) {
				for (i = 1; i <= sl_itemcount; i++) {
					$(sl_numberscontainer).append("<div class='nr'><a href='#'>" + i + "</a></div>");
				}
				$(".nr a:eq(0)", sl_numberscontainer).addClass("current");
			}

			// NUMBERS CLICK FUNCTIONS

			$(".nr a", sl_numberscontainer).bind( {

				click: function() {

					StopInterval();
					var currentnr = 0;
					for (i = 0; i < sl_itemcount; i++) {
						var thisclass = $(".nr a:eq(" + i + ")", sl_numberscontainer).attr("class");
						if (thisclass == "current") {
							var currentnr = i;
							break;
						}
					}

					$(".nr a", sl_numberscontainer).removeClass("current");
					$(this).addClass("current");

					var clickpos = 0;

					for (i = 0; i < sl_itemcount; i++) {
						var thisclass = $(".nr a:eq(" + i + ")", sl_numberscontainer).attr("class");
						if (thisclass == "current") {
							var clickpos = i;
							break;
						}
					}

					/* BRUENDL */

                    var images = new Array();

                    $('.bruendl_slider_images').each(function () {
                        images.push($(this).attr('src'));
                    });

					var currentimg = images[i];


					$("<img />").prependTo("#background_container").attr("src",currentimg);
					$('#background_container').onImagesLoad({
						selectorCallback: function() {
							$("<div class='bgimage'>").prependTo("#background_container").attr("style", "background-image: url(" + currentimg + ");");
							$("img","#background_container").remove();
							$(".stresslayer").show();
							$("#background_container .bgimage:last").fadeOut(2000, function() {
							$("#background_container .bgimage:last").remove();
								$(".stresslayer").hide();
							});
						}
					});

					/* BRUENDL END */

					var difference = currentnr - clickpos;


					difference = Math.abs(difference);

					if (currentnr > clickpos) {
						for (i = 1; i <= difference; i++) {
							Animation("left", false, difference);
						}
					}
					else {
						for (i = 1; i <= difference; i++) {
							Animation("right", false, difference);
						}
					}

					if (sl_numbers == true) {
						$(".nr a.current", sl_numberscontainer).removeClass("current");
						$(".nr a:eq(" + clickpos + ")", sl_numberscontainer).addClass("current");
					}

					sl_currentpos = clickpos;

					if (sl_thumbs == true) {
						$(".thumb", sl_thumbscontainer).removeClass("current");
						$(".thumb:eq(" + (sl_currentpos) + ")").addClass("current");
					}

					return false;

				}
			});

			// SHOW THUMBNAILS

			if (sl_thumbs == true) {
				$(sl_bar).hover(function() {
					$(sl_thumbscontainer).fadeIn("fast");
				}, function() {
					$(sl_thumbscontainer).fadeOut("fast");
				});
			}

			// THUMBNAIL CLICK FUNCTIONS

			$(".thumb a", sl_thumbscontainer).bind( {

				click: function() {

					StopInterval();
					var currentthumb = 0;
					for (i = 0; i < sl_itemcount; i++) {
						var thisclass = $(".thumb:eq(" + i + ")", sl_thumbscontainer).attr("class");
						if (thisclass == "thumb current") {
							var currentthumb = i;
							break;
						}
					}

					$(".thumb", sl_thumbscontainer).removeClass("current");
					$(this).parent().addClass("current");

					var clickpos = 0;

					for (i = 0; i < sl_itemcount; i++) {
						var thisclass = $(".thumb:eq(" + i + ")", sl_thumbscontainer).attr("class");
						if (thisclass == "thumb current") {
							var clickpos = i;
							break;
						}
					}

					var difference = currentthumb - clickpos;
					difference = Math.abs(difference);

					if (currentthumb > clickpos) {
						for (i = 1; i <= difference; i++) {
							Animation("left", false, difference);
						}
					}
					else {
						for (i = 1; i <= difference; i++) {
							Animation("right", false, difference);
						}
					}

					if (sl_numbers == true) {
						$(".nr a.current", sl_numberscontainer).removeClass("current");
						$(".nr a:eq(" + clickpos + ")", sl_numberscontainer).addClass("current");
					}

					sl_currentpos = clickpos;

					if (sl_thumbs == true) {
						$(".thumb", sl_thumbscontainer).removeClass("current");
						$(".thumb:eq(" + (sl_currentpos) + ")").addClass("current");
					}

					return false;
				}
			});

			// AUTOPLAY FUNCTION

			if (options.autoplay == true) {
				function AnimateThis() {
					Animation("right", false, 1);
				}

				var interval = setInterval(AnimateThis, options.autoplayspeed);
			}

			function StopInterval() {
				interval = clearInterval(interval);
			}


		});
	};
})(jQuery);


