var global_home_banslider_pos = 0;
var global_home_banslider_time = 10000;
var global_home_banslider_thetime;

var global_home_banslider_time = Array();
var global_home_banslider_picture = Array();
var global_home_banslider_title = Array();
var global_home_banslider_description = Array();
var global_home_banslider_url = Array();
var global_home_banslider_target = Array();

function homeGetBannersSlider(){
	var bans_prev = document.getElementById('bannerslider_previous');
	if(!bans_prev) return false;
	var bans_next = document.getElementById('bannerslider_next');	
	var bansli_connum = document.getElementById('bansli_connum');
	XMLaddress = '?h_pg=home&bin=banlide';
 	var xmlhttp = ajax();
    xmlhttp.onreadystatechange = function() { 
          if (xmlhttp.readyState==4) {
               if (xmlhttp.status==200) {
					var tags = xmlhttp.responseXML;
					var dataTag = tags.getElementsByTagName('data');
					var datareadTag = dataTag[0].getElementsByTagName('dataread');
					
					var datareadTag_sure = datareadTag[0].getAttribute('sure');
					if(datareadTag_sure!="1"){
						alert(datareadTag[0].childNodes[0].data);
						return false;
					}
					
					var bannersTag = dataTag[0].getElementsByTagName('banners');
					var banTag = bannersTag[0].getElementsByTagName('ban');
					if(banTag.length<=1) return false;
					else document.getElementById('bannerslider_controlnav').style.display = "block";
					
					for(var i=0; i<banTag.length; i++){
						
						var pictureTag = banTag[i].getElementsByTagName('picture');
						global_home_banslider_picture[i] = pictureTag[0].childNodes[0].data;
						global_home_banslider_time[i] = pictureTag[0].getAttribute('time');
						global_home_banslider_time[i] = global_home_banslider_time[i] * 1000;

						var titleTag = banTag[i].getElementsByTagName('title');
						global_home_banslider_title[i] = titleTag[0].childNodes[0].data;
						
						var descriptionTag = banTag[i].getElementsByTagName('description');
						global_home_banslider_description[i] = descriptionTag[0].childNodes[0].data;
						
						var urlTag = banTag[i].getElementsByTagName('url');
						global_home_banslider_url[i] = urlTag[0].childNodes[0].data;
						global_home_banslider_target[i] = urlTag[0].getAttribute('target');	
						
						var n_co = document.createElement('a');
						n_co.href = "javascript:;";
						n_co.id = "bannerslider_goitem_"+i;
						n_co.className = "hbin_itenn";
						n_co.innerHTML = i+1;
						n_co.onclick = function(){
							homeDirectBanner(this);					
						}
						bansli_connum.appendChild(n_co);
						
					}
					bans_prev.className = "enabled";
					bans_next.className = "enabled";
					homeGoToBanner();
               } 
          }
     };
     xmlhttp.open("GET", XMLaddress);
     xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
     xmlhttp.send(null);		
}

function homeGoToBanner(){
	var last_position = global_home_banslider_pos;
	var bansli_connum = document.getElementById('bansli_connum');
	var path_pics = "views/client/home/img/banners/";
	var bans_picture = document.getElementById('bannerslider_picture');
	
	var bans_prev = document.getElementById('bannerslider_previous');
	var bans_next = document.getElementById('bannerslider_next');
	
	var boxonban = document.getElementById('homeboxonpicbannerslider');
	
	var imagefile = path_pics+global_home_banslider_picture[global_home_banslider_pos];
	blendimage('homeboxonpicbannerslider', 'bannerslider_picture', imagefile, 700);
	
	bans_picture.alt = global_home_banslider_title[global_home_banslider_pos];
	bans_picture.title = global_home_banslider_title[global_home_banslider_pos];
	
	if(
		global_home_banslider_url[global_home_banslider_pos] != ""
		&& global_home_banslider_url[global_home_banslider_pos] != "0"
	){
		bans_picture.onclick = function(){
			window.open(global_home_banslider_url[last_position], global_home_banslider_target[last_position]);
		};
		bans_picture.className = "banner_wourl";
	} else {
		bans_picture.onclick = function(){ return false; };
		bans_picture.className = "banner_nourl";
	}
	
	var getdiv_co = bansli_connum.getElementsByTagName('a');
	for(var u=0; u<getdiv_co.length; u++){
		if(getdiv_co[u].className == "hbin_itenn_selec"){
			getdiv_co[u].className = "hbin_itenn";
		}
	}
	var bannerslider_goitem = document.getElementById('bannerslider_goitem_'+last_position);
	if(bannerslider_goitem){
		bannerslider_goitem.className = "hbin_itenn_selec";
	}
	
	var posarr_time;
	if(global_home_banslider_pos == (global_home_banslider_picture.length-1)){
		global_home_banslider_pos = 0;
		posarr_time = global_home_banslider_picture.length-1;
	} else {
		global_home_banslider_pos++;
		posarr_time = global_home_banslider_pos-1;
	}
	global_home_banslider_thetime = setTimeout("homeGoToBanner();", global_home_banslider_time[posarr_time]);
}

