// JavaScript Document
//////////////////////////////////////////////////////////////
//Class Gallery
Gallery = Class.create();

Gallery.prototype = {
	//puclic method
	initialize : function(dom, image_path, max_num, image_prefix) {
		this.dom = dom;
		this.image_path = image_path;
		this.current_num = 1;
		this.max_num = max_num;
		
		this.current_number_display = new Array();
		this.obj_back = new Array();
		this.obj_next = new Array();
		
		if(image_prefix != null){
			this.image_prefix = image_prefix;
		} else {
			this.image_prefix = "image";
		}
	},
	setImagePrefix : function(image_prefix) {
		this.image_prefix = image_prefix;
		return this;
	},
	setImageDom : function(obj_dom, photo_num){
		this.image_dom = obj_dom.getDom();
		this.dom.appendChild(this.image_dom);
		this.changeImageTo(photo_num);
		return this;
	},
	nextImage : function(){
		if( this.hasNextImage() ){
			if(this.next_effect != null){
				this.next_effect(this);
			} else {
				this.setNextImage();
			}
		} else if( this.no_more_event != null ){
			this.no_more_event();
		}
		return this.hasNextImage();
	},
	backImage : function(){
		if( this.hasBackImage() ){
			if(this.back_effect != null){
				this.back_effect(this);
			} else {
				this.setBackImage();
			}
		}
		return this.hasBackImage();
	},
	getCurrentNumber : function(){
		return this.current_num;
	},
	setClickEvent : function(event_func){
		this.dom.style.cursor = "pointer";
		this.startClickObserve(event_func);
		//this.click_event = event_func;
		return this;
	},
	setNoMoreItemFunc : function(event_func){
		this.no_more_event = event_func;
		return this;
	},
	isLastImage : function(){
		return this.current_num == this.max_num;
	},
	addCurrentNumberDisplay : function(number_display){
		number_display.setNumber(this.current_num);
		this.current_number_display.push(number_display);
		return this;
	},
	addMaxNumberDisplay : function(number_display){
		number_display.setNumber(this.max_num);
		return this;
	},
	setNextEffect : function(effect_func){
		this.next_effect = effect_func;
		return this;
	},
	setBackEffect : function(effect_func){
		this.back_effect = effect_func;
		return this;
	},
	addBackButton : function(obj_button){
		//obj_button.toHidden();
		this.obj_back.push(obj_button);
		this.syncAll();
		return this;
	},
	addNextButton : function(obj_button){
		this.obj_next.push(obj_button);
		this.syncAll();
		return this;
	},
	changeImageTo : function( num ){
		this.current_num = num;
		this.setImage( this.current_num );
	},

	//private method//////////////////////////////////
	hasNextImage : function(){
		//alert((this.max_num - this.current_num) > 0);
		return (this.max_num - this.current_num) > 0;
	},
	hasBackImage : function(){
		return this.current_num-1 >= 1;
	},
	setImage : function(num){
		this.image_dom.src =  this.getFullPath(num);
		this.syncAll();
		//this.dom.src = this.getFullPath(num);
	},
	getFullPath : function(num){
		return this.image_path + this.image_prefix + "_" + num+".jpg";
	},
	startClickObserve : function(target_func){
		Event.observe(this.dom, 'click', target_func);
	},
	setCounter : function(){
		if(this.current_number_display != null){
			for (var i = 0; i < this.current_number_display.length; i++){
				this.current_number_display[i].setNumber(this.current_num);
			}
		}
	},
	setNextImage : function(){
		this.setImage( ++this.current_num );
	},
	setBackImage : function(){
		this.setImage( --this.current_num );
	},
	syncAll : function(){
		this.setCounter();
		for (var i = 0; i < this.obj_next.length; i++){
			//alert(this.current_num+" : "+this.hasNextImage());
			if(!this.hasNextImage()){
				
				this.obj_next[i].toNoMoreItemState();
			} else {
				this.obj_next[i].toHasMoreItemState();
			}
		}
		
		for (var i = 0; i < this.obj_back.length; i++){
			if(!this.hasBackImage()){
				this.obj_back[i].toNoMoreItemState();
			} else {
				this.obj_back[i].toHasMoreItemState();
			}
		}
		
	}
	/*,
	removeObserve : function(target_func){
		Event.stopObserving(this.dom, 'click', target_func);
	}*/
	

};
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//Class Button

Button = Class.create();

Button.prototype = {
	//puclic method
	initialize : function(taget_dom, obj_html) {
		this.dom = taget_dom;
		this.dom.style.cursor = "pointer";
		//this.dom.style.display = "inline";
		
		this.has_more_html = obj_html;

		this.state = 'more';
		this.setInnerHTML(this.has_more_html);
		this.startObserve(this.has_more_html.getEvent());
	},
	setNoMoreItemState : function(obj_html){
		this.no_more_html = obj_html;
	},
	toNoMoreItemState : function(){
		if(this.no_more_html != null){
			this.setInnerHTML(this.no_more_html);
			if(!this.isNoMoreState()){
				this.stopObserve(this.has_more_html.getEvent());
				this.startObserve(this.no_more_html.getEvent());
			}
		} else {
			this.toHidden();
		}
		this.state = 'no_more';
	},
	toHasMoreItemState : function(){
		this.toVisible();
		if(this.no_more_html != null){
			this.setInnerHTML(this.has_more_html);
			if(this.isNoMoreState()){
				this.stopObserve(this.no_more_html.getEvent());
				this.startObserve(this.has_more_html.getEvent());
			}
		}
		this.state = 'more';
	},
	toHidden : function(){
		this.dom.style.visibility = 'hidden';
		return this;
	},
	toVisible : function(){
		this.dom.style.visibility = 'visible';
		return this;
	},
	isNoMoreState : function (){
		return this.state == 'no_more';
	},
	//private method//////////////////////////////////
	setInnerHTML : function(html){
		this.dom.innerHTML = html;
	},
	startObserve : function(obj_event){
		Event.observe(this.dom, obj_event['type'], obj_event['event_func']);
	},
	stopObserve : function(obj_event){
		Event.stopObserving(this.dom, obj_event['type'], obj_event['event_func']);
	}
};

