// Script that loads and populates Slideshow


var gm_s_curr = 0; // current active 
var gm_s_count; // total slides

// parse XML
function gm_s_parse(xml)
{
	var box = jQuery("#gm-slideshow");
	var ul;
	gm_s_count = jQuery(xml).find('slideshow').find('items').find('item').length;
	
	// config
	if (jQuery(xml).find('config').text() != '')
	{
		jQuery(box).css("background-image", "url('" + jQuery(xml).find('config').text() + "')");
	}
	jQuery(box).css("background-color",jQuery(xml).find('config').attr("bg"));

	// add controls
	jQuery(box).append("\
			<a href=\"#slideshow\" class=\"gm-s-left\"></a>\
			<a href=\"#slideshow\" class=\"gm-s-right\"></a>\
	");

	// add title
	jQuery(box).append("\
			<div class=\"gm-s-title\">\
				<span></span>\
			</div>\
	");
	
	// add image box 
	jQuery(box).append("\
			<div class=\"gm-s-img\">\
				<a href=\"#slideshow\"></a>\
			</div>\
	");

	// width of image box 
	var image_box = jQuery(box).find(".gm-s-img a");
	var count = jQuery(xml).find('slideshow').find('items').find('item').length;
	jQuery(image_box).css("width",(642 * count) + "px");

	// add images
	jQuery(xml).find('slideshow').find('items').find('item').each(function(i){
		jQuery(image_box).append("<img src=\"" + jQuery(this).find('imgUrl').text() + "\" alt=\"" + jQuery(this).find('title').text() + "\" width=\"642\"/>");
	});

	// tumbs holder
	jQuery(box).append("\
			<div class=\"gm-s-wrapper\">\
				<ul class=\"gm-s-thumb\">\
				</ul>\
			</div>\
	");

	// size thumbs holder
	var holder = jQuery(box).find(".gm-s-thumb");
	jQuery(holder).css("width",(131 * count) + "px");

	// add thumbs
	jQuery(xml).find('slideshow').find('items').find('item').each(function(i){
		jQuery(holder).append("\
				<li>\
					<a href=\"#slideshow\">\
						<img src=\"" + jQuery(this).find('thumb').text() + "\" width=\"109\" height=\"88\" rel=\"" + i + "\" />\
					</a>\
				</li>\
		");
	});

	// assign click on thumbs
	jQuery(box).find(".gm-s-thumb a").click(function(){
		gm_s_activate(Number(jQuery(this).find('img').attr("rel")));
	});

	// assign click on main image (next, next, next)
	jQuery(box).find(".gm-s-img a img").click(function(){
		if (jQuery(this).index() != gm_s_count - 1)
		{
			gm_s_activate(jQuery(this).index()+1);
		}
		else {
			gm_s_activate(0);
		}
	});

	// assign click navigation
	jQuery(".gm-s-left").click(function(){
		if (gm_s_curr > 0)
		{
			gm_s_activate(Number(gm_s_curr)-1);
		}
	});
	jQuery(".gm-s-right").click(function(){
		if (gm_s_curr < gm_s_count - 1)
		{
			gm_s_activate(Number(gm_s_curr)+1);
		}
	});

	// initial view (activate first)
	gm_s_activate(0)

}

// activates an item.
function gm_s_activate(id) {

	// current active
	gm_s_curr = id;

	// remove current active
	var thumb = jQuery("#gm-slideshow .gm-s-thumb");
	jQuery(thumb).find("a.active").removeClass("active");

	// activate next current
	jQuery(thumb).find("a:eq(" + id + ")").addClass("active");

	// update title
	jQuery(".gm-s-title").html( "<span>" + (id+1) + "/" + (gm_s_count) + "</span>" + jQuery(".gm-s-img a img:eq(" + id + ")").attr("alt"));

	// slide to correct image on big image box
	jQuery("#gm-slideshow .gm-s-img a").animate({
			left: -(642 * id) + "px"
		},
		600,
		'easeOutCubic'
	);

	// need to scroll?
	jQuery(thumb).animate({
			left: -(131 * id)
		},
		600,
		'easeOutCubic'
	);

	
}