var Ui = function(){ return {

	maps: {},

	map_ico: null,

	points_width: 1200,

	currencys_source: '/udata/custom/load_currencys/',

	prices: [

		{ 'from':0,'to':200000,'summary':500 },
		{ 'from':200001,'to':450000,'summary':1000 },
		{ 'from':450001,'to':1200000,'summary': 2000 },
		{ 'from':1200001,'to':2500000,'summary': 5500 },
		{ 'from':2500001,'to':5000000,'summary': 7500 },
		{ 'from':5000001,'to':10000000,'summary': 20000 },
		{ 'from':10000001,'to':30000000,'summary': 50000 },
		{ 'from':30000001,'to':999999999999,'summary':100000 }

	],

	currencys: { 'RUB':[ 1,'' ],'USD':[ 0,'$' ],'EUR':[ 0,'&euro;' ] },

	// init default settings/hanlders

	init: function()

	{

		var that = this; // copy main object

		// init Yandex Map placemark style

		that.map_ico = new YMaps.Style();
		that.map_ico.iconStyle = new YMaps.IconStyle();
		that.map_ico.iconStyle.href = '/images/current/a51.png'; // image
		that.map_ico.iconStyle.size = new YMaps.Point( 28,29 ); // placemark size
		that.map_ico.iconStyle.offset = new YMaps.Point( -9,-32 ); // mark offset from point

		// init addresses links

		var addr = document.getElements( 'a[class^=parent_]' );

		// if exists addresses links

		if( $type( addr )=='array' )

		{

			addr.each( function( el,i )

			{

				// if link got active

				if( el.hasClass( 'active' ) )

				{

					 // getting data from rel attr

					var data = el.get( 'rel' ).split( ':' );

					// if exists data

					if( $type( data )=='array' )

						// call click handler

						// data: array( 0:map_id,1:coords,2:title,3:description )

						that.map_point( data[ 0 ],{ 'coords':data[ 1 ],'title':data[ 2 ],'descr':data[ 3 ] } );

				}



				// set up click handler

				el.addEvent('click',function( e )

				{

					e.stop(); // stop click event


					// getting data from rel attr

					var data = this.get( 'rel' ).split( ':' );

					// unset active style from all links

					addr.each(function(el,i){ if( el.hasClass( 'parent_' + data[ 0 ] ) ) el.removeClass( 'active' ) });

					// set active style at current link

					this.addClass( 'active' );



					// if exists data

					if( $type( data )=='array' )

						// call click handler

						// data: array( 0:map_id,1:coords,2:title,3:description )

						that.map_point( data[ 0 ],{ 'coords':data[ 1 ],'title':data[ 2 ],'descr':data[ 3 ] } );

				});

			});

		}



		// style knob then move

		$( 'knob' ).addEvents({

			'mousedown': function(){

				this.setStyles({ 'background-position':'0 -32px','background-repeat':'no-repeat' })

			},

			'mouseup': function(){

				this.setStyles({ 'background-position':'0 0','background-repeat':'no-repeat' })

			}

		});

		



		// init contacts slider

		var slide_block = $( 'slideBlock' );

		// if exists element

		if( $type( slide_block )=='element' )

		{

			// getting scroll size

			var slide_scroll = slide_block.getScrollSize();

			// getting slide points count

			var points = $$('.slider_point').length+1;

			// check first child of slide block

			var slide_child = slide_block.getChildren('table')[0];

			//if exists

			if( $type( slide_child )=='element' )

				slide_child.setStyle( 'width',points*that.points_width );



			// init slider object

			var slider = new Slider(

				$('slider'),$('knob'),

				{

					'range': [ 0,slide_scroll.x ],

					'onChange': function( step )

					{
						step = step - 1;
						slide_block.scrollTo( step*20-20,scroll.y );
						window.console.log('step = ', step );

					}


				}

			).set( 0 );



			// check slideer position from URL

			var curr_lnk = self.location.href; var pos = 0; var fixPos = 0;

			if( /\#[0-9]+$/.test( curr_lnk ) )

				pos = curr_lnk.substr( curr_lnk.lastIndexOf( '#' )+1,curr_lnk.length );

			// set new position to selected item

			if( pos>0 )

				slider.tick( pos*60,function( position )

				{

					var i=1; var curr = $( 'item0' + ( ( pos%60 )-i ) );

					// checking while element not exists

					while( $type( curr )!='element' )

						curr = $( 'item0' + ( ( pos%60 )-(i++) ) );

					curr.setStyle( 'font-weight','bold' );

				});



			// init slider points

			var slider_points = $$( '.slider_point' );

			if( $type( slider )=='object' && $type( slider_points )=='array' )

				slider_points.each(function(el){

					// every point click
					el.addEvent('click',function(e){

						e.stop();

						// clear bold mark from all points
						
						slider_points.setStyle( 'font-weight','normal' );

						// get current point value
						
						var href = this.get( 'rel' );

						if( /\#[0-9]+$/.test( href ) )

							pos = href.substr( href.lastIndexOf( '#' )+1,href.length );
							
						// Different positions for different browsers
						
						/*if (Browser.Engine.presto) { fixPos = (pos*60) }
            			if (!Browser.Engine.presto) { fixPos = (pos*60 + 1) }*/

						// set new position to selected item

						if ( pos>0 ) {
						
						    //window.console.log(position);
							
							slider.tick( (pos*60)+1, function( position ) {

								var i=1; var curr = $( 'item0' + ( ( pos%60 )-i ) );

								// checking while element not exists
								while( $type( curr )!='element' )
									curr = $( 'item0' + ( ( pos%60 )-(i++) ) );
								curr.setStyle( 'font-weight','bold' );

							});
						}
						
					}); //end event

				});

		}

	},



	// set map point

	// map_id: int

	// argf: array( coords,title,descr )

	map_point: function( map_id,argf )

	{

		var that = this; // copy main object



		// if map element exists

		if( $type( $( 'map_' + map_id ) )=='element' )

		{

			// if map object not exists

			if( !that.maps[ map_id ] )

			{

				// init new map object

				that.maps[ map_id ] = new YMaps.Map( $( 'map_' + map_id ) );

				// enable zoom toolbar

				that.maps[ map_id ].addControl( new YMaps.Zoom() );

				// zoom by wheel

				that.maps[ map_id ].enableScrollZoom();

			}



			// coords from argf

			var coords = argf['coords'].split(',');

			// set map center ( 0 max distance, 17 min distance )

			that.maps[ map_id ].setCenter( new YMaps.GeoPoint( coords[ 0 ],coords[ 1 ] ),12 );



			// init placemark

			var placemark = new YMaps.Placemark( new YMaps.GeoPoint( coords[ 0 ],coords[ 1 ] ),{ style:that.map_ico,balloonOptions:{ maxWidth:150,hasCloseButton:true,mapAutoPan:0 } } );

			// set placemark title from argf

			placemark.name = argf[ 'title' ];

			// set placemark description from argf

			placemark.description = argf[ 'descr' ];

			// add placemark to map

			that.maps[ map_id ].addOverlay( placemark );

		}

	},



	// loading currencys

	load_currency: function()

	{

		var that = this; // copy main object



		// start request

		new Request({

			'url': that.currencys_source,

			'method': 'get',

			'evalScripts': false,

			'evalResponse': false,

			'onSuccess': function( text,xml ){

				// trying to get response

				var echo = xml.getElementsByTagName('udata').item(0).firstChild.nodeValue.split( '&' ); var data = [];

				for( var i=0;i<echo.length;i++ )

				{

					var line = echo[ i ].split( '=' );

					data[ line[ 0 ] ] = line[ 1 ];

				}



				// scan needed currencys

				for( var k in that.currencys )

				{

					// check element exists and set value

					var obj = $( k );

					if( $type( obj )=='element' && data[ k ] )

					{

						obj.set( 'html',data[ k ] );

						that.currencys[ k ][ 0 ] = data[ k ];

					}

				}

				// check element exists and set current date

				var current_date = $( 'current_date' );

				if( $type( current_date )=='element' && data[ 'current' ] )

					current_date.set( 'html',data[ 'current' ] );

			}

		}).send();

	},



	// handler for calculator

	calculator: function()

	{

		var that = this; // copy main object



		// getting price & results elements

		var price = $( 'price' ); var results = $( 'results' );



		// if not exists do nothing

		if( $type( price )!='element' || $type( results )!='element' )

			return false;



		// set styles to default

		price.removeClass( 'notFilled' ); results.setStyle( 'display','none' );



		// get price value

		var price_value = price.get( 'value' ).toInt();

		// if price not a number

		if( !/^[0-9]+$/.test( price.value ) )

		{

			// set error style on price element

			$( 'price' ).addClass( 'notFilled' ).set( 'value','' ).focus();

			return false;

		}



		// set visible on results element

		results.setStyle( 'display','block' );



		// getting currency & bid values

		var currency = $( 'currency' ).getSelected().get( 'value' );

		var bid = document.getElements( 'input[id^=bid]:checked' ).get( 'value' );



		// calculation

		var summary_to=summary_to_val=0;

		var summary = price_value*that.currencys[ currency ][ 0 ];



		for( var k in that.prices )

			if( summary>=that.prices[ k ][ 'from' ] && summary<=that.prices[ k ][ 'to' ] )

			{

				summary_to = that.prices[ k ][ 'summary' ];

				summary_to_val = that.prices[ k ][ 'summary' ]/that.currencys[ currency ][ 0 ];

				break;

			}

		var poshl = price_value*bid/100;

		var nds = ( price_value+poshl.toInt() )*0.18;

		var itogo = summary_to_val+poshl+nds;



		// setting calculation results into html

		if( $type( $( 'result_1' ) )=='element' )

			$( 'result_1' ).set( 'html',( summary_to.toFixed( 2 ) ) );

			

		if( $type( $( 'result_2' ) )=='element' )

		{

			$( 'result_2' ).set( 'html',( poshl.toFixed( 2 ) + ' ' + that.currencys[ currency ][ 1 ] ) );

			if( that.currencys[ currency ][ 0 ]!=1 && $type( $( 'result_21' ) )=='element' )

				//$( 'result_2' ).appendText(' ('+( poshl*that.currencys[ currency ][ 0 ] ).toFixed( 2 ) + ' руб.)' );

				$( 'result_21' ).set('html', '('+( itogo*that.currencys[ currency ][ 0 ] ).toFixed( 2 ) + ')' );

				

		}

		

		if( $type( $( 'result_3' ) )=='element' )

		{

			$( 'result_3' ).set( 'html',( nds.toFixed( 2 ) + ' ' + that.currencys[ currency ][ 1 ] ) );

			if( that.currencys[ currency ][ 0 ]!=1 && $type( $( 'result_31' ) )=='element' )

				//$( 'result_3' ).appendText(' ('+( nds*that.currencys[ currency ][ 0 ] ).toFixed( 2 ) + ' руб.)' );

				$( 'result_31' ).set('html', '('+( itogo*that.currencys[ currency ][ 0 ] ).toFixed( 2 ) + ')' );

		}

		

		if( $type( $( 'result_4' ) )=='element' )

		{

			$( 'result_4' ).set( 'html',( itogo.toFixed( 2 ) + ' ' + that.currencys[ currency ][ 1 ] ) );

			if( that.currencys[ currency ][ 0 ]!=1 && $type( $( 'result_41' ) )=='element' )

				//$( 'result_4' ).appendText(' ('+( itogo*that.currencys[ currency ][ 0 ] ).toFixed( 2 ) + ' руб.)' );

				$( 'result_41' ).set('html', '('+( itogo*that.currencys[ currency ][ 0 ] ).toFixed( 2 ) + ')' );

		}



		return false;

	}



}}();
