function ppmClick(id){
	for (i = 1; i<=4; i++){
		$('#ppm_'+i).removeClass('ppm_'+i+'_a');
		$('#pp_tabs_'+i).css('display','none');
	}
	$('#ppm_'+id).addClass('ppm_'+id+'_a');
	$('#pp_tabs_'+id).css('display','block');	
	return false;
}
function invmClick(id){
	for (i = 1; i<=3; i++){
		$('#invm_'+i).removeClass('invm_'+i+'_a');
		$('#inv_tabs_'+i).css('display','none');
	}
	$('#invm_'+id).addClass('invm_'+id+'_a');
	$('#inv_tabs_'+id).css('display','block');
	return false;
}
function checkboxInit(){
	$('div.checkbox').each(function(){
		if ( $('input',this)[0].checked ){
			$(this).addClass('checked');
		}
	});
	
	$('div.checkbox').click(function(){
		if ( ! $(this).hasClass('locked')){
			if ( $('input',this)[0].checked ){
				$(this).removeClass('checked');
				$('input',this)[0].checked = '';
				if ( $(this).attr('id') != '' ){
					$('.'+$(this).attr('id')).each(function(){
						$(this).removeClass('checked');
						$(this).addClass('locked');
						$('input',this)[0].checked = '';
					});
				}
			}
			else{
				if ($(this).hasClass('exclusive')){
					$('div.checkbox',$(this).closest('.optbox')).each(function(){
						$(this).removeClass('checked');
						$('input',this)[0].checked = '';
					});					
				}
				$(this).addClass('checked');
				$('input',this)[0].checked = 'checked';
				if ( $(this).attr('id') != '' ){
					$('.'+$(this).attr('id')).each(function(){
						$(this).removeClass('locked');
						$(this).addClass('checked');
						$('input',this)[0].checked = 'checked';
					});
				}
			}
		}
	});	
}
function checkAll(group){
	$('.'+group).each(function(){
		$('input',this)[0].checked = 'checked';
		$(this).addClass('checked');
	});
}
function checkNone(group){
	$('.'+group).each(function(){
		$('input',this)[0].checked = '';
		$(this).removeClass('checked');
	});
}
function kwPickerOpen(divId){
	$('.hidding').hide();
	$('#'+divId).show();
	hide ++;
}
function kwPickerClick(inputId,divId,value){
	$('#'+inputId).attr('value',value);
	$('#'+divId).css('display','none');
}
function selectOptAndExpand(selectId, optionId, obj){
	hs.close();
	hs.htmlExpand(obj,{ contentId: 'question_form' } );
	setTimeout('selectOpt(".'+selectId+'",".'+optionId+'")',200);
	return false;
}
function selectOpt(selectId, optionId){
	$('option',$(selectId)).removeAttr('selected');
	$(optionId).attr('selected','selected');
}
var objGlobal = null;
var idGlobal = null;
function askSubmit(obj,invId){
	objGlobal = obj;
	$.post('ajax.php?action=ask_submit',{
		'inv_id' : invId,
		'category' : $('option:selected',obj).val(),
		'content' : $('#ask_content',obj).val()
	},function(data){
		$('form',$(objGlobal).parent()).hide();
		$('#askInfo',$(objGlobal).parent()).html(data.getElementsByTagName('message')[0].childNodes[0].nodeValue);
		$('#askInfo',$(objGlobal).parent()).show();
	});
	return false;
}
function commentSubmit(obj,invId){
	objGlobal = obj;
	$.post('ajax.php?action=comment_submit',{
		'inv_id' : invId,
		'content' : $('#comment_content',obj).val()
	},function(data){
		$('form',$(objGlobal).parent()).hide();
		$('#commentInfo',$(objGlobal).parent()).html(data.getElementsByTagName('message')[0].childNodes[0].nodeValue);
		$('#commentInfo',$(objGlobal).parent()).show();
	});
	return false;
}
function addToObserved(obj, invId){
	
	objGlobal = obj;
	$.post('ajax.php?action=add_obsrv',{
		'inv_id' : invId
	}, function(data){
		hs.htmlExpand(objGlobal, { contentId: 'add_to_observed' });
		//$('#obsrvInfo',hs.getExpander()).text(data.getElementsByTagName('message')[0].childNodes[0].nodeValue);
		setTimeout('setExpanderText("obsrvInfo","'+data.getElementsByTagName('message')[0].childNodes[0].nodeValue+'")',500);
	});
	return false;
}
function setExpanderText(objectId, text){
	$('.'+objectId).text(text);
	
}
function saveObservedNote(obj, idObserved){
	objGlobal = $(obj).parent();
	$.post('ajax.php?action=save_obsrv_note',{
		'id_observed' : idObserved,
		'note' : $('#note_content_'+idObserved, $(obj).parent()).val()
	}, function(data){
		if (data.getElementsByTagName('status')[0].childNodes[0].nodeValue == 'ok'){
			$('.note', $('#hid_'+data.getElementsByTagName('id_observed')[0].childNodes[0].nodeValue)).html(data.getElementsByTagName('note')[0].childNodes[0].nodeValue);
		}
		
		hs.close(objGlobal);
	});
	return false;
}

