function getSearchParams(k){
 var p={};
 location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(s,k,v){p[k]=v})
 return k?p[k]:p;
}
var pagedisplayid ;
//var genres =[];
window.onload = function() {




pagedisplayid = getSearchParams("displaygroupid"); 
refreshDisplayGroupDetail(pagedisplayid);

//alert(imdbid);
}










function refreshDisplayGroupDetail(displayid){
//$('#j_4').empty();
//$('#j_4').append("<div id=\"status\">Getting Posters, please wait....</div>");


   var eventrequest= $.ajax({ 
	  url: "displaygroups.json?get("+displayid+")", 
	   dataType: "text"});
	  eventrequest.done(function(data){


var displayjson=jQuery.parseJSON(data);
//alert(displayjson.title);
document.title="Details for ("+displayid+") '"+displayjson.friendlyname+"'";
//$('#textboxid').val('some value')
$('#imdbidf').val(displayid);

$('#friendlynamef').val(displayjson.friendlyname);
refreshDisplaysInGroup(displayid);
      });
	  
}

function refreshDisplaysInGroup(displayid){
//$('#j_4').empty();
//$('#j_4').append("<div id=\"status\">Getting Posters, please wait....</div>");


   var eventrequest= $.ajax({ 
	  url: "displaysingroup.json?get("+displayid+")", 
	   dataType: "text"});
	  eventrequest.done(function(data){
	  var name="";
var displayjson=jQuery.parseJSON(data);
for (let key in displayjson) {
 if (!displayjson.hasOwnProperty(key)) { continue; }
//var name =displayjson[key].friendlyname;
var func = "";
if(displayjson[key].function==0){
func = "Primary";
}else if(displayjson[key].function==1){
func = "Auxiliary";
}else if(displayjson[key].function==2){
func = "Info";
}
name +="<li>ID:"+displayjson[key].displayid+" | "+ displayjson[key].friendlyname+" | Function: "+func+"</li>";
 


}
  $('#displayList').append(name );
      });
	  
}

String.prototype.replaceAll = function(search, replace) {
if (replace === undefined) {
    return this.toString();
}
return this.split(search).join(replace);
}



function test(value){
alert(value);
}


function getfilenamewithoutextension(filepath){
var result = "";

//extract just the filename form the full path. 

return result;
}


//$('#reload_local').bind('click', function(event){
 $(document).delegate('#reload_local', 'click', function() {
refreshDisplayGroupDetail(pagedisplayid);
});



$('#savebutton').bind('click', function(event)
{

var savedata = {
displaygroupid:pagedisplayid,
friendlyname:$('#friendlynamef').val(),

};
//alert(JSON.stringify(savedata));
//var detailjson=objectifyForm($('#detailform').serializeArray());


//alert(JSON.stringify(detailjson));
 //var data = {'bob':'foo','paul':'dog'};
$.ajax({
   url: "updatedisplaygroupsettings.json",
   type: 'POST',
   contentType:'application/json',
   data: JSON.stringify(savedata),
   dataType:'json',
   success: function(data){
     //On ajax success do this
     //alert(data);
	 if(data=="Success"){
	// alert("data has been saved");
	 		 $.notifyBar({ cssClass: "custom", html: "Data for '"+savedata.friendlyname+"' ("+savedata.displaygroupid+") has been saved.",position: "bottom"});

	 //reload the json for this movie
	 refreshDisplayGroupDetail(pagedisplayid);
	// alert("data refreshed for "+imdbid);
	 }
      },
	  error: function(XMLHttpRequest, textStatus, errorThrown){
	  	 		 $.notifyBar({ cssClass: "custom", html: "Error updating'"+savedata.friendlyname+"' ("+savedata.displaygroupid+"). "+errorThrown,position: "bottom"});

	 // alert(textStatus+" "+errorThrown);
	  }
 });
 
});














var firstpoll = true;

