/* global.js */

var request;
var popped = false;
var audio_file;
var audio_autostart;
var audio_player;
var video_player;


/* initial actions */
window.addEvent('domready', function() {
	
	// browser fixes
	if (Browser.Engine.webkit) {
		$$('.tumblr-share').setStyle('top', '-6px');
	}

	// audio
	audio_player = new Swiff('/_swf/audio.swf', {
		width: 1,
		height: 1,
		container: $('audio-player'),
		params: {
			wmode: 'transparent'
		},
		vars: {
			file: audio_file,
			autostart: audio_autostart
		}
	});
	var control = $('audio-control');
	if (control) {
	    control.set('title', 'Play song');
		control.addEvent('click', function(e) {
			e.stop();
			playSong();
		});
		control.addClass('play');
	}
	
	// email links
	$$('a.email').addEvent('click', function(e) {
		e.stop();
		noSpam(this);
	});
	
	// pop up links
	readyPops();
	$$('#pop-close, #shade').addEvent('click', function(e) {
		e.stop();
		unPop();
	});
	//popEvents();
	
	// photo & video
	if (Browser.Platform.ipod) {
		var youtube = $$('a.youtube');
		youtube.removeEvents('click');
		youtube.each(function(item) {
			item.set('href', item.get('href').split('/video:').join('http://youtube.com/watch?v='));
		});
		var flickr = $$('a.flickr');
		flickr.removeEvents('click');
	}
	
	// copy-able text inputs
	$$('input.copy').addEvent('click', function() {
		this.focus();
		this.select();
	});
	
	// preload images
	new Asset.image('/_img/pop.png');
	new Asset.image('/_img/pop-close.png');
	new Asset.image('/_img/pop-spinner.gif');
	
});
window.addEvent('load', function() {
	
	// set content height
	var content = $$('body.layout-main #content');
	if (content) {
		var bg_h = 384;
		content.setStyle('height', Math.ceil(content.getSize().y / bg_h) * bg_h);
	}
	
	// pop up links
	popEvents();
	
});


/* no spam */
function noSpam(el) {
	addy = el.get('href').contains('mailto') ? el.get('href').split('mailto:').join('') : el.get('html');
	window.location = 'mailto:'+addy.split('[at]').join('@').split(' ').join('');
}

/* audio player */
function playSong(no_cookie) {
	if (audio_player) audio_player.toElement().playSong();
	var control = $('audio-control');
	control.removeEvents('click');
	control.addEvent('click', function(e) {
		e.stop();
		pauseSong();
		Cookie.write('audio_autostart', 0);
	});
	control.removeClass('play');
	control.addClass('pause');
	control.set('title', 'Pause song');
	Cookie.write('audio_autostart', 1);
}
function pauseSong() {
	if (audio_player) audio_player.toElement().pauseSong();
    var control = $('audio-control');
    control.removeEvents('click');
    control.addEvent('click', function(e) {
    	e.stop();
    	playSong();
    });
    control.removeClass('pause');
    control.addClass('play');
    control.set('title', 'Play song');
    //Cookie.write('audio_autostart', 0);
}


/* video player */
function videoPlayer(swf, width, height) {
	pauseSong();
	return new Swiff(swf, {
		width: width,
		height: height,
		container: $('video-player'),
		params: {
			allowFullScreen: 'true'
		},
		vars: {
			rel: 0,
			autoplay: 0,
			fs: 1,
			showsearch: 0,
			showinfo: 1,
			iv_load_policy: 3
		}
	});
}


/* pop ups */
function readyPops() {
	var links = $$('a.pop');
	links.removeEvents('click');
	links.addEvent('click', function(e) {
		e.stop();
		pop(this.href);
	});
}
function pop(url) {
	if (popped) unPop();
	$('shade').setStyle('display', 'block');
	$('pop').setStyle('display', 'block');
	$$('body').setStyle('overflow', 'hidden');
	request = new Request({
		url: url+'/pop',
		method: 'get',
		evalScripts: true
	});
	request.onSuccess = function(html) {
		$('pop-spinner').setStyle('display', 'none')
		var content = $('pop-content');
		content.set('html', html);
		content.setStyles({
			'width': 'auto',
			'height': 'auto'
		});
		readyPops();
		popped = true;
		popEvents();
	}
	request.send();
}
function unPop() {
	request.cancel();
	$('pop').setStyle('display', 'none');
	$('shade').setStyle('display', 'none');
	$$('body').setStyle('overflow', 'auto');
	$('pop-spinner').setStyle('display', 'block');
	var content = $('pop-content');
	content.set('html', '');
	content.setStyles({
		'width': 240,
		'height': 240
	});
	popped = false;
}
function popEvents() {
	return true;
}




