$.app = {
  
  init: function(){
    $.app.init_t_blank();
    $.app.init_sections();
    $.app.init_nav();
    $.app.init_thumbs();
    $.app.animate();
    
  },
  
  animate: function(){
    $("#bg_w").bg({
      callback: function(bg){
        $.animation.init({
          bg: bg,
          bg_2: $("#bg_2"),
          logo: $("#logo"),
          nav: $("#navigation")
        });
      }
    });
  },
  
  init_sections: function(){
    $.app._init_sections();
    $(window).resize(function () {
      $.app._init_sections();
    });
  },
  
  _init_sections: function(){
    var w = {
      height: $(window).height()
    };
    $(".section").each(function(){
      var s = {
        height: $(this).height()
      };
      
      var padding = ((w.height-s.height)/2);
      padding = (padding<64) ? 64 : padding;
      $(this).css("padding-top",padding+"px");
      $(this).css("padding-bottom",padding+"px");
    });
  },
  
  init_nav: function(){
    $("#navigation").css("opacity",0.75);
    $("#navigation").hover(
      function(){
        $(this).stop().fadeTo('fast',1);
      },
      function(){
        $(this).stop().fadeTo('fast',0.75);
      }
    );
    $("#navigation a").click(function(e){
      e.preventDefault();
      var target = $(this).attr("href");
      
      if(target!="#home"){
        $.animation.switch_bg();
      }
      $.scrollTo($(this).attr("href"),{
        duration: 1000
        
      });
      
    });
    /*$("#navigation a").hover(
      function(){
        $(this).stop().fadeTo('fast',0.75);
      },
      function(){
        $(this).stop().fadeTo('fast',1);
      }
    );*/
  },
  
  
  init_thumbs: function(){
    $(".thumb").artist();
  },
  
  init_t_blank: function(){
    $("a[rel='t_blank']").attr("target","_blank");
  }
  
};

$.animation = {
  options: null,
  bg: null,
  bg_2: null,
  logo: null,
  nav: null,
  nav_items: [],
  current_nav_item: 0,
  intv_nav: null,
  
  init: function(options){
    $.animation.options = options;
    $.animation.prepare();
    $.animation.show_bg();
    
    $(window).scroll(function() {
      var this_scroll = $(window).scrollTop();
      
      var ratio = 0;
      if(this_scroll<($(document).height()/2)){
        ratio = this_scroll/($(document).height()/2);
      }else{
        ratio = 1;
      }
      
      $.animation.bg.css("opacity",1-ratio);
      $.animation.bg_2.css("opacity",ratio);
      
    });
  },
  
  prepare: function(){
    $.animation.bg = $.animation.options.bg.bg;
    $.animation.bg_2 = $.animation.options.bg_2;
    $.animation.bg_2.css("opacity",0).show();
    $.animation.logo = $.animation.options.logo;
    $.animation.nav = $.animation.options.nav;
  },
  
  show_bg: function(){
    $.animation.bg.fadeIn(1500,function(){});
    setTimeout($.animation.show_logo,500);
  },
  
  switch_bg: function(){
    //$.animation.bg.animate({marginTop:-($.animation.bg.height()/2)},1800);
    //$.animation.bg.fadeOut(2000);
    //$.animation.bg_2.fadeIn(2000);
  },
  
  show_logo: function(){
    $.animation.logo.fadeIn(2000,function(){
      //$.animation.bg.animate({marginTop:-($.animation.bg.height()*1.3)},1000);
      
    });
    setTimeout($.animation.show_nav,500);
  },
  
  show_nav: function(){
    //$.animation.nav.find("li").hide();
    $.animation.nav.slideDown(1000);
    
    /*$.animation.nav.children("ul").children("li").each(function(){
      //$(this).css("margin-left","-170px");
      $.animation.nav_items.push($(this));
    });*/
    
    //$.animation.intv_nav = setInterval($.animation.show_nav_items,50);
  },
  
  show_nav_items: function(){
    var nav_item = $.animation.nav_items[$.animation.current_nav_item];
    $.animation.show_nav_item(nav_item);
    if($.animation.current_nav_item<$.animation.nav_items.length-1){
     $.animation.current_nav_item++; 
    }else{
      clearInterval($.animation.intv_nav);
    }
  },
  
  show_nav_item: function(nav_item){
    nav_item.fadeIn(1000);
  }
  
};