function pollStatus(){

   setTimeout(function(){
  // this.alert("in function");
   var eventrequest= $.ajax({ 
	  url: "status.json", 
	   dataType: "text"});  
	  eventrequest.done(function(data){
	  var statusjson=jQuery.parseJSON(data);
var items = [];
for (let key in statusjson) {
 if (!statusjson.hasOwnProperty(key)) { continue; }
 parseCommand(statusjson[key]);
 }
 
 firstpoll=false;

        //Setup the next poll recursively
        pollStatus();
      });
	  eventrequest.error(function(){pollStatus()});
	  
	  
  }, 2000); //every 2 seconds
  
}

var processedStatusMessages = new Array(25); //array of length 15 to store messages
function parseCommand(com){
//alert(com.message);
if(jQuery.inArray(com.id,processedStatusMessages)<0){
//alert(com.id+" is not processed");
processedStatusMessages.shift();
processedStatusMessages.push(com.id);
if(firstpoll==false){
var messageObj = jQuery.parseJSON(com.message);
if(messageObj.action=="cached"||messageObj.action=="updated"){
		 $.notifyBar({ cssClass: "custom", html: "'"+messageObj.title+"' has been saved.",position: "bottom"});
}else if(messageObj.action=="deleted"){
		 $.notifyBar({ cssClass: "custom", html: "'"+messageObj.title+"' has been deleted.",position: "bottom"});	
}else if(messageObj.action=="trailersaved"){
		 $.notifyBar({ cssClass: "custom", html: "Trailer for '"+messageObj.title+"' has been saved.",position: "bottom"});
	
}
}

}else{
//alert(com.id+" has already been processed");
}

}





$(document).ready(function () {
    $('.slideout-menu-toggle').on('click', function(event){
    	event.preventDefault();
		event.stopImmediatePropagation();
    	// create menu variables
    	var slideoutMenu = $('.slideout-menu');
    	var slideoutMenuWidth = $('.slideout-menu').width();
    	
    	// toggle open class
    	slideoutMenu.toggleClass("open");
    	
    	// slide menu
    	if (slideoutMenu.hasClass("open")) {
	    	slideoutMenu.animate({
		    	left: "0px"
	    	});	
    	} else {
	    	slideoutMenu.animate({
		    	left: -slideoutMenuWidth
	    	}, 250);	
    	}
    });
});

$(document).ready(function () {
  //  $(":jqmData(role='editablelistview'), :jqmData(role='editable-listview')", e.target).editablelistview();
// $('#editlist').editablelistview();
    $('.slideout-menu-right-toggle').on('click', function(event){
	
    	event.preventDefault();
		event.stopImmediatePropagation();
    	// create menu variables
    	var slideoutMenu = $('.slideout-menu-right');
    	var slideoutMenuWidth = $('.slideout-menu-right').width();
    	
    	// toggle open class
    	slideoutMenu.toggleClass("open");
    	
    	// slide menu
		var width = $(document).width()- slideoutMenuWidth;
    	if (slideoutMenu.hasClass("open")) {
	    	slideoutMenu.animate({
		    	left: width 
	    	});	
    	} else {
	    	slideoutMenu.animate({
		    	left: "100%"
	    	}, 250);	
    	}
    });
});

function closeActionsMenu(){
    	var slideoutMenu = $('.slideout-menu-right');
    	var slideoutMenuWidth = $('.slideout-menu-right').width();
    	
    	// toggle open class
		if (slideoutMenu.hasClass("open")) {
    	slideoutMenu.toggleClass("open");
		}
    	
    	// slide menu
		var width = $(document).width()- slideoutMenuWidth;
    	if (slideoutMenu.hasClass("open")) {
	    	slideoutMenu.animate({
		    	left: width 
	    	});	
    	} else {
	    	slideoutMenu.animate({
		    	left: "100%"
	    	}, 250);	
    	}
}

$(window).resize(function(){
    closeActionsMenu();
});


