
function show_popup(de, pos){
	var position=pos;
	var drop_elem=de;
    var num = drop_elem.substr(9);
switch (num) {
	   case "1":
	      x = '0px'
	      break
	   case '2':
	      x = '122px'
	       break
	   case '3':
	      x = '244px'
	       break
	   case '4':
	      x = '366px'
	       break
	   case '5':
	      x = '371px'
	       break
	   case '6':
	      x = '249px'
	       break
	   case '7':
	      x = '127px'
	       break
	   case '8':
	      x = '5px'
	       break
        default:
            x = '5px'
            break
	}
		if (position.indexOf('right_side')==0){
  			document.getElementById(drop_elem).style.left='';
  			document.getElementById(de).style.right=x;
		}
		else if (position!=de){
  			document.getElementById(drop_elem).style.left=x;
  			document.getElementById(drop_elem).style.right='';
		}
        $("div[id^='drop_elem']").hide();
//        $('#'+drop_elem).slideDown(300);
        $('#'+drop_elem).show();
//        $('#'+drop_elem).toggle(200);
}
function hide_popup(de){
    var drop_elem=de;
  		document.getElementById(drop_elem).style.display='none';
}
function change_check(el){
	if (document.getElementById(el.id).className=='my_checkbox checked'){
		document.getElementById(el.id).style.backgroundImage='url(/sites/dostupno/images/checkbox.png)';
		document.getElementById(el.id).className='my_checkbox';
	} else {
		document.getElementById(el.id).style.backgroundImage='url(/sites/dostupno/images/checkbox_checked.png)';
		document.getElementById(el.id).className='my_checkbox checked';
	}
}
function change_hover(el){
	document.getElementById(el.id).style.backgroundImage='url(/sites/dostupno/images/checkbox_active.png)';
}
function change_hover_out(el){
	if (document.getElementById(el.id).className=='my_checkbox checked'){
		document.getElementById(el.id).style.backgroundImage='url(/sites/dostupno/images/checkbox_checked.png)';
	} else {
		document.getElementById(el.id).style.backgroundImage='url(/sites/dostupno/images/checkbox.png)';
	}
}

function spin(ident, cn){
    if (cn=='arrow_up'){
        document.getElementById(ident).value=(parseInt(document.getElementById(ident).value)+1);
    } else if (parseInt(document.getElementById(ident).value)>1){
        document.getElementById(ident).value=(parseInt(document.getElementById(ident).value)-1);
    }
}

function cart_spin(ident, cn, price){
    var cnt = 0;
    if (cn=='arrow_up'){
        cnt = parseInt(document.getElementById(ident).value)+1;
    } else if (parseInt(document.getElementById(ident).value)>1){
        cnt = parseInt(document.getElementById(ident).value)-1;
    }
        document.getElementById(ident).value=cnt;
        parent.myCart.editProduct({'id':ident}, {'price':price, 'count':cnt});
        myCart.editProduct({'id':ident}, {'price':price, 'count':cnt});
}

function AddProduct(prod_id, prod_price, image){
    
  var cnt = parseInt(document.getElementById(prod_id).value);
  var notification = $('#add'+prod_id);
  notification.hide();
  $(notification).css({ opacity: 0 });
  $(notification).stop();
  $(notification).show().animate({opacity: 1},850).animate({opacity: 0},850);
    myCart.addProduct({'id':prod_id, 'price':prod_price, 'image':image, 'count':cnt});
  
}

function AddProductStart(prod_id, prod_price, image){
    
  var cnt = 1;
  var notification = $('#add'+prod_id);
  notification.hide();
  $(notification).css({ opacity: 0 });
  $(notification).stop();
  $(notification).show().animate({opacity: 1},850).animate({opacity: 0},850);
    myCart.addProduct({'id':prod_id, 'price':prod_price, 'image':image, 'count':cnt});
  
}

function change_page(ident){
    $("div[id^='product_'][class^='description_full']").hide();
    $('#'+ident).fadeIn(1000);
    $("li[id^='product_'][class^='active']").removeClass('active');
    ident+="_link";
    $('#'+ident).addClass('active');
}

function no_bg(){
    $("#popupdiv_close").hide();
}

$(document).ready(function(){
    /**
     * ????? ? ???????????
     */
    var searchInput = $('#search_request');//input search field id
    searchInput.keyup(function(){
        if($(this).val().length >= 3){
            $.ajax({
                url:'/read/ajax-search',
                type:'POST',
                data:'data=' + $(this).val(),
                success:function (res) {
                    //console.log(res);
                    if(res.length > 2){
                        var listDiv = '<div id="res_list"><ul>{list}</ul></div>';
                        //template for li
                        var listLi = '<li><a href="{href}"><table><tr><td><img src="{photo}" /></td><td>{name}</td><td>{price}</td></tr></table></a></li>';
                        var list = '';
                        $.each($.evalJSON(res),function(index,item){
                            var replacedLi = listLi.replace('{href}',item.href);
                            replacedLi = replacedLi.replace('{name}',item.name_ru);
                            replacedLi = replacedLi.replace('{photo}',item.photo);
                            replacedLi = replacedLi.replace('{category}',item.category);
                            replacedLi = replacedLi.replace('{price}',item.price);
                            list += replacedLi;
                        });
                        var closeDiv = '<div id="close_list"></div>'
                        listDiv = listDiv.replace('{list}',list);                    
                        var position = searchInput.offset();
                        position.top += searchInput.height()+parseInt(searchInput.css('padding-bottom'))+parseInt(searchInput.css('padding-top'));
                        if($('#res_list')){
                            $('#res_list').remove();
                            $('#close_list').remove();
                        }
                        $('body').append(listDiv);
                        $('#res_list').css({'position':'absolute','z-index':100});
                        $('#res_list').width(searchInput.width()+parseInt(searchInput.css('padding-left'))+parseInt(searchInput.css('padding-right')));
                        $('#res_list').offset(position);
                        $('body').append(closeDiv);
                        $('#close_list').css({'position':'absolute','z-index':101,'cursor':'pointer'});
                        $('#close_list').offset({'top':position.top-15,'left':position.left+$('#res_list').width()-15});
                        $('#close_list').click(function(){
                            if($('#res_list')){
                                $('#res_list').remove();
                                $('#close_list').remove();
                            }
                        });
                    }else{
                        if($('#res_list')){
                            $('#res_list').remove();
                            $('#close_list').remove();
                        }
                    }
                }
            });
        }else{
            if($('#res_list')){
                $('#res_list').remove();
                $('#close_list').remove();
            }
        }
    });
    /**************************************/
});
