/**
 * Implements hook_block_info(). 
 */
function custom_donate_block_info() {
  var blocks = {};
  blocks['custom_donate_block'] = {
    delta: 'custom_donate_block',
    module: 'custom_donate'
  };
  return blocks;
}

function delay(callback, ms) {
  var timer = 0;
  return function() {
    var context = this, args = arguments;
    clearTimeout(timer);
    timer = setTimeout(function () {
      callback.apply(context, args);
    }, ms || 0);
  };
}

function myFunction() { 
    $('#myInput').keyup(delay(function (e) {  
         
        var input = document.getElementById("myInput").value; 
        if(input){
            drupalgap_goto("project-list/"+input);
        }
    }, 500));
}
/**
 * Implements hook_block_view().
 */
function custom_donate_block_view(delta, region) {
    var content = {};
    if (delta == 'custom_donate_block') { 
        var uid = Drupal.user.uid;
        var canteen_param = "'product-categorywise/74'";
        var freeparking_param = "'product-categorywise/55'";
        var freewifi_param = "'product-categorywise/27'";
        var freeyoga_param = "'product-categorywise/75'";
        content = '';
        //content = '<img src="app/modules/custom/custom_donate/images/iase.png" style="width:100% !important;height:180px;">';
       // content += '<div>Our rooms offer basic amenities with a sleeping area, a separate bathroom, an equipped kitchenette</div>';
        content = "<form><input type='search' name='mkeyword' id='myInput' onkeyup='myFunction()' style='width:100%;' placeholder='Search your project here' style='background:#f4f4f4;'></form>";
        /*content += '<div class="container" style="display: flex; flex-wrap: nowrap; height: 120px; overflow-x: auto; width: auto;">';
        content += '<table style="width:100%">';
        content += '<tbody>';
        content += '<tr align="center">';
        content += '<td width="20%"><a href="#" style="text-decoration:none;color:#444444;font-weight:normal;" onclick="javascript:drupalgap_goto('+ canteen_param +', {});"><img src="app/modules/custom/custom_donate/images/restaurant.png" style="border-radius:8%;width:70px;height:70px;"><br><span style="font-size:12px;">Canteen</span></a></td>';
        content += '<td width="20%"><a href="#" style="text-decoration:none;color:#444444;font-weight:normal;" onclick="javascript:drupalgap_goto('+ freeparking_param +', {});"><img src="app/modules/custom/custom_donate/images/freeparking.png" style="border-radius:8%;width:70px;height:70px;"><br><span style="font-size:12px;">Free Parking</span></a></td>';
        content += '<td width="20%"><a href="#" style="text-decoration:none;color:#444444;font-weight:normal;" onclick="javascript:drupalgap_goto('+ freewifi_param +', {});"><img src="app/modules/custom/custom_donate/images/freewifi.png" style="border-radius:8%;width:70px;height:70px;"><br><span style="font-size:12px;">Free Wifi</span></a></td>';
        content += '<td width="20%"><a href="#" style="text-decoration:none;color:#444444;font-weight:normal;" onclick="javascript:drupalgap_goto('+ freeyoga_param +', {});"><img src="app/modules/custom/custom_donate/images/spa.png" style="border-radius:8%;width:70px;height:70px;"><br><span style="font-size:12px;">Free Yoga</span></a></td>';
        content += '</tr>';
        content += '</tbody>';
        content += '</table>';
        content += '</div><br>'; */ 
        content += '<br>';
    }
    
    return content;
}
//////////////

/**
 * Implements hook_menu().
 */
