var addthis_pub				= 'Realty Planex'; 
var addthis_logo			= '';
var addthis_logo_background	= 'ffffff';
var addthis_logo_color		= '201910';
var addthis_brand			= 'IUC';
var addthis_options			= 'facebook, favorites, email, twitter, delicious, google, digg, reddit, live, more';

var ObjectGal = Class.create({
	initialize: function(container, options){
		this.container  = $(container);
		this.image			= $('product_pic_img');
		this.options 		= Object.extend({
			thumbsHolderStyle:	'#products_nav',
			thumbsStyle:		'.products_nav_list a',
			pagesStyle:			'.gal_paging_current dd',
			nextPageStyle:		'.img_but_top',
			previousPageStyle:	'.img_but_bot',
			nextImageStyle:		'#products_nav_next',
			previousImageStyle:	'#products_nav_prev',
			listStyle:			'.products_nav_list',
			perPage:			4,
			listHeight:			106
		}, options);
		
		this.list		= this.container.down(this.options.listStyle);
		this.current	= this.container.down(this.options.thumbsStyle);
		this.page		= 0;
		/*
		this.timer;
		this.seconds 	= 1;
		*/
		this.container
			.delegate(this.options.pagesStyle, 'click', this.selectPage.curry(this))
			.delegate(this.options.thumbsStyle, 'click', this.selectThumb.curry(this))
			.delegate(this.options.nextImageStyle, 'click', this.selectPrevNext.bind(this, 'next', 'previous'))
			.delegate(this.options.previousImageStyle, 'click', this.selectPrevNext.bind(this, 'previous', 'next'))
			.delegate(this.options.nextPageStyle, 'click', this.selectPrevNextPage.bind(this, -1))
			.delegate(this.options.previousPageStyle, 'click', this.selectPrevNextPage.bind(this, 1))
		;
	},
	/*
	setTimer: function(){
		if (timer) clearTimeout(timer);
		timer = setTimeout(selectPrevNext, seconds * 1000);
	},
	*/
	selectThumb: function(gallery, e){
		e.stop();
		gallery.select(this);
	},
	selectPage: function(gallery, e){
		e.stop();
		gallery.gotoPage(+this.innerHTML - 1);
	},
	gotoPage: function(page){
		if (page < 0 && page > this.lastPage) return;
		
		var thumb = this.container.down(this.options.thumbsStyle, page * this.options.perPage);
		if (thumb)  this.select(thumb);
	},
	selectPrevNext: function(first, last){
		var li = this.current.up('li'); 
		this.select( (li[first]('li') || li[last + 'Siblings']().last()).down('a') );
	},
	selectPrevNextPage: function(dir){
		this.gotoPage(this.page + dir);
	},
	select: function(thumb){		
		var number		= thumb.up('li').previousSiblings().length;
		
		this.page	 	= parseInt(number / this.options.perPage);
		this.current 	= thumb;
		
		this.container.select(this.options.thumbsStyle + '.selected').invoke('removeClassName', 'selected');		
		this.container.select(this.options.pagesStyle + '.selected').invoke('removeClassName', 'selected');	
		this.container.select(this.options.nextPageStyle).invoke(this.page == 0 ? 'addClassName' : 'removeClassName', 'disabled');
		this.container.select(this.options.previousPageStyle).invoke(this.page == this.lastPage ? 'addClassName' : 'removeClassName', 'disabled');
		this.container.down(this.options.pagesStyle, this.page).addClassName('selected');
		
		this.list.morph({ top: (-1 * this.page * this.options.listHeight) + 'px' });
		
		this.transformImage(thumb.href);
		
		thumb.addClassName('selected');
	},
	transformImage: function(src){
		this.image.src = 'none';
		this.image.addClassName('loading');
		this.image.setOpacity(1);
		CD3.ImageLoader(src, this.changeImage.bind(this));
	},
	changeImage: function(loader){
		this.image.removeClassName('loading');
		this.image.src = loader.src ;
	}
});

