/* SlideShow - 2011.10.05 
http://www.pagecolumn.com/webparts/slideshow.htm
----------------------------------------------------------- */
function addEvent ( obj, type, fn ) {
  if ( obj.attachEvent ) {
    obj["e"+type+fn] = fn;
    obj[type+fn] = function() { obj["e"+type+fn]( window.event ); }
    obj.attachEvent( "on"+type, obj[type+fn] );
  } else
    obj.addEventListener( type, fn, false );
}

function removeEvent ( obj, type, fn ) {
      if ( obj.detachEvent ) {
        obj.detachEvent( "on"+type, obj[type+fn] );
		    obj[type+fn] = null;
      } else
        obj.removeEventListener( type, fn, false );
}

Function.prototype.bind = function(obj) {
    var _method = this;
    return function() {
        return _method.apply(obj, arguments);
    };    
}

function getstyle(elem, prop) {
		if(document.defaultView)
		{
			return document.defaultView.getComputedStyle(elem, null).getPropertyValue(prop);
		}
		else if(elem.currentStyle)
		{
			var prop = prop.replace(/-(\w)/gi, function($0,$1)
			{
				return $1.toUpperCase();
			});
			return elem.currentStyle[prop];
		}
		else return null;
	}
	
var ttt = [];

function $(id) { 
    var $ =  (typeof id == "string") ? document.getElementById(id) : id;
    $.start ={};
    
 
    $.start = function() {
        var left = getstyle($,"left");
	    var top =  getstyle($,"top");	 
	    $.start.left = (left=="auto") ? 0 : parseInt(left);
	    $.start.top = (top=="auto") ? 0 : parseInt(top);
    }

    $.stop = function(){
     
     for (i in ttt) {
        clearTimeout(ttt[i]);
     }

    };

    $.move = function(settings,callbk) {
    var _this = $;
    var left = getstyle(_this,"left");
	var top =  getstyle(_this,"top");
	 
	$.start.left = (left=="auto") ? 0 : parseInt(left);
	$.start.top = (top=="auto") ? 0 : parseInt(top);
	
    if (settings.to.top == _this.start.top) { // x 
       
        var descend = (settings.to.left>_this.start.left) ? false : true;     
        var s = Math.min(_this.start.left,settings.to.left); 
        var d = Math.max(_this.start.left,settings.to.left); 
        _this.speed = (d -s)/settings.delay;
      
		for (i = s; i <= d; i++) { 
		  (function(j) { 
				var delay = (descend==true) ? (d-j) * settings.delay / (d - s) : (j-s)*settings.delay/(d - s);
				   ttt[i] = setTimeout(function() { 
						_this.style.left = j+"px";     
						if (descend == false && j ==d && callbk != undefined) { callbk.call(_this); } 
						else if (descend == true && callbk != undefined && j == s) {
							callbk.call(_this);					 
					  	}					   
					  },delay); 
			})(i); 
		
		} 
              
    } 
     
}
   
    return $;  
}

function slideshow(id){
    this.ul = document.getElementById(id);
    this.lis = this.ul.getElementsByTagName("LI");
    this.len = this.lis.length;
    this.width = parseInt(getstyle(this.ul.parentNode, "width"));
    for(i = 1; i < this.len; i++) {
    	this.lis[i].style.display = "block";
    }
    this.ul.style.width = this.len * this.width + "px";
    this.left = this.width;
    this.t= 0;
    this.k = 0;
    this.dir = "plus";
    this.delay = 1000; //tweak the time for the image change in miliseconds
}

slideshow.prototype = {

	repeat: function(fn,del){
		var _this = this;
		(function() { 
			_this.t = setInterval(fn,del);
		})();
	
	},
	
	attach: function(){
		
		_this = this;
		this.ul.onmouseover = function(){
			$(this).stop();
			_stop();
		};
	
		this.ul.onmouseout = function(){
			if (_this.t != 0) return;
			_stop();
			_go();
		}
		
	},
	
	go: function(){  
		_this = this;
		_stop = this.stop.bind(this);
		_go = this.go.bind(this);
		this.attach();
		clearInterval(this.t);   
		this.repeat(this.show.bind(this), this.delay + 2000); //tweak the time the picture stays active in miliseconds 
	},
	
	show: function(){
			_this.k = (_this.dir == "plus") ? _this.k + 1 : _this.k - 1;
		if (_this.k== _this.len) {
			_this.dir = "minus";
			_this.k = _this.len - 1;
		}
		else if (_this.k< 0) {
			_this.k = 0;
			_this.dir = "plus";
		} 
 
		$(_this.ul).move( { delay : _this.delay, to : {left : - _this.k * _this.left, top : 0 } } );
		
		/* Slideshow Pagination - homepage only - 2011.10.15 Leandro Marques */
		
		var intLis = document.getElementById("hpSlider_page").getElementsByTagName("LI").length - 1;

		var strID = "";
		for (i = 0; i <= intLis; i++) {
			strID = "hpSlider_page" + i;
			if (i == _this.k){
				document.getElementById(strID).className = "active";
			} else {
				document.getElementById(strID).className = "";	
			}
		}
		
		/* end of Slideshow Pagination */
	
	},
	
	stop: function(){
		$(this.ul).stop();
		clearInterval(this.t);
		this.t = 0;
	}
}

/* SlideShow Pagination - 2011.10.15 Leandro Marques
------------------------------------------------------------ */
function createPagination(id, ssid) {
	var intLis = document.getElementById(ssid).getElementsByTagName("LI").length - 1;
	var html = "";
	for (i = intLis; i >= 0; i--){
		if (i == 0) { 
			strClass = "active"; 
		} else { 
			strClass = ""
		}
		html = html + "<li id=\"" + id + i + "\" class=\"" + strClass + "\"></li>";
	}
	document.getElementById(id).innerHTML = html;
	for (i = intLis; i >= 0; i--) {
		addPaginationEvent(id + i);	
	}
}

function addPaginationEvent(strElement) {
	var obj = document.getElementById(strElement);

	if(obj.addEventListener) {
		obj.addEventListener("click",function(e) { 
			changePage(strElement); 
		},false);	
	} else {
		obj.attachEvent("onclick", function(e){changePage(strElement);});	
	}
		
}

function changePage(strElement) {
	var num = strElement.charAt(strElement.length - 1);
//	document.getElementById("hpSlider")
}
