/* add to fav */
function add2Fav(){
	if (document.all)
		window.external.AddFavorite(document.location, document.title);
	else
		window.sidebar.addPanel(document.title, document.location, "");
}




/***********
 * AJAX
 * ******************************/
var catchOutput = 1;
var outputDivID = "";
var ajaxLoader1 = '<center><img style="margin:0px; padding:0px; border-width:0px" src="webpageFiles/images/loadingAjax.gif"/></center>';
var ajaxLoader2 = '<div style="vertical-align:top"><center><img style="margin:0px; padding:0px; border-width:0px" src="webpageFiles/images/loadingAjax2.gif"/></center></div>';
var ajaxLoaderDelay = 500;

var outputDivTemp="";

//TODO: zrobic ustawialny prog ponizje ktorego komentarze sa ukrywane

/*
submitCommentArticle(id,formName){
	x_submitForm()
	
}*/

function injectThreadReplyForm(id){
	
	var formString = ''+
	'<div  style="overflow:hidden;margin-bottom: 5px;  border: 0px solid #ccc; ">'+
	'<div style="overflow:hidden;text-align: right; '+
	'	border-top: 1px solid #f00;'+
	'	border-bottom: 1px solid #f00;'+
		
	'	border: 1px solid #98aecb;'+
	'	padding-top:5px;'+
	'	padding-bottom:5px;'+
	'	padding-right:5px;'+
	'	margin:4px;">'+
		
	'	<h5>Odpowiedz</h5>'+
	'	<p style="text-align:right;">Dodajesz odpowiedz do powyzszego Watku</p>'+
	'	<form name="threadReply" method="post">'+
	'			<textarea 	style=" width: 95%; font-size:9px" wrap="virtual" name="tresc" rows="10" /></textarea>'+
	'		<div style="text-align:right">'+
	 '  			<div style="float:right">'+
		'   			<p style="text-align:right; margin: 10px;"><a style="width: 140px;display:block; padding:3px; border: 1px solid #ccc; cursor:pointer;"'+
						' onclick="submitForm(\'threadReply\','+ id +', threadReply, \'threadReplyForm\')"> Wyślij Odpowiedź</a></p>'+
	   	'		</div>'+
   		'	</div>'+
		'</form>'+
	'</div>'+
'</div>';
	
	
	document.getElementById('threadReplyForm').innerHTML = formString;
	
}


function injectPostReplyForm(id,div,option){
	var formString = ''+
	'<div  style="overflow:hidden;margin-bottom: 5px;  border: 0px solid #ccc; ">'+
	'<div style="overflow:hidden;text-align: right; '+
	'	border-top: 1px solid #f00;'+
	'	border-bottom: 1px solid #f00;'+
		
	'	border: 1px solid #98aecb;'+
	'	padding-top:5px;'+
	'	padding-bottom:5px;'+
	'	padding-right:5px;'+
	'	margin:4px;">'+
		
	'	<h5>Odpowiedz</h5>'+
	'	<p style="text-align:right;">Dodajesz odpowiedz do powyzszego posta</p>'+
	'	<form name="commentReply'+ id +'" method="post">'+
	'			<textarea 	style=" width: 95%; font-size:9px" wrap="virtual" name="tresc" rows="5" /></textarea><br><br>';
	if (option == 0) {
		formString = formString + '<p style="text-align:right">Wpisz swój pseudonim <input style="width: 30%;"  name="pseudonim"></p>'+
			'<p style="text-align:right">Wpisz swój email <input style="width: 30%;"  name="email"></p><br>'+
			'<p style="text-align:right"><b>UWAGA!!! </b>Jeśli chcesz, aby Twoje komentarzy były identyfikowane z Tobą,<br> zaloguj się lub zarejestruj jeśli niemasz jeszcze konta.</p>';
	};
	formString = formString + '		<div style="text-align:right">'+
	 '  			<div style="float:right">'+
		'   			<p style="text-align:right; margin: 10px;"><a style="width: 140px;display:block; padding:3px; border: 1px solid #ccc; cursor:pointer;"'+
						' onclick="submitForm(\'postReply\','+ id +', commentReply'+ id +', \'replyForm'+ id +'\','+option+')"> Wyślij Odpowiedź</a></p>'+
	   	'		</div>'+
   		'	</div>'+
		'</form>'+
	'</div>'+
'</div>';
	
	
	document.getElementById(div).innerHTML = formString;
}