var AnnouncesGal = Class.create({
	initialize: function(container, options){
		this.container  = $(container);
		this.options 	= Object.extend({
			thumbsStyle:				'#announces_list li',
			pagesStyle:					'#announces_pages li',
			nextPageStyle:				'#announces_next',
			previousPageStyle:			'#announces_prev',
			listStyle:					'#announces_list',
			perPage:					1,
			listWidth:					333
		}, options);
		
		this.list		= this.container.down(this.options.listStyle);		
		this.page		= parseInt(this.container.down('.selected').previousSiblings().length / this.options.perPage);		
		this.lastPage	= this.container.select(this.options.pagesStyle).length - 1;
		
		this.container
			.delegate(this.options.pagesStyle, 'click', this.selectPage.curry(this))
			.delegate(this.options.nextPageStyle, 'click', this.selectPrevNextPage.bind(this, 1))
			.delegate(this.options.previousPageStyle, 'click', this.selectPrevNextPage.bind(this, -1))
		;
		
		this.setTimer();
	},
	selectPage: function(gallery, e){
		e.stop();
		gallery.gotoPage(+this.previousSiblings().length);
	},
	gotoPage: function(page){
		if (page < 0) page = this.lastPage;
		if (page > this.lastPage) page = 0;
		
		var thumb = this.container.down(this.options.thumbsStyle, page * this.options.perPage);
		if (thumb)  this.select(thumb);
	},
	selectPrevNextPage: function(dir){
		this.gotoPage(this.page + dir);
	},
	select: function(thumb){
		var number		= thumb.previousSiblings().length;
		
		this.page	 	= parseInt(number / this.options.perPage);
		this.current 	= thumb;
				
		this.container.select(this.options.pagesStyle + '.selected').invoke('removeClassName', 'selected');
		this.container.down(this.options.pagesStyle, this.page).addClassName('selected');
		this.list.morph({ left: (-1 * this.page * this.options.listWidth) + 'px' });
		this.setTimer(); 
	},
	setTimer: function(){
		this.timer && clearTimeout(this.timer);
		this.timer = setTimeout(this.selectPrevNextPage.bind(this, 1), 3000);
	}
});

var PortfolioGal = Class.create({
	initialize: function(container, options){
		this.container  = $(container);
		this.image			= $('product_pic_img');
		this.image_holder	= $('folio_img_holder');
		this.options 		= Object.extend({
			thumbsStyle:		'#folio_list a',
			nextImageStyle:		'#folio_next',
			previousImageStyle:	'#folio_prev',
			listStyle:			'#folio_list',
			perPage:			25,
			listHeight:			16
		}, options);
		
		this.list		= this.container.down(this.options.listStyle);
		this.current	= this.container.down(this.options.thumbsStyle);
		this.page		= 0;
		
		this.container
			.delegate(this.options.thumbsStyle, 'click', this.selectThumb.curry(this))
			.delegate(this.options.nextImageStyle, 'click', this.selectPrevNext.bind(this, 'next', 'previous'))
			.delegate(this.options.previousImageStyle, 'click', this.selectPrevNext.bind(this, 'previous', 'next'))
		;
	},
	selectThumb: function(gallery, e){
		e.stop();
		gallery.select(this);
	},
	selectPrevNext: function(first, last){
		var li = this.current.up('li'); 
		this.select( (li[first]('li') || li[last + 'Siblings']().last()).down('a') );
	},
	select: function(thumb){		
		var number		= thumb.up('li').previousSiblings().length;
		
		this.page	 	= parseInt(number / this.options.perPage);
		this.current 	= thumb;
	
		this.container.select(this.options.thumbsStyle + '.selected').invoke('removeClassName', 'selected');		
		this.list.morph({ top: (-1 * this.page * this.options.listHeight) + 'px' });		
		this.transformImage(thumb.href);
		
		thumb.addClassName('selected');
	},
	transformImage: function(src){
		this.image.src = 'none';
		this.image.addClassName('loading');
		this.image.setOpacity(1);
		
		CD3.ImageLoader(src, this.changeImage.bind(this));
	},
	changeImage: function(loader){
		this.image.removeClassName('loading');
		this.image.src = loader.src;
		//this.image_holder.style.backgroundImage = loader.src;
		this.image_holder.setStyle({width:'auto'});
		console.log(this.image.style.width);
	} 
});

