function replaceYoutubePreview( hash, div ) {
    get( div ).innerHTML = '<img src="/graph/loading.gif" alt="Trwa ładowanie video"/>';
    var ajax = new myAjax();
    ajax.action = 'replaceYoutubePreview';
    ajax.post( 'hash='+hash, div );
}

function findOtherFriend( id ) {
	document.forms[ 'searchUserForm' ].submit();
}

function findFriend( id ) {
	var f = getValue( 'friend' );
	document.location.href = '/znajomi/'+id+'/szukaj/'+f;
}

function toggleReminder() {
	changeVisible( 'reminder' );
	if( !$.browser.msie() ) {
		$( '#reminder_mask' ).css( 'height', '30px' );
		$( '#reminder' ).css( 'height', '30px' );
	}
	changeVisible( 'reminder_mask' );
}

function toggleWV() {
	changeVisible( 'workshoper' );
	if( !$.browser.msie() ) {
		$( '#workshoper_mask' ).css( 'height', '125px' );
		$( '#workshoper' ).css( 'height', '125px' );
	}
	changeVisible( 'workshoper_mask' );
	changeVisible( 'province' );
}

function removeFriend( id ) {
	if( confirm( 'Czy na pewno chcesz usunąć tego użytkownika z listy Twoich znajomych?' ) ) {
		document.location.href = '/znajomi/usun/'+id;
	}
}
function toggleDL( check1 ) {
	if( check1.checked == true ) {
		hide( 'dl_end_div' );
	} else {
		show( 'dl_end_div' );
	}
}

function togglePhoto( id ) {
	for( i = 0; i < photos.length; i++ ) {
		var elem = photos[ i ];
		if( elem == id ) {
			show( 'photo_'+elem );
		} else {
			hide( 'photo_'+elem );
		}
	}
}

function triggerNotes( id, note ) {
	for( i = 1; i <= 5; i++ ) {
		var img = get( 'note_'+i.toString() );
		if( id >= i ) {
			img.src = getNoteSrc( i, 5 );
		} else {
			img.src = getNoteSrc( i, 0 );
		}
	}
}

function detriggerNotes( id, note ) {
	for( i = 1; i <= 5; i++ ) {
		var img = get( 'note_'+i.toString() );
		img.src = getNoteSrc( i, note );
	}	
}

function getNoteSrc( id, note ) {
	var tmp = '/graph/ocena_carZERO.gif';
	if( ( id <= note ) && ( note - id != -0.5 ) ) {
		return tmp.replace( 'ZERO', '' );
	} else if( note - id == -0.5 ) {
		return tmp.replace( 'ZERO', '_pol' );
	} else {
		return tmp.replace( 'ZERO', '_zero' );
	}
}

function searchWorkshop() {
	var href = document.location.href;
	if( href.substr( href.length - 1 ) == '/' ) {
		href = href.substring( 0, href.length - 1 );
	}
	var ar = href.split( '/' );
	ar.shift();
	ar.shift();
	ar.shift();
	if( ar.length > 3 ) {
		while( ar.length != 3 ) {
			ar.pop();
		}
	}
	var p = getValue( 'province' );
	if( p != 0 ) {
		var c = getValue( 'city' );
		var ajax = new myAjax();
		ajax.action = 'getProvinceCityNiceUrl';
		ajax.post( 'province='+p.toString()+'&city='+c.toString() );
		ajax.onLoad = function() {
			if( ar.length == 1 ) {
				ar.push( 'wszystkie' );
				ar.push( 'brak-specjalizacji' );
			} else if( ar.length == 2 ) {
				ar.push( 'brak-specjalizacji' );				
			}
			var gtl = '/'+ar.join( '/' )+'/'+this.response;
			document.location.href = gtl;
		}
	}
}

function toggleWorkshops() {
	var ajax = new myAjax();
	ajax.action = 'toggleWorkshops';
	ajax.post( 'province='+getValue( 'province' )+'&city='+getValue( 'city' ), 'workshops' );
}

