/******************************************************************************
*
*            FUNKCJE ETENDARD
*
*
*
*
*
******************************************************************************/

function in_array(search, table){
	for(var i in table){
		if(table[i] == search) {
			return i;
		}
	}
	
	return -1;
}

function cytujKomentarz(commentId) {
	if(document.all) {
		var text = document.getElementById('komentarz'+commentId).innerText
		//document.getElementById('element').innerText = "my text";
	}
	else {
		var text = document.getElementById('komentarz'+commentId).textContent
		//document.getElementById('element').textContent = "my text";
	}

	//alert(text);
	var currentText = nicEditors.findEditor('tekstkom').getContent();
	
	nicEditors.findEditor('tekstkom').setContent(currentText+'<br />"'+text+'"<br />');
}

function showFoto(imageName, posLeft, posTop) {
	newWindow = window.open("","newWindow","width=100,height=100,left="+posLeft+",top="+posTop);
	newWindow.document.open();
	newWindow.document.write('<html>');
	newWindow.document.write('<head>');
	newWindow.document.write('<title>::</title>');
	newWindow.document.write('<script type="text/javascript">  ');
	newWindow.document.write('function resize1() {');
	newWindow.document.write('var image1= new Image();');
	newWindow.document.write('image1.src = "'+imageName+'";');
	newWindow.document.write('if(image1.width==0 || image1.height==0) { setTimeout(\'resize1()\',100); }');
	//newWindow.document.write('alert(image1.width + \' - \' + image1.height);');
	newWindow.document.write('self.resizeTo(parseInt(image1.width+20),parseInt(image1.height+80));');
	newWindow.document.write('}');
	newWindow.document.write('  </script>');
	newWindow.document.write('</head>');
	newWindow.document.write('<body bgcolor="#FFFFFF" leftmargin="0" topmargin="0" marginheight="0" marginwidth="0" onBlur="self.close()">'); 
	newWindow.document.write('<center><img src='+imageName+' name="Zdjecie"></center>'); 
	newWindow.document.write('<script type="text/javascript">setTimeout(\'resize1()\',100);</script>');               
	newWindow.document.write('</body></html>');
	newWindow.document.close();
	newWindow.focus();
}

function Show(id) {
	document.getElementById(id).style.display = 'block';
}

function Hide(id) {
	document.getElementById(id).style.display = 'none';
}

function Options(id) {
	if(document.getElementById(id).style.display == 'block') {
		Hide(id);
	}
	else {
		Show(id);
	}
}

var counter = 0;
function moreFields() {
	counter++;
	
	if (counter <= 2) {
		var src = document.getElementById('readroot');
		var newFields = src.cloneNode(true);
		newFields.id = '';
		newFields.style.display = 'block';
		
		//var newField = newFields.childNodes;
		//for (var i=0;i<newField.length;i++) {
		//	var theName = newField[i].name
		//	if (theName) {
		//		newField[i].name = theName + counter;
		//	}
		//}
		
		var insertHere = document.getElementById('writeroot');
		//newFields = insertHere.parentNode.insertBefore(newFields,insertHere);
		newFields = src.parentNode.insertBefore(newFields,src.nextSibling);
		child_inputs = newFields.getElementsByTagName('INPUT');
		for (i=0;i<child_inputs.length;i++) {
			var theName = child_inputs[i].name;
			if (theName) {
				//child_inputs[i].name = theName+'[]';
				//child_inputs[i].name = theName+'['+counter+']';
			}
		}
	}
}


function ajaxVoting() {
	$$("a.vote_up").addEvent('click', function(e){
		var linkid = this.get('id');
		
		var myRequest = new Request({
			url: 'publikacje/glosuj/plus/'+linkid+'/1/',
			method: 'get',
			onSuccess: function(responseText, responseXML) {
				var splitData = responseText.split(",");
				var status = splitData[3];
				
				if (status == 'success') {
					$('points-'+linkid).empty().appendText(splitData[0]);
					//$('votes-'+linkid).empty().appendText(splitData[1]);
				}
				else {
					alert(status);
				}
			}
		});
		myRequest.send('foo=bar&from=ajax');
		
		return false;
	});
	
	$$("a.vote_down").addEvent('click', function(e) {
		var linkid = this.get('id');
		
		var myRequest = new Request({
			url: 'publikacje/glosuj/minus/'+linkid+'/1/',
			method: 'get',
			onSuccess: function(responseText, responseXML) {
				var splitData = responseText.split(",");
				var status = splitData[3];
				
				if (status == 'success') {
					$('points-'+linkid).empty().appendText(splitData[0]);
					//$('votes-'+linkid).empty().appendText(splitData[1]);
				}
				else {
					alert(status);
				}
			}
		});
		myRequest.send('foo=bar&from=ajax');
		
		return false;
	});
}

function eventsVoting() {
	$$("a.event_vote").addEvent('click', function(e) {
		var linkid = this.get('id');
		
		var myRequest = new Request({
			url: 'wydarzenia/glosuj/plus/'+linkid+'/ajax/',
			method: 'get',
			onSuccess: function(responseText, responseXML) {
				var splitData = responseText.split(",");
				var status = splitData[2];
				
				if (status == 'success') {
					$('points-'+linkid).empty().appendText(splitData[0]);
					//$('votes-'+linkid).empty().appendText(splitData[1]);
				}
				else {
					alert(status);
				}
			}
		});
		myRequest.send('foo=bar&from=ajax');
		
		return false;
	});
}
$(window).addEvent('domready', function(){
	ajaxVoting();
	eventsVoting();
	
});