$(function(){
	var imgBoxWidth = 600;
	var movImgSpan = 0;
	var objImg = $('#im').children('*');
	var numImg = objImg.size();
	var nextBtn = $('a','.detailNavi').eq(3);
	var prevBtn = $('a','.detailNavi').eq(2);
	var nextItemBtn = $('a','.detailNavi').eq(4);
	var prevItemBtn = $('a','.detailNavi').eq(1);
	var backBtn = $('a','.detailNavi').eq(0);
	var pageNum = $('img','.num').eq(2);
	var thePageNum = 1;
	var thePage = $('img','.num').eq(0);
	
	var theResult;
	var imgParts = new Array;
	var imgBox = new Array;
	//set to the category from the url
	var urlArray = location.href.split('/');
	var lang = urlArray[urlArray.length-4];
	var	category = urlArray[urlArray.length-1].split('?')[1];
	var backUrl = checkLang();
	var XMLname = '../xml/'+category+'.xml';
	
/* 	setXmlname(); */
	
	var permaArray = new Array;
	var thisPermaLink = location.href.split('/');
		thisPermaLink = thisPermaLink[thisPermaLink.length-1].split('?')[0];
	var theItemNum = '';
	var theNextItem = '';
	var thePrevItem = '';
	var parma_textA = $('a','.parma_text');
	
	
$.ajax({
	url:XMLname,
	type: "GET",
	dataType: "xml",
	timeout: 1000,
	cache:false,
	success:function(xml){
		makeDataSet(xml);
		reMakeArray();
		btn_init();
		setAattr();
	},
	error:function(){
	}
});
	

//check japanese or english then back it
function checkLang(){
	if(lang == 'e'){
		return 'http://www.gkid.co.jp/e/portfolio/'+category+'/index.html';
	}else{
		return 'http://www.gkid.co.jp/portfolio/'+category+'/index.html';
	}
}

/*
function setXmlname(){
	
	if(category == 'over-view'){
		XMLname = 'over-view.xml';
		
	}else if(category == 'consumer-electronics'){
		XMLname = 'consumer-electronics.xml';
		
	}else if(category == 'consumer-products'){
		XMLname = 'consumer-products.xml';
	
	}else if(category == 'over-view'){
		XMLname = 'professional-products.xml';
	
	}else if(category == 'over-view'){
		XMLname = 'public-transportation.xml';
	
	}
}
*/



//perse the xml which got, make HASH
function makeDataSet(theData){
	imgParts = $('image',theData);
	for(i=0; i<imgParts.size(); i++){
		var title = imgParts.eq(i).find('title').text();
		var src = imgParts.eq(i).find('src').text();
		var permalink = imgParts.eq(i).find('permalink').text();
		var page = imgParts.eq(i).find('page').text();
		var posX = imgParts.eq(i).find('posX').text();
		var posY = imgParts.eq(i).find('posY').text();
		
		var tmp = {
			title:title,
			src:src,
			permalink:permalink,
			page:page,
			posX:posX,
			posY:posY
		}
		
		if(tmp['title'] && tmp['src'] && tmp['permalink'] && tmp['page'] && tmp['posX'] && tmp['posY']){
			imgBox[i] = tmp;
		}else{
/* 			alert('“ü—ÍƒGƒ‰[‚ª‚ ‚è‚Ü‚·‚æ'); */
			return false;
		}
			
	}

}










	function reMakeArray(){
		for(i=0; i<imgBox.length; i++){
			permaArray.push(imgBox[i].page+'&'+imgBox[i].posY+'&'+imgBox[i].posX+'&'+imgBox[i].permalink);
		}
		permaArray.sort();
		
		//check the number of this permalink in array
		for(i=0; i<permaArray.length; i++){
			var theReg = new RegExp(thisPermaLink);
			if(permaArray[i].match(theReg)){
				theItemNum = i;
				backUrl = backUrl + '?page=' + permaArray[i].split('&')[0];
				
				if(i !== 0 && i !== permaArray.length-1){
					theNextItem = permaArray[i+1];
					thePrevItem = permaArray[i-1];
				}else if(i == 0){
					if(permaArray.length == 1){
						theNextItem = permaArray[i];
						thePrevItem = permaArray[i];
					}else{
						theNextItem = permaArray[i+1];
						thePrevItem = permaArray[permaArray.length-1];
					}
				}else if(i == permaArray.length-1){
					theNextItem = permaArray[0];
					thePrevItem = permaArray[i-1];
				}
			
			setEventtoItem();
			break;
			}
		}
	}




	//do layout images
	for(i=0; i<numImg; i++){
		objImg.eq(i).css({'top':'0','left':imgBoxWidth*i});
	}
	
	

	
	
	//btn init
	function btn_init(){
		backBtn.attr('href',backUrl);
		backBtn.find('img').bind('mouseover',function(event){
			$(this).animate({opacity:0},500);
			event.stopPropagation();
			return false;
		}).bind('mouseout',function(event){
			$(this).animate({opacity:1},500);
			event.stopPropagation();
			return false;
		})
		
/* 		alert(nextBtn.find('img').attr('alt')); */
		
		//if the unmber of images is only one...
		if(numImg == 1){
			nextBtn.css('cursor','default').find('img').attr('src','/img/portfolio/detail/n_white.gif');					prevBtn.css('cursor','default').find('img').attr('src','/img/portfolio/detail/b_white.gif');
		
		//else if the number of images is over one...
		}else{
			nextBtn.find('img').bind('mouseover',function(event){
				$(this).animate({opacity:0},500);
				event.stopPropagation();
			}).bind('mouseout',function(event){
				$(this).animate({opacity:1},500);
				event.stopPropagation();
			});
		}
		
		prevBtn.css('cursor','default').find('img').attr('src','/img/portfolio/detail/n_white.gif');
		
				
		//set total pege number
		pageNum.attr({
			src:'http://www.gkid.co.jp/img/portfolio/detail/'+numImg+'.gif',
			alt:numImg
			});
		//set the page number 	
		thePage.attr({
				src:'http://www.gkid.co.jp/img/portfolio/detail/1.gif',
				alt:'1'
			});
	}







	//attach event to buttun(next img and prev img)
	nextBtn.click(function(){
		if(1 <= thePageNum && thePageNum < numImg){
			if(numImg == 2 && thePageNum == 1){
				$(this).css('cursor','default').find('img').unbind('mouseover').unbind('mouseout').attr('src','/img/portfolio/detail/n_white.gif').fadeTo('500', '1');
				prevBtn.css('cursor','pointer').find('img').attr('src','/img/portfolio/detail/b.gif').unbind('mouseover').bind('mouseover', function(event){
					$(this).fadeTo('500', '0');
					event.stopPropagation();
				}).bind('mouseout',function(event){
					$(this).fadeTo('500', '1');
					event.stopPropagation();
				});	
			}else if(thePageNum == 1){
				prevBtn.css('cursor','pointer').find('img').attr('src','/img/portfolio/detail/b.gif').unbind('mouseover').bind('mouseover', function(event){
					$(this).fadeTo('500', '0');
					event.stopPropagation();
				}).bind('mouseout',function(event){
					$(this).fadeTo('500', '1');
					event.stopPropagation();
				});	
			}else if(thePageNum == (numImg-1)){
				$(this).css('cursor','default').find('img').unbind('mouseover').unbind('mouseout').attr('src','/img/portfolio/detail/n_white.gif').fadeTo('500', '1');
			};
			
			movImgSpan -= imgBoxWidth;
			thePageNum += 1;
			
			thePage.attr({
				src:'http://www.gkid.co.jp/img/portfolio/detail/'+thePageNum+'.gif',
				alt:thePageNum
			});
			
			$('#im').animate({'left':movImgSpan});
			showMovie();
			
		}
		return false;
	})
	
	prevBtn.click(function(){
		if(1 < thePageNum && thePageNum <= numImg){
			if(numImg == 2 && thePageNum == 2){
					nextBtn.css('cursor','pointer').find('img').attr('src','/img/portfolio/detail/n.gif').bind('mouseover',function(event){
					$(this).fadeTo('500','0');
					event.stopPropagation();
				}).bind('mouseout',function(event){
					$(this).fadeTo('500','1');
					event.stopPropagation();
				});
				$(this).css('cursor','default').find('img').attr('src','/img/portfolio/detail/b_white.gif').unbind('mouseover').fadeTo('500', '1');
			}else if(thePageNum == numImg){
				nextBtn.css('cursor','pointer').find('img').attr('src','/img/portfolio/detail/n.gif').bind('mouseover',function(event){
					$(this).fadeTo('500','0');
					event.stopPropagation();
				}).bind('mouseout',function(event){
					$(this).fadeTo('500','1');
					event.stopPropagation();
				});
			}else if(thePageNum == 2){
				$(this).css('cursor','default').find('img').unbind('mouseover').unbind('mouseout').attr('src','/img/portfolio/detail/n_white.gif').fadeTo('500', '1');
			}
			
			movImgSpan += imgBoxWidth;
			thePageNum -= 1;
			
			thePage.attr({
				src:'http://www.gkid.co.jp/img/portfolio/detail/'+thePageNum+'.gif',
				alt:thePageNum
			});
			
			$('#im').animate({'left':movImgSpan});
			unvisibleMovie();
				
		}
		return false;
	})
	
	
	
	
	
	
	//attach event to buttun(next item and prev item)
	function setEventtoItem(){
			nextItemBtn.attr('href',theNextItem.split('&')[3]+'?'+category);
			prevItemBtn.attr('href',thePrevItem.split('&')[3]+'?'+category);
			
			nextItemBtn.find('img').bind('mouseover',function(event){
					$(this).fadeTo('500','0');
					event.stopPropagation();
				}).bind('mouseout',function(event){
					$(this).fadeTo('500','1');
					event.stopPropagation();
				});
				
			prevItemBtn.find('img').bind('mouseover',function(event){
					$(this).fadeTo('500','0');
					event.stopPropagation();
				}).bind('mouseout',function(event){
					$(this).fadeTo('500','1');
					event.stopPropagation();
				});
	}
	
	
	 function setAattr(){
	 	parma_textA.attr('target','_blank');
	 }
	 
	 function unvisibleMovie(){
	 	$('#im').children('embed').fadeTo('500','0').css('display','none');
	 }
	 function showMovie(){
	 	$('#im').children('embed').css('display','block').fadeTo('500','1');
	 }

})
