/*
 * JS for posterdetail generated by Appery.io
 *
 */

Appery.getProjectGUID = function() {
    return '8f17a3a7-092a-4731-9c40-cef53ddb40ee';
}

function navigateTo(outcome, useAjax) {
    Appery.navigateTo(outcome, useAjax);
}

function adjustContentHeight() {
    Appery.adjustContentHeight();
}

function adjustContentHeightWithPadding() {
    Appery.adjustContentHeightWithPadding();
}

function setDetailContent(pageUrl) {
    Appery.setDetailContent(pageUrl);
}

Appery.AppPages = [{
    "name": "posterdetail",
    "location": "posterdetail.html"
}];

posterdetail_js = function(runBeforeShow) { /* Object & array with components "name-to-id" mapping */
    var n2id_buf = {
        'mobilegrid_41': 'posterdetail_mobilegrid_41',
        'mobilegridcell_42': 'posterdetail_mobilegridcell_42',
        'mobileimage_51': 'posterdetail_mobileimage_51',
        'mobilegrid_7': 'posterdetail_mobilegrid_7',
        'mobilegridcell_8': 'posterdetail_mobilegridcell_8',
        'mobileimage_32': 'posterdetail_mobileimage_32',
        'mobilegridcell_9': 'posterdetail_mobilegridcell_9',
        'mobiletextinput_73': 'posterdetail_mobiletextinput_73',
        'mobiletextinput_74': 'posterdetail_mobiletextinput_74',
        'video_52': 'posterdetail_video_52',
        'mobilegrid_66': 'posterdetail_mobilegrid_66',
        'mobilegridcell_67': 'posterdetail_mobilegridcell_67',
        'mobilebutton_60': 'posterdetail_mobilebutton_60',
        'mobilebutton_61': 'posterdetail_mobilebutton_61',
        'mobilebutton_62': 'posterdetail_mobilebutton_62',
        'mobilebutton_65': 'posterdetail_mobilebutton_65',
        'mobilebutton_63': 'posterdetail_mobilebutton_63',
        'mobilebutton_64': 'posterdetail_mobilebutton_64',
        'mobilegridcell_71': 'posterdetail_mobilegridcell_71',
        'mobilegrid_33': 'posterdetail_mobilegrid_33',
        'mobilegridcell_34': 'posterdetail_mobilegridcell_34',
        'mobileimage_46': 'posterdetail_mobileimage_46',
        'mobilegridcell_35': 'posterdetail_mobilegridcell_35',
        'mobileimage_47': 'posterdetail_mobileimage_47',
        'mobilegridcell_38': 'posterdetail_mobilegridcell_38',
        'mobileimage_48': 'posterdetail_mobileimage_48',
        'mobilegridcell_39': 'posterdetail_mobilegridcell_39',
        'mobileimage_49': 'posterdetail_mobileimage_49',
        'mobilegridcell_40': 'posterdetail_mobilegridcell_40',
        'mobileimage_50': 'posterdetail_mobileimage_50'
    };

    if ("n2id" in window && window.n2id !== undefined) {
        $.extend(n2id, n2id_buf);
    } else {
        window.n2id = n2id_buf;
    }

    if (navigator.userAgent.indexOf("IEMobile") != -1) {
        //Fixing issue https://github.com/jquery/jquery-mobile/issues/5424 on Windows Phone
        $("div[data-role=page] div[data-role=footer]").css("bottom", "-36px");
    }

    if (Appery.getTargetPlatform == "I") {
        $.each(window.n2id, function(name, id) {
            var elApperyRole = $("#" + id).attr("data-role");
            if (elApperyRole != "appery_label" && elApperyRole != "appery_link") {
                var appleDataSelector = "#" + id + "[x-apple-data-detectors], #" + id + " [x-apple-data-detectors]";
                $(appleDataSelector).attr("x-apple-data-detectors", "false");
            }
        });
    }

    Appery.CurrentScreen = 'posterdetail';

    /*
     * Nonvisual components
     */
    var datasources = [];

    /*
     * Events and handlers
     */

    // Before Show
    posterdetail_beforeshow = function() {
        Appery.CurrentScreen = "posterdetail";
        for (var idx = 0; idx < datasources.length; idx++) {
            datasources[idx].__setupDisplay();
        }
    }

    // On Load
    screen_3671_onLoad = posterdetail_onLoad = function() {
        screen_3671_elementsExtraJS();

        // TODO fire device events only if necessary (with JS logic)
        posterdetail_deviceEvents();
        posterdetail_windowEvents();
        screen_3671_elementsEvents();
    }

    // screen window events
    screen_3671_windowEvents = posterdetail_windowEvents = function() {

        $('#posterdetail').bind('pageshow orientationchange', function() {
            adjustContentHeightWithPadding();
        });

    }

    // device events
    posterdetail_deviceEvents = function() {

        document.addEventListener("deviceready", function() {

        });
    }

    // screen elements extra js
    screen_3671_elementsExtraJS = posterdetail_elementsExtraJS = function() {
        // screen (posterdetail) extra code

    }

    // screen elements handler
    screen_3671_elementsEvents = posterdetail_elementsEvents = function() {

        $("a :input,a a,a fieldset label").live({
            click: function(event) {
                event.stopPropagation();
            }
        });

    }

    $("#posterdetail").die("pagebeforeshow").live("pagebeforeshow", function(event, ui) {
        posterdetail_beforeshow();
    });

    if (runBeforeShow) {
        posterdetail_beforeshow();
    } else {
        posterdetail_onLoad();
    }

}

