// 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(id){
	if ($('tweets-here')){
		$('tweets-here').set('html','');
		//get information
		var myTwitterGitter = new TwitterGitter(id,{
			count: 5,
			onComplete: function(tweets,user) {
					tweets.each(function(tweet,i) {
						new Element('p',{
							html: tweet.text + ' ( <a href="http://twitter.com/' + id + '" 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(target, {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();
			}
		});
	});
	$('children_fm').addClass('hide');
	$('childs').addEvent('change', function(e){
		if ($('childs').value >= 1){
			$('children_fm').removeClass('hide');
			$$('#children_fm p').each(function(item, i){
				if (i < $('childs').value){
					item.removeClass('hide');
				}else{
					item.addClass('hide');
					var selectF = item.getElement('select');
					selectF.options[0].setProperty('selected', 'selected');
				}
			});
		}else{
			$('children_fm').addClass('hide');
			$$('#children_fm select').each(function(item, i){
				item.options[0].setProperty('selected', 'selected');
			});
		}
	});
}
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 dl_links(){
	var lks = $$('.dl_link');
	lks.each(function(el){
		el.addEvent('click', function(e){
			el.addClass('downloaded');
		});
	});
}

function scrollers(){
	if ($('news_scroller')){
		var acclinks = $$('h3.atStart');
		acclinks.each(function(el){
			el.addEvent('mouseenter', function(){
				el.addClass('activeacc');
			});
			el.addEvent('mouseleave', function(){
				el.removeClass('activeacc');
			});
		});
	
		var lastclicked;
		var previousitem;
	
		var accordion = new Accordion('h3.atStart', 'div.atStart', {
			opacity: false,
			alwaysHide: true,
			show: -1,
			duration: 500,
			onActive: function(toggler, element){
				acclinks.removeClass('activeacc');
				toggler.addClass('activeacc2');
				previousitem = lastclicked;
				lastclicked = toggler;
				if(previousitem){
					previousitem.removeClass('activeacc2');
				}
				//scroll.toElement(lastclicked);
			},
			onComplete: function(toggler, element){
				//alert(this);
				//console.log(lastclicked);
				if(lastclicked.hasClass('activeacc2')){
					//scroll.toElement(lastclicked);
					var elpos = lastclicked.getPosition();
					var pos = {x: 0, y: elpos.y-100};
					// alert (element.getParent().id);
					var myFx = new Fx.Scroll(window).toElement(element.getParent().id);
					// scroll.scrollTo(0,elpos.y-50);
				};
				//alert("remove class from"+element);
				//lastclicked = "";
				//scroll.toElement(toggler);
			},
			onBackground: function(toggler, element){
				//toggler.setStyle('background', '#222');
				//toggler.removeClass('activeacc2');
			}
		}, $('acc'));
	}
}

/*
function hideBlocks(){
	if ($('topBlock')){
		var myFx = new Fx.Tween($('topBlock'), {onComplete: function(){$('topBlock').destroy();}}).start('opacity', 1, 0);
		var myFx2 = new Fx.Tween($('cover'), {onComplete: function(){$('cover').destroy();}}).start('opacity', 0.8, 0);
	}
}

function appear(tar){
	var content = $(tar).get('html');
	var p = new Element('p').set('class', 'closer');
	var cl = new Element('a', {href: '#', html: 'close'}).inject(p);
	var co = new Element('div', {id: 'topBlock', style: 'opacity: 0;  filter: alpha(opacity = 0);'}).set('html', content).inject($('container'), 'before');
	var id = tar.substr(9);
	var lge_i = $('image_' + id);
	
	p.inject(co, 'top');
	var myFx = new Fx.Tween(co).set('opacity', 0).start('opacity', 0, 1);
	var imgs = co.getElements('img');
	for(a=1; a<imgs.length; a++){
		imgs[a].addEvent('click', function(e){
			lgeSrc = this.get('rel');
			thmbSrc = lge_i.get('rel');
			
			this.set('src', '/userfiles/Image/projects/thumbs/' + thmbSrc);
			lge_i.set('src', '/userfiles/Image/projects/' + lgeSrc);
			
			this.set('rel', thmbSrc);
			lge_i.set('rel', lgeSrc);
			
		});
	}
	
	
	cl.addEvent('click', function(e){
		hideBlocks();
		var e = new Event(e).stop();
	});
}

function blocks(){
	var links = $$('.block');
	links.each(function(el){
		el.addEvent('click', function(e){
			var tar = this.get('rel');
			if (!$('cover')){
				var c = new Element('div', {id: 'cover'});
				c.set('style', 'opacity: 0; filter: alpha(opacity = 0); cursor: pointer;');
				c.inject($('container'), 'before');
				var myFx = new Fx.Tween(c, {onComplete: function(){appear(tar);}}).start('opacity', 0, 0.8);
				c.addEvent('click', function(){
					hideBlocks();
				});
			}else{
				appear(tar);
			}
			var e = new Event(e).stop();
		});
	});
}
*/


function cover(){
	if (!$('cover')){
		var c = new Element('div', {id: 'cover'});																					  
		var myFx = new Fx.Tween(c).set('opacity', 0);
		c.inject('container', 'before');
		myFx.start('opacity', 1);
		$('cover').addEvent('click', function(e){
			cover();
			bounceOut()
		});
	}else{
		c = $('cover');
		var myFx = new Fx.Tween(c, {onComplete: function(){$('cover').destroy(); }}).set('opacity', 0);
		myFx.start('opacity', 0);
	}
}
function bounceIn(t){
	if (t == 'who_we_are'){
		section = 'who_we_are';
		tar = '';
	}else if ($('interpretation')){
		section = 'interpretation';
		var tar = t.get('rel');
	}else{
		section = '';
		var tar = t.get('rel');
	}
	
	//$('bouncer').get('send', {url: 'deletecontent.php', method: 'get'});
	//$('bouncer').load('deletecontent.php?r=<?php rand(0,500); ?>');
	if (!$('bouncer')){
		var b = new Element('div', {id: 'bouncer'});
		b.inject('container', 'before');
		var myRequest = new Request.HTML({url: '/project_info.php', method: 'get', update: 'bouncer', onComplete: function(){
			if ($('CloseBtn')){
				$('CloseBtn').addEvent('click', function(e){
					bounceOut();
					cover();
				});
			}
			
			var imgs = $('bouncer').getElements('img');
			var lge_i = $('largeImage');
			
			for(a=1; a<imgs.length; a++){
				imgs[a].addEvent('click', function(e){
					lgeSrc = this.get('rel');
					thmbSrc = lge_i.get('rel');
					
					lge_i.set('opacity', 0);
					lge_i.tween('opacity', 1);
					
					this.set('src', '/userfiles/Image/projects/thumbs/' + thmbSrc);
					lge_i.set('src', '/userfiles/Image/projects/' + lgeSrc);
					
					this.set('rel', thmbSrc);
					lge_i.set('rel', lgeSrc);
					
					
				});
			}
		}}).get({p: tar, s: section});
																					  
		$('bouncer').set('tween', {transition: Fx.Transitions.Bounce.easeOut, duration: 'long'});
		$('bouncer').tween('top', 20);
		
	}
}
function bounceOut(){
	if ($('bouncer')){
		$('bouncer').set('tween', {transition: Fx.Transitions.Bounce.easeOut, duration: 'long', onComplete: function(){$('bouncer').destroy(); }});
		$('bouncer').tween('top', -700);
	}
}
window.addEvent('load', function(){
	var popups = $$('.bouncer');
	popups.each(function(el){
		el.addEvent('click', function(e){
			cover();
			bounceIn(el);
			var e = new Event(e).stop();
		});
	});
});



function who_we_are(){
	var as = $$('a');
	as.each(function(el){
		if (el.get('href') == '/profile/who-we-are/'){
			el.addEvent('click', function(e){
				cover();
				bounceIn('who_we_are');
				var e = new Event(e).stop();
			});
		}
	});
}

function datePickers(){
	var d = new Date();
	da = d.getDate() + 1;
	mo = d.getMonth();
	ye = d.getFullYear();
	
	fullDate = ye + '-' + (mo < 10 ? '0' + mo : mo) + '-' + (da < 10 ? '0' + da : da);
	new DatePicker('.datePicker', {inputOutputFormat: 'Y-m-d', allowEmpty: true, minDate: {date: fullDate, format: 'Y-m-d'}});
}
function ccoptinForm(){
	var optInInputBox = $$('input.ea');
	if (optInInputBox.length){
		optInInputBox.each(function(el){
			el.initVal = el.value;
			el.addEvent('focus', function(e){
				if (el.value == el.initVal){
					el.value = '';	
				}else{
					el.select();
				}
			});
			el.addEvent('blur', function(e){
				if (el.value == ''){
					el.value = el.initVal;
				}
			});
		});
	}
}

window.addEvent('domready', function(){
	external();
	forms();
	flash();
	_getmap();
	tweet();
	slideshows();
	pdf_openers();
	section_overs();
	dl_links();
	scrollers();
	datePickers();
	if ($('caterBookForm')){
		$('caterBookForm').setProperty('target', '_blank');
	}
	ccoptinForm();
	// blocks();
});
