/*	When document is full loaded (ready) init
 *  dom element's behaviours and animations 
 */


$(document).ready(function(){
	
	$('.kalendarz').datepicker({dateFormat:'yy-mm-dd'});
	
		$('a[rel*=facebox]').facebox();
	
	$('img.obrazek').parent('a').facebox();
	
	//Transparency fix for IE6
	$('div.gora_prawy, img, table.pakiety div, table.pakiety th, table.recordsCont th, table.recordsCont td a, table.history td a, div.trfirst, div.trlast, div.dol, a.tabelka').ifixpng();
//  td.tdwiecej, div.porady_wiecej,div.uslugi_wiecej, div.placowki_wiecej, div.reklama_wiecej 
	//$.("#date").datepicker();

	$('#skladanie_ofert_data').datepicker({dateFormat:'yy-mm-dd'});
	$('#realizacja_ofert_data').datepicker({dateFormat:'yy-mm-dd'});
	// end ready function
	
	$('.tab_premium').click(function(){
		$(this).removeClass('prem_noact');
		$(this).addClass('prem_act');
		$('.tab_standard').removeClass('stand_act');
		$('.tab_standard').addClass('stand_noact');
		$('.tab_max').removeClass('max_act');
		$('.tab_max').addClass('max_noact');
		
		
		$('.stand').hide();
		$('.max').hide();
		$('.premium').fadeIn();
		//$('div.dol_lewy img, div.fistaszek img,').ifixpng();
	});
	$('.tab_standard').click(function(){
		$(this).removeClass('stand_noact');
		$(this).addClass('stand_act');
		$('.tab_premium').removeClass('prem_act');
		$('.tab_premium').addClass('prem_noact');
		$('.tab_max').removeClass('max_act');
		$('.tab_max').addClass('max_noact');
		
		$('.premium').hide();
		$('.max').hide();
		$('.stand').fadeIn();
		//$('div.dol_lewy img, div.fistaszek img,').ifixpng();
	});
	$('.tab_max').click(function(){
		$(this).removeClass('max_noact');
		$(this).addClass('max_act');
		$('.tab_premium').removeClass('prem_act');
		$('.tab_premium').addClass('prem_noact');
		$('.tab_standard').removeClass('stand_act');
		$('.tab_standard').addClass('stand_noact');
		
		$('.premium').hide();
		$('.stand').hide();
		$('.max').fadeIn();
		//$('div.dol_lewy img, div.fistaszek img,').ifixpng();
	});
	
	$('#login').click(function(){
		$(this).attr('value','');
	});
	$('#password').click(function(){
		$(this).attr('value','');
	});
	
	$('#tresc_ogloszenia_header').toggle(function(){
		$(this).next().slideDown();
		$(this).children('span').html('zwiń&nbsp;&uArr;');
		
	},function()
	{
		$(this).next().slideUp();
		$(this).children('span').html('rozwiń&nbsp;&dArr;');
	});
	
	
	$('#opis_przedmiotu_header').toggle(function(){
		$(this).next().slideDown();
		$(this).children('span').html('zwiń&nbsp;&uArr;');
		
	},function()
	{
		$(this).next().slideUp();
		$(this).children('span').html('rozwiń&nbsp;&dArr;');
	});
	
	$('#specyfikacja_header').toggle(function(){
		$(this).next().slideDown();
		$(this).children('span').html('zwiń&nbsp;&uArr;');
	},function()
	{
		$(this).next().slideUp();
		$(this).children('span').html('rozwiń&nbsp;&dArr;');
	});
	
	$('#wymagania_header').toggle(function(){
		$(this).next().slideDown();
		$(this).children('span').html('zwiń&nbsp;&uArr;');
		
	},function()
	{
		$(this).next().slideUp();
		$(this).children('span').html('rozwiń&nbsp;&dArr;');
	});
	
	$('#wadium_header').toggle(function(){
		$(this).next().slideDown();
		$(this).children('span').html('zwiń&nbsp;&uArr;');
		
	},function()
	{
		$(this).next().slideUp();
		$(this).children('span').html('rozwiń&nbsp;&dArr;');
	});
	
	$('.wysz_zaawa').toggle(function(){
		$(this).html('zaawansowana &uarr;');
		$('.zaawansowana_select').slideDown();
		$('#zaawansowana').slideDown();
	},function(){
		$(this).html('zaawansowana &darr;');
		$('.zaawansowana_select').slideUp();
		$('#zaawansowana').slideUp();
	});
	

});


