//registreerumis vormi funktsioonid
$(document).ready(function(){
	
	if($("#id_13").is(":checked") == true){
		$("#juriidiline").show();
	}
	if($("#id_tarn").is(":checked") == true){
		$("#tarnekas").show();
	}	
	
	$("#id_14").click(function () {
		$("#juriidiline").hide();
	});
	
	$("#id_13").click(function () {
		$("#juriidiline").show();
	});
	
	$("#id_tarn").click(function () {
		$("#tarnekas").toggle();
	});
	
	$("#nous2").click(function () {
		if($("#nous2").is(":checked") == true){
			$("#rega_mind").attr('disabled', false);
		}else{
			$("#rega_mind").attr('disabled', true);
		}
	});
	
	$(".lahter_reg").focus(function(){
		$(this).addClass("lahter_reg_act");
		$(this).removeClass("lahter_reg");
	});
	
	$(".lahter_reg").blur(function(){
		$(this).addClass("lahter_reg");
		$(this).removeClass("lahter_reg_act");
	});
});

//menüü kinni lahti vahetamine
$(document).ready(function(){
	var a;
	a = readCookie('menu');
	
	if(a == 1){
		mini_header();
	} else {
		normal_header();
	}

	$("#img_up").click(function () {
		mini_header();
	});

	$("#img_down").click(function () {
		normal_header();
	});

	function normal_header(){
		$("#miniheader, #img_down").hide();
		$("#header, #img_up").show();
		
		eraseCookie('menu');
	}

	function mini_header(){
		$("#header, #img_up").hide();
		$("#miniheader, #img_down").show();
		
		createCookie('menu',1,1);
	}
});


//ostukorvis
$(document).ready(function(){
	//ridade värvija
	$(".triip_ostuk").hover(function(){
		$(this).addClass("triip_active_woh");
	},function(){
		$(this).removeClass("triip_active_woh");
	});
});

//minu tellimused
$(document).ready(function(){

	//ridade värvija
	$(".order_row").hover(function(){
		$(this).addClass("triip_active");
	},function(){
		$(this).removeClass("triip_active");
	});
	
	//minu tellimused klick
	$(".order_row").click(function () {
		if($(this).next().is(".open_order") === true){
			jumpToAnchor('');
			$(".open_order").remove();
		}else{
			var nr = parseInt($(this).attr("id"));
			jumpToAnchor(nr);
			$(".open_order").remove();
			$(this).after('<tr class="open_order"><td class="open_order_cont" colspan="7" align="center">Loading ...</td></tr>');
			$(".open_order_cont").load('avat_tellim.php', { id: nr });
		}
	});
	
	//automaatselt avatav tellimus!?
	
	var file = script_name();
	var avatud = unescape(self.document.location.hash.substring(1)); // ankur
	var koht = document.getElementById(avatud+'_order');
	if(file == 'tellimused' && avatud != '' && koht != null){
		$('#'+avatud+'_order').trigger('click');
	}

});

