/*	GLOBAL FUNCTIONS
----------------------------------------*/
function $T(i,v) { return((typeof(i)=='string'?$(i):(i?i:document)).getElementsByTagName(v)); }	// GET ELEMENTS BY TAG NAME
function trim(s) { return(s.replace(/^\s+|\s+$/g, '')); }

function addEvent( obj, type, fn ) {
	if (obj.addEventListener) {
		obj.addEventListener( type, fn, false );
		EventCache.add(obj, type, fn);
	}
	else if (obj.attachEvent) {
		obj["e"+type+fn] = fn;
		obj[type+fn] = function() { obj["e"+type+fn]( window.event ); } 
		obj.attachEvent( "on"+type, obj[type+fn] );
		EventCache.add(obj, type, fn);
	}
	else {
		obj["on"+type] = obj["e"+type+fn];
	}
}

var EventCache = function(){
	var listEvents = [];
	return {
		listEvents : listEvents,
		add : function(node, sEventName, fHandler){
			listEvents.push(arguments);
		},
		remove : function(node, sEventName, fHandler){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1) {
																																			/*	fHandler removed because it wouldn't match	*/
				if(node == listEvents[i][0] && sEventName == listEvents[i][1] /* && fHandler == listEvents[i][2]*/ ) {
					item = listEvents[i];
					if(item[0].removeEventListener) {
						item[0].removeEventListener(item[1], item[2], item[3]);
					}
					if(item[1].substring(0, 2) != "on") {
						item[1] = "on" + item[1];
					}
					if(item[0].detachEvent) {
						item[0].detachEvent(item[1], item[0][sEventName+fHandler]);					
					}
					item[0][item[1]] = null;
				}
			}
		},	
		flush : function(){
			var i, item;
			for(i = listEvents.length - 1; i >= 0; i = i - 1){
				item = listEvents[i];
				if(item[0].removeEventListener){
					item[0].removeEventListener(item[1], item[2], item[3]);
				};
				if(item[1].substring(0, 2) != "on"){
					item[1] = "on" + item[1];
				};
				if(item[0].detachEvent){
					item[0].detachEvent(item[1], item[2]);
				};
				item[0][item[1]] = null;
			};
		}
	};
}();

function removeEvent( obj, type, fn ) {
	EventCache.remove(obj, type, fn);
}

function backToTop() {
	return do_backToTop();
}

function do_backToTop() {
    var x1 = x2 = x3 = 0;
    var y1 = y2 = y3 = 0;

    if (document.documentElement) {
        x1 = document.documentElement.scrollLeft || 0;
        y1 = document.documentElement.scrollTop || 0;
    }

    if (document.body) {
        x2 = document.body.scrollLeft || 0;
        y2 = document.body.scrollTop || 0;
    }

    x3 = window.scrollX || 0;
    y3 = window.scrollY || 0;

    var x = Math.max(x1, Math.max(x2, x3));
    var y = Math.max(y1, Math.max(y2, y3));

    window.scrollTo(Math.floor(x / 1.2), Math.floor(y / 1.2));

    if (x > 0 || y > 0) {
        window.setTimeout("backToTop()", 25);
    }
}


/*	BLOG SHARING
----------------------------------------*/

