function number_format(number, decimals, dec_point, thousands_sep) {
      number = (number+'').replace(',', '').replace(' ', '');
    var n = !isFinite(+number) ? 0 : +number, 
        prec = !isFinite(+decimals) ? 0 : Math.abs(decimals),
        sep = (typeof thousands_sep === 'undefined') ? ',' : thousands_sep,
        dec = (typeof dec_point === 'undefined') ? '.' : dec_point,
        s = '',
        toFixedFix = function (n, prec) {
            var k = Math.pow(10, prec);
            return '' + Math.round(n * k) / k;
        };
    // Fix for IE parseFloat(0.55).toFixed(0) = 0;
    s = (prec ? toFixedFix(n, prec) : '' + Math.round(n)).split('.');
    if (s[0].length > 3) {
        s[0] = s[0].replace(/\B(?=(?:\d{3})+(?!\d))/g, sep);
    }
    if ((s[1] || '').length < prec) {
        s[1] = s[1] || '';
        s[1] += new Array(prec - s[1].length + 1).join('0');
    }
    return s.join(dec);
}


var fusers = new fConnector('fusers');   
var banners = new fConnector('banners');
var catalog = new fConnector('catalog');
var ajax_info = {};
var old_block='';
var old_html='';
/*
var defaults = {
                count: 1,
                idAttribute: 'id',
                onGoodAdded: null,
                extDataFunc: null,
                syncAddToBasket: true,
                basketContainer: '.basket-container',
                basketCountSelector: '.basket-count',
                basketAllCountSelector: '.basket-count',
                basketSumSelector: '.basket-sum',
                basketElementTemplate: '<li><a>{details.props.Name}</a> <span>{count}</span> <span>{details.props.price}</span> <span>{priceSum}</span></li>'
            };    
            
*/            
function price_update(data){}

