window.addEvent('domready', function(){

	whatpplsay_stopTrans = false;

	var Tips1 = new Tips($$('.to_top'));

	// Hyperlink scroll fx
	var scroller = new Fx.Scroll(window, {duration: 900, wait: false, transition: Fx.Transitions.sineInOut});
	$$('a').each(function(el){
		href = el.getAttribute('href').replace(site_url, '');
		href = href.replace('http://dev.blacksheep.net.nz', '');
		if (href.substr(0, 1) == '#'){
			href = href.replace('#', '');
			//alert(href);
			el.addEvent('click', function(e){
				e = new Event(e).stop();
				href = el.getAttribute('href').replace(site_url, '');
				href = href.replace('http://dev.blacksheep.net.nz', '');
				href = href.replace('#', '');
				scroller.toElement($(href));
			});
		}
	});
	$('wrapper').setStyle('height', $('wrapper').getStyle('height').toInt() + (screen.height/3));

	// Free consultation image hover
	$('free_consultation').addEvent('mouseenter', function(){
		$('free_consultation').setAttribute('src', site_url+'component/graphics/free_consultation_hover.png');
	});
	$('free_consultation').addEvent('mouseleave', function(){
		$('free_consultation').setAttribute('src', site_url+'component/graphics/free_consultation.png');
	});
	
	// What People Say transition fx
	whatpplsay_height = 0;
	quote_limit = 0;
	$$('#what_people_say li').each(function(el){
		if (el.getStyle('height').toInt() > whatpplsay_height){
			whatpplsay_height = el.getStyle('height').toInt();
		}
		quote_limit++;
	});
	quote_current = 0;
	list = $('what_people_say').getFirst().getNext().getFirst();
	quoteFx = new Fx.Morph(list, {duration: 500, wait: false, transition: Fx.Transitions.Quad.easeOut});
	quoteContainerFx = new Fx.Morph($('what_people_say').getFirst().getNext(), {duration: 500, wait: false, transition: Fx.Transitions.Quad.easeOut});
	c_li = list.getFirst();
	$('what_people_say').getFirst().getNext().setStyle('height', c_li.getStyle('height'));
	
	function rotate_quotes() {
		if (whatpplsay_stopTrans == false){
			quote_current++;
			if (quote_current >= quote_limit){
				quote_current = 0;
				c_li = list.getFirst();
			} else{
				c_li = c_li.getNext();
			}
			if (quote_current == 0){
				newTop = 0;
			} else{
				newTop = (list.getStyle('top').toInt() - (c_li.getPrevious().getStyle('height').toInt() + 15));
			}
			quoteFx.start({
				'opacity': 0
			}).chain(function(){
				list.setStyle('top', newTop);
				quoteContainerFx.start({
					'height': c_li.getStyle('height').toInt() + 4
				}).chain(function(){
					quoteFx.start({
						'opacity': 1
					});
				});
			});
		}
	}
	rotate_quotes.periodical(7000);
	
	pauseDiv = new Element('div', {
		'id': 'pause',
		'styles': {
			'position': 'absolute',
			'bottom': 0,
			'left': 0,
			'width': $('what_people_say').getStyle('width').toInt() + $('what_people_say').getStyle('padding-right').toInt(),
			'height': 30,
			'overflow': 'hidden',
			'line-height': 30,
			'z-index': 10,
			'background-color': '#a3c8e2',
			'color': 'white',
			'padding-left': $('what_people_say').getStyle('padding-left').toInt()
		}
	});
	coverDiv = new Element('div', {
		'styles': {
			'position': 'absolute',
			'bottom': 0,
			'left': 0,
			'width': $('what_people_say').getStyle('width').toInt() + $('what_people_say').getStyle('padding-left').toInt() + $('what_people_say').getStyle('padding-right').toInt(),
			'height': 40,
			'background-color': '#e50093',
			'z-index': 15
		}
	});
	coverDiv.inject($('what_people_say'));
	pauseDiv.set('text','Paused');
	pauseDiv.inject($('what_people_say'));
	pauseDivFx = new Fx.Morph(pauseDiv, {duration: 500, wait: false, transition: Fx.Transitions.Quad.easeOut});
	
	function pause(){
		coverDiv.setStyle('background-color', '#e50093');
		pauseDivFx.start({
			'bottom': -30
		});
	}
	function unpause(){
		coverDiv.setStyle('background-color', '#e50093');
		pauseDivFx.start({
			'bottom': 0
		});
	}
	
	$('what_people_say').addEvent('mouseenter', function(){
		whatpplsay_stopTrans = true;
		$('what_people_say').setStyle('background-color', '#e50093');
		pause();
	});
	$('what_people_say').addEvent('mouseleave', function(){
		whatpplsay_stopTrans = false;
		$('what_people_say').setStyle('background-color', '#e50093');
		unpause();
	});
	$('what_people_say').getChildren().each(function(el){
		el.addEvent('mouseenter', function(){
			whatpplsay_stopTrans = true;
			$('what_people_say').setStyle('background-color', '#e50093');
			pause();
		});
		el.addEvent('mouseleave', function(){
			whatpplsay_stopTrans = false;
			$('what_people_say').setStyle('background-color', '#e50093');
			unpause();
		});
	});

	// Contact form ajax
	//$('contact_form_container').setStyle('height',$('contact_form_container').getStyle('height'));
	$('contact_form').getElement('div.loading').set('opacity',0.8);
	var cfFx = new Fx.Morph($('contact_form'), {duration: 500, wait: false, transition: Fx.Transitions.Quad.easeOut})
	$('contact_form').setStyle('height',$('contact_form').getStyle('height'));
	$('contact_form').addEvent('submit',function(e){
		e.stop();
		$('contact_form').addClass('loading');
		this.set('send',{
			onComplete: function(html){
				$('contact_form').removeClass('loading');
				var sp = html.split(':::');
				switch(sp[0])
				{
					case 'ERROR':
						if (!$('form-error'))
						{
							var formerror = new Element('div',{
							id: 'form-error'
							}).inject($('contact_form'),'before');
						}
						$('form-error').set('html',sp[1]);
					break;
					
					case 'SUCCESS':
						if ($('form-error'))
						{
							$('form-error').destroy();
						}
						cfFx.start({
							'opacity': 0
						}).chain(function(){
							$('contact_form').set('html',sp[1]);
							cfFx.start({
								'opacity': 1
							});
						});
					break;
				}
			}
		});
		this.send();
	});
	/*var ajax = new formAjax($('contact_form'), $('contact_form_container'), false, true);
	if (window.ie6){
		$('contact').setStyles({
			'position': 'absolute'
		});
		$('right').setStyles({
			'height': $('right').getStyle('height').toInt() + $('contact').getStyle('height').toInt() + 20
		});
	}*/
	

	
/***************************************
/  PNG transparency fix for IE 6
/**************************************/
	if ((window.ie6) && (document.body.filters)) 
	{
		$$('img').each(function(img){
			var imgName = img.src.toUpperCase()
			if (imgName.substring(imgName.length-3, imgName.length) == "PNG")
			{
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='scale');\"></span>" 
				img.outerHTML = strNewHTML
			}
		});
	}
});