function custom_donate_menu() {
  try {
    var items = {};
    items['content-list'] = {
      title: 'Manage Content',
      page_callback: 'custom_donate_content_list_page',
      options:{
          reloadPage:true
       }
    }; 
    items['news'] = {
      title: 'News & Events',
      page_callback: 'custom_news_list_page',
      options:{
          reloadPage:true
       }
    }; 
    items['gallery'] = {
      title: 'Gallery ',
      page_callback: 'custom_gallery_list_page',
      options:{
          reloadPage:true
       }
    }; 
    items['gallery-all-photos/%'] = {
      title: 'Gallery Photos ',
      page_callback: 'custom_gallery_allphotos_list_page',
      page_arguments: [1],
      options:{
          reloadPage:true
       }
    }; 
    items['all-photos'] = {
      title: 'All Photos ',
      page_callback: 'custom_allphotos_list_page',
      options:{
          reloadPage:true
       }
    }; 
    return items;
  }
  catch (error) {
    console.log('custom_donate_menu - ' + error);
  }
}
 
/////////////////////////////////////////////////
/**
 * Page callback function for custom_page title_callback.
 */
function custom_donate_content_list_page() {
  try {
    var content = {};
    var gallery_param = "'gallery'"; 
    var addpage_param = "'https://donategvm.iaseuniversityonline.org/donate/node/add/page', '_blank', 'location=yes'";
     
    content['row_one'] = {
        markup: '<div class="ui-grid-a"><div class="ui-block-a"><div class="ui-bar" style="padding:2px !important;"><a style="text-decoration:none;text-shadow:none;font-weight:normal;background-color:#fe9d00;color:#ffffff;" href="#" onclick="javascript:window.open('+ addpage_param +');">Add Page</a></div></div><div class="ui-block-b"><div class="ui-bar" style="padding:2px !important;"><a style="text-decoration:none;text-shadow:none;background-color:#fe9d00;color:#ffffff;font-weight:normal;" href="#" onclick="javascript:drupalgap_goto('+ gallery_param +');">Gallery</a></div></div></div>'
    };
    
    content['content'] = {
      theme: 'view',
      format: 'ul', 
      path: 'content_list_mobile_view.json',
      row_callback: 'custom_donate_content_list_page_row',
      empty_callback: 'custom_donate_content_list_page_empty',
    };
    return content;
  }
  catch (error) { console.log('custom_donate_content_list_page - ' + error); }
}

function custom_donate_content_list_page_row(view, row) {
  try {
      
    var uid = Drupal.user.uid;
    if(uid > 0){
        var nodeview_param = "'https://donategvm.iaseuniversityonline.org/donate/node/" + row.nid +"', '_blank', 'location=yes'";
        var data = '<div style="background-color:#f2f2f2;padding:5px 8px;"><p><b>'+ row.sno + '- ' + row.Type + '</b><table width="100%" border="0" cellpadding="3"><tr style="font-size:12px;"><td width="8%">Title</td><td width="90%">'+ row.Title + '</td></tr><tr style="font-size:12px;"><td width="8%">Updated On</td><td width="90%">'+ row.Updated + '</td></tr><tr style="font-size:12px;"><td width="8%">Status</td><td width="90%">'+ row.Status + '</td></tr><tr style="font-size:12px;"><td width="8%">Detail</td><td width="90%"><a href="#" onclick="javascript:window.open('+nodeview_param+');">View</a></td></tr> </table>';
        var html = data;
    }else{
        var nodeview_param = "'https://donategvm.iaseuniversityonline.org/donate/node/" + row.nid +"', '_blank', 'location=yes'";
        var data = '<div style="background-color:#f2f2f2;padding:5px 8px;"><p><b>'+ row.sno + '- ' + row.Type + '</b><table width="100%" border="0" cellpadding="3"><tr style="font-size:12px;"><td width="8%">Title</td><td width="90%">'+ row.Title + '</td></tr><tr style="font-size:12px;"><td width="8%">Updated On</td><td width="90%">'+ row.Updated + '</td></tr><tr style="font-size:12px;"><td width="8%">Status</td><td width="90%">'+ row.Status + '</td></tr><tr style="font-size:12px;"><td width="8%">Detail</td><td width="90%"><a href="#" onclick="javascript:window.open('+nodeview_param+');">View</a></td></tr> </table>';
        var html = data;
    }
    return html;
  }
  catch (error) { console.log('custom_donate_content_list_page_row - ' + error); }
}

