// Image preloader
(function($) {
  var cache = [];
  // Arguments are image paths relative to the current page.
  $.preLoadImages = function() {
    var args_len = arguments.length;
    for (var i = args_len; i--;) {
      var cacheImage = document.createElement('img');
      cacheImage.src = arguments[i];
      cache.push(cacheImage);
    }
  }
})(jQuery)

function makeTabs(containerId, targetClass, suffix) {
	suffix = suffix || '';
	//create total wrapper
	//var megaWrapper = new Element('div', {'class':'slidePanelTabs'+suffix});
	megaWrapper = $('<div class="slidePanelTabs'+suffix+'"></div>');
	//megaWrapper.injectTop($(containerId));
	megaWrapper.prependTo($('#'+containerId));
 
	//create module content wrapper
	//var slidesWrapper = new Element('div', {'class':'slidesWrapper'+suffix});	
	slidesWrapper = $('<div class="slidesWrapper'+suffix+'"></div>');
	//slidesWrapper.injectTop(megaWrapper);
	slidesWrapper.prependTo(megaWrapper);
	
 
	//create tab wrapper
	//var slideNumberWrapper = new Element('ul', {'class':'slideNumberWrapper'+suffix});
	var slideNumberWrapper = $('<ul class="slideNumberWrapper'+suffix+'"></ul>');
	var secondWrapper = $('<div class="secondWrapper"></div>');
	var slideCaptionWrapper = $('<ul class="slideCaptionWrapper'+suffix+'"></ul>');
	
	//slideNumberWrapper.injectTop(megaWrapper);
	slideNumberWrapper.prependTo(secondWrapper);
	secondWrapper.prependTo(megaWrapper);
	slideCaptionWrapper.appendTo(megaWrapper);
 
	//$$('#'+containerId+' li').each(function(module)
//console.log($('#'+containerId+' li'));
	$('#'+containerId+' .slide').each(function(idx, module)
	{
		module = $(module);
		if(module.hasClass(targetClass)) {	
			//create new tab from module title and remove it
			//var newTab = new Element('li', {'class': 'slideTab imagenumber'});
			var newTab = $('<li class="slideTab imagenumber"></li>');
			// newTab.innerHTML = module.getElement('span').innerHTML;			
			newTab.html(module.children('span').html());
			//module.getElement('span').remove();
			module.children('span').remove();
			//newTab.injectInside(slideNumberWrapper);
			newTab.prependTo(slideNumberWrapper);
			
			var newCaption = $('<li class="slideshowImageCaption"></li>');
			newCaption.html(module.children('p').html());
			module.children('p').remove();
			newCaption.prependTo(slideCaptionWrapper);
 
			//move module content to module wrapper
			//module.injectInside(slidesWrapper);
			module.prependTo(slidesWrapper);
 
			//create click event on tab
			//newTab.addEvent('click', function()
			newTab.click( function(ev) {
				//show chosen module
				//$$('#'+containerId+' div.slidesWrapper div.slideshowImageHolder').setStyle('display', 'none');
				//$('#'+containerId+' div.slidesWrapper li.slide').css('display', 'none');
				var curTab = $('#'+containerId+' .active_slide:first');
				var curCaption = $('#'+containerId+' .active_caption:first');
				
				if(curTab.length) {
					$(curTab).fadeOut(125, function() {
						module.fadeIn(125, function() {	});
					});
					
					$(curCaption).fadeOut(125, function() {
						newCaption.fadeIn(125, function() {	Cufon.refresh();	});
					});
				} else {
					module.fadeIn(125, function() {		});
					newCaption.fadeIn(125, function() {	Cufon.refresh();	});
				}

				//update tabs
				//$$('#'+containerId+' .slideTab').removeClass('slideTab_active');
				$('#'+containerId+' .slideTab').removeClass('slideTab_active');
				$('#'+containerId+' .slide').removeClass('active_slide');
				$('#'+containerId+' .slideshowImageCaption').removeClass('active_caption');
				
				newTab.addClass('slideTab_active');
				module.addClass('active_slide');
				newCaption.addClass('active_caption');				
			});
			newCaption.css('display','none');
		}
	});
 
	
	//only show first one
	//slideNumberWrapper.getFirst().fireEvent('click');
	slideNumberWrapper.children('li:first').click();
	if(slideNumberWrapper.children('li').length == 1) {
		slideNumberWrapper.children().css('display', 'none');
		$('.slideshowImageHolder').unbind('click');	
		$('.slideTab:first').unbind('click');		
	}
}

function attachImgEvents() {
	$('.slideshowImageHolder').click(function(ev) {
		var nextLi = $('.slideTab_active:first').next('li')
		if(nextLi.length) {
			nextLi.click();
		} else {
			$('.slideTab:first').click();
		}
	});
}