function deleteObserved(obj, idObserved){
	objGlobal = obj;
	$.post('ajax.php?action=del_obsrv',{
		'id_observed' : idObserved
	}, function(data){
		if (data.getElementsByTagName('status')[0].childNodes[0].nodeValue == 'ok'){
			idObserved = data.getElementsByTagName('id_observed')[0].childNodes[0].nodeValue;
			hs.close(objGlobal);
			$('#hid_'+idObserved).remove();
			$('#norm_'+idObserved).remove();
			
		}
	});
}
function showObserved(id){
	$('#hid_'+id).show();
	$('#hide_'+id).show();
	$('#show_'+id).hide();
}
function hideObserved(id){
	$('#hid_'+id).hide();
	$('#hide_'+id).hide();
	$('#show_'+id).show();
}

function deleteParticipation(obj, idUdzial){
	objGlobal = obj;
	$.post('ajax.php?action=del_participation',{
		'id_udzial' : idUdzial
	}, function(data){
		if (data.getElementsByTagName('status')[0].childNodes[0].nodeValue == 'ok') {
			idUdzial = data.getElementsByTagName('id_observed')[0].childNodes[0].nodeValue;
			hs.close(objGlobal);
			$('#tr_'+idUdzial).remove();
		}
	});
}

function deleteQuestion(obj, idQuestion){
	objGlobal = obj;
	$.post('ajax.php?action=del_question', { 'id_question' : idQuestion}, function(data){
		if (data.getElementsByTagName('status')[0].childNodes[0].nodeValue == 'ok'){
			idQuestion = data.getElementsByTagName('id_question')[0].childNodes[0].nodeValue;
			hs.close(objGlobal);
			$('#quest_'+idQuestion).remove();
			$('#ans_'+idQuestion).remove();
		}		
	});
}

function addFav(obj){
	objGlobal = $(obj).closest('#add_fav_hs');
	if ( $('#add_fav_name',objGlobal).val() != ''){
		$.post('ajax.php?action=add_fav',{
			'name' : $('#add_fav_name',objGlobal).val()
		}, function(data){			
			$('#add_fav_msg', objGlobal).html(data.getElementsByTagName('message')[0].childNodes[0].nodeValue);
			$('#add_fav_form', objGlobal).hide();
			$('#add_fav_done', objGlobal).show();							
		});
	}
	else{
		alert('Proszę wprowadzić nazwę zapytania');
	}
	return false;
}

function delFav(id){
	idGlobal = id;
	$.post('ajax.php?action=del_fav',{
		'id_fav' : id
	},function(data){
		if (data.getElementsByTagName('message')[0].childNodes[0].nodeValue == 'OK'){
			$('#fav_'+idGlobal).remove();
		}
		else{
			alert(data.getElementsByTagName('message')[0].childNodes[0].nodeValue);
		}
	});
}

