jQuery.extend({  
	promoanimation: function( target, options ) {

		var settings = {
			timeout: 5000, 
			speed: 1800,
			show_progess: true
		};

		var returntarget;
		var mode = "fade";
		var items = new Array();
		var timer;
		var current = 0;
		var progress;
		var methods = {
			/**
			* Main init
			*/
			init: function( ) {
				// get mode
				mode = returntarget.hasClass("slide") ? "slide" : "fade";
				
				var theOuter = returntarget.wrap(
						$("<div class='promo_outer'></div>")
							.css("width", returntarget.width())
							.css("height", returntarget.height()));
							
				returntarget.hide();
				
				progress = $("<div></div>")
					.css("position", "absolute")
					.css("top", returntarget.height() - 30)
					.css("right", 20)
					.css("z-index", 2010)
					.appendTo(returntarget);
					
				// find elements to show 
				returntarget.children(".promoitem").each(function(k,v) {
					$("<span></span>")
						.addClass("promo_progress")
						.appendTo(progress).click(function(e) {
							 e.stopPropagation();
							methods._setDisplay(k);
						});
					
					$(this).hide();
					items.push($(this));
				});
				
				items[0].show();
				methods._setProgress();
				progress.hide();
				returntarget.fadeIn("fast", function() {
					if (items.length > 1) {
						methods._setTimer(settings.timeout, methods._doAnimation);
						if (settings.show_progess)
							progress.show();
					}
							
				});
				returntarget.click(function(e) { methods._handleClick(e); })
				return theOuter;
			},
			
			_setTimer: function (t, callback) {
				 timer = setTimeout(callback,t);	 
			}, 
			
			_doAnimation: function() {
				var theCurrent = items[current];
				current++;
				if (current > items.length -1)
					current = 0;
				
				var theNext = items[current];
				methods._setProgress();
				if (mode == "slide") {
					theCurrent.css("z-index", 0);
					theNext.css("z-index", 1);
					
					theNext.show().css("left", returntarget.width() + 5);
					theCurrent.animate({"left": -returntarget.width()}, settings.speed, "easeOutBack", function() {});
					theNext.animate({"left": 0}, settings.speed, "easeOutBack", function() {
						
						methods._setTimer(settings.timeout, methods._doAnimation);
					});
					
				} else {       
					theCurrent.fadeOut("slow");
					theNext.fadeIn("slow");
					methods._setTimer(settings.timeout, methods._doAnimation);
				}
			},
			
			_setDisplay: function(item) {
				clearTimeout(timer);
				var theCurrent = items[current];
				current = item;
				var theNext = items[current];
				methods._setTimer(6000, methods._doAnimation);
				theCurrent.fadeOut("slow");
				theNext.css("left", 0).fadeIn("slow");
				methods._setProgress();
			},
			
			_setProgress: function() {
				progress.find("span").each(function(k,v){
					if (k == current)
						$(v).addClass("active");
					else
						$(v).removeClass("active");
				});
			},
			
			_handleClick: function(e) {
				var theURI = items[current].find("a").attr("href"); 
				e.stopPropagation();
				e.stopImmediatePropagation();
				e.preventDefault();
				if (items[current].hasClass("promoitem_popup")) {
					var theWidth = $(window).width() - 120;   
					var theHeight = $(window).height() - 150;
					 $("<div></div>")
					 	.appendTo("body")
					 	.append(
					 		$("<iframe></iframe>")
					 			.css("width", theWidth - 4)
					 			.css("height", theHeight - 31)
					 			.css("border", "none")
					 			.css("overflow", "hidden")
					 			.attr("src", theURI)
					 	)
					 	.dialog({ "width": theWidth, "height": theHeight, "title": items[current].attr("title"), "position": 'center', 
					 				"resizable": false, "dialogClass": 'promo_dialog', "draggable": false, "modal": true,
					 				"closeOnEscape": true } );
					  return false;
				} else {
					if (items[current].find("a").attr("target") == "_blank")
						window.open (theURI);
					else
						location.href=theURI;
				}
			}
		}
		
		settings.timeout = parseInt($(target).attr("time")) * 1000;
		if (settings.timeout == 0)
			settings.timeout = 5000; 
		
		if ($(target).hasClass("promoitem_hide_progress"))
			settings.show_progess = false;
		// Main startup
		if ( options ) { $.extend( settings, options ); }
      	
      	returntarget = $(target);
		// Start the init
      	return methods.init.apply( this, arguments );
	}  		   		
});  

// init the promo's 
$(function() {
	$(".promo").each(function() {
		var theElm = $(this);
		theElm.empty();
		var theID = theElm.attr("id").replace("promo_", "");
		$.ajax({ 
			"type": "GET", 
			"url": "./ajax/?type=promo_block&id=" + theID, 
			"async": true, //$(this).hasClass("async"),
			"success": function(result) {
				theElm.empty();
				result = $(result.replace(/eshop.paperpoint.nl/g,"totalsupplies.nl"));
                
				if (result.hasClass("promoblock")) {
					$(theElm).append(result);
					$.promoanimation(result);
				}
			}, 
			"error": function(jqXHR, textStatus, errorThrown) { 

			},
			dataType: "html"
		});
	});
});

