function OpenMainMenu(prefix, section_id){
    var ttt = document.getElementById(prefix+""+section_id);
    $("table[@tag='"+prefix+"']").each(function() {
        if(ttt.id != this.id){
            this.style.display = 'none';
        }
        else{
            if(this.style.display == '') this.style.display = 'none'; else this.style.display = '';
        }
    });


    var ttt = document.getElementById("im_"+section_id);
    $("img[@tag='im_']").each(function() {
        if(ttt.id != this.id){
            this.src = '/images/site/catalog_ico_plus.gif';
        }
        else{
            if(this.src.indexOf('/images/site/catalog_ico_plus.gif') != -1) this.src = '/images/site/catalog_ico_minus.gif'; else this.src = '/images/site/catalog_ico_plus.gif';
        }

    });
}


function OpenCloseRootMenu(tag, section_id){
    $("tr[@id='"+tag+"']").each(function() {
        if(this.style.display == ''){
			this.style.display = 'none';
			document.getElementById('im_'+section_id).src = '/images/site/catalog_ico_plus.gif';
		}
		else{
			this.style.display = '';
			document.getElementById('im_'+section_id).src = '/images/site/catalog_ico_minus.gif';
		}
    });
}

/*Seen*******************************/
function up() {
	var prev_disp = first_displayed_id = is_stop = '-1';
	$('tr[tag="tr_pr_seen"]').each(function(){
		if(is_stop != 1){
			var now_disp = $(this).css('display');

			if(first_displayed_id == '-1' && now_disp != 'none'){
				first_displayed_id = $(this).attr('id');
			}
			if(now_disp == 'none' && prev_disp != 'none' && prev_disp != '-1'){
				$('tr[id="'+$(this).attr('id')+'"]').each(function(){
					$(this).show();
				});
				$('tr[id="'+first_displayed_id+'"]').each(function(){
					$(this).hide();
				});
				is_stop = 1;
				return;
			}
			prev_disp = now_disp;
		}
	});
	show_seen_dots();
}

function down() {
	var next_disp = last_displayed_id = is_stop = '-1';
	$('tr[tag="tr_pr_seen"]').each(function(){
		var now_disp = $(this).css('display');

		if(now_disp != 'none'){
			last_displayed_id = $(this).attr('id');
		}
		if(is_stop != 1){
			var next_disp = $(this).attr('id').replace('tr_pr_seen_', '');
			next_disp = parseInt(next_disp)+1;
			next_disp = $('#tr_pr_seen_'+next_disp).css('display');
			if(now_disp == 'none' && next_disp != 'none' && next_disp !== undefined){
				$('tr[id="'+$(this).attr('id')+'"]').each(function(){
					$(this).show();
				});
				is_stop = 1;
				return;
			}
		}
	});
	if(is_stop == 1 && last_displayed_id != '-1'){
		$('tr[id="'+last_displayed_id+'"]').each(function(){
			$(this).hide();
		});
	}
	show_seen_dots();
}

function show_seen_dots(){
	var str = '';
	var cnt = 0;
	$('tr[tag="tr_pr_seen"]').each(function(){
		cnt++;
		if(cnt == 3){
			if($(this).css('display') != 'none'){
				str += '<b class="active">•</b>';
			}else{
				str += '<b>•</b>';
			}
			cnt = 0;
		}

	});
	$('#history_pos').html(str);
}
/*Seen*******************************/


$(document).ready(function() {
	try{
		setTimeout(function(){
			$('div[tag="d_star_rating"]').each(function(){
				$(this).bind('click', function(){
					var product_id = '';
					$(this).children('a').each(function(){
						product_id = $(this).attr('alt').replace('rating_', '');
					});
					var rating_val = '';
					$('input[name="rating_'+product_id+'"]:checked').each(function(){
						rating_val = $(this).val();
					});		
					$('div[tag="d_star_rating"]').each(function(){
						var tmp_product_id = '';
						$(this).children('a').each(function(){
							tmp_product_id = $(this).attr('alt').replace('rating_', '');
						});
						if(tmp_product_id == product_id){
							$(this).unbind('click');
						}
					});		
			
					$.ajax({
						url:	'/dinamic/st_orders.php',
						data:	'action=rate_Product&product_id='+product_id+'&rating_val='+rating_val,
						type:	'post',
						success: function (msg) {
							try{
								eval("var rating="+msg);
								$('#span_rating_'+product_id).html(rating.rating);
								$('#span_rating_voted_cnt_'+product_id).html(rating.rating_voted_cnt);
							}catch(e){alert(e);}
							alert('Спасибо, Ваш голос принят');
						}
					});
				});
			});
		}, 1000);
	}catch(e){}

	try{
		$("#product_tabs").tabs({
			select: function(event, ui){
				window.location.href = window.location.pathname+ui.tab.hash;
				ShowHideProductByVendorByHash(ui.tab.hash.replace('#', ''));
				return true;
			}
		});
	}catch(e){}

	try{
		ShowHideProductByVendorByHash(window.location.hash.replace('#', ''));
	}catch(e){}

});


