var sm;
var smSound = null;
var playingurl;
var lock = false;

jQuery(document).ready(function() {
	jQuery(".preview_link_js").click(function(e) {
    if ( lock == true ) return false;
    lock = true;
    setTimeout(function(){lock = false;}, 250);
	if ( window.playingurl == jQuery(this).attr("aref") ) {
		//if(console)console.log("stopped ",playingurl);
			if(smSound) {
				smSound.stop();
				smSound.destruct();
				smSound = null;
			}
			playingurl = null;
			return false;
		}
        playTrack(jQuery(this), jQuery(this).attr("aref"));
	e.stopPropagation();
	e.preventDefault();
	});
   
});
function prehear(url) {
playTrack(null, url);
}

function preview(url,elem) {
        var theURL = url;
        if ( url.match(/\.mp3$/i) ) {        
                if(jQuery(elem).hasClass("stop_en")==true){
                        smSound.stop();
                }else{
                        jQuery(elem).addClass("stop_en");
                        jQuery(elem).removeClass("play_en1");                        
                        if (smSound) smSound.stop();
                        soundManager.destroySound('track');
                        smSound = soundManager.createSound({
                                id:'track',
                                url:theURL,
                                onfinish:function(){           
                                        jQuery(elem).addClass("play_en1");
                                        jQuery(elem).removeClass("stop_en");
                                        
                                        
                                },
                                onstop:function(){                
                                        jQuery(elem).addClass("play_en1");
                                        jQuery(elem).removeClass("stop_en");
                                        
                                }
                        });
                smSound.play();
                }
        }else if(url.match(/\.flv$/i)) {
              // jQuery.fn.colorbox({html:"<div id='player' style='width:320px;height:240px;margin:auto;overflow:hidden;'></div>", innerWidth:"322px", innerHeight:"242px", scrolling:'false',scalePhotos:'true'}) 
              // flowplayer("player", "http://www.mastermixdigital.com/js/flowplayer/flowplayer-3.1.5.swf" ,url)
              if(jQuery(elem).attr("src")=="/images/stop_en.png"){
                        smSound.stop();
                }else{
                        jQuery(elem).attr("src","/images/stop_en.png");        
                        if (smSound) smSound.stop();
                        soundManager.destroySound('track');
                        smSound = soundManager.createSound({
                                id:'track',
                                url:theURL,
                                onfinish:function(){                
                                        jQuery(elem).attr("src","/images/play_en1.png");
                                },
                                onstop:function(){                
                                        jQuery(elem).attr("src","/images/play_en1.png");
                                }
                        });
                smSound.play();
                }
        }
        
}  


function playTrack (me, url) {
    function setPlaying() {
jQuery(me).children(".no-margin").attr('src', '/images/stop_en.png');
  if (jQuery(me).is(":not(:has(div))") && jQuery(me).is(":not(:has(.no-margin))")) jQuery(me).html("<img style='margin-left:0px;' src='http://images.mastermixdj.com/m/buttons/Large/stop_button.png' alt='click to stop preview'/>");
playingurl = url;
    }
    function setStopped() {
jQuery(me).children(".no-margin").attr('src','/images/play_en1.png');
 if (jQuery(me).is(":not(:has(div))") && jQuery(me).is(":not(:has(.no-margin))")) jQuery(me).html("<img style='margin-left:0px;' src='http://images.mastermixdj.com/m/buttons/Large/play_button.png' alt='click to play preview again'");
playingurl = null;
    }
    function setLoading() {
    jQuery(me).children(".no-margin").attr('src','/images/mastermixloading.gif');
     if (jQuery(me).parents().is(".yellow")){
        jQuery(me).children(".no-margin").attr('src' , '/images/bassline-loader.gif');
     }
 if (jQuery(me).is(":not(:has(div))") && jQuery(me).is(":not(:has(.no-margin))")){
  jQuery(me).html("<img style='height:79px;margin-left:0px;' src='http://images.mastermixdj.com/m/buttons/Large/loading.gif' alt='Loading'");
    if (jQuery(me).parents().is(".yellow")){
        jQuery(me).html("<img style='height:79px;margin-left:0px;' src='http://images.mastermixdj.com/m/buttons/Large/bassline-ajax-loader.gif' alt='Loading'" );
    }
  }
playingurl = url;
    }
    //if(console)console.log("in with ",me," and ",url,", playing ",playingurl);
	if(smSound){
	smSound.stop();
	smSound.destruct();
	smSound = null;
	}
	setLoading();
	
    smSound = soundManager.createSound({id: 'sound0', url: url, autoLoad: true, autoPlay: true,
        onfinish: setStopped,
        onload: setPlaying, onstop: setStopped
        });

	//if(console)console.log("playing: ", playingurl);
    return false;
}


//http://images.mastermixdj.com/m/buttons/Large/stop_button.png
function Ppreview(url,elem) {
        var theURL = url;
        if ( url.match(/\.mp3$/i) ) {
                if(jQuery('.PlayNow:hidden').attr("src")=="http://images.mastermixdj.com/m/buttons/Large/play_button.png"){                
                        smSound.stop();
                }else{
                        jQuery('.PlayNow').toggle();
                        jQuery('.PlayText').children('span').toggle();
                        if (smSound) smSound.stop();
                        soundManager.destroySound('track');
                        smSound = soundManager.createSound({
                                id:'track',
                                url:theURL,
                                onfinish:function(){                
                                        jQuery('.PlayNow').toggle();
                                        jQuery('.PlayText').children('span').toggle(); 
                                },
                                onstop:function(){                
                                        jQuery('.PlayNow').toggle();
                                        jQuery('.PlayText').children('span').toggle(); 
                                }
                        });
                smSound.play();
                }
        }else if(url.match(/\.flv$/i)) {
              
              if(jQuery('.PlayNow').attr("src")=="http://images.mastermixdj.com/m/buttons/Large/stop_button.png"){
                        smSound.stop();
                }else{
                        jQuery('.PlayNow').attr("src","http://images.mastermixdj.com/m/buttons/Large/stop_button.png");        
                        if (smSound) smSound.stop();
                        soundManager.destroySound('track');
                        smSound = soundManager.createSound({
                                id:'track',
                                url:theURL,
                                onfinish:function(){                
                                        jQuery('.PlayNow').attr("src","http://images.mastermixdj.com/m/buttons/Large/play_button.png");
                                },
                                onstop:function(){                
                                        jQuery('.PlayNow').attr("src","http://images.mastermixdj.com/m/buttons/Large/play_button.png");
                                }
                        });
                smSound.play();
                }
        }
        
}  