// JQuery Based Picture Rotator Application // gFly_Pic_Rotr V2.2009.12.16
// Grendelfly - A Division of SC&G Technology Solutions
//
// Requires JQuery: jquery-1.3.2.min.js


$(function(){
$(document).ready(function() {

	// Set Presets
	  var fTimer = ""; // Declears Variable fTime

	// Rotator Styles
	  $('#gFly_Pic_Rotr').css({
		  'width':'500px',
		  'height':'274px'
		  });	
	
	  $('#gFly_Pic_Rotr_Cntr').css({
		  'position':'absolute',
		  'width':'500px',
		  'text-align':'right'
		  });	
	
	  $('#gFly_Pic_Rotr_Cntr img').css({
		  'margin':'0px',
		  'padding':'0px',
		  'border':'none',
		  'cursor':'pointer',
		  'margin-top':'8px',
		  
		  // opacity		
		  'filter':'alpha(opacity=50)',
		  '-moz-opacity':'0.5',
		  '-khtml-opacity':'0.5',
		  'opacity':'0.5'
		  });	
	
	// Hide Stats Field
	  $('#gFly_Pic_Rotr_Stats').css({'display':'none'});
	
	// Hide all images/p
	  $('#gFly_Pic_Rotr img').css({'position':'absolute','display':'none'});
	
	// Show first image
	  $("#gFly_Pic_Rotr img").each(function (index, domEle) { // index is 'number count' / domEle is 'this'
		  if(index == 0){$(domEle).fadeIn();};
		  }); // $("#gFly_Pic_Rotr div").each(function (index, domEle)

	  $("#gFly_Pic_Rotr_Cntr img").each(function (index, domEle) { // index is 'number count' / domEle is 'this'
		  if( $(domEle).attr('id') == 0){

				$(domEle).css({
				  // opacity		
				  'filter':'alpha(opacity=100)',
				  '-moz-opacity':'1',
				  '-khtml-opacity':'1',
				  'opacity':'1'
				  });	
			  
			  };
		  }); // $("#gFly_Pic_Rotr div").each(function (index, domEle)



	// Controls -- buttons or img 'id' must corrispond with the img number in order
	  $('#gFly_Pic_Rotr_Cntr img.gFly_Pic_Rotr_Btn').click(function(){


		  var imgNum = parseInt( $(this).attr('id') );
		  $('#gFly_Pic_Rotr img').fadeOut(3000); // fade out current img
		  $('#gFly_Pic_Rotr_Cntr img').css({ // opacity
			  'filter':'alpha(opacity=50)',
			  '-moz-opacity':'0.5',
			  '-khtml-opacity':'0.5',
			  'opacity':'0.5'
			  });	
		  
		  $("#gFly_Pic_Rotr img").each(function (index, domEle) { // index is 'number count' / domEle is 'this'
		  if(index == imgNum){
			  $(domEle).fadeIn(3000);
			  $('#gFly_Pic_Rotr_Stats').html(index); // record what img we're on
			  }; // if(index == imgNum)	  
		  }); // $("#gFly_Pic_Rotr div").each(function (index, domEle)
		  
		  // image indicator and button
	  	  $(this).css({ // opacity
		  'filter':'alpha(opacity=100)',
		  '-moz-opacity':'1',
		  '-khtml-opacity':'1',
		  'opacity':'1'
		  });
		  
		  // Stop Auto Rotation
		  clearInterval(fTimer);
		  
		  }); // $('button.gFly_Pic_Rotr_Btn').click


	// Auto Rotate Function
	var fTimer = setInterval(function(){fRotate()}, 5000); // timed at 2 seconds per rotation
	function fRotate(){
		var nImg = $("#gFly_Pic_Rotr img").length; nImg = nImg - 1; // Get number of divs in set
		var nPnl = $('#gFly_Pic_Rotr_Stats').html(); nPnl = parseInt(nPnl) + 1; // Get what div (panel) number we're currently on and moves one forward
		if (nPnl>nImg) { nPnl=0; }; // if we've seen all panels let's start from the beginning
	
		  $('#gFly_Pic_Rotr img').fadeOut(3000); // fade out current img
		  $('#gFly_Pic_Rotr_Cntr img').css({ // opacity
			  'filter':'alpha(opacity=50)',
			  '-moz-opacity':'0.5',
			  '-khtml-opacity':'0.5',
			  'opacity':'0.5'
			  });	
	
		$("#gFly_Pic_Rotr img").each(function (index, domEle) { // index is 'number count' / domEle is 'this'
			if (index==nPnl) {
			  $(domEle).fadeIn(3000);
			  $('#gFly_Pic_Rotr_Stats').html(index); // record what img we're on
			  return false; // stops process			
			  }
			  }); // $("#gFly_Pic_Rotr div").each(function (index, domEle)
		  
		$("#gFly_Pic_Rotr_Cntr img").each(function (index, domEle) { // index is 'number count' / domEle is 'this'
			if ( $(this).attr('id') == $('#gFly_Pic_Rotr_Stats').html()) {
			  $(domEle).css({ // opacity
			  'filter':'alpha(opacity=100)',
			  '-moz-opacity':'1',
			  '-khtml-opacity':'1',
			  'opacity':'1'
			  });
			  }
			  }); // $("#gFly_Pic_Rotr div").each(function (index, domEle)
		  
	
		}; // function fRotate(){



}); // $(document).ready(function()
}); // $(function()