
/**********JQUERY****************/
$(document).ready(function(){
	/*************view big photo**********/
	var u1=document.URL;
	var u2=document.location.pathname;
	url=u1.replace(u2,'');
	$('a.view').fancyzoom({Speed:400,showoverlay:false});
	var thiss='';
	filter_set($,thiss);

	function getFilters($){
		var par='';
		var param='';
		$("#form_filters").find("input[checked],input[type='text'] ").each(function(){
			if (this.value!='') {
				par+=this.name+'='+this.value+'&';
			}

		});
		if (par!='') {
			par+='filter=1';
		}
		return par;
	};

	/********pager recurs function***********/
	function ontest(thiss,$){
		$('span.pager a').click(function(){
			var href = $(this).attr('href');
			var len=href.indexOf('?')+1;
			var param=href.substr(len,href.length);
			var ff=getFilters($);
			if (ff!='') {
				param+='&'+ff;
			}
			$('#keyword').focus();
			$('#load_data').css('display','block');

			var ansv=$.ajax({
				type: "GET",
				url: url+"/index.php",
				data: param,
				success: function(msg){
					$('#load_data').css('display','none');
					$('#filters_ajax_data').html('');
					$('#filters_ajax_data').html(msg);
					select_checkbox_from_select_tov($,this);
					kak_ugodno($);
					filter_set($,this);
					ontest(this,$);
					selektTov(thiss,$);
				}
			});
			return false;
		});
	};
	/******** End pager recurs function***********/

	function show_down_select_tov($){
		$("#sr_field, #sravnit_submit_ajax, #table_select_vov, #redirect_submit_ajax").slideDown("slow");
		return true;
	};

	function hide_up_select_tov($){
				$("#sr_field, #sravnit_submit, #table_select_vov, #redirect_submit_ajax").slideUp("slow");
		return true;
	};

	function select_checkbox_from_select_tov($,thiss){
		$("#table_select_tov").find("tr").each(function(){
			//alert(this.id);
			var id=this.id;
			var idd=id.substr(4,id.length);
			//alert(idd);

			$("td.sub_brend input[type=checkbox]").each(function(){
				if(this.id==idd){
					//alert(this.id);
					$(this).attr("checked","checked");
				}
			});


		});
		return true;
	}

	function work_select_tov($){
		$(".del_span").click(function(){
			var tw=$(this).attr("id");
			var idd=tw.substr(4,tw.length);
			$("#tab_"+idd).remove();
			$("#"+idd).attr("checked",'')
			var html2=$("#table_select_tov").html();
			//alert(html2.length);
			if(html2.length<2){
				//$("#sr_field").slideUp("slow");
				//$("#sravnit_submit").slideUp("slow");
				hide_up_select_tov($);
			};

		});
		return true;
	}

	function selektTov(thiss,$){
		var ukrshop_img_url=url+"/admin/download/jpg_small/";
		var url='';
		var src1='';//$("#sr_field").html();
		$("td.sub_brend input[type=checkbox]").click(function(){

			var src='';
			var id=$(this).attr("id");
			var title=$(this).attr("title");
			var html2=$("#table_select_tov").html();
			if($(this).attr("checked")){
				url=ukrshop_img_url+id+".jpg";
				src="<tr id='tab_"+id+"'><td><span id='spa_"+id+"' class='del_span' style='cursor: pointer; color:red;'>"+$("#ubrat").attr('title')+"</span></td><td><img src='"+url+"'  alt='' border='0'/></td><td>"+title+"</td></tr>";

				html2+=src;
			}else{
				$("#table_select_tov").find("tr").each(function(){
					var tw=this.id;
					var idd=tw.substr(4,tw.length);

					if(idd==id){
						$("#"+tw).remove();
						html2=$("#table_select_tov").html();
					}
				});



			}
			if(html2.length<2){
				//$("#sr_field").slideUp("slow");
				//$("#sravnit_submit").slideUp("slow");
				hide_up_select_tov($);
			}else{

				$("#table_select_tov")
				.html(html2);
				show_down_select_tov($);
				//$("#sr_field");
				//.slideDown("slow");
				//$("#sravnit_submit").slideDown("slow");
			};
			work_select_tov($);
		});
		return true;
	};
	function work_cookie($,html){

	};

	function research_filters($,thiss){

		var params = {};
		var par='';
		var param='';
		$("#podobr").parent().find("input[checked], input[type='hidden'],input[type='text'] ").each(function()
		{
			par+=this.name+'='+this.value+'&';

		});
		param=par.substr(0,par.length-1);

		$.ajaxSetup({
			url: url+"/index.php",
			timeout: 3000,
			type: "GET"
		});


		//$.cookie('filters',param);  //загоняем в куки фильтры
		$('#keyword').focus();
		$('#load_data').css('display','block');
		var ansv=$.ajax({
			type: "GET",
			url: url+"/index.php",
			data: param,
			success: function(msg){
				$('#load_data').css('display','none');
				$('#filters_ajax_data').html('');
				$('#filters_ajax_data').html(msg);
				var thiss=this;

				filter_set($,thiss);
				ontest(thiss,$);
				//selektTov(thiss,$);
			}
		});
		return false;

	}
	function filter_set($,thiss){

		//$("#form_filters").click(function(){
		var fil='';
		$("#form_filters").find("input[checked]").each(function(){
			fil+="<input checked='checked' id='id_"+$(this).attr("id")+"' class='fil' type='checkbox' value='"+$(this).attr("value")+"' name='id_"+$(this).attr("name")+"'><span id=idd_"+$(this).attr("id")+">"+$(this).parent().text()+"</span></input>";
		});
		//alert(fil+' dfdd');
		if(fil!=''){
			$("#enable_filters")
			.html(fil)
			.slideDown("slow");
			$("#echo_filters").css("display","block");
			$("#enable_filters").find('input').click(function(){
				var id=$(this).attr('id');
				var idd=id.substr(3,id.length);
				$("#"+idd).attr("checked",'');
				$("#idd_"+idd).remove();
				$(this).remove();

				var vv=0;
				$("#form_filters").find("input[checked]").each(function(){
					vv=1;

				});
				if(vv==0){
					$("#enable_filters").slideUp("fast");
					$("#echo_filters").css("display","none");
					var thiss= $(this);
					research_filters($,thiss)
				}else{
					thiss=$(this);
					research_filters($,thiss);

				}

			});
		}else{

			$("#enable_filters").slideUp("slow");
			$("#echo_filters").css("display","none");
		};



	};
	function close_shop_info($){
		$('#shop_info').find("#close").click(function(){
			$("#shop_info").hide(1000);
		});

	}
	//-------------------------dialog-------------------
	$("a.wievshop_info").click(function(e){

		if($("#shop_info").css("display")=='block'){
			//$("#shop_info").css("display","none");
			$("#shop_info").hide(1000);
		}

		var lld=$(this).attr("id");
		var lid=lld.substr(4,lld.length);
		var lll=lld.substr(0,3);

		//$('#keyword').focus();
		//alert($(this).offset().top);


		//$("#shop_info").scrollTop($("#shop_info").scrollTop()+ e.target.scrollTop());
		if(lll=='new'){
			var param="family_id=newsone&id_cat="+lid

		}else{
			var param="family_id=wievshop&id_cat="+lid
		}
		var top=$(this).offset().top;

		var iTop = ($(window).height() ) / 2 + $(window).scrollTop();
		var ansv=$.ajax({
			type: "GET",
			url: url+"/index.php",
			data: param,
			success: function(msg){
				//alert(msg);
				$("#shop_info").show(1000);
				$("#shop_info").css({"border":"5px dotted black","top":iTop});
				$("#shop_info").html(msg);
				close_shop_info($);
			}

		});
		return false;
	});

	function kak_ugodno($){
		$('.rating_layer').each(function(i,el)
		{
			el = $(el);
			var prod_id = el.attr('prod_id');
			if ( !prod_id )
				return;
			vote_build(el,prod_id,1);
		});
		return true;
	}

	$("#new_opinion").click(function(){
		//alert('dddddddddd');return false;
		$('#keyword').focus();
		$('#load_data').css('display','block');
		var v1=$("#name").attr('value');
		v1=$.trim(v1);
		var v2=$("#opinion").attr('value');
		v2=$.trim(v2);

		//alert('ddddd'+v1);
		if(v1==''){
			alert($('#name').attr('title'));
			$('#name').focus();
			return false;
		}
		if(v2==''){
			alert($('#opinion').attr('title'));
			$('#opinion').focus();
			return false;
		}
		var param="name="+$('#name').attr('value')+"&opinion="+$('#opinion').attr('value')+"&add_otziv=1&id_prod="+$("#id_prod_form").attr('value')+"&kcaptcha="+$("#kcaptcha").attr('value')+"&other=review&ajax=1";
		//alert(param);
		var ansv=$.ajax({
			type: "GET",
			url: url+"/index.php",
			data: param,
			success: function(msg){
				//alert($("#add_otziv").attr('title'));
				$("#otziv").html(msg);

				//alert(msg+'Ok');
				$('#load_data').css('display','none');
				otziv($);
				/*$('#filters_ajax_data').html('');
				$('#filters_ajax_data').html(msg);
				var thiss=this;
				//research_filters($,this);
				//alert('sssss');
				filter_set($,thiss);
				ontest(thiss,$);
				selektTov(thiss,$);
				*/
			}
		});

		return false;

	});


	$("#all_shops_show").click(function(){
		//$("tr.all_shops").show(1000);
		$("tr.all_shops").css({"display":"table-row"});
		$("tr.all_shop_limit").css({"display":"none"});
		$(this).hide(1000);
		return false;
	});
	$("#sravnit_submit_ajax").click(function(){
		var sel_id='';
		$("#table_select_tov").find("tr").each(function(){
			var id=this.id;
			var idd=id.substr(4,id.length);
			sel_id+=idd+",";
		});
		sel_id=sel_id.substr(0,sel_id.length-1);
		//alert(sel_id);
		//var param="family_id=sravnenie&id_sravn="+sel_id;
		$("#id_sravn").attr("value",""+sel_id);
		//alert($("#id_sravn").attr("value"));
		//return false;
		/*
	var ansv=$.ajax({
			type: "GET",
			url: url+"/index.php",
			data: param,
			success: function(msg){

			//$("filters_ajax_data").html('');
			//alert(msg);
			//$("filters_ajax_data_sravn").html(msg);
			//$(body).html('kjdhfjkhsdjkhfjhsdfhks df');
			//$("filters_ajax_data_sravn").html('kjdhfjkhsdjkhfjhsdfhks df');

			//$("filters_ajax_data").html("safasdfasdfa");
			}
	});
	*/
	});

	$("#form_filters").click(function(){

		var fil='';
		$(this).find("input[checked]").each(function(){
			fil+="<input checked='checked' id='id_"+$(this).attr("id")+"' class='fil' type='checkbox' value='"+$(this).attr("value")+"' name='id_"+$(this).attr("name")+"'><span id=idd_"+$(this).attr("id")+">"+$(this).parent().text()+"</span></input>";
		});
		if(fil!=''){
			$("#enable_filters")
			.html(fil)
			.slideDown("slow");
			$("#echo_filters").css("display","block");
			$("#enable_filters").find('input').click(function(){
				var id=$(this).attr('id');
				var idd=id.substr(3,id.length);
				$("#"+idd).attr("checked",'');
				var bb=$("#idd_"+idd).hide(200);
				$("#idd_"+idd).remove();
				$(this).remove();

				var vv=0;
				$("#form_filters").find("input[checked]").each(function(){
					vv=1;
				});
				if(vv==0){
					$("#enable_filters").slideUp("fast");
					$("#echo_filters").css("display","none");

				};
				research_filters($,this);
			});
		}else{
			$("#enable_filters").slideUp("slow");
			$("#echo_filters").css("display","none");
		};

		//****************************************************************************************
	});



	var ukrshop_img_url=url+"/admin/download/jpg_small/";

	/******** sravnenie checkbox *****************/
	$("td.sub_brend input[type=checkbox]").click(function(){

			var src = '';
			var id = $(this).attr("id");
			var title = $(this).attr("title");
			var html2 = $("#table_select_tov").html();
			if ($(this).attr("checked")) {
				url_sravn=ukrshop_img_url+id+".jpg";
				src="<tr id='tab_"+id+"'><td><span id='spa_"+id+"' class='del_span' style='cursor: pointer; color:red;'>"+$("#ubrat").attr('title')+"</span></td><td><img src='"+url_sravn+"'  alt='' border='0'/></td><td>"+title+"</td></tr>";
				html2+=src;
			} else {
				$("#table_select_tov").find("tr").each(function(){
					var tw=this.id;
					var idd=tw.substr(4,tw.length);
					if (idd==id) {
						$("#"+tw).remove();
						html2=$("#table_select_tov").html();
					}
				});
			}
			if (html2.length < 2) {
				hide_up_select_tov($)
			} else {
				$("#table_select_tov").html(html2);
				show_down_select_tov($);
			};
			work_select_tov($);
		});


	/******** view filters *****************/
	$("#podobr").click(function(){
		var params = {};
		var par='';
		var param='';
		$(this).parent().find("input[checked], input[type='hidden'],input[type='text'] ").each(function()
		{
			par+=this.name+'='+this.value+'&';

		});
		param=par.substr(0,par.length-1);
		//param+=getIdBrand($);
		$.ajaxSetup({
			url: url+"/index.php",
			timeout: 3000,
			type: "GET"
		});
		$('#keyword').focus();
		$('#load_data').css('display','block');
		var ansv=$.ajax({
			type: "GET",
			url: url+"/index.php",
			data: param,
			success: function(msg){
				$('#load_data').css('display','none');
				$('#filters_ajax_data').html('');
				$('#filters_ajax_data').html(msg);
				var thiss=this;
				//research_filters($,this);
				//alert('sssss');
				filter_set($,thiss);
				ontest(thiss,$);
				selektTov(thiss,$);
			}
		});
		return false;
	});


	/********pager start***********/
	$('span.pager a').click(function(){
		var href=$(this).attr('href');
		//alert(href);
		var len=href.indexOf('?')+1;

		var param=href.substr(len,href.length);
		//setIdBrand($,param);
		var ff=getFilters($);

		if(ff!=''){
			param+='&'+ff;
		}
		//alert(param);
		$('#keyword').focus();
		$('#load_data').css('display','block');
		//alert(param);
		var ansv=$.ajax({
			type: "GET",
			url: url+"/index.php",
			data: param,

			success: function(msg){
				//alert(msg);
				$('#load_data').css('display','none');
				$('#filters_ajax_data').html('');
				$('#filters_ajax_data').html(msg);
				var thiss=this;
				kak_ugodno($);

				ontest(thiss,$);
				selektTov(thiss,$);

			}
		});

		//alert(ansv.html());
		return false;
	});

//	$('.rating_layer').each(function(i,el)
//	{
//		el = $(el);
//		var prod_id = el.attr('prod_id');
//		if ( !prod_id )
//			return;
//		vote_build(el,prod_id,1);
//	});
kak_ugodno($);
});

	vote_images = [];
	vote_images['f'] = new Image();vote_images['f'].src = '/img/star_f.gif';
	vote_images['h'] = new Image();vote_images['h'].src = '/img/star_h.gif';
	vote_images['e'] = new Image();vote_images['e'].src = '/img/star_e.gif';
	vote_images['o'] = new Image();vote_images['o'].src = '/img/star_o.gif';

	vote_msgs = ['без рейтинга','1','2','3','4','5'];

	vote_url = '/vote.php';
	function vote_build(div,id,bevents,text)
	{
		var param = "act=get&prod_id="+id;
		var ansv=jQuery.ajax({
				type: "GET",
				url: vote_url,
				data: param,
				success: function(msg)
				{
					vote_display(div,msg,bevents,text)
				}
		});
	}


	function vote_letter(i,rating)
	{
			if ( i <= rating-1 )
				return 'f';
			if (i+0.5 <= rating )
				return 'h';
			return 'e';
	}

	function vote_display(div,msg,bevents,text)
	{
		var html = '';
		if ( div.attr('built') )
		    return;
		text = text || msg;
		var rating = msg;
		var divhtml = div.html();
		var divhtml = divhtml.replace(/%R/,
		'<div class="rating_podskazka" style="display:inline;height:20px;">'+rating+'</div><br>');
		html += divhtml;
		for ( var i = 0 ; i < 5 ; i++ )
		{
//			html += '<img '+
//				(bevents?'onMouseOver=\'vote_select("'+div.attr('id')+'",'+i+')\' onMouseOut=\'vote_unselect("'+div.attr('id')+'",'+msg+')\' onClick=\'vote_send("'+div.attr('id')+'",'+i+')\'':'')+
//				' src="'+vote_images[vote_letter(i,msg)].src+'" align="absmiddle" border="0">';
			html += '<img src="'+vote_images[vote_letter(i,msg)].src+'" align="absmiddle" border="0">';

		}
//		html += ;
		div.html(html);
		div.attr('title',Math.round(msg));
		if ( bevents )
		{
			div.find('img').each(function(img_num,el)
			{
				el = $(el);
				el.bind('mouseover',function(e)
				{
					div.find('img').each(function (i,el)
					{
						el = jQuery(el);
						if (i <= img_num )
							el.attr('src',vote_images['o'].src);
						else
							el.attr('src',vote_images['e'].src);

					});
					div.find('.rating_podskazka').text(''+vote_msgs[img_num+1]);
					div.attr('title',vote_msgs[img_num+1]);
				});

				el.bind('mouseout',function(e)
				{
					div.find('img').each(function (i,el)
					{
						el=jQuery(el);
						el.attr('src',vote_images[vote_letter(i,msg)].src);
					});
					div.find('.rating_podskazka').text(msg);
				});

				el.bind('click',function(e)
				{
					if ( div.attr('sent') )
						return;
					var param = "act=set&prod_id="+div.attr('prod_id')+"&vote="+(img_num+1);
					var ansv=jQuery.ajax(
					{
							type: "GET",
							url: vote_url,
							data: param,
							success: function(msg)
							{
								div.find('img').each(function(i,el)
								{
									el=jQuery(el);
									el.unbind('mouseover');
									el.unbind('mouseout');
									el.unbind('click');
								});

								vote_build(div,div.attr('prod_id'),0);
							},
							complete: function(a,b){div.attr('sent','sent')}
					});

				});
			});
		} //bind events
		div.attr('built','built');


	}
