// Initial-Zeugs
jQuery(document).ready(function(){
	initScroller();	
	loadTweets();
	registerPushChat();
});


function m(name){
	ma(name, 'klatschheftli.ch');
}

function m2(name){
	ma2(name, 'klatschheftli.ch');
}

function ma(name, domain){
	var z = '&#0';
	z += '64';
	var y = 'mai';
	y += 'lto:';
	var x = name + z + domain;
	document.write('<a href="' + y + x + '">Mail</a>');
}

function ma2(name, domain){
	var z = '&#0';
	z += '64';
	var y = 'mai';
	y += 'lto:';
	var x = name + z + domain;
	document.write('<a href="' + y + x + '">' + x + '</a>');
}








// Reloads every 1 second and displays the new countdown time
function countdown(){
	var timestring = displayTime(remsecs);
	document.getElementById('countdown').innerHTML = timestring;

	remsecs = remsecs - 1;
	if(remsecs < 0){
		if(remsecs == -1){
			setTimeout('location.reload()', 2000);
		}
		document.getElementById('countdown').innerHTML = 'fertig';
	}
	else{
		window.setTimeout("countdown()", 1000);
	}
}


// returns a formatted time
function displayTime(secs){
	var dd = Math.floor(secs / (60*60*24));
	secs = secs - (dd * (60*60*24));

	var dh = Math.floor(secs / (60*60));
	secs = secs - (dh * (60*60));

	var dm = Math.floor(secs / 60);
	secs = secs - (dm * 60);

	var ds = Math.floor(secs);

	var re = '';

	if(dd > 1){
		re += dd + '&nbsp;Tagen, ';
	}
	else{
		if(dd == 1){
			re += '1&nbsp;Tag, ';
		}
	}

	if(dh > 1){
		re += dh + '&nbsp;Stunden, ';
	}
	else{
		if(dh == 1){
			re += '1&nbsp;Stunde, ';
		}
		else{
			if(re.length > 0){
				re += '0&nbsp;Stunden, ';
			}
		}
	}

	if(dm > 1){
		re += dm + '&nbsp;Minuten, ';
	}
	else{
		if(dm == 1){
			re += '1&nbsp;Minute, ';
		}
		else{
			if(re.length > 0){
				re += '0&nbsp;Minuten, ';
			}
		}
	}

	if(ds > 1){
		re += ds + '&nbsp;Sekunden';
	}
	else{
		if(ds == 1){
			re += '1&nbsp;Sekunde';
		}
		else{
			re += '0&nbsp;Sekunden';
		}
	}

	return re;
}









/* Loads tweets */
function loadTweets(){
	/*
	var api_base = 'http://search.twitter.com/search.json';
	var api_query = 'klatschheftli+OR+Amber_Steele+OR+MissSchweiz+OR+BJHyatt+OR+MisterSchweiz+OR+FMBabillard+OR+klatschheftli.ch';
	var fullurl = api_base + '?q=' + api_query + '&rpp=8';
	*/
	

	var loadurl = '/wp-content/themes/shawne/twitter-content.html';
	jQuery.ajax({ 
		'url': loadurl,
		'success': function(responseData){
			$('#twitter-placeholder').html(responseData);
		}
	});
}

function initScroller(){

	$('#scroller-header').find('a').each(function(index, element){
		$(element).click(function(){
			var index = $(this).attr('index');
			changeCentreContent(index);
		});
	});

	changeCentreContent(1);
	
}


function changeCentreContent(i){
	$('#scroller-body').find('li').each(function(index, el){
		$(el).hide();
	});

	$('#scroller-header').find('li a').each(function(i2, el){
		if(i2 == i){
			$(el).css('color', '#880000');
		}
		else{
			$(el).css('color', '#ddb3b3');
		}
	});

	var active_el = $('#scroller-body').find('li')[i];
	$(active_el).show();
	
}




/* == PUSHER CHAT ============ */

function registerPushChat(){
    if(jQuery('#slam_container')){
        var elText = jQuery('#slam_container textarea');
        var elName = jQuery('#slam_container input[name="slam_name"]');
        elText.keyup(function(ev){
            if(ev.keyCode == 13){
                chat_submit(elName.val(), elText.val());
            }
        });
    }
}


function chat_submit(name, text){

    // Validation HERE
    
    jQuery('#slam_container textarea').val('');
    jQuery.ajax({
        'url': templateDir + '/ajax/submit-chat.php',
        'data': { 'name': name, 'text': text }
    });
}



/* Sound */

var SoundData = {};
SoundData.sounds = [];

var atest = new Audio();
if(atest.canPlayType('audio/mpeg') != ''){
	SoundData.soundtype = 'mp3';
}
else{
	SoundData.soundtype = 'ogg';
}

function playSnd(type){
	var mod = 6;

	if(typeof SoundData.sounds[type] == 'undefined'){
		SoundData.sounds[type] = { 'files' : [], 'currentIndex': 0 };
		for(var i = 0; i < mod; i++){
			SoundData.sounds[type].files.push(new Audio(templateDir + '/sounds/' + type + '.' + SoundData.soundtype));
			SoundData.sounds[type].files[i].volume = 0.2;
		}
	}
	SoundData.sounds[type].files[SoundData.sounds[type].currentIndex].play();
	SoundData.sounds[type].currentIndex = (SoundData.sounds[type].currentIndex + 1) % mod;
}