function custom_donate_content_list_page_empty(view) {
  try {
    return "Sorry, no data found.";
  }
  catch (error) { console.log('custom_donate_content_list_page_empty - ' + error); }
}
////////////////////////////////////////////////////////////////////////////////////////
 
///////////////////////////////////////////////////////////////////////////////////////////// 

/**
 * Page callback function for custom_page for material title_callback.
 */
function custom_news_list_page() {
  try {
    var content = {};
    
    var home_param = "'home'"; 
    //var addnews_param = "'https://donategvm.iaseuniversityonline.org/donate/node/add/news-events', '_blank', 'location=yes'";
    var addnews_param = "'node/add/news-events'";
    var uid = Drupal.user.uid;
    var rolesarray = Drupal.user.roles;
    if(uid > 0){
        if (rolesarray.length != 0) {
            $.each(rolesarray, function(role_id, role) { 
                if(role=="sitemanager" || role=="administrator" || role=="content creator"){
                    content['row_one'] = {
                        markup: '<div class="ui-grid-a"><div class="ui-block-a"><div class="ui-bar" style="padding:2px !important;"><a style="text-decoration:none;text-shadow:none;font-weight:normal;background-color:#fe9d00;color:#ffffff;" href="#" onclick="javascript:drupalgap_goto('+ addnews_param +',{});">Add News & Events</a></div></div><div class="ui-block-b"><div class="ui-bar" style="padding:2px !important;"><a style="text-decoration:none;text-shadow:none;background-color:#fe9d00;color:#ffffff;font-weight:normal;" href="#" onclick="javascript:drupalgap_goto('+ home_param +');">Home</a></div></div></div>'
                    };
                }
            });
        }
    }    
    content['news'] = {
      theme: 'view',
      format: 'ul', 
      path: 'news_list_mobile_view.json',
      row_callback: 'custom_news_list_page_row',
      empty_callback: 'custom_news_list_page_empty',
    };
    return content;
  }
  catch (error) { console.log('custom_news_list_page - ' + error); }
}

function custom_news_list_page_row(view, row) {
  try {
      
    var uid = Drupal.user.uid;
    var viewnode_param = "'node/" + row.nid + "'";
    if(uid > 0){
        var data = '<div style="background-color:#ffffff;padding:5px 8px;"><p><b>'+ row.sno + '- ' + row.title + '</b><table width="100%" border="0" cellpadding="3"><tr style="font-size:12px;"><td width="8%">Detail</td><td width="90%"><a style="text-decoration:none;color:#222222;font-weight:normal;" href="#" onclick="javascript:drupalgap_goto('+ viewnode_param +', {});">View</a></td></tr>    </table>';
        var html = data;
    }else{
        var data = '<div style="background-color:#ffffff;padding:5px 8px;"><p><b>'+ row.sno + '- ' + row.title + '</b><table width="100%" border="0" cellpadding="3"><tr style="font-size:12px;"><td width="8%">Detail</td><td width="90%"><a style="text-decoration:none;color:#222222;font-weight:normal;" href="#" onclick="javascript:drupalgap_goto('+ viewnode_param +', {});">View</a></td></tr>    </table>';
        var html = data;
    }
    
    return html;
  }
  catch (error) { console.log('custom_news_list_page_row - ' + error); }
}

function custom_news_list_page_empty(view) {
  try {
    return "Sorry, no data found.";
  }
  catch (error) { console.log('custom_news_list_page_empty - ' + error); }
}
///////////////////////////////////////////////////////////////////////////////////////////// 

/**
 * Page callback function for custom_page for gallery title_callback.
 */
