(function($){
$.fn.rotator = function(options) {
var opts = $.extend({}, $.fn.rotator.defaults, options);
return this.each(function(){
path = opts.path;
images = opts.images;
headers = opts.headers;
descriptions = opts.descriptions;
image = $(this).children('img');
var title = $(image).attr("title")
var alt = $(image).attr("alt")
$(image).wrap("<div class='image_wrap'></div>")
$(image).parents(".image_wrap").append("<div class='description'></div>")
$(image).parents(".image_wrap").children(".description").css({opacity:0.7})
$(image).parents(".image_wrap").append("<h4>"+ title +"</h4>")
othis = $(this).children('.image_wrap')
setInterval("rotate(path,images,othis,headers,descriptions)", opts.interval);
});
}
rotate = function (path,images,othis,headers,descriptions){
//console.log('in function')
image = othis.children('img');
for(var i = 0; i<images.length; i++){
var src = path + images[i];
if (src == image.attr('src')){
if (i+1 == images.length) var g = 0;
else var g = i+1;
var newsrc = path + images[g];
//console.log(src)
//console.log(newsrc)
}
}
var i = new Image(); 
         $(i).load(function(){
                $('#img_holder .image_wrap').prepend(i);
                //console.log(headers[i])
                //console.log(headers[g])
                if (headers[g-1] != headers[g]){
                  $('#img_holder h4').fadeOut()
                }
                //$('#img_holder .description p').fadeOut()
                $('#img_holder .image_wrap img:last').fadeOut('slow', function(){
	               
	               $(this).siblings("h4").text(headers[g]).fadeIn("fast")
 	               //$(this).siblings(".description").children("p").text(descriptions[g]).fadeIn("fast")
	               $(this).remove()
	            });
                }).attr('src', newsrc).attr("width", "444");
}
$.fn.rotator.defaults = {
 interval: 6500,
 path: '/images/layout/',
 time: '5000',
 images: ['image1.jpg','image2.jpg','image3.jpg'],
 headers: [],
 descriptions : []
};
})(jQuery);
