/**
 * Heavy Weight Slideshow v1.0 Class
 * by wirexmedia (info@wirexmedia.com)
 **/
 
// Initiate cufon before document is ready for IE reasons
Cufon.replace('.hw-slideshow .controls .title span', {
	fontFamily: 'Verve Std',
	separate: 'none'
});

var _HWSlideshow = new HWSlideshow();
function HWSlideshow() {
	var _l0 = this;
	$(document).ready(function() {
		_l0.build();
	});
};


HWSlideshow.prototype.build = function() {
	var _l0 = this;
	
	// Cache properties
	_l0.slides = new Array();
	_l0.slide_index = 0;
	$('.hw-slideshow .list li img', _l0.doc).each(function() {
		_l0.slides[_l0.slide_index] = new Object();
		_l0.slides[_l0.slide_index].width = $(this).parent().width();
		_l0.slides[_l0.slide_index].height = $(this).parent().height();
		_l0.slides[_l0.slide_index].title = $(this).attr('title');
		_l0.slides[_l0.slide_index].source = $(this).attr('src');
		$(this).parent().removeAttr('style');
		$(this).attr('title', '');
		$(this).attr('src', '');
		_l0.slide_index++;
	});
	
	// Controls
	_l0.num_elemts = $('.hw-slideshow .list', _l0.doc).children().length;
	_l0.slide_pos = 1;
	if (_l0.num_elemts > 1) {
		$('.hw-slideshow .controls .back span', _l0.doc).click(function() {
			if (_l0.slide_pos <= 1) {
				_l0.slide_pos = _l0.num_elemts;
			} else {
				_l0.slide_pos--;
			}
			_l0.updateSlideshow();
		});
		$('.hw-slideshow .controls .next span', _l0.doc).click(function() {
			if (_l0.slide_pos >= _l0.num_elemts) {
				_l0.slide_pos = 1;
			} else {
				_l0.slide_pos++;
			}
			_l0.updateSlideshow();
		});
	} else {
		$('.hw-slideshow .controls').hide();
	}
	
	// Window resize
	$(window).resize(function() {
		_l0.adaptBackground();
	});
	_l0.updateSlideshow();
	
	// Refresh cufon
	//Cufon.refresh();
};

HWSlideshow.prototype.updateSlideshow = function() {
	var _l0 = this;
	var _image = $('.hw-slideshow .list li:nth-child('+_l0.slide_pos+') img', _l0.doc);
	setTimeout(function() {
		_image.attr('src', _l0.slides[(_l0.slide_pos-1)].source).bind('cacheload', function() {
			$('.hw-slideshow .controls .title', _l0.doc).empty();
			$('.hw-slideshow .controls .title', _l0.doc).append(_l0.slide_pos + '/' + _l0.num_elemts + ' - <span>' + _l0.slides[(_l0.slide_pos-1)].title + '</span>');
			Cufon.refresh();
			$('.hw-slideshow .list li', _l0.doc).hide();
			$(this).parent().fadeIn();
			_l0.adaptBackground();
		});
	}, 100);
};

HWSlideshow.prototype.adaptBackground = function() {
	var _l0 = this;
	var ws = $('body').width() / _l0.slides[(_l0.slide_pos-1)].width;
	var hs = $('body').height() / _l0.slides[(_l0.slide_pos-1)].height;
	var scale = Math.max(ws, hs);
	$('.hw-slideshow .list', _l0.doc).css({
		'width' : Math.round(scale * _l0.slides[(_l0.slide_pos-1)].width),
		'height' : Math.round(scale * _l0.slides[(_l0.slide_pos-1)].height)
	});
};
