(function ($) {
	$.fn.extend({
		xmlSlideShow: function (options) {	
			
			var defaults = {
				autoplay: true
			};
			
			/* options */
			var options = $.extend (defaults, options);
			
			/* relevant code goes here */

			return this.each (function () {
				var obj = $(this);
				var o = options;
				var images = [];
				var descriptions = [];
				var links = [];
				var xmlData = [];
				var obj_id = '#' + $(this).attr('id') + ' ';
				var currentID = 0;
				
				/*
var openLink = function() {
					window.location = links[currentID];
				}
*/
			
				var viewPicture = function(id,sg) {

					if (sg == 1) o.autoplay = false;
					
					$(obj_id + ".left_content").animate({opacity:0.0}, 'fast');
					$(".ig_i_con img").animate({opacity:0.0}, 'fast', function() {
						$(this).attr('src', images[id]).load(function() {
/* 							$(obj_id + '.ig_i_con a').attr('href', links[id]); */
							currentID = id;
							$(obj_id+'.left_content').html(descriptions[id]);
							

							$(obj_id+".right_content ul li").removeClass('active');
							$(".l_for_i_" + id).addClass('active');
							$(this).animate({opacity:1.0}, 'medium', function() {
								$(obj_id + '.left_content').animate({opacity:1.0}, 'fast');
							});
						});

					});
					
					if (o.autoplay) 
						setTimeout(function() { if (o.autoplay) viewPicture ( (id + 1) % images.length ); }, 4900);
					var target = $(xmlData[id]).find('weblink').attr('target');
					if (target != 'false') {
						$('.extern_link').attr('target', target);
					} else {
						$('.extern_link').attr('target', '_self');
					}
				}
				
				var setupGallery = function() {			
					var xmlD = $.parseXML(o.xml);	
					$(xmlD).find("item").each(function() {
						images.push ($(this).find('file').text());
						descriptions.push ( $(this).find('desc').text() );
						links.push ( $(this).find('weblink').text() );
						xmlData.push($(this));
					});
/* 					console.log(links);	 */
				}
				
				
				
				var setupMarkup = function() {
					obj.html('');
					/* append picture 1 */
					obj.append('<div class="ig_i_con"><a href="javascript:void(0);" class="extern_link"><img src="'+images[0]+'" alt="Image Gallery" /></a></div>');
					obj.append('<div class="ig_contents"><div class="left_content">'+descriptions[0]+'</div><div class="right_content"><ul></ul></div></div>');
				}
				
				var setupNav = function() {
					$(".right_content ul").append('<li class="l_for_i_0 active"><div class="ig_gal_btn"><a href="javascript:void(0);">1</a></div></li>');
					
					for (var i = 2; i <= images.length; ++i) {
						$(".right_content ul").append('<li class="l_for_i_'+(i-1)+'"><div class="ig_gal_btn"><a href="javascript:void(0);">' + i + '</a></div></li>');
					}
				}
				
				start = true;
				
				var startSlideshow = function(ind) {
					if (o.autoplay) 
						setTimeout(function() { if (o.autoplay) viewPicture ( (ind + 1) % images.length ); }, 4900);
				}
				
				var buttonClickHandler = function(obj) {
					var array = $(obj).parent().attr('class').split('_');
					var index = array [array.length - 1];
					viewPicture(index,1);
				}
				
									
				
				setupGallery();
				setupMarkup();
				setupNav();
				
				/* setup the clickables */
				$(".ig_gal_btn").click(function() { buttonClickHandler($(this)); });
				$(".extern_link").click(function() {
					if ($(xmlData[currentID]).find('thickbox').text() == 'true') {
						var thickboxTitle = $(xmlData[currentID]).find('name').text();
						if (thickboxTitle != false && thickboxTitle != 'undefined' )
							tb_show(thickboxTitle, links[currentID]);
						else
							tb_show('',links[currentID]);
					} else {
						if ($(this).attr('target') == '_blank') 
							window.open(links[currentID], '_blank');
						else
							window.location = links[currentID];
					}
				});

				
				var divWidth = parseInt($(obj_id+".ig_contents").width());
				var rightWidth = parseInt($(obj_id+".right_content").width());
				var rightRealWidth = ($(obj_id+".right_content ul li:last").position().left + 25) -$(obj_id+".right_content ul li:first").position().left;
				rightWidth = rightRealWidth;
				$(obj_id+".right_content").css('width', rightWidth + 5);
				$(obj_id+".right_content ul").css('width', rightWidth + 5);		
				$(obj_id+".right_content").css('margin-left', (divWidth - rightWidth - 5) + 'px');
				
				/*
					Make it so that the margin left + width of div is == 699
					rightWidth + left_margin = divWidth
					divWidth - rightWidth = leftMargin
				*/
/* 				alert(left_margin + '  ' + add_left); */
				
				startSlideshow(0);
			});
		}
	});
}) (jQuery);
