/*
First attempt at a basic/simple moduclar Carousel.
This works by having an invisible div, with a nested unordered list within.
We count the list items of the list, and then create the bullets!
*/
$.fn.bulletsCarousel = function(options) {  
   
         var  defaults = {  
             idContainer: 'mainBulletContainer',  
             idContent: 'mainBulletContent',  
             leftButton: 'leftButton' ,
			 rightButton: 'rightButton',
			 bulletList: 'bulletContainer',
			 timer: false, //do we want self-switching "tabs" ?
			 time: 6500
           },  
          vars = $.extend({}, defaults, options);  
		  
		  
//Check to see if we have the right container available or somebody called our function for no reason!
if( $("#"+vars.idContainer).length > 0) {
	
	
	//variable declaration
	var i=0; //-used as a counter
	var bullet = 1; //-keep track of which bullet we are using. Right now we are at bullet : 1
 	var myTimeout; //time for auto-scroll
	//sanity check: We must count how many list items there are in our vars.idContent div.
	var listSize = $("#"+vars.idContent + " ul li").size();

	//if our list size is for any reason smaller(!) or equal to Zero, then we bestow invisibilty upon our navigational buttons.
	//Reason: they are not to be pressed and yield no results.
	if(listSize <= 0)
	{
		$(vars.leftButton).css("display", "none");
		$(vars.rightButton).css("display", "none");
		return false; //ending the function no reason to keep active code
	}
	//we are to create a list that will be autofilled with bullet items. These lie within our custom css.

	$("div."+vars.bulletList).append('<ul class="serialList">');
	
	for(i=1;i<=listSize;i++) { 
		$("div."+vars.bulletList+" ul.serialList").append("<li><div class='bullet'></div></li>"); 
		if(i==listSize) $("div."+vars.bulletList).append("</ul>");
	}

	//Populate our Container with the first child of the list and set the first bullet as active!
	document.getElementById(vars.idContainer).innerHTML = $("#"+vars.idContent + " ul li:nth-child(1) div").html();
	// first bullet is active!
	$("div."+vars.bulletList+" ul.serialList li:nth-child(1) div.bullet").css("background-image", "url(fileadmin/templates/images/bullet-active.png)");
	
		$("div."+vars.rightButton).click( function() {
		clearTimeout(myTimeout);
			vars.time = 87000;
					//we show the fadeout effect and
					if( !($.browser.msie) )
						$("#"+vars.idContainer).fadeOut();
					document.getElementById(vars.idContainer).innerHTML = "";
					
					//bullet = bullet + 1; Since we are moving towards the next bullet.
					// yet if we are currently at the last bullet (listSize) then we have to return to bullet:1
					if(bullet<listSize) {
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet.png)");	
						bullet++;
						document.getElementById(vars.idContainer).innerHTML = $("#"+vars.idContent + " ul li:nth-child("+bullet+") div").html();					
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet-active.png)");
						$("#"+vars.idContainer).stop(true,true).fadeIn(720);
					}
					else if(bullet==listSize) {
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet.png)");
						bullet=1;
						document.getElementById(vars.idContainer).innerHTML = $("#"+vars.idContent + " ul li:nth-child("+bullet+") div").html();
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet-active.png)");
						$("#"+vars.idContainer).stop(true,true).fadeIn(720);
					}
					if(vars.timer==true)
	   				myTimeout = setTimeout(callBack, 110000); 
		});
	///////////////////////////////////////////////////////////////////////////////
		$("div."+vars.leftButton).click( function() {
		clearTimeout(myTimeout);
			vars.time = 110000;
					//we show the fadeout effect and
					
					if( !($.browser.msie) )
					$("#"+vars.idContainer).stop(true,true).fadeOut();
					document.getElementById(vars.idContainer).innerHTML = "";
					
					//bullet = bullet + 1; Since we are moving towards the next bullet.
					// yet if we are currently at the last bullet (listSize) then we have to return to bullet:1
					if(bullet>1) {
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet.png)");	
						bullet--;
						document.getElementById(vars.idContainer).innerHTML = $("#"+vars.idContent + " ul li:nth-child("+bullet+") div").html();					
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet-active.png)");
						$("#"+vars.idContainer).stop(true,true).fadeIn(720);
					}
					else if(bullet==1) {
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet.png)");
						bullet=listSize;
						document.getElementById(vars.idContainer).innerHTML = $("#"+vars.idContent + " ul li:nth-child("+bullet+") div").html();
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet-active.png)");
						$("#"+vars.idContainer).stop(true,true).fadeIn(720);
					}
					if(vars.timer==true)
	   				myTimeout = setTimeout(callBack, 110000); 			
		});
	////////////////////////////////////MAIN BULLETS//////////////////////////////////////////////
	$("div."+vars.bulletList+" ul.serialList li").click( function() {
	
	clearTimeout(myTimeout);
	vars.time = 110000;
			if( !($.browser.msie) )
			$("#"+vars.idContainer).stop(true,true).fadeOut();
			$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet.png)");
			bullet  = $(this).index()+1;
			$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet-active.png)");
			document.getElementById(vars.idContainer).innerHTML = $("#"+vars.idContent + " ul li:nth-child("+bullet+") div").html();
			$("#"+vars.idContainer).stop(true,true).fadeIn(720);
			
			if(vars.timer==true)
	   		myTimeout = setTimeout(callBack, 110000); 
		});		
	
	}	
	/////////////////////////////////////////////////////////////////////////////////////////////	
function callBack() { 
					if( !($.browser.msie) )
					$("#"+vars.idContainer).stop(true,true).fadeOut();
					document.getElementById(vars.idContainer).innerHTML = "";
					
					//bullet = bullet + 1; Since we are moving towards the next bullet.
					// yet if we are currently at the last bullet (listSize) then we have to return to bullet:1
					if(bullet<listSize) {
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet.png)");	
						bullet++;
						document.getElementById(vars.idContainer).innerHTML = $("#"+vars.idContent + " ul li:nth-child("+bullet+") div").html();					
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet-active.png)");
						$("#"+vars.idContainer).stop(true,true).fadeIn(720);
					}
					else if(bullet==listSize) {
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet.png)");
						bullet=1;
						document.getElementById(vars.idContainer).innerHTML = $("#"+vars.idContent + " ul li:nth-child("+bullet+") div").html();
						$("div."+vars.bulletList+" ul.serialList li:nth-child("+bullet+") div.bullet").css("background-image", "url(fileadmin/templates/images/bullet-active.png)");
						$("#"+vars.idContainer).stop(true,true).fadeIn(720);
					}						  
						myTimeout = setTimeout(callBack, vars.time);
			}
	if(vars.timer==true)
	   myTimeout = setTimeout(callBack, vars.time); 
	  //moving all by itself
}

