Ape.Element.fotosupershow = function(){
	var that = this;
	var started = 0;
	
	/* Basics */
	this.start = function(){
		/* Basics */
		$('#fotosupershow .tabs_box .tab__fotosupershow').bind('click.fotosupershow', setThisActive);
		
		/* Fotoshow und Thumbnails */
		//$('.fotoshow_weiter').bind('click.fotoshow_weiter', weiter);
		//$('.fotoshow_zurueck').bind('click.fotoshow_zurueck', zurueck);
		
		$('.fotoshow_thumbs a').bind('click.fotoshop_showitem', showitem);
		
		$('.thumbnails_weiter').bind('click.fotoshop_thumbnails_weiter', thumbnails_weiter);
		$('.thumbnails_zurueck').bind('click.fotoshop_thumbnails_zurueck', thumbnails_zurueck);
		
		/* Fazit Vergleichstest */
		$('.fazitdropdown').bind('change.fazitfahrzeugauswahl', getFazitDataFromFahrzeug);
		
		//started = 1;
	};
	
	function setThisActive(){
		$('#fotosupershow .tabs_box .blaettern').hide();
		
		switch($(this).attr('id')){
			case 'tab__fotosupershow_0':
				$('#blaettern_bild').show();
			break;
			case 'tab__fotosupershow_1':
				$('#blaettern_thumbnails').show();
			break;
			case 'tab__fotosupershow_6':
				//Google Karte erst beim öffnen des tabs laden sonst fehlerhafte darstellung
				var timer = window.setTimeout('onLoad()', 100);
			break;
		}
	}
	
	/* Fotoshow und Thumbnails */
	
	var Bilder = null;
	var Position = null;
	var KlickCounter = null;
	var AlternativTitel = null;
	var URL = null;
	
	var ThumbnailBilder = 12;
	
	this.weiter = function (){
		showPicture(that.Position+1);
		return false;	
	}
	
	this.zurueck = function (){
		showPicture(that.Position-1);
		return false;
	}
	
	function thumbnails_weiter(){
		showThumbnails(that.Page+1);
	}
	function thumbnails_zurueck(){
		showThumbnails(that.Page-1);
	}
	function showThumbnails(naechsteSeite){
		
		if(naechsteSeite > Math.ceil(that.Bilder.length/that.ThumbnailBilder)){
			naechsteSeite = 1;
		}
		if(naechsteSeite < 1){
			naechsteSeite = Math.ceil(that.Bilder.length/that.ThumbnailBilder);
		}
		
		$.getJSON('?module=thumbnails&Seite=' + naechsteSeite +'&URL='+that.URL,function(Data){
			
			
			$('.tabs_box #blaettern_thumbnails .seiten').html('Seite ' + naechsteSeite + ' von '+Math.ceil(that.Bilder.length/that.ThumbnailBilder));

			var newThumbnails = '';
			$.each(Data, function(i,item){
				newThumbnails += '<div class="thumb"><a href="javascript:;" id="item_'+i+'"><img src="' + item.Bild.FILE_NAME + '" width="117" height="78" /></a></div>';
			});
		
			$('#tabcontent__fotosupershow_1').html(newThumbnails+"");
			$('#tabcontent__fotosupershow_1 a').unbind('click.fotoshop_showitem').bind('click.fotoshop_showitem', showitem);
			
			that.Page = naechsteSeite;
		});
	}
	function showitem(){
		var number = $(this).attr('id').split('_').pop();
		showPicture(number);
	}
	function showPicture(Position){
		
		$('#tabcontent__fotosupershow_1').hide();
		$('#tab__fotosupershow_1').removeClass('aktiv');
		
		$('#tabcontent__fotosupershow_0').show();
		$('#tab__fotosupershow_0').addClass('aktiv');
		
		var title_a = $('#tab__fotosupershow_1').children('a').html();
		var title_span = $('#tab__fotosupershow_1').children('span').html();
		
		$('#tab__fotosupershow_1').children('span').remove();
		$('#tab__fotosupershow_1').children('a').remove();
		
		if(title_a !== null){
			var title = title_a;
		}else{
			var title = title_span;
		}
		
		$('#tab__fotosupershow_1').append('<a href="javascript:;">'+title+'</a>');
		
		
		
		
        $('#blaettern_thumbnails').hide();
		$('#blaettern_bild').show();
		
		var content = null;
		
		Position = parseInt(Position, 10);
		
		if(Position >= that.Bilder.length){
			Position = 0;
		}
		
		if(Position < 0){
			Position = that.Bilder.length-1;
		}
		if(that.KlickCounter >= that.KlicksUntilReload){
			//seitenreload einleiten
			
			var newUrl = window.location.protocol+'//'+window.location.host+window.location.pathname+'?fotoshow_tab=bild&fotoshowitem='+Position+'#article_detail';
			
			$('.fotoshow_weiter').attr('href', newUrl);
			
			location.href = newUrl;
			
		}else{
			content_click(window.globalAgofCode, window.prod);
			
			that.KlickCounter++;
			
			var oldbox;
			var newbox;
			if(that.Position % 2){
				oldbox = '0';
				newbox = '1';
			}else{
				oldbox = '1';
				newbox = '0';
			}

			$('#tabcontent__fotosupershow_0').find('#fotoshowimage_'+oldbox).hide().end().find('#fotoshowimage_'+newbox).show();
			
			setImageFields(oldbox, newbox, that.Bilder[Position]);

			$('.tabs_box #blaettern_bild .seiten').html('Bild ' + (Position+1) + ' von ' + that.Bilder.length);
			
			that.Position = Position;
				
			initLightbox();	
		}
		return false;
	}
	function setImageFields(old, target, data){
		
		resetImageFields(old);
		
		var ziel = $('#tabcontent__fotosupershow_0 #fotoshowimage_'+target);
		
		$('div.bild img', ziel).attr('src', ''+data.Klein.FILE_NAME);
		$('div.bild img', ziel).attr('alt', ''+data.Klein.ALT_TEXT);
		$('div.bild img', ziel).attr('title', ''+data.Klein.ALT_TEXT);
		
		$('.zoomlink', ziel).attr('href', ''+data.FotoURL);
		$('.zoomlink img', ziel).attr('alt', ''+data.Klein.SIGNATURE);
				
		if(data.Klein.SOURCE != ''){
			$('cite', ziel).html('Foto: '+data.Klein.SOURCE);
		}
		$('p.signaturzeile', ziel).html(''+data.Klein.SIGNATURE);
	}
	function resetImageFields(target){
		var ziel = $('#tabcontent__fotosupershow_0 #fotoshowimage_'+target);
		
		$('div.bild img', ziel).attr('src', '/img/loading.gif');
		$('div.bild img', ziel).attr('alt', '');
		$('div.bild img', ziel).attr('title', '');
		
		$('.zoomlink', ziel).attr('href', '');
		$('.zoomlink img', ziel).attr('alt', '');
		
		$('cite', ziel).html('');
		$('p.signaturzeile', ziel).html('');
	}
	
	
	
	
	/* Vergleichstest */
	
	var Wertungenfazit = null;
	
	function getFazitDataFromFahrzeug(){
		$('body').css('cursor','wait');
		var Slnr = trim($(this).val());
		var id = $(this).attr('id').split('_').pop();
		
		if(id === 1){
			SetDropdown(2,Slnr);
		}else{
			SetDropdown(1,Slnr);
		}
		
		for(var i in that.Wertungenfazit[Slnr]){
			if(typeof that.Wertungenfazit[Slnr][i].wert !== 'undefined' && that.Wertungenfazit[Slnr][i].wert !== ''){
				$('#'+i+'_'+id+'_wert').html(''+that.Wertungenfazit[Slnr][i].wert);
			}else{
				if(i !== 'Summe'){
					$('#'+i+'_'+id+'_wert').html('&mdash;');
				}
			}
			
			if(typeof that.Wertungenfazit[Slnr][i].punkte !== 'undefined' && that.Wertungenfazit[Slnr][i].punkte !== ''){
				$('#'+i+'_'+id+'_punkte').html(''+that.Wertungenfazit[Slnr][i].punkte);
			}else{
				$('#'+i+'_'+id+'_punkte').html('&mdash;');
			}
		}
		$('body').css('cursor','default');
	}
	
	function SetDropdown(Dropdown, Slnr){
		var DropdownSlnr = $('#selectfazitmenu_'+Dropdown).val();
		
		var dropDownOptions = '';
		
 		$.each(dropdownArray, function(i,item){
 			if (item.Slnr != Slnr){
				if(item.Slnr === DropdownSlnr){
					dropDownOptions += '<option value="'+ item.Slnr +'" selected="">' + item.Name + '</option>';
				}else{
					dropDownOptions += '<option value="'+ item.Slnr +'">' + item.Name + '</option>';
				}
			}
		});
		$('#selectfazitmenu_' + Dropdown).html(''+dropDownOptions);
	}
	
	function trim (zeichenkette) {
	  // Erst führende, dann Abschließende Whitespaces entfernen
	  // und das Ergebnis dieser Operationen zurückliefern
	  return zeichenkette.replace(/^\s+/, '').replace(/\s+$/, '');
	}
	
	
	function checkBrowserName(name){  
		var agent = navigator.userAgent.toLowerCase();  
		if (agent.indexOf(name.toLowerCase())>-1) {  
			return true;  
		}  
		return false;  
	}
};
