
recommendation_form_conf = {
        textarea_comment: 'Napisz rekomendację...',
        input_author: 'podpis...'
};

function parseCompanyId(company_id)
{
    return company_id.split('_')[0];
}

$(document).ready(function(){
    $.ajaxSetup({'async': true});
    
    $(".share-items").hide();
    
    //
    //        podziel sie
    //
    
    $("a.share").click(function(){
            
        $(this).parents(".krak-belt").next(".share-items").slideDown("slow");
        return false;
                
    });
            
    $("a.close").click(function(){
        $(this).parent().slideUp("slow");
        return false;                
    });
    
    //
    //        rekomenduj
    //
    
        
    $(".recommends").hide();
    
    
    $("a.rec-link, a.recommend").hover(function(){
        if ($.browser.msie && $.browser.version <= "7.0"){
            $(this).next().css("z-index", 1000000);
            
            // FIXME:  no nie dało sie inaczej... IE7 sucks!
            // http://css-tricks.com/snippets/jquery/fixing-ie-z-index/
            var zIndexNumber = 1000;
            $("div.item-search").each(function() {
                if (($(this).get(0).tagName == 'P') 
                && ($(this).attr('class') == 'industry'))
                {
                    $(this).css("margin-left", "125px");
                    $(this).css('z-index', -1);
                } else {
                    $(this).css("zIndex", zIndexNumber);
                    zIndexNumber -= 10;
                }
            });
            
            // schowaj "podziel się"
            $("div.share-items").css("z-index", -1);
            
            $(this).parent().parent().parent().parent().parent().find('.industry').each(function(){
                if (($(this).get(0).tagName == 'P') 
                        && ($(this).attr('class') == 'industry'))
                {
                    $(this).css("margin-left", "125px");
                    $(this).css('z-index', -1);
                }
            });
            
            /*
            $(this).parent().parent().parent().parent().parent().find('.cross').each(function(){
                if (($(this).get(0).tagName == 'P') 
                        && ($(this).attr('class') == 'cross'))
                {
                    $(this).css("margin-left", "125px");
                    $(this).css('z-index', -1);
                }
            });
            */
            
            $(this).next().show();
            $(this).next().css('height', "35px");
            $(this).next().css('bottom', "-35px");
            $(this).next().find("input.rec-send-box").toggle();
            $(this).next().find("input.rec-send-box").css({display:"none"});
            $(this).next().find("a.closeMe").toggle();
            $(this).next().find("a.closeMe").css({display:"none"});
            
            $(this).next().find(".rec-max").hide();
            $(this).next().find(".rec-min").show();
            $(this).next().find(".rec-max").css('z-index', 1000000);
        } else {
            $(this).next().find(".rec-min").show();
            $(this).next().css({height:"35px", bottom:"-35px"}).show();
            $(this).next().find(".rec-max").hide();
        }
            
        }, function(){
            var self = this;
            setTimeout(function(){
                                if (!$(self).next().hasClass("aaa")){
                                   $(self).next().hide();
                                };    
                            }, 100);
    });

    $(".recommends").hover(function(){
                                    
        $(this).addClass("aaa");    
        
        }, function(){
            
            if ($(this).children(".rec-max").css("display") == "block"){
                
                    //$(this).addClass("aaa");
                    
                } else {
                    
                    $(this).removeClass("aaa").hide();
            }

    });
    
    $(".rec-max a.closeMe").click(function(){ //.rec-max .rec-send-box, 
        $(this).parents(".recommends").css({height:"35px", bottom:"-35px"}).hide();
        $(this).parents(".rec-max").hide();    
        
        // wyczyszczenie wartości w formie okienka
        $(this).parent().find('textarea[name=comment]').val(recommendation_form_conf.textarea_comment);
        $(this).parent().find('input[name=author]').val(recommendation_form_conf.input_author);
        
        return false;
    });
    

    $("a.blue").click(function(){
        if ($.browser.msie && $.browser.version <= "7.0")
        {
            $(this).parent().parent().find("input.rec-send-box").show();
            $(this).parent().parent().find("a.closeMe").show();
        }
        var ajax_url = '/profil/dodajrekomendacje.html';
        var params = {
                  check: 1,
                  company_id: parseCompanyId($(this).parent().parent().find('input[name=companyId]').val()) || '',
                  category_id: $(this).parent().parent().find('input[name=categoryId]').val() || ''
          };
        var ajax_url = '/profil/dodajrekomendacje.html';
        
        var current_element = $(this);
        $.ajax({
            type: 'GET',
            url: ajax_url,
            data: params,
            dataType: 'json',
            beforeSend: function(){
            },
            success: function(data){
                if (data.messages){
                    var msgs = '';
                    for (key in data.messages)
                    {
                        if (key == "alreadyRecomended")
                        {
                            msgs += data.messages[key] + "\n";
                            current_element.parents().find(".recommends").hide();
                            alert(msgs);
                            return;
                        }
                    }
                }
                // wyświetl teraz reszte
                current_element.parents(".recommends").css({height:"101px", bottom:"-101px"});
                current_element.parent().hide();
                current_element.parent().next().show();
            },
            complete: function(){
            }
        });
        
        return false;
    });
    
    
    $("a.rec-link, a.recommend").click(function(){
        return false;
    });
    
    
    //
    //
    //
    
    $("textarea.rec-box, input.rec-signature").click(function(){
        if ($(this).val() == 'Napisz rekomendację...')
        {
            $(this).val('');
        }
    });

    $("textarea.rec-box inner, input.rec-signature").click(function(){
        if ($(this).val() == 'podpis...')
        {
            $(this).val('');
        }
    
    });

    // click submit one recommendation form result
    $(".rec-send-box[type=button]").click(function(e){
        e.preventDefault();
        var ajax_url = '/profil/dodajrekomendacje.html';
        var params = {
                  comment: $(this).parent().find('textarea[name=comment]').val() || '',
                  author: $(this).parent().find('input[name=author]').val() || '',
                  company_id: parseCompanyId($(this).parent().find('input[name=companyId]').val()) || '',
                  category_id: $(this).parent().find('input[name=categoryId]').val() || ''
          };
        var ajax_url = '/profil/dodajrekomendacje.html';
        
        var current_element = $(this);
        
        $.ajax({
            type: 'POST',
            url: ajax_url,
            data: params,
            dataType: 'json',
            beforeSend: function(){
            },
            success: function(data){
                var msgs = '';
                if (data)
                {
                    if (data.result){
                        msgs = 'Twoja rekomendacja została dodana';
                    } else {
                        if (data.messages){
                          for (key in data.messages)
                          {
                              if (key == "alreadyRecomended")
                              {
                                  msgs += data.messages[key] + "\n";
                                  break;
                              }
                              // innych nie wyświetlamy
                          }
                      } else {
                          // msgs = 'Twoja rekomendacja nie została dodana';
                      }
                    }
                } else {
                    //msgs = 'Wystąpił wyjątek w modeule dodajrekomendacje';
                }
                current_element.parent().find('textarea[name=comment]').val(recommendation_form_conf.textarea_comment);
                current_element.parent().find('input[name=author]').val(recommendation_form_conf.input_author);
                current_element.parents().find(".recommends").hide();
                if (msgs)
                {
                    alert(msgs);
                }
            },
            complete: function(){
            }
        });    
    });
    
    // click submit on standalone recommendation form
    $('#rec-max-inner > form > input[class=rec-send]').click(function (e){
        e.preventDefault();
        
        // $(location).attr('href');
        var params = {
                comment: $('#rec-max-inner > form > textarea[name=comment]').val() || '',
        author: $('#rec-max-inner > form > input[name=author]').val() || '',
        company_id: parseCompanyId($('#rec-send-company_id').val()) || '',
        category_id: $('#rec-send-category_id').val() || ''
        };
        
        var ajax_url = '/profil/dodajrekomendacje.html';
        var current_element = $(this);
        $.ajax({
            type: 'POST',
        url: ajax_url,
        data: params,
        dataType: 'json',
                beforeSend: function(){
                },
                success: function(data){
                    var msgs = '';
                    if (data)
                    {
                        if (data.result){
                            msgs = 'Twoja rekomendacja została dodana';
                        } else {
                            if (data.messages){
                                msgs = '';
                              for (key in data.messages)
                              {
                                  if (key == "alreadyRecomended")
                                  {
                                      msgs += data.messages[key] + "\n";
                                      break;
                                  }
                                  // innych nie wyświetlamy
                              }
                          } else {
                              // msgs = 'Twoja rekomendacja nie została dodana';
                          }
                        }
                    } else {
                        //msgs = 'Wystąpił wyjątek w modeule dodajrekomendacje';
                    }
                    current_element.parent().find('textarea[name=comment]').val(recommendation_form_conf.textarea_comment);
                    current_element.parent().find('input[name=author]').val(recommendation_form_conf.input_author);
                    if (msgs)
                    {
                        alert(msgs);
                    }
                    return false;
                },
                complete: function(){
                }
            });    
            return true;
    });
});
