function dirname(path) {
    // Returns the directory name component of the path  
    // version: 810.114
    // discuss at: http://phpjs.org/functions/dirname
    return path.replace(/\\/g,'/').replace(/\/[^\/]*\/?$/, '');
}

// zdefinowanie sciezki bazowej
var _BASE_PATH = new String( dirname( $("script[src$='js/general.js']").attr('src') ) );
_BASE_PATH = (_BASE_PATH != '' ) ? _BASE_PATH.substring( 0 , _BASE_PATH.length - 2 ) : '';


function fTooltip( sDivName ){
	$('.tooltip').not('#'+sDivName).hide();
	$('#'+sDivName).toggle();
}



$(document).ready( function() {
		
	// obsluga zewnatrznych linkow
	$('a.external').click( function(){	window.open(this.href,'_blank'); return false;})
	
	//Wyłączanie focus
	$('a').focus(function() { this.blur(); });
	
	
	$('a.submit').click( function() { 
		$('input[alt]').each( function(){
			if ( $(this).val() == $(this).attr('alt') )
				$(this).val( '' );	
		})
		$(this).parents('form:first').submit();
	})
	
	fSetFocusBlurOnInput();

	if ( $('#boxTooltip').size() && $('#btnTooltip').size() ){

		var bOverOnStart = true;
		$('#boxZgoda').mouseover( function(){
			if ( bOverOnStart ){
				$('#boxClickZgoda').hide(); $('#boxTooltip').show();
				bOverOnStart = false;
			}
		})
		
		$('#btnTooltip').click( function(){	$('#boxClickZgoda').hide(); $('#boxTooltip').show(); })
		$('#boxTooltip a').click( function(){ $('#boxClickZgoda').show(); $('#boxTooltip').hide();})
	}


	// uruchomienie leadow
	if ( $('#boxFlashLead').size() ){
		var swfLead = new SWFObject("themes/main/swf/cube.swf", "flashLead", "277", "100", "8", "#ffffff");
		swfLead.addParam("base", ".");
		swfLead.addParam("menu", "false");
		swfLead.write("boxFlashLead");
		$('#boxFlashLead').addClass('active');
	}

	// uruchomienie kalkulatora
	if ( $('#boxFlashCalc').size() ){
		var swfCalc = new SWFObject("themes/main/swf/n01kalk_kalk_kredyt_konso.swf", "flashKalkulator", "394", "290", "8", "#ffffff");
		swfCalc.addParam("base", ".");
		swfCalc.addParam("menu", "false");
		//swfCalc.addParam("wmode", "transparent");
		swfCalc.addVariable('&wniosekUrl','wniosek.php');
		swfCalc.addVariable('szXmlFile', '../xml/n01kalk_kalk_kredyt_konso.xml' );
		
		
		if ( $('#iKwotaKredytu').size() && $('#iRatIlosc').size() ){
			swfCalc.addVariable('iKredytValCrnt', $('#iKwotaKredytu').val() );
			swfCalc.addVariable('iRatIloscValCrnt', $('#iRatIlosc').val() );
			swfCalc.addVariable('iRatSplaconych', $('#iRatSplaconych').val() );
			swfCalc.addVariable('szFromCookieKalkulatorId','gotowkaGlowna');
		} 
		
		swfCalc.write("boxFlashCalc");
		$('#boxNoFlash').addClass('active');
	}
	
	function format(item) {
	  	return item;
	}
	$("#sMiasto").autocomplete("index.php?sModul=mAutocomplete&sAction=autocompleteCity",{
		scroll: true,
		width: 250,
		minChars: 4,
		
		max: 20,
		delay:10,
		selectFirst: true, 
		cacheLength: 1,
		delay: 10,
		multiple: false,
		formatItem: formatItem,
		parse: function(data) {
			for(i=0;i<data.length;i++){
				return $.map(eval(data), function(row) {
						return {
							data: row,
							value: row[i],
							result: row
						}
				})
			};
			
		},
		formatItem: function(item) {
			return format(item);
		}
	}).result(function(e, item) {
		$("#content").append("<p>selected " + format(item) + "</p>");
	});
	$("#sKod").autocomplete("index.php?sModul=mAutocomplete&sAction=autocompleteCode",{
		scroll: false,
		width: 49,
		
		max: 10,
		selectFirst: false, 
		multiple: false,
		formatItem: formatItem,
		parse: function(data) {
			for(i=0;i<data.length;i++){
				return $.map(eval(data), function(row) {
						return {
							data: row,
							value: row[i],
							result: row
						}
				})
			};
			
		},
		formatItem: function(item) {
			return format(item);
		}
	}).result(function(e, item) {
		$("#content").append("<p>selected " + format(item) + "</p>");
	});
	
	
	
});
function fCheckCode(){
	
	var sCity = $('#sMiasto').val();
	
	
	if(!sCity ){
			return false;
	}
	
	sUrl = 'index.php?sModul=mAutocomplete&sAction=checkCode';
	
	$.ajax({
			type: 'POST',
			url: sUrl,
			dataType:'xml',
			data: '&sCity='+sCity,
			success: function(data){
				$('#sKod').attr('value',$(data).find('content').text());
			},
			error: function(XMLHttpRequest, textStatus, errorThrown){
				$('#sKod').attr('value','');}
	});
	
	
}

