// JavaScript Document

$(document).ready(function() {
	
	// slider home
	
	var iSherfiTouchPosX = 0 ;
	var iGapX = 0 ;
	var cCurrent ;
	var cCurrentPosX  = 0 ;
	var iCurrentPanel = 0 ;
	 
	var background = document.getElementById('bg');
	background.addEventListener('touchstart', function(e) {
	 
				iGapX = 0 ;
				e.preventDefault();
				var touch = e.touches[0];
				iSherfiTouchPosX = touch.pageX ;
				document.getElementById('debug').innerHTML = 'touchstart >>> ' + iSherfiTouchPosX ;
				cCurrent = document.getElementById('content') ;
				$('#menu_list > li > ul').fadeOut();
                iMenuItemSelected = null ;
	 
			}, false);
	 
			
	 
	background.addEventListener('touchmove', function(e) {
	 
				var touch = e.touches[0];
				var iCurrentPosX = Number(touch.pageX) ;
				iGapX = iSherfiTouchPosX - iCurrentPosX;
				cCurrent.style.left = cCurrentPosX-iGapX + 'px' ;
				document.getElementById('debug').innerHTML = cCurrent.style.left ;
				
	}, false);
	 
			
	 
	background.addEventListener('touchend', function(e) { movePanel(); }, false);
	
	function movePanel()
	{
			var iLimit = 300 ;
			var iPosXCurrent = cCurrent.style.left.substr(0, cCurrent.style.left.length - 2);
			var iPosXAnimateGoal ;
			
			if(Math.abs(iGapX) > iLimit) {
			
				if(iPosXCurrent > cCurrentPosX){
					iPosXAnimateGoal = cCurrentPosX + 1024 ;
				}
				else if(iPosXCurrent < cCurrentPosX){
					iPosXAnimateGoal = cCurrentPosX - 1024 ;
				}
				else iPosXAnimateGoal = cCurrentPosX ;
											   
			}
			else iPosXAnimateGoal = cCurrentPosX ;
			
			if(iPosXAnimateGoal > 0) iPosXAnimateGoal = 0 ;
			if(iPosXAnimateGoal < 0 - $("#content").outerWidth(true)) iPosXAnimateGoal = cCurrentPosX ;
			document.getElementById('debug').innerHTML = iPosXAnimateGoal ;
			
			$("#content").animate({"left": iPosXAnimateGoal}, "easein");
			var iOldPanel = Math.abs(cCurrentPosX / 1024) ;
			cCurrentPosX = iPosXAnimateGoal ;
			iCurrentPanel = Math.abs(cCurrentPosX / 1024) ;
			updateHomeData(iOldPanel);
		
	}
	
	function switchPanel(pID)
	{
		var iPosXAnimateGoal = pID * -1024 ;
		var oldID = iCurrentPanel ;
		iCurrentPanel = pID ;
		cCurrentPosX = iPosXAnimateGoal ;
		updateHomeData(oldID);
		$("#content").animate({"left": iPosXAnimateGoal}, "easein");

	}
	
	function updateHomeData(iOldPanel)
	{
		$("#thumb_titles > li").css('display', 'none');
		$("#thumb_links > li").css('display', 'none');
		$("#call_list > li").css('display', 'none');
		$("#thumb_images > li > img").css('opacity', '0.5');

		var newbie = $("#thumb_titles > li").get(iCurrentPanel);
		newbie.style.display = 'block' ;
		newbie = $("#thumb_links > li").get(iCurrentPanel);
		newbie.style.display = 'block' ;
		newbie = $("#thumb_images > li > img").get(iCurrentPanel) ;
		newbie.style.opacity = '1' ;
		newbie = $("#call_list > li").get(iCurrentPanel) ;
		newbie.style.display = 'block' ;
        
        $("#call_list_shadow").css("width", ($("#call_list").outerWidth(true) + 20) + 'px');
        var position = $("#call_list").position();
        $("#call_list_shadow").css("left", (position.left - 10) + 'px'); 	
	}
	
	updateHomeData(0);
    if(homeFocusPanel && homeFocusPanel != 0) switchPanel(homeFocusPanel);
	
	// thumbs handlers
	$("#thumb_images > li").click( function (){ switchPanel($("#thumb_images > li").index(this)) ; } );
	
});
