/*
	@muudetud: 9.12.2011 12:43
*/

var HIND_TYYP='ost', HIND_YHIK='eur', HIND_KM_TYYP='nokm', HIND_KM=1.2;

if( typeof jQuery == 'undefined' ){}
else
{
	$(document).ready(function(){
		/* pop-up aken */
		$('a[rel="popup"]').click(function(){
			uus_popup_aken(this.href);
			return false;
		});
	});

	$(window).load(function(){

		/* prettyphoto, lightbox */
		if( $('a[rel^=prettyphoto], a[rel^=lightbox]').length > 0 )
		{
			$('<link>').appendTo('head').attr({
				rel: 'stylesheet', 
				type: 'text/css', 
				href: kodulehe_aadress  + 'kujundused/iitee/assets/css/prettyPhoto.css'
			});

			$.getScript(kodulehe_aadress + 'js/prettyPhoto/jquery.prettyPhoto.js', function(){
				$("a[rel^=prettyPhoto], a[rel^=lightbox]").prettyPhoto({
					animation_speed: 'normal',
					opacity: 0.35,
					show_title: true,
					allow_resize: true,
					default_width: 500,
					default_height: 344,
					counter_separator_label: '/',
					theme: 'facebook',
					hideflash: false,
					wmode: 'opaque',
					autoplay: false,
					modal: false,
					deeplinking: false,
					social_tools: ''
				});
			});
		}


		/*
			teenuste ikoonide vahetused
			---------------------------
		*/
		if( $('table.teenuse_rida').length > 0 )
		{
			$('table.teenuse_rida').each(function(i){
				var pic = $(this).find('img'), opic = $(pic).attr('src');

				if( opic.match(/(\_mv.jpg)$/i) )
				{
					$(this).hover(function(){
						var npic = opic.replace(/_mv/gi, '');
						$(pic).attr('src', npic);
					}, function(){
						$(pic).attr('src', opic)
					});
				}
			});
		}

		/*
			hinna kalkulaator
			-----------------
		*/
		if( $('#calc-content').length > 0 )
		{
			//var top = 450;
			var top = $('#menyy').position().top-70,
				top1 = $('#menyy').outerHeight()+20;

if(window.console) console.log('#top: ', top, '|', top1);
			$('#calc-content').css('top', top);

			$(window).scroll(function(event){
				// y-pos.
				var y = scroll_top();

				if( y >= top1 ){
					// lisame klassi .fixed
					$('#calc-content').addClass('fixed').css('top', 0);
				} else {
					// eemaldame klassi .fixed
					$('#calc-content').removeClass('fixed').css('top', top);
				}
			});

			// määrame ära mis on vaikimisi peidetud
			$('#hw_hinnakirja_vorm span.hind_km, #hw_hinnakirja_vorm span[class^="rendihind_"], #hw_hinnad_kokku span[id^="hw_rent_"], #hw_hinnad_kokku, #calc-content span[id="telli_nupp"]').hide();

			$('span.kuva_hind-nokm, span.kuva_osthind').parent().addClass('valitud');

			$('#calc-content-left').addClass('showForm');

/*
			$('.kuva_hind_eek, .kuva_hind_eur').live('click', function(){
				var id = $(this).attr('title').split('-')[1], b = $('span[title^="valuuta-"]');

				$(b).each(function(i){
					var k = $(b[i]).attr('title').split('-')[1],
						k_low = k.toLowerCase();

					if( k == id ){

						HIND_YHIK = k_low;
						$('.hind_' + k_low).show();

						$('span[title="valuuta-' + k + '"]').parent().addClass('valitud');
					} else {
						$('.hind_' + k_low).hide();

						$('span[title="valuuta-' + k + '"]').parent().removeClass('valitud');
					}
				});
			});
*/

			$('.kuva_hind-km, .kuva_hind-nokm').live('click', function(){
				var id = $(this).attr('rel').split('-')[1], b = $('span[rel^="kuva_hind-"]');

				$(b).each(function(i){
					var k = $(b[i]).attr('rel').split('-')[1],
						k_low = k.toLowerCase();

					if( k == id ){
						HIND_KM_TYYP = k_low;
						$('.hind_' + k_low).show();
						$('span[rel="kuva_hind-' + k + '"]').parent().addClass('valitud');
					} else {
						$('.hind_' + k_low).hide();
						$('span[rel="kuva_hind-' + k + '"]').parent().removeClass('valitud');
					}
				});
			});

			$('.kuva_osthind, .kuva_rendihind').live('click', function(){
				var t = $(this).attr('class'), m = $('span[rel^="kuva_hind-"]');

if(window.console) console.log('#ost/rent hind: ', $(this), '|', t, '|', m);

				$(m).each(function(i){
					var txt = $(m[i]).attr('title').split('-')[1].toLowerCase();

					if( $('.hind_' + txt).attr('style') && $('.hind_' + txt).attr('style') == 'hidden'){}
					else{

						if( t == 'kuva_rendihind' ){

							$('.osthind_' + txt).hide();
							$('.rendihind_' + txt).show();

							$('.calc-ost-res').hide();
							$('.calc-rent-res').show();

							HIND_TYYP = 'rent';

							$('#hw_hinnakiri_valikud li span.kuva_osthind, .ost_rent_btn li span.kuva_osthind').parent().removeClass('valitud');
							$('#hw_hinnakiri_valikud li span.kuva_rendihind, .ost_rent_btn li span.kuva_rendihind').parent().addClass('valitud');

						} else if( t == 'kuva_osthind'){

							$('.rendihind_' + txt).hide();
							$('.osthind_' + txt).show();

							$('.calc-rent-res').hide();
							$('.calc-ost-res').show();

							HIND_TYYP = 'ost';

							$('#hw_hinnakiri_valikud li span.kuva_rendihind, .ost_rent_btn li span.kuva_rendihind').parent().removeClass('valitud');

							$('#hw_hinnakiri_valikud li span.kuva_osthind, .ost_rent_btn li span.kuva_osthind').parent().addClass('valitud');
						}
					}
				})
			});

			// @28.09.2010
			$('input[name="kombi_valik"]').click(function(){
				var kas_m2rgitud=$(this).is(':checked'), next_elm=$(this).parents().find('tr')[1];
				if(next_elm){
					var n = $(next_elm).find('input[type="checkbox"]');
					if(n){
						n_id = $(n).attr('id').split('_')[1];

						$(n).attr('checked', kas_m2rgitud);
						(kas_m2rgitud ? $(n).attr('disabled', 'disabled') : $(n).removeAttr('disabled'));
					}
				}
			});

			$().arvuta_hinnad_k6ik_kokku();

			// töökohad
			if( $('tr[class^="tookohad-"]').length > 0 ){
				var ires = '', 
					tr_id = $('tr[class^="tookohad-"]').attr('id').split('_')[2], 
					a = [], 
					ex, 
					s = [], 
					c = 0,
					kogus = 0,
					v22rtus = '';

				$('input[id^="slider-data-"]').each(function(item){
					ex = $(this).val().split('|');
					a.push([ex[0], ex[1], ex[2]]);
				});

				// kuna a[]-l on nüüd ka sisu sees, siis loeme selle masiivi pikkuse kokku
				c = a.length;

				for(i in a){
					if(i == 0){
						var first = a[i][0].split('-')[0];
					}
					if( (c-1)==i ){
						var last = a[i][0].split('-')[1];
					}

					var e=a[i][0].split('-');
					s.push([e[0], e[1], a[i][1], a[i][2]]);
				}

				$('#slider-result').slider({
					range: "min",
					min: parseInt(first),
					max: parseInt(last),
					slide: function(event, ui){
						kogus = ui.value;

						for(j in s){
							/*
								0: <nimetus>, nt. 1-3
								1: <hind>
								2: <rendihind>
							*/
							if(kogus>=s[j][0] && kogus<=s[j][1]){
								// määrame uue info hw_data[<id>]-le
								v22rtus = '0|' + tr_id + '|Töökohti ' + kogus + '|' + kogus + '|' + kogus*s[j][2] + '|' + kogus*s[j][3];

								$('input[name="hw_data\\[' + tr_id + '\\]"]').val(v22rtus);

								if( kogus > 0 ){
									if($('#m2rkeruut_' + tr_id).is(':checked')){}
									else {
										$('#m2rkeruut_' + tr_id).attr('checked', 'checked');
									}
								} else {
									// kogus on null, võtame märkeruudu märgistuse maha
									$('#m2rkeruut_' + tr_id).removeAttr('checked');
								}

								$('#hw_rida_' + tr_id).trigger('click');
							}
						}

						$('input[name="slider-res-txt"]').val(kogus);
					}
				});
				//});//jquery-ui
			}// tr.tookohad

			// @added: 6.10.2010
			$('#calc-content-left input[id="hw_tyhista_nupp"]').click(function(){
				$('#calc-content-left')
					.slideRightHide()
					.css('z-index', -1);

				$('#calc-content span[id="telli_nupp"]').show();

				// calc-content-left form:input.val -> reset()
			});

			$('#calc-content span[id="telli_nupp"]').click(function(){
				// peidame pakkumise nupu
				$(this).hide();

				// avame küljele andmete sisestusväljad
				$('#calc-content-left')
					.css('z-index', 700)
					.slideRightShow();

				$().arvuta_k6rgused();
			});

			// päringu saatmine
			$('#calc-content-left input[name="saada_hw_p2ring"]').click(function(){
				var dat = $('#hw_send_data').val(), 
					vormi_sisu = $($('#calc-content-left')[0].elements).not('#hw_send_data').serialize();

				$('.ui-dialog').remove();

				$.ajax({
					type    : 'POST',
					url     : kodulehe_aadress + 'hw_p2ring.php?r=' + Math.random(),
					data    : 'valik=' + HIND_TYYP + '&' + vormi_sisu + dat,
					cache   : false,
					success : function(msg){
						// kuvame teate
						$('#dialog_message p').html(msg);

						$('.ui-dialog').remove();

						$('#calc-content-left, #hinnakirja_tulemus').hide();
						$('#hinnakiri_p2ring_ok').show();
					},
					error: function(xhr, textStatus, errorThrown){
						$('#dialog_message p').html(xhr.responseText);

						$('.ui-dialog').remove();
						$('#dialog_message').dialog({
							modal: true,
							buttons: {
								"Ok": function(){
									$( this ).dialog('close');
								}
							},
							close: function(){
								// tühjendame teated
								$('#dialog_message p').empty();

								$('.ui-dialog').remove();
							}
						});
					}
				});
			});

			$('#calc-content').fadeIn(2000);
		}// $('#calc-content')

	}); // window.load()



	jQuery.fn.extend({
		slideRightShow: function() {
			return this.each(function() {
				$(this).show('slide', {direction: 'right'}, 2000);
			});
		},
		slideLeftHide: function() {
			return this.each(function() {
				$(this).hide('slide', {direction: 'left'}, 1000);
			});
		},
		slideRightHide: function() {
			return this.each(function() {
				$(this).hide('slide', {direction: 'right'}, 2000);
			});
		},
		slideLeftShow: function() {
			return this.each(function() {
				$(this).show('slide', {direction: 'left'}, 1000);
			});
		},
		check: function(){
			return this.filter(':radio, :checkbox').attr('checked', true);
		},
		uncheck: function(){
			return this.filter(':radio, :checkbox').removeAttr('checked');
		},
		arvuta_k6rgused: function(){
			// @added: 22.12.2010
			// @desc: kastide kõrgused

			$('#calc-content-res').removeAttr('style');
			$($('.hw_hinnakiri_kast_sisu')[0]).removeAttr('style');

			var box_a = parseFloat($($('.hw_hinnakiri_kast_sisu')[0]).height()); //#calc-content-left
			var box_b = parseFloat($('#calc-content-res').height()+130); //#hinnakirja_tulemus 121px

			if( box_a != box_b ){
				if( box_a > box_b ){
					$('#calc-content-res').css('height', parseFloat(box_a-130));
				} else {
					$($('.hw_hinnakiri_kast_sisu')[0]).css('height', box_b);
				}
			} else {
			}
		},
		arvuta_hinnad_k6ik_kokku: function(){
			$('tr[id^="hw_rida_"], span[id^="hw_res_row_del_"]').live('click', function(){
				$('#calc-content-res').empty();

				// anname kasutajale märku et me alustasime hindade kokku arvutamist ja kuvame pildi seniks

				$('#hw_algus_teade').hide();

				var check_id_val = $(this).attr('id').split('_');

				if( check_id_val[1] == 'res' ){
					var id = check_id_val[4];
				} else {
					var id = check_id_val[2];
				}

				if( $(this).hasClass('tookohad_data') ){}
				else {
					if( $('#m2rkeruut_1').is(':checked') && (id == 2) ){
					} else {
						if( $('#m2rkeruut_' + id).is(':checked') ){
							$('#m2rkeruut_' + id).attr('checked', false);
							$('#m2rkeruut_' + id).parent().removeClass('lisa_m2rkeruut');
						} else {
							$('#m2rkeruut_' + id).attr('checked', true);
							$('#m2rkeruut_' + id).parent().addClass('lisa_m2rkeruut');
						}
					}

					if( $('#m2rkeruut_1').is(':checked') ){
						$('#m2rkeruut_2').attr('checked', true).andSelf().attr('disabled', true);
					} else {
						if( $('input[id="m2rkeruut_2"]').attr('disabled') ){
							$('#m2rkeruut_2').attr('checked', false).andSelf().removeAttr('disabled');
						}
					}
				}

				var next_id, 
					kombi=$('input[name="kombi_valik"]'), 
					c=$('input[id^="m2rkeruut_"]'), 
					a=[], 
					get_id='', 
					idata='', 
					stemp=[], 
					s=[], 
					o=[], 
					s2=[],
					s3=[],
					s4=[],
					summad_kokku = 0,
					tookohad = $('input[name="tookohad"]'),
					ires = '',
					hinna_summad_kokku = 0,
					rendihinna_summad_kokku = 0,
					_euro = 15.6466;

				/* hw_data -> 0:<1|0>, 1:<id>, 2:<nimetus>, 3:<kogus, def. 1>, 4:<hind>, 5:<rendihind>, 6:<uus hind???> */
				// esmalt uurime kas "kombi_valikud" märkeruut on valitud, siis võtame selle järgne märkeruudu id ja filtreerime otsingutulemustest välja


				// nullime väärtused (summade tekstiväljades)
				$('#hw_hinnad_kokku input').val('0.00');

				if( kombi.length > 0 ){
					if( $(kombi).is(':checked') ){
						// korjame kombi valiku info
						var kv_data_id = $(kombi).attr('id').split('_')[1];
						if(kv_data_id){
							var kv_data = $('input[name="hw_data\\[' + kv_data_id + '\\]"]').val();
							if(kv_data){
								ex = kv_data.split('|');

								// lisame kirje s3 masiivi
								s3.push([ex[0], ex[1], ex[2], ex[3], ex[4], ex[5]]);

								// otsime "finantsi"-i märkeruudu
								var next_elm = $(kombi).parents().find('tr')[1];

								if(next_elm){
									var next_elm_data = $(next_elm).children().find('input[name^="hw_data"]');
									if(next_elm_data){
										ex = $(next_elm_data).val().split('|');
										next_id = parseInt(ex[1]);

										s3.push([ex[0], ex[1], ex[2], ex[3], 0, 0]);
									}
								}
							}
						}

						$('input[id^="m2rkeruut_"]').each(function(item){
							if($(this).is(':checked')){
								get_id = $(this).attr('id').split('_')[1];
								if(get_id>2){
									// küsime infot
									idata=$('input[name="hw_data\\[' + get_id + '\\]"]').val();
									if(idata){
										ex = idata.split('|');
										a.push([parseInt(ex[0]), parseInt(ex[1]), ex[2], parseInt(ex[3]), ex[4], ex[5]]);
									}
								}
							}
						});

						if(typeof a=='object' && a.length>0){
							for(i in a){
								if(a[i][0]==1){
									s.push([parseInt(a[i][0]), parseInt(a[i][1]), a[i][2], parseInt(a[i][3]), a[i][4], a[i][5]]);
								} else {
									s2.push([parseInt(a[i][0]), parseInt(a[i][1]), a[i][2], parseInt(a[i][3]), a[i][4], a[i][5]]);
								}
							}
						}

						if(typeof s != 'undefined' && typeof s == 'object' && s.length>0){
							s.sort(function(a,b){return b[4]-a[4];});

							i=0;
							for(i in s){
								var uus_hind;
								if(i<=2){
									uus_hind=0;
									s3.push([s[i][0], s[i][1], s[i][2], s[i][3], s[i][4], s[i][5], uus_hind]);
								} else {
									s3.push([s[i][0], s[i][1], s[i][2], s[i][3], s[i][4], s[i][5]]);
								}
							}
						}

						if(typeof s2 != 'undefined' && typeof s2 == 'object' && s2.length>0){
							i=0;
							for(i in s2){
								s3.push([s2[i][0], s2[i][1], s2[i][2], s2[i][3], s2[i][4], s2[i][5]]);
							}
						}

						if( (typeof s3 != 'undefined' && typeof s3 == 'object' && s3.length>0) 
							|| (typeof s4 != 'undefined' && typeof s4 == 'object' && s4.length>0) 
						){
							i=0;
							ires = '';
							var percentage_val = [], 
								percentage_val2 = [], 
								tmp_summa = 0, 
								hw_send_data = '', 
								loendur = 0, 
								u_hind = 0, 
								u_rhind = 0;

							for(i in s3){
								loendur++;
								/*
									0: <1|0> (grupp)
									1: <id>
									2: <nimetus>
									3: <kogus>
									4: <hind>
									5: <rendihind>
									6: <uus hind|undefined(false)>
								*/
								if( typeof s3[i][6] == 'undefined' && (s3[i][4]>=0) && (s3[i][5]>=0) ){
									var n_hind = s3[i][4];
									var n_rendihind = s3[i][5];
								} else {
									var n_hind = s3[i][6];
									var n_rendihind = s3[i][6];
								}

								// kontrollime kas on protsent
								if(typeof s3[i][4] == 'string' )
								{
									var hind_pros = s3[i][4].split('%');
								}

								if( typeof s3[i][5] == 'string' )
								{
									var rhind_pros = s3[i][5].split('%');
								}

								u_hind = (typeof hind_pros == 'object' && hind_pros.length > 1 ? 0 : n_hind);
								u_rhind = (typeof rhind_pros == 'object' && rhind_pros.length > 1 ? 0 : n_rendihind);

								// reavärvi vahetus
								row_color = (loendur%2 == 1 ? ' style="border-bottom:1px #CCC solid;"' : '');

								ires += '<div id="hw_res_row_' + s3[i][1] + '">' +
											'<div class="hw_rida_vasakule">' +
												'<span class="jrk">' + loendur + '</span>' + 
												'<span class="nimetus">' + s3[i][2] + '</span>' + 
												'<br class="clear" />' + 
											'</div>' +
											'<span class="hind_km">' + 
												'<span class="osthind_km">' + arvuta_raha_kokku(u_hind*HIND_KM) + '</span>' + 
												'<span class="rendihind_km">' + arvuta_raha_kokku(u_rhind*HIND_KM) + '</span>' + 
											'</span>' + 
											'<span class="hind_nokm">' + 
												'<span class="osthind_nokm">' + u_hind + '</span>' + 
												'<span class="rendihind_nokm">' + u_rhind + '</span>' + 
											'</span>' + 
											'<span id="hw_res_row_del_' + s3[i][1] + '" class="del"><span>&nbsp;</span></span>' + 
											'<br class="clear" />' + 
										'</div>';

								if( typeof s3[i][4] == 'string' && hind_pros.length > 1 ){
									// on protsent
									percentage_val.push({id:s3[i][1], val:parseFloat(hind_pros[0])});

								} else {
									hinna_summad_kokku += parseFloat(n_hind);
								}

								if( typeof s3[i][5] == 'string' && rhind_pros.length > 1 ){
									percentage_val2.push({id:s3[i][1], val:parseFloat(rhind_pros[0])});
								} else {
									rendihinna_summad_kokku += parseFloat(n_rendihind);
								}

								hw_send_data += '&qdata[' + s3[i][1] + '][name]=' + encodeURIComponent(s3[i][2]) + 
									'&qdata[' + s3[i][1] + '][qty]=' + s3[i][3] + 
									'&qdata[' + s3[i][1] + '][price]=' + (typeof hind_pros=='object' && hind_pros.length>1 ? s3[i][4] : n_hind) + 
									'&qdata[' + s3[i][1] + '][rprice]=' + (typeof rhind_pros=='object' && rhind_pros.length>1 ? s3[i][5] : n_rendihind);
							}

							// hindade kokku arvutamine on lõpule jõudnud, peidame animatsiooni

							$(ires).hide().appendTo('#calc-content-res').fadeIn(2000);

							$().arvuta_k6rgused();

							if( percentage_val.length > 0 ){
								tmp_summa = hinna_summad_kokku;

								j = 0;
								for(j in percentage_val){
									tmp_price = tmp_summa;
									hinna_summad_kokku = parseFloat(percentage_val[j].val*tmp_price/100);

									if( $('#hw_res_row_' + percentage_val[j].id).length > 0 ){
										//$('#hw_res_row_' + percentage_val[j].id + ' span.price').html(hinna_summad_kokku + ' EEK (' + arvuta_raha_kokku(hinna_summad_kokku/_euro) + ' &euro;)');

										$('#hw_res_row_' + percentage_val[j].id + ' span.price').html(hinna_summad_kokku + '&euro;');
									}

									hinna_summad_kokku = parseFloat(tmp_summa + hinna_summad_kokku);
								}
							}

							if( percentage_val2.length > 0 ){
								tmp_summa = rendihinna_summad_kokku;

								j = 0;
								for(j in percentage_val2){
									tmp_rprice = tmp_summa;
									rendihinna_summad_kokku = parseFloat(percentage_val2[j].val*tmp_rprice/100);

									if( $('#hw_res_row_' + percentage_val2[j].id).length > 0 ){
										/*
										$('#hw_res_row_' + percentage_val2[j].id + ' span.rprice').html(rendihinna_summad_kokku + ' EEK (' + arvuta_raha_kokku(rendihinna_summad_kokku/_euro) + ' &euro;)');
										*/
										$('#hw_res_row_' + percentage_val2[j].id + ' span.rprice').html(rendihinna_summad_kokku + ' &euro;');
									}

									rendihinna_summad_kokku = parseFloat(tmp_summa + rendihinna_summad_kokku);
								}
							}


							// ostuhind
							if(typeof hinna_summad_kokku != 'undefined'){
								// käibemaks
								var h_ost_k2ibemaks = parseFloat(hinna_summad_kokku*20/100);
								$('#calc-content input[name="osthind_k2ibemaks_km"]').val( h_ost_k2ibemaks );
								$('input[name="osthind_k2ibemaks_nokm"]').val( (h_ost_k2ibemaks/_euro).toFixed(2) );

								// summa
								var h_ost_summad = parseFloat(hinna_summad_kokku - h_ost_k2ibemaks);
								$('#calc-content input[name="osthind_summad_km"]').val( h_ost_summad );
								$('input[name="osthind_summad_nokm"]').val( (h_ost_summad/_euro).toFixed(2) );

								// summad kokku
								$('#calc-content input[name="osthind_summad_koos_km"]').val( hinna_summad_kokku );
								$('input[name="osthind_summad_koos_nokm"]').val( (hinna_summad_kokku/_euro).toFixed(2) );

							}

							// summa
							if(typeof hinna_summad_kokku != 'undefined'){
								// käibemaks
								/*
								var h_rent_k2ibemaks = parseFloat(rendihinna_summad_kokku*20/100);
								$('#calc-content input[name="rendihind_k2ibemaks_eek"]').val( h_rent_k2ibemaks );
								$('input[name="rendihind_k2ibemaks_eur"]').val( (h_rent_k2ibemaks/_euro).toFixed(2) );
								*/
								var h_rent_k2ibemaks = parseFloat(rendihinna_summad_kokku*HIND_KM);
								$('#calc-content input[name="rendihind_k2ibemaks_km"]').val( h_rent_k2ibemaks );
								$('input[name="rendihind_k2ibemaks_nokm"]').val( h_rent_k2ibemaks.toFixed(2) );

								// summa
								/*
								var h_rent_summad = parseFloat(rendihinna_summad_kokku - h_rent_k2ibemaks);
								$('#calc-content input[name="rendihind_summad_eek"]').val( h_rent_summad );
								$('input[name="rendihind_summad_eur"]').val( (h_rent_summad/_euro).toFixed(2) );
								*/
								var h_rent_summad = parseFloat(rendihinna_summad_kokku - h_rent_k2ibemaks);
								$('#calc-content input[name="rendihind_summad_km"]').val( h_rent_summad );
								$('input[name="rendihind_summad_nokm"]').val( h_rent_summad.toFixed(2) );


								// summad kokku
								/*
								$('#calc-content input[name="rendihind_summad_koos_eek"]').val( rendihinna_summad_kokku );
								$('input[name="rendihind_summad_koos_eur"]').val( (rendihinna_summad_kokku/_euro).toFixed(2) );
								*/
								$('#calc-content input[name="rendihind_summad_koos_km"]').val( rendihinna_summad_kokku );
								$('input[name="rendihind_summad_koos_nokm"]').val( rendihinna_summad_kokku.toFixed(2) );
							}

							$('#hw_send_data').val( hw_send_data );
						}

					}// $(kombi).is(':checked')
					else
					{
						// kombi polnud valitud
						$('input[id^="m2rkeruut_"]').each(function(item){
							if($(this).is(':checked')){
								get_id = $(this).attr('id').split('_')[1];
								// küsime infot
								idata=$('input[name="hw_data\\[' + get_id + '\\]"]').val();
								if(idata){
									ex = idata.split('|');
									/*
										0: <1|0> - kombi valik || not
										1: <id>
										2: <nimetus>
										3: <kogus>
										4: <hind>
										5: <rendihind>
										6: <uus_hind>
									*/

									a.push([ parseInt(ex[0]), parseInt(ex[1]), ex[2], parseInt(ex[3]), ex[4], ex[5] ]);
								}
							}
						});

						if(typeof a != 'undefined' && typeof a == 'object' && a.length>0){
							i=0;
							ires = '';

							var percentage_val = [], 
								percentage_val2 = [], 
								tmp_summa = 0, 
								hw_send_data = '', 
								loendur = 0, 
								row_color = '';

							for(i in a){
								loendur++;

								// reavärvi vahetus
								row_color = (loendur%2 == 1 ? ' style="border-bottom:1px #CCC solid;"' : ' style="border-bottom:1px #EEE solid;"');

								// kontrollime kas on protsent
								if(typeof a[i][4] == 'string'){
									var hind_pros = a[i][4].split('%');
								}

								if( typeof a[i][4] == 'string' && hind_pros.length > 1 ){
									// on protsent
									percentage_val.push({id:a[i][1], val:parseFloat(hind_pros[0])});

								} else {
									hinna_summad_kokku += parseFloat(a[i][4]);
								}

								if(typeof a[i][5] == 'string'){
									var rhind_pros = a[i][5].split('%');
								}

								if( typeof a[i][5] == 'string' && rhind_pros.length > 1 ){
									percentage_val2.push({id:a[i][1], val:parseFloat(rhind_pros[0])});

								} else {
									rendihinna_summad_kokku += parseFloat(a[i][5]);
								}

								var k_hind = (typeof percentage_val != 'undefined' && percentage_val > 0 ? 0 : a[i][4]);
								var k_rhind = (typeof percentage_val2 != 'undefined' && percentage_val2 > 0 ? 0 : a[i][5]);

								ires += '<div id="hw_res_row_' + a[i][1] + '">' +
											'<div class="hw_rida_vasakule">' +
												'<span class="jrk">' + loendur + '</span>' + 
												'<span class="nimetus">' + a[i][2] + '</span>' + 
												'<br class="clear" />' + 
											'</div>' +
											'<span class="hind_km">' + 
												'<span class="osthind_km">' + arvuta_raha_kokku(k_hind*HIND_KM) + '</span>' + 
												'<span class="rendihind_km">' + arvuta_raha_kokku(k_rhind*HIND_KM) + '</span>' + 
											'</span>' + 
											'<span class="hind_nokm">' + 
												'<span class="osthind_nokm">' + k_hind + '</span>' + 
												'<span class="rendihind_nokm">' + k_rhind + '</span>' + 
											'</span>' + 
											'<span id="hw_res_row_del_' + a[i][1] + '" class="del"><span>&nbsp;</span></span>' + 
											'<br class="clear" />' + 
										'</div>';

								hw_send_data += '&qdata[' + a[i][1] + '][name]=' + encodeURIComponent(a[i][2]) + 
									'&qdata[' + a[i][1] + '][qty]=' + a[i][3] + 
									'&qdata[' + a[i][1] + '][price]=' + a[i][4] + 
									'&qdata[' + a[i][1] + '][rprice]=' + a[i][5];
							}

							$(ires).hide().appendTo('#calc-content-res').fadeIn(2000);

							$().arvuta_k6rgused();

							if( percentage_val.length > 0 ){
								tmp_summa = hinna_summad_kokku;

								j = 0;
								for(j in percentage_val){
									tmp_price = tmp_summa;
									hinna_summad_kokku = parseFloat(percentage_val[j].val*tmp_price/100);

									if( $('#hw_res_row_' + percentage_val[j].id).length > 0 ){
										/*
										$('#hw_res_row_' + percentage_val[j].id + ' span.price')
											.html(hinna_summad_kokku + ' EEK (' + arvuta_raha_kokku(hinna_summad_kokku/_euro) + ' &euro;)');
										*/
										$('#hw_res_row_' + percentage_val[j].id + ' span.price')
											.html( hinna_summad_kokku + ' &euro;' );
									}

									hinna_summad_kokku = parseFloat(tmp_summa + hinna_summad_kokku);
								}
							}

							if( percentage_val2.length > 0 ){
								tmp_summa = rendihinna_summad_kokku;

								j = 0;
								for(j in percentage_val2){
									tmp_rprice = tmp_summa;
									rendihinna_summad_kokku = parseFloat(percentage_val2[j].val*tmp_rprice/100);

									if( $('#hw_res_row_' + percentage_val2[j].id).length > 0 ){
										/*
										$('#hw_res_row_' + percentage_val2[j].id + ' span.rprice')
											.html(rendihinna_summad_kokku + ' EEK (' + arvuta_raha_kokku(rendihinna_summad_kokku/_euro) + ' &euro;)');
										*/

										$('#hw_res_row_' + percentage_val2[j].id + ' span.rprice')
											.html(rendihinna_summad_kokku + ' &euro;');
									}

									rendihinna_summad_kokku = parseFloat(tmp_summa + rendihinna_summad_kokku);
								}
							}

							// ostuhind
							if(typeof hinna_summad_kokku != 'undefined'){
								// käibemaks
								var h_ost_k2ibemaks = parseFloat(hinna_summad_kokku*20/100);
								$('#calc-content input[name="osthind_k2ibemaks_km"]').val( h_ost_k2ibemaks );
								$('input[name="osthind_k2ibemaks_nokm"]').val( (h_ost_k2ibemaks/_euro).toFixed(2) );

								// summa
								var h_ost_summad = parseFloat(hinna_summad_kokku - h_ost_k2ibemaks);
								$('#calc-content input[name="osthind_summad_km"]').val( h_ost_summad );
								$('input[name="osthind_summad_nokm"]').val( (h_ost_summad/_euro).toFixed(2) );

								// summad kokku
								$('#calc-content input[name="osthind_summad_koos_km"]').val( hinna_summad_kokku );
								$('input[name="osthind_summad_koos_nokm"]').val( (hinna_summad_kokku/_euro).toFixed(2) );
							}

							// summa
							if(typeof hinna_summad_kokku != 'undefined'){
								// käibemaks
								var h_rent_k2ibemaks = parseFloat(rendihinna_summad_kokku*20/100);
								$('#calc-content input[name="rendihind_k2ibemaks_km"]').val( h_rent_k2ibemaks );
								$('input[name="rendihind_k2ibemaks_nokm"]').val( (h_rent_k2ibemaks/_euro).toFixed(2) );

								// summa
								var h_rent_summad = parseFloat(rendihinna_summad_kokku - h_rent_k2ibemaks);
								$('#calc-content input[name="rendihind_summad_km"]').val( h_rent_summad );
								$('input[name="rendihind_summad_nokm"]').val( (h_rent_summad/_euro).toFixed(2) );

								// summad kokku
								$('#calc-content input[name="rendihind_summad_koos_km"]').val( rendihinna_summad_kokku );
								$('input[name="rendihind_summad_koos_nokm"]').val( (rendihinna_summad_kokku/_euro).toFixed(2) );

							}

							$('#hw_send_data').val(hw_send_data);
						}
					}

					if( $('div[id^="hw_res_row_"]').length > 0 ){
						$('#hw_hinnad_kokku').show();

						if( $('#calc-content span[id="telli_nupp"]').is(':visible') ){}
						else
						{
							$('#calc-content span[id="telli_nupp"]').show();
						}
					} else {
						$('#hw_hinnad_kokku, #calc-content span[id="telli_nupp"]').hide();
						$('#hw_algus_teade').show();
					}

					/*
					if( HIND_YHIK == 'eek' ) $('.kuva_hind_eek').trigger('click');
					if( HIND_YHIK == 'eur' ) $('.kuva_hind_eur').trigger('click');
					*/
					if( HIND_KM_TYYP == 'km' ) $('.kuva_hind-km').trigger('click');
					if( HIND_KM_TYYP == 'nokm' ) $('.kuva_hind-nokm').trigger('click');

					if( HIND_TYYP == 'ost' ) $('.kuva_osthind').trigger('click');
					if( HIND_TYYP == 'rent' ) $('.kuva_rendihind').trigger('click');
				}


			});
		}
		// \@24.09.2010
	});



	function scroll_top(){
		return f_filterResults (
			window.pageYOffset ? window.pageYOffset : 0,
			document.documentElement ? document.documentElement.scrollTop : 0,
			document.body ? document.body.scrollTop : 0
		);
	}

	function f_filterResults(n_win, n_docel, n_body) {
		var n_result = n_win ? n_win : 0;
		if (n_docel && (!n_result || (n_result > n_docel)))
			n_result = n_docel;
		return n_body && (!n_result || (n_result > n_body)) ? n_body : n_result;
	}

	/*asukoha_kaart(aadress);*/
	function asukoha_kaart(x, y, m_data, icon_image, layer_id)
	{
		$.getScript(kodulehe_aadress + 'js/markerclusterer.js', function(){
			var center = new google.maps.LatLng(x, y);
			var options = {
				zoom: 7,
				center : center,
				mapTypeControl: true,
				mapTypeControlOptions: {
					style: google.maps.MapTypeControlStyle.DROPDOWN_MENU, 
					position: google.maps.ControlPosition.TOP_RIGHT
				},
				mapTypeId: google.maps.MapTypeId.ROADMAP
			};

			var map = new google.maps.Map(document.getElementById(layer_id), options);
			var markers = [];
			//var infoWindow = new google.maps.InfoWindow({});

			if( icon_image == 'none' || !icon_image ){}
			else
			{
				var image = new google.maps.MarkerImage(icon_image, 
				  // This marker is 20 pixels wide by 32 pixels tall. 
				  new google.maps.Size(32, 32), 
				  // The origin for this image is 0,0. 
				  new google.maps.Point(0,0), 
				  // The anchor for this image is the base of the flagpole at 0,32. 
				  new google.maps.Point(0, 32));
			}

			for(var i=0;i<m_data.length;i++)
			{
				var infoWindow = new google.maps.InfoWindow({});

				var bodytxt = ( m_data[i].info == '' ? m_data[i].title : m_data[i].info.replace(/&amp;/g,'&').replace(/&lt;/g,'<').replace(/&gt;/g,'>').replace(/&quot;/g, '"') );
				var latLng = new google.maps.LatLng(m_data[i].latitude, m_data[i].longitude);

				if( icon_image == 'none' || !icon_image )
				{
					var marker = new google.maps.Marker({
						position:latLng,
						title:m_data[i].title,
						draggable:false
					});
				}
				else
				{
					var marker = new google.maps.Marker({
						position:latLng,
						title:m_data[i].title,
						icon:image,
						draggable:false
					});
				}

				infoWindowHtml = '<div class="gmap_sisu" style="height:' + (bodytxt.length+10) + 'px;">' + bodytxt + '<\/div>';
				infoWindow.setContent(infoWindowHtml);
				infoWindow.open(map, marker);

				markers.push(marker);
			}

			var mc = new MarkerClusterer(map, markers);
		});
	}
}


/*
	Abiunktsioonid
	==============
*/
/*
	uus popup aken
	--------------
*/
function uus_popup_aken(link, laius, korgus)
{
	if(!laius) var laius = 800; // 770
	if(!korgus) var korgus = 600; // 650

	var left = parseInt((screen.availWidth/2) - (laius/2));
	var top = parseInt((screen.availHeight/2) - (korgus/2));

	window.open(link, '_blank', 
		'scrollbars=yes,toolbar=no,location=no,status=no,width=' + laius + ',height=' + korgus 
		+ ',screenX=' + left + ',screenY=' + top);
};


function arvuta_raha_kokku(summa)
{
	var raha=Math.floor(summa);
	var sendid=Math.round((summa % 1)*100);
	if(sendid<10)sendid = '0' + sendid;
	return(raha+'.'+sendid)
}



