jQuery(document).ready(function($)
{
	var slideCount = new Array();
	var slideWidth = new Array();
	var slideTotal = new Array();
	var slideSpeed = new Array();
	
	var themepath = setBasePath($('link').attr('href'));
	
	$('body').click(function(event) {
	   if ($(event.target).is('.picFw')) {
		
	    var index = $('.picFw').index(event.target);
		
		if(slideCount[index]>=0 && slideCount[index]<(slideTotal[index]-1))
		{
			slideCount[index]++;
			var slideOffset = slideCount[index]*slideWidth[index];
			$(".slidePix:eq("+index+") > div.slideContainer").animate( { marginLeft: -slideOffset+'px'}, parseInt(slideSpeed[index]) );
			var countText = $(".slidePix:eq("+index+") > div.slideContainer").next().next().children('span.picCurrent').text();
			if(countText=='')
			{
				$("div.slideInfo").children('span.picCurrent').text(slideCount[index]+1);
			}
			else
			{
				$(".slidePix:eq("+index+") > div.slideContainer").next().next().children('span.picCurrent').text(slideCount[index]+1);
			}
		}
	   }
		
		if ($(event.target).is('.picRw')) {
		    var index = $('.picRw').index(event.target);
			
			if(slideCount[index]>0)
			{
				slideCount[index]--;
				var slideOffset = (slideCount[index])*slideWidth[index];
				$(".slidePix:eq("+index+") > div.slideContainer").animate( { marginLeft: -slideOffset+'px'}, parseInt(slideSpeed[index]) );
				
				var countText = $(".slidePix:eq("+index+") > div.slideContainer").next().next().children('span.picCurrent').text();
				if(countText=='')
				{
					$("div.slideInfo").children('span.picCurrent').text(slideCount[index]+1);
				}
				else
				{
					$(".slidePix:eq("+index+") > div.slideContainer").next().next().children('span.picCurrent').text(slideCount[index]+1);
				}
			}
		}
		
	 });
	
	function initSlide()
	{		
		$('.slidePix').each(function (i) {
			var pixSet = $(this).attr('name');
			
			$(this).load(themepath+'/js_slide/lib/picloader.php',{pixSet: pixSet }, 
				function(){ 
					var total = $(this).children().children('img').length;
					slideTotal.push(total) ;
					slideCount.push(0);
					slideWidth.push($(this).css('width').replace(/px/,''));
					slideSpeed.push($(this).attr('speed'));
					
					if($(this).attr('control')==undefined)
					{
						$(this).append(appendControl());
						$(this).children('div.slideControl').next().children('span.picTotal').text(total);						
					}
					else
					{
						var controldiv = '#'+$(this).attr('control');
						$(controldiv).append(appendControl());
						$('div.slideControl').next().children('span.picTotal').text(total);
					}
								
					
					
				});
		});
	}
	
	function appendControl()
	{
		var control = '<div class="slideControl"><ul><li><a class="picRw" href="javascript: void(0)"></a></li><li><a class="picFw" href="javascript: void(0)"></a></li></ul></div>';
		control += '<div class="slideInfo"><span>Bild </span><span class="picCurrent">1</span>/<span class="picTotal"></span></div>';
		return control;
	}
	
	function setBasePath(path)
	{
		var splitpath = path.split('/');
		splitpath.pop();
		return splitpath.join('/');
	}
	
	initSlide();
	
});
