var  Start = Class.create();

/************************************************************
*
*/

Start.prototype = {
	
	initialize: function( ourCity, options ) {
		this.options = options;

		if(ourCity){
			this.setAddress( this.options.cityList.select(".current").first() );
		}else {
			this.hideAddress();
		}

		this.options.cityList.hide();
		this.options.addressList.hide();
		
		this.options.anotherCity.observe( "click", this.anotherCityClick.bind(this) );		
		this.options.cityList.select( "."+ this.options.cityItemClass).each( function( item ) {
			var currentCityId = this.options.currentCity.readAttribute("_id");
			if( item.readAttribute("_id") == currentCityId ) item.hide();
			item.observe( "click", this.cityItemClick.bind( this, item ) );		
		}.bind( this ));
	
		this.options.closeCity.observe( "click", function(){
						this.options.anotherCity.show();
						this.options.cityList.hide();
		}.bind(this) );

		this.options.showAddress.observe( "click", function(){
						this.options.addressList.show();
		}.bind(this) );

		this.options.closeAddress.observe( "click", function(){
						this.options.addressList.hide();
		}.bind(this) );
	
	},
	
	anotherCityClick: function( ){
		this.options.cityList.show();
		this.options.anotherCity.hide();
	},
	
	cityItemClick: function( item ){
		var innerElement = item.select("span").first();
		this.options.currentCity.update( innerElement.innerHTML ); 
		//this.options.currentCity._id = item.readAttribute("_id"); 
		this.options.currentCity.writeAttribute({"_id":item.readAttribute("_id")});
		
		this.options.anotherCity.show();
		this.options.cityList.hide();
		
		this.options.cityList.select( "."+ this.options.cityItemClass).each( function( currentItem, item ) {
			var currentCityId = currentItem.readAttribute("_id");
			( item.readAttribute("_id") == currentCityId ) ? item.hide() : item.show();
		}.bind( this, item ));

		// set address
		this.setAddress( item ); 
		
		// set cookie
		this.createCookie( "__currentCityId", item.readAttribute("_id"), 30, "/" );
		
		//fire event
		this.options.currentCity.fire("city:change");
	},
	
	/**
	* Set address
	*/
	setAddress: function( item ){
		this.options.addressList.select( "."+ this.options.addressItemClass).each( function( currentItem, item ) {
			var currentCityId = currentItem.readAttribute("_id");
			//console.log(item);
			if( item.readAttribute("_id") == currentCityId ) {
				item.show();
			} else {
				item.hide()
			}
		}.bind( this, item ));
		this.options.tip.hide();
	},
	
	hideAddress: function(){
		this.options.addressList.select( "."+ this.options.addressItemClass).invoke('hide');
		this.options.tip.show();
	},
	
	/**
	*	Util method
	*/
	createCookie: function( name, value, days, path ) {
		if (days) {
			var date = new Date();
			date.setTime(date.getTime()+(days*24*60*60*1000));
			var expires = ";expires="+date.toGMTString();
		}
		else var expires = "";
		document.cookie = name + "=" + value + expires + "; path=" + path;
	}
	
};