function datePrzetargChange(){
	$('#form_przetarg').attr('action','index.php?mod=mod&scr=auctions&evt=showFirstSelect');
	$('#form_przetarg').submit();
}

function changeEvt(evt){
	//$('#screen').attr('action','index.php?mod=mod&scr=auction&evt='+evt);
	//$('#form_przetarg').submit();
	//$('#screen').attr('method','POST');
	$('#evt').attr('value','addProfile');
	//$('#mod').attr('value','');
	//$('#scr').attr('value','');
	
	//$("#evt").remove();
	//$("#mod").remove();
	//$("#scr").remove();
	var str = $("#screen").serialize();
	$.post("index.ajax.php?"+str,function(data){
		if (!data)
		{
			$("#load").show();
		}
		else
		{	
			eval("var json_obiekt = ("+data+")");
			zm='';
			for (key in json_obiekt)
			{
				zm+=json_obiekt[key]+'\n';
			}
			alert(zm);
		}
		$('#evt').attr('value','init');
	})
}

function saveProfileUE(){
	$('#evt').attr('value','saveProfileUE');
	var str = $("#screen").serialize();
	$.post("index.ajax.php?"+str,function(data){
		if (!data)
		{
			$("#load").show();
		}
		else
		{	
			eval("var json_obiekt = ("+data+")");
			zm='';
			for (key in json_obiekt)
			{
				zm+=json_obiekt[key]+'\n';
			}
			alert(zm);
		}
		$('#evt').attr('value','initUE');
	})
}

function changeTenderFrom(){
	tender_from=$('#tender_from').val();
	if (tender_from==1)
		document.location='index.php?mod=mod&scr=auctions&evt=initUE';
	else
		document.location='index.php?mod=mod&scr=auctions&evt=init';
}

function changeTenderStoreFrom(){
	tender_from=$('#tender_from').val();
	if (tender_from==1)
		document.location='index.php?mod=mod&scr=store&evt=showUE';
	else
		document.location='index.php?mod=mod&scr=store&evt=show';
}

function changeTenderEmailFrom(){
	tender_from=$('#tender_from').val();
	if (tender_from==1)
		document.location='index.php?mod=mod&scr=history&evt=emailsUE';
	else
		document.location='index.php?mod=mod&scr=history&evt=emails';
}

function loadProfile(evt){
	//$('#form_przetarg').attr('action','index.php?mod=mod&scr=auctions&evt=showFirstSelect');
	//$('#form_przetarg').submit();
	//$('#screen').attr('action','index.php?mod=mod&scr=auctions&evt='+evt);
	//$('#screen').attr('method','POST');
	$('#evt').attr('value',evt);
}

function mainCategoryChange(){
	//$('#form_przetarg').attr('action','index.php?mod=mod&scr=auctions&evt=showSecondSelect');
	$('#form_przetarg').attr('action','dodaj_przetarg_krok_2');
	$('#form_przetarg').submit();
}


function deleteStore(id_store)
{
	$.post("index.ajax.php?mod=mod&scr=store&evt=delPrzetarg&p_id="+id_store,function(data){
		$("#store_"+id_store).remove();
	})
}

function addStore(id_store)
{
	
	$('.loader'+id_store).show();
	$.post("index.ajax.php?mod=mod&scr=store&evt=addPrzetarg&p_id="+id_store,function(data){
		setTimeout(function(){
		$('.loader'+id_store).hide();	
		},800);
		if(data==1)
		{
			$('#infoBoxStoreErr').hide();
			$('#infoBoxStore').children('div').children('p').html('Dodano do schowka');
			$('#infoBoxStore').show();
		}
		else
		{
			$('#infoBoxStore').hide();
			$('#infoBoxStoreErr').children('div').children('p').html('Nie dodano do schowka');
			$('#infoBoxStoreErr').show();
		}
		
	});
}