function switchInvBox(tab,index){
	if (index < 0 ){ index = boxInv[tab].length-1; }
	if (index > boxInv[tab].length-1 ){ index = 0; }
	$('#img_box_image_1').css('background-image','url("galeria/homepage/'+boxInv[tab][index]['id']+'.jpg")');
	if ( navigator.appVersion.indexOf("MSIE 6.") >= 0){
		$('#img_box_link_1').css('background-image','url("galeria/homepage/'+boxInv[tab][index]['id']+'.jpg")');
	}
	$('#img_box_href_1').attr('href','inwestycja-'+boxInv[tab][index]['id']+'.htm');
	$('#img_box_txt_1').attr('href','inwestycja-'+boxInv[tab][index]['id']+'.htm');
	$('#img_box_href_1').attr('title',boxInv[tab][index]['name']);
	$('#img_box_txt_1').text(boxInv[tab][index]['name']);
	boxInvTab = tab;
	boxInvIndex = index;
	return false;
}
function switchArtBox(tab,index){
	if (index < 0 ){ index = boxArt[tab].length-1; }
	if (index > boxArt[tab].length-1 ){ index = 0; }
	$('#img_box_image_2').css('background-image','url("images/middle/'+boxArt[tab][index]['img']+'.jpg")');
	if ( navigator.appVersion.indexOf("MSIE 6.") >= 0){
		$('#img_box_link_2').css('background-image','url("images/middle/'+boxArt[tab][index]['img']+'.jpg")');
	}
	$('#img_box_href_2').attr('href','z_branzy_'+boxArt[tab][index]['id']+'.htm');
	$('#img_box_txt_2').attr('href','z_branzy_'+boxArt[tab][index]['id']+'.htm');
	$('#img_box_href_2').attr('title',boxArt[tab][index]['title']);
	$('#img_box_txt_2').text(boxArt[tab][index]['title']);
	boxArtTab = tab;
	boxArtIndex = index;
	return false;
} 
function switchParticipationMode(){
	if ( $('input[name=mode]:checked').val() == 'linked'){
		$('.p_exist').attr('disabled','disabled');
		$('.p_add').attr('disabled','disabled');
	}
	else if ( $('input[name=mode]:checked').val() == 'exist'){
		$('.p_exist').attr('disabled','');
		$('.p_add').attr('disabled','disabled');
	}
	else{
		$('.p_exist').attr('disabled','disabled');
		$('.p_add').attr('disabled','');
	}
}

function postParticipation(obj, invId){
	objGlobal = obj;
	$.post('ajax.php?action=participate',{
		'inv_id' : invId,
		'mode' 	: $('input[name=mode]:checked', obj).val() ,
		'id_firma'	: $('option:selected', $('#firma', obj)).val(),
		'branza_u' : $('input[name=userCompanyBranza]:checked', obj).val(),
		'nazwa' : $('#nazwa', obj).val(),
		'kraj' : $('#kraj', obj).val(),
		'wojewodztwo' : $('option:selected', $('#wojewodztwo', obj)).val(),
		'branza' : $('option:selected' ,$('#branza', obj)).val(),
		'miasto' : $('#miasto', obj).val(),
		'kod_pocztowy' : $('#kod_pocztowy', obj).val(),
		'ulica' : $('#ulica', obj).val(),
		'telefon' : $('#telefon', obj).val(),
		'fax' : $('#fax', obj).val(),
		'www' : $('#www', obj).val(),
		'email' : $('#email', obj).val(),
		'c_imie_nazwisko' : $('#c_imie_nazwisko', obj).val(),
		'c_stanowisko' : $('#c_stanowisko', obj).val(),
		'c_telefon' : $('#c_telefon', obj).val(),
		'c_email' : $('#c_email', obj).val()
	}, function(data){
		$('form[name=participate]',$(objGlobal).parent()).hide();
		$('#lock',$(objGlobal).parent()).val(data.getElementsByTagName('lock')[0].childNodes[0].nodeValue); 
		$('#id_participate',$(objGlobal).parent()).val(data.getElementsByTagName('id_participate')[0].childNodes[0].nodeValue);
		$('#participationData', $(objGlobal).parent()).html(data.getElementsByTagName('html')[0].childNodes[0].nodeValue);
		$('form[name=confirmParticipation]',$(objGlobal).parent()).show();
	});
	return false; 
}

function confirmParticipationF(obj){	
	objGlobal = obj;
	$.post('ajax.php?action=parti_conf',{
		'id_participate' : $('#id_participate', obj).val(),
		'lock' : $('#lock', obj).val()
	}, function(data){
		$('form',$(objGlobal).parent()).hide();
		$('#formConfirm',$(objGlobal).parent()).hide();
		$('#partInfo', $(objGlobal).parent()).text(data.getElementsByTagName('message')[0].childNodes[0].nodeValue);		
		$('#partInfo', $(objGlobal).parent()).show();
		if ( data.getElementsByTagName('reload_on_close')[0].childNodes[0].nodeValue == 1){
			$('#part_close', $(objGlobal).parents('.highslide-body')).click(function(){
				window.location.reload();
			});
		}
	});
	return false;
}

function filterInvestment(){
	$.post('ajax.php?action=inv_list',{'filter' : $('#inv_filter').val()}, function(data){
		investments = data.getElementsByTagName('investment');
		$('option', $('#inv_id')).remove();
		for (i=0; i < investments.length; i++){
			id = investments[i].getElementsByTagName('id')[0].firstChild.data;
			nazwa = investments[i].getElementsByTagName('nazwa')[0].firstChild.data;
			$('#inv_id').append('<option value="'+id+'">'+nazwa+'</option>');
		}
	});
}

