﻿/*
kamome.js slideshow 1.0

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 slideshow1 = slideshow({
		elem_id : 'top_slideshow',
		slide_width : 600,
		slide_height : 300,
		stay : 5000,
		fade_pace : 60,
		controll_panel : true,
		play : true,
		to : true,
		elem_class_name : 'slideshow'
	});
};

var slideshow = function (spec) {
	
	var
	stack,
	i = 0,
	op_level = 10,
	op_level_reset = 10,
	play_timer
	;
	
	var build = function () { // once each cycle.
		var that;
		
		var checkSpec = function () {
			if (spec.controll_panel !== false) {
				spec.controll_panel = true;
			}
			spec.elem_class_name = spec.elem_class_name || 'slideshow';
			if (spec.to !== false) {
				spec.to = true;
			}
			if (spec.elem_id) {
				that = document.getElementById(spec.elem_id);
			} else {
				alert('\'elem_id\' is not found.');
				return false;
			}
			return true;
		};
		
		var getStack = function () {
			var results = [];
			
			dom.walk_the_DOM(that, 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;
		};
		
		var setControll = function (dom_obj) {
			var that = dom_obj;
			var show_timer, hide_timer;
			
			var checkReverse = function () {
				spec.to = !spec.to;
				clearTimeout(play_timer);
				changeDirection();
				setSlides(spec.to);
				panel.reverse_check.className = spec.to === true ? 'reverse_off' : 'reverse_on';
				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(play_timer);
				}
			};
			
			var toNext = function (e) {
				clearTimeout(play_timer);
				changeDirection();
				iInc(true);
				setSlides(spec.to);
				loadStatus();
				play_timer = setTimeout(playSlides, spec.stay);
			};
			
			var toPrevious = function (e) {
				clearTimeout(play_timer);
				changeDirection();
				iInc(false);
				setSlides(spec.to);
				loadStatus();
				play_timer = setTimeout(playSlides, spec.stay);
			};
			
			var openURL = function () {
				if (stack[i].href) {
					if (stack[i].rel === 'newWin') {
						window.open(stack[i].href);
					} else {
						window.location = stack[i].href;
					}
					return true;
				} else {
					return false;
				}
			};
			
			var loadStatus = function () {
				if (stack[i].href) {
					caption_icon.className = (stack[i].rel === 'newWin' ? 'newWin' : 'normal');
				} else {
					caption_icon.className = '';
				}
				caption_text.innerHTML = stack[i].alt || 'untitled...';
				nombre_text.nodeValue = (i + 1) + ' / ' + stack.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';
			
			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);
			
			that.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);
			
			that.reloadStatus = loadStatus;
			that.mask = mask;
			
			return that;	
		};
		
		checkSpec();
		stack = getStack();
		
		if (spec.controll_panel !== false) {
			stack.controll_panel = setControll(that);
		}
		
		start();
		
		return true;
	};
	
	var changeDirection = function () {
		op_level = op_level_reset;
		stack[i].style.zIndex = '1';
		dom.setStyleOpacity(stack[i], op_level_reset);
		stack[i].foth.style.zIndex = '1';
		stack[i].foth.style.visibility = 'hidden';
		dom.setStyleOpacity(stack[i].foth, op_level_reset);
		stack[i].back.style.zIndex = '1';
		stack[i].back.style.visibility = 'hidden';
		dom.setStyleOpacity(stack[i].back, op_level_reset);
	}
	
	var setSlides = function (to) {
		if (to !== false) {
			stack[i].back.style.zIndex = '1';
			stack[i].back.style.visibility = 'hidden';
			stack[i].style.zIndex = '3';
			stack[i].style.visibility = 'visible';
			stack[i].foth.style.zIndex = '2';
			stack[i].foth.style.visibility = 'visible';
		} else {
			stack[i].foth.style.zIndex = '1';
			stack[i].foth.style.visibility = 'hidden';
			stack[i].style.zIndex = '3';
			stack[i].style.visibility = 'visible';
			stack[i].back.style.zIndex = '2';
			stack[i].back.style.visibility = 'visible';
		}
	};
	
	var iInc = function (bool) {
		if (bool === true) {
			if (i < stack.length - 1) {
				i += 1;
			} else if (i === stack.length - 1) {
				i = 0;
			}
		} else if (bool === false) {
			if (i > 0) {
				i -= 1;
			} else if (i === 0) {
				i = stack.length - 1;
			}
		}
	}
	
	var playSlides = function fade () {
		if (spec.play === false) {
			return;
		} else if (op_level > 0) {
			op_level -= 0.5;
			dom.setStyleOpacity(stack[i], op_level);
			play_timer = setTimeout(fade, spec.fade_pace);
		} else {
			op_level = op_level_reset;
			dom.setStyleOpacity(stack[i], op_level_reset);
			iInc(spec.to);
			setSlides(spec.to);
			if (stack.controll_panel) {
				stack.controll_panel.reloadStatus();
			}
			play_timer = setTimeout(fade, spec.stay);
		}
		return;
	};
	
	var start = function () {	
		spec.play = true;
		setSlides(spec.to);
		stack.controll_panel.reloadStatus();
		play_timer = setTimeout(playSlides, spec.stay);
	};
	
	var setSpec = function () {
		spec.slide_width = parseInt(spec.slide_width, 10) + 'px';
		spec.slide_height = parseInt(spec.slide_height, 10) + 'px';
		spec.stay = spec.stay || 5000;
		spec.fade_pace = spec.fade_pace || 60; // on a millisecond basis.
	} ();
	
	var ss = document.styleSheets[0];
	var elem_css_selector = "#" + spec.elem_id;
	var elem_css_value = "width:" + spec.slide_width + ';' + "height:" + spec.slide_height +';';
	var elem_css_rule = elem_css_selector + '{' + elem_css_value + '}';
	if (document.all) {
		// IE
		ss.addRule(elem_css_selector, elem_css_value);
		} else {
		// FireFox
		ss.insertRule(elem_css_rule, ss.cssRules.length);
	}

	dom.addListener(window, 'load', build, false);
}

var dom = {
	walk_the_DOM : function walk(node, func) {
		func(node);
		node = node.firstChild;
		while (node) {
			walk(node, func);
			node = node.nextSibling;
		}
	},
	getElementsByAttribute : function (attribute, value, tagname) {
		var all = document.getElementsByTagName(tagname);
		var elements = [];
		for (i = 0; i < all.length; i += 1) {
			if (attribute === 'class') {
				var element = all[i].getAttribute("class") || all[i].getAttribute("className");
				if ( element === value) {
					elements.push(all[i]);
				}
			} else if (all[i].getAttribute(attribute) === value) {
				elements.push(all[i]);
			}(i);
		};
		return elements;
	},
	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('Please update your browser in order to view this page correctly.');
			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;
	},
	logging : function (obj) {
		var str;
		var that = this;
		
		if (!(that.flag)) {
			that.p = document.createElement('P');
			that.p.style.fontSize = '80%';
			that.p.style.color = '#0033ff';
			that.node = document.getElementsByTagName('body')[0].appendChild(that.p);
			that.flag = true;
		}
		
		str = document.createTextNode(obj.toString() + ', ');
		that.br = document.createElement('BR');
		
		if (!that.last_str) {
			that.last_str = that.node.appendChild(str);
			that.node.appendChild(that.br);
		} else {
			that.last_br = that.node.insertBefore(that.br, that.last_str);
			that.last_str = that.node.insertBefore(str, that.last_br);
		}
		
	}
};

setSlideshow();