function custom_gallery_list_page() {
  try {
    var content = {};
    
    var home_param = "'all-photos'"; 
    var addgallery_param = "'https://donategvm.iaseuniversityonline.org/donate/node/add/gallery', '_blank', 'location=yes'";
    var uid = Drupal.user.uid;
    var rolesarray = Drupal.user.roles;
    
    if(uid > 0){
        if (rolesarray.length != 0) {
            $.each(rolesarray, function(role_id, role) { 
                if(role=="sitemanager" || role=="administrator" || role=="content creator"){
                    content['row_one'] = {
                        markup: '<div class="ui-grid-a"><div class="ui-block-a"><div class="ui-bar" style="padding:2px !important;"><a style="text-decoration:none;text-shadow:none;font-weight:normal;background-color:#fe9d00;color:#ffffff;" href="#" onclick="javascript:window.open('+ addgallery_param +');">Add Gallery</a></div></div><div class="ui-block-b"><div class="ui-bar" style="padding:2px !important;"><a style="text-decoration:none;text-shadow:none;background-color:#fe9d00;color:#ffffff;font-weight:normal;" href="#" onclick="javascript:drupalgap_goto('+ home_param +');">All Photos</a></div></div></div>'
                    };
                }
            });
        }
    }    
    content['gallery'] = {
      theme: 'view',
      format: 'grid', 
      columns: '2', 
      path: 'gallery_list_mobile_view.json',
      row_callback: 'custom_gallery_list_page_row',
      empty_callback: 'custom_gallery_list_page_empty',
    };
    return content;
  }
  catch (error) { console.log('custom_gallery_list_page - ' + error); }
}

function custom_gallery_list_page_row(view, row) {
  try {
      
    var uid = Drupal.user.uid;
    
    if(row.image.src==""){
        var imagesrc = "app/modules/custom/custom_home/images/noimageav.png";
    }else{ 
        var imagesrc = row.image.src;
    }
    
    var galparam = "'gallery-all-photos/"+row.nid+"'";
    var gallery_photos_param = 'onclick="javascript:drupalgap_goto('+galparam+',{});"'; 
    if(uid > 0){
        var data = "<span style='border: 1px solid #ffffff; min-height:100px;text-align:center;font-size:10px;background:#ffffff;'><a href='#' "+gallery_photos_param+" style='text-decoration:none;text-shadow:none;color:#cccccc;'><img src='"+imagesrc+"' style='height:100px;width:100px;'><br>"+row.title+"</a></span>";
        var html = data;
    }else{
       var data = "<span style='border: 1px solid #ffffff; min-height:100px;text-align:center;font-size:10px;background:#ffffff;'><a href='#' "+gallery_photos_param+" style='text-decoration:none;text-shadow:none;color:#cccccc;'><img src='"+imagesrc+"' style='height:100px;width:100px;'><br>"+row.title+"</a></span>";
       var html = data;
    }
    
    return html;
  }
  catch (error) { console.log('custom_gallery_list_page_row - ' + error); }
}

function custom_gallery_list_page_empty(view) {
  try {
    return "Sorry, no data found.";
  }
  catch (error) { console.log('custom_gallery_list_page_empty - ' + error); }
}
///////////////////////////////////////////////////////////////////////////////////////////// 
/**
 * Page callback function for custom_page for gallery title_callback.
 */
