
var Promo = function(promos) {

	var c = $('#promo');
	var current = 0;
	var init = false;
	var timer = null;

	var build = function() {

		if(init) {
			c.empty();
		} else {
			init = true;
		}

		// current item
		var item = promos[current];

		// left col with image
		var col1 = $('<div>').attr('class', 'left');
		var img = $('<img>').attr({
			'src': '/store/images/' + item['image'],
			'alt': item['title']
		}).hide();

		col1.append(img);
		c.append(col1);

		// right col
		var col2 = $('<div>').attr('class', 'right');

		// title
		var title = $('<div>').attr('class', 'title').append($('<p>').text(item['title']));
		col2.append(title);

		// content holder
		var content = $('<div>').attr('class', 'content').hide();

		// summary
		var summary = $('<p>').text(item['summary']);
		content.append(summary);

		// button container
		var btns = $('<div>').addClass('btns');

		// page link
		if(item['relatedurl']) {
			var article = $('<div>').attr('class', 'btn-promo').append(
				$('<p>').append(
					$('<a>').attr({
						'href': item['relatedurl']
					}).text('View Products')
				)
			);
			btns.append(article);
		}

		// page url
		if(item['page']) {
			var article = $('<div>').attr('class', 'btn-promo').append(
				$('<p>').append(
					$('<a>').attr({
						'href': '/' + item['page']['pathname'],
						'title': item['page']['title']
					}).text('Read More')
				)
			);
			btns.append(article);
		}

		// product link
		if(item['product']) {
			var product = $('<div>').attr('class', 'btn-promo').append(
				$('<p>').append(
					$('<a>').attr({
						'href': '/products/' + item['product']['pathname']
					}).text('View Product')
				)
			);
			btns.append(product);
		}

		// video link
		if(item['video']) {
			var video = $('<div>').attr('class', 'btn-promo').append(
				$('<p>').append(
					$('<a>').attr({
						'href': '#flv_' + item['video']['filename']
					}).text('Watch Video').bind('click', function() {

						var e = $(this), f = e.attr('href').split('_').pop();

						var html = new Swf({
							'src': '/swf/player.swf',
							'height': 400,
							'width': 600,
							'flashvars': {
								'file': '/store/videos/' + f
							}
						}).html();

						new Popup('promovideo').open(html, {
							'height': 400,
							'width': 600
						});

						return false;

					})
				)
			);
			btns.append(video);
		}

		content.append(btns);
		col2.append(content);

		// build pagination
		var pn = $('<div>').attr('class', 'pagnation');

		// previous
		var p = $('<a>').attr({
			'href': '#previous'
		}).append($('<img>').attr({
			'src': '/images/pag-prev.gif',
			'alt': 'previous'
		})).bind('click', previous);
		
		pn.append(p);

		// promos
		for(var x = 0; x < promos.length; x += 1) {
			var src = (current == x) ? 'pag-on.gif': 'pag-off.gif';
			var y = $('<a>').attr({
				'href': '#promo-' + x
			}).append($('<img>').attr({
				'src': '/images/' + src,
				'alt': x
			})).bind('click', set);
			pn.append(y);
		}

		// next
		var n = $('<a>').attr({
			'href': '#next'
		}).append($('<img>').attr({
			'src': '/images/pag-next.gif',
			'alt': 'next'
		})).bind('click', next);
		pn.append(n);

		col2.append(pn);

		c.append(col2);

		// image
		$('#promo img').fadeIn();

		// right col
		$('#promo .content').fadeIn();

		// start interval
		start_timer();
	};

	// pagination controls
	var set = function() {
		var e = $(this), i = e.attr('href').split('-').pop();
		current = i;

		stop_timer();

		build();

		return false;
	};

	var next = function() {
		var t = promos.length - 1, n = current + 1;

		if(n > t) {
			current = 0;
		} else {
			current += 1;
		}

		stop_timer();

		build();

		return false;
	};

	var previous = function() {
		var t = promos.length, n = current - 1;

		window.clearInterval(timer);

		if(n < 0) {
			current = t - 1;
		} else {
			current -= 1;
		}

		stop_timer();

		build();

		return false;
	};

	var start_timer = function() {
		timer = window.setInterval(function() {
			next();
		}, 10000);
	};

	var stop_timer = function() {
		window.clearInterval(timer);
	};

	// build promo panel
	build();

};

