(function($){

  
  $.fn.idzynsCarousel = function(options){
  
    this.each(function(){
    	$('ul.carousel', this).each(function(){
    		var data = $('> li', this).clone(true);
    		
    	    var _initCallback = function(c){
    		    $('#content-rhtarw').bind('click',function(){ c.startAuto(0); c.next();return false;});
    		    $('#content-lftarw').bind('click',function(){ c.startAuto(0); c.prev(); return false;});
    		    if(c.list.parents('#content-1').length == 1){
    		    	$('li', c.list).children().animate({opacity: 1.0});
    			}
    	    };
    	    
    		var _itemLoadCallback = function(){
    			  
    		};
    	    
    		var _itemVisibleInCallback_BeforeAnimation = function(c, item, i, state, evt){
    		  	var idx = c.index(i, data.length);
    			// need to add detection - i only want to do carousel-content-pane children
    			var addItem = $($(data.get(idx-1)).html());
    			if(c.list.parents('#content-1').length == 1){
    				addItem.css('opacity', 0);
    			}
    			
    		    c.add(i, addItem);
    		    
    		    // conditionally modify the height of #content-pane-top p if there is an anchor...
    		    
    		    /*
if($('+ a', addItem).length > 0 && c.list.parents('#content-pane-top').length > 0){
					var text = $(addItem);
					var subtract = function(e, x){
						var h = Math.floor(e.height());
						var j = Math.ceil(x.height());
						var cssDimensionToFloat = function(str, context){
							var cx = $($(context||'body, html').get(0));
							var valueAsFloat = null;
							var m;
							switch(true){
								case (m = str.match(/^([\d.]+)em$/)):
								case (m = str.match(/^([\d.]+)%$/)):
									valueAsFloat = cssDimensionToFloat(cx.css('font-size'), cx)*parseFloat(m);
								    break;
								case (m = str.match(/^([\d.]+)px$/)):
								default:
									valueAsFloat = parseFloat(m);
							}
							
							return valueAsFloat;
						};
						
						var lh = cssDimensionToFloat(e.css('line-height'), h);
						return parseInt(Math.floor(h) - Math.ceil(((j/lh)*lh)+cssDimensionToFloat(e.css('margin-bottom'))*2));
					};

					text.css('height', subtract(text, $('+ a img', text)));
				} 
*/   		    
    		};
    	  
    		var _itemVisibleInCallback_AfterAnimation = function(c, item, i, state, evt){
    			if(c.list.parents('#content-1').length == 1){
    				$(item).children().animate({opacity: 1.0});
    				
    			}
    			
    		};
    	  
    		var _itemVisibleOutCallback_BeforeAnimation = function(c, item, i, state, evt){
    			if(c.list.parents('#content-1').length == 1){
    				$(item).children().animate({'opacity': 0});
    				
    			}
    		};
    	  
    		var _itemVisibleOutCallback_AfterAnimation = function(c, item, i, state, evt){
    			c.remove(i);
    			/*
if(c.list.parents('#content-1').length == 1){
    				var background = $('#content-1', c.list.parents('#content-1'));
        			
    			}
*/
    		};	
			
    		$(this).empty().jcarousel({
                scroll: 1,
                initCallback: _initCallback,
                buttonNextHTML: null,
                buttonPrevHTML: null,
                animation: 1500,
				auto: 8,
                wrap: 'circular',
                itemVisibleInCallback: {
    				onBeforeAnimation: _itemVisibleInCallback_BeforeAnimation,
    				onAfterAnimation: _itemVisibleInCallback_AfterAnimation
    			},
                itemVisibleOutCallback: {
    				onBeforeAnimation: _itemVisibleOutCallback_BeforeAnimation,
    				onAfterAnimation: _itemVisibleOutCallback_AfterAnimation	
	    		}
            });
    	});
    });
    
    $('ul.carousel', this).css('visibility', 'visible');
  };
})(jQuery);

