

	/**
	 *
	 * ww_imagecycle
	 * @author: Wido Widlewski <wido@websimplex.de>
	 *
	 *
	 */

	jQuery(document).ready(function (){
	
		/* display the images to cycle */
//		jQuery('.aniImages').show();
		
		var timeout_value = parseInt(jQuery(document).find('div.ww_imagecycle_timeout').html());
		var refresh_value = 750;
		var speed_value = parseInt(jQuery(document).find('div.ww_imagecycle_speed').html());
		var autostop_value = parseInt(jQuery(document).find('div.ww_imagecycle_autostop').html());
		
		/* get the content for the zoom icon */
		var zoom_icon = '';
		zoom_icon = jQuery('#ww_imagecycle_zoom').html();
						
				
		/* get the content of the navigation */		
		var navigation = jQuery(document).find('#ww_imagecycle_navigation').html();
				
				
		/* try to test, whether the navigation exists in DOM, thanks to hint from Ingo Franzen */
				
		/* if the navigation is not empty => navigation will be displayed */
		if ((navigation != '') && (navigation != null) && (navigation != undefined))
		{
			/* navigation is hidden at the beginning via CSS, to avoid problem with deactivated JS */
			jQuery('#ww_imagecycle_navigation').show();
			
			/* get the number of images in the slideshow */
			var images = jQuery('#ww_imagecycle_container').find('img');
			
			/* get the image height to align the navigation to the bottom */
			var image_heights = (parseInt(jQuery('#ww_imagecycle_container').find('img').height()) * (images.length - 1));
			var image_width = parseInt(jQuery('#ww_imagecycle_container').find('img').width());
			
			var navigation_height = parseInt(jQuery('#ww_imagecycle_navigation').height());
			var navigation_width = parseInt(jQuery('#ww_imagecycle_navigation').width());
			
			var navigation_offset = jQuery('#ww_imagecycle_navigation').offset();
			
			var navigation_top = navigation_offset['top'];
			var navigation_left = navigation_offset['left'];
			
			/* IE calculates things somehow strange ?!?! ... */
			if(jQuery.browser.msie)
			{
				var new_top = parseInt(navigation_top - image_heights - navigation_height - 11);
				var new_left = parseInt(navigation_left - navigation_width);			
			}
			else
			{
				var new_top = parseInt(navigation_top - image_heights - navigation_height - 2);
				var new_left = parseInt(navigation_left + image_width - navigation_width - 0);
			}
	
			jQuery('#ww_imagecycle_navigation').css('top', new_top + 'px');
			jQuery('#ww_imagecycle_navigation').css('left', new_left + 'px');
			
			/* append the imagecounter data to the prevnext div, initialize with 1/(images.length) */
			jQuery('#ww_imagecycle_prevnext').append('<div id="ww_imagecycle_counter"><p>1/' + images.length + '</p></div>');
			
			
			/* set an interval to display the current slideshow object number */
			refresh = window.setInterval(refresh_navigation, refresh_value);
		}
				
		/* the cycle function with some options */
		jQuery('#ww_imagecycle_container').cycle({
			fx: 'fade', 
			prev: '#ww_imagecycle_prev_link',
			next: '#ww_imagecycle_next_link',
			timeout: timeout_value, 
			speed: speed_value,
			autostop: autostop_value
		});
		
		
		
		
		/* refresh the navigation, to show the current slideshow object number and the zoom icon, if a big image exists */
		function refresh_navigation() {
		
			var counter = 0;
			var count_all = jQuery('#ww_imagecycle_container').children().length;
			var element = '';
			
			/* hide the zoome icon */
			jQuery('#ww_imagecycle_zoom').hide('');
						
			/* foreach slideshow elements */
			jQuery('#ww_imagecycle_container').children().each(function(){
				
				counter = counter + 1;
				
				if (jQuery(this).css('display') != 'none')
				{
					//element = jQuery(this);
					
					/* overwrite the counter data with the current element, x/y */
					jQuery('#ww_imagecycle_counter').find('p').html(counter + '/' + count_all);
					
					/* some content in the var zoom_icon => zoom icon will be shown, if big image exists */
					if (zoom_icon != '')
					{
						/* get the href */
						var href = '';
						
						/* slideshow object is a hyperlink */
						if (jQuery(this).attr('href'))
						{
							href = jQuery(this).attr('href');
						}
						
						/* if slideshow element is a hyperlink */
						if (href.length > 0)
						{
							/* loook for image extensions */
							url_string = /\.jpg$|\.jpeg$|\.png$|\.gif$|\.bmp$/;
							
							image_ext = href.toLowerCase().match(url_string);
							
							/* check, wheather element is a link and href points to an image */
							if (image_ext == '.jpg' || image_ext == '.jpeg' || image_ext == '.png' || image_ext == '.gif' || image_ext == '.bmp')
							{
								jQuery('#ww_imagecycle_zoom').show();
								
								/*
								jQuery('#ww_imagecycle_zoom').html('');
								
								// copy the link into the zoom_div 
								var link_copy = element.clone();
								// remove the slideshow image from the link
								link_copy.find('img').remove();
								// append the zoom_icon to the link
								link_copy.append(zoom_icon);
								
								jQuery('#ww_imagecycle_zoom').html(link_copy);
								*/
								
								/* simulate a click to the slideshow object */
								//jQuery('#ww_imagecycle_zoom').click(function(){
									
									//element.click();
									
								//});
								
								/*
								jQuery('#ww_imagecycle_zoom').mouseover(function(){
									jQuery(this).css('cursor', 'pointer');
								});
								
								jQuery('#ww_imagecycle_zoom').mouseout(function(){
									jQuery(this).css('cursor', '');
								});
								*/
								
							}
						}
						
					}
					
				}
		
			});
			
			
		} //function refresh_navigation
		
		
		
		/* if link is clicked, pause the cycle animation */
		jQuery('#ww_imagecycle_container').find('a').click( function(){
			
			jQuery('#ww_imagecycle_container').cycle('pause');
			
			/* clear the interval */
			if (navigation != '')
			{
				window.clearInterval(refresh);
			}
		
		});
		
		/* resume cycle animation, if mouse is over link */
		jQuery('#ww_imagecycle_container').find('a').mouseover(function(){
		
			jQuery('#ww_imagecycle_container').cycle('resume');
			
			/* set the interval */
			if (navigation != '')
			{
				refresh = window.setInterval(refresh_navigation, refresh_value);
			}
		
		});
		
	});
