jQuery.noConflict();

jQuery.easing['jswing'] = jQuery.easing['swing'];

jQuery.extend( jQuery.easing,
{
	def: 'easeOutQuad',
	swing: function (x, t, b, c, d) {
		//alert(jQuery.easing.default);
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeOutQuad: function (x, t, b, c, d) {
		return -c *(t/=d)*(t-2) + b;
	},
	easeInCubic: function (x, t, b, c, d) {
		return c*(t/=d)*t*t + b;
	},
	easeOutCubic: function (x, t, b, c, d) {
		return c*((t=t/d-1)*t*t + 1) + b;
	},
	easeInOutCubic: function (x, t, b, c, d) {
		if ((t/=d/2) < 1) return c/2*t*t*t + b;
		return c/2*((t-=2)*t*t + 2) + b;
	}
});

/**
 * @author alexander.farkas
 * 
 * @version 2.1
 */
(function($){
	
	$.manageAjax = (function(){
		var cache 			= {},
			queues			= {},
			presets 		= {},
			activeRequest 	= {},
			allRequests 	= {},
			defaults 		= {
						queue: true, //clear
						maxRequests: 1,
						abortOld: false,
						preventDoubbleRequests: true,
						cacheResponse: false,
						complete: function(){},
						error: function(ahr, status){
							var opts = this;
							if(status &&  status.indexOf('error') != -1){
								setTimeout(function(){
									var errStr = status +': ';
									if(ahr.status){
										errStr += 'status: '+ ahr.status +' | ';
									}
									errStr += 'URL: '+ opts.url;
									throw new Error(errStr);
								}, 1);
							}
						},
						success: function(){},
						abort: function(){}
				}
		;
		
		function create(name, settings){
			var publicMethods = {};
			presets[name] = presets[name] ||
				{};
			
			$.extend(true, presets[name], $.ajaxSettings, defaults, settings);
			if(!allRequests[name]){
				allRequests[name] 	= {};
				activeRequest[name] = {};
				activeRequest[name].queue = [];
				queues[name] 		= [];
			}
			$.each($.manageAjax, function(fnName, fn){
				if($.isFunction(fn) && fnName.indexOf('_') !== 0){
					publicMethods[fnName] = function(param){
						fn(name, param);
					};
				}
			});
			return publicMethods;
		}
		
		function complete(opts, args){
			
			if(args[1] == 'success'){
				opts.success.apply(opts, [args[0].successData, args[1]]);
				if (opts.global) {
					$.event.trigger("ajaxSuccess", args);
				}
			}
			
			if(args[1] === 'abort'){
				opts.abort.apply(opts, args);
				if(opts.global){
					$.active--;
					$.event.trigger("ajaxAbort", args);
				}
			}
			
			opts.complete.apply(opts, args);
			
			if (opts.global) {
				$.event.trigger("ajaxComplete", args);
			}
			
			if (opts.global && ! $.active){
				$.event.trigger("ajaxStop");
			}
			//args[0] = null; 
		}
		
		function proxy(oldFn, fn){
			return function(xhr, s, e){
				fn.call(this, xhr, s, e);
				oldFn.call(this, xhr, s, e);
				xhr = null;
				e = null;
			};
		}
		
					
		function callQueueFn(name){
			var q = queues[name];
			if(q && q.length){
				var fn = q.shift();
				if(fn){
					fn();
				}
			}
		}

		
		function add(name, opts){
			if(!presets[name]){
				create(name, opts);
			}
			opts = $.extend({}, presets[name], opts);
			//aliases
			var allR 	= allRequests[name],
				activeR = activeRequest[name],
				queue	= queues[name];
			
			var id 			= opts.type +'_'+ opts.url.replace(/\./g, '_'),
				oldComplete = opts.complete,
				ajaxFn 		= function(){
								activeR[id] = {
									xhr: $.ajax(opts),
									ajaxManagerOpts: opts
								};
								activeR.queue.push(id);
								return id;
							}
				;
				
			if(opts.data){
				id += (typeof opts.data == 'string') ? opts.data : $.param(opts.data);
			}
			
			if(opts.preventDoubbleRequests && allRequests[name][id]){
				return false;
			}
			
			allR[id] = true;
			
			opts.complete = function(xhr, s, e){
				if(opts.abortOld){
					$.each(activeR.queue, function(i, activeID){
						if(activeID == id){
							return false;
						}
						abort(name, activeID);
						return activeID;
					});
				}
				oldComplete.call(this, xhr, s, e);
				//stop memory leak
				if(activeRequest[name][id]){
					if(activeRequest[name][id] && activeRequest[name][id].xhr){
						activeRequest[name][id].xhr = null;
					} 
					activeRequest[name][id] = null;
				}
				xhr = null;
				activeRequest[name].queue = $.grep(activeRequest[name].queue, function(qid){
					return (qid !== id);
				});
				allR[id] = false;
				e = null;
				delete activeRequest[name][id];
			};
			
			if(cache[id]){
				ajaxFn = function(){
					activeR.queue.push(id);
					complete(opts, cache[id]);
					return id;
				};
			} else if(opts.cacheResponse){
				 opts.complete = proxy(opts.complete, function(xhr, s){
					if(s != 'success'){
						return false;
					}
					cache[id][0].responseXML 	= xhr.responseXML;
					cache[id][0].responseText 	= xhr.responseText;
					cache[id][1] 				= s;
					//stop memory leak
					xhr = null;
					return id; //strict
				});
				
				opts.success = proxy(opts.success, function(data, s){
					cache[id] = [{
						successData: data,
						ajaxManagerOpts: opts
					}, s];
					data = null;
				});
			}
			
			ajaxFn.ajaxID = id;
			
			if(opts.queue){
				opts.complete = proxy(opts.complete, function(){
					
					callQueueFn(name);
				});
				 
				if(opts.queue === 'clear'){
					queue = clear(name);
				}
				
				queue.push(ajaxFn);
				
				if(activeR.queue.length < opts.maxRequests){
					callQueueFn(name); 
				}
				return id;
			}
			return ajaxFn();
		}
		
		function clear(name, shouldAbort){
			$.each(queues[name], function(i, fn){
				allRequests[name][fn.ajaxID] = false;
			});
			queues[name] = [];
			
			if(shouldAbort){
				abort(name);
			}
			return queues[name];
		}
		
		function getXHR(name, id){
			var ar = activeRequest[name];
			if(!ar || !allRequests[name][id]){
				return false;
			}
			if(ar[id]){
				return ar[id].xhr;
			}
			var queue = queues[name],
				xhrFn;
			$.each(queue, function(i, fn){
				if(fn.ajaxID == id){
					xhrFn = [fn, i];
					return false;
				}
				return xhrFn;
			});
			return xhrFn;
		}
		
		function abort(name, id){
			var ar = activeRequest[name];
			if(!ar){
				return false;
			}
			function abortID(qid){
				if(qid !== 'queue' && ar[qid] && typeof ar[qid].xhr !== 'unedfiend' && typeof ar[qid].xhr.abort !== 'unedfiend'){
					ar[qid].xhr.abort();
					complete(ar[qid].ajaxManagerOpts, [ar[qid].xhr, 'abort']);
				}
				return null;
			}
			if(id){
				return abortID(id);
			}
			return $.each(ar, abortID);
		}
		
		function unload(){
			$.each(presets, function(name){
				clear(name, true);
			});
			cache = {};
		}
		
		return {
			defaults: 		defaults,
			add: 			add,
			create: 		create,
			cache: 			cache,
			abort: 			abort,
			clear: 			clear,
			getXHR: 		getXHR,
			_activeRequest: activeRequest,
			_complete: 		complete,
			_allRequests: 	allRequests,
			_unload: 		unload
		};
	})();
	//stop memory leaks
	$(window).unload($.manageAjax._unload);
})(jQuery);

(function($) {
	$('html').addClass('js-on');
	var getJSON = $.manageAjax.create('jsonCache', {queue: true, cacheResponse: true, maxRequests: 3, dataType: 'json'});
	
	var switcher = {
		imgDivs: {},
		elm: null,
		idx: 0,
		switcherCode: 	'<div id="teaser-wrapper-2" style="display: none">'+
							'<div class="pager">'+
								'<div title="" class="prev"><a title="" href="#">Zur&uuml;ck</a></div>'+
								'<div title="" class="next"><a title="" href="#">Vor</a></div>'+
							'</div>'+
							'<div class="stage">'+
								'<div class="stage-design">'+
									'<div class="teaser first"><div style="width:100%"><div class="left"><span></span></div><div class="center"><span></span></div><div class="right"><span></span></div></div></div>'+
									'<div class="teaser current"><div style="width:100%"><div class="left"><span></span></div><div class="center"><span></span></div><div class="right"><span></span></div></div></div>'+
									'<div class="teaser last"><div style="width:100%"><div class="left"><span></span></div><div class="center"><span></span></div><div class="right"><span></span></div></div></div>'+
								'</div>'+
							'</div>'+
						'</div>',
		timer: null,
		getInitialImages: function() {
			
		},
		pos: 0,
		copyTeaser: function(from, to){
			$.each(['left', 'center', 'right'], function(i, pos){
				to[pos]
					.css({backgroundImage: from[pos].css('backgroundImage')})
					.find('span')
						.html(from[pos].find('span').html());
			});
		},
		handleClick: function(a, b) {
			var elm = $('#teaser-wrapper');
			elm.unbind('uiscrollerend');
			if(b.newIndex > b.oldIndex) {
				switcher.idx++;
				
				if(switcher.idx >= gallery_length) {
					switcher.idx -= gallery_length;
				}
				
				switcher.copyTeaser(switcher.imgDivs.current, switcher.imgDivs.first);
				
				switcher.copyTeaser(switcher.imgDivs.last, switcher.imgDivs.current);
								
				getJSON.add({
					url: json_path+'triplet.json?_idx='+(switcher.idx+1)+'&_language_id='+language_id,
					success: switcher.setNextTriplet
				});
			}
			else {
				switcher.idx--;
				if(switcher.idx <= 1) {
					switcher.idx += gallery_length;
				}
				
				switcher.copyTeaser(switcher.imgDivs.current, switcher.imgDivs.last);
				
				switcher.copyTeaser(switcher.imgDivs.first, switcher.imgDivs.current);
				
				
				getJSON.add({
					url: json_path+'triplet.json?_idx='+(switcher.idx-1)+'&_language_id='+language_id,
					success: switcher.setPreviousTriplet
				});
			}
			elm.scroller('moveTo', 'goTo1', false);
			elm.bind('uiscrollerend', switcher.handleClick);
			$('#teaser-wrapper').find('div div.teaser div span').css({opacity: 0});
		},
		setTriplet: function(teaserGroup, data){
			$.each(data, function(name, content){
				$('div.'+ name, teaserGroup)
					.css({backgroundImage: 'url('+image_path+content.img+')'})
					.find('span')
						.html(content.title);
			});
		},
		setPreviousTriplet: function(data) {
			switcher.setTriplet($('#teaser-wrapper').find('div.teaser.first'), data);
		},
		setNextTriplet: function(data) {
			switcher.setTriplet($('#teaser-wrapper').find('div.teaser.last'), data);
		},
		onTimeOut: function() {
			switcher.idx = switcher.getRandom(1,gallery_length);
			getJSON.add({
				url: json_path+'triplet.json?_idx='+(switcher.idx)+'&_language_id='+language_id,
				success: function(data) {
					var currentTeaser = $('#teaser-wrapper').find('div.teaser.current');
					$.each(data, function(name, content){
						$('div.'+ name, currentTeaser)
							.animate(
								{opacity: 0}, 
								{
									duration: 250, 
									complete: function() {
										$(this)
											.css({backgroundImage: 'url('+content.img+')'})
											.find('span').html(content.title);
									}
								})
							.animate({opacity: 0.99}, {duration: 250});
					});
				}
			});
			
			if(switcher.idx == gallery_length) {
				getJSON.add({
					url: json_path+'triplet.json?_idx=1&_language_id='+language_id,
					success: switcher.setNextTriplet
				});
			}
			else {
				getJSON.add({
					url: json_path+'triplet.json?_idx='+(switcher.idx+1)+'&_language_id='+language_id,
					success: switcher.setNextTriplet
				});
			}
			if (switcher.idx == 1){
				getJSON.add({
					url: json_path+'triplet.json?_idx='+gallery_length+'&_language_id='+language_id,
					success: switcher.setPreviousTriplet
				});
			} else {
				getJSON.add({
					url: json_path+'triplet.json?_idx='+(switcher.idx-1)+'&_language_id='+language_id,
					success: switcher.setPreviousTriplet
				});
			}
			switcher.timer = window.setTimeout(switcher.onTimeOut, gallery_timer);
		},
		randArr: [],
		randCount:0,
		getRandom: function(min,max) {
			var tmp = min+parseInt(Math.random() * ( max-min+1 ), 10);
			
			if(switcher.randCount === switcher.randArr.length) {
				for(var i = 0; i < gallery_length; i++) {
					switcher.randArr[i] = 0;
					
				}
				switcher.randCount = 0;
			}
			
			if(switcher.randArr[tmp-1] !== 1) {
				switcher.randArr[tmp-1] = 1;
				switcher.randCount++;
				return tmp;
			} else {
				return switcher.getRandom(min,max);
			}
		},
		init: function() {
			var width;
			$('body').append(switcher.switcherCode);
			$('#teaser-wrapper-2').find('div div.teaser div span').css({opacity: 0});
			switcher.idx = switcher.getRandom(1,gallery_length);
			
			getJSON.add({
				url: json_path+'triplet.json?_idx='+switcher.idx+'&_language_id='+language_id,
				success: function(data) {
					var teaserWrapper2 = $('#teaser-wrapper-2'),
						teaserWrapper, teaserWrapperWidth, stageDesign, teaserList;
					
					$.each(data, function(name, content){
						$('div.current div.'+ name, teaserWrapper2)
							.css({backgroundImage: 'url('+ image_path + content.img+')'})
								.find('span')
								.html(content.title);
					});
					
					
					$('#wide').append('<div id="teaser-wrapper">'+$('#teaser-wrapper-2').html()+'</div>');
					
					teaserWrapper = $('#teaser-wrapper');
					teaserWrapperWidth = teaserWrapper.width();
					
					teaserWrapper2.remove();
					if($.browser.msie && parseInt($.browser.version, 10) <= 6) {
						DD_belatedPNG.fix('#teaser-wrapper .pager .prev a,#teaser-wrapper .pager .next a');
					}
					if(switcher.idx == gallery_length) {
						getJSON.add({
							url: json_path+'triplet.json?_idx=1&_language_id='+language_id,
							success: switcher.setNextTriplet
						});
					}
					else {
						getJSON.add({
							url: json_path+'triplet.json?_idx='+(switcher.idx+1)+'&_language_id='+language_id,
							success: switcher.setNextTriplet
						});
					}
					if (switcher.idx == 1){
						getJSON.add({
							url: json_path+'triplet.json?_idx='+gallery_length+'&_language_id='+language_id,
							success: switcher.setPreviousTriplet
						});
					} else {
						getJSON.add({
							url: json_path+'triplet.json?_idx='+(switcher.idx-1)+'&_language_id='+language_id,
							success: switcher.setPreviousTriplet
						});
					}
						
					switcher.randArr = [gallery_length];
					$('#outer').css({marginTop: '-'+teaserWrapper.height()+'px'});
					teaserWrapper
						.scroller({
							prevLink: "div.prev a",
							nextLink: "div.next a",
							recalcStageOnresize: true,
							animateOptions: {
								duration: 625,
								easing: 'easeInOutCubic'
							}
						})
						.scroller('moveTo', 'goTo1', false)
						.hover(function() {
							window.clearTimeout(switcher.timer);
						}, function() {
							switcher.timer = window.setTimeout(switcher.onTimeOut, gallery_timer);
						});
					
					$('div.teaser', teaserWrapper).each(function(i) {
						$(this).width(teaserWrapperWidth - parseInt($(this).css('paddingLeft'), 10) - parseInt($(this).css('paddingRight'), 10));
					});
					
					if($.browser.opera || $.browser.safari) {$('div.teaser div.center', teaserWrapper).width($('div.teaser', teaserWrapper).width() -2); }
					teaserWrapper.scroller('update', true).scroller('moveTo','goTo1', false);
					if($.browser.mozilla && jQuery.browser.version.substr(0,3) <"1.9") {$('div.teaser.first', teaserWrapper).width($('div.teaser.first', teaserWrapper).width() -1); }
					
					stageDesign = $('div.stage-design', teaserWrapper);
					
					$(window).resize(function() {
						teaserWrapper.unbind('uiscrollerend');
						$('div.teaser', teaserWrapper).each(function(i) {
							$(this).width(teaserWrapper.width() - parseInt($(this).css('paddingLeft'), 10) - parseInt($(this).css('paddingRight'), 10));
							if($.browser.opera || $.browser.safari) {$('div.teaser div.center', teaserWrapper).width($('div.teaser', teaserWrapper).width() -2); }
							if($.browser.mozilla && jQuery.browser.version.substr(0,3) <"1.9") {$('div.teaser.first', teaserWrapper).width($('div.teaser.first', teaserWrapper).width() -1); }
						});
						teaserWrapper.scroller('update', true).scroller('moveTo','goTo1', false);
						teaserWrapper.bind('uiscrollerend', switcher.handleClick);
						width = stageDesign.width();
						stageDesign.width(width + 10);
					});
					
					teaserWrapper.bind('uiscrollerend', switcher.handleClick);
					
					stageDesign.width(stageDesign.width()+20);
					
					teaserList = teaserWrapper.find('div.teaser');
					
					switcher.imgDivs = {};
					$.each(['first', 'current', 'last'], function(i, pos1){
						var jElm = teaserList.filter('.'+ pos1);
						switcher.imgDivs[pos1] = {};
						$.each(['left', 'center', 'right'], function(i, pos2){
							switcher.imgDivs[pos1][pos2] = jElm.find('div.'+ pos2);
						});
					});
					
					teaserList.find('div')
						.hover(function() {
							if($(this).attr('class') !== '') {
								$(this).find('span').css({opacity: 0.8});
							}
						}, function() {
							if($(this).attr('class') !== '') {
								$(this).find('span').css({opacity: 0});
							}
						});
					switcher.timer = window.setTimeout(switcher.onTimeOut, gallery_timer);
				}
			});
		}
	};
	$(document).ready(function() {
		$('html').addClass('js-on');
		switcher.init();
	});
})(jQuery);

