//linki zewnętrzne

function targetBlank() {
	if (document.getElementsByTagName) {
		var anchors = document.getElementsByTagName( "a" );
		for (var loop = 0; loop < anchors.length; loop++) {
			var anchor = anchors[loop];
			if (anchor.getAttribute("href") && anchor.getAttribute("rev") == "_blank") {
				anchor.target = "_blank";
			}
			else if ( !(anchors[loop].rev.indexOf('_popup') < 0) ) {
				anchors[loop].onclick=function() {

					var
					re,
					tmpWidth,
					tmpHeight,
					tmpArray,
					tmpParam;

					re = /width([0-9]{1,3})/;
					if ( re.test(this.rev) ) {
						tmpArray = re.exec(this.rev);
						tmpWidth = tmpArray[1];
					}
					re = /height([0-9]{1,3})/;
					if ( re.test(this.rev) ) {
						tmpArray = re.exec(this.rev);
						tmpHeight = tmpArray[1];
					}

					var lewo = (screen.width) ? (screen.width-tmpWidth)/2 : 0;
					var gora = (screen.height) ? (screen.height-tmpHeight)/2 : 0;

					if ( tmpWidth && tmpHeight ) {
						tmpParam = "width=" + tmpWidth + ",height=" + tmpHeight + ",top=" + gora + ",left=" + lewo + ",scrollbars=yes";
					}

					window.open(this.href,'',tmpParam); return false;
				}
			}
		}
	}
}
//<a href="http://www.wp.pl" rev="_popup-width200-height200" >www.wp.pl</a>

// zmiana value

function changeInputValue() {
	var inp;
	var dflText;
	inp = document.getElementsByTagName('input');
	for( var i=0 ; i < inp.length ; i++ ) {
		if ( inp[i].type == "text" && inp[i].className.indexOf('changeInputValue')>0 ) {
			dflText = inp[i].value;
			//alert(dflText);
			if ( dflText != "" ) {
				inp[i].setAttribute('autocomplete','off');
				eval("inp[i].onfocus = function() {if( this.value == '"+dflText+"' ) this.value='';}");
				eval("inp[i].onblur = function() {if( this.value == '' ) this.value='"+dflText+"';}");
			}
		}
	}
}

pokazUkryjFAQvar = false;

function pokazUkryjFAQ() {

	var faq = document.getElementById('faq');
	if ( faq ) {
		if (document.getElementById && document.getElementsByTagName) {
			var pytanie = faq.getElementsByTagName('dt');
			var odpowiedz = faq.getElementsByTagName('dd');
			for( var i=0; i<pytanie.length; i++ ) {
				pytanie[i].odpRef = odpowiedz[i];
				if (pytanie[i].className == "pytanieActive") {
					odpowiedz[i].className = "odpowiedz";
				}
				else {
					pytanie[i].className='pytanie';
					odpowiedz[i].className='hide';
				}
				pytanie[i].onclick = function() {
					if (this.className == "pytanieActive") {
						this.className= "pytanie";
						this.odpRef.className = "hide";
					} else {
						this.className='pytanieActive';
						this.odpRef.className = "odpowiedz";
					}
				};
			}

			if (lacze || lista) { return; }

			var lacza = faq.getElementsByTagName('a');
			var listy = faq.getElementsByTagName('ul');
			var lacze = new Array();
			var lista = new Array();
			var j = 0;

			for (i = 0; i < lacza.length; i++)
			{
				if (lacza[i].className == 'aul')
				{
					lacze[j++] = lacza[i];
				}
			}

			j = 0;

			for (i = 0; i < listy.length; i++)
			{
				if (listy[i].className == 'default')
				{
					lista[j] = listy[i];
					lista[j++].className = 'hide';
				}
			}

			//console.log(listy.length, lista.length, lacza.length, lacze.length, pokazUkryjFAQvar);

			for (i = 0; i < lacze.length; i++)
			{
				lacze[i].odpRef = lista[i];
				lacze[i].onclick = function()
				{
					if (this.odpRef.className == 'default')
					{
						this.odpRef.className = 'hide';
						this.innerHTML = this.innerHTML.replace(/:$/, '.');
					}
					else
					{
						this.odpRef.className = 'default';
						this.innerHTML = this.innerHTML.replace(/\.$/, ':');
					}
				};
			}
			pokazUkryjFAQvar = true;
		}
	}
}

