var varElements;
var varSettings;
var varLast=0;
var timer;
var fadeInTime=2000;
var fadeOutTime=3000;

var slogans = new Array();
slogans[0] = "<a href=\"facility.php\" class=\"slide1\"></a>";
slogans[1] = "<a href=\"blog.php\" class=\"slide2\"></a>";
slogans[2] = "<a href=\"The-Kidney-Diet.pdf\" target=\"_blank\" class=\"slide3\"></a>";
slogans[3] = "<a href=\"nephro-d.php\" class=\"slide4\"></a>";
slogans[4] = "<a href=\"http://www.1067litefm.com/cc-common/mediaplayer/player.html?redir=yes&mps=Get_Connected.php&mid=http://a1135.g.akamai.net/f/1135/18227/1h/cchannel.download.akamai.com/18227/podcast/NEWYORK-NY/WLTW-FM/GCKidneyDoctor.mp3?CPROG=PCAST?CCOMRRMID&CPROG=RICHMEDIA&MARKET=NEWYORK-NY&NG_FORMAT=ac&NG_ID=wltw106fm&OR_NEWSFORMAT=&OWNER=&SERVER_NAME=www.1067litefm.com&SITE_ID=1873&STATION_ID=WLTW-FM&TRACK=\" target=\"_blank\" class=\"slide5\"></a>";

(function($_) {
	
    $_.fn.innerfade = function(options) {
        return this.each(function() {   
            $_.innerfade(this, options);
        });
    };

    $_.innerfade = function(container, options) {
        var settings = {
        	'animationtype':    'fade',
            'speed':            2000,
            'type':             'sequence',
            'timeout':          7000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null
        };
        if (options)
            $_.extend(settings, options);
        if (settings.children === null)
            var elements = $_(container).children();
        else
            var elements = $_(container).children(settings.children);
            
        varElements = elements;
        varSettings = settings;
        	
        if (elements.length > 1) {
            $_(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $_(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
                $_("#pebbles"+i).removeClass('pebbles pebbles_selected').addClass('pebbles');				
            };
            
        	$_("#pebbles0").addClass('pebbles_selected');
        	$_("#dynamic-slide-txt").html(slogans[0]);			
			$_("#dynamic-slide-txt").fadeIn();
			
            if (settings.type == "sequence") { 
                timer = setTimeout(function() {
                	varLast = 1;
                    $_.innerfade.next(elements, settings, 1, 0, 1);
                }, settings.timeout);
                $_(elements[0]).show();
            } else if (settings.type == "random") { 
            		var last = Math.floor ( Math.random () * ( elements.length ) );
                timer = setTimeout(function() {
                    do { 
												current = Math.floor ( Math.random ( ) * ( elements.length ) );
										} while (last == current );             
										$_.innerfade.next(elements, settings, current, last, 1);
                }, settings.timeout);
                $_(elements[last]).show();
						} else if ( settings.type == 'random_start' ) {
								settings.type = 'sequence';
								var current = Math.floor ( Math.random () * ( elements.length ) );
								timer = setTimeout(function(){
									$_.innerfade.next(elements, settings, (current + 1) %  elements.length, current, 1);
								}, settings.timeout);
								$_(elements[current]).show();
						}	else {
							alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
						}
		}
    };

// LOOP
    $_.innerfade.next = function(elements, settings, current, last, effect) {
    	if(effect == 1){
	        if (settings.animationtype == 'slide') {
	            $_(elements[last]).slideUp(settings.speed);
	            $_(elements[current]).slideDown(settings.speed);
	        } else if (settings.animationtype == 'fade') {
				//alert('1st call') // hiding all, it works fine
				$_("#dynamic-slide-txt").fadeOut(settings.speed);	
				$_(elements[last]).fadeOut(settings.speed);
				
				$_(elements[current]).fadeIn(settings.speed, function() {
								$_("#dynamic-slide-txt").html(slogans[current]);
								$_("#dynamic-slide-txt").fadeIn(1);
								removeFilter($_(this)[0]);
							});
	        } else
	            alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
    	} else {
    		clearTimeout(timer);
    		for (var i = 0; i < elements.length; i++) {
                $_(elements[i]).fadeOut();
            };
    		$_(elements[current]).fadeIn();
    	}
    	
    	for (var i = 0; i < elements.length; i++) {
           $_("#ctrl_btn_"+i).removeClass('selected'); 
        };
        
		//alert('hi')
		$_("#dynamic-slide-txt").fadeIn();
		$_("#dynamic-slide-txt").html(slogans[current]);				
        $_("#ctrl_btn_"+current).addClass('selected');
		
        if (settings.type == "sequence") {
            if ((current + 1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings.type == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
        timer = setTimeout((function() {
            $_.innerfade.next(elements, settings, current, last, 1);
        }), settings.timeout);
    };
    
    $_.innerfade.go = function(slide) {
    	//alert(varElements+' s: '+slide);
    	//clearTimeout(timer);
    	$_.innerfade.next(varElements, varSettings, slide, 0, 0);
    };

})($_);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}
