
function moveSlider(target, st) {
    var slider = target.parentNode;
    var btns = slider.parentNode.childNodes;
    var pre = null;
    var next = null;
	
    for(var i=0; i<btns.length; i++) {
		if(btns[i].tagName == "A") {
			if(pre)
				next = btns[i];
			else
				pre = btns[i];
		}
    }
	
	
    
    var tWidth = parseInt(target.style.width);
    var sWidth = slider.offsetWidth;
    
    if(tWidth > sWidth ) {
		
		var x = parseInt(target.style.left);
		
		if(x < 0)
			addClass(pre, "active");
		if( x + tWidth > sWidth)
			addClass(next, "active");
		
		addClass(next, "active");
		if(!st) {
			target.style.top = "0px";
			target.style.left = "0px";
		}
		
		pre.onclick = function () {
			target.count = 14;
			if(target.speed)
				target.count = target.speed;
			preSlider(target,sWidth);
			var x = parseInt(target.style.left);
			if(x < 0)
				addClass(pre, "active");
			else
				removeClass(pre, "active");
			if( x + tWidth > sWidth)
				addClass(next, "active");
			else
				removeClass(next, "active");
		}
		next.onclick = function() {
			target.count = 14;
			if(target.speed)
				target.count = target.speed;
			nextSlider(target, sWidth);
			var x = parseInt(target.style.left);
			if(x < 0)
				addClass(pre, "active");
			else
				removeClass(pre, "active");
			if( x + tWidth > sWidth)
				addClass(next, "active");
			else
				removeClass(next, "active");
		}
		/*
		 pre.onmouseover = function () {
		 target.moving = true;
		 backwardSlider(target, sWidth);
		 }
		 next.onmouseover = function () {
		 target.moving = true;
		 forwardSlider(target, sWidth);
		 }
		 
		 pre.onmouseout = function() {
		 var x = parseInt(target.style.left);
		 if(x < 0)
		 addClass(pre, "active");
		 else
		 removeClass(pre, "active");
		 if( x + tWidth > sWidth)
		 addClass(next, "active");
		 else
		 removeClass(next, "active");
		 target.moving = false;
		 }
		 next.onmouseout = function () {
		 var x = parseInt(target.style.left);
		 if(x < 0)
		 addClass(pre, "active");
		 else
		 removeClass(pre, "active");
		 if( x + tWidth > sWidth)
		 addClass(next, "active");
		 else
		 removeClass(next, "active");
		 target.moving = false;
		 }
		 */
    } else {
		removeClass(next, "active");
		removeClass(pre, "active");
    }
	
	
}

function preSlider(target, sWidth) {
    var x = parseInt(target.style.left);
    var tWidth = parseInt(target.style.width);
    var step = 40;
    if(target.step)
    	step = target.step;
    
    if(target.count > 0){
		if(x + step < 0 ) {
			x += step;
			target.style.left = x + "px";
			setTimeout(
					   function() {
					   preSlider(target, sWidth);
					   },
					   50);
		}else {
			x = 0;
			target.style.left = x + "px";
		}
		target.count --;
    }
}

function nextSlider(target, sWidth) {
    var x = parseInt(target.style.left);
    var tWidth = parseInt(target.style.width);
    var step = 40;
    if(target.step)
    	step = target.step;
    if(target.count > 0){
		if((x+tWidth) > (sWidth + step)) {
			x -= step;
			target.style.left = x + "px";
			setTimeout(
					   function() {
					   nextSlider(target, sWidth);
					   },
					   50);
		}else {
			x = sWidth -tWidth;
			target.style.left = x + "px";
		} 
		target.count --;
    }
	
}
function forwardSlider(target, sWidth) {
	
    var x = parseInt(target.style.left);
	
    var tWidth = parseInt(target.style.width);
	
    var step = 40;
	if(target.step)
		step = target.step;
    if(target.moving){
		if((x+tWidth) > (sWidth + step)) {
			x -= step;
			target.style.left = x + "px";
			setTimeout(
					   function() {
					   forwardSlider(target, sWidth);
					   },
					   150);
		}else {
			x = sWidth -tWidth;
			target.style.left = x + "px";
		} 
	    
    }
}
function backwardSlider(target, sWidth) {
    var x = parseInt(target.style.left);
	
    var tWidth = parseInt(target.style.width);
	
    var step = 40;
 	if(target.step)
 		step = target.step;
    if(target.moving){
		if(x + step < 0 ) {
			x += step;
			target.style.left = x + "px";
			setTimeout(
					   function() {
					   backwardSlider(target, sWidth);
					   },
					   150);
		}else {
			x = 0;
			target.style.left = x + "px";
		} 
	    
    }
}

