var cochin = {
	src: '/swf/hebar.swf'
};

sIFR.useStyleCheck = true;
sIFR.activate(cochin);
sIFR.replace(cochin, {
	selector: 'h1.opalche',
	css: {'.sIFR-root': { 'color':'#a21c1d', 'font-size':'18px','text-indent':'0px' } },
	wmode: 'transparent'
});

sIFR.replace(cochin, {
	selector: 'div#contra',
	css: {'.sIFR-root': { 'color':'#eee3d3', 'font-size':'14px','text-indent':'0px' } },
	wmode: 'transparent'
});

CD3.Behaviors({
	'#eko_materials': function(){
		new CD3.Lightbox(this.select('a.eko_in_img'));
	},
	'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') || '');
			}
		}
	},
	'#font_bg,#font_en': function(){
		new CD3.FontSwitcher(this, 'page', {
			reset:	'.reset',
			plus:	'.plus',
			minus:	'.minus'
		});
	},
	'#custom_radio input[type=radio]':CD3.Radio,
	'#custom_checkbox input[type=checkbox]':CD3.Checkbox,
	'#player':function(){
		flowplayer("player", "/swf/flowplayer-3.1.5.swf",{
			clip: {
				autoPlay: false,
				image: '/images/demo/demo-1.jpg',  
          	 	//scaling: 'orig' ,
				autoBuffering: false 
			}		
		});
	},
	'#info_gallery .showme_video':function(){
		flowplayer("mainimagewrapper", "/swf/flowplayer-3.1.5.swf",{
			clip: {
				autoPlay: false,
				//image: '/images/demo/demo-1.jpg',  
          	 	//scaling: 'orig' ,
				autoBuffering: false 
			}		
		});
	},
	'#gallery .gal_cent,#video_gallery .gal_cent':function(){
		this.style.width = this.getElementsByTagName('li').length > 5 ? 135 + 'px' : 'auto';
	},
	'#center_num':function(){
		this.style.width = 	(this.down('div').clientWidth + 81) + 'px' ;
	},
	'#slider': function(){
		var frame = $('frame');
		
		new PoliSlider(this, {
			scrollBy:				this.hasClassName('team') ? 780 : 775,
			buttons: 				{next: frame.down('.last'), previous: frame.down('.first')},
			paging:					$('listing'),
			setButtonVisibility:	function(button, visible){ button[visible ? 'removeClassName' : 'addClassName']('none'); },
			fading:					true
		});
	}
/*,
	'#eko_init_button:click': function(e){
		e.stop();
		$('eko_init_container').show();
		$('eko_mats_container').hide();
	},
	'#eko_mats_button:click': function(e){
		e.stop();
		$('eko_init_container').hide();
		$('eko_mats_container').show();
	}
*/
});


CD3.Behaviors('#video_gallery', function(){
	var videolist = $('thumbslist'),
		container = $('mainimagewrapper');
		

	function selectVideo(link){
		videolist.select('.selected').invoke('removeClassName', 'selected');
		link.addClassName('selected');
		
		container.setAttribute('href', link.getAttribute('href'));
		
		flowplayer("mainimagewrapper", "/swf/flowplayer-3.1.5.swf",{
			clip: {
				autoPlay: false,
				autoBuffering: false 
			}		
		});		
	}
	
	function nextImage(direction, fallback){
		var selected = videolist.down('a.selected');
					
		selectVideo(((selected && selected.up('li')[direction]('li')) || videolist.select('li')[fallback]()).down('a'));
	}
	
	return {
		'div.controls:click': {
			'#thumbslist a': function(e){
				e.stop();
				selectVideo(this);
			},
			'#preva': nextImage.curry('previous', 'last'),
			'#nexta': nextImage.curry('next', 'first')
		}
	};

});

CD3.Behaviors('#gallery', function(gallery){
	var loader 			= CD3.ImageLoader(), 
		container		= $('mainimagewrapper'),
		image 			= $('mainimage'),
		thumbslist		= $('thumbslist'),
		thumbscount		= thumbslist.getElementsByTagName('li').length,
		highlightsLink	= $('highlights_link'),
		isInfoGallery	= !!$('info_gallery'),
		isIndexGallery 	= !!highlightsLink,
		isProductGallery= !!$('folio_gallery');
		
		
		if (isInfoGallery){
			thumbslist.setStyle({
				width:		thumbscount * 19 + 'px',
				position:	'relative',
				left:		'0px'
			});
		}else{
			thumbslist.setStyle({
				width:		thumbscount * 27 + 'px',
				position:	'relative',
				left:		'0px'
			});
		}
		
	
	
	if (isInfoGallery){
		container.style.height = '350px';
	} else if(isProductGallery){
		loader($('mainimage').src, function(){
			container.style.height = container.getHeight() + 'px';
		});
	}
	
	function showImage(){
		image.appear({
			duration: .4,
			afterFinish: function(){
				container.removeClassName('loading');
			}
		 });
	}
	
	function onLoad(loader){
		if (isInfoGallery){
			image.src = loader.src;
			container.morph('height:350px', {
				duration: .4,
				afterFinish: showImage
			});
		} else if(isProductGallery){
			image.src = loader.src;
			container.morph('height:' + loader.height + 'px', {
				duration: .4,
				afterFinish: showImage
			});
		}	else {
			image.style.backgroundImage = 'url('+ loader.src +')'; // loader.src;
			showImage();
		}
	}
			
	function selectImage(thumb){
		thumbslist.select('.selected').invoke('removeClassName', 'selected');
		thumb.addClassName('selected');
		
		container.addClassName('loading');
		
		var src = thumb.href;
		if (isIndexGallery){
			src = thumb.getAttribute('data-src');
			highlightsLink.href = thumb.href;
			highlightsLink.title = thumb.title;
		}
		
		image.fade({
			duration: .3,
			afterFinish: loader.curry(src, onLoad)
		});
		
		var position = parseInt(thumb.id.match(/\w+_(\d+)/)[1]);
			position = position < 5 ? 0 : (position < thumbscount - 4 ? position - 5 : thumbscount - 5);
			
		thumbslist.morph('left: ' + ( -27 * position) + 'px');
		
		if (isInfoGallery){
			location.hash = 'mainimage';
		}
	}
	
	function nextImage(direction, fallback){
		var selected = thumbslist.down('a.selected');
					
		selectImage(((selected && selected.up('li')[direction]('li')) || thumbslist.select('li')[fallback]()).down('a'));
	}
	
	return {
		'div.controls:click': {
			'#thumbslist a': function(e){
				e.stop();
				selectImage(this);
			},
			'#preva': nextImage.curry('previous', 'last'),
			'#nexta': nextImage.curry('next', 'first')
		}
	};
});
						
if ($('google_out')){
	google.load("maps", "2.x");
	google.setOnLoadCallback(function(){
		var map = new google.maps.Map2(document.getElementById("maps"));
		map.setCenter(new google.maps.LatLng(42.660064, 23.404559), 15);
		map.addOverlay(new GMarker(new GLatLng(42.659907,23.403363), {draggable: false, clickable: false}));
		map.setUIToDefault();
	}); 
}
