function externalLinks() 
{ 
	if (!document.getElementsByTagName) return; 
		var anchors = document.getElementsByTagName("a"); 
		
	for (var i=0; i<anchors.length; i++) 
	{ 
		var anchor = anchors[i]; 
		if (anchor.getAttribute("href") && anchor.getAttribute("name") == "external") 
			anchor.target = "_blank"; 
	} 
}
function display_timer(sec){
		var html_obj = document.getElementById('timer');
		if(sec == 0){
			html_obj.innerHTML = "00:00:00";
			return 0;
		}
		
		var seconds = sec % 60;
		var minutes = ((sec - seconds) % 3600) / 60;
		var hours = (sec - seconds - (minutes * 60)) / 3600;
		
		if(seconds <= 9){
			seconds = "0" + seconds;
		}
		if(minutes <= 9){
			minutes = "0" + minutes;
		}
		if(hours <= 9){
			hours = "0" + hours;
		}
		
		html_obj.innerHTML = hours + ":" + minutes + ":" + seconds;
		sec -= 1;
		setTimeout("display_timer("+sec+")",1000);
}
window.onload = externalLinks;

//SURVEY VOTES
function vote_survey() {
	var radioObj = document.survey_form.answer;
    var value_return = 0;
   	var radioLength = radioObj.length;
   	for(var i = 0; i < radioLength; i++) {
		if(radioObj[i].checked) {
			value_return = radioObj[i].value;
		}
	}
	if(value_return == 0){
		alert("You must check an option!");
	}
	else{
		$.getJSON("json/vote_survey.php?answer_id="+value_return, print_vote_result);
	}
    	
}
    
function print_vote_result(vote){
	if(vote.result == 1){
		alert("Thank you for your vote!");
	}
	else if(vote.result == 2){
		alert("You have already voted for this survey!");
	}
	else{
		alert("Your vote have not been saved! Come back later!");
	}
}

//GUESTBOOK
function submit_entry(target) {
	if(target == 'guestbook'){
		var htmlObj = document.guestbook_message;
	}
	else{
		alert("Use only the buttons from the form, please!");
		return 0;
	}
    var value_return = 1;
   	var name = htmlObj.nickname.value;
   	var location = htmlObj.location.value;
   	var message = htmlObj.message.value;
   	
   	if(name == '' || location == '' || message == ''){
   		alert("No empty fields, please!");
   		return 0;
   	}
   	
   	var my_reg = /(@)|(href(=|\s{1}=))|(www)|(<a)|(<img)|(http)/i;
   	
   	if(my_reg.test(name) || my_reg.test(location) || my_reg.test(message)){
   		value_return = 0;
   	}
   	
	if(value_return == 0){
		alert("No emails, links or urls in the form, please!");
		return 0;
	}
	else{
		name = urlencode(name);
		location = urlencode(location);
		message = urlencode(message);
		show_message('frm-message');
		$.getJSON("json/submit_entry.php?name="+name+"&location="+location+"&message="+message+"&target="+target, print_submit_entry);
	}
    	
}

function print_submit_entry(entry){
	if(entry.result == 1){
		alert("Thank you for your entry!");

	}
	else if(entry.result == 2){
		alert("You have already posted an entry today! Come back tomorrow!");
	}
	else{
		alert("Your entry have not been saved! Come back later!");
	}
}

function urlencode( str ) {
    // *     example 3: urlencode('http://www.google.nl/search?q=php.js&ie=utf-8&oe=utf-8&aq=t&rls=com.ubuntu:en-US:unofficial&client=firefox-a');
    // *     returns 3: 'http%3A%2F%2Fwww.google.nl%2Fsearch%3Fq%3Dphp.js%26ie%3Dutf-8%26oe%3Dutf-8%26aq%3Dt%26rls%3Dcom.ubuntu%3Aen-US%3Aunofficial%26client%3Dfirefox-a'
                                     
    var histogram = {}, histogram_r = {}, code = 0, tmp_arr = [];
    var ret = str.toString();
    
    var replacer = function(search, replace, str) {
        var tmp_arr = [];
        tmp_arr = str.split(search);
        return tmp_arr.join(replace);
    };
    
    // The histogram is identical to the one in urldecode.
    histogram['!']   = '%21';
    histogram['%20'] = '+';
    
    // Begin with encodeURIComponent, which most resembles PHP's encoding functions
    ret = encodeURIComponent(ret);
    
    for (search in histogram) {
        replace = histogram[search];
        ret = replacer(search, replace, ret) // Custom replace. No regexing
    }
    
    // Uppercase for full PHP compatibility
    return ret.replace(/(\%([a-z0-9]{2}))/g, function(full, m1, m2) {
        return "%"+m2.toUpperCase();
    });
    
    return ret;
}


function show_message(pElement) {
	if(document.getElementById(pElement).style.display=="block")
		document.getElementById(pElement).style.display="none";
	else
		document.getElementById(pElement).style.display="block";
}





var pic_timer;
var clip_pics = new Array();

function play_slideshow(clip_id) {

stop_slideshow();

var html_obj = document.getElementById('clip_'+clip_id);
var clip_obj = clip_pics['clip_'+clip_id];

clip_obj.index = (clip_obj.index + 1) % clip_obj.images.length;
html_obj.src   = clip_obj.images[clip_obj.index].src;

pic_timer = setTimeout('play_slideshow('+clip_id+')', '500');
}


function stop_slideshow() {
	
if (pic_timer) {
	clearTimeout(pic_timer);
	}
}

//UNDER MENU
var mtimer=null;
var menuToHide=null;
var listeMenu=new Array("under-movies","under-about","under-connect");

function show_under(menu, state) {
	if(state) {
		if(mtimer) {
			clearInterval(mtimer);
			mtimer=null;		
		}
		hideAll();
		document.getElementById(menu).style.display="block";
	}
	else {
		menuToHide=menu;
		mtimer=setInterval(hide,300);
	}
}

function hideAll() {
	for(var x=0; x<listeMenu.length; x++) {
		document.getElementById(listeMenu[x]).style.display="none";
	}
}

function hide() {
	document.getElementById(menuToHide).style.display="none";
	clearInterval(mtimer);
	mtimer=null;	
}

function resetTimer() {
	clearInterval(mtimer);
	mtimer=null;	
}