function share(parent,className) {
	var sharing = parent.select('div.'+className);
			
	for(var i=0; i<sharing.length; i++) {
		var s=sharing[i];
		//	ACTIVATE HOVER STATE
		s.style.cursor = 'pointer';
		
		//	ACTIVATE PROTOTIP
		var permalink = s.getAttribute('permalink');
		var postname = s.getAttribute('postname');
		var bitlyurl = s.getAttribute('bitlyurl');
		
		if(document.body.id == 'blog') {
			// styles for blog page
			var stem = 'leftMiddle'
			var tip = 'topLeft';
			var target = 'topRight';
			var startBG = 'b2b2b2';
			var startTextColor = 'ffffff';
			var y = -10;
			var x = 0;
		} else {
			// styles for home page		
			var stem = 'topRight';
			var tip = 'topRight';
			var target = 'bottomRight';
			var startBG = '3b3b3b';
			var startTextColor = 'aaaaaa';
			var y = 5;
			var x = 0;
		}
		
		new Tip(s.id,'<div class="icons">'+
									'<a href="mailto:?subject=New Switchyard Article: '+postname+'&body=Check out this article from Switchyard Creative: '+permalink+'" class="email" title="Email To A Friend"><span>Email</span></a>' +
									'<a href="http://www.facebook.com/sharer.php?u='+permalink+'&t='+postname+'" class="facebook" title="Share On Facebook"><span>Facebook</span></a>'+
									'<a href="http://twitter.com/home?status='+postname+' '+bitlyurl+'" class="twitter" title="Tweet this"><span>Twitter</span></a>'+
									'<a href="http://delicious.com/save?v=5&noui&jump=close&url='+encodeURIComponent(permalink)+'&title='+encodeURIComponent(postname)+'" class="delicious" title="Add to delicous"><span>Delicious</span></a>'+
									'<a href="http://digg.com/submit?phase=2&url='+encodeURIComponent(permalink)+'&title='+encodeURIComponent(postname)+'" class="digg" title="Digg this"><span>Digg</span></a>'+
									'<a href="http://reddit.com/submit?url='+encodeURIComponent(permalink)+'" class="reddit" title="Reddit"><span>Reddit</span></a></div>',
			{
			hideOn: false,
			hideAfter: .5,
			stem: stem,
			style: 'protoswitchyard',
			delay: .1,
			hook: { tip: tip, target: target},
			offset: { x: x, y: y }
		});
		
		s.observe('prototip:shown', function(event) {
  		this.morph('background:#ff3333; color:#fff;',{duration: 0.1}); // our target element, we shake it with Scriptaculous
  		this.tip.shake();
		})
		s.observe('prototip:hidden', function(event) {
  		this.morph('background:#'+startBG+'; color:#'+startTextColor+';',{duration: 0.1}); // our target element, we shake it with Scriptaculous
		})
	}
	
	
	/*
	var other_versions = other_versions_bar.getElementsByTagName('a');
	for(var i = 0; i<other_versions.length; i++) {
		var num = (i+1);
		var ratecount = other_versions[i].getAttribute('ratecount');
		var songTitle = other_versions[i].getAttribute('songTitle');
		var rating = other_versions[i].getAttribute('rating');
		new Tip('v'+num,'<div class="songtitle">'+songTitle+'</div> <div class="ratecount">Rated By '+ratecount+'</div>',{ delay: .1, title: '<span class="rate"><span class="r'+rating+'"></span></span>', className: 'protoblue', stem: 'topLeft'});
		$('v'+num).observe('prototip:shown', function() {this.tip.wrapper.shake();});
	}*/
}

/*	SLIDESHOW
----------------------------------------*/


var currentSlide = 1;
var interval;

function init_slideshow(parent) {
	var slides = parent.select('div.'+'slide');
	var delay = 8000;
	interval = window.setInterval("do_fadeSlide('up','"+parent.id+"',1)", delay);
	slides[0].appear({ duration: 1.0, from: 0, to: 1 });
	addEvent($('slidePrev'),'click',fadeSlide('down',parent.id,.25 ));
	addEvent($('slideNext'),'click',fadeSlide('up',parent.id, .25 ));

	var links = parent.select('a.'+'slide_link');
	for(var i=0; i<links.length; i++) {
		addEvent(links[i],'mouseover',slideHover(links[i]));
		addEvent(links[i],'mouseout',slideUnhover(links[i]));
	}
}

function slideHover(e) {
	return function() { e.parentNode.className += ' hover'; };
}

function slideUnhover(e) {
	return function() { e.parentNode.className = trim(e.parentNode.className.replace(/hover/,'')); };
}

function fadeSlide(direction,parent,duration) {
	return function() { window.clearInterval(interval);do_fadeSlide(direction,parent,duration) };
}

function do_fadeSlide(direction,parent,duration) {

	var slides = $(parent).select('div.'+'slide');

	var key = currentSlide-1;
	slides[key].fade({ duration: duration, from: 1, to: 0 });
	
	if(direction == 'up') currentSlide++;
	else if(direction == 'down') currentSlide--;

	if(currentSlide > slides.length) currentSlide = 1;
	else if(currentSlide == 0) currentSlide = slides.length;
	
	key = currentSlide-1;
	window.setTimeout(function(){slides[key].appear({ duration: duration, from: 0, to: 1 });},(duration*1000));
	if(!slides[key].className.match('visible'))	slides[key].className += ' visible';
	
}

/*	INITIALIZE JAVASCRIPT
----------------------------------------*/

function init() {
	var blog = $('blog-content');
	if(blog) {
		share(blog,'share');
	}
	
	var slideshow = $('slideshow');
	if(slideshow) {
		init_slideshow(slideshow);
	}
	
	var topLink = $T('menu-item-30','a');
	addEvent(topLink[0],'click',backToTop);
	topLink[0].onclick = function(){return false;};

    jQuery('#contact form :submit').click(function () {
        if(typeof _gaq == "object"){
            _gaq.push(['_trackPageview', '/contact/complete']);
            
        }
    });
}

addEvent(window, 'load', init);
addEvent(window, 'unload',EventCache.flush);



