var MSG_TYPE_OK = 1
var MSG_TYPE_ERROR = 2
var MSG_TYPE_INFO = 3

function showUploadPopup(){
    window.open('upload.xhtml', 'Video hochladen','resizable=no,menubar=no,scrollbars=no,width=500,height=350,left='+((window.screen.width-500)/2)+',top='+((window.screen.height-350)/2));
}

function showPicPopup(pic){
      var str = 'resizable=no,menubar=no,scrollbars=no,width=650,height=560,left='+((window.screen.width-650)/2)+',top='+((window.screen.height-560)/2);
      window.open('picslide.xhtml?id-pic='+pic, "Gallery", str);
//    window.open(, 'Video hochladen',);
}

function checkUploadData(){
	
	var vidName = document.getElementById("inp-vid-name").value;
	var vidFile = document.getElementById("inp-vid-file").value;
	
	if(vidName != "" && vidFile != ""){
		removeUploadForm();
		return true;
	}else{
		alert('Du musst eine Datei zum hochladen auswählen und Deinem Video einen Namen geben.');
	    return false;
	}
	
}

function updatePlayer2(){
    var vidSelect = document.getElementById("inp-vid-type");
	
	var player2 = document.getElementById("inp-player-2")
    var char2 = document.getElementById("inp-char-2")
    
    if (vidSelect.options[vidSelect.selectedIndex].value == "1"){
		player2.selectedIndex = 0;
    	char2.selectedIndex = 0;
    }
    
}

function addSmiley(smiley){	
	var code = "[:)"+smiley+"]";

    put(code);	
}

function put(code){
	var area = document.getElementById("inp-post");
	area.focus();

	if(typeof document.selection != 'undefined'){
		// IE ranz hack...
	    var range = document.selection.createRange();
	    range.text = code;
	    range = document.selection.createRange();
	    range.select();	
	    return;
	   
	}else if(typeof area.selectionStart != 'undefined'){
		// braver gecko ;)
		var start = area.selectionStart;
		var end = area.selectionEnd;
		var vor = area.value.substring(0,start);
		var hinter = area.value.substr(end);
		area.value = vor+code+hinter;
		area.selectionEnd = area.selectionStart = start+code.length;
	}else{
		alert('dein browser kann leider kein ordentliches javascript :( Empfehlung: firefox');
	}
}

function addQuote(){
  var nick = window.prompt('Name', '');

  if (!nick)return;
  
  var text = window.prompt('Text', '');
  if (!text)return;
  
  put("[quote=\""+nick+"\"]"+text+"[/quote]");
  
}

function addImg(){
  var url = window.prompt('Grafikadresse', 'http://...jpg');
  if(!url || 'http://...jpg' == url)return;
  
  put("[img]"+url+"[/img]");
  
}

function addUrl(){
  var url = window.prompt('Link', 'http://...');
  if(!url || url == 'http://...'){
	alert('Fehler: Kein Link angegeben');
	return;
  }
  var text = window.prompt('Beschreibung', '- Klick mich -');
  if(!text){
	alert('Fehler: Kein Test für den Link angegeben');
	return;
  }

  put("[url="+url+"]"+text+"[/url]");
}



function insert(aTag, eTag) {
  var area = document.getElementById("inp-post");
  area.focus();
  
  if(typeof document.selection != 'undefined') {
  
    //IE
  
    //text rein
    var range = document.selection.createRange();
    var insText = range.text;
    range.text = aTag + insText + eTag;
    
    // Cursor position setzen
    range = document.selection.createRange();
    if (insText.length == 0) {
      range.move('character', -eTag.length);
    } else {
      range.moveStart('character', aTag.length + insText.length + eTag.length);      
    }
    range.select();
    
  }else if(typeof area.selectionStart != 'undefined'){
    //gecko
  
    // text rein
    var start = area.selectionStart;
    var end = area.selectionEnd;
    var insText = area.value.substring(start, end);
    area.value = area.value.substr(0, start) + aTag + insText + eTag + area.value.substr(end);
    
    // cursor pos setzen
	var pos;
    if (insText.length == 0) {
      pos = start + aTag.length;
    } else {
      pos = start + aTag.length + insText.length + eTag.length;
    }
    area.selectionStart = pos;
    area.selectionEnd = pos;
    
  }else{
    //andere browser
    alert('Ihr Browser ist mist, oder Javascript ist nicht vollständig aktiviert');
  
    /*    // cursor pos abfragen
    var pos;
    var re = new RegExp('^[0-9]{0,3}$');
    while(!re.test(pos)) {
      pos = prompt("Einfügen an Position (0.." + input.value.length + "):", "0");
    }
    if(pos > input.value.length) {
      pos = input.value.length;
    }
    var insText = prompt("Bitte geben Sie den zu formatierenden Text ein:");
    input.value = input.value.substr(0, pos) + aTag + insText + eTag + input.value.substr(pos);
    */
    
  }
}