function injectArticleReplyForm(id,option,div){
	
	var formString = '<div style="margin-bottom: 50px; border: 1px solid #ccc; padding: 10px;">'+	
	'<div style="text-align:right">' +
		'<h3>Komentuj Artykuł</h3>'+
		'<p style="text-align:right">Masz własne zdanie? Podziel się nim z innymi!</p>'+
		
		'<form name="articleForm" method="post">'+
			'<p style="padding:0px; margin-bottom: 10px;"></p>'+
			'<textarea style="width: 98%; height:300px;" wrap="virtual" name="tresc"    /></textarea><br><br>';
		if (option == 0) {
			formString = formString + '<p style="text-align:right">Wpisz swój pseudonim <input style="width: 30%;"  name="pseudonim"></p>'+
			'<p style="text-align:right">Wpisz swój email <input style="width: 30%;"  name="email"></p><br>'+
			'<p style="text-align:right"><b>UWAGA!!! </b>Jeśli chcesz, aby Twoje komentarzy były identyfikowane z Tobą,<br> zaloguj się lub zarejestruj jeśli niemasz jeszcze konta.</p>';
		};
		formString = formString + '<div style="text-align:right; margin-top: 10px;">'+
				'<!--<input class="submit" type="submit" value="&nbsp;&nbsp;Komentuj&nbsp;&nbsp;" />-->'+
				'<a style="cursor:pointer;" onclick="submitForm(\'articleReply\',' + id + ', articleForm, \'articleReplyForm\','+option+')"> Dodaj Komentarz </a>'+
			'</div>'+
		'</form>'+
		
'</div>'+
'</div>';

	document.getElementById('articleReplyForm').innerHTML = formString;
}


/*-----------------------------------------------*/
// gathers data from forms, and sends via POST

function submitForm(type,id,formName,div,option) {
	//alert (id + '_'+formName);
//	alert (formName + '_' + formName.name);
	
	
	catchOutput = 1;
	outputDivID = div;
	
	var fn = document.forms[formName.name];
	
	//document.getElementById(outputDivID).innerHTML = '<center><img style="margin:0px; padding:0px; border-width:0px" src="webpageFiles/images/loadingAjax.gif"/></center>'
	//var fn = formName;
	
	//document.getElementById("switchOff").style.display = "block"; // przycmienie wszystkiego


	// Gather values and names from the Forms
	/*var url = 'ajax.php?ajax=form';
	var params = null;
	var mf = document.formName;
	var j = mf.elements.length;
	*/
	//for (i=0;i<j;i++){
	
	if (type=="articleReply")
		var url = 'ajax.php?form=articleReply';
	if (type=="postReply")
		var url = 'ajax.php?form=postReply';
	if (type=="threadReply")
		var url = 'ajax.php?form=threadReply';
	
	
	var params = 'id='+id+ '&';
		//params = params + '&' + fn.elements[0].name + '=' + fn.elements[0].value.replace(/&/g,'%26');
		params = params + '&' + fn.elements[0].name;
		params = params + '=' + fn.elements[0].value.replace(/&/g,'%26');
		if (option == 0) {
			params = params + '&' + fn.elements[1].name;
			params = params + '=' + fn.elements[1].value.replace(/&/g,'%26');
			params = params + '&' + fn.elements[2].name;
			params = params + '=' + fn.elements[2].value.replace(/&/g,'%26');
		};
	//alert(fn.elements[0].name+'<<'+fn.elements[0].value);
	//}
	//	alert ('params:'+params);

	
	//////////// Create XMLHttpRequest()	
	//document.getElementById(pageElement).innerHTML = callMessage;
	try {
		req = new XMLHttpRequest(); 
	} catch(e) {
		try {
		req = new ActiveXObject("Msxml2.XMLHTTP");
		
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			
			} catch (E) {
				req = false;
			}
		}
	}
	

	req.onreadystatechange = function() {retrieve();};
	req.open("POST",url,true);
	req.setRequestHeader("Content-type", "application/x-www-form-urlencoded"); //application/x-www-form-urlencoded
	//req.overrideMimeType('text/html; charset=ISO-8859-2');
	req.setRequestHeader("Content-length", params.length);
	req.setRequestHeader("Connection", "close");
	
	req.send(params);
}
/*----------- End of SubmitAjax ----------------*/










function komentujKomentarz(id,option){
	
	/*var t = document.getElementById('replyForm'+id).innerHTML
	
	if (t =="")
		x_get('ajax=komentowanieOdpowiedzi&id='+id, 'replyForm'+id);
	else
		document.getElementById('replyForm'+id).innerHTML = "";*/
	
	
	injectPostReplyForm(id,'replyForm'+id,option);
	
	
}