function addStoreUE(id_store)
{
	
	$('.loader'+id_store).show();
	$.post("index.ajax.php?mod=mod&scr=store&evt=addPrzetargUE&p_id="+id_store,function(data){
		setTimeout(function(){
		$('.loader'+id_store).hide();	
		},800);
		if(data==1)
		{
			$('#infoBoxStoreErr').hide();
			$('#infoBoxStore').children('div').children('p').html('Dodano do schowka');
			$('#infoBoxStore').show();
		}
		else
		{
			$('#infoBoxStore').hide();
			$('#infoBoxStoreErr').children('div').children('p').html('Nie dodano do schowka');
			$('#infoBoxStoreErr').show();
		}
		
	})
}
/*
function addStoreUE(id_store)
{
	$.post("index.ajax.php?mod=mod&scr=store&evt=addPrzetargUE&p_id="+id_store,function(data){
	})
}
*/
function showFirstSelect()
{
	date=$('#date_przetarg').val();
	date_baz='2008-09-15';
	//if (date<date_baz)
	//	alert('mniejsza');
	//else
	//	alert('wiekasz');
	$.post("index.ajax.php?mod=mod&scr=auctions&evt=showFirstSelect",function(data){
		alert(data);
		$("#test")=data;
	})
	//alert($('#date_przetarg').val());
}

function changeTender()
{
	wart=$('#main_tender').val();
	if(wart==1)
	{
		$('#children_tenders_s_div').show();
		$('#children_tenders_p_div').hide();
		$('#children_tenders_m_div').hide();
	}
	else if(wart==2)
	{
		$('#children_tenders_s_div').hide();
		$('#children_tenders_p_div').show();
		$('#children_tenders_m_div').hide();
	}
	else if(wart==3)
	{
		$('#children_tenders_s_div').hide();
		$('#children_tenders_p_div').hide();
		$('#children_tenders_m_div').show();
	}
	else
	{
		$('#children_tenders_s_div').hide();
		$('#children_tenders_p_div').hide();
		$('#children_tenders_m_div').hide();
	}
}

function changeState()
{
	state=$('#state_ue').val();
	$.post("index.ajax.php?mod=mod&scr=auctions&evt=cities&state="+state,function(data){
		eval("var json_obiekt = ("+data+")");
	
		obiekt = '<select name="city_ue" id="city_ue">';
		obiekt += '<option value="0" selected="selected">Wybierz miasto</option>';
		for (key in json_obiekt) {
			obiekt += '<option value="' + json_obiekt[key].nazwa + '" >' + json_obiekt[key].nazwa + '</option>';
		}
			
		$('#city_ue').remove();
		$('#city_div').append(obiekt);
	})
}

function changeSector()
{
	sector=$('#sector').val();
	$.post("index.ajax.php?mod=mod&scr=auctions&evt=leafs&sector="+sector,function(data){
		eval("var json_obiekt = ("+data+")");
	
		obiekt = '<select name="leaf" id="leaf">';
		obiekt += '<option value="0" selected="selected">Wybierz podbranżę</option>';
		for (key in json_obiekt) {
			obiekt += '<option value="' + json_obiekt[key].id + '" >' + json_obiekt[key].nazwa + '</option>';
		}
			
		$('#leaf').remove();
		$('#leaf_div').append(obiekt);
	})
}

function changeSectorUE()
{
	sector=$('#sector_ue').val();
	$.post("index.ajax.php?mod=mod&scr=auctions&evt=leafsUE&sector_ue="+sector,function(data){
		eval("var json_obiekt = ("+data+")");
	
		obiekt = '<select name="leaf_ue" id="leaf_ue">';
		obiekt += '<option value="0" selected="selected">Wybierz podbranżę</option>';
		for (key in json_obiekt) {
			obiekt += '<option value="' + json_obiekt[key].id + '" >' + json_obiekt[key].nazwa + '</option>';
		}
			
		$('#leaf_ue').remove();
		$('#leaf_ue_div').append(obiekt);
	})
}
