/*
kamome.js slideshow 1.1

The MIT License

Copyright (c) 2009 firstAudience.com : Keisuke Nakayama

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.

*/

var setSlideShow = function () {
	
	var configurations = [
		{
			elem_id :		'top_slideshow',
			slide_width :		693,
			slide_height :		250,
			stay :			5000,
			controll_panel : false, 
			panel_font_size : '90%'
		}
	]
	

	var Slideshow = function (target_node, config) {
		
		var spec = {
			elem_id :		config.elem_id	|| 'top_slideshow',
			slide_width :   	config.slide_width ? (parseInt(config.slide_width, 10) + 'px') : '640px',
			slide_height :		config.slide_width ? (parseInt(config.slide_height, 10) + 'px') : '480px',
			panel_font_size :	config.panel_font_size || '100%',
			stay :  		config.stay || 5000,
			fade_pace :		config.fade_pace || 60,// on a millisecond basis.
			controll_panel :	(config.controll_panel || config.controll_panel === undefined) ? true : false,
			play :			(config.play || config.play === undefined) ? true : false,
			to :			(config.to || config.to === undefined) ? true : false,
			elem_class_name :	config.elem_class_name || 'slideshow',
			play_list : function () {
				var results = [];
				
				dom.walk_the_DOM(target_node, function (node) {
					if (node.nodeType === 1) {
						if (node.tagName === 'LI') {
							results.push(node);
						} else if (node.tagName === 'A') {
							results[results.length - 1].href = node.getAttribute('href');
							results[results.length - 1].rel = node.getAttribute('rel');
						} else if (node.tagName === 'IMG') {
							results[results.length - 1].img = node;
							results[results.length - 1].alt = node.getAttribute('alt');
						}
					}
				});
				
				for (var i = 0; i < results.length; i += 1) {
					if (i === 0) {
						results[i].back = results[results.length - 1];
						results[i].foth = results[i + 1];
					} else if (i === results.length -1) {
						results[i].back = results[i - 1];
						results[i].foth = results[0];
					} else {
						results[i].back = results[i - 1];
						results[i].foth = results[i + 1];
					}
				}
				
				return results;
			}(),
			current_slide : 0,
			i : 0,
			op_level : 10,
			op_level_reset : 10,
			play_timer : {}
		}
		
		var build = function (target_node) {
			
			var setControll = function (dom_obj) {
				var target_node = dom_obj;
				var show_timer, hide_timer;
				
				var checkReverse = function () {
					spec.to = !spec.to;
					clearTimeout(spec.play_timer);
					preparation();
					setSlides(spec.to);
					panel.reverse_check.className = spec.to === true ? 'reverse_off' : 'reverse_on';
					spec.play_timer = setTimeout(playSlides, spec.stay);
				};
				
				var togglePlay = function (e) {
					var et = e.target || e.srcElement;
					var er = e.relatedTarget || e.toElement;
					if (et.className === 'pause_to_play') {
						spec.play = true;
						panel.play.className = 'play_to_pause';
						loadStatus();
						playSlides();
					} else if (et.className === 'play_to_pause') {
						spec.play = false;
						panel.play.className = 'pause_to_play';
						loadStatus();
						clearTimeout(spec.play_timer);
					}
				};
				
				var toNext = function (e) {
					clearTimeout(spec.play_timer);
					preparation();
					iInc(true);
					setSlides(spec.to);
					loadStatus();
					spec.play_timer = setTimeout(playSlides, spec.stay);
				};
				
				var toPrevious = function (e) {
					clearTimeout(spec.play_timer);
					preparation();
					iInc(false);
					setSlides(spec.to);
					loadStatus();
					spec.play_timer = setTimeout(playSlides, spec.stay);
				};
				
				var openURL = function () {
					if (spec.play_list[spec.current_slide].href) {
							if (spec.play_list[spec.current_slide].rel === 'newWin') {
									window.open(spec.play_list[spec.current_slide].href);
							} else {
									window.location = spec.play_list[spec.current_slide].href;
							}
							return true;
					} else {
							return false;
					}
				};
				
				var loadStatus = function () {
					if (spec.play_list[spec.current_slide].href) {
							caption_icon.className = (spec.play_list[spec.current_slide].rel === 'newWin' ? 'newWin' : 'normal');
					} else {
							caption_icon.className = '';
					}
					caption_text.innerHTML = spec.play_list[spec.current_slide].alt || 'untitled...';
					nombre_text.nodeValue = (spec.current_slide + 1) + ' / ' + spec.play_list.length;
				};
				
				var show_panel = function () {
					show_timer = setTimeout(function () {
							panel.style.display = 'block';
					}, 100);
					clearTimeout(hide_timer);
				};
				
				var hide_panel = function () {
					hide_timer = setTimeout(function () {
							panel.style.display = 'none';
					}, 100);
					clearTimeout(show_timer);
				};
				
				var mask = document.createElement('DIV');
				mask.className = 'mask';
				mask.style.width = spec.slide_width;
				mask.style.height = spec.slide_height;
				
				var panel = document.createElement('DIV');
				var side = document.createElement('DIV');
				var info_disp = document.createElement('P');
				var caption = document.createElement('STRONG');
				var caption_text = document.createElement('SPAN');
				var caption_icon = document.createElement('SPAN');
				var nombre_text = document.createTextNode('0 / 0');
				var controll = document.createElement('UL');
				panel.play = document.createElement('LI');
				panel.reverse_check = document.createElement('LI');
				panel.next = document.createElement('LI');
				panel.previous = document.createElement('LI');
				panel.nombre = document.createElement('LI');
				
				panel.play.innerHTML = '<span>Play or Pause</span>';
				panel.reverse_check.innerHTML = '<span>Reverse</span>';
				panel.next.innerHTML = '<span>forth</span>';
				panel.previous.innerHTML = '<span>back</span>';
				caption_icon.innerHTML = '&nbsp;'
				
				panel.className = 'panel';
				panel.play.className = 'play_to_pause';
				panel.reverse_check.className = 'reverse_off';
				panel.next.className = 'foth';
				panel.previous.className = 'back';
				panel.nombre.className = 'nombre';
				info_disp.className = 'info_disp';
				
				panel.nombre.style.fontSize = spec.panel_font_size;
				info_disp.style.fontSize = spec.panel_font_size;
				
				caption.appendChild(caption_icon);
				caption.appendChild(caption_text);
				info_disp.appendChild(caption);
				panel.appendChild(info_disp);
				controll.appendChild(panel.previous);
				controll.appendChild(panel.next);
				panel.nombre.appendChild(nombre_text);
				controll.appendChild(panel.nombre);
				controll.appendChild(panel.play);
				controll.appendChild(panel.reverse_check);
				panel.appendChild(controll);
				mask.appendChild(panel);
				
				target_node.appendChild(mask);
				
				dom.addListener(mask, 'mouseover', show_panel, false);
				dom.addListener(mask, 'mouseout', hide_panel, false);
				dom.addListener(panel.reverse_check, 'mouseup', checkReverse, false);
				dom.addListener(panel.play, 'mouseup', togglePlay, false);
				dom.addListener(panel.next, 'mouseup', toNext, false);
				dom.addListener(panel.previous, 'mouseup', toPrevious, false);
				dom.addListener(caption, 'click', openURL, false);
				
				target_node.reloadStatus = loadStatus;
				target_node.mask = mask;
				
				return target_node;
			};
			
			if (spec.controll_panel !== false) {
				spec.play_list.controll_panel = setControll(target_node);
			}
			
			start();
		};
		
		var preparation = function () {
			spec.op_level = spec.op_level_reset;
			spec.play_list[spec.current_slide].style.zIndex = '1';
			dom.setStyleOpacity(spec.play_list[spec.current_slide], spec.op_level_reset);
			spec.play_list[spec.current_slide].foth.style.zIndex = '1';
			spec.play_list[spec.current_slide].foth.style.visibility = 'hidden';
			dom.setStyleOpacity(spec.play_list[spec.current_slide].foth, spec.op_level_reset);
			spec.play_list[spec.current_slide].back.style.zIndex = '1';
			spec.play_list[spec.current_slide].back.style.visibility = 'hidden';
			dom.setStyleOpacity(spec.play_list[spec.current_slide].back, spec.op_level_reset);
		}
		
		var setSlides = function (to) {
			if (to !== false) {
				spec.play_list[spec.current_slide].back.style.zIndex = '1';
				spec.play_list[spec.current_slide].back.style.visibility = 'hidden';
				spec.play_list[spec.current_slide].style.zIndex = '3';
				spec.play_list[spec.current_slide].style.visibility = 'visible';
				spec.play_list[spec.current_slide].foth.style.zIndex = '2';
				spec.play_list[spec.current_slide].foth.style.visibility = 'visible';
			} else {
				spec.play_list[spec.current_slide].foth.style.zIndex = '1';
				spec.play_list[spec.current_slide].foth.style.visibility = 'hidden';
				spec.play_list[spec.current_slide].style.zIndex = '3';
				spec.play_list[spec.current_slide].style.visibility = 'visible';
				spec.play_list[spec.current_slide].back.style.zIndex = '2';
				spec.play_list[spec.current_slide].back.style.visibility = 'visible';
			}
		};
		
		var iInc = function (bool) {
			if (bool === true) {
				if (spec.current_slide < spec.play_list.length - 1) {
						spec.current_slide += 1;
				} else if (spec.current_slide === spec.play_list.length - 1) {
						spec.current_slide = 0;
				}
			} else if (bool === false) {
				if (spec.current_slide > 0) {
						spec.current_slide -= 1;
				} else if (spec.current_slide === 0) {
						spec.current_slide = spec.play_list.length - 1;
				}
			}
		}
		
		var playSlides = function fade () {
			if (spec.play === false) {
				return;
			} else if (spec.op_level > 0) {
				spec.op_level -= 0.5;
				dom.setStyleOpacity(spec.play_list[spec.current_slide], spec.op_level);
				spec.play_timer = setTimeout(fade, spec.fade_pace);
			} else {
				spec.op_level = spec.op_level_reset;
				dom.setStyleOpacity(spec.play_list[spec.current_slide], spec.op_level_reset);
				iInc(spec.to);
				setSlides(spec.to);
				if (spec.play_list.controll_panel) {
						spec.play_list.controll_panel.reloadStatus();
				}
				spec.play_timer = setTimeout(fade, spec.stay);
			}
			return;
		};
		
		var start = function () {	
			spec.play = true;
			setSlides(spec.to);
			if (spec.play_list.controll_panel) {
					spec.play_list.controll_panel.reloadStatus();
			}
			spec.play_timer = setTimeout(playSlides, spec.stay);
		};
		
		var setSlideScreenSize = function () {
			var ss1 = document.createElement('style');
			var def = '#' + spec.elem_id + '{' + 'width:' + spec.slide_width + '; height:' + spec.slide_height + '; overflow: hidden; }';
			ss1.setAttribute("type", "text/css");
			if (ss1.styleSheet) {   // IE
			    ss1.styleSheet.cssText = def;
			} else {                // the world
			    var tt1 = document.createTextNode(def);
			    ss1.appendChild(tt1);
			}
			var hh1 = document.getElementsByTagName('head')[0];
			hh1.appendChild(ss1);
		}();
		
		build(target_node);
	};
	
	var dom = {
		walk_the_DOM : function walk(node, func) {
			func(node);
			node = node.firstChild;
			while (node) {
				walk(node, func);
				node = node.nextSibling;
			}
		},
		setStyleOpacity : function (elem, palam) {
			elem.style.filter = 'alpha(opacity=' + (palam * 10) + ')';
			elem.style.MozOpacity = palam / 10;
			elem.style.opacity = palam / 10;
		},
		addListener : function (elem, eventType, func, cap) {
			if (elem.addEventListener) {
				elem.addEventListener(eventType, func, cap);
			} else if (elem.attachEvent) {
				elem.attachEvent('on' + eventType, func);
			} else {
				alert('Sorry. This Page does not support your browser. ');
				return false;
			}
			return elem;
		},
		removeListener : function (elem, eventType, func, cap) {
			if (elem.removeEventListener) {
				elem.removeEventListener(eventType, func, cap);
			} else if (elem.detachEvent) {
				elem.detachEvent('on' + eventType, func);
			} else {
				return false;
			}
			return elem;
		}
	};
	
	dom.addListener(window, 'load', function () {
		for (var i = 0; i < configurations.length; i++) {
			var node = document.getElementById(configurations[i].elem_id);
			if (node) {
				new Slideshow(node, configurations[i]);
			}
		}
	}, false);
}();

