$(document).ready(function(){
    
    $('.input').enablePlaceholder();  
    
    $.ajaxSetup({
        type: 'get',
        url: 'index.php',
        dataType: 'json'      
    });



    
    $('.carusel').cycle({
        fx: 'fade',
        pause:1,
        pauseOnPagerHover:1,
        cleartypeNoBg: 1,
        speed: 600,
        timeout: 6000,
        pager:  '#pagerRandom'
    });
     
    $('.compareNow').live('click', function() 
    {
        var addProductID = $(this).attr('id').replace('compareNowID_','');
        var addProductName = $(this).attr('title');

        $.ajax({       
            data:{
                'do':'compare',
                'compareID': addProductID
            },
            success: function(data){
                
                var addChangeProductID = '#compareNowID_'+ addProductID;

                $(addChangeProductID).replaceWith('<ins class="compareSelected" id="compareSelectedID_'+addProductID+'" title="'+ addProductName +'" ><b>+</b>&nbsp;<span>товар добавлен</span></ins>');
                      
               
                if (data.compareResponce == 1){
                    $('#compareInfo').show();
                      
                    if ($('#compareList').length == 0){
                        $('#compareInfo .title').after('<ul class="compareList" id="compareList"></ul>');
                    }
                    
                    var addProductLink ='product_'+ addProductID +'.html';
                    $('#compareList').append('<li class="item"><a id="compareRemoveID_'+ addProductID +'" href="#" class="compareRemove">x</a>\n<a class="link" href="'+ addProductLink +'">'+ addProductName +'</a></li>');           
                }
            }
        });
            
        return false;
    });
    
    
    $('.compareRemove').live('click', function() {

        var removeProductID = $(this).attr('id').replace('compareRemoveID_','');
      
        $.ajax({       
            data:{
                'do':'compare',
                'compareRemoveID': removeProductID
            },
            success: function(data){
                    
                var removeChangeProductID = '#compareSelectedID_'+ removeProductID;

                
                if ($(removeChangeProductID).length){
                    
                    var removeProductName = $(removeChangeProductID).attr('title');
                    
                    $(removeChangeProductID).replaceWith('<a href="#" class="compareNow" id="compareNowID_'+  removeProductID +'" title="'+ removeProductName +'"><b>+</b>&nbsp;<span>добавить к сравнению</span></a>');
                }  
                
                removeProductID = '#compareRemoveID_'+ removeProductID;
                 
                $(removeProductID).parent().remove();
                
                if (data.compareResponce == 0){
                    $('#compareInfo').hide();
                }
                
            }
        });
            
        return false;
    });
      
    //PriceNOW
      
    $('.priceNow').live('click', function() 
    {
        var addProductID = $(this).attr('id').replace('priceNowID_','');
        var addProductName = $(this).attr('title');

        $.ajax({       
            data:{
                'do':'price_request',
                'priceRequestID': addProductID
            },
            success: function(data){
                
                var addChangeProductID = '#priceNowID_'+ addProductID;

                $(addChangeProductID).replaceWith('<ins class="priceSelected" id="priceSelectedID_'+addProductID+'" title="'+ addProductName +'" ><b>$</b>&nbsp;<span>товар добавлен</span></ins>');
                      
               
                if (data.priceResponce == 1){
                    $('#priceInfo').show();
                      
                    if ($('#priceList').length == 0){
                        $('#priceInfo .title').after('<ul class="priceList" id="priceList"></ul>');
                        $('#priceRequestSuccess').remove();
                        $('#priceRequestError').remove();
                    }
                    
                    var addProductLink ='product_'+ addProductID +'.html';
                    $('#priceList').append('<li class="item"><a id="priceRemoveID_'+ addProductID +'" href="#" class="priceRemove">x</a><a class="link" href="'+ addProductLink +'">'+ addProductName +'</a></li>');           
                }
            }
        });
            
        return false;
    });
    
    
    $('.priceRemove').live('click', function() {

        var removeProductID = $(this).attr('id').replace('priceRemoveID_','');
      
        $.ajax({       
            data:{
                'do':'price_request',
                'priceRemoveID': removeProductID
            },
            success: function(data){
                    
                var removeChangeProductID = '#priceSelectedID_'+ removeProductID;

                
                if ($(removeChangeProductID).length){
                    
                    var removeProductName = $(removeChangeProductID).attr('title');
                    
                    $(removeChangeProductID).replaceWith('<a href="#" class="priceNow" id="priceNowID_'+  removeProductID +'" title="'+ removeProductName +'"><b>$</b>&nbsp;<span>запрос цены</span></a>');
                }  
                
                removeProductID = '#priceRemoveID_'+ removeProductID;
                 
                $(removeProductID).parent().remove();
                
                if (data.priceResponce == 0){
                    $('#priceInfo').hide();
                }
                
            }
        });
            
        return false;
    });
      
      
    $('.printMap').click(function () { 
        var contentSrc = 'data/map.gif';
        link = 'about:blank';
        var nw = window.open(link, '_blank','toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=yes, resizable=yes, width=800, height=550');
        nw.document.open();
        nw.document.write(getPrintMap(contentSrc));
        nw.document.close();
        return false;
    });
      
    getPrintMap = function(contentSrc) {
    
        return '<html>\n' +
        '<head>\n' +
        '<title>ООО «ТЭМБО», г.Мытищи, Новомытищинский проспект, дом 2</title>\n' +
        '</head>\n' +
        '<body onload="setTimeout(window.print(), 1);">\n' +
        '<img src="' + contentSrc + '" alt="" />\n' +
        '</body>\n' +
        '</html>';
    };
   
  
});

function doStat(req) {
    if(req){
        document.getElementById('tgenexe').innerHTML     = req['tgenexe'];
        document.getElementById('tgencompile').innerHTML = req['tgencompile'];
        document.getElementById('tgendb').innerHTML      = req['tgendb'];
        document.getElementById('tgenall').innerHTML     = req['tgenall'];
        document.getElementById('tgensql').innerHTML     = req['tgensql'];
    }
}