// Bg
$.fn.bg = function(options){
  
  $bg_w = $(this);

  var bg = {
    bg_w: $bg_w,
    bg: $bg_w.find("#bg"),
    bg_2: $bg_w.find("#bg_2"),
    
    init: function(){
      bg.fix_bg_w();
      bg.bg.fix_bg(function(){
        setTimeout(bg.show,500);
      });
      bg.bg_2.fix_bg();
      
      $(window).resize(function () {
        bg.fix_bg_w();
        bg.bg.fix_bg(function(){});
        bg.bg_2.fix_bg();
      });
    },
    
    fix_bg: function(){
      bg.bg.fix_bg(function(){
        bg.show();
      });
    },
    
    fix_bg_w: function(){
      bg.bg_w.css("width",$(window).width()+"px");
      bg.bg_w.css("height",$(window).height()+"px");
      bg.bg_w.css("position","fixed");
      bg.bg_w.css("top","0");
      bg.bg_w.css("left","0");
      bg.bg_w.css("overflow","hidden");
    },
    
    show:function(){
      options.callback(bg);
    }
    
  };
  
  bg.init();
  
};

$.fn.fix_bg = function(callback){

  var o = {width: $(this).width(),height: $(this).height()};
  var w = {width: $(window).width(),height: $(window).height()};
  
  var ratio =  o.height/o.width;
  if(ratio > (w.height/w.width)){
    $(this).width(w.width);
    $(this).height(w.width * ratio);
  }else{
    $(this).height(w.height);
    $(this).width(w.height / ratio);
  }
  $(this).css("position","absolute");
  //$(this).css("left","50%");
  $(this).css("top","0");
  //$(this).css("margin-left","-"+(Math.round($(this).width()/2))+"px");
  //$(this).css("margin-top","-"+(Math.round($(this).height()/2))+"px");
  
  if(typeof callback == 'function'){
    callback.call();
  } 
};


