/**
 * @author femto
 */
var EXLXML = new Class({
	Implements: [Options, Events],
	options: {
		rowOffset: 2,
		identifyColomn: 0
	},
	
	initialize: function(dataPath,imageFolder, options){
		this.setOptions(options);
		this.rows = [];
		this.rowIDs = {};
		
		this.imageFolder = imageFolder;
		
		this.loadEXLXML(dataPath);
	},
	
	loadEXLXML: function(dataPath){
		
    var http = new JKL.ParseXML( dataPath );            
    var func = function ( data ) {
				
				
				var jsonRowData = data['Workbook']['ss:Worksheet']['Table']['Row'];
				
				
				
				jsonRowData.each(function(item,index){
					
					if (this.options.rowOffset <= index && $type(item.Cell) == 'array') {
						var tmpRowArray = [];
							item.Cell.each(function(item2, index2){
								if(item2['Data']){
									
									tmpRowArray[index2] = item2['Data']['#text'];
								}
							})
							
						this.rows[index - this.options.rowOffset] = tmpRowArray;
					}
				}.bind(this))
				
				this.identifyRow();
				this.setDollsThumnails();
				this.fireEvent('readyAllXMLInfo');
			}
			
.bind(this)
    http.async( func );                             // 呼び出し先関数を指定する
    http.parse(); 
		
	},
	
	identifyRow: function(){
		if (this.rows) {
				this.rows.each(function(item, index){
						this.rowIDs[item[this.options.identifyColomn]] = index;
			}
			
.bind(this))
		}
	},
	
	getDataFromCol: function(colNumber){
	
		var results = [];
		
		$each(this.rows, function(item, index){
				results[index] = item[colNumber];
		}
		
.bind(this))
		
		return results;
	},
	
	getDataFromRowAndCol: function(row, col){
	
		var result = '';
		
		result = this.rows[row][col];
		
		return result;
	},
	
	getDataFromRow: function(row){
		var results = [];
		if ($type(row) == 'string') {
				results = this.rows[this.rowIDs[row]];
		}
		else {
				results = this.rows[row];
		}
		
		return results;
		
	},
	setDollsThumnails: function(){
		
		var boxIndex = {'category_all':'all',
						'category_ange':'A',
						'category_rosa':'B',
						'category_separate':'C',
						'category_jewel':'D',
						'category_pure':'E',
						'category_nano':'F'};
		
		var directIndexHash = {'all':'category_all',
		'ange':'category_ange',
		'rosa':'category_rosa',
		'separate':'category_separate',
		'jewel':'category_jewel',
		'pure':'category_pure',
		'nano':'category_nano'};
		
		var directIndex = ['all','ange','rosa','separate','jewel','pure','nano'];
		
		var boxNameArray = [];
		
		var menuState = '';
		
		var dollsBodyType = this.getDataFromCol(3);
		
		var dollsByCategory = {'all':{'injected':false,'container':'category_all','ID':[],'Alt':[],'New':[]},
		'A':{'injected':false,'container':'category_ange','ID':[],'Alt':[],'New':[]},
		'B':{'injected':false,'container':'category_rosa','ID':[],'Alt':[],'New':[]},
		'C':{'injected':false,'container':'category_separate','ID':[],'Alt':[],'New':[]},
		'D':{'injected':false,'container':'category_jewel','ID':[],'Alt':[],'New':[]},
		'E':{'injected':false,'container':'category_pure','ID':[],'Alt':[],'New':[]},
		'F':{'injected':false,'container':'category_nano','ID':[],'Alt':[],'New':[]}};
		
        this.getDataFromCol(0).each(function(item,i){
            if(!dollsByCategory.all['ID'].contains(item)){
                dollsByCategory.all['ID'].push(item);
                dollsByCategory.all['Alt'].push(this.getDataFromCol(1)[i]);
                dollsByCategory.all['New'].push(this.getDataFromCol(4)[i])
            }
        }.bind(this))
        
		/*dollsByCategory.all['ID'] = this.getDataFromCol(0);
		dollsByCategory.all['Alt'] = this.getDataFromCol(1);
		dollsByCategory.all['New'] = this.getDataFromCol(4)*/
		
		this.getDataFromCol(0).each(function(item,index){	
			if (item) {
				dollsByCategory[dollsBodyType[index]]['ID'].push(item);
				dollsByCategory[dollsBodyType[index]]['Alt'].push(this.getDataFromCol(1)[index]);
				dollsByCategory[dollsBodyType[index]]['New'].push(this.getDataFromCol(4)[index]);
			}
		}.bind(this));
		
		

		
		var bridgeFunc = function(){
			this.fireEvent('clickThumnail',arguments);
		}.bind(this)
		
		
		
		
		$each(dollsByCategory,function(item,index,object){
			boxNameArray.push(item.container);
			item.open = new Fx.Tween($(item['container']),{fps:20});
			item.close = new Fx.Tween($(item['container']),{duration:10});
			item.close.set('opacity',0);
		}.bind(this))
		
		
		var locationInPage = location.href;
		var startIn = locationInPage.indexOf('#');
		if (startIn > 0) {
			var pageID = locationInPage.slice(startIn + 1, locationInPage.length);
			if (directIndex.contains(pageID)) {
				changeLayer($$('#buttons p.' + directIndexHash[pageID] + ' a')[0],true);
			}else {
				changeLayer($$('#buttons p.' + directIndexHash['all'] + ' a')[0],true);
			}
		}
		else {
			changeLayer($$('#buttons p.' + directIndexHash['all'] + ' a')[0],true);
		}
		
		
		
		
		$$('div#buttons a').addEvent('click',function(e){
			e = new Event(e);
			
			
			changeLayer(this,false);
			
		})
		
		
		function changeLayer(el,init){
			
			var tmpEl = el;
			var clickState = tmpEl.getParent().getProperty('class').trim();
			
			if (!clickState.test('selected')) {
			
				if (!dollsByCategory[boxIndex[clickState]].injected) {
					dollsByCategory[boxIndex[clickState]].thumnails = new ThumnailInjecter($(dollsByCategory[boxIndex[clickState]].container),dollsByCategory[boxIndex[clickState]]['ID'],imageFolder,'_face_m.jpg',dollsByCategory[boxIndex[clickState]]['Alt'],dollsByCategory[boxIndex[clickState]]['New'],{clickFunc:function(){
						var bridgeEvent = bridgeFunc.pass(this.get('href'));
						bridgeEvent();
			}})
					//dollsByCategory[boxIndex[clickState]].thumnails.injectThumnails($(dollsByCategory[boxIndex[clickState]].container));
					dollsByCategory[boxIndex[clickState]].injected = true;
				}
				
				$$('p.selected').removeClass('selected');
				tmpEl.getParent().addClass('selected');
				
				if (menuState) {
					dollsByCategory[boxIndex[menuState]].close.set('opacity', 0);
					$(dollsByCategory[boxIndex[menuState]].container).setStyle('display', 'none');
				//.chain(function(){this.element.setStyle('display', 'none');});
				}
				$(clickState).setStyle('display','block');
				if (init) 
					dollsByCategory[boxIndex[clickState]].open.set('opacity', 1);
				else {
					dollsByCategory[boxIndex[clickState]].open.start('opacity', 1);
					$('bottomImage').setStyle('height',$(clickState).getSize().y + 'px')
				}
				
				
				menuState = clickState;
				
				if(clickState != 'category_all')
				document.fireEvent('select_category',clickState)
			}
			
		}
		
	},
	clickThumnailFunction: function(name){
		
		this.fireEvent('clickThumnail',name)
	}
	
})
