var tropicalia = {

  setup : function () {

    // IE7 Hacks

    if (jQuery.browser.msie && jQuery.browser.version < 8) {
      jQuery('.alignleft, .alignright').parent('p').css('z-index', '20');
    }

    // ...

    jQuery('a[href="#top"]').click(function() {
      tropicalia.page.scroll_to_top();
      return false;
    });

    jQuery('a[href="#back"]').click(function() {
      window.history.back();
      return false;
    });

    jQuery('a[rel="external"]').attr('target', '_blank');

    // Home

    if (jQuery('body#home').length) {

      jQuery('.block .image-wrapper').each(function () {
        var wrapper = jQuery(this);
        wrapper.find('image:gt(0)').hide();
        window.setInterval(function () {
          var img = wrapper.find('img:visible');
          var next = img.next('img');
          if (next.length == 0) {
            next = wrapper.find('img:first');
          }
          img.hide(0, function () {
            next.show();
          });
        }, 5 * 1000);
      });

    }

    // Identifisignificados

    if (jQuery('body.identifisignificados').length) {

      jQuery('div.chrono').each(function () {
        var chrono = jQuery(this);
        chrono.find('ul li a').click(function () {
          chrono.find('ul li').removeClass('active');
          var link = jQuery(this);
          var content = chrono.find('div.year div.content-wrapper');
          content.html('<p>Carregando...</p>');
          content.load(link.attr('href') + '?action=ajax', function () {
            link.parent().addClass('active');
            content.find('.gallery img').unwrap();
          });
          return false;
        });
        chrono.find('ul li:first a').click();
      });

    }

    // Olhar colírico

    if (jQuery('body.olhar-colirico').length) {

      var player = jQuery('div.player-wrapper');
      if (player.length) {
        var pages = 0;
        player.find('.video').each(function () {
          var video = jQuery(this);
          if (video.hasClass('first')) {
            pages++;
          }
          video.addClass('page' + pages);
        });
        var prev = player.find('div.navigation a.prev');
        var next = player.find('div.navigation a.next');
        prev.hide();
        if (pages <= 1) {
          next.hide();
        } else {
          var page = 1;
          player.find('.video').hide();
          player.find('.video.page' + page).show();
          prev.click(function () {
            player.find('.video').hide();
            player.find('.video.page' + (--page)).show();
            if (page == 1) {
              prev.hide();
            }
            if (page < pages) {
              next.show();
            }
            return false;
          });
          next.click(function () {
            player.find('.video').hide();
            player.find('.video.page' + (++page)).show();
            if (page == pages) {
              next.hide();
            }
            if (page > 1) {
              prev.show();
            }
            return false;
          });
        }
      }

      jQuery('.album').each(function () {
        var album = jQuery(this);
        album.find('a').click(function (event) {
          if (album.find('.modal').length) {
            jQuery('.album .modal').remove();
          } else {
            jQuery('.album .modal').remove();
            if (jQuery.browser.msie && jQuery.browser.version < 8) {
              jQuery('.album').css('z-index', '');
              album.css('z-index', '30');
            }
            album.append('<div class="modal"></div>');
            var modal = album.find('.modal');
            modal.load(jQuery(this).attr('href') + '?action=ajax', function () {
              modal.fadeIn();
              jQuery('.content-wrapper.albums').css('padding-bottom', modal.height());
              jQuery('html, body').animate( {
                scrollTop : event.pageY
              }, 'slow');
              modal.find('a.play, a.show-lyrics').click(function () {
                modal.find('.data .lyrics').load(jQuery(this).attr('href') + '?action=ajax', function () {
                  modal.find('.data ul li').removeClass('active');
                  modal.find('.data ul li:first').addClass('active');
                  modal.find('.data .info').hide();
                  modal.find('.data .lyrics').show();
                  jQuery('.content-wrapper.albums').css('padding-bottom', modal.height());
                });
                return false;
              });
            });
            var hover = false;
            modal.hover(function(){
              hover = true;
            }, function(){
              hover = false;
            });
            jQuery('body').mouseup(function(){
              if(!hover) {
                jQuery('.content-wrapper.albums').css('padding-bottom', '');
                modal.remove();
              }
            });
          }
          return false;
        });
      });

      jQuery('.movie').each(function () {
        var movie = jQuery(this);
        movie.find('a').click(function (event) {
          if (movie.find('.modal').length) {
            jQuery('.movie .modal').remove();
          } else {
            jQuery('.movie .modal').remove();
            if (jQuery.browser.msie && jQuery.browser.version < 8) {
              jQuery('.movie').css('z-index', '');
              movie.css('z-index', '30');
            }
            movie.append('<div class="modal"></div>');
            var modal = movie.find('.modal');
            modal.load(jQuery(this).attr('href') + '?action=ajax', function () {
              modal.fadeIn();
              jQuery('html, body').animate( {
                scrollTop : event.pageY
              }, 'slow');
              modal.find('a[href*="#synopsis"]').click(function () {
                modal.find('.data ul li').removeClass('active');
                modal.find('.data ul li:eq(0)').addClass('active');
                modal.find('.data .info').hide();
                modal.find('.data .synopsis').show();
                return false;
              });
              modal.find('a[href*="#info"]').click(function () {
                modal.find('.data ul li').removeClass('active');
                modal.find('.data ul li:eq(1)').addClass('active');
                modal.find('.data .synopsis').hide();
                modal.find('.data .info').show();
                return false;
              });
            });
            var hover = false;
            modal.hover(function(){
              hover = true;
            }, function(){
              hover = false;
            });
            jQuery('body').mouseup(function(){
              if(!hover) {
                modal.remove();
              }
            });
          }
          return false;
        });
      });

      jQuery('.photo > a').each(function (index) {
        jQuery(this).click(function () {
          jQuery('.photo-large').remove();
          var link = jQuery(this);
          var html = '<div class="modal photo-large">';
          if (index > 0) {
            html += '<a class="prev" href="#prev">Anterior</a>';
          }
          if (index < (jQuery('.photo > a').length - 1)) {
            html += '<a class="next" href="#next">Próxima</a>';
          }
          html += '<a class="close" href="#close">Fechar</a>';
          html += '<img src="' + link.attr('href') + '" /><p>' + link.attr('title') + '</p></div>';
          jQuery('#content').append(html);
          var modal = jQuery('.photo-large');
          modal.show();
          modal.find('img').bind('load', function () {
            var img = jQuery(this);
            var w = img.width();
            var h = img.height() + (w < 500 ? 50 : 30);
            modal.css({
              'width' : w,
              'height' : h,
              'margin-top' : ((h / 2) + 10) * -1,
              'margin-left' : ((w / 2) + 10) * -1
            });
            modal.find('a.close').css({
              'margin-top' : ((h / 2) + 42) * -1,
              'margin-left' : ((w / 2) - 5)
            }).click(function () {
              modal.remove();
              return false;
            });
            modal.find('a.prev').css({
              'margin-left' : ((w / 2) + 86) * -1
            }).click(function () {
              modal.remove();
              jQuery('.photo > a:eq(' + (index - 1) + ')').click();
              return false;
            });
            modal.find('a.next').css({
              'margin-left' : (w / 2)
            }).click(function () {
              modal.remove();
              jQuery('.photo > a:eq(' + (index + 1) + ')').click();
              return false;
            });
          });
          /*
          jQuery('body').click(function(){
            modal.remove();
          });
          */
          return false;
        });
      });

      jQuery('.bibliography').each(function () {
        var biblio = jQuery(this);
        biblio.find('a').click(function () {
          var link = jQuery(this);
          var content = biblio.find('div.content-wrapper');
          if (!link.parent().hasClass('active')) {
            content.load(link.attr('href') + '?action=ajax', function () {
              link.parent().addClass('active');
            });
          } else {
            content.empty();
            link.parent().removeClass('active');
          }
          return false;
        });
      });

    }

    // Futurível

    if (jQuery('body.futurivel').length) {

      jQuery('a[href$="#contrib"]').click(function () {
        jQuery('#modal-background').show();
        jQuery('.modal').show();
        return false;
      });
      var modal = jQuery('.modal');
      var hover = false;
      modal.hover(function(){
        hover = true;
      }, function(){
        hover = false;
      });
      jQuery('body').mouseup(function(){
        if(!hover) {
          jQuery('#modal-background').hide();
          modal.hide();
        }
      });
      
    }

    // Geral

    jQuery('.content-wrapper > p, .content-wrapper > blockquote').each(function (index) {
      var p = jQuery(this);
      if (p.text().match(/^\s*$/i)) {
        return true; // continue
      }
      p.hover(function () {
        if (!p.has('.comments').length) {
          jQuery('.content-wrapper > p, .content-wrapper > blockquote').css('background-color', '');
          jQuery('.content-wrapper > p, .content-wrapper > blockquote').find('.comments').remove();
          p.css('background-color', '#f1ead7');
          p.prepend('<a class="comments" href="#"><span></span></a>');
          jQuery.get(tropicalia.comments.counter_url(index), function (data) {
            var response = data.responseText.replace(/\s+/gi, ' ');
            var matches = response.match(/comments \(([0-9]+)\)/i);
            var count = parseInt(matches[1], 10);
            p.find('.comments span').html('<strong>' + count + '</strong> comentário' + (count != 1 ? 's' : ''));
          });
        }
      }, function () {
        var comments = jQuery('.content-wrapper > p .comments, .content-wrapper > blockquote .comments');
        var timeout = window.setTimeout(function () {
          comments.remove();
          p.css('background-color', '');
        }, 5 * 1000);
        comments.click(function () {
          tropicalia.comments.show(p, index);
          var modal = jQuery('.modal');
          var hover = false;
          modal.hover(function() {
            hover = true;
          }, function() {
            hover = false;
          });
          jQuery('body').mouseup(function() {
            if (!hover) {
              modal.remove();
              jQuery('#modal-background').hide();
            }
          });
          return false;
        }).hover(function () {
          window.clearTimeout(timeout);
        }, function () {
          comments.remove();
          p.css('background-color', '');
        });
      });
    });

    jQuery('.content-wrapper > p > a:has(img.size-thumbnail)').click(function () {
      jQuery('.photo-large').remove();
      var link = jQuery(this);
      jQuery('#content').append('<div class="modal photo-large"><img src="' + link.attr('href') + '" /><p>' + link.attr('title') + '</p></div>');
      var modal = jQuery('.photo-large');
      modal.show();
      modal.find('img').bind('load', function () {
        var img = jQuery(this);
        var w = img.width();
        var h = img.height() + (w < 500 ? 50 : 30);
        modal.css({
          'width' : w,
          'height' : h,
          'margin-top' : ((h / 2) + 20) * -1,
          'margin-left' : ((w / 2) + 20) * -1
        });
      });
      jQuery('body').click(function(){
        modal.remove();
      });
      return false;
    });

  },

  page : {

    scroll_to_top : function () {
      jQuery('html, body').animate( {
        scrollTop : 0
      }, 'slow');
    }

  },

  comments : {

    key : 'fd43917c53a1793f877f6594a0205b1e',

    counter_url : function (pos) {
      var url = 'http://www.intensedebate.com/js/getCommentLink.php';
      url += '?acct=' + tropicalia.comments.key;
      url += '&postid=' + encodeURIComponent(post_id + '-' + pos);
      url += '&posturl=' + encodeURIComponent(post_url + '%23' + pos);
      url += '&posttitle=' + encodeURIComponent(post_title.replace(/#/, '%23'));
      return url;
    },

    script : function (pos) {
      var url = 'http://intensedebate.com/js/genericCommentWrapper2.php';
      url += '?acct=' + tropicalia.comments.key;
      url += '&postid=' + encodeURIComponent(post_id + '-' + pos);
      url += '&url=' + encodeURIComponent(post_url + '%23' + pos);
      url += '&title=' + encodeURIComponent(post_title.replace(/#/, '%23'));
      return url;
    },

    show : function (p, pos) {
      jQuery('head script[src*="intensedebate.com"], #' + idcomments_div).remove();
      p.after('<div id="' + idcomments_div + '" class="modal"></div>');
      jQuery('#modal-background').show(); /*.click(function () {
        div.remove();
        jQuery(this).hide();
      }); */
      var div = jQuery('#' + idcomments_div).show();
      var modal = jQuery('.modal');
      var hover = false;
      modal.hover(function() {
        hover = true;
      }, function() {
        hover = false;
      });
      jQuery('body').mouseup(function() {
        if (!hover) {
          modal.remove();
          jQuery('#modal-background').hide();
          jQuery('head script[src*="intensedebate.com"], #' + idcomments_div).remove();
          jQuery(this).unbind('mouseup');
        }
      });
      var script = jQuery('<script>').attr({
        type : 'text/javascript',
        src: tropicalia.comments.script(pos),
        rel : 'intensedebate'
      });
      document.getElementsByTagName('head')[0].appendChild(script.get(0));
    }

  }

};

jQuery(document).ready(function() {
  tropicalia.setup();
});

