// MAIN IMAGE ROTATION SLIDESHOW FUNCTION
var mainInterval; 		
var whatsnewrotate = true;
$(function(){	
		      	   
	
	SS = {
		currentnum:0,
		showWrapper:'',
		showItems:'',
		sItems:'',
		showLength:0,
		controls:false,	
	 //setup slideshow - (main container,target tag,add numberlinks,place numberlinks rel to container (before, after, append, prepend))
		setupShow : function(wrpr,itms,plyr,plyrloc) {
			//main container of slideshow
			showWrapper = wrpr;
			//containers within main container that container img info/will be changed
			sItems = itms;
			showItems = showWrapper.children(itms);
			//length of show based on number of showItems above
			showLength = showItems.length;
		  
		  if (plyr && showLength >1) {
			  //write number links for controls if specified, add img alt tag for qtip tooltip
			var nums = '<div id="numberLinks">';
			for(i=0;i<showLength;i++) {
				i==0 ? nums += '<a href="" class="currentnum" alt="' +  $('#mainRotation a:eq(0)').attr('alt') + '">' + (i + 1) + '</a>' : nums += '<a href="" alt="' +  $('#mainRotation a:eq(' + i  + ')').attr('alt') + '">' + (i + 1) + '</a>';
				}
			nums += '<a href="" class="pausebtn">Pause</a></div>';
			//determine where numberLinks goes relative to the showWrapper
			if (plyrloc == "before") {
				showWrapper.before(nums)
			} else if (plyrloc == "after") {
				showWrapper.after(nums);
			} else if (plyrloc == "append") {
				showWrapper.append(nums);
			} else if (plyrloc == "prepend") {
				showWrapper.prepend(nums);
			}
			
			$('#numberLinks a').click(function() {
				if ($(this).attr('class') == 'pausebtn') {
					whatsnewrotate = false;
				//	clearInterval(showInterval);
					$(this).removeClass('pausebtn').addClass('playbtn').text('Play');
				} else if ($(this).attr('class') == 'playbtn') {
					whatsnewrotate = true;
					SS.fadeShow();
					$(this).removeClass('playbtn').addClass('pausebtn').text('Pause');
				} else {
					  
					whatsnewrotate = false;
					//clearInterval(showInterval);
					$('#numberLinks a.pausebtn').removeClass('pausebtn').addClass('playbtn').text('Play');
					
					var curnum = (parseInt($(this).siblings('.currentnum').text())) - 1;
					var curli = showWrapper.find(sItems + ':eq('+curnum+')');
				
					 var thisnum = (parseInt($(this).text())) - 1;
					 $(this).siblings('.currentnum').removeClass('currentnum').end().addClass('currentnum');
					 var thisli = showWrapper.find(sItems + ':eq(' + thisnum + ')');
					 
					//curli.removeClass('current').fadeOut('slow');
					 thisli.addClass('current').find('img, .mainImgText').fadeIn('slow');
					 thisli.siblings().removeClass('current').find('img, .mainImgText').hide();
				
				}
			
				return false;
			});		
			
			
			
			
			
			
			/*--- MAIN IMAGES SLIDEHOW TOOLTIPS ---*/
			$('#mainRotation').append('<div id="tooltip"><p></p><div class="tooltiparrow"></div></div>');
					
			$('#numberLinks a').hover(function(){
											
				if($(this).text() == 'Play' || $(this).text() == 'Pause') {
					return;
				} else {
				
					thisIndex = (parseInt($(this).text()))-1;
					//get tooltip text from numberLinks a alt, number parsed from number of rollover
					//$('#mainRotation #tooltip p').text($('#mainRotation ul li:eq(' + thisIndex + ') h3:eq(0)').text())
					$('#mainRotation #tooltip p').text($('#mainRotation #numberLinks a:eq(' + thisIndex + ')').attr('alt'));
					
					var addwidth=0;
					var tipwidth = $('#mainRotation #tooltip').width();
					var navoffset = $(this).parent().position({ border: true, padding: true });
					var navnumoffset = $(this).position({ border: true, padding: true });
				
					//find center point to place pop up
					var targetX = navnumoffset.left - (tipwidth/2) + 20;
					//var targetX = $(this).outerWidth()+7;
					var targetY = navoffset.top - $('#mainRotation #tooltip').height();
					//tell the dd to display and position
					
					$('#mainRotation #tooltip').css('left',targetX).css('top',targetY).show();
					$('#mainRotation #tooltip .tooltiparrow').css('width',tipwidth);
				
				}
				
			},function(){
				if($(this).text() == 'Play' || $(this).text() == 'Pause') {
					return;
				} else {
					$('#mainRotation #tooltip').hide();
				}
			});
			
		
			
			//set controls variable to true so fadeShow will change number links
			controls = true;
			}
		},
		fadeShow : function() {
			if (showLength >1) {
			//get the current image
                var currentItem = $('#mainImgs a.current');//('ul.slideshow_nothumbs li.current');
				
				//if current item is the last li, go back to the first
				if (showWrapper.find(sItems + ':eq(' + (showLength-1) + ')').attr('class')== "current") {
					
					currentItem.removeClass('current').find('img, .mainImgText').fadeOut(1200);
					showWrapper.find(sItems + ':eq(0)').addClass('current').find('img, .mainImgText').fadeIn(1200);
					if (controls) {
						$('#numberLinks').find('a.currentnum').removeClass('currentnum');
						$('#numberLinks').find('a:eq(0)').addClass('currentnum');
					}
				} else {
					currentItem.removeClass('current').find('img, .mainImgText').fadeOut(1200);
					currentItem.next().addClass('current').find('img, .mainImgText').fadeIn(1200);
					if (controls) {
						thisn = $('#numberLinks').find('a.currentnum');
						thisn.next().addClass('currentnum');
						thisn.removeClass('currentnum'); 
					}
						
				}
			}
		}
	};
	
	
		
	//('#mainImgs a img').hide();
	//$('#mainImgs a:eq(0) img').fadeIn();
	//setup slideshow - (main container,target tag,add numberlinks,place numberlinks rel to container (before, after, append, prepend))	
	//SS.setupShow($('#mainImgs'),'a',true,'after');
	//START What's New slideshow rotation	
	//var showInterval = setInterval("SS.fadeShow()",12000);
	
//var mainInterval = setInterval(rotateThings,6000);
/*function rotateThings() {

		if (whatsnewrotate) {
			SS.fadeShow();	
		}
	}*/
	
	
});	  