function filterCompany(){
	$.post('ajax.php?action=company_list',{'filter' : $('#company_filter').val()}, function(data){
		investments = data.getElementsByTagName('company');
		$('option', $('#id_firma')).remove();
		for (i=0; i < investments.length; i++){
			id = investments[i].getElementsByTagName('id')[0].firstChild.data;
			nazwa = investments[i].getElementsByTagName('nazwa')[0].firstChild.data;
			$('#id_firma').append('<option value="'+id+'">'+nazwa+'</option>');
		}
	});
}
function filterCompanyHS(obj){
	objGlobal = $(obj).parents('form');
	$.post('ajax.php?action=company_list',{'filter' : $('#company_filter').val()}, function(data){
		investments = data.getElementsByTagName('company');
		$('option', $('select[name=firma]', $(objGlobal))).remove();
		
		for (i=0; i < investments.length; i++){
			id = investments[i].getElementsByTagName('id')[0].firstChild.data;
			nazwa = investments[i].getElementsByTagName('nazwa')[0].firstChild.data;
			$('select[name=firma]',$(objGlobal)).append('<option value="'+id+'">'+nazwa+'</option>');
		}
	});
}
function filterCompanyHSonEnter(evt){
	if(evt.keyCode == 13) {
		filterCompanyHS(this);
		return false;
	}
	return true;
};

function loadBranza(obj, postfix){
	if ( isNaN(postfix)){
		postfix = '';
	}
	objGlobal = $(obj).parents('form');
	$.post('ajax.php?action=branza_list',{
		'id_group' : $('option:selected', $(obj)).val()
	}, function(data){
		branze = data.getElementsByTagName('branza');
		$('option', $('select[name=branza'+postfix+']', $(objGlobal))).remove();
		$('select[name=branza'+postfix+']',$(objGlobal)).append('<option value="">...wybierz</option>');
		for (i=0; i < branze.length; i++){
			id = branze[i].getElementsByTagName('id')[0].firstChild.data;
			nazwa = branze[i].getElementsByTagName('nazwa')[0].firstChild.data;
			$('select[name=branza'+postfix+']',$(objGlobal)).append('<option value="'+id+'">'+nazwa+'</option>');
		}
	});
}

function participationSubmit(obj){
	if ( $('option:selected', $('#inv_id',$(obj).parents('form'))).val() != undefined && $('option:selected', $('#inv_id',$(obj).parents('form'))).val() != 0 ){
		return true;
	}
	else{
		alert('Proszę wybrać inwestycję');
		return false;
	}
}

function participationConfirm(obj){
	if ( $('#part_confirm_check', $(obj).parents('form')).attr('checked')	 ){
		return true;
	}
	else{
		alert('Poroszę potwierdzić poprawność danych');
		return false;
	}
}

function clearSearch(mode){
	$('#str').val('');
	$('#dro').val('');
	$('#drd').val('');
	$('#dzo').val('');
	$('#dzd').val('');
	$('#wo').val('');
	$('#wd').val('');
	$('#ct').val('');
	$('#fi').val('');
	if ( mode ){
		checkAll('woj_chk');
		checkAll('etap_chk');
		checkAll('pp_chk');
		checkAll('s_1');
		checkAll('s_2');
		checkAll('s_3');
	}
	else{
		checkNone('woj_chk');
		checkNone('etap_chk');
		checkNone('pp_chk');
		checkNone('s_1');
		checkNone('s_2');
		checkNone('s_3');
	}
	return false;
}

$(window).load(function(){

	$('img.rating_bt').mouseover(function(){
		index = $(this).attr('alt');
		$('img.rating_bt').each(function(){
			if ( $(this).attr('alt') <= index ){
				$(this).attr('src','css/kompasinwestycji/img/pink_dot.png');
			}
		});
	});	
	
	$('img.rating_bt').mouseout(function(){
		$('img.rating_bt').attr('src','css/kompasinwestycji/img/gray_dot.png');
	});
	
	
	$('img.rating_bt').click(function(){
		index = $(this).attr('alt');
		$.get('ajax.php',{'action':'inv_rating','inv_id':globalInvId,'rating':index},function(data){
			$('#rating_bar').hide();
			$('#rating_thx').show();			
		});
	});
});

function aInvM(location,name,id,ikonka){
	match = location.match(/\(([0-9]+.[0-9]+),.([0-9]+.[0-9]+)\)/);
	temp = new GMarker(new GLatLng(parseFloat(match[0]),parseFloat(match[1])), {title:name, clickable : true, icon : ikonka	});
	GEvent.addListener(temp, "click", function(){ window.location= "inwestycja-"+id+".htm"; });
	return temp;
}

