(function($){
 $.fn.carousel = function(options) {

  var defaults = {
   animationspeed : 'slow',
   loaderimageurl: '',
   itemstoshow: 3,
   vertical:false,
   itemtype:'li',
   itemstoscroll:1,
   noticemetimer:1800, 
   noticeme:true,
   multiclick:true,
   nextredirectlink:"",
   circular:true
  };
  var options = $.extend(defaults, options);
  return this.each(function() {
       var obj = $(this);
       obj.first=0;
       obj.last=0;
       obj.firstshown=0;
       obj.lastshown=options.itemstoshow-1+obj.firstshown;
       if (options.height){
		if (options.height!='auto'){obj.height(options.height);};
       if (options.width){
        obj.width(options.width);}
		};
   var items = $(options.itemtype,obj);
   if (items.size()==0)
    return;  
   obj.originalsize = items.size();
   while (items.size()<options.itemstoshow+4)
   {
       tmp=items.clone();
       tmp.insertBefore(items[0]);
       items = $(options.itemtype,obj);
   };
   obj.number_of_items = items.size();
   
   $(items[obj.number_of_items-1]).insertBefore($(items[0]))
   $(items[obj.number_of_items-2]).insertBefore($(items[obj.number_of_items-1]))
   obj.first=obj.number_of_items-2;
   obj.last=obj.number_of_items-3;
	var objmaxheight = 0;
	i=0;
   items.each(function(){
    this.origiheight=$(this).height();
	if (this.origiheight>objmaxheight){objmaxheight = this.origiheight;}
    this.origiwidth=$(this).width();
    if ((i<obj.firstshown)||(i>obj.lastshown)){
	if (options.vertical){$(this).hide();}else{
    $(this).width(0).hide();};};
	img = new Image;
	$(img).attr("src",options.loaderimageurl);
	thisimg = $('img',this);
	if ((thisimg).attr("realsrc")){
	thisimg.after(img).hide();}
	i++;
   });
   if (options.height=='auto'){obj.height(objmaxheight);};
   for (i=obj.firstshown;i<=obj.lastshown;i=i+1) 
	{
    var origitarget = items[i].origiwidth;
    $(items[i]).width(origitarget).show(options.animationspeed);
 	  var image=($('img',items[i]))[0];
	  if($(image).attr('realsrc')){

		$(image).load(function () {
			$(this).fadeIn();
			$(this).next("img").remove();
		})    
    .error(function () {
      // notify the user that the image could not be loaded
    })
    // *finally*, set the src attribute of the new image to our image
    .attr('src', $(image).attr('realsrc'))
	.removeAttr('realsrc');
	};
	}

	if (options.vertical){
		obj.prepend('<div class="placeholder"></div>');
	    var prevLink = $('<div class="down_arrow_container"><div class="down_arrow"></div></div>');
	    prevLink.prependTo(obj);
	    var nextLink = $('<div class="up_arrow_container"><div class="up_arrow"></div></div>');
	    nextLink.prependTo(obj);
	}
	else
	{
		if (options.prevLink){
	    var prevLink = $(options.prevLink);
		}else{
		    var prevLink = $('<div class="right_arrow_container"><div class="right_arrow"></div></div>');
		    prevLink.prependTo(obj);		
		};
		if (options.nextLink){
			var nextLink = $(options.nextLink);
		}else{
		    var nextLink = $('<div class="left_arrow_container"><div class="left_arrow"></div></div>');
		    nextLink.appendTo(obj);
		};
	}
	prevLink.css({position:"relative"});
	nextLink.css({position:"relative"});

	function clickPrev(snum){
	if ((options.circular||(obj.firstshown>0))&&snum>0&&previsactive){
	  checknext(true);
	  if (options.vertical){
	  $(items[obj.lastshown]).hide(options.animationspeed,function(){clickPrev(snum-1);});
	  } else{
	  $(items[obj.lastshown]).animate({width:(0)},options.animationspeed,"",function(){$(this).hide(); clickPrev(snum-1);});};
	  $(items[obj.last]).insertBefore($(items[obj.first]));	
      obj.first=obj.last;
	  obj.last=obj.last-1;
	  obj.lastshown=obj.lastshown-1;
	  obj.firstshown=obj.firstshown-1;
      if (obj.first<0) {obj.first = obj.number_of_items-1};
      if (obj.last<0) {obj.last = obj.number_of_items-1};
      if (obj.lastshown<0) {obj.lastshown = obj.number_of_items-1};
      if (obj.firstshown<0) {obj.firstshown = obj.number_of_items-1};
      checkprev();
	  if (options.vertical){
      $(items[obj.firstshown]).show(options.animationspeed,function(){previsactive=true;});
	  } else {
	  var origitarget = items[obj.firstshown].origiwidth;
      $(items[obj.firstshown]).show().animate({width:(origitarget)},options.animationspeed,"",function(){previsactive=true;});};
	  //alert(items[obj.firstshown].message);
	  var image=($('img',items[obj.firstshown]))[0];
	  if($(image).attr('realsrc')){
		$(image).load(function () {
			$(this).fadeIn();
			$(this).next("img").remove();
		}).error(function () {// notify the user that the image could not be loaded
		})// *finally*, set the src attribute of the new image to our image
		.attr('src', $(image).attr('realsrc'))
		.removeAttr('realsrc');
	  }	
  }
   }
   function clickNext(snum){	  
	if ((options.circular||(obj.lastshown<obj.originalsize-1))&&snum>0&&nextisactive){
	  checkprev(true);
      if (options.vertical){
	  $(items[obj.firstshown]).hide(options.animationspeed,function(){clickNext(snum-1);});
	  } else{
	  $(items[obj.firstshown]).animate({width:(0)},options.animationspeed,"",function(){$(this).hide(); clickNext(snum-1);});};
	  $(items[obj.first]).insertAfter($(items[obj.last]));	
	  $(items[obj.first]).insertAfter($(items[obj.last]));	
	  obj.last=obj.first;
	  obj.first=obj.first+1;
	  obj.lastshown=obj.lastshown+1;
	  obj.firstshown=obj.firstshown+1;
	  if (obj.first >= obj.number_of_items) {obj.first = 0};
	  if (obj.last >= obj.number_of_items) {obj.last = 0};
	  if (obj.lastshown >= obj.number_of_items) {obj.lastshown = 0};
	  if (obj.firstshown >= obj.number_of_items) {obj.firstshown = 0};
	  var origitarget = items[obj.firstshown].origiwidth;
      checknext();
	  if (options.vertical){
	  $(items[obj.lastshown]).show(options.animationspeed,function(){nextisactive=true;});
	  } else {
	  $(items[obj.lastshown]).show().animate({width:(origitarget)},options.animationspeed,"",function(){nextisactive=true;});};
	  //alert(items[obj.firstshown].message);
	  var image=($('img',items[obj.lastshown]))[0];
	  if($(image).attr('realsrc')){
		$(image).load(function () {
			$(this).fadeIn();
			$(this).next("img").remove();
		}).error(function () {// notify the user that the image could not be loaded
		})// *finally*, set the src attribute of the new image to our image
		.attr('src', $(image).attr('realsrc'))
		.removeAttr('realsrc');
	  }	
	}
   }

   var nextisactive=true;
   var previsactive=true;
   var numofclicknextinactive=0; 
   var nextisclickable=true;
   var previsclickable=true;
   
   function checkprev(activate){
	if (options.circular){return true};
	ndiv = prevLink.find("div");
	if (activate){previsactive=true;ndiv.fadeTo("fast",1).removeClass("inactive");return true};
    if (obj.firstshown==0){ndiv.fadeTo("fast",0.5).addClass("inactive");return false};
	if (ndiv.hasClass("inactive")){
	ndiv.removeClass("inactive").fadeTo("fast",1)}
	return true;
   };
   function checknext(activate){
	if (options.circular){return true};
	ndiv = nextLink.find("div");
	if (activate){nextisactive=true;ndiv.fadeTo("fast",1).removeClass("inactive");return true};
    if (obj.lastshown>=obj.originalsize-1){ndiv.fadeTo("fast",0.5).addClass("inactive");return false};
	if (ndiv.hasClass("inactive")){
	ndiv.removeClass("inactive").fadeTo("fast",1)}
	return true;
   };
   if (!options.circular){
   checkprev();
   checknext();}
   else {
	prevLink.find("div").fadeTo("fast",1).removeClass("inactive");
	nextLink.find("div").fadeTo("fast",1).removeClass("inactive");
   };
   
   prevLink.click(function() {
	if (options.multiclick){for(i=0;i<options.itemstoscroll;i++){clickPrev(1);};
	} else {
	clickPrev(options.itemstoscroll);};
    previsactive=false;
    return false;
   });


   nextLink.click(function() {
	if (options.multiclick){for(i=0;i<options.itemstoscroll;i++){clickNext(1);
	};} else {
	clickNext(options.itemstoscroll);};
	nextisactive=false;
    return false;
   });
   nextLink.dblclick(function(){
   if (!checknext()&&options.nextredirectlink){window.location = options.nextredirectlink;}
   });

	if (options.vertical){
		prevLink.hover( function() { ($(this).find("div").addClass("down_arrow_hover") )} ,
						function() { ($(this).find("div").removeClass("down_arrow_hover") )} );   
		nextLink.hover( function() { ($(this).find("div").addClass("up_arrow_hover") )} ,
						function() { ($(this).find("div").removeClass("up_arrow_hover") )} );   
   } else{
   		prevLink.hover( function() { if (checkprev()){($(this).find("div").addClass("right_arrow_hover").stop().animate({left:0},"fast"));clearInterval(intervaln);}},
						function() { ($(this).find("div").removeClass("right_arrow_hover").stop().animate({left:-5},"fast") );noticemeinterval();} );   
		nextLink.hover( function() { if (checknext()){($(this).find("div").addClass("left_arrow_hover").stop().animate({left:0},"fast") );clearInterval(intervaln);}},
						function() { ($(this).find("div").removeClass("left_arrow_hover").stop().animate({left:5},"fast") );noticemeinterval();} );   
   };
   function noticemeinterval(){
   clearInterval(intervaln);intervaln = setInterval(noticeme, options.noticemetimer );
   };
   function noticeme(){
    if (checkprev()&&options.noticeme){prevLink.find("div").animate({left:0},"fast").animate({left:-5},"fast");};
	if (checknext()&&options.noticeme){nextLink.find("div").animate({left:0},"fast").animate({left:5},"fast");};
	};
	var intervaln=0;
	if (!options.vertical){noticeme();if(options.noticeme){intervaln = setInterval(noticeme, options.noticemetimer );}};
	
  });
 };
})(jQuery);

