//cambios 14 Febrero > inclusión de c{odigo de limpieza de cadenas para hashId}
$(document).ready(function() {
    $('#izqui, #dere, #cen, #cuarta').mouseover(function(){
		$(this).css("cursor", "pointer");
	});
    $("div.dertext, div.iztext, div.centext, div.cuartatext, #izqui img.flecha, #cen img.flecha, #dere img.flecha, #cuarta img.flecha").hide();//.css("display", "none");
    
    function show_izqui () {
        $("div.dertext, #dere img.flecha").fadeOut("fast");
      $("div.centext, #cen img.flecha").fadeOut("fast");
      $("div.cuartatext, #cuarta img.flecha").fadeOut("fast");
      $("div.iztext, #izqui img.flecha").fadeIn("slow");
      $("#izqui div").addClass("selected");
      $("#dere div, #cen div, #cuarta div").removeClass("selected");
    }
    function show_cen () {
        $("div.iztext, #izqui img.flecha").fadeOut("fast");
      $("div.dertext, #dere img.flecha").fadeOut("fast");
      $("div.cuartatext, #cuarta img.flecha").fadeOut("fast");
      $("div.centext, #cen img.flecha").fadeIn("slow");
      $("#cen div").addClass("selected");
      $("#izqui div, #dere div, #cuarta div").removeClass("selected");
    }
    function show_dere () {
        $("div.iztext, #izqui img.flecha").fadeOut("fast");
      $("div.centext, #cen img.flecha").fadeOut("fast");
      $("div.cuartatext, #cuarta img.flecha").fadeOut("fast");
      $("div.dertext, #dere img.flecha").fadeIn("slow");
      $("#dere div").addClass("selected");
      $("#izqui div, #cen div, #cuarta div").removeClass("selected");
    }
    function show_cuarta () {
        $("div.iztext, #izqui img.flecha").fadeOut("fast");
      $("div.dertext, #dere img.flecha").fadeOut("fast");
      $("div.centext, #cen img.flecha").fadeOut("fast");
      $("div.cuartatext, #cuarta img.flecha").fadeIn("slow");
      $("#cuarta div").addClass("selected");
      $("#izqui div, #dere div, #cen div").removeClass("selected");
    }
    function filterPath(string) {
    return string
      .replace(/^\//,'')
      .replace(/(index|default).[a-zA-Z]{3,4}$/,'')
      .replace(/\/$/,'');
    }
    var locationPath = filterPath(location.pathname);
    var scrollElem = scrollableElement('html', 'body');
    function scroll_to (object) {
        $(scrollElem).animate({
        scrollTop: $(object).offset(300).top
        }, 800);
        return false;
    }
     $("#izqui").click(function left() {
        $('#izqui').mouseover(function(){
                $(this).css("cursor", "auto");
        });
        $('#dere, #cen, #cuarta').mouseover(function(){
		$(this).css("cursor", "pointer");
	});
      show_izqui();
      scroll_to('#izqui');
      return true;
    });
     $("#dere").click(function () {
        $('#dere').mouseover(function(){
                $(this).css("cursor", "auto");
        });
        $('#izqui, #cen, #cuarta').mouseover(function(){
		$(this).css("cursor", "pointer");
	});
      show_dere();
      scroll_to('#dere');
      return true;
    });
     $("#cen").click(function () {
        $('#cen').mouseover(function(){
                $(this).css("cursor", "auto");
        });
        $('#izqui, #dere, #cuarta').mouseover(function(){
		$(this).css("cursor", "pointer");
	});
      show_cen();
      scroll_to('#cen');
      return true;
    });
    $("#cuarta").click(function () {
        $('#cuarta').mouseover(function(){
                $(this).css("cursor", "auto");
        });
        $('#izqui, #dere, #cen').mouseover(function(){
		$(this).css("cursor", "pointer");
	});
      show_cuarta();
      scroll_to('#cuarta');
      return true;
    });
    
    var hashId = location.hash.replace('#','');
    if (hashId.length > 0) {                               
        if  (($('div.iztext').find('*[id="' + hashId +'"]')).length ) {
            show_izqui();
        } 
        if  (($('div.centext').find('*[id="' + hashId +'"]')).length ) {
            show_cen();
        } 
        if  (($('div.dertext').find('*[id="' + hashId +'"]')).length ) {
            show_dere();
        }
        if  (($('div.cuartatext').find('*[id="' + hashId +'"]')).length ) {
            show_cuarta();
        } 
        scroll_to("#" + hashId);
    }
    $('a[href*=#]').click(function(event){
        var ancla = $(this).attr( 'href' ).replace('#','');
        //event.preventDefault();
        if  (($('div.iztext').find('*[id="' + ancla +'"]')).length ) {
            show_izqui();
        }
        if  (($('div.centext').find('*[id="' + ancla +'"]')).length ) {
            show_cen();
        }
        if  (($('div.dertext').find('*[id="' + ancla +'"]')).length ) {
            show_dere();
        }
        if  (($('div.cuartatext').find('*[id="' + ancla +'"]')).length ) {
            show_cuarta();
        }
        scroll_to('#' + ancla);
    });
    
    
    
    $('a[href*=#]').each(function() {
      var thisPath = filterPath(this.pathname) || locationPath;
      if (  locationPath == thisPath
      && (location.hostname == this.hostname || !this.hostname)
      && this.hash.replace(/#/,'') ) {
        var $target = $(this.hash), target = this.hash;
        if (target) {
          var targetOffset = $target.offset().top;
          $(this).click(function(event) {
            //event.preventDefault();
            if  (($('div.iztext').find('*[id="' + target +'"]')).length ) {
                show_izqui();
            }
            if  (($('div.centext').find('*[id="' + target +'"]')).length ) {
                show_cen();
            }
            if  (($('div.dertext').find('*[id="' + target +'"]')).length ) {
                show_dere();
            }
            if  (($('div.cuartatext').find('*[id="' + target +'"]')).length ) {
                show_cuarta();
            }
            scroll_to(target);
          });
        }
      }
    });
    
    function scrollableElement(els) {
    for (var i = 0, argLength = arguments.length; i <argLength; i++) {
      var el = arguments[i],
          $scrollElement = $(el);
      if ($scrollElement.scrollTop()> 0) {
        return el;
      } else {
        $scrollElement.scrollTop(1);
        var isScrollable = $scrollElement.scrollTop()> 0;
        $scrollElement.scrollTop(0);
        if (isScrollable) {
          return el;
        }
      }
    }
    return [];
    }
});
