
function addLoadEvent(func) { 
  var oldonload = window.onload; 
if (typeof window.onload != 'function') { 
	    window.onload = func; 
  } else { 
    window.onload = function() { 
      if (oldonload) { 
       oldonload(); 
     } 
      func();
	  } 
	  }
}
Gallery = {};
var Gallery = {
	'loaded':0,
	'SA':{},
	'SB':{},
	'curL':{},
	'otherL':{},
	'tempL':{},
	'holderWidth':236,
	'currentLayer':"SA",
	'curX':0,
	'stateChange':false,
	'direction':"init",
	'lastDir':'',
	'timeId':0,
	'divWidth':0,
	'intervalId':0,
	'CMove':function()
	{
		if(this.lastDir == 'left')this.startLeft();
		if(this.lastDir == 'right') this.startRight();
	},
	'init':function()
	{
		if(this.intervalId >0 )
		{
			clearInterval(this.intervalId);
		}
		this.direction = "init";
		var GI = document.getElementById("galleryItems");
		var AS = GI.getElementsByTagName("a");
		var IMGS = GI.getElementsByTagName("img");
		var ArrayImg = new Array();
		this.divWidth = 0;
		for (var i = 0; i < AS.length; i++)
		{
			var Img = new Image();
			Img.src = IMGS.item(i).src;
			this.divWidth += Img.width + 11;
		}
		document.getElementById("SA").style.width = this.divWidth + "px";
		document.getElementById("SB").style.width = this.divWidth + "px";
		document.getElementById("SA").innerHTML = document.getElementById("galleryItems").innerHTML;
		document.getElementById("SA").style.left = 0 +"px";
		document.getElementById("SB").innerHTML = document.getElementById("galleryItems").innerHTML;
		document.getElementById("SB").style.left = (this.divWidth) + "px";
		this.SA = document.getElementById("SA");
		this.SB = document.getElementById("SB");
		this.curL = this.SA;
		this.otherL = this.SB;
		Gallery.loaded = 1;
		this.startLeft();
	},
	'startLeft':function(){
		if(this.direction == "init" && Gallery.loaded == 1)
		{
			this.lastDir = this.direction = "left";
			this.intervalId = setInterval(function(){Gallery.moveLeft();},12);
		}else if(this.direction != "left"  && Gallery.loaded == 1)
		{
			this.stop();
			this.lastDir = this.direction = "left";
			this.intervalId = setInterval(function(){Gallery.moveLeft();},12);
		}else{}
		
		
		//if(parseInt(this.curL.style.left) < 0)this.otherL.style.left = parseInt(this.curL.style.left) + this.divWidth + "px";
		
	},
	'startRight':function(){
		if(this.direction != 'right')
		{
			this.lastDir = this.direction = "right";
		this.stop();
		//if(parseInt(this.curL.style.left) + this.divWidth <= this.holderWidth)this.otherL.style.left = parseInt(this.curL.style.left) - this.divWidth + "px";
		this.intervalId = setInterval(function(){Gallery.moveRight();},12);
		}
	},
	'moveLeft':function(){
		this.curL.style.left = (parseInt(this.curL.style.left) - 1) + "px";
		this.otherL.style.left = (parseInt(this.otherL.style.left) - 1) + "px";
		if(parseInt(this.curL.style.left) + this.divWidth <= this.holderWidth )
		{
			this.otherL.style.left = parseInt(this.curL.style.left) + this.divWidth  + "px";
			this.tempL = this.curL;
			this.curL = this.otherL;
			this.otherL = this.tempL;
		}
	},
	'moveRight':function(){
		this.curL.style.left = (parseInt(this.curL.style.left) + 1) + "px";
		this.otherL.style.left = (parseInt(this.otherL.style.left) + 1) + "px";
		if(parseInt(this.curL.style.left) >= 0 )
		{
			this.otherL.style.left = parseInt(this.curL.style.left) - this.divWidth + "px";
			this.tempL = this.curL;
			this.curL = this.otherL;
			this.otherL = this.tempL;
		}
	},
	'stop':function(){
		if(this.intervalId > 0 && this.direction != 'init')
		{
			clearInterval(this.intervalId);
			this.direction = "stop";
		}
	}
}
function CMove()
{
		Gallery.CMove();
	
}
function getPosition(e) {
	if(Gallery.direction == 'init' )return;
    e = e || window.event;
    var cursor = {x:0, y:0};
    if (e.pageX || e.pageY) {
        cursor.x = e.pageX;
        cursor.y = e.pageY;
    } 
    else {
        var de = document.documentElement;
        var b = document.body;
        cursor.x = e.clientX + 
            (de.scrollLeft || b.scrollLeft) - (de.clientLeft || 0);
        cursor.y = e.clientY + 
            (de.scrollTop || b.scrollTop) - (de.clientTop || 0);
    }
		if(Gallery.curX == 0)
		{
			Gallery.curX = cursor.x; return ;
		}
		if(Gallery.curX == cursor.x)
		{
			return;
		}
		Gallery.curX = cursor.x;
    if( cursor.x < 130 ) //&& Gallery.direction == "left"
	{
		Gallery.startRight();
	}else if(cursor.x > 236 ) //&& Gallery.direction == "right"
	{
		Gallery.startLeft();
	}else if(cursor.x <= 236 && cursor.x >= 130 ){ //
		Gallery.stop();
	}else{}
}
