/* jQuery Slide Show Plugin ©2010 John Davenport Scheuer
   as first seen in http://www.dynamicdrive.com/forums/
   username: jscheuer1 - This Notice Must Remain for Legal Use
   */
(function($){
	var cre = /\bslide_(\d+)\b/, //regex for individual buttons' count value
	sre = / img$/, //regex for selector
	pre = /^(bottom)|(top)$/, //regex for auto descriptions positioning/slide in
	dre = /\s|(&nbsp;)/g, //regex for description text
	lre = /\W/g, //regex for path if setting cookie for page only
	ire1 = /\./g, //regex for identifier (replace .)
	ire2 = /#/g, //regex for identifier (replace #)
	ie = false, //boolean/element for use in avoiding IE perpetual loading bug for random thumb sorting
	cookie = { //private cookie services
		set: function(n, v, d){ // cookie.set takes (name, value, optional_persist_days)
			if(d){var dt = new Date(); // defaults to session if 0 or no days specified
			dt.setDate(dt.getDate() + d);
			d = '; expires=' + dt.toGMTString();}
			document.cookie = n + '=' + escape(v) + (d || '') + '; path=/';
		},
		get: function(n){ // cookie.get takes (name)
			var c = document.cookie.match('(^|;)\x20*' + n + '=([^;]*)');
			return c? unescape(c[2]) : null;
		},
			kill: function(n, p){ // cookie.kill takes (name)
			cookie.set(n, '', -1);
		}
	};
	/*@cc_on @*/
	/*@if(@_jscript_version >= 5)
	ie = document.createElement('div');
	ie.style.position = 'absolute';
	ie.style.visibility = 'hidden';
	$(function(){document.body.appendChild(ie);});
	@end @*/
	$.fn.slideShow = function(cObj, run){
		if(!this.length && run !== true){
			var s = this.selector;
			$('head').append('<style type="text/css">' + s + ' {overflow: hidden;}</style>');
			$(function(){$(s).slideShow(cObj, true);});
			return this;
		}
		if(!sre.test(this.selector)){
			if(!this.length){
				return this;
			}
			this.scrollTop(0);
			cObj = cObj || {};
			cObj.pvt = {};
			cObj.cookie = cObj.cookie === true? 0 : typeof cObj.cookie === 'string' && cObj.cookie !== '' && !isNaN(cObj.cookie)? cObj.cookie - 0 : cObj.cookie;
			cObj.pvt.slideShowInst = this.selector.replace(ire1, '_class_').replace(ire2, '_id_').replace(lre, '_') + (cObj.pageonly? '_' + location.pathname.replace(lre, '_') : '');
			if(cObj.cookie === 'kill'){cookie.kill(cObj.pvt.slideShowInst);}
			cObj.parent = this.css({position: 'relative', overflow: 'hidden'}).data('slideShow', cObj);
			this.add(cObj.mouseoveradd || '').bind('mouseenter.pause mouseleave.pause', function(e){
				if(e.type === 'mouseenter'){
					cObj.pause = !cObj.nopause && !cObj.stopped && !cObj.imhere;
					cObj.imhere = true;
				} else {
					cObj.pause = cObj.imhere = false;
				}
			});
			this.find('img, span').css('display', 'block').hide();
			if(typeof cObj.cookie === 'number' && (cObj.pvt.cookie = cookie.get(cObj.pvt.slideShowInst))){
				cObj.pvt.cookie = cObj.pvt.cookie.split('.');
				if(cObj.pvt.cookie.length > 2){
					cObj.pvt.cookr = cObj.pvt.cookie.slice(2);
				}
				cObj.count = cObj.pvt.cookie[0] - 0;
				cObj.stopped = cObj.pvt.cookie[1] - 0;
			}
			$(this.selector + ' img').slideShow(cObj);
			return this;
		}
		cObj.ln = this.length;
		function setCookie(){
			cookie.set(cObj.pvt.slideShowInst, (cObj.count % cObj.ln) + '.' + (cObj.stopped? 1 : 0) + (cObj.pvt.rand || ''), cObj.cookie);
		}
		if(cObj.random){
			if(cObj.controls && (cObj.syncThumbs = $(cObj.controls + ' [class*=slide_]')) && cObj.syncThumbs.length === cObj.ln){
				var thumbs = [], temp = this; cObj.pvt.rand = cObj.pvt.rand || '';
				this.each(function(i){
					if(cObj.pvt.cookr && cObj.pvt.cookr.length === cObj.ln){
						$(cObj.syncThumbs[cObj.pvt.cookr[i]]).data('index', i);
						$(temp[cObj.pvt.cookr[i]]).data('index', i);
					} else {
						$(cObj.syncThumbs[i]).data('index', i);
						$(this).data('index', i);
					}
				}).sort(function(a, b){return cObj.pvt.cookr? $(a).data('index') - $(b).data('index') : 0.5 - Math.random();})
				.each(function(i){
					var idx = $(this).data('index'), tbn;
					cObj.syncThumbs.each(function(){
						if((tbn = $(this)) && tbn.data('index') === idx){
							this.className = this.className.replace(cre, '');
							tbn.addClass('slide_' + (i + 1));
							thumbs[i] = this.cloneNode(true);
							if(typeof cObj.cookie === 'number' && !cObj.pvt.cookr){
								cObj.pvt.rand += '.' + idx;
							}
							return;
						}
					});
				});
				if(cObj.pvt.cookr){
					cObj.pvt.rand = '.' + cObj.pvt.cookr.join('.');
				}
				if(typeof cObj.cookie === 'number' && !cObj.pvt.cookr){
					setCookie();
				}
				cObj.syncThumbs.each(function(i){
					if(ie){
						this.parentNode.insertBefore(thumbs[i], this);
						ie.appendChild(this.parentNode.removeChild(this));
					} else {this.parentNode.replaceChild(thumbs[i], this);}
				});
			} else {
				this.sort(function(){return 0.5 - Math.random();});
			}
			if(cObj.onrandinit){
				cObj.onrandinit.call(cObj);
			}
		}
		cObj.count = cObj.count || 0; cObj.rotaterate = cObj.rotaterate || 2500; cObj.ims = []; cObj.initrate = cObj.initrate || 'normal';
		cObj.faderate = cObj.faderate || 3000; cObj.counter = cObj.counter || ['', '/'], cObj.pausres = cObj.pausres || ['Play', 'Pause'];
		cObj.thumbactivate = cObj.thumbactivate || 'click';
		$(cObj.pausres.join('')).filter('img').each(function(){ //preload any pause/resume images not already in the markup
			var im, s = $(this).attr('src');
			if(!$('.pausres img[src="' + s + '"]').length){
				im = new Image();
				im.src = s;
			}
		});
		cObj.pvt.pausresup = function(){ //fires when pause/resume is changed
			if(cObj.stopped && cObj.onstop){
				cObj.onstop.call(cObj);
			}
			if(typeof cObj.cookie === 'number'){
				setCookie();
			}
			if(!cObj.controls){return;}
			$(cObj.controls + ' .pausres').each(function(){
			var prop = typeof this.value === 'string'? 'value' : 'innerHTML';
			this[prop] = cObj.stopped? cObj.pausres[0] : cObj.pausres[1];				
			});
		};
		var p = [cObj.parent.innerHeight(), cObj.parent.innerWidth()], timer = 0, timer2 = 0;
		this.css({position: 'absolute', top: 0, left: 0}).each(function(i){
			cObj.ims[i] = $(this);
			var im = $(new Image());
			im.bind('load', function(){
				cObj.ims[i].data('loaded', true)
				.css({marginTop: (p[0] - cObj.ims[i].outerHeight()) / 2, marginLeft: (p[1] - cObj.ims[i].outerWidth()) / 2});
			});
			if(i === cObj.count){
				im.bind('load', function(){
					cObj.ims[i].fadeIn(cObj.initrate, function(){
						if(cObj.showback){
							$('#' + cObj.showback).css('backgroundColor', cObj.background || 'black');
						}
						if(cObj.oninit){
							cObj.oninit.call(cObj);
						}
						if(cObj.onslide){
							cObj.onslide.call(cObj);
						}
						if(!cObj.stopped){
							timer = setTimeout(cObj.pvt.slide, cObj.rotaterate);
						}
						cObj.pvt.pausresup();
					});
				});
				if(cObj.descriptions){
					if(pre.test(cObj.descriptions)){
						cObj.descObj = {};
						cObj.desc = $(document.createElement('div')).css({position: 'absolute', width: '100%'})
						.addClass(cObj.descriptions + '_desc');
						cObj.descwrap = cObj.desc.wrap(document.createElement('div')).parent()
						.css({position: 'absolute', width: '100%', height: 0}).css(cObj.descriptions, 0);
						cObj.descwrap.prepend(document.createElement('div')).children('div').first()
						.css({position: 'absolute', height: '100%', width: '100%', opacity: cObj.descopacity || 0.7})
						.addClass(cObj.descriptions + '_descbg');
						if(!cObj.descallways){
							cObj.parent.append(cObj.descwrap).add(cObj.mouseoveradd || '').hover(function(){
								cObj.over = true;
								cObj.desc.html(cObj.ims[cObj.count % cObj.ln].next('span').html() ||
								cObj.ims[cObj.count % cObj.ln].parent().not(cObj.parent).next('span').html() || '&nbsp;');
								if(cObj.desc.html().replace(dre, '') === '') {return;}
								cObj.descObj[cObj.descriptions] = 0;
								cObj.descObj.height = cObj.desc.outerHeight();
								cObj.descwrap.stop().animate(cObj.descObj);
								}, function(){
									cObj.over = false;
									cObj.descObj[cObj.descriptions] = '-' + cObj.desc.outerHeight() + 'px';
									cObj.descObj.height = cObj.desc.outerHeight();
									cObj.descwrap.stop().animate(cObj.descObj);
								}
							);
						} else {
							cObj.parent.append(cObj.descwrap);
							cObj.over = true;
							cObj.desc.html(cObj.ims[i].next('span').html() ||
							cObj.ims[i].parent().not(cObj.parent).next('span').html() || '&nbsp;');
							if(cObj.desc.html().replace(dre, '') !== ''){
								cObj.descObj[cObj.descriptions] = 0;
								cObj.descObj.height = cObj.desc.outerHeight();
								cObj.descwrap.stop().animate(cObj.descObj);
								}
							}
					} else {
						cObj.desc = $(cObj.descriptions)
						.html(cObj.ims[i].next('span').html() ||
						cObj.ims[i].parent().not(cObj.parent).next('span').html() || '&nbsp;');
					}
				}
			}
			im.attr('src', cObj.ims[i].attr('src'));
		});
		if(cObj.controls){
			$(cObj.controls + ' .count').html(cObj.counter[0] + 1 + cObj.counter[1] + cObj.ln);
			$(cObj.controls + ' .stop')
			.bind('click', function(e){clearTimeout(timer); cObj.stopped = true; cObj.pvt.pausresup(); e.preventDefault();});
			$(cObj.controls + ' .go').bind('click', function(e){
				if(cObj.stopped){
					clearTimeout(timer);
					cObj.stopped = false;
					cObj.pvt.slide();
					cObj.pvt.pausresup();
				}
				e.preventDefault();
			});
			$(cObj.controls + ' .pausres').bind('click', function(e){
				clearTimeout(timer);
				if(cObj.stopped){
					cObj.stopped = false;
					cObj.pvt.slide();
				} else {
					cObj.stopped = true;
				}
				cObj.pvt.pausresup();
				e.preventDefault();
			});
			$(cObj.controls + ' .next').bind('click', function(e){ //next button
				clearTimeout(timer);
				cObj.stopped = false;
				cObj.pvt.slide();
				cObj.stopped = true;
				cObj.pvt.pausresup();
				e.preventDefault();});
			$(cObj.controls + ' .prev').bind('click', function(e){ //previous button
				clearTimeout(timer);
				cObj.ims[cObj.count % cObj.ln].fadeOut(cObj.faderate);
				cObj.count = cObj.count - 2 + cObj.ln;
				cObj.stopped = false;
				cObj.pvt.slide();
				cObj.stopped = true;
				cObj.pvt.pausresup();
				e.preventDefault();
			});
			$(cObj.controls + ' .first').bind('click', function(e){ //first button
				cObj.stopped = true;
				cObj.pvt.pausresup();
				clearTimeout(timer);
				if(cObj.count % cObj.ln !== 0){
					cObj.ims[cObj.count % cObj.ln].fadeOut(cObj.faderate);
					cObj.count = cObj.ln - 1;
					cObj.stopped = false;
					cObj.pvt.slide();
					cObj.stopped = true;
				}
				e.preventDefault();
			});
			$(cObj.controls + ' .last').bind('click', function(e){ //last button
				cObj.stopped = true;
				cObj.pvt.pausresup();
				clearTimeout(timer);
				if(cObj.count % cObj.ln !== cObj.ln - 1){
					cObj.ims[cObj.count % cObj.ln].fadeOut(cObj.faderate);
					cObj.count = cObj.ln - 2;
					cObj.stopped = false;
					cObj.pvt.slide();
					cObj.stopped = true;
				}
				e.preventDefault();
			});
			cObj.active = $(cObj.controls + ' [class*=slide_]').bind(cObj.thumbactivate, function(e){ //individual buttons
				var num = this.className.match(cre);
				if(num && num[1]){
					cObj.stopped = true;
					cObj.pvt.pausresup();
					clearTimeout(timer);
					if(cObj.count % cObj.ln !== (num[1] - 1 + cObj.ln) % cObj.ln){
						cObj.ims[cObj.count % cObj.ln].fadeOut(cObj.faderate);
						cObj.count = num[1] - 2 + cObj.ln;
						cObj.stopped = false;
						cObj.pvt.slide();
						cObj.stopped = true;
					}
				}
				e.preventDefault();
			}).filter('.slide_' + (cObj.count + 1)).addClass('active').end();
		}
		cObj.pvt.slide = function(){
			clearTimeout(timer);
			if(cObj.stopped){
				return;
			}
			if(cObj.pause || !cObj.ims[(cObj.count + 1) % cObj.ln].data('loaded')){
				clearTimeout(timer);
				timer = setTimeout(cObj.pvt.slide, 300);
				return;
			}
			cObj.count = cObj.count % cObj.ln;
			cObj.ims[cObj.count].fadeOut(cObj.faderate);
			cObj.ims[(++cObj.count) % cObj.ln].fadeIn(cObj.faderate, function(){
				if(cObj.onslide){
					cObj.onslide.call(cObj);
				}
				clearTimeout(timer);
				timer = setTimeout(cObj.pvt.slide, cObj.rotaterate);
				if(typeof cObj.cookie === 'number'){
					setCookie();
				}
			});
			if(cObj.desc){
				clearTimeout(timer2);
				timer2 = setTimeout(function(){
					cObj.desc.html(cObj.ims[cObj.count % cObj.ln].next('span').html() ||
					cObj.ims[cObj.count % cObj.ln].parent().not(cObj.parent).next('span').html() || '&nbsp;');
					if(cObj.over){
						cObj.descObj[cObj.descriptions] =
						cObj.desc.html().replace(dre, '') === ''? '-' + cObj.desc.outerHeight() + 'px' : 0;
						cObj.descObj.height = cObj.desc.outerHeight();
						cObj.descwrap.stop().animate(cObj.descObj);
					}
				}, 200);
			}
			if(cObj.controls && cObj.active.length){
				cObj.active.removeClass('active');
				$(cObj.controls + ' .slide_' + (cObj.count % cObj.ln + 1)).addClass('active');
			}
			if(cObj.controls){
				$(cObj.controls + ' .count').html(cObj.counter[0] + (cObj.count % cObj.ln + 1) + cObj.counter[1] + cObj.ln);
			}
		};
	}
})(jQuery);
