//$ICON_PAGE=-1;
//$ICON_PAGE_MAX=0;
//$CATEGORY=null;

/*
	$(document).ready(function(){
		// Initialize history plugin.
		// The callback is called at once by present location.hash. 
		$.historyInit(pageload, "jquery_history.html");
		
		// set onlick event for buttons
		$("a[rel='history']").click(function(){
			// 
			var hash = this.href;
			hash = hash.replace(/^.*#/, '');
			// moves to a new page. 
			// pageload is called at once. 
			// hash don't contain "#", "?"
			$.historyLoad(hash);
			return false;
		});
	});
	function pageload(hash) {
		// alert("pageload: " + hash);
		// hash doesn't contain the first # character.
		if(hash) {
			// restore ajax loaded state
			if($.browser.msie) {
				// jquery's $.load() function does't work when hash include special characters like aao.
				hash = encodeURIComponent(hash);
			}
			$("#load").load(hash + ".html");
		} else {
			// start page
			$("#load").empty();
		}
	}
*/


$(document).ready(function(){
	// Get Value from DOM
	$ICON_SIZE=$("#MENU-ICON-LIST li").width();//110
	$ICON_STAGE_SIZE=$("#MENU-ICON").width();//720
	$ICON_STAGE_MARGIN=parseInt($("#MENU-ICON-LIST").css("margin-left"));//30
	$("#MENU-ICON-LIST").css("margin-left","0");
	//
	$hash=location.hash;
	makeIconList();
	// ======== Set Category Menu ========
	$("#MENU-CATEGORY ul li").click(function(){
		location.hash=$("a",this).attr("href");
		makeIconList();
	});
	$("#MENU-ICON-PAGE-BACK").click(function(){slideIcon('-1');});
	$("#MENU-ICON-PAGE-NEXT").click(function(){slideIcon('+1');});
	
	/*$(window).unload(function(){
		alert("Bye!");
	});*/
	
	/*$("#SITE-MENU .language ul li a").each(function(){
		$(this).attr("href")=
	});*/
	
	$("a").each(function(){
		/*alert($(this).attr("href"));*/
		if($(this).attr("href").substr(0,1)=="#"){
			$(this).click(function(){
				
				/*$("#CONTENT-BOX").load("content_"+$(this).attr("href").replace(/^.*#/,'')+".html #CONTENT");
				$("#MENU-ICON-LIST li.here").removeClass("here");
				$(this).parent().addClass("here");*/
				location.hash=$(this).attr("href");
				makeIconList();
			});
		}
	});
	// External Link
	//$('a[@href^="http://"]').not($('a[@href^="http://"]').find('img').parents('a')).addClass("extlink").attr("target","_blank");
	//$('a[@href^="http://"]').addClass("extlink").attr("target","_blank");
	//$('a[@href^="/"]').addClass("extlink").attr("target","_blank");
	checkLink($("body"));
	
	var $h=0;
	$("#STAGE-FOOT-BODY div.column div.inner").each(function(){
		if($h<$(this).height()){$h=$(this).height();}
	});
	$("#STAGE-FOOT-BODY div.column div.inner").height($h);
});