function jumpToAnchor(an) {
	window.location = String(window.location).replace(/\#.*$/, "") + "#"+an;
}

// tagastab scripti faili nime URL-ilt
function script_name(){
	url = unescape(self.document.location.pathname.substring(1));
	url_array = url.split("/");
	
	var x = url_array.length-1;
	if(x >= 0){
		temp = url_array[x].split(".");
		return temp[0];
	}
	return '';
}

//korvi_mess
$(document).ready(function(){
	
	$("#lisainf").focus(function(){
		$(this).addClass("focus");
	});
	
	$("#lisainf").blur(function(){
		$(this).removeClass("focus");
		
		$.post('ostukorv_act.php?act=save_message', { lisa_inf: $("#lisainf").val() });
	});
});





$(document).ready(function(){

	if ( $.browser.msie && (parseInt($.browser.version, 10) == 6 || parseInt($.browser.version, 10) == 8) ) {
		//@author: Ewen Elder <glomainn at yahoo dot co dot uk> <ewen at jainaewen dot com>
		// Set the width via CSS.	
		$("select.menu").ieSelectWidth({
			containerClassName : 'select-container',
			overlayClassName : 'select-overlay'
		});
	}

	$.fn._ie_select=function() { 
		
		return $(this).each(function() { 
		
		  var a = $(this),
			  p = a.parent();
			
		  p.css('position','relative');
			
		  var o = a.position(),
			  h = a.outerHeight(),
			  w = a.outerWidth(),
			  l = o.left,
			  t = o.top;
				
		  var c = a.clone(true);
			
		  $.data(c,'element',a);
			
		  c.css({
			zIndex   : 100,
			height   : h,
			top      : t,
			left     : l,
			position : 'absolute',
			width : 'auto',
			opacity  : 0
		  }).attr({
			id    : this.id + '-clone'
		  }).change(function() {
			$.data(c,'element')
			  .val($(this).val())
			  .trigger('change')
		  });
		  
		  a.before(c);
		  
		  if(c.width() < w){
			c.css({
				width : w+'px'
			});
		  }
		  
		  a.before(c).click(function() {
			c.trigger('click');
		  });
		  
		  
		}); // END RETURN
		
	}; // END PLUGIN

	if ($.browser.msie &&  parseInt($.browser.version, 10) == 7) {
		$('select.menu')._ie_select();
	}
	
});




//kinni lahti käiv tootepuu.
$(document).ready(function(){
	
	//vastavalt klikkimisele avame ja suleme ridu
	$("span.puuleht").click(function () {
		
		if($(this).parent("li.oks").next().is(":hidden") === true){
			$(this).parent("li.oks").next().show();
			
			$(this).parent("li.oks").addClass("avatud");
			$(this).parent("li.oks").removeClass("suletud");
		}else{
			$(this).parent("li.oks").next("ul").hide();
			
			$(this).parent("li.oks").addClass("suletud");
			$(this).parent("li.oks").removeClass("avatud");			
		}
		
	});

	$("li.oks").hover(
		function () {
			$(this).css('background', 'yellow');
	}, 
		function () {
			$(this).css('background', '');
	});
	
	$("#open_all").click(function () {
		$(".puuoks,.oks").addClass("avatud");
		$(".puuoks,.oks").removeClass("suletud");
		
		$(".puuoks,.oks").show();
	});
	
	$("#close_all").click(function () {
		$(".puuoks").hide();
		$(".puuoks,.oks").addClass("suletud");
		$(".puuoks,.oks").removeClass("avatud");
		$(".level_0").show();
	});
	
});



//reageerib seonduvatele toodetele.<div style="display: none;" id="seonduvad" class="boxsisu_2">
$(document).ready(function(){
	$("[href='#show']").click(function(){
		$("#seonduvad tr:hidden").show();
		$("#seonduvad").toggle();
	});
	$("[href='#show_0']").click(function(){
		$("#seonduvad tr:hidden").show();
		$("#seonduvad").show();
	});
	$("[href='#show_1']").click(function(){
		$("#seonduvad tr").hide();
		$("#seonduvad tr:first").show();
		$("tr.row_class_1").show();
		$("#seonduvad").show();
	});
	/*
	$("[href='#show_2'], [href='#show_4']").click(function(){
		$("#seonduvad tr").hide();
		$("#seonduvad tr:first").show();
		$("tr.row_class_2").show();
		$("tr.row_class_4").show();
		$("#seonduvad").show();
	});
	*/
	$("[href='#show_2']").click(function(){
		$("#seonduvad tr").hide();
		$("#seonduvad tr:first").show();
		$("tr.row_class_2").show();
		$("#seonduvad").show();
	});
	$("[href='#show_3']").click(function(){
		$("#seonduvad tr").hide();
		$("#seonduvad tr:first").show();
		$("tr.row_class_3").show();
		$("#seonduvad").show();
	});
	$("[href='#show_4']").click(function(){
		$("#seonduvad tr").hide();
		$("#seonduvad tr:first").show();
		$("tr.row_class_4").show();
		$("#seonduvad").show();
	});
	$("[href='#show_5']").click(function(){
		$("#seonduvad tr").hide();
		$("#seonduvad tr:first").show();
		$("tr.row_class_5").show();
		$("#seonduvad").show();
	});
	/*
	if(unescape(self.document.location.hash.substring(1)) == 'show_2' || unescape(self.document.location.hash.substring(1)) == 'show_4'){
		$("#seonduvad tr").hide();
		$("#seonduvad tr:first").show();
		$("tr.row_class_2").show();
		$("tr.row_class_4").show();
		$("#seonduvad").show();
	}
	*/
});



$(document).ready(function(){
	
	//trasport valitud
	var x_value = $('input[name="transp"]').filter(':checked').length;
	var n = document.getElementById('next');
	if(n != null){
		if(x_value > 0){
			document.getElementById('next').disabled = false;
		}else{
			document.getElementById('next').disabled = true;
		}
	}
});


function select_all(lyliti, lambid_silmale, lambid_varjus){

	if(document.getElementById(lyliti).checked==true){
		$('input[name^="'+lambid_silmale+'"]').attr('checked', true);
		if (lambid_varjus !== undefined ) {
			$('input[name^="'+lambid_varjus+'"]').attr('checked', true);
		}
	}else{
		$('input[name^="'+lambid_silmale+'"]').attr('checked', false);
		if (lambid_varjus !== undefined ) {
			$('input[name^="'+lambid_varjus+'"]').attr('checked', false);
		}
	}
}

// reg form
$(document).ready(function(){
	
	var tegevus = $("#id_teg option:selected").val();
	if(tegevus == 1){
		$(".extra_1").show();
	}

	$("#id_teg").change(function(){
		if($("#id_teg option:selected").val() == 1){
			$(".extra_1").show();
			$("#teg_2").focus();
		}else{
			$(".extra_1").hide();
			$("#teg_2").val('');
		}
	});
	
});
this.trclicker = function(){
	$("td").click(function () {
		$("tr").removeClass("clicked_tr");
		$(this).parent("tr").addClass("clicked_tr");
	});
}
// otsing
$(document).ready(function(){

	$(".ext_srch").click(function () {
		
		var x = $("#id_o1").val();
		var $dialog = $('<div id="otsi123"></div>').load('acar_ajax/otsing_2.0.php', { src: x }, 
			function(){
				$dialog.dialog({
					title: x,
					modal: true,
					width: 900
				});
				imagePreview();
				trclicker();
				price_config();
			}
		);
		
	});

});

$(document).ready(function(){
	$(".ret_false").click(function () {
		return false;
	});
});


$(".ret_false").live('click', function() {
	return false;
});

$(".like, .notlike").live('click', function() {
	var v = $(this).attr('val');
	var r = $(this).parent().attr('rate');
	
	$(this).parent().load('acar_ajax/save_rating.php', { rate: r, hinne: v });
});



$(document).ready(function(){
	
	$(".price_setup_dialog").click(function () {
		$(".setup_price:last").dialog({
			title: 'Configuration',
			modal: true,
			width: 'auto'
		});	
	});
	
	price_config();

});

function price_config(){
	var c = $('#client_id').attr('title');
	if(readCookie(c+'hind1') == 1){		
		$(".pr_conf_1:last").attr('checked', true);
	}else if(readCookie(c+'hind1') == 0){
		$(".pr_conf_1:last").attr('checked', false);
	}

	if(readCookie(c+'slett') == 1){		
		$(".pr_conf_2:last").attr('checked', true);
	}else if(readCookie(c+'slett') == 0){
		$(".pr_conf_2:last").attr('checked', false);
	}
	
	// linnukeste pealelugemine
	if($(".pr_conf_1:last").is(":checked") == true){
		$('.hind1').removeClass("display_off");
	}else{
		$('.hind1').addClass("display_off");
	}
	if($(".pr_conf_2:last").is(":checked") == true){
		$('.slett').removeClass("display_off");
	}else{
		$('.slett').addClass("display_off");
	}
}

$(".pr_conf_1").live('click', function() {
	var c = $('#client_id').attr('title');
	// kontrollime kas linnuke on sees või väljas
	if($(this).is(":checked") == true){
		$('.hind1').removeClass("display_off");
		createCookie(c+'hind1', 1, 999);
	}else{
		$('.hind1').addClass("display_off");
		createCookie(c+'hind1', 0, 999);
	}
});
$(".pr_conf_2").live('click', function() {
	var c = $('#client_id').attr('title');
	// kontrollime kas linnuke on sees või väljas
	if($(this).is(":checked") == true){
		$('.slett').removeClass("display_off");
		createCookie(c+'slett', 1, 999);
	}else{
		$('.slett').addClass("display_off");
		createCookie(c+'slett', 0, 999);
	}
});