function custom_allphotos_list_page() {
  try {
    var content = {};
    
    var home_param = "'gallery'"; 
    var addgallery_param = "'https://donategvm.iaseuniversityonline.org/donate/node/add/gallery', '_blank', 'location=yes'";
    var uid = Drupal.user.uid;
    var rolesarray = Drupal.user.roles;
    
    if(uid > 0){
        if (rolesarray.length != 0) {
            $.each(rolesarray, function(role_id, role) { 
                if(role=="sitemanager" || role=="administrator" || role=="content creator"){
                    content['row_one'] = {
                        markup: '<div class="ui-grid-a"><div class="ui-block-a"><div class="ui-bar" style="padding:2px !important;"><a style="text-decoration:none;text-shadow:none;font-weight:normal;background-color:#fe9d00;color:#ffffff;" href="#" onclick="javascript:window.open('+ addgallery_param +');">Add Gallery</a></div></div><div class="ui-block-b"><div class="ui-bar" style="padding:2px !important;"><a style="text-decoration:none;text-shadow:none;background-color:#fe9d00;color:#ffffff;font-weight:normal;" href="#" onclick="javascript:drupalgap_goto('+ home_param +');">Gallery</a></div></div></div>'
                    };
                }
            });
        }
    }    
    content['gallery'] = {
      theme: 'view',
      format: 'grid', 
      columns: '2', 
      path: 'allphotos_list_mobile_view.json',
      row_callback: 'custom_allphotos_list_page_row',
      empty_callback: 'custom_allphotos_list_page_empty',
    };
    return content;
  }
  catch (error) { console.log('custom_allphotos_list_page - ' + error); }
}

function custom_allphotos_list_page_row(view, row) {
  try {
      
    var uid = Drupal.user.uid;
    
    if(row.image.src==""){
        var imagesrc = "app/modules/custom/custom_home/images/noimageav.png";
    }else{ 
        var imagesrc = row.image.src;
    }
     
    if(uid > 0){
        var data = "<span style='border: 1px solid #ffffff; min-height:100px;text-align:center;font-size:10px;background:#ffffff;'><img src='"+imagesrc+"' style='height:100px;width:100px;'><br>"+row.title+"</span>";
        var html = data;
    }else{
        var data = "<span style='border: 1px solid #ffffff; min-height:100px;text-align:center;font-size:10px;background:#ffffff;'><img src='"+imagesrc+"' style='height:100px;width:100px;'><br>"+row.title+"</span>";
        var html = data;
    }
    
    return html;
  }
  catch (error) { console.log('custom_allphotos_list_page_row - ' + error); }
}

function custom_allphotos_list_page_empty(view) {
  try {
    return "Sorry, no data found.";
  }
  catch (error) { console.log('custom_allphotos_list_page_empty - ' + error); }
}
///////////////////////////////////////////////////////////////////////////////////////////// 

//
/**
 * Page callback function for custom_page for gallery title_callback.
 */
function custom_gallery_allphotos_list_page(e) {
  try {
    var content = {};
   
    content['gallery'] = {
      theme: 'view',
      format: 'grid', 
      columns: '2', 
      path: 'gallery_allphotos_list_mobile_view.json/'+e,
      row_callback: 'custom_gallery_allphotos_list_page_row',
      empty_callback: 'custom_gallery_allphotos_list_page_empty',
    };
    return content;
  }
  catch (error) { console.log('custom_gallery_allphotos_list_page - ' + error); }
}

function custom_gallery_allphotos_list_page_row(view, row) {
  try {
      
    var uid = Drupal.user.uid;
    
    if(row.image.src==""){
        var imagesrc = "app/modules/custom/custom_home/images/noimageav.png";
    }else{ 
        var imagesrc = row.image.src;
    }
     
    if(uid > 0){
        var data = "<span style='border: 1px solid #ffffff; min-height:100px;text-align:center;font-size:10px;background:#ffffff;'><img src='"+imagesrc+"' style='height:100px;width:100px;'><br>"+row.title+"</span>";
        var html = data;
    }else{
        var data = "<span style='border: 1px solid #ffffff; min-height:100px;text-align:center;font-size:10px;background:#ffffff;'><img src='"+imagesrc+"' style='height:100px;width:100px;'><br>"+row.title+"</span>";
        var html = data;
    }
    
    return html;
  }
  catch (error) { console.log('custom_gallery_allphotos_list_page_row - ' + error); }
}

function custom_gallery_allphotos_list_page_empty(view) {
  try {
    return "Sorry, no data found.";
  }
  catch (error) { console.log('custom_gallery_allphotos_list_page_empty - ' + error); }
}
///////////////////////////////////////////////////////////////////////////////////////////// 
