// JavaScript Core Document

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

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 hide_notice(){
	if ($('hide_notice')){
		$('hide_notice').addEvent('click', function(e){
			var myFx = new Fx.Tween($('notice_holder'), {duration: 2000});
			myFx.start('opacity', '1', '0');
			var e= new Event(e);
			e.stop();
		});
	}
}
function sliders(){
	var sliders = $$('.bf_line');
	sliders.each(function(el){
		var steps = el.getProperty('rel');
		var target = el.getElement('input');
		var slider = new Slider(el, el.getElement('.bf_knob'), {
			steps: steps,
			wheel: true,
			range: [1],	// Minimum value is 8
			onChange: function(step){
				// Everytime the value changes, we change the font of an element
				el.getElement('.bf_tip').removeClass('hide');
				target.value = step;
				if ($(el.id + '_' + step) && !isNaN(step)){
					el.getElement('.bf_tip').set('html', $(el.id + '_' + step).innerHTML);
					// alert ($(el.id + '_' + step).innerHTML);
				}
			}
		});
		el.getElement('.bf_tip').addClass('hide');
		slider.set(1);
	});
	/*
	var tipz = new Tips('.bf_knob',{
		className: 'tipz',
		fixed: true,
		hideDelay: 250,
		showDelay: 50
	});
	*/
}

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;
	*/
}

function tweet(){
	if ($('tweets-here')){
		var req = new Request.HTML({url:'/feeds.php',
			onSuccess: function(html) {
				//Clear the text currently inside the results div.
				//Inject the new DOM elements into the results div.
				$('tweets-here').adopt(html);
				$('tweets-here').removeClass('loading');
				if (html != 'Oops, the Tweet feeder is empty!'){
					bouncer = bouncescroll.periodical(6000);
					var bouncer;
				}
			}
		}).send();
	}
}

function flash(){
	var flash = $$('.flash');
	flash.each(function(el){
		var fl = new Swiff(el.getProperty('rel'), {width: el.getStyle('width'), height: el.getStyle('height'), container: el});
	});
}

function extras(){
	if ($('wordleMap')){
		wordleMap();
	}
}
function wordleMap(){
	var alerts = $$('#wordleMap area');
	alerts.each(function(el){
		el.set('title', el.get('alt'));
		if (el.getProperty('rel') != null){
			el.addEvent('click', function(e){
				alert (el.getProperty('rel'));
				var e = new Event(e);
				e.stop();
			});
		};
	 });
}
var portfolioFunctionality = function(){
	if ($('portfolioList')){
		$('portfolioList').set('style', 'height: ' + $('portfolioList').getSize().y.toInt() + 'px;');
		var portfolioItems = $$('.portfolio_item');
		portfolioItems.each(function(el){
			el.show = true;
			el.addEvent('click', function(e){
				var target = $('portfolioPreviewArea');
				var myHTMLRequest = new Request.HTML({
					url: '/gallery-demo/projectUrl/'+el.getProperty('rel'),
					update: $('portfolioPreviewArea'),
					onComplete: function(){
						portfolioItems.each(function(el2){
							el2.removeClass('active');
							var myFx = new Fx.Scroll(window).toElement($('header'));
						});
						el.addClass('active');
						var p = new Element('p', {'html': 'continue to browse &raquo;', 'style': 'cursor: pointer'}).inject($('projectSummary'), 'bottom').addEvent('click', function(){
							var myFx = new Fx.Scroll(window).toElement(el);
						});
					}
				}).send();
				var e = new Event(e).stop();
			});
		});
		var filters = $$('#filters a');
		filters.each(function(el){
			el.addEvent('click', function(e){
				filters.each(function(el2){
					el2.getParent().removeClass('active');
				});
				el.getParent().addClass('active');
				portfolioItems.each(function(el2){
					if(el2.hasClass(el.rel) || (el.rel == 'section_0')){
						if (!el2.show){
							el2.myFx = new Fx.Morph(el2, {'duration': 300});
							el2.myFx.start({'width': 291, 'margin-right': 6, 'opacity': 1});
						}
						el2.show = true;
					}else{
						if (el2.show){
							el2.myFx = new Fx.Morph(el2, {'duration': 300});
							el2.myFx.start({'width': 0, 'opacity': 0, 'margin-right': 0});
						}
						el2.show = false;
					}
				});
				var e = new Event(e).stop();
			});
		});
	}
}
var tooltips = function(){
	var myTips = new Tips('.tooltip', {
		className: 'tooltip-container',
		offset: {'x': -45, 'y': 10},
        onShow: function(tip, el){
            tip.setStyles({
                visibility: 'hidden',
                display: 'block'
            }).fade('in');
        }
    });
}
var autoSlimbox = function(){
	var maxWidth = 700, maxHeight = 600;
	$$('img').each(function(el){
		if (el.naturalWidth > el.width || el.naturalHeight > el.height){
			if (el.getParent().nodeName != 'A'){
				el.alt = el.alt ? el.alt : 'No ALT tag is set.';
				var a = new Element('a', {'rel': 'lightbox[]', 'href': el.src, 'title': el.alt}).wraps(el);
				if (el.naturalWidth > maxWidth || el.naturalHeight > maxHeight){
					// Do physical resize				
					// alert ('resizing ' + el.src);
					var myRequest = new Request({'url': '/_lib/js/resize_img.php'}).send("url=" + el.src);
				}
			}
		}
	});
}

window.addEvent('domready', function(){
	external();
	forms();
	tweet();
	flash();
	// sliders();
	hide_notice();
	extras();
	
	portfolioFunctionality();
	tooltips();
	autoSlimbox();

/*
var csize = $('container_bottom').getSize();
var cheight = csize.y;
var remainder = cheight % 33;
var paddingtop = $('container_bottom').getStyle('padding-top').toInt();
var paddingbottom = $('container_bottom').getStyle('padding-bottom').toInt();
var newheight = cheight+remainder-paddingtop-paddingbottom;
$('container_bottom').setStyle('height',newheight);
//console.log(remainder);
//alert("oh: "+cheight+"\nrem: "+remainder+"\npt: "+paddingtop+"\npb: "+paddingbottom+"\nh: "+newheight);
*/


});