function rateComment(ocena,id){
	
	//Ajax goes here :)
	
	// Zrobic dzialanie tej liczby, zeby innerHTML jkao INTa czytal
	
	//alert (document.getElementById('ocena'+id).style.fontWeight);
	
	if (1){
	
		if (ocena == "up"){
			var liczba = document.getElementById('ocena'+id).innerHTML;
			liczba = parseInt(liczba)+1;
			document.getElementById('ocena'+id).innerHTML = liczba; 
			
			
			
		}else{
			var liczba = document.getElementById('ocena'+id).innerHTML;
			liczba = parseInt(liczba)-1;
			//alert(document.getElementById('ocena'+id).style.fontSize);
			
			document.getElementById('ocena'+id).innerHTML = liczba;
		}
	
		x_send("ajax=ocenPost&id="+id+"&score="+ocena);
		
	}
	document.getElementById('plusMinus'+id).innerHTML = "";
	
	//document.getElementById('up'+id).src = "webpageFiles/images/iconRating1off.gif";
	//document.getElementById('down'+id).src = "webpageFiles/images/iconRating2off.gif";
	
	
	
	
}


function rozwinKomenty(id){
	
	
	
	var div = document.getElementById('re'+id).style.display
	
	if (div == 'block')
		document.getElementById('triangle'+id).src= "webpageFiles/images/iconTriangle1.gif";
	else
		document.getElementById('triangle'+id).src= "webpageFiles/images/iconTriangle2.gif";
		
	x_get('ajax=komentarzeOdpowiedzi&id=' + id,'re'+id);
}

//function articleReplyForm(id){
	
	/*if (document.getElementById('articleCommentForm').innerHTML !="")
		document.getElementById('articleCommentForm').innerHTML = "";
	else{
		x_get('ajax=komentowanieArtykulu&id='+id,'articleCommentForm');
	}*/
	//injectThreadReplyForm(id,'articleReplyForm');
	
//}



function loadArticleComments(cpage,sort,articleID){
	x_get('ajax=komentarze&cpage=' + cpage + '&sort=' + sort + '&id=' + articleID,'contentFromAJAX');
}


function x_get(url,div){ // if onlySend ==1 then no data is displayed back at the container!
	
	if (outputDivID ==""){
		
		outputDivID = div;
		
		url = 'ajax.php?' + url;
		catchOutput = 1;
		url = url + '&random='+Math.random();
		simpleAjax(url);
		
		
		if ((outputDivID == "content1") || (outputDivID == "content2") || (outputDivID == "content3") || (outputDivID == "content4")){
			outputDivTemp = outputDivID;
			setTimeout('delayInsert(outputDivTemp,ajaxLoader2)',ajaxLoaderDelay);
			//delayInsert(outputDivID,ajaxLoader2);
			//alert(outputDivID+"<");
		}
		else{
			
			outputDivTemp = outputDivID;
			setTimeout('delayInsert(outputDivTemp,ajaxLoader1)',ajaxLoaderDelay);
		}
		
		return 1;
	}else{
		return 0;
	}
	
}

// wrzuca,  kiedy jeszcze nie udalo sie sciagnac, czyl iblad moze dlatego, ze wrzuca, ale tam juz cos jest ?
function delayInsert(where,what){

	//alert(where);
	if (document.getElementById(where).innerHTML == "")
		document.getElementById(where).innerHTML = what;
}

function x_send(url){ 
	
	
	url = 'ajax.php?' + url;
	catchOutput = 0;
	url = url + '&random='+Math.random();
	simpleAjax(url);
	//
	//document.getElementById(outputDivID).innerHTML = '<center><img style="margin:0px; padding:0px; border-width:0px" src="webpageFiles/images/loadingAjax.gif"/></center>'
	
}



function simpleAjax(url) {
//	document.getElementById("switchOff").style.display = "inline";
	try {
		req = new XMLHttpRequest(); 
	} catch(e) {
		try {
		req = new ActiveXObject("Msxml2.XMLHTTP");
		
		} catch (e) {
			try {
				req = new ActiveXObject("Microsoft.XMLHTTP");
			
			} catch (E) {
				req = false;
			}
		}
	}

	req.onreadystatechange = function() {retrieve();};

	req.open("GET",url,true);
	req.send(null);
}


function retrieve() {
	var output = '';
	if(req.readyState == 4) {
		if(req.status == 200) {
			//document.getElementById("switchOff").style.display = "none";
			output = req.responseText;
			if (catchOutput == 1){
				document.getElementById(outputDivID).innerHTML = output;
				outputDivID = "";
			}
		}
	}
}


















/***********************
 * Pozostale
 */


function toggleReplies(commentID){
	var div = document.getElementById(commentID).style.display
	
	if (div == 'block'){
		document.getElementById(commentID).style.display='none';
	}
	else{
		document.getElementById(commentID).style.display='block';
	}
	
	
}

