var index = 0;

var Site = {
	
	start: function()
	{
		var carousel = ($('carousel-list'));
		if(carousel)
		{
			sized = false;
			images = $$('#carousel-list img');
			items =  $$('#carousel-list li');
			
			
			images.each(function(element, i)
			{
				items[i].hide();
				image = new Asset.image(element.src, 
				{
					'onload' :function()
					{
						if(!sized)
						{
							
							var morph = new Fx.Morph('carousel', {duration: 'long', transition: Fx.Transitions.Sine.easeOut});
							
							height = '315px';
							if( carousel.hasClass('v2'))
							{
								height = '285px';
							}
							
							
							morph.start({
							    'height': height,
							    'border': '0',
							    'background-color': '#FFF'
							});
							
							$('loadingbox').hide();
							sized = true;
							items[i].show();
							
							pcarousel();
							
						}
						
					},
					
					'onerror' : function(){
					   if("function" === $type(error)){
					                        error(i, imgs[i], imgs);
					                    }
					                }
				});


			});
		}
		
		lbxs = $$('.lightbox');
		if(lbxs.length > 0)
		{
			var slimbox_js = new Asset.javascript('/assets/default/scripts/library/slimbox.js',
			{
				id: 'slimbox-js'
			});
			
			var slimbox_css = new Asset.css('/assets/default/styles/library/slimbox/slimbox.css', 
			{
				id: 'slimbox-css',
				title: 'slimbox CSS'
			});
		}
		
		datepickers = $$('.datepicker');
		if(datepickers.length > 0)
		{
			var calendar_js = new Asset.javascript('/assets/default/scripts/library/calendar.js',
			{
				id: 'calendar-js',
				onload:function()
				{
					datepickers.each(function(element, index)
					{
						new Calendar({ id: element.id, format: 'd/m/Y' }, {direction: 1 }); 
					});
				}
			});
			
			var calendar_css = new Asset.css('/assets/default/styles/library/calendar/calendar.css', 
			{
				id: 'calendar-css',
				title: 'calendar CSS'
			});
			
			
			
		}
		
		var formcheck = null;
		
		buttons = $$('.moveform');
		if(buttons.length > 0)
		{
			buttons.each(function(element, index)
			{
				element.addEvent('click',function()
				{
					curelements = $$('#' + element.get('current') + ' .required');
					
					errors = false;
					curelements.each(function(el,i)
					{
						if(el.value.length == 0)
						{
							el.addClass('warning');
							errors = true;
						}
						else
						{
							el.removeClass('warning');
						}
					});
					
					if(!errors)
					{
						if(element.id == 'details_complete')
						{
							if($('business').value == 'No')
							{
								$$('fieldset').addClass('hide');
								$(element.get('or')).removeClass('hide');
							}
							else
							{
								$$('fieldset').addClass('hide');
								$(element.get('element')).removeClass('hide');
							}
						}
						else
						{
							$$('fieldset').addClass('hide');
							$(element.get('element')).removeClass('hide');
						}
					}

					
				});
			})
		}
		
		pitems = $$('.portfolioitem');
		
		if(pitems.length > 0)
		{
			
			
			pitems.each(function(el,i)
			{
				hover = $('portfolio-hover-' + el.get('pid'));
				var fx = new Fx.Tween(hover);
				el.startevent = fx;
				
				el.addEvent('mouseenter',function(event)
				{
					el.startevent.cancel();						
					el.startevent.start('top', '0');
				});
				
				el.addEvent('mouseleave',function(event)
				{
					el.startevent.cancel();
					el.startevent.start('top', '375px');
				});
			})
		}
	}

};

Carousel = function()
{
	imgs = $$('#carousel-list li');
	count = imgs.length;
	
	var fx = new Fx.Tween(imgs[index])
	fx.start('opacity', 0).chain(function()
	{
		this.set('display','none');
		this.callChain();
	}).chain(function()
	{
		index++;
		if(index >= count)
		{
			index = 0;
		}
		
		var fx2 = new Fx.Tween(imgs[index]);
		fx2.set('opacity',0).chain(function()
		{
			this.set('display','');	
			this.callChain();
		}).chain(function()
		{
			this.start('opacity', 1);
		});
		fx2.callChain();
	});
	
}

pcarousel = Carousel.create(
{
	periodical: 5000
});

window.addEvent('domready', function(){
	Site.start();
	
	if(Browser.ie6)
	{
		$('twitterfeed').setStyle('display','none');
		$('offer').setStyle('display','none');
	}
	
	if(window.innerWidth < 1100)
	{
		$('twitterfeed').setStyle('display','none');
	}
	
	if(window.innerHeight < 750)
	{
		$('offer').setStyle('display','none');
	}
});

