/**
 * Array.implement: David Walsh, http://davidwalsh.name/array-shuffling-mootools
 */
Array.implement({
	shuffle: function() {
		//destination array
		for(var j, x, i = this.length; i; j = parseInt(Math.random() * i), x = this[--i], this[i] = this[j], this[j] = x);
		return this;
	}
});

//prevent IE to flicker
document.write('<style type="text/css">.random.default {display: none}</style>');

//TOP BANNER
jpBanner = new Jp_AttractMode({
	closeLabel: 'Close',
	openLabel: 'Open',
	duration: 500,
	elemenToHide: 'data' //a css rule will be written to hide elements with this class while the DOM loads
});
//for a page to start with the banner closed, use the following in the header:
//jpBanner.addOptions({
//	startClosed: true
//});

//SERVICES MENU
jpTab = new Jp_Tab({
	duration: 125, //duration for fading out, and then fading in (total effect duration is then duration * 2)
	defaultTab: 'what-we-do', //default copy, hidden when selecting a service
	classToHide: 'hide-at-start' //a css rule will be written to hide elements with this class while the DOM loads
});

//CLIENTS QUOTES
jpQuotes = new Jp_Tab({
	delay: 8000,
	duration: 250, //duration for fading out, and then fading in (total effect duration is then duration * 2)
	defaultTab: 'what-we-do', //default copy, hidden when selecting a service
	classToHide: 'hide-at-start', //a css rule will be written to hide elements with this class while the DOM loads
	show: 0,
	loop: true,
	randomize: true,
	resize: false
});

//CASE STUDIES SLIDE SHOW
jpSlideshow = new Jp_Slideshow({
	maxspeed: 100, // pixels to move with each iteration
	numberOfItems: 4,
	classToHide: 'hide-at-start', //a css rule will be written to hide elements with this class while the DOM loads
	classToRemove: 'no-margin'
});



window.addEvent('domready', function() {
	//BANNER QUOTES
	new Jp_Showcase($$('#attract-mode blockquote', '#attract-mode div.attract-text'), {
		delay: 8000, //time the case will be shown
		duration: 375, //effect duration
		classToHide: 'hide-at-start',
		fadeImageOnly: false,
		startFadingSimultaneously: false,
		randomize: true
	});

	//SHOWCASE
	new Jp_Showcase('showcase', {
		delay: 6000, //time the case will be shown
		duration: 750, //effect duration
		linkToUpdate: 'showcase-link'
	});

	//SMOOTH SCROLL
	new SmoothScroll({
		links: '.smoothAnchors',
		duration: 250
	});

	//ACCORDIONS
	var triggers = $$('.accordion > h5');
	var data = $$('.accordion > .accordion-data');
	if (triggers.length && data.length) {
		new Accordion(triggers, data, {
			show: -1,
			duration: 150,
			alwaysHide: true,  // allow all sections to be hidden at once
			onActive: function(h) {
				h.removeClass('closed');
			},
			onBackground: function(h) {
				h.addClass('closed');
			}
		});
	}


	//randomiser
	new ElementRandomiser($$('.random'));

	//subscribe form
	new jpForm('subscribe', {
		valuesToConsiderNull: ['name', 'email'],
		ajax: true
	});

	//contact form
	new jpForm('enquiry', {
		valuesToConsiderNull: ['Name*', 'Company', 'Telephone Number*', 'Email Address*', 'Enquiry'],
		ajax: true
	});

	//start top banner
	var trigger = $$('.toggle a');
	var banner = $$('.data');
	if (banner.length && trigger.length) {
		jpBanner.start(trigger[0], $$('.data')[0]);
	}

	//start services menu
	var servicesUl = $$('.service-list ul');
	if (servicesUl.length) {
		jpTab.start(servicesUl[0]);
	}

	//start clients quotes
	var clientsQuotes = $$('.client-list li a');
	if (clientsQuotes.length) {
		jpQuotes.start(clientsQuotes);
	}

	//start case studies slide show
	var thumbs = $$('.jp-slideshow');
	var navigation = $$('.project-pagination ul');
	if (thumbs.length && navigation.length) {
		jpSlideshow.start(thumbs[0], navigation[0]);
	}

	//slimbox autoload
	/*
	Slimbox.scanPage = function() {
		$$(document.links).filter(function(el) {
			return el.rel && el.rel.test(/^lightbox/i);
		}).slimbox({
			closeKeys: [27, 88], //"c" doesn't close the lightbox (copy: ctrl + c, firebug: ctrl + shift + c)
			captionAnimationDuration: 0,
			imageY: '55px'
		}, null, function(el) {
			return (this == el) || ((this.rel.length > 8) && (this.rel == el.rel));
		});
	};
	Slimbox.scanPage();
	*/
});

var ElementRandomiser = new Class({
	initialize: function(elements) {
		var elementToDisplay = $random(0, elements.length - 1);
		for (var i = 0; i < elements.length; i++ ) {
			if (i == elementToDisplay) {
				$(elements[i]).setStyle('display', 'block');
			}
			else {
				$(elements[i]).setStyle('display', 'none');
			}
		}
	}
});
