if (window.console && window.console.firebug) {
  
  console.log("Debugging YO!")

}else{
          
    if (! ("console" in window) || !("firebug" in console)) {
        var names = ["log", "debug", "info", "warn", "error", "assert", "dir", "dirxml", "group"
                     , "groupEnd", "time", "timeEnd", "count", "trace", "profile", "profileEnd"];
        window.console = {};
        for (var i = 0; i <names.length; ++i) window.console[names[i]] = function() {};
    }
}



function Modal(){};

Modal.prototype = {
    bg: "<div id='modal_bg'><div id='close_button'></div></div>",
    modal_wrap: "<div id='modal_wrap'></div>",
    
    init: function() {
        $('body').append(this.bg)
        $('#modal_bg').click(function(){
            modal.close();
        });

        $(window).resize(function(){
            modal.reposition_modal();
        });
        
        $('#music_thumbs li').click(function(){
                    var rel = $(this).attr("rel")
                    var options = new Array();
                       options = rel.split(',')
                       
                       var div_id = options[0]
        
                    modal.show({type: "music", target_div: div_id})
        
                        return false;   
                });
	       
	       $('.art_thumbs li a').click(function(){
               var rel = $(this).attr("rel")
               var options = new Array();
               options = rel.split(',')

               // options_array: div_id, height, width
               console.log(options)

               var div_id = options[0]

               var lg_img = $("#"+options[0]+" >img")
               console.log(lg_img)
               
   	           // var width = lg_img.attr("width") + 200
   	           var width = parseInt(options[1]) + 200
   	           // var height = lg_img.attr("height")
               var height = parseInt(options[2])
               
   	           console.log(div_id)
   	           console.log(width + ", " + height)

               modal.show({type: "art", target_div: div_id, width: width, height: height})

               return false;

	       });
	       
	       $('.art').each(function(){
	           $(this).prepend("<img src='"+$(this).attr('rel')+"' />")
	       })
	       
    },
    
    show: function(options) {
        this.options = options
        console.log(this.options)
        
         $('#modal_bg')
                .css({"opacity": 0.90})
                .fadeIn("slow", function() {
                        modal.check_modal_type()}
                        )
        
         
    },
    
    close: function() {
        $('#modal_bg')
            .fadeOut("slow")
        $("#modal_wrap, #thumb_wrapper, #thumb_slider").remove();
    },
    
    check_modal_type: function() {
        if(this.options.type == "music") {
            modal.populate_music()
        } else {
            modal.populate_art()      
        }  
    },
    
    populate_music: function() {
        
        var win_width = $(window).width()
        var win_height = $(window).height()
        
        this.modal_width = 970
        this.modal_height = 550
        
        var content =  $("#"+this.options.target_div).clone().prependTo('body');
        var songs = $(content).find("ol.songs li")
        console.log(songs)
        
        $(content).wrap(this.modal_wrap)
        
        var player = $(content).find(".cover").after("<div id='modal_player'><div id='player'></div></div>")
        console.log(player)
        
        $("#modal_wrap")
            .css({width: this.modal_width+"px", height: this.modal_height+"px", left: (win_width - this.modal_width)/2+"px", top: (win_height - this.modal_height)/2+"px" })
            .fadeIn("normal", function(){
                $(content).show("fast", function(){
                    var mp3s_array = new Array()
                    var titles_array = new Array()
                    $(songs).each(function(i){
                          console.log($(this))
                          var song = "/recordings/" + $(this).attr("rel")
                          var title = $(this).html()
                          if (song != "/recordings/undefined") {
                          mp3s_array.push(song)
                          titles_array.push(title)
                          }
                    });
                    if(mp3s_array.length > 0) {
                        AudioPlayer.embed("player", {soundFile: mp3s_array, titles: titles_array, transparentpagebg: "yes"})
                    }
                })
                
        })
            
    },
    
    reposition_modal: function() {
        if ($("#modal_wrap").length > 0) {
            var win_width = $(window).width()
            var win_height = $(window).height()
            $("#modal_wrap").css({left: ((win_width - $('#modal_wrap').width())/2) -20 +"px", top: (win_height - $("#modal_wrap").height())/3+"px"})
        }
    },
    
    populate_art: function() {
        console.log("art");
        
        var win_width = $(window).width()
        var win_height = $(window).height()
        
        this.modal_width = this.options.width
        this.modal_height = this.options.height
        
        var content = $("#"+this.options.target_div).clone().prependTo('body');  
        $(content).wrap(this.modal_wrap)
        
        var thumb_wrapper = "<div id='thumb_wrapper'><ul></ul></div>"
        $('body').append(thumb_wrapper)
        var thumbs = $('.art_thumbs li').clone()
        var thumbs_for_width = $('#thumb_wrapper ul').width(60 * thumbs.length)
        $('#thumb_wrapper ul').append($(thumbs))
        
        $('#thumb_wrapper a').click(function(){
              $("#modal_wrap div").remove();
              var rel = $(this).attr("rel")
  	          var options = new Array();
              options = rel.split(',') 
              var content = $('#'+options[0]).clone().appendTo('#modal_wrap');
              $("#modal_wrap")
                  .animate({width: (parseInt(options[1])+200)+"px", left: (win_width - parseInt(options[1])-240)/2+"px"}, function(){$(content).show()})
              return false; 
        })
        $('#thumb_wrapper').after("<div id='thumb_slider'><span id='abstract'>abstract</span><span id='playful'>playful</span></div>")  
        
        $("#modal_wrap")
            .css({width: this.modal_width+"px", height: this.modal_height+"px", left: ((win_width - this.modal_width)-40)/2+"px", top: (win_height - this.modal_height)/3+"px", padding: '20px'})
            .fadeIn("normal", function(){
                $(content).fadeIn("fast", function(){
                    $('#thumb_wrapper').animate({bottom: 20}, "normal", 
                    jQuery.easing.easeInOutElastic(), 
                    function(){
                        $('#thumb_slider').slider({
                            animate: true, 
                            orientation: 'horizontal', 
                            // max: thumbs_for_width.width()-win_width,
                            max:100,
                            slide: function(event, ui) {
                                // $('#thumb_wrapper ul').css({left: (-ui.value * ((thumbs_for_width.width()-win_width)/100))})
                                // $('#thumb_wrapper ul').css({ left: '-' + ui.value + 'px' });
                            },
                            stop: function(event, ui) {
                                var mv_left = -ui.value * ((thumbs_for_width.width()-win_width)/100)
                                $('#thumb_wrapper ul').animate({ left: mv_left }, 500);
                            }
                            });
                            
                        })
                })
            })
        
        
    }
    
}