<?php
// $Id: yui_tabview.module

/*
 * Drupal Hizmetleri (drupalhizmetleri)
 * YUI component based module thanks to jeff for original code of yui treeview
 */

/*
 * Implementation of hook_menu()
 */
function yui_tabview_menu() {
  $items['tabview/sample'] = array(
    'page callback' => 'yui_tabview_sample',
    'access callback' => TRUE,
    'type' => MENU_CALLBACK
  );
  return $items;
}

/*
 * Implementation of hook_form_alter()
 */
function yui_tabview_sample() {

  $nodes[] = (object)array('nav' => 'Tab One Label', 'content' => 'Tab One Content');
  $nodes[] = (object)array('nav' => 'Tab Two Label', 'content' => 'Tab Two Content');
  $nodes[] = (object)array('nav' => 'Tab Three Label', 'content' => 'Tab Three Content');
  $output = build_tabview("test",$nodes);
  return $output;
}

function build_tabview($name,$nodes) {
  $yui_source = variable_get('yui_source','http://yui.yahooapis.com/2.5.1');
  yui_add_css('tabview', $yui_source, '/build/tabview/assets/skins/sam/tabview.css');
  yui_add_js('tabview', $yui_source, '/build/yahoo-dom-event/yahoo-dom-event.js');
  yui_add_js('tabview', $yui_source, '/build/element/element-beta-min.js');
  yui_add_js('tabview', $yui_source, '/build/connection/connection-min.js');
  yui_add_js('tabview', $yui_source, '/build/tabview/tabview-min.js');

  drupal_add_css(drupal_get_path("module", "yui_tabview")."/yui_tabview.css");
  $tab_number=1;
  foreach ($nodes as $node) {
    $yui_nav.= '<li '.($tab_number==1?'class="selected"':'').'><a href="#tab'.$tab_number.'"><em>'.$node->nav.'</em></a></li>';
    $yui_content.= ' <div><p>'.$node->content.'</p></div>';
    $tab_number++;
  }
  $output='
    <script type="text/javascript">
    var myTabs = new YAHOO.widget.TabView("'.$name.'");
    </script> 
    <div id="'.$name.'" class="yui-navset">
        <ul class="yui-nav">
            '.$yui_nav.'
        </ul>            
        <div class="yui-content">
            '.$yui_content.'
        </div>
    </div>
    ';
  return $output;
}


/**
 * Implementation of hook_filter_tips().
 */
function yui_tabview_filter_tips($delta, $format, $long = false) {
  global $base_url;
  if ($delta == 0) {
    switch ($long) {
      case 0:
        return t('Formatting as follows<BR>(YUI_TAB)<BR>(HEADER)Tab1(/HEADER)<BR>Tab1 Content<BR>(HEADER)Tab2(/HEADER)<BR>Tab2 Content<BR>(/YUI_TAB)');
      case 1:
        $output = '<h4>'. t('Yui Tabview tips') .'</h4>';
        return $output;
    }
  }
}

/**  
 * Implementation of hook_filter(). 
 *
 */
function yui_tabview_filter($op, $delta = 0, $format = -1, $text = '') {
  switch ($op) {
    case 'list':
      return array(0 => t('Yui Tabs'));
    case 'no cache':
      // No caching for the PHP evaluator.
      return $delta == 0;
    case 'description':  
      return t('Turns simple html into a yui tabs');
    case 'process':
        if(strpos($text,'(YUI_TAB') !== FALSE) {

            $yui_source = variable_get('yui_source','http://yui.yahooapis.com/2.5.1');
            yui_add_css('tabview', $yui_source, '/build/tabview/assets/skins/sam/tabview.css');
            yui_add_js('tabview', $yui_source, '/build/yahoo-dom-event/yahoo-dom-event.js');   
            yui_add_js('tabview', $yui_source, '/build/element/element-beta-min.js');
            yui_add_js('tabview', $yui_source, '/build/connection/connection-min.js');
            yui_add_js('tabview', $yui_source, '/build/tabview/tabview-min.js');
            drupal_add_css(drupal_get_path("module", "yui_tabview")."/yui_tabview.css");
        }
        while(strpos($text,'(YUI_TAB') !== FALSE) {
           $text =  yui_tabview_insertab($text);  
        }
      return $text;
    default:
      return $text;
  }
} 


function yui_tabview_insertab($text) {
    $start_chuck = strpos($text, '(YUI_TAB)');
    $end_chuck = strpos($text,'(/YUI_TAB)');
    $chuck = substr($text, $start_chuck, $end_chuck - $start_chuck + 10);
    $working_chuck = substr($chuck, 9, $end_chuck - $start_chuck - 9);
    $tabs = explode('(HEADER)', $working_chuck);
    $navs = '';   
    $content = '';
    $tab = 1;
    foreach($tabs as $value) {
        $pos = strpos($value, '(/HEADER)');
        $class = '';
        if($pos !== false) {
            if($tab == 1) {
                $class = ' class="selected" ';
            }
            $navs .= '<li'.$class.'><a href="#tab'.$tab.'"><em>'.substr($value, 0, $pos).'</em></a></li>';
            $content .= '<div>'.substr($value, $pos + 9) .'</div>';
            $tab++;
        }
    }
    $new_chuck = '<script type="text/javascript">
var myTabs = new YAHOO.widget.TabView("test");
</script>
<div id="test" class="yui_navset">
    <ul class="yui-nav">
'.$navs.'
    </ul>
    <div class="yui-content">
'.$content.'
    </div>
</div>';
    $text = str_replace($chuck, $new_chuck, $text);
    $text = str_replace('(YUI_TAB)', '', $text);
    return $text;
}