function ShowHideProductByVendorByHash(hash){
	if(hash == '' || hash == 'alt_prods' || hash == 'description'){
		$('#d_product_byvendor').hide();
	}else $('#d_product_byvendor').show();
}

function ShowHideProductByVendor(id){
	for(i=1;i<6;i++){
		if(i == id){
			$("#t_"+i).attr("class", "act_tab"+i);
			$("#tab_"+i).show();
		}
		else{
			$("#t_"+i).attr("class", "tab"+i);
			$("#tab_"+i).hide();
		}
	}
}

function ShowComplainCheif(){
	var offset = $('#a_ComplainCheif').offset();
	$('#d_complainChief').css({'top': offset.top-220, 'left': offset.left-450});
	$.ajax({
		url:	'/dinamic/st_orders.php',
		data:	'action=show_ComplainCheif',
		type:	'post',
		success: function (msg) {
			$('#d_complainChief').html(msg);
			$('#d_complainChief').show();
		}
	});
}

function CheckComplainCheifForm(){
	cf_name = $('#cf_name').val();
	cf_email = $('#cf_email').val();
	cf_phone = $('#cf_phone').val();
	cf_text = $('#cf_text').val();
	error = '';
	if(!cf_name.length){
		error += "\nПоле 'Ваше имя' должно быть заполнено";
	}
	if(!cf_phone.length){
		error += "\nПоле 'Ваш телефон' должно быть заполнено";
	}
	if(!cf_text.length){
		error += "\nПоле 'Сообщение' должно быть заполнено";
	}

	if(error.length){
		alert(error);
		return false;
	}else{
		$.ajax({
			url:	'/dinamic/st_orders.php',
			data:	'action=save_ComplainCheif&name='+encodeURIComponent(cf_name)+'&email='+encodeURIComponent(cf_email)+'&phone='+encodeURIComponent(cf_phone)+'&text='+encodeURIComponent(cf_text)+'&url='+encodeURIComponent(window.location.href),
			type:	'post',
			success: function (msg) {
				$('#d_complainChief').hide();
				alert('Спасибо, Ваша жалоба отправлена Директору.');
			}
		});		
		return false;
	}
}

function CheckAskQuestion(){
	fb_name = $('#fb_name').val();
	fb_email = $('#fb_email').val();
	fb_phone = $('#fb_phone').val();
	fb_text = $('#fb_text').val();
	error = '';
	if(!fb_name.length){
		error += "\nПоле 'Ваше имя' должно быть заполнено";
	}
	if(!fb_phone.length){
		error += "\nПоле 'Ваш телефон' должно быть заполнено";
	}
	if(!fb_text.length){
		error += "\nПоле 'Сообщение' должно быть заполнено";
	}

	if(error.length){
		alert(error);
		return false;
	}else{
		$.ajax({
			url:	'/dinamic/st_orders.php',
			data:	'action=save_FeedBack&'+$('#f_fb').serialize(),
			type:	'post',
			success: function (msg) {
				$(':input','#f_fb').not(':button, :submit, :reset, :hidden').val('');

				alert('Спасибо, Ваш вопрос отправлен. С Вами свяжутся в ближайшее время.');
			}
		});		
		return false;
	}
}

function SendFeedback(){

	fb_name = $('#fb_name').val();
	fb_email = $('#fb_email').val();
	fb_town = $('#fb_town').val();
	fb_phone = $('#fb_phone').val();
	fb_text = $('#fb_text').val();
	error = '';
	if(!fb_name.length){
		error += "\nПоле 'Ваше имя' должно быть заполнено";
	}
	if(!fb_phone.length){
		error += "\nПоле 'Ваш телефон' должно быть заполнено";
	}
	if(!fb_town.length){
		error += "\nПоле 'Город' должно быть заполнено";
	}
	if(!fb_text.length){
		error += "\nПоле 'Сообщение' должно быть заполнено";
	}
	if(error.length){
		alert(error);
		return false;
	}else{
		$.ajax({
			url:	'/dinamic/st_orders.php',
			data:	'action=save_FeedBack&'+$('#f_fb').serialize(),
			type:	'post',
			success: function (msg) {
				$(':input','#f_fb').not(':button, :submit, :reset, :hidden').val('');
				alert('Спасибо, Ваш запрос отправлен. С Вами свяжутся в ближайшее время.');
			}
		});		
		return false;
	}
}

