﻿(function($) {
	jQuery.extend({
		init_slide: function(firstname,secondname,speed){
			var mywidth,myheight,myend,myfirst,myfirstlink,myfirstsrc;

			if (!speed){speed=1000;}
			
			cntImages = $('#'+firstname+' img').size();
			myfirst=$('#'+firstname+' img:eq(0)').clone();
			myfirstlink=$(myfirst).attr('rel');
			myfirstsrc=$(myfirst).attr('src');
			var myImage = new Image();
			mywidth=myImage.width?myImage.width:587;
			myheight=myImage.height?myImage.height:500;
			myImage.src=$(myfirst).attr('src');
		
			var mylink = new Array();
			var chk,mysrc,myrel,mytitle;
				
			$('#'+secondname).prepend('<div style="width:'+mywidth+';height:'+myheight+';" id="'+secondname+'show" class="showbg" ></div>');

			$('#'+firstname+' img').each(function(e){
				var f=e+1;
				chk=$(this).attr('title');
				myrel=$(this).attr('rel');
				mysrc=$(this).attr('src');
				
				if (chk) { mytitle=chk; }
				
				if(chk===undefined){
					mytitle=$(this).attr("src");
					mytitle = mytitle.replace(new RegExp(/\.*\w*\//g),"");
				}
				
				if (mytitle===undefined){
					mytitle = '';
				}

				mylink[e]='<a style=\"cursor:pointer\" link=\"'+myrel+'\" rel=\"'+mysrc+'\" title=\"'+mytitle+'\" no=\"'+f+'\" >'+f+'</a>';
				
				$('#'+secondname+'link').html('Bilder: '+mylink.join(""));
				$('#'+secondname+'link a').addClass("link_nor");

				if (mylink.length==1)  
					$('.project-detail-imagebrowser').hide();
				else 
					 $('.project-detail-imagebrowser').show();
			});
			
			$('#prevImage').hide();
			if (cntImages>1) {
				$('#nextImage a').attr({ 
					rel: $('#'+firstname+' img:eq(1)').attr('src'),
					no: '2',
					title: $('#'+firstname+' img:eq(1)').attr('title')
				});
			} else {
				$('#nextImage').hide();
			}
				
			$('#'+secondname+'link a:eq(0)').addClass("link_act");
			
			var mydis=$(mylink[0]).attr("title");
			if(mydis!='undefined'){
				$('#'+secondname+'dis').html(mydis);
			}


			if (myfirstlink) {
				$('#'+secondname+'show').append('<a href='+myfirstlink+' ><img src='+myfirstsrc+'></a>');
			} else {		
				$('#'+secondname+'show').append(myfirst);			
			}
		
			//Klick auf einzelne Bilder
			$('#'+secondname+'link a').click(function(){
				makeLinks(parseInt($(this).attr('no')),this.rel,this.title);
				$('#'+secondname+'link a').removeClass("link_act");
				$(this).addClass("link_act");
			});

			// Klick auf Weiterlink
			$('#nextImage a').click(function(){
				makeLinks(parseInt($(this).attr('no')),this.rel,this.title);
			});	
			
			// Klick auf Vorherlink
			$('#prevImage a').click(function(){
				makeLinks(parseInt($(this).attr('no')),this.rel,this.title);
			});			
		
			function makeLinks (num,relAttr,titleAttr) {
			
			
				if (num > 1) {
					$('#prevImage').show();
					$('#prevImage a').attr({
						rel: $('#'+firstname+' img:eq('+(num-2)+')').attr('src'),
						no: (num-1),
						title: $('#'+firstname+' img:eq('+(num-2)+')').attr('title')
					});	
				} else {
					$('#prevImage').hide();
				}
				if (num < cntImages) {
					$('#nextImage').show();
					$('#nextImage a').attr({
						rel: $('#'+firstname+' img:eq('+num+')').attr('src'),
						no: (num+1),
						title: $('#'+firstname+' img:eq('+(num)+')').attr('title')
					});			
				} else {
					$('#nextImage').hide();
				}
				$('#'+secondname+'show img:eq(0)').fadeTo("slow", 0.33,function(){
					$('#'+secondname+'show img:eq(0)').remove();
					$('#'+secondname+'show').append('<img src='+relAttr+' style="display:none">');
					$('#'+secondname+'show img').fadeIn(speed);				
					$('#'+secondname+'dis').html(titleAttr);
				});			
			}
		}
	});
})(jQuery);
