var d = document,c = 0,f,fadedin,fadedout,t,body = document.getElementsByTagName('body')[0];

function ce(tagName, attr, inner) {
	var el = document.createElement(tagName);
	if (attr) {
		if ((options = attr) != null)
			for (var name in options) {
				var value = options[name];
				el.setAttribute(name, value);
			}
	}
	if (inner) {
		el.innerHTML = inner;
	}
	return el;
}

function extend() {
  var target = arguments[0] || {}, i = 1, length = arguments.length, deep = false, options;

  if (typeof target === "boolean") {
    deep = target;
    target = arguments[1] || {};
    i = 2;
  }

  if (typeof target !== "object" && !isFunction(target))
    target = {};

  if (length == i) {
    return target;
  }

  for (; i < length; i++)
    if ((options = arguments[i]) != null)
      for (var name in options) {
        var src = target[name], copy = options[name];

        if (target === copy)
          continue;

        if (deep && copy && typeof copy === "object" && !copy.nodeType)
          target[name] = extend(deep,
            src || (copy.length != null ? [] : { })
          , copy);

        else if (copy !== undefined)
          target[name] = copy;
      }
  return target;
}

function ge() {
	var ea;
	for (var i = 0; i < arguments.length; i++) {
		var e = arguments[i];
		if (typeof e == 'string')
			e = document.getElementById(e);
		if (arguments.length == 1)
			return e;
		if (!ea)
			ea = new Array();
		ea.push(e);
	}
	return ea;
}

function geByClass(searchClass, node, tag) {
	var classElements = new Array();
	if (node == null)
		node = document;
	if (tag == null)
		tag = '*';
	if (node.getElementsByClassName) {
		classElements = node.getElementsByClassName(searchClass);
		if (tag != '*') {
			for (i = 0; i < classElements.length; i++) {
				if (classElements.nodeName == tag)
					classElements.splice(i, 1);
			}
		}
		return classElements;
	}
	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	return classElements;
}

function isParent(child, parent) {
    if (!child || !parent) {
        return false;
    }
    while (true) {
        if (child == parent) {
            return true;
        }
        if (child.parentElement) {
            child = child.parentElement;
        } else if (child.parentNode) {
            child = child.parentNode;
        } else {
            return false;
        }
    }
}

function isOffsetParent(child, parent) {
    if (!child || !parent) {
        return false;
    }
    while (true) {
        if (child == parent) {
            return true;
        }
		if (child.offsetParent) {
            child = child.offsetParent;
        } else {
            return false;
        }
    }
}

function getParent(child) {
    if (!child) {
        return false;
    }
	if (child.parentElement) {
		child = child.parentElement;
	} else if (child.parentNode) {
		child = child.parentNode;
	} else {
		return false;
	}
	return child;
}


function reInit (options) {
	var defaults = {
		rotateTime: 6000,
		fade: true,
		fadeTime: 600,
		slides: 5
	};

	options = extend(defaults, options);
	
	var ea = $('.poster-wrapper').get();
	
	var w = 0;
	var p = $('.thumbs li');
	p.each(function() {
		w = w + p.outerWidth(true);
	});
	$('.thumbs').css('width',w);
	
	for(var i = 0; i < options.slides; i++) {
		
		$('#re' + i).mouseover( function(event) {
			var target = event.relatedTarget;
			var current = event.currentTarget;
			var pattern = new RegExp("selected");
			if(!isParent(target, this.firstChild) && !pattern.test(current.className)) {
				for(var j = 0; j < options.slides; j++) {
					$('#re' + j).removeClass('selected');
				}
				$('#re' + c + 't').fadeOut(options.fadeTime/5);
				$(this).addClass('selected');
				var b = this.id;
				$('#' + b + 't').fadeIn(options.fadeTime/5);
				clearTimeout(t);
			} 
		});
		
		$('#re' + i).mouseout( function(event) {
			var target = event.relatedTarget;
			var pattern = new RegExp("re");
			if(!isParent(target, this.firstChild)) {
				//alert(target.nodeName);
				if(target.offsetParent) { var off = target.offsetParent.id; }
				else { var off = ''; }
				if(!pattern.test(off)) {
					c = this.id.charAt(2);
					reHandler(options);
				}
			}
		});
		
		$('.poster-wrapper:eq(' + i + ')').mouseover( function(event) {
			clearTimeout(t);
		});
		
		$('.poster-wrapper:eq(' + i + ')').mouseout( function(event) {
			var target = event.relatedTarget;
			var p = document.getElementById(this.offsetParent.id.slice(0,3));
			if(!isParent(target, p)) {
				c = this.offsetParent.id.charAt(2);
				reHandler(options);
			}
		});
	}
	
	f = true;
	reHandler(options);
	//infoHandler();
}

function reHandler (options) {
	clearTimeout(t);
	if(f == true) {
		f = false;
		if(c == (options.slides - 1)) { var m = 0; }
		else { var m = c - 0 + 1; }
		if (options.fade) {
			$('#re' + c).addClass('selected');
			$('#re' + c + 't').fadeIn(options.fadeTime);
			t = setTimeout ( function() {
				$('#re' + c).removeClass('selected');
				$('#re' + m).addClass('selected');
				
				$('#re' + m + 't').fadeIn(options.fadeTime);
				$('#re' + c + 't').fadeOut(options.fadeTime, function () {
					c++;
					reHandler(options);
				});
			}, options.rotateTime);
		}
		else {
			$('#re' + c).addClass('selected');
			$('#re' + c + 't').fadeIn(options.fadeTime);
			t = setTimeout ( function() {
				$('#re' + c).removeClass('selected');
				$('#re' + m).addClass('selected');
				
				$('#re' + m + 't').show();
				$('#re' + c + 't').hide(0, function () {
					c++;
					reHandler(options);
				});
			}, options.rotateTime);
		}
	}
	else {
		if(c == (options.slides - 1)) { var m = 0; }
		else { var m = c - 0 + 1; }
		if (options.fade) {
			t = setTimeout ( function() {
				$('#re' + c).removeClass('selected');
				$('#re' + m).addClass('selected');
				
				$('#re' + m + 't').fadeIn(options.fadeTime);
				$('#re' + c + 't').fadeOut(options.fadeTime, function () {
					if (c > (options.slides - 2)) { c = 0; }
					else { c++; }
					reHandler(options);
				});
			}, options.rotateTime);
		}
		else {
			t = setTimeout ( function() {
				$('#re' + c).removeClass('selected');
				$('#re' + m).addClass('selected');
				
				$('#re' + m + 't').show();
				$('#re' + c + 't').hide(1,function () {
					if (c > (options.slides - 2)) { c = 0; }
					else { c++; }
					reHandler(options);
				});
			}, options.rotateTime);
		}
	}
}
/*
function infoHandler () {
	
	//var ea = geByClass('item');
	//alert(ea[0]);
	
	$('.item').each(function() {
		$(this).mouseover( function(event) {
			showInfo(this);
		});
		
		$(this).mouseout( function(event) {
			hideInfo(this);
		});
	});
}

function showInfo (el) {
	var d = geByClass('info', el, 'div')[0];
	var im = geByClass('image', el, 'img')[0];
	d.style.display = 'block';
	im.style.borderColor = '#56a4e6';
}

function hideInfo (el) {
	var d = geByClass('info', el, 'div')[0];
	var im = geByClass('image', el, 'img')[0];
	d.style.display = 'none';
	im.style.borderColor = '';
}
*/