function makeIconList(){
	// ======== Select Category ========
	$ICON_PAGE=-1;
	$ICON_PAGE_MAX=0;
	$CATEGORY=null;
	
	//==== Make List ====//
	$("#MENU-ICON-LIST *").remove();//Reset List
	var $hot = $("#DATA ul li").is("[@ref=hot]");// HOT check
	if(!$hot){$("#MENU-CATEGORY ul li a[@href=#hot]").parent().remove();}// Remove Menu
	if(!$CATEGORY){$("#DATA ul").each(function(){// Search Category
		if("#"+$(this).attr("ref")==location.hash){$CATEGORY=$(this).attr("ref");}
	});}
	if(!$CATEGORY){$("#DATA ul li").each(function(){// Search Item
		//if($("a",this).attr("href")==location.hash){$CATEGORY=$(this).parent().attr("ref");}
		if($("a",this).attr("href").replace(/^.*#/,'#')==location.hash){$CATEGORY=$(this).parent().attr("ref");}//replace for IE6
	});}
	if($CATEGORY){// Set List
		$("#MENU-ICON-LIST").append($("#DATA ul[@ref="+$CATEGORY+"] li").clone());
	}else{
		if($hot){
			$CATEGORY="hot";
			$("#MENU-ICON-LIST").append($("#DATA ul li[@ref=hot]").clone());
		}else{
			$CATEGORY=$("#DATA ul:first").attr("ref");
			$("#MENU-ICON-LIST").append($("#DATA ul:first li").clone());
		}
	}
	//==== Set Menu Highlight====//
	$("#MENU-CATEGORY ul li a:not([@href=#"+$CATEGORY+"])").parent().removeClass("here");
	$("#MENU-CATEGORY ul li a[@href=#"+$CATEGORY+"]").parent().addClass("here");// Category Hilight
	
	//==== Icon Menu Pagination ====
	var $items = Math.floor(($ICON_STAGE_SIZE - ($ICON_STAGE_MARGIN * 2)) / $ICON_SIZE);// Number of One Page
	var $num = 0;
	$("#MENU-ICON-LIST li").each(function(){
		$num++;
		if($num==1) {
			var $w = $ICON_STAGE_MARGIN;
			$(this).before('<li class="none" style="width:'+$w+'px;"></li>');// Set Left Spacer
			$ICON_PAGE_MAX++;// Page Count Up
		}else if($num==$items) {
			var $w = ($ICON_STAGE_SIZE-$ICON_SIZE*$items-$ICON_STAGE_MARGIN);
			$(this).after('<li class="none" style="width:'+$w+'px;"></li>');// Set Right Spacer
			$num=0;
		}
		$("a",this).click(function(){
			//$("#CONTENT-BOX").load("content_"+$(this).attr("href").replace(/^.*#/,'')+".html #CONTENT");
			loadContent($(this).attr("href"));// Content Load
			$("#MENU-ICON-LIST li.here").removeClass("here");
			$(this).parent().addClass("here");
			location.hash=$(this).attr("href");
			//return false;
		});
		//if($("a",this).attr("href")==location.hash){
		//alert($("a",this).attr("href"));
		//alert($("a",this).attr("href").replace(/^.*#/,'#'));
		if($("a",this).attr("href").replace(/^.*#/,'#')==location.hash){//replace for IE6
			$ICON_PAGE=$ICON_PAGE_MAX-1;
			$(this).addClass("here");
			//$TARGET_PAGE=$("a",this).attr("href");
			$TARGET_PAGE=$("a",this).attr("href").replace(/^.*#/,'#');//replace for IE6
		}
	});
	$("#MENU-ICON-LIST").css("width",$ICON_PAGE_MAX*$ICON_STAGE_SIZE+"px");
	
	//==== Set Icon Page Position ====
	if($ICON_PAGE<0){
		$ICON_PAGE=0;
		$("#MENU-ICON-LIST li:eq(1)").addClass("here");
		$TARGET_PAGE=$("#MENU-ICON-LIST li:eq(1) a").attr("href");
	}
	$v=$ICON_PAGE*720*-1;
	$("#MENU-ICON-LIST").css("left",$v+"px");
	
	//==== Set Page Number Points ====
	$("#MENU-ICON-PAGE li:not(:first)").remove();// Reset
	for(var $i=0;$i<$ICON_PAGE_MAX;$i++){
		$("#MENU-ICON-PAGE li:first").clone().appendTo("#MENU-ICON-PAGE").removeClass("here");
	}
	$("#MENU-ICON-PAGE li:first").remove();
	if($ICON_PAGE_MAX<2){
		$("#MENU-ICON-PAGE").hide();
		$("#MENU-ICON-PAGE-BACK").hide();
		$("#MENU-ICON-PAGE-NEXT").hide();
	} else {
		$("#MENU-ICON-PAGE").show();
		$("#MENU-ICON-PAGE-BACK").show();
		$("#MENU-ICON-PAGE-NEXT").show();
	}

	$("#MENU-ICON-PAGE li").click(null).click(function(){
		var index=$("#MENU-ICON-PAGE li").index(this);
		slideIcon(index);
	});
	$("#MENU-ICON-PAGE li:not(:eq("+$ICON_PAGE+"))").removeClass("here");
	$("#MENU-ICON-PAGE li:eq("+$ICON_PAGE+")").addClass("here");
	//$("#CONTENT-BOX").load("content_"+$TARGET_PAGE.replace(/^.*#/, '')+".html #CONTENT",function(){checkLink();});
	loadContent($TARGET_PAGE);
	//==== External Link Check ====
	//checkLink();
}

function slideIcon($dir){//alert($dir);
	var $v=0;
	if(typeof($dir)=="string"){
		$ICON_PAGE+=parseInt($dir);
		if($ICON_PAGE<0){$ICON_PAGE=($ICON_PAGE_MAX-1)};
		if($ICON_PAGE>($ICON_PAGE_MAX-1)){$ICON_PAGE=0};
	}else{
		
		$ICON_PAGE=parseInt($dir);
	}
	$v=$ICON_PAGE*720*-1;
	$("#MENU-ICON-LIST").animate(
		{"left": $v+"px"},
		"slow", "easeInQuad"
	);
	$("#MENU-ICON-PAGE li:not(:eq("+$ICON_PAGE+"))").removeClass("here");
	$("#MENU-ICON-PAGE li:eq("+$ICON_PAGE+")").addClass("here");
}

function loadContent(target){
	//$("#CONTENT-ICON img").attr("src","./icon/loader.gif");
	$("#CONTENT-ICON img").hide();
	//$("#CONTENT-BOX").hide();
	//alert(target);
	$("#CONTENT-BOX").load("content_"+target.replace(/^.*#/,'')+".html #CONTENT",function(){checkLink($("#CONTENT-BOX"));showContent();});
	//location.hash=target;
	//urchinTracker("/"+target.replace(/^.*#/,''));
	pageTracker._trackPageview("/08M/"+target.replace(/^.*#/,''));// Google analytics
}

function checkLink(jqObj) {
	// External Link
	//$('a[@href^="http://"]').not($('a[@href^="http://"]').find('img').parents('a')).addClass("extlink").attr("target","_blank");
	//$('a[@href^="http://"]',jqObj).addClass("extlink").attr("target","_blank");
	//$('a[@href^="/"]',jqObj).addClass("extlink").attr("target","_blank");
	//Internal Link
	$("a[@ref=internal]").click(function(){
		location.hash=$(this).attr("href");
		location.reload();
		$('html, body').scrollTop(0).scrollLeft(0);
	});
}
function showContent(){
	//$("#LOADING").hide();
	//$("#CONTENT-BOX").show();
}