function systemEkspercki(turnOn) {
	var overflowSE = document.getElementById('overflowSE');

	if ( !overflowSE ) {
		overflowSE = document.createElement('div');
		overflowSE.id = "overflowSE";

		document.body.appendChild(overflowSE);
		overflowSE.style.visibility = "hidden";
	}

	var systemEkspercki = document.getElementById('systemEkspercki');

	if ( !systemEkspercki ) {
		var systemEkspercki = document.createElement('div');

		systemEkspercki.id = "systemEkspercki";
		overflowSE.appendChild(systemEkspercki);
	}


	if (turnOn==true) {
		document.getElementsByTagName('html')[0].className = "overflowNone";
		document.body.style.overflow = "hidden";

    var sEflashFO = { movie:"swf/sub/poradnik.swf?id=0", width:"1000", height:"550", majorversion:"8", build:"0"};
    UFO.create(sEflashFO, "systemEkspercki");

    if ( overflowSE.offsetWidth < 1000 ) {
      systemEkspercki.style.marginLeft = "0";
      systemEkspercki.style.left = "0";
    }

    if (overflowSE.offsetHeight<550) {
      systemEkspercki.style.marginTop = "0";
      systemEkspercki.style.top = "0";
    }
		overflowSE.style.visibility = "";
    systemEkspercki.style.display = "block";
	} else  {
		overflowSE.style.visibility = "hidden";
		document.getElementsByTagName('html')[0].className = "";
		document.body.style.overflow = "";

		systemEkspercki.style.display = "none";
    systemEkspercki.innerHTML = "";
    }
	scroll (0,0);

//return false;
}

// Dodanie do formularza parametru blan
// Submit formularza
function submitWniosek(idele, elehref)
{
	var ele = document.getElementById(idele);
	ele.target='_blank';
	ele.submit();
}


$(document).ready(function(){

	//
	targetBlank();
	changeInputValue();
	if (pokazUkryjFAQvar == false) { pokazUkryjFAQ(); }

	//
	$('dl.faq dd').hide();
	$("dl.faq dt").toggle(function(){
			$(this).next('dd').show();
			$(this).css("color", "#1a3573");
		}, function () {
			$(this).next('dd').hide();
			$(this).css("color", "#ffffff");
	});

	$("p.faqNav").toggle(function(){
			$(this).text('zwiń wszystkie')
			$('dl.faq dd').show();
		}, function () {
			$(this).text('rozwiń wszystkie')
			$('dl.faq dd').hide();
	});

	$("a.jakPrzystapic").click(function(){
		$('dl.faq dt#ktoMoze').trigger('click')
		$('dl.faq dt#jakPrzystapic').trigger('click')
	});

	$("dl#repaymentSchedule dd").hide();
	$("#repaymentScheduleTrigger,#repaymentScheduleTriggerLabel").css("visibility", "visible");
	$("#repaymentScheduleTrigger").click(
	  function() {
	    if ( !this.checked ) {
		    $("dl#repaymentSchedule dd").css("display","block");
		    $("dl#repaymentSchedule dd").hide();
			} else {
		    $("dl#repaymentSchedule dd").css("display","block");
		    $("dl#repaymentSchedule dd").show();
			}
		}
	)
	
	$('a.newSimulation').click( function() {
	  $('#newSimulationForm').css('display', 'block');
	  $(this).hide();

		window.location.hash = this.getAttribute("href");


	  return false;
	});


});