// JavaScript Core Document

function external(){
	var l = $$('a');
	l.each(function(el){
		if (el.getProperty('rel')=='external'){
			el.setProperty('target', '_blank');
		}
	});
}

function pdf_openers(){
	var l = $$('#downloads li a');
	l.each(function(el){
		el.set('target', '_blank');
	});
}

function slideshows(){
	/* settings */
	var logged = $$('.editor');
	if (logged.length > 0) return false;
	var slideshows = $$('.slideshow');
	slideshows.each(function(container){
		var images = container.getElements('img');
		if (container.getProperty('rel')){
			var showDuration = container.getProperty('rel') * 1;
		}else{
			var showDuration = 4000;
		}
		var currentIndex = 0;
		var interval;
		/* opacity and fade */
		images.each(function(img,i){ 
			if(i > 0) {
				img.set('opacity', 0);
			}
		});
		/* worker */
		var show = function() {
			images[currentIndex].fade('out');
			images[currentIndex = currentIndex < images.length - 1 ? currentIndex+1 : 0].fade('in');
		};
		/* start once the page is finished loading */
		window.addEvent('load',function(){
			interval = show.periodical(showDuration);
		});
	});
}

function bouncescroll(){
	var l;
	var bouncescrollers = $('tweets-here');
	var first = bouncescrollers.getFirst();
	var fx1 = new Fx.Tween(bouncescrollers,{transition: Fx.Transitions.Quad.easeOut, duration: 500});
	bouncescrollers.setStyle('margin-left','722px');	
	var pop = first.dispose();
	pop.inject(bouncescrollers);
	fx1.start('margin-left', '0px');
	first = bouncescrollers.getFirst().get('html');
	l = first.length;
	switch (l){
		case l<100:
			counter = 4000;
			break;
		case l>=200:
			counter = 6000;
			break;
		default:
			counter = 5000;
			break;
	}
	$clear(bouncer);
	bouncer = bouncescroll.periodical(counter);
}

function tweet(){
	if ($('tweets-here')){
		$('tweets-here').set('html','');
		//get information
		var myTwitterGitter = new TwitterGitter('Wordyfitt',{
			count: 5,
			onComplete: function(tweets,user) {
					tweets.each(function(tweet,i) {
						new Element('p',{
							html: tweet.text + ' ( <a href="http://twitter.com/Wordyfitt" rel="external">' + user.name + '</a></span> )',
							//html: '<img src="' + user.profile_image_url.replace("\\",'') + '" align="left" alt="' + user.name + '" /> <strong>' + user.name + '</strong><br />' + tweet.text + '<br /><span>' + tweet.created_at + ' via ' + tweet.source.replace("\\",'') + '</span>',
							'class': 'tweet clear'
					}).inject('tweets-here');
					//var obj = new mooquee($('tweets-here'), {direction: 'left'});
				});
			}
		}).retrieve();
		bouncer = bouncescroll.periodical(4000);
	}
}

function flash(){
	var movies = $$('.flash');
    movies.each(function(el){
		var container = el.id;
		var id = el.id;
		var target = el.get('rel');
		var dims = el.getSize();
		var width = dims.x;
		var height = dims.y;
		var obj = new Swiff('/_lib/flash/' + target + '.swf', {id: id, width: width, height: height, container: container});
	});
}
function forms(){
	var s = $$('form #sort');
	s.each(function(el){
		el.addEvent('change', function(e){
			if (el.value != 'Please select'){
				el.getParent().getParent().submit();
			}
		});
	});
}
function _getmap(){
	var map_exists = $('gmap');
	if (map_exists){
		window.addEvent('load',function(){
			load('gmap');
		});	
		window.addEvent('unload',function(){
			GUnload();
		});	
	}
}

function section_overs(){
	if ($('section_list')){
		var overs = $$('#section_list a');
		overs.each(function(el){
			var allImgs = $$('.project_img img');
			var targets = $$('.' + el.getProperty('rel') + ' .project_img img');
			el.addEvent('mouseover', function(e){
				allImgs.each(function(el2){
					el2.set('style', 'opacity: 0.3');
				});
				targets.each(function(el2){
					el2.set('style', 'opacity: 1');
				});
			});
			el.addEvent('mouseout', function(e){
				allImgs.each(function(el2){
					el2.set('style', 'opacity: 1');
				});
			});
		});
	}
}
function brochure_popups(){
	var links = $$('.brochure_link');
	links.each(function(el){
		el.addEvent('click', function(e){
			window.open(el.href, 'brochure', 'width=900, height=700');
			var e = new Event(e);
			e.stop();
		});
	});
}
window.addEvent('domready', function(){
	external();
	forms();
	flash();
	_getmap();
	tweet();
	slideshows();
	pdf_openers();
	section_overs();
	brochure_popups();
});