$(function(){
	init_slideshows();
	init_article_galleries();
	init_article_rotations();
});

function init_slideshows()
{
	$('.slideshow').each(function()
	{
		var sid = this.id;
		if(slideshows[sid] === undefined) return false;

		var ss	= $(this);
		var s_fx	 		= slideshows[sid].fx;
		var s_timeout = slideshows[sid].timeout;
		var s_speed 	= slideshows[sid].speed;
		var s_easing 	= (slideshows[sid].easing !== undefined) ? slideshows[sid].easing: '';
		var s_pager		= '';
		
		if(slideshows[sid].thumbnails !== undefined)
		{
			s_pager = sid + '_pager';
			$(ss).after('<ul id="' + s_pager + '" class="slideshow_pager"></ul>');
			s_pager = '#' + s_pager;
		}

		// HANDLE SOME IE6s ISSUES
		if($.browser.msie && $.browser.version.substr(0,1)<7)
		{
			DD_belatedPNG.fix('.slideshow_buttons a');
			$(ss).css('overflow','hidden');
		}

		// INIT CYCLE/SLIDESHOW
		$(ss).cycle({
			fx:s_fx,
			speed:s_speed,
			next:'#' + sid + '_next',
			prev:'#' + sid + '_prev',
			timeout:s_timeout,
			easing:s_easing,
			pager:s_pager,
			
			// callback fn that creates a thumbnail to use as pager anchor 
				pagerAnchorBuilder: function(idx, slide) { 
					if(slideshows[sid].thumbnails !== undefined)
					{	var src = $(slide).find('img').attr('src');
						return '<li><a href="#"><img src="/tn' + src + '" /></a></li>'; 
					}
				}
		});
		
		if(slideshows[sid].autostart === undefined)
		{
			$(ss).cycle('pause');
			$('#' + sid + '_play').show();
			$('#' + sid + '_pause').hide();
		}
		
		if(	slideshows[sid].thumbnails !== undefined && 
				typeof check_slideshow_thumbnails == 'function') check_slideshow_thumbnails(s_pager);
		
		$('#' + sid + '_play').click(function() { 
			$(this).hide();
			$('#' + sid + '_pause').show();
			$(ss).cycle('resume');
			return false;
		});
		
		$('#' + sid + '_pause').click(function() { 
			$(this).hide();
			$('#' + sid + '_play').show();
			$(ss).cycle('pause');
			return false;
		});
		
		// IF FANCYBOX LOADED
		if(typeof $.fancybox == 'function')
		{
			// SET FANCY BOX FUNCTION TO EACH IMAGE SLIDE
			$(ss).parent().find('a[href$=.jpg],a[href$=.jpeg],a[href$=.png],a[href$=.gif]').each(function(){
				$(this).fancybox({'titleShow':false}).css('cursor', 'pointer');
			});
			
			// TRIGGER IMAGES CLICK AND GIVE IT POINTER CURSOR
			$(ss).find('img').click(function(){
				var src = $(this).attr('src');
				$(ss).cycle('pause');
				$('#' + sid + '_play').show();
				$('#' + sid + '_pause').hide();
				$(ss).parent().find('a[href=\'' + src + '\']').trigger('click');
			}).css('cursor', 'pointer');			
		}
	});
}

function init_article_galleries()
{
	$('.article_gallery .article_gallery_inner').each(function(){
		$(this).addClass('gal_loading');
	});
	
	$(window).load(function(){
		$('.article_gallery .article_gallery_inner').each(function(){

			var ag = $(this);
			var ag_id = ag.parent().attr('id');

			ag.parent()
				.append('<span id="' + ag_id + '_prev" class="article_gallery_prev">&laquo;</span>')
				.append('<span id="' + ag_id + '_next" class="article_gallery_next">&raquo;</span>');

			if(ag.find('.slide img').length){
				ag.find('.slide img').each(function(){
					var img = $(this);
					img.attr('height', img.height()).attr('width', img.width());
				});
			}

			ag.removeClass('gal_loading');

			ag.cycle({
		    fx:     'fade', 
		    speed:  'fast', 
		    timeout: 0, 
		    next:   '#' + ag_id + '_next', 
		    prev:   '#' + ag_id + '_prev'
			});

			if(ag.find('.slide').length < 2){

				ag.find('.slide').each(function(){
					$(this)
					.css('position', 'relative')
					.css('opacity', '1');
				})


				$('.article_gallery_prev').hide();
				$('.article_gallery_next').hide();
			}
		});
	});
}

function init_article_rotations()
{
	$('.article_rotation .article_rotation_inner').each(function(){
		
		var ar = $(this);
		var ar_id = ar.parent().attr('id');
		
		$(ar).cycle({
			fx:				'fade',
			speed:		1500,
			pager:  '.article_rotation_nav',
			pagerAnchorBuilder:function(idx, slide){ 
				$(slide).find('slide_topic').html();
					return '<span>' + $(slide).find('.slide_topic').html() + '</span>';
			},
			timeout:	5000
		});
		
		if(typeof article_rotation_loaded == 'function'){
			var module_pos = ar.parents('.module').attr('id');
			article_rotation_loaded(module_pos);
		}
	});
}
