/*
 * 	loopedSlider 0.5.1 - jQuery plugin
 *	written by Nathan Searles	
 *	http://nathansearles.com/loopedslider/
 *
 *	Copyright (c) 2009 Nathan Searles (http://nathansearles.com/)
 *	Dual licensed under the MIT (MIT-LICENSE.txt)
 *	and GPL (GPL-LICENSE.txt) licenses.
 *
 *	Built for jQuery library
 *	http://jquery.com
 *
 */

/*
 *	markup example for $("#loopedSlider").loopedSlider();
 *
 *	<div id="loopedSlider">	
 *		<div class="container">
 *			<div class="slides">
 *				<div><img src="01.jpg" alt="" /></div>
 *				<div><img src="02.jpg" alt="" /></div>
 *				<div><img src="03.jpg" alt="" /></div>
 *				<div><img src="04.jpg" alt="" /></div>
 *			</div>
 *		</div>
 *		<a href="#" class="previous">previous</a>
 *		<a href="#" class="next">next</a>
 *		<ul class="pagination">
 *			<li><a href="#">1</a></li>
 *			<li><a href="#">2</a></li>
 *			<li><a href="#">3</a></li>
 *			<li><a href="#">4</a></li>
 *		</ul>	
 *	</div>
 *
*/
var $j = jQuery.noConflict();

$j.fn.pngFix = function() {
	  if (!$j.browser.msie || $j.browser.version >= 9) { return $j(this); }
	  return $j(this).each(function() {
	    var img = $j(this);
	    var src = img.attr('src');
	    alert(img);
	    alert(src);
	    img.attr('src', 'tl_files/Advocard/images/transparent.gif')
	        .css('filter', "progid:DXImageTransform.Microsoft.AlphaImageLoader(enabled='true',sizingMethod='crop',src='" + src + "')");
	  });
	};




