//Private variables
var colsDefault = 0;
var rowsDefault = 0;
//var rowsCounter = 0;

//Private functions
function setDefaultValues(txtArea)
{
	colsDefault = txtArea.cols;
	rowsDefault = txtArea.rows;
	//rowsCounter = document.getElementById("rowsCounter");
}

function bindEvents(txtArea)
{
	txtArea.onkeyup = function() {
		grow(txtArea);
	}
}

//Helper functions
function grow(txtArea)
{
    var linesCount = 0;
    var lines = txtArea.value.split('\n');

    for (var i=lines.length-1; i>=0; --i)
    {
        linesCount += Math.floor((lines[i].length / colsDefault) + 1);
    }

    if (linesCount >= rowsDefault)
        txtArea.rows = linesCount + 1;
	else
        txtArea.rows = rowsDefault;
	//rowsCounter.innerHTML = linesCount + " | " + txtArea.rows;
}

//Public Method
jQuery.fn.autoGrow = function(){
	return this.each(function(){
		setDefaultValues(this);
		bindEvents(this);
	});
};




function showComment(el){
el.style.zIndex = "9999";
el.getElementsByTagName('span')[0].style.display = "block";
}

function hideComment(el){
el.style.zIndex = "99";
el.getElementsByTagName('span')[0].style.display = "none";
}

function toggle_bookmarks(){
$('.tx-timtabsociable-pi1').toggle();
}

var src = null;
var selectMothod = null;

var step1 = $('<div id="paymentMethod"><h2>Wie möchten Sie Zahlen?</h2><p style="margin: 15px 0 0 0;text-align: center;"><a href="javascript:void(0)" onclick="paymentMethod(1)" class="morelink">Lastschriftverfahren</a> <strong style="padding: 0 20px 0 20px;">oder</strong> <a href="javascript:void(0)" onclick="paymentMethod(2)" class="morelink">eBank2Pay (Online Überweisung)</a></p></div>');

$(document).ready(function(){

$(".tx-guestbook-form-textarea").autoGrow();


$('#landesgruppen_select').change(function(){

	var selected = $('#landesgruppen option:selected').val();
	window.location.href = 'http://www.grundschulverband.de/'+selected;

});

$("#accordion").accordion({
			autoHeight: false,
			navigation: true
		});


$('a[rel=lightbox]').lightBox({fixedNavigation:true, overlayBgColor: '#ccc'});


	var iframe_init = false;
	var iframe_container;
	var iframe;


	$('.mcpfile').click(function(){
		
		src = $(this).attr('href');
		
		
		if(!iframe_init){

			selectMethod = $('<div id="popup_container"></div>');
			selectMethod.empty();
			selectMethod.append(step1);
			
			//iframe_container = $('<div></div>');
			//iframe = $('<iframe src="'+src +'" width="600" height="570" frameborder="0"></iframe>');

			//$(iframe_container).append(iframe);

			//$('body').append(iframe_container);

			$('body').append(selectMethod);

			$(selectMethod).dialog({
				width: 604,
				modal: true,
				title: 'Artikel online kaufen',
					close: function(){
					
				},
				closeText: 'Fenster schließen'
			});



			iframe_init = true;

		}else{
			selectMethod.empty();
			selectMethod.append(step1);			
			$(selectMethod).dialog('open');

		}

		return false;

	});
	
	if($('.tx-guestbook-form-error').length > 0){
		if($('.tx-guestbook-form-error').html() != ''){
			showCommentForm();
			var loc = window.location;
			window.location = loc+'#comment';
		}
	}

});

function paymentMethod(method){
	if(method == 1){
		$(selectMethod).dialog('close');
		$('#popup_container').html('<iframe src="'+src.replace('ebank2pay', 'lastschrift') +'" width="600" height="570" frameborder="0" id="paymentFrame"></iframe>');
$(selectMethod).dialog('open');
	}else{
$(selectMethod).dialog('close');
		$('#popup_container').html('<iframe src="'+src +'" width="600" height="570" frameborder="0" id="paymentFrame"></iframe>');
$(selectMethod).dialog('open');
	}

}

function showCommentForm(){

$('#comment_form').slideDown();

}