function changeTheme(){
	var styles = document.getElementsByTagName("link");
	for(var i = 0; i < styles.length; i++){
		styles[i].disabled = true;
	}
	var selector= document.getElementById('inp-profile-theme');
	var link = document.getElementById("css_"+selector.options[selector.selectedIndex].value);
	link.disabled = false;
}

function printProps(obj, objName) {                                                                               
  var output = "" ;                                                                                                
  for (var prop in obj) {                                                                                          
    output += obj + "." + prop + "\n" ;                                                                        
  }                                                                                                                
  return output ;                                                                                                  
}

function show_beef_form(id, nick){
	title = document.getElementById("popup-title").innerHTML = nick+' beefen';
	popup = document.getElementById("popup-content");
	title.innerHTML = '';
	popup.innerHTML = '';
	
	shit = document.createElement("img");
	shit.setAttribute('src', '../../../img/common/beef.png');
	shit.setAttribute('style', 'float: left;width: 80px; height: 80px; border:none;');
	popup.appendChild(shit);

	beefForm = document.createElement("form");
	beefForm.setAttribute('id', 'beef-form');
//	beefForm.setAttribute('onsubmit', "doBeef("+id+", this); return false;");
	popup.appendChild(beefForm);

	// styles hier sind bischen gehackt, vielleicht mal css aufräumen
	notice = document.createElement("p");
	notice.innerHTML = "Willst du "+nick+" wirklich Beefen?<br />"+nick+" sieht dann auf der Startseite deinen Beef!";
	notice.setAttribute('style', 'padding:20px 0 0 0;color:black;float:none;');
	beefForm.appendChild(notice);

	div = document.createElement("div");
	beefForm.appendChild(div);
	
	office = document.createElement("input");
	office.setAttribute('type', 'checkbox');
	office.setAttribute('name', 'office');
	office.setAttribute('id', 'office');
	office.setAttribute('class', 'small');
	div.appendChild(office);
	
	officeLab = document.createElement("label");
	officeLab.setAttribute('for', 'office');
	officeLab.innerHTML='Mit Office';
	div.appendChild(officeLab);

	buttonBar = document.createElement("div");
	buttonBar.setAttribute('id', 'button-bar');
	beefForm.appendChild(buttonBar);
	
	
	submitBut = document.createElement("input");
	submitBut.setAttribute('type', 'button');
	submitBut.setAttribute('value', 'Beefen!');
	submitBut.setAttribute('onclick', "doBeef("+id+"); return false;");
	buttonBar.appendChild(submitBut);
	
	cancelBut = document.createElement("input");
	cancelBut.setAttribute('type', 'button');
	cancelBut.setAttribute('value', 'Nee, doch nicht.');
	cancelBut.setAttribute('onclick', "hide_popup();");
	buttonBar.appendChild(cancelBut);

	clearHack = document.createElement("div");
	clearHack.setAttribute('style', "clear:both;");
	beefForm.appendChild(clearHack);
	
	show_popup();
}


function show_message(message, title, type){
	
	wtitle = document.getElementById('popup-title');
	wcontent = document.getElementById('popup-content');
	wtitle.innerHTML = '';
	wcontent.innerHTML = '';
	
	wicon = document.createElement('img');
	
	//TYPE
	switch (type) {
		case MSG_TYPE_OK:
			pic = "dialog-ok-apply.png";
			break;
		case MSG_TYPE_ERROR:
			pic = "dialog-error.png";
			break;
		default:
			pic = "dialog-information.png";
			break;
	}
	wicon.src = "../../../img/common/"+pic;
//	wicon.style.display = 'block';
//	wicon.style.float = 'left';
	wtitle.appendChild(wicon);
	
	
	//TITLE
	tspan = document.createElement('span');
	wtitle.appendChild(tspan);
	tspan.innerHTML = title;
	
	//CONTENT
	p = document.createElement("p");
	p.setAttribute('style', 'color: black');
	p.innerHTML = message;
	wcontent.appendChild(p);

	//Button-Bar
	buttonBar = document.createElement("div");
	buttonBar.setAttribute('id', 'button-bar');
	wcontent.appendChild(buttonBar);
	
	ok = document.createElement("input");
	ok.setAttribute('type', 'button');
	ok.setAttribute('value', 'OK');
	ok.setAttribute('onclick', 'hide_popup()');
	buttonBar.appendChild(ok);
	
	clearer = document.createElement('br');
	clearer.style.clear = 'both';
	wcontent.appendChild(clearer);
	
	show_popup();
	
}

function show_popup(){
	document.getElementById("overlay").style.display = 'block';
}

function hide_popup(){
	document.getElementById("overlay").style.display = 'none';
	document.getElementById("popup-icon").style.display = 'none';
	document.getElementById("popup-title").innerHTML = '';
	document.getElementById("popup-content").innerHTML = '';
}

function log(str){

	if(isset_global('console')){
		console.log(str);
	}else{
		//this is only for hardcore7 
		//debugging without firebug
		//alert(str);
	}
}

function isset_global(var_name){
	return typeof( window[var_name] ) != "undefined";
} 