var wescompapers = {};
wescompapers.Movies = {};
wescompapers.Movies.Gallery = function () {
	function Gallery() {
		this._jq_elements_cached = $();
		this._jq_main_image = $();
		this._position = 0;
		this._programs = [];
	}
	Gallery.__instance = null;

	Gallery.getInstance = function () {
		if (wescompapers.Movies.Gallery.__instance === null) {
			wescompapers.Movies.Gallery.__instance = new wescompapers.Movies.Gallery();
		}

		return wescompapers.Movies.Gallery.__instance;
	};

	Gallery.prototype.init = function () {
		var that = this;
		that._jq_main_image = $('.mvMainTeaser > .main_image');

		$(that).bind('wc_image_loaded', function () {
			that._jq_main_image.fadeTo(200, 1, function () {
			});
		});

		$(that).bind('wc_position_changed', function () {
			that._jq_main_image.fadeTo(200, 0.001, function () {
				that._updateGallery();
			});
		});

		that._jq_elements_cached = $('.wc-movies');

		$.getJSON("http://movies.bendbulletin.com/movies/gallery_page?output=json&callback=?", null, function (data) {
			that.__init(data);
		});
	};

	Gallery.prototype.forward = function () {
		var that = this;
		var pos = that._position + 1;
		if (pos >= that._programs.length) {
			pos = 0;
		}
		that._position = pos;
		$(that).trigger('wc_position_changed', that._position);
	};

	Gallery.prototype.backward = function () {
		var that = this;
		var pos = that._position - 1;
		if (pos < 0) {
			pos = that._programs.length - 1;
		}
		that._position = pos;
		$(that).trigger('wc_position_changed', that._position);
	};

	Gallery.prototype.setPosition = function (pos) {
		var that = this;
		if (pos < 0) {
			pos = 0;
		}
		if (pos >= that._programs.length) {
			pos = that._programs.length - 1;
		}
		that._position = pos;
		$(that).trigger('wc_position_changed', that._position);
	};

	Gallery.prototype.__init = function (gallery_data) {
		var that = this;
		that._programs = gallery_data.programs.program;

		for (var i = that._programs.length - 1; i >= 0; i--) {
			var li = Gallery._createPositionElement(that, i);
			$('.wc-movies-nav > ul > .arrow:first').after(li);
		}

		$(that).trigger('wc_ready');
	};

	Gallery._createPositionElement = function (instance, i) {
		var a = $('<a href="#">' + String(i + 1) + '</a>');
		a.click(function () {
			instance.setPosition(this._gallery_position);
			$(instance).trigger('wc_ui_click');
			return false;
		});

		a.get(0)._gallery_position = i;
		var li = $('<li></li>').append(a);

		if(i == 0) {
			li.addClass('active');
		}

		return li;
	};

	Gallery.prototype._updateGallery = function () {
		var that = this;

		var program = that._programs[that._position];
		that._jq_elements_cached.each(function (i) {
			var el = $(this);

			if (el.is('.wc-movies-gallery-title')) {
				that._updateTitle(program, el);
			}

			if (el.is('.wc-movies-gallery-caption')) {
				that._updateCaption(program, el);
			}

			if (el.is('.wc-movies-gallery-trailer')) {
				that._updateTrailer(program, el);
			}

			if (el.is('.wc-movies-gallery-detail')) {
				that._updateDetail(program, el);
			}

			if (el.is('.wc-movies-gallery-fandango')) {
				that._updateFandango(program, el);
			}
		});

		$('.wc-movies-nav > ul > li').each(function (i) {
			var li = $(this);

			if (!li.is('.arrow')) {
				if (i - 1 === that._position) {
					li.addClass('active');
				} else {
					li.removeClass('active');
				}
			}
		});
	};

	Gallery.prototype._updateTitle = function (program, el) {
		var that = this;

		if (el.is('h2')) {
			el.empty();
			el.append(program.title);
		}

		if (el.is('img')) {
			el.attr('alt', program.title);
			el.attr('src', program.image_url + '?w=430');
			el.get(0).onload = function () {
				$(that).trigger('wc_image_loaded');
			};
		}
	};

	Gallery.prototype._updateCaption = function (program, el) {
		var that = this;

		el.empty();
		el.append(program.image_caption);
	};

	Gallery.prototype._updateTrailer = function (program, el) {
		var that = this;

		if(program.trailer !== null && program.trailer !== undefined && program.trailer.length > 0) {
			el.attr('title', 'view trailer: ' + program.title);
			el.attr('href', program.trailer);
			el.parent().parent().removeClass('gry');
			el.unbind('click');
		} else {
			el.attr('title', '');
			el.attr('href', '#');
			el.click(function() {
				return false;
			});
			el.parent().parent().addClass('gry');
		}
	};

	Gallery.prototype._updateDetail = function (program, el) {
		var that = this;

		el.attr('title', 'view movie times and locations: ' + program.title);
		el.attr('href', '/apps/pbcs.dll/section?Profile=1048&ExpNodes=1020&Category=MOVIES&content=detail&mid=' + program.TMSId);
	};

	Gallery.prototype._updateFandango = function (program, el) {
		var that = this;

		if(program.fandango !== null && program.fandango !== undefined && program.fandango.length > 0) {
			el.attr('title', 'buy tickets online: ' + program.title);
			el.attr('href', program.fandango);
			el.parent().parent().removeClass('gry');
			el.unbind('click');
		} else {
			el.attr('title', '');
			el.attr('href', '#');
			el.click(function() {
				return false;
			});
			el.parent().parent().addClass('gry');
		}
	};

	return Gallery;
}();

$(document).ready(function () {
	var movies = wescompapers.Movies.Gallery.getInstance();
	var set_interval_id = null;

	$(movies).bind('wc_ready', function () {
		set_interval_id = window.setInterval(function () {
				movies.forward();
			},
			8000
		);
	});

	$(movies).bind('wc_ui_click', function () {
		if (set_interval_id !== null) {
			window.clearInterval(set_interval_id);
			set_interval_id = null;
		}
	});

	$('.wc-movies-nav > ul > .arrow > a:first').click(function () {
		movies.backward();
		$(movies).trigger('wc_ui_click');
		return false;
	});

	$('.wc-movies-nav > ul > .arrow > a:last').click(function () {
		movies.forward();
		$(movies).trigger('wc_ui_click');
		return false;
	});

	movies.init();
});