var PortfolioProds = Class.create({
	initialize: function(container, options){
		this.container  = $(container);
		this.options 	= Object.extend({
			nextPageStyle:		'#scroll_next',
			previousPageStyle:	'#scroll_prev',
			listStyle:			'#scroll_folio',
			pagesStyle:			'#scroll_folio li',
			perPage:			5,
			listWidth:			750,
			itemWidth:			150
		}, options);
		
		this.list		= this.container.down(this.options.listStyle);
		this.page		= 0;
		this.lastPage	= parseInt(this.container.select(this.options.pagesStyle).length / this.options.perPage) + 1;
		//console.log(this.container.select(this.options.pagesStyle).length % this.options.perPage, this.lastPage);
		this.container
			.delegate(this.options.previousPageStyle,	'click', this.selectPrevNextPage.bind(this, -1))
			.delegate(this.options.nextPageStyle,		'click', this.selectPrevNextPage.bind(this, 1))
		;
	},
	selectPrevNextPage: function(dir){
		this.page = this.page + dir;
		if (this.page < 0) this.page = this.lastPage;
		if (this.page >= this.lastPage) this.page = 0;
		
		this.list.morph({ left: (-1 * this.page * this.options.listWidth) + 'px' });
	}
});

var IndexOffers = Class.create({
	initialize: function(container, options){
		this.container  = $(container);
		this.options 	= Object.extend({
			thumbsStyle:				'.objects_property',
			pagesStyle:					'#offers_index_tabs li',
			listStyle:					'#offers_index_wrap',
			perPage:					1,
			listWidth:					892
		}, options);
		
		this.list		= this.container.down(this.options.listStyle);
		
		this.page		= parseInt(this.list.down('.selected').previousSiblings().length / this.options.perPage);
		this.lastPage	= this.container.select(this.options.pagesStyle).length - 1;
		
		this.container
			.delegate(this.options.pagesStyle, 'click', this.selectPage.curry(this))
		;
	},
	selectPage: function(gallery, e){
		e.stop();
		gallery.gotoPage(+this.down('em').innerHTML);
				
		gallery.container.select(gallery.options.pagesStyle + '.selected').invoke('removeClassName', 'selected');	
		this.addClassName('selected');		
	},
	gotoPage: function(page){
		if (page < 0 && page > this.lastPage) return;
		
		var thumb = this.container.down(this.options.thumbsStyle, page * this.options.perPage);
		if (thumb)  this.select(thumb);
	},
	selectPrevNext: function(first, last){
		var li = this.current.up('li'); 
		this.select( (li[first]('li') || li[last + 'Siblings']().last()).down('a') );
	},
	select: function(thumb){
		var number		= thumb.previousSiblings().length;
		
		this.page	 	= parseInt(number / this.options.perPage);
		this.current 	= thumb;
		
		this.list.morph({ left: (-1 * this.page * this.options.listWidth) + 'px' });
	}
});