function homeDirectBanner(me){
	var n_pos = me.id.substring(me.id.lastIndexOf('_')+1, me.id.length);
	global_home_banslider_pos = n_pos;
	clearTimeout(global_home_banslider_thetime);
	homeGoToBanner();
}

function homeNextBanner(){
	if(global_home_banslider_picture.length == 0) return false;
	clearTimeout(global_home_banslider_thetime);
	homeGoToBanner();
}

function homePreviousBanner(){
	if(global_home_banslider_picture.length == 0) return false;
	clearTimeout(global_home_banslider_thetime);
	if(global_home_banslider_pos == 1){
		global_home_banslider_pos = global_home_banslider_picture.length-1;
	} else if(global_home_banslider_pos == 0){
		global_home_banslider_pos = global_home_banslider_picture.length-2;
	} else {
		global_home_banslider_pos-=2;
	}
	homeGoToBanner();	
}

/***************************************************************************************/

var currentSection = "pane_1";
var tabTag = "-tab";
var paneTag = "pane_";

function ScrollSection(link, scrollArea, offset){

	if (currentSection == link){
		return;
	}
	lastSection = currentSection;
	currentSection = link;

	theScroll = document.getElementById(scrollArea);
	position = findElementPos(document.getElementById(link));
	
	if (offset != "") {
		offsetPos = findElementPos(document.getElementById(offset));
		position[0] = position[0] - offsetPos[0];
	}

	scrollStart(theScroll, theScroll.scrollLeft, position[0], "horiz");
}

function ScrollArrow(direction, scrollArea, offset) {

	toolbarElem = document.getElementById('content');
	toolbarNames = new Array();
    
    var theindi = toolbarElem.getElementsByTagName('div');
	if(theindi.length > 1){
		for (var i = 0; i < theindi.length; i++){
			if(theindi[i].className == "banntur_boxitem")
				toolbarNames.push(theindi[i].id.split("_")[1]);
		}
	}

	for(var i = 0; i < toolbarNames.length; i++){
		if (toolbarNames[i] == currentSection.split("_")[1]){
			if (direction == "left") {
				if (i - 1 < 0) {
					gotoTab = toolbarNames[toolbarNames.length - 1];
				} else {
					gotoTab = toolbarNames[i - 1];
				}
			} else {
				if ((i + 1) > (toolbarNames.length - 1)){
					gotoTab = toolbarNames[0];
				} else {
					gotoTab = toolbarNames[i + 1];
				}
			}
		}
	}

	ScrollSection(paneTag+gotoTab, scrollArea, offset);

}

var scrollanim = {time:0, begin:0, change:0.0, duration:0.0, element:null, timer:null};

function scrollStart(elem, start, end, direction){

	if(scrollanim.timer != null){
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	}
	scrollanim.time = 0;
	scrollanim.begin = start;
	scrollanim.change = end - start;
	scrollanim.duration = 25;
	scrollanim.element = elem;
	
	if(direction == "horiz"){
		scrollanim.timer = setInterval("scrollHorizAnim();", 15);
	} else {
		scrollanim.timer = setInterval("scrollVertAnim();", 15);
	}
}

function scrollVertAnim(){
	if(scrollanim.time > scrollanim.duration){
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	} else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollTop = move; 
		scrollanim.time++;
	}
}

function scrollHorizAnim(){
	if(scrollanim.time > scrollanim.duration){
		clearInterval(scrollanim.timer);
		scrollanim.timer = null;
	} else {
		move = sineInOut(scrollanim.time, scrollanim.begin, scrollanim.change, scrollanim.duration);
		scrollanim.element.scrollLeft = move;
		scrollanim.time++;
	}
}

var moveanim = {time:0, beginX:0, changeX:0.0, beginY:0, changeY:0, duration:0.0, element:null, timer:null};

function moveStart(elem, startX, endX, startY, endY, duration){
	if (moveanim.timer != null){
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	}
	moveanim.time = 0;
	moveanim.beginX = startX;
	moveanim.changeX = endX - startX;
	moveanim.beginY = startY;
	moveanim.changeY = endY - startY;
	moveanim.duration = duration;
	moveanim.element = elem;

	moveanim.timer = setInterval("moveAnimDo();", 15);
}

function moveAnimDo(){
	if (moveanim.time > moveanim.duration) {
		clearInterval(moveanim.timer);
		moveanim.timer = null;
	} else {
		moveX = cubicOut(moveanim.time, moveanim.beginX, moveanim.changeX, moveanim.duration);
		moveY = cubicOut(moveanim.time, moveanim.beginY, moveanim.changeY, moveanim.duration);
		moveanim.element.style.left = moveX + "px";
		moveanim.element.style.top = moveY + "px";
		moveanim.time++;
	}
}

function findElementPos(elemFind){
	var elemX = 0;
	var elemY = 0;
	do {
		elemX += elemFind.offsetLeft;
		elemY += elemFind.offsetTop;
	} while ( elemFind = elemFind.offsetParent )

	return Array(elemX, elemY);
}

function sineInOut(t, b, c, d){
	return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
}
