var timerRunning = false; 

$(document).ready(function(){

var loaded = false;

if(window.history.pushState)  { 

 window.onpopstate = function(event) {

   if(event.state) {
     loaded = true;
     var link = '';
     var page = event.state.page;
     var linkID = event.state.linkID;
     var groupID = event.state.groupID;
     var parentID = event.state.parentID;
     var artistVal = event.state.artistVal;
     var labelVal = event.state.labelVal;
     if(page == 'catalog.php') {
       if(artistVal > 0) {
         link = 'artist_id='+artistVal;
       } else if(labelVal > 0) {
         link = 'label_id='+labelVal;
       } else if(linkID != undefined) {
         link = 'cPath='+linkID;
       }
     } else if(page == 'cms.php') {
       if(parentID > 0) {
         link = 'content_id='+linkID+'&group_id='+groupID+'&parent_id='+parentID;
       } else if(groupID > 0) {
         link = 'content_id='+linkID+'&group_id='+groupID;
       } else {
         link = 'cgID='+linkID;
       }
     } else if(page == 'product_info.php') {
       link = 'products_id='+linkID;
     }
     
     jQuery.ajax({'url': page+'?'+link+'&sessID='+sessVal,
                  'cache':false, 
                  'beforeSend':function(){showLoader()},
                  'complete':function(){removeLoader()},
                  'success':function(html){
                    $("#contentBody").html(html);
                    scrollPage();
                    loadLinks();
                    stopTimer();
                    setTitle();
                  }
     });
     return;
   } else {
     if(loaded == true) window.location = requestPage;
     return;
   }
 }
}   

  $("#jquery_jplayer").jPlayer({supplied: "mp3, oga", swfPath: "http://localhost/monette_live"});

  $(".homeLink").click(function() {
    setHistory($(this));
    jQuery.ajax({'url':'catalog.php?sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   scrollPage();
                   loadLinks();
                   setTitle();
                 }
    })
    return false;
  });

  $(".cartLink").click(function() {
    setHistory($(this));
    
    jQuery.ajax({'url':'shopping_cart.php?sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   scrollPage();
                   loadLinks();
                   stopTimer();
                   setTitle();
                 }
    })
    return false;
  });
  
  $(".relLink").click(function() {
    setHistory($(this));
    stopTimer();
    jQuery.ajax({'url':'new_releases.php?sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   scrollPage();
                   loadLinks();
                   setTitle();
                 }
    })
    return false;
  });  

  $(".alLink").click(function() {
    setHistory($(this));
    stopTimer();
    jQuery.ajax({'url':'top_albums.php?sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   scrollPage();
                   loadLinks();
                   setTitle();
                 }
    })
    return false;
  }); 

  $(".sLink").click(function() {
    setHistory($(this));
    stopTimer();
    jQuery.ajax({'url':'top_singles.php?sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   scrollPage();
                   loadLinks();
                   setTitle();
                 }
    })
    return false;
  });
  
  $(".npLink").click(function() {
    setHistory($(this));
    stopTimer();
    jQuery.ajax({'url':'products_new.php?sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   scrollPage();
                   loadLinks();
                   setTitle();
                 }
    })
    return false;
  });  
  
  $(".cgLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    jQuery.ajax({'url':'cms.php?cgID='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   scrollPage();
                   loadLinks();
                   setTitle();
                 }
    })
    return false;
  });
  
  $(".singlesLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    jQuery.ajax({'url':'product_info.php?products_id='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks(); 
                   scrollPage();
                   setTitle();
                 }
    })
    return false;
  });
  
  
 
  
  $(".artistsLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    jQuery.ajax({'url':'catalog.php?artist_id='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks();
                   scrollPage();
                   setTitle(); 
                 }
    })
    return false;
  });       
     
  loadLinks(); 

	$("#fb").append('<span class="fb">Powered by <a href="http://www.suomedia.com" target="_blank">Suocommerce</a></span>');  
     
});
   
   $("#jquery_jplayer").jPlayer("onSoundComplete", function() {
     $('.playerStop').removeClass('playerStop').addClass('playerPlay');

   });

