<?php

/**
 * Implement block_info()
 */
function policia_slider_css3_block_info() {
  
  $blocks['slider_home'] = array(
    'info' => t('Slider home CSS3'),
  );
  $blocks['slider_recomendados'] = array(
    'info' => t('Slider recomendados CSS3'),
  );
  $blocks['slider_observatorio'] = array(
    'info' => t('Slider Observatorio del delito CSS3'),
  );
  $blocks['slider_noticias'] = array(
    'info' => t('Slider Noticias CSS3'),
  );
  return $blocks;
}

/**
 * Implements hook_block_view().
 */
function policia_slider_css3_block_view($delta = '') {
  $block = array();
  switch ($delta) {
    case 'slider_home':
    	$array = array();
    	$id = 'slideshow-home-principal';
			$markup['#prefix'] = '<div id="' . $id . '" class="slideshow">';
			$markup['#suffix'] = '</div>';
			$prefix_id = 'shp';
			$result = db_query("SELECT bid FROM bean WHERE type='slider_home_principal'");
			foreach ($result as $key => $value) {
				$bean = bean_load($value->bid);
				$image = theme('image_style', array('style_name' => '2000', 'path' => $bean->field_slider_imagen['und'][0]['uri'], 'alt' => $bean->field_slider_imagen['und'][0]['alt'], 'title' => $bean->field_slider_imagen['und'][0]['title']));
				if(isset($bean->field_link['und'][0]['url'])){
					$array[$bean->field_peso['und'][0]['value']] = l($image, $bean->field_link['und'][0]['url'], array('html' => TRUE));
				}else{
					$array[$bean->field_peso['und'][0]['value']] = $image;
				}
			}
			ksort($array);
			$array = array_values($array);
			break;
		case 'slider_recomendados':
			$block['subject'] = 'Recomendados';
    	$array = array();
    	$id = 'slideshow-home-recomendados';
			$markup['#prefix'] = '<div id="' . $id . '" class="slideshow">';
			$markup['#suffix'] = '</div>';
			$prefix_id = 'sr';
			$result = db_query("SELECT bid FROM bean WHERE type='slider_inferior_home'");
			foreach ($result as $key => $value) {
				$bean = bean_load($value->bid);
				$image = theme('image_style', array('style_name' => '1400x450', 'path' => $bean->field_slider_imagen['und'][0]['uri'], 'alt' => $bean->field_slider_imagen['und'][0]['alt'], 'title' => $bean->field_slider_imagen['und'][0]['title']));
				if(isset($bean->field_link['und'][0]['url'])){
					$array[$bean->field_peso['und'][0]['value']] = l($image, $bean->field_link['und'][0]['url'], array('html' => TRUE));
				}else{
					$array[$bean->field_peso['und'][0]['value']] = $image;
				}
			}
			ksort($array);
			$array = array_values($array);
    	break;
  	case 'slider_observatorio':
  		$array = array();
    	$id = 'slideshow-observatorio-delito';
			$markup['#prefix'] = '<div id="' . $id . '" class="slideshow">';
			$markup['#suffix'] = '</div>';
			$prefix_id = 'odd';
  		if(arg(0) == 'node' && is_numeric(arg(1))){
  			$node = node_load(arg(1));
  			$slider = field_get_items('node', $node, 'field_slider');
  			if($slider){
  				foreach ($slider as $key => $value) {
  					$image = theme('image_style', array('style_name' => '2000', 'path' => $value['uri'], 'alt' => $value['alt'], 'title' => $value['title']));
						$array[] = $image;
  				}
  			}
  		}
  		break;
  	case 'slider_noticias':
  		$array = array();
    	$id = 'slideshow-noticias';
			$markup['#prefix'] = '<div id="' . $id . '" class="slideshow">';
			$markup['#suffix'] = '</div>';
			$prefix_id = 'nt';
  		if(arg(0) == 'node' && is_numeric(arg(1))){
  			$node = node_load(arg(1));
  			$slider = field_get_items('node', $node, 'field_imagen_noticia');
  			if($slider){
  				foreach ($slider as $key => $value) {
  					$image = theme('image_style', array('style_name' => '660x400', 'path' => $value['uri'], 'alt' => $value['alt'], 'title' => $value['title']));
						$array[] = $image;
  				}
  			}
  		}
  		break;
	}
	$prefix = null;
	$label = null;
	$content = '<div class="slideshow-inner"><ul>';
	$style = '#'.$id.' label:not(.arrows):active { 
		bottom: -46px; 
	}
	';
	$margin_left = -36;
	$left = 0;
	$count = count($array) - 1;
	foreach ($array as $key => $value) {
		if($key == 0){
			$prefix .= '<input type="radio" id="'.$prefix_id.'button-'.$key.'" name="controls" checked="checked"/><label for="'.$prefix_id.'button-'.$key.'"></label>';
		}else{
			$prefix .= '<input type="radio" id="'.$prefix_id.'button-'.$key.'" name="controls"/><label for="'.$prefix_id.'button-'.$key.'"></label>';
		}
		//Botones de anterior y siguiente
		if($key == 0){
			$label .= '<label for="'.$prefix_id.'button-'.$key.'" class="arrows goto-last" id="first-'.$prefix_id.'arrow-'.$key.'">></label>';
		}
		$label .= '<label for="'.$prefix_id.'button-'.$key.'" class="arrows" id="'.$prefix_id.'arrow-'.$key.'">></label>';
		if ($key == $count) {
			$label .= '<label for="'.$prefix_id.'button-'.$key.'" class="arrows goto-first" id="last-'.$prefix_id.'arrow-'.$key.'">></label>';
		}
		$content .= '<li id="'.$prefix_id.'slide'.$key.'">'.$value.'</li>';
		$style .= '#'.$id.' input[type=radio]#'.$prefix_id.'button-'.$key.':checked~label[for='.$prefix_id.'button-'.$key.'] { 
				background-color: 
				rgba(100,100,100,1) 
			}
		';
		$style .= '#'.$id.' label[for='.$prefix_id.'button-'.$key.'] { 
				margin-left: '.$margin_left.'px 
			}
		';
		$style .= '#'.$id.' input[type=radio]#'.$prefix_id.'button-'.$key.':checked~.slideshow-inner>ul { 
				left: '.$left.'% 
			}
		';
		$margin_left += 30;
		$left += -100;
		$var2 = $key-1;
		if($key != $count){
			$var = $key+1;
			$style .= 'input[type=radio]#'.$prefix_id.'button-'.$key.':checked~.arrows#'.$prefix_id.'arrow-'.$var.' {
					right: 7%; display: block;
				}';
			$style .= 'input[type=radio]#'.$prefix_id.'button-'.$key.':checked~.arrows#'.$prefix_id.'arrow-'.$var2.' { 
				left: -19px 
			}';
		}
		if($key != 0){
			$style .= 'input[type=radio]#'.$prefix_id.'button-'.$key.':checked~.arrows#'.$prefix_id.'arrow-'.$var2.' {
		    left: 2%;
		    display: block;
		    -webkit-transform: scaleX(-1);
		    -moz-transform: scaleX(-1);
		    -ms-transform: scaleX(-1);
		    -o-transform: scaleX(-1);
		    transform: scaleX(-1);
			}
		';
		}
	}
	$left = $left + 100;
	$style .= '#'.$id.' input[type=radio]#first-'.$prefix_id.'button-'.$key.':checked~.slideshow-inner>ul { 
				left: '.$left.'% 
			}
		';
	$style .= '#'.$id.' input[type=radio]#last-'.$prefix_id.'button-'.$key.':checked~.slideshow-inner>ul { 
				left: 0% 
			}
		';
	$content .= '</ul></div>';

	$markup['#markup'] = '<form><fieldset>'. $prefix. $label .$content.'<input type="submit" value="slider" class="element-invisible"/></fieldset></form>';

	drupal_add_css(
		$style,
    array(
      'group' => CSS_THEME,
      'type' => 'inline',
      'media' => 'screen',
      'preprocess' => FALSE,
      'weight' => '9999',
    )
  );
	$block['content'] = render($markup);
	return $block;
}