// Artist
$.fn.artist = function(options){
  
  options = $.extend({},options);
  
  return this.each(function(){
    var $this = $(this);
    var artist = {

      artist: null,
      modal: null,
      
      init: function(){
        artist.prepare();
        artist._hover();
        artist._click();
      },
      
      prepare: function(){
        
      },
      
      load_artist: function(callback){
        var uri = "/artists/feed/artist/";

        var data = {
          id: $this.attr("rel")
        };

        $.ajax({
          url: uri,
          data: data,
          dataType: "jsonp",
          success: function(data) {
            if(data.success){
              artist.artist = data.artist;
              if(typeof callback == 'function'){
                callback.call();
              }
            }
          }
        });
      },
      
      build: function(){
        if(artist.artist!=null){
          var artist_html = "";
          artist_html += "<article class=\"artist\">";
          artist_html += "<header>";
          artist_html += "<h1>"+artist.artist.title+"</h1>";
          artist_html += artist.build_images(artist.artist);
          artist_html += "<nav><ul class=\"clearfix tab_nav\">";
          artist_html += "<li class=\"active\"><a href=\"#tab_artist_info\">Info</a></li>";
          if(artist.artist.tourdates!=null){
            artist_html += "<li><a href=\"#tab_artist_tourdates\">Tourdates</a></li>";
          }
          if(artist.artist.videos!=null){
            artist_html += "<li><a href=\"#tab_artist_videos\" class=\"full\">Video</a></li>";
          }
          if(artist.artist.sounds!=null){
            artist_html += "<li><a href=\"#tab_artist_sounds\" class=\"full\">Audio</a></li>";
          }
          artist_html += "</nav></ul>";
          artist_html += "</header>";
          
          artist_html += "<div class=\"body\">";
          if(artist.artist.body!=null){
            artist_html += "<div id=\"tab_artist_info\" class=\"clearfix\"><div class=\"col col_1 j_sp\">"+artist.artist.body+"</div><div class=\"col col_2\">";
            artist_html += artist.build_links(artist.artist);
            artist_html += artist.build_contacts(artist.artist);
            artist_html += "</div></div>";
          }
          artist_html += artist.build_tourdates(artist.artist);
          artist_html += artist.build_videos(artist.artist);
          artist_html += artist.build_sounds(artist.artist);          
          
          artist_html += "</div>";
          
          
          artist_html += "</article>";
          return artist_html;
        }
        return null;
      },
      
      build_images: function(artist){
        var artist_html = "";
        if(artist.images!=null){
          artist_html += "<div class=\"cycle\">";
          for(var i in artist.images){
            var image = artist.images[i];
            artist_html += image.html;
          }
          artist_html += "</div>";
        }
        return artist_html;
      },
      
      build_videos: function(artist){
        var artist_html = "";
        if(artist.videos!=null){
          artist_html += "<div id=\"tab_artist_videos\" class=\"tab j_sp\" style=\"display:none;\">";
          for(var i in artist.videos){
            var video = artist.videos[i];
            artist_html += video.embed;
          }
          artist_html += "</div>";
        }
        return artist_html;
      },
      
      build_sounds: function(artist){
        var artist_html = "";
        if(artist.sounds!=null){
          artist_html += "<div id=\"tab_artist_sounds\" class=\"tab j_sp\" style=\"display:none;\">";
          for(var i in artist.sounds){
            var sound = artist.sounds[i];
            artist_html += "<div id=\"artist_sound_"+sound.unique_id+"\" class=\"artist_sound\"><div id=\"artist_sound_"+sound.unique_id+"_inner\" class=\"inner_sound\">"+sound.unique_id+"</div></div>";
          }
          artist_html += "</div>";
        }
        return artist_html;
      },
      
      embed_sounds: function(_artist){
        
        if(_artist.sounds!=null){
          
          for(var i in _artist.sounds){
            var sound = _artist.sounds[i];
            
            artist.get_from_api(sound,function(sound,api_data){
              var swf_object_sound = {
                flashvars : {
                  url: api_data.uri,
                  show_comments: false,
                  auto_play: false,
                  color: 'b20303'
                },
                params : {allowScriptAccess: "always",menu: "false"},
                attributes : false,
                url : "http://player.soundcloud.com/player.swf",
                id: "artist_sound_"+sound.unique_id+"_inner",
                width: "100%",
                height: "81"
              };
              swfobject.embedSWF(swf_object_sound.url,swf_object_sound.id,swf_object_sound.width,swf_object_sound.height,"9.0.0","/swf/expressInstall.swf",swf_object_sound.flashvars,swf_object_sound.params,swf_object_sound.attributes);
              
            });
                        
          }
          $("#tab_artist_sounds").jScrollPane();
        }      
         
        
      },
      
      get_from_api: function(sound,callback){
        var uri = "http://api.soundcloud.com/tracks/"+sound.sound_id+".json?client_id=87f36a71f7b3400517789e735e8df123";
        $.getJSON(uri, function(data){
          callback(sound,data);
        });
      },
      
      
      build_links: function(artist){
        var artist_html = "";
        if(artist.links!=null){
          artist_html += "<div id=\"tab_artist_links\" class=\"tab\"><ul>";
          for(var i in artist.links){
            var link = artist.links[i];
            artist_html += "<li>"+link.description+": <a href=\""+link.uri+"\">"+link.uri+"</a></li>";
          }
          artist_html += "</ul></div>";
        }
        return artist_html;
      },
      
      build_contacts: function(artist){
        var artist_html = "";
        if(artist.contacts!=null){
          artist_html += "<div id=\"tab_artist_contacts\" class=\"tab\"><ul>";
          for(var i in artist.contacts){
            var contact = artist.contacts[i];
            artist_html += "<li>"+contact.contact_type+": <a href=\"mailto:"+contact.email+"\">"+contact.email+"</a></li>";
          }
          artist_html += "</ul></div>";
        }
        return artist_html;
      },
      
      build_tourdates: function(artist){
        var artist_html = "";
        if(artist.tourdates!=null){
          artist_html += "<div id=\"tab_artist_tourdates\" class=\"tab j_sp\" style=\"display:none;\"><ul>";
          for(var i in artist.tourdates){
            var tourdate = artist.tourdates[i];
            artist_html += "<li><strong>"+tourdate.scheduled_at+"</strong>: "+tourdate.event+" / "+tourdate.description+"</li>";
          }
          artist_html += "</ul></div>";
        }
        return artist_html;
      },
      
      _click: function(){
        $this.click(function(e){
          e.preventDefault();
          artist.load_artist(function(){
            $("#modal").modal({html: artist.build(), callback: function(modal,_modal){
              artist.post_build(modal,_modal);
            }});
          });
        });
      },
      
      
      _hover: function(){
        $this.css("opacity",0.75);
        $this.hover(
          function(){
            $this.stop().fadeTo('fast',1);
          },
          function(){
            $this.stop().fadeTo('fast',0.75);
          }
        );
      },
      
      post_build: function(modal,_modal){
        artist.modal = modal;
        modal.find(".tab_nav").tab_nav({
          full: function(tab,callback){artist._full(tab,callback);},
          not_full: function(callback){artist._not_full(callback);},
          after_click: function(tab){artist.after_click(tab);}
        });
        artist.embed_sounds(artist.artist);
        modal.find(".col_1").jScrollPane();
      },
      
      _full: function(tab,callback){
        artist.modal.find("header").animate({height:64},'fast');
        artist.modal.find(".body").animate({height:460},'fast',function(){
          callback();
        });
        
      },
      
      _not_full: function(callback){
        artist.modal.find("header").animate({height:320},'fast');
        artist.modal.find(".body").animate({height:188},'fast',function(){
          callback();
        });
      },
      
      after_click: function(tab){
        if(tab.hasClass("j_sp")){
          tab.jScrollPane();
          tab.find(".col_1").jScrollPane();
        }
      }
      
      
      
    };
    artist.init();
  });

};


