/* html text fade in */
var GCITickerTape = new Class({
	Implements:[Options],
	options:{
		duration:2500,
		transition:Fx.Transitions.Sine.easeInOut,
		fps:60,
		delay:500
	},
	initialize:function(options) {
		this.setOptions(options);
		this.createInterface();
		return this;
	},
	createInterface:function() {
		this.tickertape = document.id('gcitickertape');
		this.slider = this.tickertape.getElement('a');
		this.slider.set({
			styles:{left:-this.tickertape.getSize().x},
			tween:{duration:this.options.duration,fps:this.options.fps,transition:this.options.transition}						
		});
		this.tickertape.setStyle('visibility','visible');
	},
	slide:function(){
		(function() {this.slider.tween('left',0);}.bind(this)).delay(this.options.delay);
	}
});
/* Cross Fade Portfolio with enlarge options.  */
var Portfolio4 = new Class({

	Implements: [Events, Options],

	options: {
		//onError:$empty
		//onLoad:$empty
		debug:false,
		images:'gcislideshow',
		controls:'gcicontrols',
		selected:0.9,
		rest:0.6,
		delay:3000,
		duration:800,
		transition:Fx.Transitions.Sine.easeIn,
		fps:60
	},

	initialize: function(options){
		this.setOptions(options);
		if (this.options.debug) this.checkElements();
		this.createInterface();
		this.play();
	},
	
	checkElements:function(){
		if (document.id(this.options.images).getElements('img').length < 1) this.fireEvent('onError','Portfolio4 - No image elements defined');
		if (document.id(this.options.controls).getElements('li').length<1) this.fireEvent('onError','Portfolio4 - No control elements defined');
	},
	
	createInterface:function() {
		this.slideshow = document.id(this.options.images);
		this.images = this.slideshow.getElements('img');
		this.controls = document.id(this.options.controls);
		this.number = 0;
		this.large = false;
				
		this.slideshow.addEvents({
			mouseenter:function(){this.large ? this.controls.setStyle('visibility','visible') : this.view.setStyle('display','block');}.bind(this),
			mouseleave:function() {this.large ? this.controls.setStyle('visibility','hidden') : this.view.setStyle('display','none');}.bind(this),
			click:function(event) {event.stop();if (!this.large) this.grow();}.bind(this)
		});
		
		this.images.each(function(e,i){
			if (e.hasClass('start')) this.number = i;
			e.set({
				styles:{opacity:e.hasClass('start') ? 1 : 0},
				tween:{duration:this.options.duration,fps:this.options.fps,transition:this.options.transition}
			});
		}.bind(this));
		
		this.controls.getElement('li.next').addEvent('click',function(event){event.stop();clearInterval(this.timer);this.increment();this.play();}.bind(this));
		this.controls.getElement('li.prev').addEvent('click',function(event){event.stop();clearInterval(this.timer);this.decrement();this.play();}.bind(this));
		this.mask = new Mask(document.body,{hideOnClick:false,style:{opacity:.8,backgroundColor:'black','z-index':8000}});
		this.wrap = new Element('div',{'class':'gcisswrap',styles:{display:'none',position:'absolute',top:'50%',left:'50%','z-index':9000}}).inject(document.body);
		this.close = new Element('a',{'class':'gciclose',events:{click:function(event){event.stop();this.shrink();}.bind(this)}}).inject(this.wrap);
		this.view = new Element('div',{'class':'gcigrow',styles:{position:'absolute',display:'none'},events:{click:function(event){event.stop();if (!this.large) this.grow();}.bind(this)}}).inject(this.slideshow);
	},
	
	grow:function() {
		this.large = true;
		this.stop();
		this.view.setStyle('display','none');
		this.slideshow.inject(this.wrap);
		this.mask.show();
		this.wrap.setStyle('display','block');
		this.play();
	},
	
	shrink:function() {
		this.large = false;
		this.stop();
		this.controls.setStyle('visibility','hidden');
		this.wrap.setStyle('display','none');
		this.slideshow.inject(document.id('gcislideshowwrap'));
		this.mask.hide();
		this.play();
	},
	
	increment:function(){this.selectItem(this.number == this.images.length-1 ? 0 : this.number+1);},	
	decrement:function() {this.selectItem(this.number == 0 ? this.images.length-1 : this.number-1);},
	
	selectItem:function(selection) {
		this.images[this.number].tween('opacity',0);
		this.number = selection;
		this.images[this.number].setStyle('display','block').tween('opacity',1);
	},
	
	stop:function() {clearInterval(this.timer);},
	play:function(){clearInterval(this.timer);this.timer = this.increment.periodical(this.options.delay,this);}
});