function cenzuraToggle(id){
	var div = document.getElementById('cenzura'+id).style.display;
	
	
	if (div == 'none'){
		document.getElementById('cenzura'+id).style.display='block';
		document.getElementById('cenzuraTekst'+id).style.display='none';
	}
	
	
	
}


function ocenArtykul(score,id,userid){
	
	var t = document.getElementById("iloscOcen").innerHTML;
	document.getElementById("iloscOcen").innerHTML = parseInt(t)+1;
	document.getElementById("mojaOcena").innerHTML = '<span style="color: #aaa">Dziękujemy za oddanie głosu</span>';

	//alert(score + ' ' +id)
	x_send("ajax=ocenArtykul&id="+id+"&score="+score+"&userid="+userid);
}





function ratingOver(score){
	//var t = document.getElementById("ratingComment").innerHTML;
	
	
	
	if (score == "1")
		document.getElementById("ratingComment").innerHTML = "Dno";
	if (score == "2")
		document.getElementById("ratingComment").innerHTML = "Słaby";
	if (score == "3")
		document.getElementById("ratingComment").innerHTML = "Średni";
	if (score == "4")
		document.getElementById("ratingComment").innerHTML = "Dobry";
	if (score == "5")
		document.getElementById("ratingComment").innerHTML = "Świetny";
		
	
	
	
		
	
	for (var i=1; i<=score; i++){
		document.getElementById("r"+i).src="webpageFiles/images/r44.gif";
	}
	
	
}
function ratingOut(){
	
	document.getElementById("ratingComment").innerHTML = "";
	
	for (var i=1; i<=5; i++){
		document.getElementById("r"+i).src="webpageFiles/images/r04.gif";
	}
		
}



function onBtnOverX(which,me){

	
	//if (outputDivID != which)
	//	document.getElementById('content'+outputDivID).innerHTML = "";
	
	
	if (outputDivID == ""){
	
			for (var i=0;i<=4;i++){
				if (i!= which){
						document.getElementById("content"+i).style.display="none";
						
				}
			}
			
			var classElements = getElementsByClassName("btnOver");
			/*for each (var c in classElements){
					c.className = "btn";
			}*/
			
			//array.forEach(function(){})
			
			for (i=0;i<classElements.length;i++)
				classElements[i].className ="btn";
			
			
			
			
			
			if (which != 0){
				
				if ((document.getElementById("content"+which).innerHTML == "")){
					
					document.getElementById("content"+which).style.display="block";	
					x_get("polecamy="+which,"content"+which);
					
					me.className="btnOver";
				}else{
					
					document.getElementById("content"+which).style.display="block";	
					me.className="btnOver";
				}
				
			}else{
				document.getElementById("content"+which).style.display="block";	
				me.className="btnOver";
				
			}
			
	}
	
}





function getElementsByClassName(searchClass) {

	var classElements = new Array();
	var node = null;
	var tag = null;
	if ( node == null )
		node = document;
	if ( tag == null )
		tag = '*';

	var els = node.getElementsByTagName(tag);
	var elsLen = els.length;
	var pattern = new RegExp("(^|\\s)"+searchClass+"(\\s|$)");
	for (i = 0, j = 0; i < elsLen; i++) {
		if ( pattern.test(els[i].className) ) {
			classElements[j] = els[i];
			j++;
		}
	}
	
	return classElements;
}

/**********************/
//Maszynista

var delay=111;
var delay2=13;
var currentChar=0;
var destination="";
var level =1.0;



function type()
{
  if (document.getElementById)
  {
    var dest=document.getElementById(destination);
    if (dest)
    {
      dest.innerHTML= machinistText.substr(0, currentChar);
      currentChar++;
      if (currentChar>text.length)
      {
//machinistText.length
      //  ww
				//clearTimeout();
				currentChar=0;
				setTimeout("dest.innerHTML=\"WWW\"",10000);
				//dest.innerHTML="WWW";
				setTimeout("type()", 4000);
      }
      else
      {
      //alert(\'d\');
      //alert(currentChar + \'_\' + machinistText.substr(currentChar,1));
      
      //} //dodane
      //alert(text.length);
      if (machinistText.substr(currentChar-1,1) == " ")
     	 setTimeout("type()", delay2 + Math.random()*100);
      else if ((currentChar>4)&&(machinistText.substr(currentChar-3,1) == ".")){
      	
         setTimeout("type()", 1200);
         }
        else if ((currentChar>4)&&(machinistText.substr(currentChar-3,1) == ",")){
      	
         setTimeout("type()", 300);
         }
      else
      	
         setTimeout("type()", delay);
      }
    }
  }
}

function startTyping(textParam, delayParam, destinationParam)
{
  text=textParam;
  delay=delayParam;
  currentChar=1;
  destination=destinationParam;
  type();
}
/** Enf of Maszynista */