function toggleWorkshopNote( id ) {
	if( id != 0 ) {
		var ajax = new myAjax();
		ajax.action = 'toggleWorkshopNote';
		ajax.post( 'id='+id, 'workshop_note' );
	}
}

function toggleCitiesWorkshop() {
	var ajax = new myAjax();
	ajax.action = 'toggleCities';
	ajax.post( 'province='+getValue( 'province' )+'&act=toggleWorkshops', 'cities' );
}

function toggleCities( province, cities, city ) {
	if( typeof( province ) != 'string' ) province = 'province';
	if( typeof( cities ) != 'string' ) cities = 'cities';
	if( typeof( city ) != 'string' ) city = '0';
	var ajax = new myAjax();
	ajax.action = 'toggleCities';
	ajax.post( 'province='+getValue( province )+'&city='+city, cities );
}

function submitPoll( id ) {
	var s = getRadioValueByName( 'sonda_'+id );
	var ajax = new myAjax();
	ajax.action = 'submitPoll';
	ajax.post( 'id='+id+'&value='+s, 'sonda' );
}

function toggleGroups( id, g ) {
	var ajax = new myAjax();
	ajax.action = 'toggleGroups';
	if( g != undefined ) {
		ajax.post( 'category='+id+'&g='+g, 'groups' );
	} else {
		ajax.post( 'category='+id, 'groups' );		
	}
	$( 'a.category' ).css( 'font-weight', 'normal' );
	$( '#category_'+id.toString() ).css( 'font-weight', 'bold' );
	$( '#box_add_link' ).attr( 'href', '/grupy/dodaj/'+id );
	ajax.onLoad = function() {
		reloadLeftLinksGroups();
	}

}

function reloadLeftLinksGroups() {
	var ajax = new myAjax();
	ajax.action = 'reloadLeftLinksGroups';
	ajax.post( '', 'left_links' );
}


function switchModel( model ) {
	var ajax = new myAjax();
	ajax.action = 'getGenerations';
	ajax.post( 'model='+getValue( model ) );
	ajax.onLoad = function() {
		get( 'generation' ).innerHTML = this.response;
	}
}

function switchBrand( brand ) {
	var ajax = new myAjax();
	ajax.action = 'getModels';
	ajax.post( 'brand='+getValue( brand ) );
	ajax.onLoad = function() {
		get( 'model' ).innerHTML = this.response;
	}
}

function sendAvatar() {
		
}

function changeAt( id, value ) {
	var cols = {
		gray1:'button_d8d8d8_small',
		gray2:'button_a4a4a5_small',
		gray3:'button_5d5d5d_small',
		bgBlank:'b_left',
		bg2:'b_left bg_a4a4a5',
		bg3:'b_left bg_d8d8d8',
		bgRed:'b_left bg_c70118',
		red:'button_c70118_small'
	};
	get( id ).value = value;
	var goRed = id+'_'+value.toString();
	for( var i = 1; i <= 3; i++ ) {
		var it = id+'_'+i.toString();
		var n = 'gray'+i.toString();
		get( it ).className = ( it == goRed ) ? cols.red : cols[ n ];
	}
//	putHtml( 'tester', value+' '+id );
	switch( value.toString() ) {
		case '1':
			get( id+'_3_left' ).className = cols.bg2;
			get( id+'_2_left' ).className = cols.bgRed;
			break;
		case '2':
			get( id+'_2_left' ).className = cols.bg3;
			get( id+'_3_left' ).className = cols.bgRed;
			break;
		case '3':
			get( id+'_2_left' ).className = cols.bg3;
			get( id+'_3_left' ).className = cols.bg2;
			break;
	}
}

function setAt() {
	var ar = new Array( 'at_about', 'at_personal', 'at_dl', 'at_com' );
	for( i in ar ) {
		var elemstr = ar[ i ];
		if( typeof( elemstr ) == 'string' ) {
			var elem = get( elemstr );
			changeAt( elemstr, elem.value );
		}
	}
}