function formatItem(row) {  
	if(row[1] == undefined) {  
		return row[0];  
	} else {  
		return row[0] + " (id: " + row[1] + ")";  
	}  
} 
function setFocusAndHide(tip,input){
	tip.style.visibility='hidden';
	document.getElementById(input).focus();
}

function hideOnFocus(input){
	
	$('#'+input.id+'Error').attr('style','visibility:hidden');
	
}

function getLastRadioButton(element){
	var array = element.parentNode.getElementsByTagName('div');
	var el = array[array.length-1];
	if(!el.getElementsByTagName('input')[0].checked)
	el.getElementsByTagName('input')[0].click();
	
}

function clearInput(clear){
	if(!clear)
		$('#sPyt4Inp').attr('value','');
}

/**
 * @desc nakladka na eyeblastera
 */
function ebConversionTracker(conv) { 
	var ebConversionImg = new Image(); 
	var ebConversionURL = 'HTTP://bs.serving-sys.com/BurstingPipe/ActivityServer.bs?cn=as&ActivityID='+conv+'&ns=1'; 
	ebConversionImg.src = ebConversionURL; 
}


/* wyslanie formularza */
function fSendForm(sFormName){
		document.forms[sFormName].submit();
}


/* funckaja pomocnicza */
function $$(sIdName){
		return document.getElementById(sIdName);
}	



/* czyszczenie pol formularza na focus */
function fSetFocusBlurOnInput(){
	
	$('#fPersonal  input').each( function(){
			if ( $(this).val() == '' )
				 $(this).val( $(this).attr('alt') );
		
	});
	
	$('#fPersonal  input').focus( function(){
			if ( $(this).val() == $(this).attr('alt') )
					$(this).val( '' );	
	});
	
	$('#fPersonal  input').blur( function(){
			if ( $(this).val() == '' )
					$(this).val( $(this).attr('alt') );	
	});

}

//obiekt zliczajacy uzycie kalkulatora
var oCalcStat = {
	iChangeCounter :0,
	
	notice: function() {
		/*
		if ( this.iChangeCounter>0 && pageTracker != undefined ){
			pageTracker._trackPageview('/iCalcChange,'+this.iChangeCounter+'.xml');
		}
		*/
		this.iChangeCounter++;
		$('#iChangeCounter').val( this.iChangeCounter - 1);
		delete this.timeoutID;
	},
	
	setup: function() {
		this.cancel();
		var self = this;
		this.timeoutID = window.setTimeout(function() { self.notice();}, 1000 );
	},
	
	cancel: function() {
		if(typeof this.timeoutID == "number") {
			window.clearTimeout(this.timeoutID);
			delete this.timeoutID;
		}
	}
};

/**
* funkcja wywolujaca zmiany w formularzu
* @param {Object} oVars
*/
function FsetVarsFromCalculator(oVars){
	
	if (oCalcStat.iChangeCounter > 0) {
		$('#iKwotaKredytu').val( oVars.oCookieVars.iKredytValCrnt );
		$('#iRatIlosc').val(oVars.oCookieVars.iRatIloscValCrnt );
		$('#iRatSplaconych').val(oVars.oCookieVars.iRatSplaconych );
	}
	oCalcStat.setup();
}


function fTDPixelView(){
	 $.ajax({
		   type: "GET",
		   url: "index.php",
		   data: "sModul=mTDReport",
		   success: function(msg){	$('body').append(msg); }
	 });
}