$("#posterdetail").die("pageinit").live("pageinit", function(event, ui) {
    Appery.processSelectMenu($(this));
    posterdetail_js();
});

////my code
params={};location.search.replace(/[?&]+([^=&]+)=([^&]*)/gi,function(s,k,v){params[k]=v})

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

window.onload = function() {
//setTimeout(function(){window.scrollTo(0, 1);}, 100);
//refreshPosterList("");
var imdbid = getSearchParams("imdbid"); 
alert(imdbid);
}

function refreshCustomLists(){
$('#customlists').empty();
//$('#customlists').append("<div id=\"status\">Getting List Names.....</div>");

   var eventrequest= $.ajax({ 
	  url: "customlists.json", 
	   dataType: "text"});
	  eventrequest.done(function(data){

var displayjson=jQuery.parseJSON(data);
var items = [];
for (let key in displayjson) {
 if (!displayjson.hasOwnProperty(key)) { continue; }
var name =displayjson[key];
 var arg = "<li><a class=\"customlistselection\" id=\""+name+"\" href=\"#\" >"+name+"</a></li>";
 items.push(arg);
 
 //},0);//end settimeout
 // $('#j_4').append(arg); 
//$('#rateit'+displayjson[key].imdbid).rateit();
//delay(1);
//$('#status').text("Getting Posters, please wait.....  Found "+key).delay(10);
}
//$('#status').text("Getting Posters, Please Wait");


  $('#customlists').append( items.join('') );
//$('#customlists').listview('refresh');
 $('#customlists').trigger("create");
bindcustomlist();




      });
	  eventrequest.error(function(){refreshCustomLists()});
}

function bindcustomlist() {
$('.customlistselection').bind('click', function(event) {
//$(".customlistselection").click(function (event) {
//alert("custom list: "+this.id);
//get currently selected
var listname = this.id;
event.stopPropagation();
event.preventDefault();
event.stopImmediatePropagation();
closeActionsMenu();

var movies = getchecked();
var arrayLength = movies.length;
if(arrayLength>0){
//should probably get have a confirmation message.....
var movienames ="";
for (var i = 0; i < arrayLength; i++) {
    //alert(myStringArray[i]);
    //remove the movie
	movienames += "<li>"+$('#'+movies[i]).attr('title')+"</li>";
	
	//	$('#j_4').refresh();
}
//alert("The following will be deleted: "+movienames);

  $('<div>').simpledialog2({
    mode: 'blank',
    headerText: 'Add to List?',
    headerClose: true,
	width: '80%',
    blankContent : 
      "<ul data-role='listview'>"+movienames+"</ul>"+
      // NOTE: the use of rel="close" causes this button to close the dialog.
      "<table><tr><td><a rel='close' data-role='button' href='#'>Cancel</a></td>"+
	  "<td><a id=\"confirmaddtolist\" name=\""+listname+"\" rel='close' data-role='button' href='#'>OK</a><td><tr></table>"
  })


}

  });
}

$(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 () {
  //  $('#editlist').editablelistview();
});
$(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);	
    	}
}
