// ------------------------------------------------------------- //
//	xx.js v0.0.1                                       10/07/07	 //
//                                                               //	
//	INFORMATION                                                  //
//	 - Version  :v0.0.1                                          //
//	 - Author   :Kaoru Ishikura	                                 //
//	 - E-Mail   :ishikura@sakura-creative.com                    //
//	 - HomePage :http://www.sakura-creative.com/                 //
//                                                               //
//	Copyright (C) 2007 SAKURA CREATIVE.Inc All Rights Reserved.	 //
// ------------------------------------------------------------- //


$.ajax({
    url: "js/kyotowafu.xml",
    type: "GET",
    dataType: "xml",
    timeout: 3000,
    error: function(){
        alert("Error loading XML document");
    },
    success: function(xml){
		//add work lists
		var workCounter = 0;
		$("#worklist dd").prepend("<ul></ul>");
		$(xml).find("work").each(function(){
       		var works = $(this).text();
        	$("#worklist dd ul").append("<li id=\"w" + workCounter + "\">" + works + "</li>").hide();
			workCounter++;
    	});
		
		//#global_navi group
		$("#global_navi").each(function(){
			var _workList = $(this).find("#worklist"), _productList = $(this).find("#productlist"), _itemList = $(this).find("#worklist, #productlist");
			
			
			//add effect
			var isTimeout = null;
			_workList.mouseout(function(){
				isTimeout = setTimeout(function(){
					$(this).find("ul").eq(0).css("display", "none");
				}, 1000);
			});
			_workList.mouseover(function(){
				clearTimeout(isTimeout);
			});
			//add effect
			var isTimeout2 = null;
			_productList.mouseout(function(){
				isTimeout2 = setTimeout(function(){
					$(this).find("ul").eq(1).css("display", "none");
				}, 1000);
			});
			_productList.mouseover(function(){
				clearTimeout(isTimeout2);
			});
			

			//click a p#work button
			_workList.find(".toggleBtn").click(function(){
				if($(this).parent().parent().html().toLowerCase().indexOf("ul") == -1){ return;}
				_productList.find("ul").hide();
				clearTimeout(isTimeout);
				clearTimeout(isTimeout2);
				$(this).parent().parent().find("ul").toggle();
				return false;
			});

			//click a p#product button
			_productList.find(".toggleBtn").click(function(){
				if($(this).parent().parent().html().toLowerCase().indexOf("ul") == -1){ return;}
				_workList.find("ul").hide();
				clearTimeout(isTimeout);
				clearTimeout(isTimeout2);
				$(this).parent().parent().find("ul").toggle();
				return false;
			});
			
			/*
			if(_productList.find("ul").css("display") == "block") {
				_workList.find(".toggleBtn").click(function(){
				clearTimeout(isTimeout2);
				$(this).parent().parent().find("ul").show();
				}
			)};
			*/

			//click a p#work button
			/*_workList.find(".toggleBtn").click(function(){
				if($(this).parent().parent().html().toLowerCase().indexOf("ul") == -1){ return;}
				if(_productList.find("ul").css("display") == "block"){
					_productList.find("ul").hide(0,function(){
					clearTimeout(isTimeout);									
					});
				}
				$(this).parent().parent().find("ul").toggle();
				return false;
			});*/
			
			//click a p#product button
			/*_productList.find(".toggleBtn").click(function(){
				if($(this).parent().parent().html().toLowerCase().indexOf("ul") == -1){ return;}
				if(_workList.find("ul").css("display") == "block"){
					_workList.find("ul").hide(0,function(){
					clearTimeout(isTimeout);									
					});
				}
				$(this).parent().parent().find("ul").toggle();
				return false;
			});*/
			

			
			//click work lists
			_workList.find("li").click(function(){
				var clickedWorkNo = 0, clickedProductNo = 0, clickedTxt = $(this).text();				
				//mark clicked "li"
				$(this).addClass("clicked");
				for(var i=0,len=_workList.find("ul li").length; i<len; i++){
					if(
						$(this).parent().find("li").eq(i).attr("class") != null
						&& $(this).parent().find("li").eq(i).attr("class").indexOf("clicked") != -1
					){
						clickedWorkNo = i;
						$(this).removeClass("clicked").addClass("listSelected");
					}else if($(this).parent().find("li").eq(i).attr("class") != null){
						$(this).parent().find("li").eq(i).removeClass("listSelected");
					}
				}
				
				//clicked "li" text is set to work lists "dt p"
				_workList.find("dt span").html(clickedTxt).css("background", $(xml).find("works").eq(clickedWorkNo).attr("color")).css("color","#f1f1f1");
				
				//show "#productlist"
				$("#productlist").show();
				//hide "#worklist ul"
				$(this).parent().hide();
				//clean "p" of _productList
				_productList.find("dt span").empty();
				
				//prepend "<ul></ul>" to product list "dd"
				if(_productList.find("dd").html().toLowerCase().indexOf("ul") == -1){
					_productList.find("dd").prepend("<ul></ul>");
				}
				
				//clean "li" of "ul"
				_productList.find("ul").empty();
				//add product lists
				var productCounter = 0;
				$(xml).find("works").eq(clickedWorkNo).find("name").each(function(){
					var products = $(this).text(); 
        			$("#productlist dd ul").append("<li><a href=\"" + $(this).parent().find("photo").eq(0).attr("href") + "\" id=\"p" + productCounter + "\">" + products + "</a></li>").hide();
					productCounter++;
				});
				_productList.find("ul li:last-child").addClass("end");
				
				//click product lists
				_productList.find("li a").click(function(){
					var clickedTxt = $(this).text();				
					//mark clicked "li"
					$(this).parent().addClass("clicked");
					for(var i=0,len=_workList.find("ul li").length; i<len; i++){
						if(
							$(this).parent().parent().find("li").eq(i).attr("class") != null
							&& $(this).parent().parent().find("li").eq(i).attr("class").indexOf("clicked") != -1
						){
							clickedProductNo = i;
							$(this).parent().removeClass("clicked").addClass("listSelected");
						}else if($(this).parent().parent().find("li").eq(i).attr("class") != null){
							$(this).parent().parent().find("li").eq(i).removeClass("listSelected");
						}
					}
					
					//clicked "li" text is set to product lists "dt p"
					_productList.find("dt span").html(clickedTxt);
					$(this).parent().parent().hide();
					return false;
				});
				
				//add lightBox effect
				_productList.find("li a").each(function(){
					$(this).lightBox({
						_clickedWorkNo: clickedWorkNo,
						_productCounter: productCounter,
						_xml: xml
					});
				});
				
				return false;
			});
		});
	}
});