function show_form(old_block_selector, callback){ 
    old_block=old_block_selector;
    data = $(old_block_selector).html();
    old_html=data;
    $(old_block).html('');
    
    $('#AddFeedbackss').html(data);
    $('div.filter').animate({opacity: 0}, 0);
    $('div.filter').show();
    $('div.filter').animate({opacity: 1}, 200);
    $('#AddFeedbackss').animate({opacity: 0}, 0);
    $('#AddFeedbackss').show();
    $('#AddFeedbackss').animate({opacity: 1}, 500);
    $('.close_block_ui').click(function(){ hide_form(); });
    if (typeof callback == 'function') {
        callback();
    }

}
function hide_form( callback){ 
    $(old_block).html(old_html);
    
    $('div.filter').animate({opacity: 1}, 0);
    $('div.filter').hide();
    $('div.filter').animate({opacity: 0}, 200);
    $('#AddFeedbackss').animate({opacity: 1}, 0);
    $('#AddFeedbackss').hide();
    $('#AddFeedbackss').animate({opacity: 0}, 500);
    if (typeof callback == 'function') {
        callback();
    }

}


       
        if ((navigator.userAgent.indexOf('iPhone') != -1) || (navigator.userAgent.indexOf('iPod') != -1) || (navigator.userAgent.indexOf('iPad') != -1) || (window.navigator.userAgent.indexOf('Android') != -1)) {
        $(document).ready(function() {
			
			
			
			
            
                      $('ul#my-menu ul').each(function(index) {
                        $(this).prev().addClass('collapsible').click(function() {
                          if ($(this).next().css('display') == 'none') {

                              $(this).parent().parent().children().find('ul').each(function(){
                                    $(this)
                                    //$(this).css('display', 'none')
                                    $(this).slideUp(200, function () {
                                      $(this).prev().removeClass('expanded').addClass('collapsed');
                                      $(this).find('ul').each(function() {
                                        $(this).hide().prev().removeClass('expanded').addClass('collapsed');
                                      });
                                    });
                                })
                              $(this).next().slideDown(200, function () {
                              $(this).prev().removeClass('collapsed').addClass('expanded');
                            });
                          }else {
                            $(this).next().slideUp(200, function () {
                              $(this).prev().removeClass('expanded').addClass('collapsed');
                              $(this).find('ul').each(function() {
                                $(this).hide().prev().removeClass('expanded').addClass('collapsed');
                              });
                            });
                          }
                          return false;
                        });
                      });
                      
                    });
         }
         

         
     $(function(){
        $('a[rev=tocart]').ishopToBasket({basketCountSelector:'#cartcount',onAddedCallback : function(){
            show_form('#good_added_splash',function(){
                setTimeout("hide_form();", 7000);
            });
        }});            
            
		$('#call_back').click(function(){
			show_form('#phhonemy',function(){
				
			});
			return false;
		});

        $('#city-link').click(function(){

            show_form('#city_selector', function(){
                
                $('#sity_table a').click(function(){
                    
                    ajax_info = {region : $(this).attr('rel')};
                    fusers.execute({update_visitor_options:ajax_info}, function(value, obj){
                        hide_form(); 
                        if (obj.result.passed) { $('#city-link').html(ajax_info.region); } });                     
                        return false;
                });
            });

            return false;
        });    
    var last_count = 0;
    $('.basket_input input[name=quantity]').focus(function(){
        last_count = parseInt($(this).attr('value'));
    });
    $('.basket_input input[name=quantity]').keyup(function(){
           var objid = $(this).attr('id');
           objid=objid.slice(5);
           var count = parseInt($(this).attr('value'));
           obj={id:objid,count:count-last_count}
           var iconnector= new fConnector('ishop');   
           last_count=last_count+obj.count;
           iconnector.execute({add_to_cart:obj},function(flag,result){
            var res=result.result.cart;
            var xsum=number_format(res.sum, 0, '.', ' ');
            $('#basket_sum_total').html(xsum);
            
            
        });
        
        return false;           
           
    });
    
    
    $('a.plus').click(function(){
        var objid = $(this).attr('rel');
        var ibutton = $('#good_'+objid);

        i = ibutton.attr('value');
        i = (i/1)+1;
        ibutton.attr('value',i);
        obj={id:objid,count:1}
        var iconnector= new fConnector('ishop');   
        iconnector.execute({add_to_cart:obj},function(flag,result){
            var res=result.result.cart;
            var xsum=number_format(res.sum, 0, '.', ' ');
            $('#basket_sum_total').html(xsum);
        });
        
        return false;
    });

    $('a.minus').click(function(){
        var objid = $(this).attr('rel');
        var ibutton = $('#good_'+objid);

        i = ibutton.attr('value');
        if (i > 1) {
            i = (i/1)-1;
            ibutton.attr('value',i);
            obj={id:objid,count:-1}
            var iconnector= new fConnector('ishop');   
            iconnector.execute({add_to_cart:obj},function(flag,result){
                var res=result.result.cart;
                var xsum=number_format(res.sum, 0, '.', ' ');
                $('#basket_sum_total').html(xsum);
            });
        }
        
        return false;
    });

    
    $('ul.sub:eq(6)').attr('class', 'sub sub_r');
    $('ul.sub:eq(7)').attr('class', 'sub sub_r');
    $('li.main_list:eq(8)').attr('class','main_list mediat');
    
    $('div.m_block div.ct_el:eq(3)').attr('class','ct_el lst');
    $('div.m_block div.ct_el:eq(7)').attr('class','ct_el lst');
    $('div.m_block div.ct_el:eq(11)').attr('class','ct_el lst');
    
    $('div.four_phones').hide();
    $('div.four_phones:eq(0)').show();
    
    $('div.phones3bars div.bar').click(function(){
        i = $('div.phones3bars div.act span').html();
        $('div.phones3bars div.act').html('<a href="">'+i+'</a>');
        $('div.phones3bars div.act').removeClass('act');
        $(this).addClass('act');
        i = $('div.phones3bars div.act a').html();
        $(this).html('<span>'+i+'</span>');
        i = $(this).index();
        i = i/2;
        $('div.four_phones').hide();
        $('div.four_phones:eq('+i+')').show();
        return false;
    });
    
    this_url = document.location.href;
    $('ul.ctb_options a').each(function(){
        var ahref = $(this).attr('href');
        if (ahref.charAt(0) == '#') {
            $(this).attr('href', this_url+ahref);
        }
    });
    
    $('.ttop').attr('href',this_url+$('.ttop').attr('href'));
    
    i = $('div.phone_cat_ft div.paging a').index();
    $('div.phone_cat_ft div.txt strong').html(i);
    
    $('.onPage-selector').change(function(){
        
        v=$(this).val();
        
        if (v) 
        {
            document.location.href = v;
        }
    });
    
    $('a.banner, a.banner2, a.banner-in-list').click(function(){
        var banner_id = $(this).attr('rel');
        var url = document.location.pathname;
        banners.execute({add_click:{ "banner_id" : banner_id, "url" :url }});
    });    

    
    $('.phones3bars .bar').hover(function(){
        $('.phones3bars .bar').removeClass('act');
        $(this).addClass('act');
        
        $('.m_block.four_phones').hide();
        var xid = $(this).attr('rel');
        $('#'+xid).show();
        
    });
    
    $('.details-menu li').click(function(){
        $('.details-block').hide();
        $('.details-menu li').removeClass('selected');
        $(this).addClass('selected');
        var id= $('a', this).eq(0).attr('rel');
        $('#'+id).show();
        
    });
    
    $('#searchf').keyup(function(){
        var word = $(this).attr('value');
        catalog.execute({search_tips:{"word":word}},function(a,self){
            var tips = self.result.tips;
            if (typeof tips == 'undefined') {
                $('#search_suggestion').hide();
                return false;                
            }
            
            if (tips.length){
                $('#search_suggestion').show();
                $('.list_matches_sch').empty();
                for (var key in tips) {
                    
                    var link = tips[key]['link'];
                    var name = tips[key]['value'];
                    var tmp = '<li class="suggestion_entry"><a href="'+link+'" style="text-decoration:none;">'+name+'</a></li>';
                    $('.list_matches_sch').append(tmp);
                }
                
            } else {
                $('#search_suggestion').hide();
            }
            
        });
    });
    $('.search').hover(function(){}, function(){$('#search_suggestion').hide();});
     
 });