function addLoadEvent(func, params) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
	  if( params != undefined ) {
		  window.onload = func(params);
	  } else {
		  window.onload = func;
	  }
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
  	  if( params != undefined ) {
//		  params = params[ 2 ];
	      func( null, null, params[ 2 ]);
	  } else{
		  func();
	  }
    }
  }
}




function saveNewPassword() {
	var op = get( 'old_pass' ).value;
	var np = get( 'new_pass' ).value;
	var npr = get( 'new_pass_repeat' ).value;
	var ajax = new myAjax();
	ajax.action = 'saveNewPassword';
	ajax.post( 'op='+op+'&np='+np+'&npr='+npr );
	ajax.onLoad = function() {
		var d = get( 'passwordMessage' );
		switch( this.response ) {
			case 'ok':
				putHtml( d, 'Zapisano nowe hasło' );
				break;
			case 'not_empty':
				putHtml( d, 'Hasło nie może być puste' );
				break;
			case 'bad_compare':
				putHtml( d, 'Hasło nie zgadza się z powtórzeniem hasła' );
				break;
			case 'bad_pass':
				putHtml( d, 'Stare hasło jest nieprawidłowe' );
				break;
			default:
				putHtml( d, this.response );
				break;
		}
	}
}

function sifr_replace() {
	if(typeof sIFR == "function"){
		sIFR.replaceElement(named({ sSelector:"div#login_panel div.sifr_l_ht", sFlashSrc:"/swf/sifr_century_gothic.swf", sColor:"#ffffff", sLinkColor:"#ffffff", sHoverColor:"#ffffff", sBgColor:'transparent', sWmode:'transparent' }));
	}
}

function searcher( target ) {
	var t = getSearch( target );
	if( t.length < 3 ) {
		alert( 'Wyszukiwany tekst musi mieć conajmniej 3 znaki' );
	} else {
		document.location = '/szukaj/'+t;//+'/kategoria/'+o;
	}
}

function searchCurrentOnEnter( e, target, linker ) {
	var keycode;
	if (window.event) keycode = window.event.keyCode;
	else if (e) keycode = e.which;
	if(keycode == "13"){
		searchCurrent( target, linker );
	}
	
}

function searchCurrent( target, linker ) {
	var t = getSearch( target );
	if( t.length < 3 ) {
		alert( 'Wyszukiwany tekst musi mieć conajmniej 3 znaki' );
	} else {
		var h = linker;
		var len = h.length;
		var ls = h.substr( len - 1, 1 );
		if( ls == '/' ) {
			h = h.substr( 0, len - 1 );
		}
		document.location.href = h + '/szukaj/'+t;
	}
}

function getSearch( target ) {
	get( target ).value = trim( getValue( target ) );
	return getValue( target ).split( ' ' ).join( '+' );
}

function enterLogin( evt ) {
   evt = (evt) ? evt : ((event) ? event : null);
	if (evt) {
		if( evt.keyCode == 13 ) {
			publicLogin();
		}
	} else {
		
	}
}

function redirectActivation() {
	setTimeout( 'redirectLogin()', 15000);
}

function redirectLogin() {
	document.location.href = '/profil/';
}

function publicLogin() {
	var l 	= getValue( 'public_login' );
	var p	= getValue( 'public_password' );
	var g = getRadioValueByName( 'save_pass' );
//	get( 'loginMessage' ).value = '';
	var ajax = new myAjax();
	var pstr = 'login='+l+'&pass='+p+'&save_pass='+g+'&cn=Client';
	ajax.action = 'publicLogin';
//	ajax.post ( pstr, 'loginMessage' );
	ajax.post ( pstr );
	ajax.onLoad = function() {
		if( this.response == 'ok' ) {
			document.location.href = '/profil/';
		} else {
			alert( this.response );
		}
	}
}



