﻿; (function($) {
    $.fn.bigslide = function(userOptions) {
        var $t = $(this);
        var userOptions = userOptions || {};
        // setup holds the custom events and will be applayed to "this"
        var setup = customEvents;

        if (typeof(userOptions.extend) === 'function') {
            // create copy of customEvents
            var copyCustomEvents = $.extend(true, {},
            customEvents);
            // extend base options
            setup = userOptions.extend(copyCustomEvents) || setup;
        }

        var options = $.extend({},
        $.fn.bigslide.defaults, userOptions);

        // unbinding of all xase Events (tabs are unbound on the setupTabs event)
        $t.unbind('.' + options.namespace);

        // iterates over all custom event arrays and alls each function inside. This functions bind
        // the custom events on the Element and pass the arguments and options provided to the plugin
        // to all those custom events
        for (customEvent in setup) {
            var $self = $t;
            if (setup[customEvent] instanceof Array) {
                for (var i = 0; i < setup[customEvent].length; i++) {
                    setup[customEvent][i].call($self, options);
                }
            } else {
                setup[customEvent].call($self, options);
            }
        }

        // automatically initialize the plugin or not...
        if (options.autoInitialize) {
            $t.trigger("initialize." + options.namespace);
        }
    };

    $.bigslide = {
        extend: function(extend) {
            if (typeof(extend) !== 'function') {
                return;
            }
            // create copy of customEvents
            var copyCustomEvents = $.extend(true, {},
            customEvents);
            // extend base options
            var userCustomEvents = extend(copyCustomEvents) || customEvents;
            // extending the defaults
            customEvents = $.extend(customEvents, userCustomEvents);
        }
    };


    $.fn.bigslide.defaults = {
	// funktionierende optionen
        namespace: 'bigslide',
        autoInitialize: true,
        extend: null,
			json_file: null,
			setup_html_from_json_data: false,
			data_array: null,
			setup_html_from_data_array: false,

			btnPrev: null,
			btnNext: null,
			hvrPrev: null,
			hvrNext: null,
			btnClose: null,
			goto_class: null,
			border_basic_class: null,
			border_active_class: null,
			preload_big_next: 3,
			preload_big_prev: 1,
			delay_play: 5000,
			fade_in_thumbs_speed: 0,

			url_loading_bar_thumb: "images/ajax/ajax-loader_small_flower_597A15.gif",
			loading_text_thumb: "Lädt...",
			url_loading_bar_lightbox: "images/ajax/ajax-loader_bert2_597A15.gif",
			loading_text_lightbox: "Lädt...",

			start: 0,
			play_action: 'play',  // 'play' | 'pause'

			initCallback:null,
			onJSONReady: null,
			onPrepareReady: null
    };

    // Private Variables: customEvents
	var customEvents = {
        initialize: [function(options) {
			this.bind("initialize." + options.namespace,
			function(e) {
				if (e.isDefaultPrevented() || e.isPropagationStopped()) {
					return;
				}
				var $t = $(this);
				/* --- */
				if (options.json_file !== null && options.setup_html_from_json_data==true) {
					$t.trigger('get_json_data.' + options.namespace);
					return false;
				}
				if (options.data_array !== null && options.setup_html_from_data_array==true) {
					$t.trigger('setup_html_from_array.' + options.namespace);
					return false;
				}
				return false;
				/* --- */
			});
        }],
		setup_html_from_array: [function(options) {
            this.bind("setup_html_from_array." + options.namespace,
            function(e) {
                if (e.isDefaultPrevented() || e.isPropagationStopped()) {
                    return;
                }
                var $t = $(this);
				/* --- */
//				$('ul',$t).remove();
				$("*",$t).remove();


				var html_structure='';
					html_structure += '<div class="image_box_border_type1"><div class="image_box"><div class="t"><div class="tl"></div><div class="tli"></div><div class="tri"></div><div class="tr"></div></div><div class="l"><div class="lt"></div><div class="lb"></div></div><div class="r"><div class="rt"></div><div class="rb"></div></div><div class="b"><div class="bl"></div><div class="bli"></div><div class="bri"></div><div class="br"></div></div><div class="bordered_content">';
					html_structure += '<img class="image_0" src="'+options.data_array[options.start]+'" alt=""/>';
					html_structure += '<img class="image_1" style="display:none;" src="'+((options.start+1)>=options.data_array.length ? options.data_array[0] : options.data_array[options.start+1])+'" alt=""/>';
					html_structure += '</div></div></div>'
					$($t).append(html_structure);

				$t.trigger('prepare.' + options.namespace);
				return false;
				/* --- */
            });
        }],
		prepare: [function(options) {
			this.bind("prepare." + options.namespace,
			function(e) {
				if (e.isDefaultPrevented() || e.isPropagationStopped()) {
					return;
				}
				var $t = $(this);
				/* --- */
				if (options.initCallback !== null) {
					options.initCallback();
				}
				$t.trigger('prepare_bigslides.' + options.namespace);
/*
				//$t.trigger('lightbox_preload_image_v4.' + options.namespace);
				//$t.trigger('ightbox_preload_all_images.' + options.namespace);
*/
				if (options.onPrepareReady !== null & options.json_file == null) {
					options.onPrepareReady();
				}
				return false;
				/* --- */
			});
        }],
		prepare_bigslides: [function(options) {
			this.bind("prepare_bigslides." + options.namespace,
			function(e) {
				if (e.isDefaultPrevented() || e.isPropagationStopped()) {
					return;
				}
				var $t = $(this);
				/* --- */
				$($t).data('bigslide_vars');
				$t.data('bigslide_vars', {
					image_0: true,
					actioning: false,
					play_action: options.play_action,
					amountImages: options.data_array.length,
					animateto_tmp: options.start
				});
				$($t).bind("click", function(){
//					$t.trigger('next.' + options.namespace);
//					var i=0;
//					if($t.data('bigslide_vars').play_action=='play' && i==0){
//console.log('pause');
//						$t.trigger('pause.' + options.namespace);
//						i++;
//					}
//					if($t.data('bigslide_vars').play_action=='pause' && i==0){
//console.log('button_play_pause');
//						$t.trigger('play_pause.' + options.namespace);
//						i++;
//					}
				});
				$t.trigger('preload_image_only.' + options.namespace, options.start);
				if($t.data('bigslide_vars').play_action=='play'){
					//$t.trigger('animateto.' + options.namespace, options.start+1);
					$t.data('bigslide_vars').timeout_play = setTimeout("$('#"+$t.attr('id')+"').trigger('animateto.bigslide',"+options.start+1+");",options.delay_play);
				}
//				$t.trigger('goto.' + options.namespace);
				return false;
				/* --- */
			});
        }],
        animateto: [function(options) {
            this.bind("animateto." + options.namespace,
            function(e, to) {
                if (e.isDefaultPrevented() || e.isPropagationStopped()) {
                    return;
                }
                var $t = $(this);
				/* --- */
				$t.data('bigslide_vars').actioning = true;
//alert(to+' '+($t.data('bigslide_vars').amountImages-1));
				var i = 0;
					if(to>=($t.data('bigslide_vars').amountImages)){
						$t.data('bigslide_vars').animateto_tmp=0;
					} else {
						$t.data('bigslide_vars').animateto_tmp=to;
					}
//					alert($t.data('bigslide_vars').animateto_tmp);

//					$t.trigger('lightbox_preload_image_v4.' + options.namespace, to);
				
				if($t.data('bigslide_vars').image_0==true){
					//console.log('FadeIn Bild '+next_image);
						$(".slideshow_philo .image_1").attr("src", options.data_array[$t.data('bigslide_vars').animateto_tmp]);
						$(".slideshow_philo .image_0").fadeOut(1000,function(){
							i++;
							//code redundancy
							if(i>=2){
								$t.data('bigslide_vars').actioning = false;
							}
						});
						$(".slideshow_philo .image_1").fadeIn(1000,function(){
							$(".slideshow_philo .image_0").attr("src", "");
							i++;
							//code redundancy
							if(i>=2){
								$t.data('bigslide_vars').actioning = false;
							}
						});
						$t.data('bigslide_vars').image_0=false;
				} else {
					//console.log('FadeIn Bild '+next_image);
						$(".slideshow_philo .image_0").attr("src", options.data_array[$t.data('bigslide_vars').animateto_tmp]);
						$(".slideshow_philo .image_1").fadeOut(1000,function(){
							i++
							//code redundancy
							if(i>=2){
								$t.data('bigslide_vars').actioning = false;
							}
						});
						$(".slideshow_philo .image_0").fadeIn(1000,function(){
							$(".slideshow_philo .image_1").attr("src", "");
							i++;
							//code redundancy
							if(i>=2){
								$t.data('bigslide_vars').actioning = false;
							}
						});
						$t.data('bigslide_vars').image_0=true;
				}
				$t.trigger('preload_image_only.' + options.namespace, $t.data('bigslide_vars').animateto_tmp);

				if($t.data('bigslide_vars').play_action=='play'){
					$t.data('bigslide_vars').timeout_play = setTimeout("$('#"+$t.attr('id')+"').trigger('play_action.bigslide');$('#"+$t.attr('id')+"').trigger('next.bigslide');",options.delay_play);
				}
				return false;
				/* --- */
            });
        }],
		previous: [function(options) {
            this.bind("previous." + options.namespace,
            function(e) {
                if (e.isDefaultPrevented() || e.isPropagationStopped()) {
                    return;
                }
                var $t = $(this);
				/* --- */

				return false;
				/* --- */
            });
        }],
		next: [function(options) {
            this.bind("next." + options.namespace,
            function(e) {
                if (e.isDefaultPrevented() || e.isPropagationStopped()) {
                    return;
                }
                var $t = $(this);
				/* --- */
				if($t.data('bigslide_vars').actioning == false){
					$t.trigger('animateto.' + options.namespace, ($t.data('bigslide_vars').animateto_tmp+1));
				}
				return false;
				/* --- */
            });
        }],
		play_pause: [function(options) {
            this.bind("play_pause." + options.namespace,
            function(e,action) {
                if (e.isDefaultPrevented() || e.isPropagationStopped()) {
                    return;
                }
                var $t = $(this);
				/* --- */
//console.log("play_pause"+action);

				$t.data('bigslide_vars').play_action=action;

//				alert($t.data('cleanslideshow_vars').play_action);
				var i=0;

				if($t.data('bigslide_vars').play_action=='play' && i==0){
//					alert('play');
					i++;
//					$t.trigger('play_action.' + options.namespace);
					$t.trigger('next.' + options.namespace);
					$t.data('bigslide_vars').play_action='play';
					return false;
				}
				if($t.data('bigslide_vars').play_action=='pause' && i==0){
//					alert('pause');
					i++;
					$t.trigger('pause.' + options.namespace);
//					$t.data('cleanslideshow_vars').play_action='pause';
//					clearTimeout($t.data('cleanslideshow_vars').timeout_play);
					return false;
				}
				return false;
				/* --- */
            });
        }],
		pause: [function(options) {
            this.bind("pause." + options.namespace,
            function(e) {
                if (e.isDefaultPrevented() || e.isPropagationStopped()) {
                    return;
                }
                var $t = $(this);
				/* --- */
				$t.data('bigslide_vars').play_action='pause';
				clearTimeout($t.data('bigslide_vars').timeout_play);
				return false;
				/* --- */
            });
        }],
		preload_image_only: [function(options) {
			this.bind("preload_image_only." + options.namespace,
			function(e, index) {
				if (e.isDefaultPrevented() || e.isPropagationStopped()) {
					return;
				}
				var $t = $(this);
				/* --- */
				//preload nexts
//!!! improved preload next => do the same with previous preloading
				for(var i=1;i<=options.preload_big_next;i++){
//console.log(i);
					if((index+i)<=($t.data('bigslide_vars').amountImages-1)){
//console.log("preload "+ (index+i));
						$("<img>").attr("src", options.data_array[index+i]);
					}
					if((index+i)>($t.data('bigslide_vars').amountImages-1)){
//console.log("bigger preload "+ (-($t.data('bigslide_vars').amountImages)+index+i));
//console.log((index+i) + " " + ($t.data('bigslide_vars').amountImages-1));
						$("<img>").attr("src", options.data_array[-($t.data('bigslide_vars').amountImages)+index+i]);
					}
				}
				//preload previouses
				for(i=1;i<=options.preload_big_prev;i++){
					if((index-i)>=0){
						//console.log($t.data('bigslide_vars').amountImages + " " + (index+i));
						$("<img>").attr("src", options.data_array[index-i]);
					}
				}
				return false;
				/* --- */
			});
		}]
	};

	// ### PUBLIC FUNCTIONS ###
	function css(el, prop) {
//		alert("css();");
		return parseInt($.css(el[0], prop)) || 0;
	};

	function width(el) {
//		alert("width();");
		return  el[0].offsetWidth + css(el, 'marginLeft') + css(el, 'marginRight');
	};
	function height(el) {
//		alert("height();");
		return el[0].offsetHeight + css(el, 'marginTop') + css(el, 'marginBottom');
	};
	// *** GET_IMAGE_SIZE(SRC) ***
	function getImgSize(imgSrc){
		var newImg = new Image();
		newImg.src = imgSrc;
		var height = newImg.height;
		var width = newImg.width;
		var p = $(newImg).ready(function(){
			return {width: newImg.width, height: newImg.height};
		});
		return p;
		//alert (p[0]['width']+" "+p[0]['height']);
	}

})(jQuery);