CD3.Behaviors({
	'body': {
		'focus:in': {
			'input[type=text]': function(){
				if (this.getValue() == this.getAttribute('title')) this.setValue('');
			}
		},
		'focus:out': {
			'input[type=text]': function(){
				if (this.getValue().length == 0) this.setValue(this.getAttribute('title') || '');
			}
		}
	},
	'#text_size': function(){
		new CD3.FontSwitcher(this, 'content', {
			reset:	'.reset',
			plus:	'.plus',
			minus:	'.minus'
		});
	},
	'#bookmark': {
		mouseover: function(){
			addthis_open(this, '', location.href, addthis_pub)
		},
		mouseout: addthis_close,
		click: addthis_sendto
	},
	'#infos_index_tabs:click': (function(){
		function scroll(left, item){
			return function(e){
				e.stop();
				$('infos_index_tabs').select('li').each(function(element){
					element.removeClassName('current');
				});
				this.addClassName('current');
				$('infos_index_texts').morph("left: " + left + "px");
			
				$(item).morph("opacity:0", {
					afterFinish: function(e){
						e.element.setOpacity(1);
					}
				});
			};
		}
		return {
			'#index_tabs_important': scroll(0, 'index_texts_faq'),
			'#index_tabs_faq': scroll(-650, 'index_texts_important')
		};
	})(),
	'#gotoheader:click':function(){
		Effect.ScrollTo('header', { duration:'0.8'});
	},
	'#object_map:click':function(){
		var body = parseInt($$('body')[0].getStyle('height'));
		$('overlay_map').setStyle({display:'block',height:+body+'px'});
		
		this.addClassName('selected');
		$('google_map').style.visibility = 'visible';
		$('close_map').style.display = 'block';
	},
	'#close_map:click':function(){
		this.style.display= 'none';
		$('object_map').removeClassName('selected');
		$('google_map').style.visibility = 'hidden';
		$('overlay_map').setStyle({display:'none',zIndex: '0'});
	},
	'#overlay_map:click': function(){
		this.setStyle({display:'none',zIndex: '0'});
		$('close_map').style.display = 'none';
		$('object_map').removeClassName('selected');
		$('google_map').style.visibility = 'hidden';
	},
	'#show_top_offers:click':function(){
		var body = parseInt($$('body')[0].getStyle('height'));
		$('overlay_offers').setStyle({display:'block',height:+body+'px', zIndex: '900'});
		$('top_offers').style.visibility = 'visible';
		$('top_offers').style.top = '296px';
		$('top_offers').down('.sIFR-flash').style.display = 'block';
	},
	'#close_top_offers:click':function(){
		$('top_offers').style.visibility = 'hidden';
		$('top_offers').style.top = '-1000px';
		$('top_offers').down('.sIFR-flash').style.display = 'none';
		$('overlay_offers').setStyle({display:'none',zIndex: '0'});
	},
	'#overlay_offers:click': function(){
		this.setStyle({display:'none',zIndex: '0'});
		$('top_offers').style.visibility = 'hidden';
		$('top_offers').down('.sIFR-flash').style.display = 'none';
	},
	'#estate_request_link:click':function(){
		var body = parseInt($$('body')[0].getStyle('height'));
		this.style.zIndex = '1500';
		$('overlay_property').setStyle({display:'block',height:+body+'px',zIndex: '900'});
		$('property_request').setStyle({height: '355px', left: '-500px', top: '-404px', visibility: 'visible', width: '812px', zIndex: '1500'});
		$('close_property_request').style.display = 'block';
	},
	'#close_property_request:click':function(){
		$('estate_request_link').style.zIndex = '0';
		$('property_request').setStyle({height: '0', width: '0', zIndex: '0', visibility: 'hidden'});
		$('overlay_property').setStyle({display:'none',zIndex: '0'});
		this.style.display = 'none';
	},
	'#overlay_property:click': function(){
		this.setStyle({display:'none',zIndex: 0});
		$('estate_request_link').style.zIndex = '0';
		$('property_request').setStyle({height: '0', width: '0', zIndex: '0', visibility: 'hidden'});
		$('close_property_request').style.display = 'none';
	},
	/*
	'#overlay_info:click': function(){
		this.setStyle({display:'none',zIndex: 0});
		$('estate_request_link').style.zIndex = '0';
		$('property_request').setStyle({height: '0', width: '0', zIndex: 0, visibility: 'hidden'});
		$('close_property_request').style.display = 'none';
		$('close_map').style.display = 'none';
		$('object_map').removeClassName('selected');
		$('google_map').style.visibility = 'hidden';
	},
	*/
	'#advanced_search:click': function(){
		$('search_boxer').morph("height: 110px",{
			afterFinish: function(){
				$('search_boxer').select('.advanced').each(function(advanced){
					//new Effect.Opacity(advanced, { from: 0, to: 1, duration: 0.5 });
					advanced.style.display = 'block';
				})
			}
		});		
		this.style.display = 'none';
		$('short_search').style.display = 'block';
	},
	'#short_search:click': function(){		
		$('search_boxer').morph("height: 56px",{
			beforeStart: function(){
				$('search_boxer').select('.advanced').each(function(advanced){
					//new Effect.Opacity(advanced, { from: 1.0, to: 0, duration: 0.5 });
					advanced.style.display = 'none';
				})
			}
		});
		$('search_boxer').select('.advanced').each(function(advanced){
			//new Effect.Opacity(advanced, { from: 0, to: 1, duration: 0.5 });
			if (advanced.down('.dropper').down('li').down('a') && advanced.down('.dropper').down('input[type=hidden]')) {
				advanced.down('.dropper').down('a').down('span').innerHTML = advanced.down('.dropper').down('li').down('a').innerHTML;
				advanced.down('.dropper').down('input[type=hidden]').value = 0;
			}
		});
		$('select_exposure').select('input[type=checkbox]').each(function(checker){
			checker.checked = '';
		});
		this.style.display = 'none';
		$('advanced_search').style.display = 'block';
	},
	'#results_header':function(){
		$('search_boxer').setStyle({height: '110px'});
		$('search_boxer').select('.advanced').each(function(advanced){
			//advanced.setStyle({opacity:1});
			advanced.style.display = 'block';
		})
		$('advanced_search').style.display = 'none';
		$('short_search').style.display = 'block';
	},
	'#tab_menu':function(){
		$('tabcon_2').style.display = 'none'; 
		$('tabcon_3').style.display = 'none'; 
	},
	'#tab_menu li:click':function(){
		$$('#tab_menu li').each(function(all){
			all.removeClassName('selected');
		})
		this.addClassName('selected');
		var data = this.readAttribute('data');
		
		$('tabcon_1').style.display = 'none'; 
		$('tabcon_2').style.display = 'none'; 
		$('tabcon_3').style.display = 'none'; 
		
		$(data).style.display = 'block';
	},
	'#select_exposure a:click':function(){
		this.next('div').toggle();
	},
	'#search_fields select': CD3.Select,
	'#object_gallery': ObjectGal,
	'#portfolio_gallery': PortfolioGal,
	'#offers_index': IndexOffers,
	'#announces': AnnouncesGal,
	'#portfolio_products': PortfolioProds,
	'#image_gallery_1, #image_gallery_2, #image_gallery_3': function(){
		if ($('gallery_slider_1') || $('gallery_slider_2') || $('gallery_slider_3')) {
			var element = parseInt(this.down('li').getStyle('width')) + 15;
			this.style.width = this.getElementsByTagName('li').length * element  + 'px';
			
			var scrollCointer = this.up('div'),
				scroll 		  = new CD3.Scroller(scrollCointer, this.up('div').next('div').identify(), {
					styleSlider: 	'scroll',
					styleArrow:		'arrow',
					styleMoveUp:	'uparrow',
					styleMoveDown:	'downarrow'
				});
			if (scroll){
				var selected = this.down('.selected');
				if (selected && scrollCointer.getWidth() < selected.offsetLeft + selected.getWidth()){
					scroll.scrollTo(selected.offsetLeft);
				}
			}
		}
	}
});

