/*
 * 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; }
                });
            }
        };

        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);

(function ($) {
	$.fn.elStretch = function (options) {
		
		options = $.extend({
			imagewidth: 1024,
			imageheight: 768,
			alttext: "alternative image description",
			interval: 5000,
			fadingspeed: 1000,
			arrayname: bgimages
		}, options);

		return this.each(function() {

			// VARIABLES FROM OPTIONS

			//jQuery.fx.interval = 35;

			var st_originalheight = $(options.imageheight);
			st_originalheight = st_originalheight[0];
			var st_originalwidth = $(options.imagewidth);
			st_originalwidth = st_originalwidth[0];
			var st_interval = $(options.interval);
			st_interval = parseInt(st_interval[0]);
			var st_fadingspeed = $(options.fadingspeed);
			st_fadingspeed = parseInt(st_fadingspeed[0]);
			var st_array = $(options.arrayname);

			// GENERAL VARIABLES

			var st_container = $("#elstretch");
			var st_windowwidth = $(this).width();
			var st_windowheight = $(this).height();
			var st_bgcount = st_array.length;
			var st_img = ""; /* ? */
			var st_h_difference;
			var st_v_difference;
			var st_currarrayitem;
			
			function AttachImages() {
				var currentimg = $("img.stretch",st_container);
				var currentimgsrc = $(currentimg).attr("src");
				for ( var i=0; i < st_bgcount; i++ ) {
					st_container.prepend("<img class='stretch' src='"+st_array[i]+"' alt='"+options.alttext+"' style='display: none;' />");
				}
			}



			AttachImages();

			// CHANGE SIZE AND POSITION OF IMGS
			
			
			// DETECT IPAD

			var isipad = /iPhone|Android/.test( navigator.userAgent );



			function ChangeSizesAndPositions() {
			
				if ( isipad == false ) {
			
				st_windowwidth = $(this).width();
				st_windowheight = $(this).height();
				var windowratio = st_windowwidth / st_windowheight;
				var sourceratio = st_originalwidth / st_originalheight;

					if ( sourceratio < 1 ) {
						// do sth for high images
					} else {
						if ( windowratio >= sourceratio ) {

							var widthratio = st_windowwidth / st_originalwidth;
							var newheight = st_originalheight * widthratio;
							var newwidth = st_originalwidth * widthratio;
							var topdifference = (st_windowheight - newheight) / 2;
							$("img.stretch",st_container).css({"width": newwidth, "height": newheight, "margin-top": topdifference, "margin-left": "auto"});

						}
						else {

							var heightratio = st_windowheight / st_originalheight;
							var newheight = st_originalheight * heightratio;
							var newwidth = st_originalwidth * heightratio;
							var leftdifference = (st_windowwidth - newwidth) / 2;
							$("img.stretch",st_container).css({"width": newwidth, "height": newheight, "margin-left": leftdifference, "margin-top": "auto"});
						}
					}
				
				} else {
				
					$("img.stretch,st_container").css({"width": "940px", "height": "588px", "margin-top": "0", "margin-left": "0"});
				
				}
				

			}


			// CALL FUNCTIONS
			
			$("img", st_container).fadeIn("slow");

			if ( $("img.ani1",st_container).length ) {
				$("img.ani1").fadeIn(5000);
			}
			ChangeSizesAndPositions();
			
			
			$("img",st_container).css("z-index",10);
			var currentpos = 1;
			var currentimg = $("img",st_container)[currentpos - 1];
			$(currentimg).css("z-index",15);

			function Fader(st_fadingspeed, st_interval, st_container) {

				function FadeImages() {

					if ( currentpos < st_bgcount ) {
						var currentimg = $("img",st_container)[currentpos - 1];
						$("img",st_container).hide().css("z-index",10);
						$(currentimg).show().next().show();
						$(currentimg).css("z-index",15);
						$(currentimg).fadeOut(st_fadingspeed, function() {
							currentpos = currentpos + 1;
							var currentimg = $("img:eq("+(currentpos + 1)+")",st_container);
							$(currentimg).prev().show().css("z-index",10);
							$(currentimg).css("z-index",15);
						});
					}
					else {
						var currentimg = $("img:last",st_container);
						$("img",st_container).hide();
						$("img:last",st_container).show();
						$("img:first",st_container).show();
						$(currentimg).fadeOut(st_fadingspeed, function() {
							currentpos = 1;
							var currentimg = $("img",st_container)[currentpos - 1];
							$("img:last").show().css("z-index",10);
							$(currentimg).css("z-index",15);
						});
					}
                    if( $('#slogan')[0] && !$('#slogan.editmode')[0] && $('#slogan .big').length >= 4 ){
                        var currentSlogan = $('#slogan li.first');
                        var allSlogan = $('#slogan li');
                        currentSlogan.fadeOut('normal', function(){
                            if( $(this).next().find('.big')[0] ){
                                $(this).removeClass('first').next().css('display','block').fadeIn().addClass('first');
                            } else {
                                allSlogan.removeClass('first');
                                $('#slogan ul li:first').fadeIn().addClass('first');
                            }
                        });
                    }
				}

				setInterval(FadeImages, st_interval);

			}

			// DIFFER BETWEEN ONE IMAGE AND MORE THAN ONE

			if ( st_bgcount > 1 ) {
				$("img:first",st_container).load(function() {
					Fader(st_fadingspeed, st_interval, st_container);
				});
			}
			else {
				// there is only one image
			}

			// WHAT HAPPENS WHEN WINDOW IS BEING RESIZED?

			if ( isipad == false ) {
			
				$(window).resize(function() {
					//DefinePositions();
					ChangeSizesAndPositions();

				});
			
			}

		});
	};
})(jQuery);

