var DWidgets = {

	init: function() {

		// Add toggle link
		$('.widget').find('.header').append('<a href="#" class="toggle" title="Toggle Widget">Toggle Widget</a>');

		// Add click behavior on toggle link
		$('.widget').find('a.toggle').click(function() {
												var widget = $(this).parents().get(1);
												var contentWidget = $(widget).find('.content');
												var stateClass = 'toggled';
												var fx = 'normal';

												if($(widget).hasClass(stateClass)) {
													$(widget).toggleClass(stateClass);
													$(contentWidget).slideDown(fx);
												} else {
													$(contentWidget).slideUp(fx, function(){
																							$(widget).toggleClass(stateClass);
																					   });
												};
												
												return false;
											});

		// Add ui.tabs
		var tabbedContent = $('.widget').find('ul.tabs');
		$(tabbedContent).parent().tabs({fx:{opacity: 'toggle'}});

		// Init slideshow
		DWidgets.Photos.initialize();
			
	},

	/* 
	 * Photos slideshow 
	 *
	 */
	Photos: {
		initialize : function(){
			var photos = $('.widget').find('.photos');
			var animation;

			// Show first image by default
			DWidgets.Photos.show(photos, 0);
			
			// add slideshow nav markup
			$(photos).append('<ul>' +
								'<li><a class="prev" href="#" title="Show previous photo">Previous</a></li>' +
								'<li><a class="play" href="#" title="Play slideshow">Play</a></li>' +
								'<li><a class="pause" href="#" title="Pause slideshow">Pause</a></li>' +
								'<li><a class="next" href="#" title="Show next photo">Next</a></li>' +
							'</ul>');

			// add slideshow behavior
			$(photos).find('ul a.prev').click(function() {
												var elm = $(this).parents().get(2);
												if($(elm).hasClass('status-play')){
													DWidgets.Photos.stopSlideshow(elm);
												}
												DWidgets.Photos.showPrev(elm)
												return false;
											  });
			$(photos).find('ul a.play').click(function() {
												var elm = $(this).parents().get(2);
												DWidgets.Photos.startSlideshow(elm);
												return false;
											  });
			$(photos).find('ul a.pause').click(function() {
												var elm = $(this).parents().get(2);
												DWidgets.Photos.stopSlideshow(elm);
												return false;
											  });
			$(photos).find('ul a.next').click(function() {
												var elm = $(this).parents().get(2);
												if($(elm).hasClass('status-play')){
													DWidgets.Photos.stopSlideshow(elm);
												}
												DWidgets.Photos.showNext(elm);
												return false;
											  });
										
			// add image click behaviour
			$(photos).find('ol a').click(function() {
											var elm = $(this).parents('.photos').get(0);
											if($(elm).hasClass('status-play')) {
												DWidgets.Photos.stopSlideshow(elm);
											} else {
												DWidgets.Photos.startSlideshow(elm);
											}
											return false;
										});
		},

		/* Start slideshow */
		startSlideshow : function(elm) {
			if( $(elm).hasClass('status-play') ) { return; }
			animation = setInterval(function(){DWidgets.Photos.showNext(elm);}, 2000);
			$(elm).addClass('status-play');
		},

		/* Stop slideshow */
		stopSlideshow : function(elm) {
			clearInterval(animation);
			$(elm).removeClass('status-play');
		},

		// Show a particulary image
		show : function(elm, index) {
			var items = DWidgets.Photos.getPhotos(elm);

			// prepare change on containers
			var currentItem = $(elm).find('ol li:visible');
			var nextImage = items[index];

			// toggle items
			$(currentItem).hide();
			$(nextImage).show();
		},

		// Show next image
		showNext : function(elm) {
			var items = DWidgets.Photos.getPhotos(elm);
			var current = DWidgets.Photos.getCurrentPhoto(elm);
			if(current < items.length - 1 ) {
				DWidgets.Photos.show(elm, current + 1);
			} else {
				DWidgets.Photos.show(elm, 0);
			}
		},

		// Show previous image
		showPrev : function(elm) {
			var items = DWidgets.Photos.getPhotos(elm);
			var current = DWidgets.Photos.getCurrentPhoto(elm);
			if(current > 0 ) {
				DWidgets.Photos.show(elm, current - 1);
			} else {
				DWidgets.Photos.show(elm, items.size() - 1);
			}
		},

		// Get all photos
		getPhotos : function(elm) {
			return $(elm).find('ol li');
		},

		// Get current phot index
		getCurrentPhoto: function(elm) {
			var currentPhoto = $(elm).find('ol li:visible');
			return $(elm).find('ol li').index(currentPhoto);
		}
		
	}
	
};

$(document).ready(function(){

	// Add class for jsenabled
	if(!($('html').hasClass('jsenabled'))){
		$('html').addClass('jsenabled');
	}

	// Initialize Widgets	
  if($('.widget').size()>0) {
	DWidgets.init();
  };
});