var opal = {
  src: '/swf/opal.swf'
};

//sIFR.forceWidth = true;
//sIFR.fitExactly = true;
//sIFR.forceSingeLine = true;
sIFR.activate(opal);

sIFR.replace(opal, {
  selector: '#info_title h1, #info_title h2, .info_title h1'
  ,css: [
    '.sIFR-root { color: #e3e0d6; font-weight: normal; }'
	,'a { text-decoration: none; font-size: 21px; }'
    ,'a:link { color: #e3e0d6; }'
    ,'a:hover { color: #e3e0d6; }'
  ]
  ,wmode: 'transparent'
});
sIFR.replace(opal, {
  selector:  '.title_h'
  ,css: [
    '.sIFR-root { color: #ffffff; font-weight: normal; text-align:center; }'
  ]
  ,wmode: 'transparent'
});
sIFR.replace(opal, {
  selector:  '.object_h'
  ,css: [
    '.sIFR-root { color: #ffffff; font-weight: normal; }'
  ]
  ,wmode: 'transparent'
});
sIFR.replace(opal, {
  selector: '#contacts_list h2'
  ,css: [
    '.sIFR-root { color: #2e2e2f; font-size: 19px; }'
  ]
  ,wmode: 'transparent'
});

sIFR.replace(opal, {
  selector: '#subinfos_menu h3'
  ,css: [
    '.sIFR-root { color: #e3e0d6; font-size: 19px; }'
  ]
  ,wmode: 'transparent'
});

sIFR.replace(opal, {
  selector: '#property_request_wrap #property_request h4'
  ,css: [
    '.sIFR-root { color: #2e2e2f; font-size: 18px; }'
  ]
  ,wmode: 'transparent'
});

sIFR.replace(opal, {
  selector: '#matrix_header h2'
  ,css: [
    '.sIFR-root { color: #2e2e2f; font-size: 19px; }'
  ]
  ,wmode: 'transparent'
});

sIFR.replace(opal, {
  selector: '#top_offers h3'
  ,css: [
    '.sIFR-root { color: #f7f3ed; font-size: 22px; }'
  ]
  ,wmode: 'transparent'
});

sIFR.replace(opal, {
  selector: '.folio_title span'
  ,css: [
    '.sIFR-root { color: #ffffff; font-size: 21px; }'
  ]
  ,wmode: 'transparent'
  ,forceSingleLine: true
});








