/* we can't use $ on this page because it's already being used by lightwindow */


jQuery(function(){

	var isOldIE = jQuery.browser.msie && jQuery.browser.version < 8;

	if(isOldIE) {

		// Enforce correct dimensions.
		jQuery('#partnerRotation').css({
			'width':'363',
			'height':'221'
		});
	
		// Vertical center hack for IE 6 & 7.
		var thisImg = jQuery("#partnerRotation img:first");
		var ah = (parseInt(thisImg.height(),10));
		var ph = thisImg.parent().height();
		var mh = Math.ceil((ph-ah) / 2);
		thisImg.css('margin-top', mh);
	}	

	
	var rotation = [];
	
	/* loop event (requires "jQuery.timers.js" plugin) 
	syntax: everyTime(duration[ms], function, iterations [0 = endless])
	*/
	jQuery(this).everyTime(8000,function(){
										 
		/* upon fadeout of current image, callback function loads next image */
		jQuery('#partnerRotation').children().fadeOut(500,function(){
			
			/* remove the fadeOut image to free up browser memory */
			jQuery('#partnerRotation').empty();
			
			/* build/rebuild unused-image array 
			("partners" array is dynamically generated outside of this file)
			(slice creates independent copy of array) 
			*/
			if (rotation.length == 0) rotation = partners.slice();
			
			/* remove and use randomly selected image from array */
			var r = Math.floor ( Math.random() * rotation.length ); 
			var newImg = rotation[r];
			rotation.splice(r,1);
			

			var thisLink = jQuery('#imageQueue').find('a.container.'+newImg).clone();


					
			/* clone div with link & image from queue and display */
			thisLink
			.attr('id','active')
			.appendTo('#partnerRotation')
			.hide()
			.fadeIn(500);
			
			// Vertical center hack for IE 6 & 7.
			if(isOldIE) {
				var thisImg = thisLink.find('img:first');
				var ah = (parseInt(thisImg.height(),10)) + 69;
				var ph = thisImg.parent().height();
				var mh = Math.ceil((ph-ah) / 2);
				thisImg.css('margin-top', mh);
			}	

			
			/* on image click, load Lightwindow 
			 (automatically destroyed when associated link is removed from DOM)
			*/
			myLightWindow.createWindow(jQuery('#partnerRotation a').attr('id'));
			
		});
	},0);
});
