﻿/**
 * __options
 * @background:Number			background color
 * @width:Number				width of the slideshow
 * @height:Number				height of the slideshow
 * @time:Timer					time between slides - set to 0 for no timeout
 * @fullscreen:Boolean			if true, width/height are ignored and will play full screen on top of a modal window
 * @swf:String					change the path to the swf
 * @start:Number				start the slideshow at
 * @close:Function				close callback
**/
$.flSlideshow = function(__images, __options) {
	
	var images = __images;
	var v = 9;
	var ie6 = $.browser.msie && $.browser.version < 7;
	
	__options = $.extend({}, {
		background:'#000000',
		width:100,
		height:100,
		time:0,
		fullscreen:true,
		swf:'media/flSlideshow/flslideshow.swf', //media/flSlideshow/
		start:0,
		close:function(){}
	}, __options);
	
	if (__options.start > 0 && __options.start < __images.length) {
		images = $.merge(__images.splice(__options.start, __images.length), __images.splice(0, __options.start));
	}
	
	flSlideshowClose = function(__closeFunction) {
		
		$('div#flSlideshowModal, div#flSlideshow, img#noflash-image').css({
			display:'none'
		});
		
		setTimeout(function(){
			$('div#flSlideshowModal, div#flSlideshow, img#noflash-image').remove();
		}, 200);
		
	}
	
	try {
		
		$('div#flSlideshow').remove();
		$('div#flSlideshowModal').remove();
		$('img#noflash-image').remove();
		
	}catch(e){}
	
	$('<div id="flSlideshowModal">').css({
		'margin':'0',
		'padding':'0',
		'position':ie6?'absolute':'fixed',
		'top':ie6?$(window).scrollTop():'0',
		'left':ie6?$(window).scrollLeft():'0',
		'display':'block',
		'width':'100%',
		'height':'100%',
		'zIndex':'5000',
		'overflow':'hidden',
		'background-color':'#000000',
		'opacity':0.8
	}).appendTo('body');
	
	if (swfobject.hasFlashPlayerVersion(v + '.0.0')) {
	
		var w		= __options.fullscreen == true ? '100%' : __options.width;
		var h		= __options.fullscreen == true ? '100%' :  __options.height;
		var swf		= __options.swf;
		
		if (__options.fullscreen.toString().match(/true/)) {
			
			$('<div id="flSlideshow">').css({
				'margin':'0',
				'padding':'0',
				'position':ie6?'absolute':'fixed',
				'top':ie6?$(window).scrollTop():'0',
				'left':ie6?$(window).scrollLeft():'0',
				'display':'block',
				'width':'100%',
				'height':'100%',
				'zIndex':'6000',
				'overflow':'hidden'
			}).appendTo('body');
			
			$('<div id="flSlideshowFlash">').appendTo('div#flSlideshow');
			
			$('div#flSlideshowFlash').css('visibility','hidden');
			
			swfobject.embedSWF(swf, 'flSlideshowFlash', w, h, v + ".0.0", '', {images:images.toString(),time:__options.time,close:flSlideshowClose,closeParams:__options.close},
			{menu:"false",allowScriptAccess:"always",allowFullScreen:"true",wmode:"transparent",bgcolor:__options.background },{style:'outline:none'});
			
		} else {
			
			// inline slideshow tbc
			
		}
		
	} else {
		
		var noflash = $('<img src="'+(__options.swf.replace(/(.+\/)(.+\.swf)$/i, '$1noflash.png'))+'">')
						.attr('id','noflash-image')
						.css({
							'margin':'-105px 0 0 -103px',
							'left':'50%',
							'top':'50%',
							'position':'absolute',
							'zIndex':'6000',
							'cursor':'pointer'
						})
						.click(function(){
							window.open('http://www.adobe.com/go/EN_US-H-GET-FLASH');
							flSlideshowClose();
						});
						
		$(document.body).prepend(noflash);
		
	}
	
	$('<img src="'+(__options.swf.replace(/(.+\/)(.+\.swf)$/i, '$1close.png'))+'" />')
		.click(flSlideshowClose)
		.css({
			'margin':'10px',
			'position':'absolute',
			'right':'0',
			'top':'0',
			'zIndex':'6001',
			'cursor':'pointer'
		})
		.attr('title', 'Close gallery')
		.appendTo('div#flSlideshow');
	
};
