// requires jquery library

(function($) {

	$.fn.SSImageFlow = function(options) {
		var
			slides_el = this;

		function init() {
			var
				plugin_options = {};

			plugin_options.ImageFlowID = slides_el.attr('id');
			plugin_options.reflections = false;
			plugin_options.reflectionP = 0;
			plugin_options.onClick = function(evt) { handleImageFlowOnClick(evt); };

			// set the aspect ratio
			// if (options.size.width !== undefined && options.size.width !== undefined) {
			//  /* 115 is the height of the navigation */
			//  plugin_options.aspectRatio = (options.size.width) / (options.size.height);
			// }

			// middle alignment
			if (options.align !== undefined && options.align == 'middle') {
				plugin_options.reflectionP = 0.5;
			}
			
			// autoplay
			if (options.align !== undefined && options.autoplay) {
				plugin_options.slideshow = true;
				plugin_options.slideshowAutoplay = true;
			}
			

			// override any specific plugin options
			$.extend(plugin_options, options.plugin);

			// hide_slider
			if (options.hide_slider) { plugin_options.slider = false; }

			// move wrapping links to the image tag
			removeImageFlowWrappingLinks();
			
			// add outer div wrapper
			addSizeDivWrapper();

			var img_flow = new ImageFlow();
			img_flow.init(plugin_options);
		}
		
		function addSizeDivWrapper() {
			if (options.size.width !== undefined || options.size.height !== undefined) {
				
				slides_el.wrap('<div class="AWCImageFlowWrapper"></div>');
				var slide_wrapper = slides_el.parents('.AWCImageFlowWrapper');

				if (options.size.width !== undefined) {
					slide_wrapper.css({'width':options.size.width+'px'});
				}
				if (options.size.height !== undefined) {
					slide_wrapper.css({'height':(parseInt(options.size.height, 10))+'px'});
				}
				
				if (options.align !== undefined && options.align == 'middle') {
					slide_wrapper.css({'position':'relative'});
					
					if (options.reflections !== undefined && options.reflections) {
						// slides_el.css({'margin-top':'-65px'});
					} else {
						slides_el.css({'margin-top':'-115px'});
					}
				}
			}
			
		}
		

		function removeImageFlowWrappingLinks() {
			$(slides_el).children('a').each(function() {
				var
					a_tag = $(this),
					a_onclick = a_tag.get(0).onclick,
					img_tag = a_tag.children('img');

				img_tag.data({props: {href:a_tag.attr('href'), target:a_tag.attr('target'), onclick:a_onclick}});

				img_tag.insertAfter(a_tag);
				a_tag.remove();
			});

			$(slides_el).children('img').each(function(offset) {
				$(this).attr('data-origOffset', offset);
				$(slides_el).data('img-'+offset, $(this).data('props'));
			});
		}

		function handleImageFlowOnClick(onclick_evt) {

			var 
				evt = window.event || onclick_evt,
				evt_target = $(evt.target || evt.srcElement),
				image_offset = $(evt_target).data('origOffset'),
				img_data = slides_el.data('img-'+image_offset);
			
			if (img_data !== null && img_data !== undefined) {
				var should_click_href = true;
				if (img_data.onclick !== undefined && img_data.onclick !== null) {
					var return_val = img_data.onclick.apply(evt_target);
					should_click_href = false;
					if (return_val === false) {
						// onclick handler explicitly returned false
						should_click_href = false;
					} else if (img_data.href !== undefined && img_data.href !== '#') {
						// allow click-throughs even with an onclick
						should_click_href = true;
					}
				}
				
				
				if (should_click_href && img_data.href !== undefined) {
					if (img_data.target !== undefined && img_data.target.length > 0) {
						AWCow(img_data.href, window.width, window.height, '');
					} else {
						window.location = img_data.href;
					}
				}
			}
		}


		// init image flow
		init();
		
	
		
	};
	
	
})(jQuery);

