<?php
function lhnhelpout_help($path, $arg) {
  $content = '';
  switch ($path) {
    case 'admin/help#livehelpnow-helpout':
      $content .= "<img src='http://www.livehelpnow.net/images/logo_livehelpnow.gif' border='0' style='float:none' /><br /><a href='http://www.livehelpnow.net/lnd/trial.aspx' target='_new' style='font-size:16px;'><b>Get LiveHelpNow account</b></a><br /><a href='http://www.livehelpnow.net/products/live_chat_system/buttons/' target='_blank' style='font-size:14px;'>See available Live chat buttons</a>";
	  return t($content);
      break;
  }
}

function lhnhelpout_menu() {
  $items = array();
  $items['admin/config/user-interface/livehelpnow-helpout'] = array(
    'title' => 'LiveHelpNow Help Desk -- HelpOut Tab',
    'description' => 'LiveHelpNow Help Desk -- HelpOut Tab',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('lhnhelpout_form'),
    'access arguments' => array('access administration pages'),
    'type' => MENU_NORMAL_ITEM,
  );
  return $items;
}
function lhnhelpout_form($form, &$form_state) {
  $form['lhnhelpout_clientid'] = array(
    '#type' => 'textfield',
    '#title' => t('LiveHelpNow Account #'),
    '#default_value' => variable_get('lhnhelpout_clientid', ''),
    '#size' => 30,
    '#maxlength' => 255,
    '#description' => t('*Must be numeric<br /><a href="http://www.livehelpnow.net/lnd/trial.aspx" target="_new">Signup for Account</a>'),
    '#required' => TRUE,
  );
  $form['lhnhelpout_chatwindow'] = array(
    '#type' => 'textfield',
    '#title' => t('Chat Window ID'),
    '#default_value' => variable_get('lhnhelpout_chatwindow', '0'),
    '#size' => 30,
    '#maxlength' => 255,
    '#description' => t('*Must be numeric'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_invitewindow'] = array(
    '#type' => 'textfield',
    '#title' => t('Invitation Window ID'),
    '#default_value' => variable_get('lhnhelpout_invitewindow', '0'),
    '#size' => 30,
    '#maxlength' => 255,
    '#description' => t('*Must be numeric'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_department'] = array(
    '#type' => 'textfield',
    '#title' => t('Department ID'),
    '#default_value' => variable_get('lhnhelpout_department', '0'),
    '#size' => 30,
    '#maxlength' => 255,
    '#description' => t('*Must be numeric'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_autochat'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable automatic chat invitation'),
    '#default_value' => variable_get('lhnhelpout_autochat', '0'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_theme'] = array(
    '#type' => 'select',
    '#title' => t('HelpOut Tab color theme'),
    '#default_value' => variable_get('lhnhelpout_theme', 'default'),
    '#options' => array(
    	'default'=>t('default'),
    	'red'=>t('red'),
    	'orange'=>t('orange'),
    	'yellow'=>t('yellow'),
    	'green'=>t('green'),
    	'blue'=>t('blue'),
    	'purple'=>t('purple'),
    ),
    '#required' => FALSE,
  );
  $form['lhnhelpout_slideout'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable slideout panel'),
    '#default_value' => variable_get('lhnhelpout_slideout', '0'),
    '#description' => t('Uncheck if you will only be using the Live Chat system'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_chat'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable chat button'),
    '#default_value' => variable_get('lhnhelpout_chat', '0'),
    '#description' => t('Uncheck to hide the "chat with us" option'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_ticket'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable ticket button'),
    '#default_value' => variable_get('lhnhelpout_ticket', '0'),
    '#description' => t('Uncheck to hide the "submit a ticket" option'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_callback'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable callback buttonl'),
    '#default_value' => variable_get('lhnhelpout_callback', '0'),
    '#description' => t('Uncheck to hide the "request callback" option'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_knowbase'] = array(
    '#type' => 'checkbox',
    '#title' => t('Knowledge base search'),
    '#default_value' => variable_get('lhnhelpout_knowbase', '0'),
    '#description' => t('Uncheck to hide the Knowledge Base search bar, and skip to "More options"'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_moreoptions'] = array(
    '#type' => 'checkbox',
    '#title' => t('Enable More options panel'),
    '#default_value' => variable_get('lhnhelpout_moreoptions', '0'),
    '#description' => t('Uncheck to display the "More options" slider automatically'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_searchtitle'] = array(
    '#type' => 'textfield',
    '#title' => t('"Search" title'),
    '#default_value' => variable_get('lhnhelpout_searchtitle', 'Find answers'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_searchmessage'] = array(
    '#type' => 'textfield',
    '#title' => t('"Search" message'),
    '#default_value' => variable_get('lhnhelpout_searchmessage', 'Please search our knowledge base for answers or click [More options]'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_noresults'] = array(
    '#type' => 'textfield',
    '#title' => t('No Results Found message'),
    '#default_value' => variable_get('lhnhelpout_noresults', 'No results found for'),
    '#required' => FALSE,
  );
  $form['lhnhelpout_views'] = array(
    '#type' => 'textfield',
    '#title' => t('Article Views label'),
    '#default_value' => variable_get('lhnhelpout_views', 'view'),
    '#required' => FALSE,
  );
  
  return system_settings_form($form);
}

function lhnhelpout_block_info() {
  $blocks['livehelpnow-helpout']['info'] = t('LiveHelpNow Help Desk -- HelpOut Tab');
  return $blocks;
}

function lhnhelpout_block_view($delta = '') { 
  switch ($delta) {
    case 'livehelpnow-helpout':
		global $user;
		$lhnUserName = "";
		$lhnUserEmail = "";
		if($user->uid > 0){
			$lhnUserName = urlencode($user->name);
		$lhnUserEmail = urlencode($user->mail);
		}
		//$block['subject'] = t('LiveHelpNow Help Desk');
		
		$clientid = variable_get('lhnhelpout_clientid', '');
		$clientid = str_replace("lhn","",strtolower($clientid));
		$chatwindow = variable_get('lhnhelpout_chatwindow', '0');
		$invitewindow = variable_get('lhnhelpout_invitewindow', '0');
		$department = variable_get('lhnhelpout_department', '0');
		$autochat = variable_get('lhnhelpout_autochat', '0');
		$theme = variable_get('lhnhelpout_theme', 'default');
		$slideout = variable_get('lhnhelpout_slideout', '0');
		$chat = variable_get('lhnhelpout_chat', '0');
		$ticket = variable_get('lhnhelpout_ticket', '0');
		$callback = variable_get('lhnhelpout_callback', '0');
		$knowbase = variable_get('lhnhelpout_knowbase', '0');
		$moreoptions = variable_get('lhnhelpout_moreoptions', '0');
		$searchtitle = variable_get('lhnhelpout_searchtitle', 'Find answers');
		$searchmessage = variable_get('lhnhelpout_searchmessage', 'Please search our knowledge base for answers or click [More options]');
		$noresults = variable_get('lhnhelpout_noresults', 'No results found for');
		$views = variable_get('lhnhelpout_views', 'view');
		
		$content = PHP_EOL.'<script type="text/javascript">'.PHP_EOL;
		$content .= 'var lhnCustom1 = "'.$lhnUserEmail.'";'.PHP_EOL;
		$content .= 'var lhnCustom2 = "'.$lhnUserName.'";'.PHP_EOL;
		$content .= 'var lhnCustom3 = "";'.PHP_EOL;
		$content .= 'var lhnAccountN = "'.$clientid.'";'.PHP_EOL;
		$content .= 'var lhnInviteEnabled = '.$autochat.';'.PHP_EOL;
		$content .= 'var lhnWindowN = '.$chatwindow.';'.PHP_EOL;
		$content .= 'var lhnInviteN = '.$invitewindow.';'.PHP_EOL;
		$content .= 'var lhnDepartmentN = '.$department.';'.PHP_EOL;
		$content .= 'var lhnTheme = "'. $theme .'";'.PHP_EOL;
		$content .= 'var lhnHPPanel = '.$slideout.'; '.PHP_EOL;
		$content .= 'var lhnHPKnowledgeBase = '.$knowbase.'; '.PHP_EOL;
		$content .= 'var lhnHPMoreOptions = '.$moreoptions.'; '.PHP_EOL;
		$content .= 'var lhnHPChatButton = '.$chat.'; '.PHP_EOL;
		$content .= 'var lhnHPTicketButton = '.$ticket.'; '.PHP_EOL;
		$content .= 'var lhnHPCallbackButton = '.$callback.'; '.PHP_EOL;
		$content .= 'var lhnLO_helpPanel_knowledgeBase_find_answers = "'.$searchtitle.'";'.PHP_EOL;
		$content .= 'var lhnLO_helpPanel_knowledgeBase_please_search = "'.$searchmessage.'";'.PHP_EOL;
		$content .= 'var lhnLO_helpPanel_typeahead_noResults_message = "'.$noresults.'";'.PHP_EOL;
		$content .= 'var lhnLO_helpPanel_typeahead_result_views = "'.$views.'";'.PHP_EOL;
		$content .= '</script>'.PHP_EOL;
		$content .= '<script src="//www.livehelpnow.net/lhn/widgets/helpouttab/lhnhelpouttab-current.min.js" type="text/javascript" id="lhnscript"></script>'.PHP_EOL;
	break;
  }
  $block['content'] = t($content);
  return $block;
}