// JavaScript Document
jq$=jQuery.sub();
var myXml;
var btnNext,btnPrev,btnNextIcon,btnPrevIcon;
var visibleLimt=0;
var moveIndex=0;
var moveTarget=0;
var targetPos=0;
var currentPos=0;
//jq$.fx.interval = 5;
var movePower=0;
var intervalId;
var iconWidth=240;
var iconMargin=20;
var iconDistan=260;

var minIcon=3;
var maxIcon=6;
var showIcon=0;
var totalIcon=0;
var loopFuc;
var leftOutArea=0;
var rightOutArea=0;
var unitDuration=200;
var duration=0;
var commandList=[];
var cmNext="nextPage";
var cmPrev="prevPage";
var lastCommand;

var running=false;
var looping=false;
var loopDuration=Math.round(1000/30);
var keepLoop=false;

/*********************************************/
var contentsIndex=-1;
var hotNewsIndex=0;


$(document).ready(function(){
	initPbBox();
	initContents();
	initHotNewNav();
	btnNext=jq$("<a class='btnNext'></a>").hide().appendTo(jq$("#thumbNav"));
	btnPrev=jq$("<a class='btnPrev'></a>").hide().appendTo(jq$("#thumbNav"));
	btnNextIcon=jq$("<span></span>").appendTo(btnNext);
	btnPrevIcon=jq$("<span></span>").appendTo(btnPrev);
	jq$.ajax({
        type: "GET",
		url: "data.xml?v="+Math.random(),
		dataType: "xml",
		success: function(xml) {
			myXml=xml;
			buildThumb("");
		}
	});
	jq$(window).resize(resizeHdl);
	jq$("#headerWapper #contactUs").click(contactToggleHdl);
	jq$("li.joinus>ol>li").each(function(index){
		var bgPath='url("images/joinus/no'+(index+1)+'.gif")';
		jq$(this).css({"background-image":bgPath})	
	})
});
function buildThumb(filter){
	//console.log(jq$("item",jq$(myXml)).has("tag:contains('"+filter+"')"));
	//var itemList=jq$("item",jq$(myXml));
	jq$("#thumbNav ul").html("");
	var itemList;
	//console.log(filter=="");
	switch(filter){
		case "All":
			//console.log("all");
			itemList=jq$("item",jq$(myXml));
			break;
		case "":
			//console.log("Recent");
			itemList=jq$('item[tag*="Recent"]',jq$(myXml));
			break;
		default:
			//console.log("Other");
			itemList=jq$('item[tag*="'+filter+'"]',jq$(myXml));
	}
	totalIcon=itemList.length;
	itemList.each(function(index,itm){
		var titleStr=jq$("title",jq$(itm)).text();
		var thumbPath=jq$("thumb",jq$(itm)).text();
		var profilePath=jq$("profile",jq$(itm)).text();
		var thumb=jq$("<img src='"+thumbPath+"'>");
		var title=jq$("<p>"+titleStr+"</p>").css({"text-align":"center","font-size":12,"font-weight":"normal"});
		var offsetPosit=index*iconDistan;
			var thumbItm=jq$("<li ojectIndex='"+index+"' path='"+profilePath+"'></li>").css({"display":"block","margin":0,"padding":0,"width":240,"position":"absolute","top":28,"left":offsetPosit,"cursor":"pointer"}).hide().click(function(){openPbBox(jq$(this).attr("path"))});
		unselectable(title);
		unselectable(thumb);
		thumbItm.append(thumb);
		thumbItm.append(title);
		thumbItm.hide().appendTo(jq$("#thumbNav ul"));
	});
		resizeHdl();
}
function navEnter(){
	jq$("span",jq$(this)).stop().fadeTo(200, 1);
}
function navLeave(){
	jq$("span",jq$(this)).stop().fadeTo(300, 0);
}
function thumbClickHdl(){
	
}
function nextHdl(){
	addCommand(cmNext);
}
function prevHdl(){
	addCommand(cmPrev);
}
function addCommand(newCommand){
	if(commandList.length!=0){
		if(commandList[commandList.length-1]==newCommand){
			commandList.push(newCommand);
		}else{
			commandList=[newCommand];
		}
	}else{
		commandList.push(newCommand);
	}
	//console.log("addCommand: "+running);
	if(!running){
		runCommand();
	}
}
function commandNext(){
	//console.log("next");
	moveTarget-=showIcon;
	startLoop();
	//console.log(moveTarget);
	//gotoIndex();
}
function commandPrev(){
	//console.log("prev");
	moveTarget+=showIcon;
	startLoop();
	//console.log(moveTarget);
	//gotoIndex();
}
function resizeHdl(){
	var bodyWidth=jq$("body").outerWidth();
	var minWidth=960;
	var finialWidth;
	if(bodyWidth>minWidth){
		var tmpIcon=Math.floor(bodyWidth/iconDistan);
		showIcon=tmpIcon>maxIcon ? maxIcon:tmpIcon;
		showIcon=showIcon>totalIcon ? totalIcon:showIcon;
		finialWidth=bodyWidth;
	}else{
		finialWidth=minWidth;
		showIcon=minIcon;
	}
	jq$("#thumbNav").width(finialWidth);
	jq$("#headerWapper").width(finialWidth);
	jq$("#contentWapper").width(finialWidth);
	jq$("#footerWapper").width(finialWidth);
	jq$("#hotnewsWapper").width(finialWidth);
	var MaxLimt=(showIcon-1)*iconDistan;
	
	//console.log("showIcon :"+showIcon+" | " +MaxLimt + " | "+iconDistan);
	
	duration=unitDuration*showIcon;
	jq$("#thumbNav ul").css({"width":(showIcon*iconDistan-iconMargin)});
	jq$("#thumbNav ul li").each(function(index){
		if($(this).is(':hidden')){
			$(this).css({opacity:0}).show();
		}
		var myLeft=parseInt(jq$(this).css("left").replace("px"));
		//console.log(index+" Left :"+myLeft);
			if( myLeft>=0 && myLeft<=MaxLimt){
				jq$(this).stop().fadeTo(200,1);
			}else{
				jq$(this).stop().fadeTo(100,0).hide();
			}
	});
	showNav();
}
function showNav(){
	if(totalIcon>showIcon){
		btnNext.fadeIn(300).unbind().bind('click',nextHdl);
		btnPrev.fadeIn(300).unbind().bind('click',prevHdl);
	}else{
		btnNext.fadeOut(100).unbind('click');
		btnPrev.fadeOut(100).unbind('click');
	}
}
function contactToggleHdl(){
	jq$("#contactInfo").stop(true, true).slideToggle(300);
}
function unselectable(object){
	jq$(object).attr('unselectable','on').css('MozUserSelect','none');
}
function openLightBox(){
	LightBoxBG.height(jq$(document).height()).click(closeLightBox).appendTo(LightBoxHolder);
	lightBoxContent.appendTo(LightBoxHolder).css({"top":jq$(window).scrollTop()+50,"left":(jq$(window).width()-lightBoxContent.width())*0.5});
	LightBoxHolder.height(jq$("body").outerHeight()).css({"z-index":9999}).hide().appendTo(jq$("body"));
	resizeHdl()
	LightBoxHolder.fadeTo(200,1);
}
function closeLightBox(){
	LightBoxHolder.fadeTo(100,0,function(){jq$(this).remove()});
}
/*********************************/
function initContents(){
	jq$("#mainNav li a").each(function(){
		jq$(this).click(hotContentTo);
	}
	);
	jq$("#contents li").each(function(index){
		jq$(this).hide();
	});
}
function hotContentTo(){
	var parentList=jq$("li",jq$(this).parent().parent());
	var totalItm=parentList.length;
	var id=jq$(">li",jq$(this).parent().parent()).index(jq$(this).parent());
	jq$("#mainNav li").removeClass("selected").each(
		function(index){
			if(index==id){
				jq$(this).addClass("selected");
			}
		}
	);
	jq$("#contents li").each(function(index){
		if(index==id){
			if (jq$(this).text()!=""){
				jq$("ul#contents").animate({
					height:jq$(this).outerHeight()+30
				},500);
			} else {
				jq$("ul#contents").animate({
					height:jq$(this).outerHeight()
				},500);			
			}
			jq$(this).children().hide();
			jq$(this).show()
			var nofChid=jq$(this).children().length;
			var delayUnit=1000/nofChid;
			//console.log(nofChid);
			jq$(this).children().each(
				function(index){
					jq$(this).delay(index*delayUnit).fadeIn(300);
				}
			);
		}else{
			jq$(this).hide();
		}
	});
	buildThumb(jq$(this).text());
	contentsIndex=id;
}
/*********************************/
function initHotNewNav(){
	jq$("#hotNewNav li").each(function(index){
		if(index==hotNewsIndex){
			jq$(this).addClass("selected");
		}
		//console.log("Hi "+index);
		jq$("a",jq$(this)).click(hotNewsTo);
	});
	jq$("#hotNewContent>li").each(function(index){
		if(index!=hotNewsIndex){
			jq$(this).hide();
		}
	});
}
function hotNewsTo(){
	hotNewsIndex=jq$(this).parent().parent().children().index(jq$(this).parent());
	jq$("#hotNewNav li").removeClass("selected");
	jq$(this).parent().addClass("selected");
	jq$("#hotNewContent>li").each(function(index){
		if(index!=hotNewsIndex){
			jq$(this).hide();
		}else{
			jq$(this).show();
			/*jq$("ul#hotNewContent").animate({
				height:jq$(this).outerHeight()
			},500);*/
			jq$(this).height(jq$(this).height())
			jq$(this).children().hide();
			jq$(this).show();
			var nofChid=jq$(this).children().length;
			var delayUnit=1000/nofChid;
			//console.log(nofChid);
			jq$(this).children().each(
				function(index){
					jq$(this).delay(index*delayUnit).fadeIn(300);
				}
			);
		}
	});
}
function runCommand(){
	if(commandList.length!=0){
	running=true;
	var currentCommand=commandList.splice(0,1);
		switch(currentCommand[0]){
			case cmNext:
				//console.log(cmNext);
				commandNext();
				break;
			case cmPrev:
				//console.log(cmPrev);
				commandPrev();
				break;
			default:
				running=false;
		}
	}else{
		running=false;
	}
	//running=true;
	
}
function navLoop(){
	//console.log("//=================================//");
	//console.log(targetPos+" | "+currentPos);
	if(targetPos!=currentPos){
		currentPos+=(targetPos-currentPos)*0.3;
		currentPos=Math.abs(targetPos-currentPos)<1? targetPos:currentPos;
		//console.log(currentPos);
	}else{
		stopLoop();
	}
}
function startLoop(){
	if(!keepLoop){
		targetPos=moveTarget*iconDistan;
		keepLoop=true;
		mainLoop();
	}
}
function stopLoop(){
	keepLoop=false;
	runCommand();
}
function mainLoop(){
	//console.log(keepLoop);
	navLoop();
	if(keepLoop){
		var newOpacity;
		var viewLimt=jq$("#thumbNav>ul").width()-iconWidth;
		var posMinLimt=-iconDistan;
		var MaxWidth=totalIcon*iconDistan;
		var posMaxLimt=(totalIcon-1)*iconDistan;
		//console.log(thumbNavWidth);
		jq$("#thumbNav ul li").each(function(index){
			var newPos=(index*iconDistan+currentPos)%MaxWidth;
			if(newPos<=posMinLimt){
				newPos=posMaxLimt+(newPos-posMinLimt);
				//jq$(this).appendTo(jq$("#thumbNav ul"));
				//console.log("Min: "+newPos);
			}else if(newPos>posMaxLimt){
				newPos= posMinLimt+(posMaxLimt-newPos);
				//console.log("Max: "+newPos);
				//jq$(this).prependTo(jq$("#thumbNav ul"));
			}
			jq$(this).css({"left":newPos});
			if(newPos<0){
				newOpacity=1+newPos/100;
			}else if(newPos>viewLimt){
				newOpacity=1+(viewLimt-newPos)/100;
			}else{
				newOpacity=1;
			}
			newOpacity=newOpacity<0 ? 0:newOpacity;
			//console.log("Hey: "+newOpacity);
			if(newOpacity==0){
					jq$(this).css({"opacity":newOpacity}).hide();
				}else{
					jq$(this).css({"opacity":newOpacity}).show();
				}
		});
	
		setTimeout('mainLoop()', loopDuration);
	}
}