// Modal
$.fn.modal = function(options){
  
  options = $.extend({
    opacity: 0.75,
    margin: 96,
    html: null
  },options);
  
  return this.each(function(){
    var $this = $(this);
    var modal = {

      overlay: null,
      content: null,
      content_height: 0,
      full: false,
      close: null,
      
      init: function(){
        modal.prepare();
        modal.build();
        modal._resize();
        $(window).resize(function(){
          modal._resize();
        });
        modal._show();
      },
      
      prepare: function(){
        $this.html("<div id=\"modal_overlay\"></div><div id=\"modal_content\"><div class=\"inner\"></div></div><a id=\"modal_close\" href=\"#\">x</a>");
        modal.overlay = $("#modal_overlay");
        modal.overlay.css("opacity",options.opacity);
        modal.content = $("#modal_content");
        modal.content_height = modal.content.height();
        modal.close = $("#modal_close");
        
        modal.overlay.click(function(){
          modal._hide();
        });
        modal.close.click(function(e){
          e.preventDefault();
          modal._hide();
        });
      },
      
      _resize: function(){},
      
      _show: function(){
        $this.fadeIn('slow');
        if(typeof options.callback == 'function'){
          options.callback($this,modal);
        }
      },
      
      _hide: function(){
        $this.fadeOut('slow',function(){
          modal._reset();
        });
      },
      
      _reset: function(){
        $this.html("");
        modal.overlay = null;
        modal.content = null;
        modal.content_height = 0;
      },
      
      build: function(){
        if(options.html!=null){
          modal.content.find(".inner").html(innerShiv(options.html));
        }
      }
      
    };
    modal.init();
  });

};

$.fn.tab_nav = function(options){
  
  options = $.extend({
    full: null,
    not_full: null,
    after_click: null
  },options);
  
  return this.each(function(){
    var $this = $(this);
    var tab_nav = {
      
      active_tab: null,
      full: false,
      
      init: function(){
        tab_nav.prepare();
        tab_nav.init_click();
      },
      
      prepare: function(){
        tab_nav.active_tab = $this.find(".active").children("a").attr("href");
      },
      
      init_click: function(){
        $this.find("a").click(function(e){
          e.preventDefault();
          var target = $(this).attr("href");
          $(tab_nav.active_tab).hide();
          $(target).show();

          tab_nav.active_tab = target;
          $(this).parent().parent().find(".active").removeClass("active");
          $(this).parent().addClass("active");
          
          if(!tab_nav.full && $(this).hasClass("full") && options.full!=null && typeof options.full == 'function'){
            tab_nav.full = true;
            options.full($(target),function(){
              if(options.after_click!=null && typeof options.after_click == 'function'){
                options.after_click($(target));
              }
            });
          }else if(tab_nav.full && !$(this).hasClass("full") && options.not_full!=null && typeof options.not_full == 'function'){
            tab_nav.full = false;
            options.not_full(function(){
              if(options.after_click!=null && typeof options.after_click == 'function'){
                options.after_click($(target));
              }
            });
          }else if(options.after_click!=null && typeof options.after_click == 'function'){
            options.after_click($(target));
          }
        });
      }
      
    };
    tab_nav.init();
  });

};


$(document).ready(function(){  
  $.app.init();
});