var myForm = new Class({
	Implements:[Events,Options],
	options:{
		delay:2500
	},
	initialize:function(form,type,options) {
		this.setOptions(options);
		this.form = document.id(form).setStyle('z-index',900);
		this.type = type;
		if (type == 'rental') {
			this.name = this.form.getElement('p.cottagename');
			this.close = this.form.getElement('a.gciformclose').addEvent('click',function(event){event.stop();this.reset();this.hide();}.bind(this));
			this.mask = new Mask(document.body,{hideOnClick:false,style:{opacity:.8,backgroundColor:'#000','z-index':800}});
		} else {
			document.id('contactreset').addEvent('click',function(event){this.reset();}.bind(this));			
		}
		this.tye = this.form.getElement('div.gcithankyou').setStyles({display:'none',opacity:0.9});
		this.validator = new Form.Validator.Inline(this.form,{useTitles:true,errorPrefix:'',warningPrefix:''});
		if (type == 'rental') this.reset();
		this.submitform = new Form.Request(this.form,null,{
			noCache:true,
			extraData:{'formtype':this.type},
			onSend:function() {this.tye.setStyle('display','block').getElement('p.gcisend').setStyle('visibility','visible');}.bind(this),
			onSuccess:function() {this.success();}.bind(this),
			onFailure:function() {this.failure();}.bind(this)
		});		
		return this;
	},
	
	setid:function(el,value) {
		this.id = value;
		this.relElement = el;
		if (this.type == 'rental') {
			this.name.set('html',value);
			this.form.getElement('input.id').setProperty('value',value);
		}
		return this;
	},
	
	getid:function() {return this.id;},
	
	show:function() {
		this.form.setStyle('display','block');
		var relpos = this.relElement.getPosition();
		if (this.type == 'rental')
			this.form.setPosition({x:relpos.x-this.form.getSize().x-10,y:relpos.y-this.form.getSize().y});
		else
			this.form.setPosition({x:relpos.x,y:relpos.y-this.form.getSize().y});
		this.mask.show();
		return this;
	},
	
	hide:function() {
		if (this.type == 'rental') this.form.setStyle('display','none');
		this.reset();
		this.tye.setStyle('display','none').getElements('div').each(function(e){e.setStyle('display','none');});
		if (this.mask) this.mask.hide();
		return this;
	},
	
	reset:function() {
		this.id = null;
		if (this.type == 'rental') this.name.set('html','');
		this.form.getElements('input').each(function(e){
			if (e.getProperty('type') == 'text') e.setProperty('value','');
			if (e.getProperty('type') == 'checkbox') e.setProperty('checked',false);
		});
		this.form.getElements('select').each(function(e){e.setProperty('value','');});
		this.validator.reset();		
	},
	success:function() {
		this.tye.getElement('p.gcisend').setStyle('visibility','hidden');
		this.tye.getElement('div.gcisuccess').setStyle('display','block');
		this.hide.delay(this.options.delay,this);
	},
	
	failure:function() {
		this.tye.getElement('p.gcisend').setStyle('visibility','hidden');
		this.tye.getElement('div.gcifailure').setStyle('display','block');
		this.hide.delay(this.options.delay,this);
	}
});

var PageLoader = new Class({
	initialize:function() {
		window.addEvents({
			domready:function() {
				if (document.id('gcitickertape')) var mytickertape = new GCITickerTape().slide();
				this.getFormButtons();
				if (document.id('gcislideshow')) new Portfolio4({debug:false});
				if (document.id('rentalform')) this.rentalform = new myForm('rentalform','rental');
				if (document.id('contactform')) new myForm('contactform','contact');
				if (document.id('g7contactform')) new myForm('g7contactform','g7');
			}.bind(this)
		});
	},
	
	getFormButtons:function() {
		var myformbtns = $$('p.formbtn');
		myformbtns.each(function(e){
			e.set({
				styles:{display:'block'},
				events:{click:function(event){event.stop();this.showrentalform(e);}.bind(this)}
			});
		}.bind(this));		
	},
	
	showrentalform:function(el) {
		var myProperty = el.getParent('div.formContainer').getElement('div.hiddenTxt p');
		this.rentalform.setid(el, myProperty.get('html')).show();
	}
});
