$(document).ready(function() {
	

	

/////////Custom New Html Loading Made by Mage!!
var current_loc = location+"";
//////Click Load This
$('a.this_load').live('click', function() { 
$("#sm2-container").remove();
$(".page_load .loading").show();
history.pushState({ path: this.path }, '', this.href);

$('.top_playlist').animate({top:"-396px"},500);
$('.close_playlist').removeClass('close_playlist').addClass('show_playlist');

$('.content').load(
    this.href+"&in=one", 
    {},
    function() {
		
      //
      //this is the ajax callback 
      //
      $(".page_load .loading").fadeOut('slow');
    }
);   

return false
  
})
////////End Click Load This
   
 
 ////Back Load This  


    $(window).unload(function(e){    
        e.preventDefault();
        $(window).trigger('popstate ');      

    });    

    $(window).bind('popstate ',function(){
      if(current_loc!=location){
	  current_loc = location+"";
     $('.content').load(location+"&in=one");
	  }

    });

///////////////////////End Custom New Html Loading Here










$('.roll_video_image').mouseover(function(){
	
	var current_img = $(this).attr('src');
	if(!current_img.match(/ytimg/)) {
	var new_img = current_img.split('&img=');
	var exploded = new_img[1].split('.');
	$(this).attr('src',new_img[0]+'&img='+exploded[0]+'_1.jpg');
    $(this).delay(500).queue(function(){ 
	$(this).attr('src',new_img[0]+'&img='+exploded[0]+'_2.jpg');
	})
	$(this).delay(500).queue(function(){ 
	$(this).attr('src',new_img[0]+'&img='+exploded[0]+'_3.jpg');
	})
	$(this).delay(500).queue(function(){ 
	$(this).attr('src',new_img[0]+'&img='+exploded[0]+'_4.jpg');
	})
	
	var current_rolled_on = $(this);
	
	$('.roll_video_image').mouseout(function(){
	$(current_rolled_on).attr('src',current_img);
	
	})
	}
	
})

$(document).bind('click', function (e) {
var clicked = $(e.target);
          
if (!clicked.parents().hasClass("top_playlist")) {
if (!clicked.hasClass("show_playlist")) {
if (!clicked.hasClass("close_playlist")) {

      $('.top_playlist').animate({top:"-396px"},500);
	  $('.close_playlist').removeClass('close_playlist').addClass('show_playlist');

}
}
}
});
	
$('.play_in_pop_up').live('click',function(){
$('.top_playlist').animate({top:"-396px"},500);
$('.top_playlist').html('empty');
window.open( "playlist/?pop_up=yes", "Ersy.com | Pop Up Playlist", 
"status = yes, height = 367, width = 480, resizable = no,toolbars=no,menubar=no,location=no,");
})


$('.show_playlist').live('click',function(){
	var text = $(".top_playlist").text();
   
	
	$('.top_playlist').animate({top:"0"},500);
	$(this).removeClass('show_playlist').addClass('close_playlist');
	 if(text=="empty"){
    $(".top_playlist").html("<center>Loading...<img src='template/default/images/loading.gif'></center>");
	$('.top_playlist').load('playlist/');	
	}
	var show_playlist = true;
	
})
$('.close_playlist').live('click',function(){
	$('.top_playlist').animate({top:"-396px"},500);
	$(this).removeClass('close_playlist').addClass('show_playlist');

	
	
})


$('.add_to').live('click',function(){
	$(this).removeClass('add_to').addClass('remove_song_from_playlist');
	var song = $(this).attr('id');
	$.ajax({url: "add-to/?song="+song});
	$('.top_playlist').load('playlist/');	
	
})

$('.play_all').live('click',function(){
	var song = $(this).attr('id');
	$('.add_to').removeClass('add_to').addClass('remove_song_from_playlist');
	$.ajax({url: "add-to/?play_all="+song});
	$('.top_playlist').load('playlist/');	
	
})
$('.remove_all').live('click',function(){
	var song = $(this).attr('id');
	$('.remove_song_from_playlist').removeClass('remove_song_from_playlist').addClass('add_to');
	$.ajax({url: "add-to/?remove_all="+song});
	$('.top_playlist').load('playlist/');	
	
})

$('.shuffle_all').live('click',function(){
	$('.add_to').removeClass('add_to').addClass('remove_song_from_playlist');
	var song = $(this).attr('id');
	$.ajax({url: "add-to/?shuffle_all="+song});
	$('.top_playlist').load('playlist/');	
	
})

$('.play_selected').live('click',function(){


    $("input[type=checkbox]:checked").each(function(){
		var song = $(this).val();
		$("#"+song).removeClass('add_to').addClass('remove_song_from_playlist');
     $.ajax({url: "add-to/?song="+song});
    });
 $('.top_playlist').load('playlist/');
	
})

$('.remove_selected').live('click',function(){


    $("input[type=checkbox]:checked").each(function(){
		var song = $(this).val();
		$("#"+song).removeClass('remove_song_from_playlist').addClass('add_to');
     $.ajax({url: "add-to/?remove="+song});
    });
 $('.top_playlist').load('playlist/');
	
})

$('.remove_song_from_playlist').live('click',function(){
	$(this).removeClass('remove_song_from_playlist').addClass('add_to');
	var song = $(this).attr('id');
	$.ajax({url: "add-to/?remove="+song});
	$('.top_playlist').load('playlist/');	
	
})
$('.jp-playlist-item-remove').live('click',function(){
	
	var song = $(this).attr('id');
	$.ajax({url: "add-to/?remove="+song});
	//$('.top_playlist').load('playlist/');	
	
})

//////////Show Av///



$('.mix_av').live('mouseover',function(){
	var current = $(this);
     $(this).children('.total_m').show();
	
	$('.mix_av').mouseout(function(){
	  $(this).children('.total_m').hide();
	
	})
	
	
})

//////////Sticky Footer Play Toolbar


//settings
var fadeSpeed = 200, fadeTo = 0.5, topDistance = 30;
var sibar = function() { $('#stickybar').fadeTo(fadeSpeed,1); }, stbar = function() { $('#stickybar').fadeTo(fadeSpeed,fadeTo); };
var inside = false;
//do
$(window).scroll(function() {
position = $(window).scrollTop();
if(position > topDistance && !inside) {
//add mouseover events
stbar();
$('#stickybar').bind('mouseenter',sibar);
$('#stickybar').bind('mouseleave',stbar);
inside = true;
}
});
//close
$('#closebtn').live('click', function(event) {
$('#stickybar').toggle('show');
});
/////////////End Sticky Footer



///////Songs Autocomplete///////

	$(function() {
	
	   $(".search").autocomplete({
                source: "js/?type=search_top",
                minLength: 2,
             
            });
		
		
	});

////////Links Effect////

$('a.nudge').live('mouseover mouseout', function(event) {
  if (event.type == 'mouseover') {
      $(this).animate({ paddingLeft: '10px' }, 400);
  } else {
    $(this).animate({ paddingLeft: 0 }, 400);
  }
});

////Sticky Header
       var stickyHeaderTop = $('.top_bar').offset().top;
            $(window).scroll(function(){
                    if( $(window).scrollTop() > stickyHeaderTop ) {
                            $('.top_bar').addClass("sticky");
                    } else {
                            $('.top_bar').removeClass("sticky");
                    }
            })
	
});