function showSliderContent(container, urlKey, callback, colorMode) {
    if(container.selectedItem) {
		container.selectedItem.style.display = "none";
	}
    if(!container.ulList) {
		container.ulList = {};
    }
    if(container.ulList[urlKey]) {
		container.ulList[urlKey].style.display = "block";
		container.selectedItem = container.ulList[urlKey];
		var ul = container.ulList[urlKey];
		var li = ul.getElementsByTagName("LI")[0];
		var a = li.getElementsByTagName("A")[0];
		if(ul.selectedItem) {
			removeClass(ul.selectedItem, "selected");
		}
		addClass(li, "selected");
		ul.selectedItem = li;
		callback.call(a);
		moveSlider (ul);
		return;
    }
    var ul =  document.createElement("ul");
    container.ulList[urlKey] = ul;
    container.selectedItem = ul;
    container.appendChild(ul);
    
    var url = _url;
    if(urlKey) {
		url += "?" + urlKey + "&" + siteKey;
    } else {
        url += "?" + siteKey;
    }
    var loader = new net.ContentLoader(
									   url, 
									   function () {
									   loadSliderContent(this.req, ul, urlKey, callback);
									   });
}

function loadSliderContent(req, ul, urlKey, callback) {
    var itemList = getJsonFromReq(req);
    if(urlKey == "mobotix=1") {
		setSliderContent2(itemList, ul, urlKey, callback);
    }else if(urlKey.indexOf("mobotix=1&") != -1
			 && urlKey.indexOf("domain=") == -1) {
		itemList = itemList[1];
		setSliderContent2(itemList, ul, urlKey, callback);
    }else {
		setSliderContent1(itemList, ul, urlKey, callback);
	}
    moveSlider(ul);
}

function setSliderContent2(itemList, ul, urlKey, callback) {
    var width = 0
    for(var i=0; i<itemList.length; i++) {
		var li = document.createElement("li");
		var a = document.createElement("a");
		a.href="javascript:void(0)";
		a.innerHTML = itemList[i].title;
		a.urlKey = urlKey;
		a.index = itemList[i].title;
		a.onclick = function () {
			if(ul.selectedItem)
				removeClass(ul.selectedItem, "selected");
			ul.selectedItem = this.parentNode;
			addClass(ul.selectedItem, "selected");
			callback.call(this);
		}
		ul.appendChild(li);
		li.appendChild(a);
		
		if(width == 0) {
			if(ul.selectedItem)
				removeClass(ul.selectedItem, "selected");
			ul.selectedItem = li;
			addClass(ul.selectedItem, "selected");
			callback.call(a);
		}
		width += li.offsetWidth;
    }
	
    ul.style.width = width + "px";
}

function setSliderContent1(itemList, ul, urlKey, callback) {
    var width = 0
    var li;
    for(var i in itemList) {
		li = document.createElement("li");
		var a = document.createElement("a");
		a.href="javascript:void(0)";
		a.innerHTML = itemList[i];
		a.urlKey = urlKey;
		a.index = i;
		a.onclick = function () {
			if(ul.selectedItem)
				removeClass(ul.selectedItem, "selected");
			ul.selectedItem = this.parentNode;
			addClass(ul.selectedItem, "selected");
			callback.call(this);
		}
		ul.appendChild(li);
		li.appendChild(a);
		if(width == 0) {
			if(ul.selectedItem)
				removeClass(ul.selectedItem, "selected");
			ul.selectedItem = li
			addClass(ul.selectedItem, "selected");
			callback.call(a);
		}
		
		width += li.offsetWidth;
    }
	
    if(width == 0) {
		li = document.createElement("li");
		var a = document.createElement("a");
		a.href = "javascript:void(0)";
		a.innerHTML = " ";
		a.urlKey = urlKey;
		a.index = -1;
		a.onclick = function () {
			if(ul.selectedItem) 
				removeClass(ul.selectedItem, "selected");
			ul.selectedItem = li;
			addClass(ul.selectedItem, "selected");
			callback.call(a);
		}
		ul.appendChild(li);
		li.appendChild(a);
		
    }
	
    if(width == 0) {
		if(ul.selectedItem)
			removeClass(ul.selectedItem, "selected");
		ul.selectedItem = li
		addClass(ul.selectedItem, "selected");
		callback.call(a);
    }
    width += li.offsetWidth;
    ul.style.width = width + "px";
}