////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//Class NumberButton

NumberButton = Class.create();

NumberButton.prototype = {
	//puclic method
	initialize : function(target_dom, image_path, max_num, image_prefix) {
		this.dom = target_dom;
		
		image_prefix = image_prefix == null ? "num" : image_prefix;
		this.makeButton(image_path, max_num, image_prefix);
	},
	setEventObserv : function(type, obj_event){
		for (var i = 0; i < this.buttons.length; i++){
			Event.observe(this.buttons[i], type, obj_event);
		}
	},
	//private method//////////////////////////////////
	makeButton : function(image_path, max_num, image_prefix){
		this.buttons = new Array();
		for(var i = 1; i <= max_num; i++){
			image_full_path = image_path + image_prefix + "_" + i + ".gif";
			button_dom = new DomObject("img", {'src':image_full_path, "title":i});
			button_dom = button_dom.getDom();
			button_dom.style.cursor = "pointer";
			this.dom.appendChild(button_dom);
			this.buttons.push(button_dom);
		}
	}
};

////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//Class HtmlTags

HtmlTags = Class.create();

HtmlTags.prototype = {
	//puclic method
	initialize : function(tag_name, attributes){
		this.tag_name = tag_name;
		
		if(attributes != null) this.attributes = attributes;
	},
	setAttribute : function(name, value){
		this.attributes[name] = value;
		return this;
	},
	setChild : function(obj_html){
		this.child_html = obj_html;
		return this;
	},
	setEventObserve : function(type,event_func){
		this.obj_event = {"type":type, "event_func":event_func};
		return this;
	},
	getEvent : function(){
		return this.obj_event;
	},
	getHtml : function(){
		return this.getStartTag()+this.getChild()+this.getEndTag();
	},
	//private method
	getStartTag : function(){
		html = "";
		html += "<"+this.tag_name;
		if(this.attributes != null){
			for(var key in this.attributes){
				html += " "+key+'="'+this.attributes[key]+'"';
			}
		}
		html += this.getEndTag() == "" ? " />" : ">";
		return html;
	},
	getChild : function(){
		return this.child_html == null ? "" : this.child_html;
	},
	getEndTag : function(){
		switch(this.tag_name){
			case "img":
			case "meta":
			case "base":
			case "hr":
			case "base":
				return "";
			default:
				return "</"+this.tag_name+">";
		}
	},
	toString : function(){
		return this.getHtml();
	}
};
////////////////////////////////////////////////////////////////
//Class DomObject
DomObject = Class.create();

DomObject.prototype = {
	//puclic method
	initialize : function(tag_name, attributes){
		this.dom = document.createElement(tag_name);
		
		if(attributes != null){
			for(var key in attributes){
				this.dom.setAttribute(key, attributes[key]);
			}
		}
	},
	setEventObserve : function(type, obj_func){
		Event.observe(this.dom, type, obj_func);
		return this;
	},
	setInnerHTML : function(html){
		this.dom.innerHTML = html;
		return this;
	},
	getDom : function(){
		return this.dom;
	}

	//private method

};
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//Class ImageNumberDisplay
ImageNumberDisplay = Class.create();

ImageNumberDisplay.prototype = {
	//puclic method
	initialize : function(dom, path, figure){
		this.dom = dom;
		this.figure = figure == null ? 2 : figure;
		this.image_path = path;
		this.image_prefix = "counter_n";
		this.image_type = "gif";
		this.num_images = new Array();
		this.digit_dom = new Array();
		
		this.imagePreLoad();
		this.initDigitDom();
	},
	setImagePrefix : function(image_prefix){
		this.image_prefix = image_prefix;
		return this;
	},
	setImageType : function(image_type){
		this.image_type = image_type;
		return this;
	},
	setNumber : function(number){
		number = number.toString();
		
		short_figure = this.figure - number.length;
		if(short_figure < 0) alert("to short figure for current count number!");
		
		for(i = 0; i < short_figure; i++){
			number = "0"+number;
		}

		for(i = 0; i < number.length; i++){
			this.digit_dom[i].getDom().src = this.num_images[number.charAt(i)].src;
		}
	},

	//private method
	imagePreLoad : function(){
		for(var i = 0; i < 10; i++){
			filepath = this.image_path + this.image_prefix + i + "." + this.image_type;
    		this.num_images[i] = new Image();
    		this.num_images[i].src = filepath;
		}
	},
	initDigitDom : function(){
		for(var i = 0; i< this.figure; i++){
			this.digit_dom[i] = new DomObject("img");
			this.digit_dom[i].getDom().src = this.num_images[0].src;
			this.dom.appendChild(this.digit_dom[i].getDom());
		}
	}

};
////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////
//Class ImageNumberDisplay
TextNumberDisplay = Class.create();

TextNumberDisplay.prototype = {
	//puclic method
	initialize : function(dom, figure){
		this.dom = dom;
		this.figure = figure == null ? 2 : figure;
	},
	setNumber : function(number){
		number = number.toString();
		
		short_figure = this.figure - number.length;
		if(short_figure < 0) alert("to short figure for current count number!");
		
		/*for(var i = 0; i < short_figure; i++){
			number = "0"+number;
		}*/

		this.dom.innerHTML = number;
	}

	//private method

};
////////////////////////////////////////////////////////////////////