function loadLinks() {

  $(".homeLink").unbind('click');
  $(".cgLink").unbind('click');
  $(".genLink").unbind('click');
  $(".catLink").unbind('click');
  $(".pLink").unbind('click');
  $(".artistLink").unbind('click');
  $(".labelLink").unbind('click');
  $(".contentLink").unbind('click');
  

  
  $(".homeLink").click(function() {
    setHistory($(this));
    jQuery.ajax({'url':'catalog.php?sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   scrollPage();
                   loadLinks();
                   setTitle();
                 }
    })
    return false;
  });
  
  $(".cgLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    jQuery.ajax({'url':'cms.php?cgID='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   scrollPage();
                   loadLinks();
                   setTitle();
                 }
    })
    return false;
  });  

  $(".genLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    jQuery.ajax({'url':'catalog.php?cPath='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks();
                   scrollPage();
                   setTitle(); 
                 }
    })
    return false;
  }); 
  
  $(".catLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    jQuery.ajax({'url':'catalog.php?cPath='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks();
                   scrollPage();
                   setTitle(); 
                 }
    })
    return false;
  });  
  
  $(".pLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    jQuery.ajax({'url':'product_info.php?products_id='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks(); 
                   scrollPage();
                   setTitle();
                 }
    })
    return false;
  });
  
  $(".artistLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    jQuery.ajax({'url':'catalog.php?artist_id='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks();
                   scrollPage();
                   setTitle(); 
                 }
    })
    return false;
  });

  $(".labelLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    jQuery.ajax({'url':'catalog.php?label_id='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks();
                   scrollPage();
                   setTitle(); 
                 }
    })
    return false;
  });
  
  $(".contentLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    var linkGroup = $(this).attr("data-group");
    jQuery.ajax({'url':'cms.php?content_id='+linkID+'&group_id='+linkGroup+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks();
                   scrollPage();
                   setTitle(); 
                 }
    })
    return false;
  });
  
  $(".contentSubLink").click(function() {
    setHistory($(this));
    stopTimer();
    var linkID = $(this).attr("data-id");
    var linkGroup = $(this).attr("data-group");
    var linkParent = $(this).attr("data-parent");
    jQuery.ajax({'url':'cms.php?content_id='+linkID+'&group_id='+linkGroup+'&parent_id='+linkParent+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks();
                   scrollPage();
                   setTitle(); 
                 }
    })
    return false;
  });     
  


   $('.play_track').toggle(function(){
     if($(this).hasClass("playerPlay")) {
       $('.playerStop').removeClass('playerStop').addClass('playerPlay');
       $("#jquery_jplayer").jPlayer("pause");
       $(this).removeClass('playerPlay').addClass('playerStop');
       var trackUrl = $(this).attr("data-rel");
       var trackUrl2 = trackUrl.replace("mp3", "ogg");
       $("#jquery_jplayer").jPlayer("setMedia",{mp3:trackUrl,oga:trackUrl2}).jPlayer("play");
     } else {
       $("#jquery_jplayer").jPlayer("pause");
       $(this).removeClass('playerStop').addClass('playerPlay')
     }  
   }, function(){
     if($(this).hasClass("playerStop")) {
       $("#jquery_jplayer").jPlayer("pause");
       $(this).removeClass('playerStop').addClass('playerPlay');
     } else {
       $('.playerStop').removeClass('playerStop').addClass('playerPlay');
       $("#jquery_jplayer").jPlayer("pause");
       $(this).removeClass('playerPlay').addClass('playerStop');
       var trackUrl = $(this).attr("data-rel");
       var trackUrl2 = trackUrl.replace("mp3", "ogg");
       $("#jquery_jplayer").jPlayer("setMedia",{mp3:trackUrl,oga:trackUrl2}).jPlayer("play");
      }
     });    

}

function loadArtist() {
    var linkID = $("#artistList option:selected").val();
    var option = $("#artistList option:selected");
    stopTimer();
    jQuery.ajax({'url':'catalog.php?artist_id='+linkID+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                   $("#contentBody").html(html);
                   loadLinks();
                   scrollPage(); 
                   setTitle();
                   setHistory(option);
                 }
    })
    return false;  
}

function loadFeatured(er) {
    stopTimer();
    var link_id = er;
    var loc = $("#"+link_id).attr("href");
    pageData=new Object();             
    pageData = {linkID:link_id,page:'catalog.php',artistVal:link_id};
    window.history.pushState(pageData, '', loc);
    jQuery.ajax({'url':'catalog.php?artist_id='+link_id+'&sessID='+sessVal,
                 'cache':false, 
                 'beforeSend':function(){showLoader()},
                 'complete':function(){removeLoader()},
                 'success':function(html){
                    $("#contentBody").html(html);
                    loadLinks();
                    scrollPage();
                    setTitle();   
                   } 
                 });
                 

    return false;
}

function scrollPage() {
   $('html,body').animate({ scrollTop: $("BODY").offset().top }, { duration: 'slow'});
}

function showLoader() {
 var pageHeight = ($(document).height());
 $("#contentLoader").css("height", pageHeight+"px");
  $("#contentLoader, #loader-panel").fadeTo('slow', 0.8);
}

function removeLoader() {
  $("#contentLoader, #loader-panel").fadeOut("slow");
}

function setHistory(elem) {
  if(window.history.pushState)  {

    var loc = $(elem).attr('href');
    if(!loc) {
      var loc = $(elem).attr("data-url");
    }
    var page_data = $(elem).attr("data-page");
    var link_id =  $(elem).attr("data-id");
    var group_id =  $(elem).attr("data-group");
    var parent_id =  $(elem).attr("data-parent");
    var artist = $(elem).attr("data-artist");
    var label = $(elem).attr("data-label");
    pageData=new Object();
    pageData = {page:page_data,linkID:link_id,groupID:group_id,parentID:parent_id,artistVal:artist,labelVal:label}; 
    window.history.pushState(pageData, '', loc);
  }
}

function setTitle() {
  document.title = $("#pageTitle").val();
}

function stopTimer() {
    if (timerRunning)
        window.clearInterval(playSlideshow); 
}
