
/**
 * Ad Age Slideshow
 *
 * @author: 	Rahmin Pavlovic
 * @date: 		27 June 2008
 * @version: 	30 July 2008
 *
 * copyright (c) 2008, Crain Communications
 *
 */

var thisSlideshow = null;
function Slideshow(id) {

	// configurable variables
	this.id = id;
	this.repeat = 0;
	this.fade = 500;
	this.delay = 3.5;
	this.directory = '';
	this.autoPlay = false;
	this.autoStart = true;
	this.pagingLimit = 14;
	this.container = 'creativity_slideshow';

	// non-configurable variables
	this.html = [];
	this.ctrls = [];
	this.slide = {};
	this.images = [];
	this.length = 0;
	this.counter = 0;
	this.timer = null;
	this.initiated = false;
	this.loadingMessage = '<br />Loading slideshow...<br /><br /><div align="center"><img src="http://adage.com/images/global/ajax-indicator-036.gif" width="24" height="24" alt="" /></div><br /><br />';

	this.initiate = function() {
		if(trim(this.directory) != '') {

			// display loading message
			DOM.getElementById(this.container).innerHTML = this.loadingMessage;

			// remember this instance for sub-routine
			thisSlideshow = this;

			// pull slideshow XML
			new AJAX.FileRequest(
				'/ajax/get_slideshow.php',
				function() {
					if(this.request && this.response) {
						// parse JSON return
						thisSlideshow.slide = JSON.parse(this.response);

						if(thisSlideshow.slide.image && thisSlideshow.slide.image.length) {
							for(var i=0; i<thisSlideshow.slide.image.length; i++) {

								thisSlideshow.images[i] = new Slide();

								if(thisSlideshow.slide.image[i].title) {
									thisSlideshow.images[i].title = thisSlideshow.slide.image[i].title;
								}

								if(thisSlideshow.slide.image[i].description) {
									thisSlideshow.images[i].description = thisSlideshow.slide.image[i].description;
								}

								if(thisSlideshow.slide.image[i].filename) {
									thisSlideshow.images[i].src = thisSlideshow.directory + thisSlideshow.slide.image[i].filename;
								}

								if(thisSlideshow.slide.image[i].width) {
									thisSlideshow.images[i].width = parseInt(thisSlideshow.slide.image[i].width);
								}

								if(thisSlideshow.slide.image[i].height) {
									thisSlideshow.images[i].height = parseInt(thisSlideshow.slide.image[i].height);
								}

								if(thisSlideshow.slide.image[i].credit) {
									thisSlideshow.images[i].credit = thisSlideshow.slide.image[i].credit;
								}

								if(thisSlideshow.slide.image[i].caption) {
									thisSlideshow.images[i].caption = thisSlideshow.slide.image[i].caption;
								}

								thisSlideshow.load(thisSlideshow.images[i].src, thisSlideshow.images[i].width, thisSlideshow.images[i].height);

							}
							thisSlideshow.length = thisSlideshow.slide.image.length;

							if(thisSlideshow.autoStart) {
								thisSlideshow.start();
							}
						}
					}
				},
				null,
				'get',
				'path=' + escape(this.directory)
			)
		}
	}

	this.show = function() {
		//DOM.fade(this.id + '_slides', 0, 100, this.fade);
		DOM.setOpacity(this.id + '_slides', 100);
	}

	this.hide = function() {
		DOM.setOpacity(this.id + '_slides', 0);
	}

	this.start = function() {
		if(this.counter == this.images.length) {
			this.counter = 0;
			if(!this.repeat || !this.autoPlay) {
				this.stop();
			}
		}

		if(this.initiated) {
			this.hide();
		}
		else if(Window.getLocationHash() && Window.getLocationHash().isNumeric()) {
			this.counter = Window.getLocationHash();
		}
		if(!this.images[this.counter]) {
			this.counter = 0;
		}

		this.html.destroy();

		this.getPlayerControls();

		this.html.push('<div id="', this.id, '_slides" style="width:', this.images[this.counter].width, 'px;">');

		if(this.images[this.counter].title && this.images[this.counter].title != '[object Object]') {
			this.html.push('<h2 class="slideshow_title" style="width:', this.images[this.counter].width, 'px;">', this.images[this.counter].title, '</h2>');
		}
	
		if(this.images[this.counter].description && this.images[this.counter].description != '[object Object]') {
			this.html.push('<h4 class="slideshow_description" style="width:', this.images[this.counter].width, 'px;">', this.images[this.counter].description, '</h4>');
		}

		this.html.push('<img src="', this.images[this.counter].src, '" width="', this.images[this.counter].width,'" height="', this.images[this.counter].height,'" alt="" />');

		if(this.images[this.counter].credit && this.images[this.counter].credit != '[object Object]') {
			this.html.push('<div class="slideshow_credit" style="width:', this.images[this.counter].width, 'px;">', this.images[this.counter].credit, '</div>');
		}

		if(this.images[this.counter].caption && this.images[this.counter].caption != '[object Object]') {
			this.html.push('<div class="slideshow_caption" style="width:', this.images[this.counter].width, 'px;">', this.images[this.counter].caption, '</div>');
		}

		this.html.push('</div>');

		DOM.getElementById(this.container).innerHTML = this.html.join('');

		if(!this.initiated) {
			DOM.fade(this.id + '_slides', 0, 100, this.fade);
		}
		else {
			this.show();
		}

		this.initiated = true;

		this.counter++;

		if(this.counter == parseInt(this.images.length) + 1) {
			this.stop();
			if(this.autoPlay && this.repeat == true) {
				this.counter = 0;
				this.start();
			}
		}
		else if(this.autoPlay) {
			thisSlideshow=this;
			this.timer=setTimeout(function() { thisSlideshow.start(); }, this.delay*1000);
			delete thisSlideshow;
		}
	}
	this.stop = function() {
		if(this.timer) {
			clearTimeout(this.timer);
		}
	}

	this.go = function(i) {
		if(this.counter != i+1) {
			this.counter = i;
			this.start();
		}
	}

	this.next = function() {
		this.stop();
		if(this.counter == this.images.length) {
			this.counter = 0;
		}
		this.start();
	}

	this.getNext = function() {
		if(this.counter+1 == this.images.length) {
			return 0;
		}
		return this.counter+1;
	}

	this.previous = function() {
		this.stop();
		if(this.counter == 1) {
			this.counter = this.images.length-1;
		}
		else {
			this.counter -= 2;
		}
		this.start();
	}

	this.getPrevious = function() {
		if(this.counter-1 <= 1) {
			return this.images.length-1;
		}
		return (this.counter - 2);
	}

	this.getPlayerControls = function() {

//		pagination_begin=((parseInt(this.pagingLimit) / 2) < 1) ? 1 : (parseInt(this.pagingLimit) / 2);

		pagination_begin=((this.counter - 3) < 1)? 1 : (this.counter - 3);
		pagination_total=((parseInt(this.counter) + 3) > this.images.length) ? this.images.length : (this.counter + 3);

		pagination_total=(pagination_total < this.pagingLimit) ? this.pagingLimit : pagination_total;
		pagination_begin=((pagination_total - pagination_begin) < this.pagingLimit) ? ((pagination_total - this.pagingLimit) ) : pagination_begin;
		pagination_total=(pagination_total > this.images.length) ? this.images.length : pagination_total;

//alert(pagination_total)
		this.html.push('<p class="page">');

		if(this.images.length > this.pagingLimit) {
			if(pagination_begin > 0) {
				this.html.push('<a href="#', (parseInt(this.counter) - parseInt(this.pagingLimit)), '" onclick="', this.id, '.go(', (parseInt(this.counter) - parseInt(this.pagingLimit)),')"  title="Previous set in slideshow">&#8249;&#8249;</a>');
			}
			else {
				this.html.push('<a href="#0" onclick="', this.id, '.go(0)" title="Previous set in slideshow">&#8249;&#8249;</a>');
			}
		}

		this.html.push('<a href="#'+this.getPrevious()+'" onclick="', this.id, '.previous()" title="Previous image in slide">&#8249;</a>');

		for(var i=pagination_begin; i<pagination_total; i++) {
		//for(var i=pagination_begin; i<this.images.length; i++) {
			this.html.push('<a href="#', i, '" onclick="', this.id, '.go(', i,')"');

			if(this.counter == i) {
				this.html.push(' class="active"');
			}
			
			if(this.images[i] && this.images[i].title != '' && this.images[i].title != '[object Object]') {
				this.html.push(' title="View the entry for ', this.images[i].title, '">', i+1, '</a>');
			}
			else if(!this.images[i]) {
			//	alert(i)
			}
			else {
				this.html.push(' title="View image ', i+1, '">', i+1, '</a>');
			}

			if((parseInt(this.counter) + parseInt(this.pagingLimit))-1 == i) {
				break;
			}
		}

		this.html.push('<a href="#'+this.getNext()+'" onclick="', this.id, '.next()" title="Next image in slide">&#8250;</a>');
		
		if(this.images.length > this.pagingLimit) {
			if((parseInt(this.counter) + parseInt(this.pagingLimit)) <= this.images.length) {
				this.html.push('<a href="#', (parseInt(this.counter) + parseInt(this.pagingLimit)), '" onclick="', this.id, '.go(', (parseInt(this.counter) + parseInt(this.pagingLimit)),')" title="Next set of pages">&#8250;&#8250;</a>');
			}
			else {
				this.html.push('<a href="#', (this.images.length-1), '" onclick="', this.id, '.go(', (this.images.length-1),')"  title="Previous set in slideshow">&#8250;&#8250;</a>');
			}
		}

		this.html.push('</p>');
	}

	this.load = function(src, w, h) {
		img = new Image(w, h);
		img.setAttribute('src', src);
	}
}

/**
 * Image constructor
 *
 */

function Slide() {
	this.title = null;
	this.description = null;
	this.src = null;
	this.width = null;
	this.height = null;
	this.credit = null;
	this.caption = null;
}