(function($j) {
	$j.fn.loopedSlider = function(options) {
		
	var defaults = {			
		container: '.gallery-container',
		slides: '.slides',
		pagination: '.pagination',
		containerClick: false, // Click container for next slide
		autoStart: 5000, // Set to positive number for auto interval and interval time
		slidespeed: 1000, // Speed of slide animation
		fadespeed: 2000, // Speed of fade animation
		textfadespeed: 1000, // Speed of fade animation
		autoHeight: false, // Set to positive number for auto height and animation speed
		topPosition: -400,
		topPositionMax: 400
	};
		
	var links = ["http://www.advocard.de/komplett-rechtsschutz-privat.html",
	             "http://www.advocard.de/rechtsschutz-plus-xl.html",
	             "http://www.advocard.de/privat-rechtschutz.html",
	             "http://www.advocard.de/beruf-rechtschutz.html",
	             "http://www.advocard.de/verkehrs-rechtsschutz.html",
	             "http://www.advocard.de/wohnung-rechtschutz.html"
	             ];
			
	
	this.each(function() {
		
		var obj = $j(this);
		var o = $j.extend(defaults, options);
		var pagination = $j(o.pagination+' li a',obj);
		var slidesText = $j('#slides-text');
		var slideImages = $j('#slide-images');
		var m = 0;
		var t = 1;
		var s = $j(o.slides,obj).children().size();
		var w = $j(o.slides,obj).children().outerWidth();
		var p = 0;
		var u = false;
		var n = 0;
		//console.log('U begin: '+u);
/*		$j(o.slides,obj).css({width:(s*w)});*/

		//add to images absolute and block 
		$j(o.slides,obj).children().each(function(){
			$j(this).css({position:'absolute', 'top':o.topPosition});
			/*p=p+w;*/
		});
		//set up pagination
		$j(pagination,obj).each(function(){
			n=n+1;
			$j(this).attr('rel',n);
			$j(pagination.eq(0),obj).parent().addClass('active');
		});
		//show first image
		slideImages.children().eq(0).addClass('active-image').css('top',0).fadeIn(o.fadespeed);
		$j('#home-product-more-info').attr('href',links[0]);
		//show first description
		slidesText.children().eq(0).show();
		slidesText.children().eq(0).addClass('active-text');
		
		
		//$j(o.slides,obj).children(':eq('+(s-1)+')').css({position:'absolute',left:-w});
		
		if(o.autoHeight){autoHeight(t);}
		
		$j('.next',obj).click(function(){
			//console.log('U next: '+u);
			if(u===false) {
				var children = $j(o.slides,obj).children();
				var found = false;
				var i = 0;
				while(!found)
				{
					if(children.eq(i).hasClass('active-image'))
						found = true;
					i++;
					if(i>10) found = true;
				}
				t = i+1;
				//fix counter
				if(t===s+1){t=1;}
				if(t===0){t=s;}
				
				current(t);
				animate('fade',t);
				if(o.autoStart){clearInterval(sliderIntervalID);}
			} return false;
		});
		
		$j('.previous',obj).click(function(){
			//console.log('U prev: '+u);
			if(u===false) {
				var children = $j(o.slides,obj).children();
				var found = false;
				var i = 0;
				while(!found)
				{
					if(children.eq(i).hasClass('active-image'))
						found = true;
					i++;
					if(i>10) found = true;
				}
				t = i-1;
				//fix counter
				if(t===s+1){t=1;}
				if(t===0){t=s;}
				
				current(t);
				animate('fade',t);
				if(o.autoStart){clearInterval(sliderIntervalID);}
			} return false;
		});
		
		/*if (o.containerClick) {
			$j(o.container ,obj).click(function(){
				if(u===false) {
					animate('next',true);
					if(o.autoStart){clearInterval(sliderIntervalID);}
				} return false;
			});
		}*/
		
		$j(pagination,obj).click(function(){
			//console.log('U pagin: '+u);
			if(u===false) {
				if ($j(this).parent().hasClass('active')) {return false;}
				else {
					t = $j(this).attr('rel');
					$j(pagination,obj).parent().siblings().removeClass('active');
					$j(this).parent().addClass('active');
					animate('fade',t);
					if(o.autoStart){clearInterval(sliderIntervalID);}
				}
			} return false;
			
		});
		
		if (o.autoStart) {
			sliderIntervalID = setInterval(function(){
				if(u===false) {
					var children = $j(o.slides,obj).children();
					var found = false;
					var i = 0;
					while(!found)
					{
						if(children.eq(i).hasClass('active-image'))
							found = true;
						i++;
						if(i>10) found = true;
					}
					t = i+1;
					//fix counter
					if(t===s+1){t=1;}
					if(t===0){t=s;}
					
					animate('fade',t);
				
				}
			}, o.autoStart);
		}
		
		function current(t) {
			if(t===s+1){t=1;}
			if(t===0){t=s;}
			$j(pagination,obj).parent().siblings().removeClass('active');
			$j(pagination+'[rel="' + (t) + '"]',obj).parent().addClass('active');
		};
		
		function autoHeight(t) {
			if(t===s+1){t=1;}
			if(t===0){t=s;}	
			var getHeight = $j(o.slides,obj).children(':eq('+(t-1)+')',obj).outerHeight();
			$j(o.container,obj).animate({height: getHeight},o.autoHeight);					
		};		
		
		function animate(dir,clicked){
			//disable click
			u = true;	
			//console.log('U animate after u: '+u);
			var prev = t;
			switch(dir){
				case 'fade':
					t = [t]*1;
					/*m = (-(t*w-w));*/
					
					current(t);
					if(o.autoHeight){autoHeight(t);}
					//we have to change image if we ar eon 6 step -> later use div id
					//start animation
					//move picture out
					var currentDiv = $j(o.slides,obj).children(".active-image");
					var nextDiv = $j(o.slides,obj).children().eq(t-1);

					currentDiv.animate({"opacity": 0, "top": "-=400px"}, o.slidespeed,"", function () { $j(o.slides,obj).children(".active-image").hide()});
					
					//move picture out
					nextDiv.css({'opacity':0}).show();
					nextDiv.addClass('active-image');
					nextDiv.animate({"opacity": 1, "top": "+="+o.topPositionMax+"px"}, o.slidespeed, "",  function () { 
						
						$j(o.slides,obj).children(".active-image").show();
						u=false;
					} );
					
					currentDiv.removeClass('active-image');
					
					break; 
				default:
					break;
				}
		
			//change text also
			if(t!=prev)
			{
				if(t>s) t = 1;
				//slidesText.children().eq(t-1).css('display','block');
				slidesText.children().eq(prev-1).fadeOut(o.textfadespeed, function () { 
					slidesText.children().eq(prev-1).removeClass('active-text'); 
					slidesText.children().eq(t-1).fadeIn(o.textfadespeed); } );
				slidesText.children().eq(t-1).addClass('active-text');
			} else
			{
				slidesText.children(".active-text").fadeOut(o.textfadespeed, function () {  } );
				slidesText.children(".active-text").removeClass('active-text');
				slidesText.children().eq(t-1).fadeIn(o.textfadespeed, function () {  });
				slidesText.children().eq(t-1).addClass('active-text');
			}
			//change link
			$j('#home-product-more-info').attr('href',links[t-1]);
			
			//console.log('U animation after: '+u);
			
			};
		});
	};
})(jQuery);
