/*------------------------------------------------------------
	Script Rental
------------------------------------------------------------*/
(function($){
$.extend({
	photoViewer : function(xmlpath, settings) {
		settings = $.extend({
			defaultPhotoNum : 0
		}, settings);

		$.get(xmlpath + '?r=' + new Date().getTime(), function(xml){
			convertXml(xml);
			generateThumbBox();
			generatePhotoBox();
			insertCaption();
			setControl();
		});

		var itemArray = [];

		/* Convert Photo Box & Thumnail Box */
		function convertXml(){
			$(arguments[0]).find('item').each(function(i, val){
				var item = $(this);
				itemArray[i] = {};
				itemArray[i].photo = new Image();
				itemArray[i].photo.src = item.find('photo').attr('src');
				itemArray[i].thumb = new Image();
				itemArray[i].thumb.src = item.find('thumb').attr('src');
				itemArray[i].caption = item.find('caption').text();
			});
		}

		/* Generate Thumnail Box */
		function generateThumbBox(){
			var thumbBoxHTML = '<div id="thumbCase"><ul>';
			$.each(itemArray, function(i, val){
				if(i == settings.defaultPhotoNum){
					thumbBoxHTML += '<li><a href="' + itemArray[i].photo.src + '" class="activeThumb"><img src="' + itemArray[i].thumb.src + '" alt="" width="93" height="70" /></a></li>';
				} else {
					thumbBoxHTML += '<li><a href="' + itemArray[i].photo.src + '"><img src="' + itemArray[i].thumb.src + '" alt="" width="93" height="70" /></a></li>';
				}
			});
			thumbBoxHTML += '</ul></div>';
			$('#photoBox').append(thumbBoxHTML);
		}

		/* Generate Photo Box */
		function generatePhotoBox(){
			var photoCase = $('#photoCase');
			photoCase.css({
				height : photoCase.height() + 'px',
				position : 'relative'
			}).html('<p class="activePhoto"><img src="' + itemArray[settings.defaultPhotoNum].photo.src + '" alt="" width="522" height="392" /></p>');
		}

		/* Insert Caption */
		function insertCaption(){
			$('#captionBox').html(itemArray[settings.defaultPhotoNum].caption);
		}

		/* Set Control */
		function setControl(){
			var photoCase = $('#photoCase');
			$('#thumbCase a').each(function(i, val){
				$(this).click(function(e){
					if(!$(this).hasClass('activeThumb')){
						photoCase.append('<p class="prepPhoto"><img src="' + itemArray[i].photo.src + '" alt="" width="522" height="392" /></p>');
						$('#photoCase p.activePhoto').fadeOut('normal', function(){
							$(this).remove();
							$('#photoCase p.prepPhoto').removeClass('prepPhoto').addClass('activePhoto');
						});
						$('#thumbCase a.activeThumb').removeClass('activeThumb');
						$('#thumbCase a:eq(' + i + ')').addClass('activeThumb');
						$('#captionBox').html(itemArray[i].caption);
					}
					e.preventDefault();
				})
			});
		}
	}
});
})($jq['1.3.2']);
