/*
    *****************************************
    JAVASCRIPT ESTUDIOPICA.COM.AR
    *****************************************
    =========================================
    www.estudiopica.com.ar
    Estudio Pica - 2010
    =========================================
    
    Leyendo codigo se aprende. Adelante!
*/


jQuery(document).ready(init);

function init() {	
	// aplicar evento para hacer toggle a los submenues:
	jQuery("#access ul.sf-menu > li > a").click(toggleShowSubMenus);
	// si no es IE:
	if(!jQuery.browser.msie){
		// hacer aparecer los elementos:
		hacerAparecerLosElementos();
	} else{
		// si es IE:
		// hacer aparecer los elementos para IE (sin animacion)
		hacerAparecerLosElementosIE();
	}
	// agregar sliders:
	agregarSliders();
}

function agregarSliders(){
	var currentPosition = 0;
	var slideWidth = 510;
	var slides = jQuery('.wp-caption');
	var numberOfSlides = slides.length;

  // Remove scrollbar in JS
  jQuery('#slidesContainer').css('overflow', 'hidden');

  // Wrap all .slides with #slideInner div
  jQuery(slides)
  .wrapAll('<div id="slideInner"></div>')
  // Float left to display horizontally, readjust .slides width
  .css({
    'float' : 'left',
    'width' : slideWidth
  });

  // Set #slideInner width equal to total width of all slides
  jQuery('#slideInner').css('width', slideWidth * numberOfSlides);

  // Insert left and right arrow controls in the DOM
  jQuery('#slideshow')
    .prepend('<span class="control" id="leftControl">Move left</span>')
    .append('<span class="control" id="rightControl">Move right</span>');

  // Hide left arrow control on first load
  manageControls(currentPosition);

  // Create event listeners for .controls clicks
  jQuery('.control')
    .bind('click', function(){
    // Determine new position
      currentPosition = (jQuery(this).attr('id')=='rightControl')
    ? currentPosition+1 : currentPosition-1;

      // Hide / show controls
      manageControls(currentPosition);
      // Move slideInner using margin-left
      jQuery('#slideInner').animate({
        'marginLeft' : slideWidth*(-currentPosition)
      });
    });

  // manageControls: Hides and shows controls depending on currentPosition
  function manageControls(position){
    // Hide left arrow if position is first slide
    if(position==0){ jQuery('#leftControl').hide() }
    else{ jQuery('#leftControl').show() }
    // Hide right arrow if position is last slide
    if(position==numberOfSlides-1){ jQuery('#rightControl').hide() }
    else{ jQuery('#rightControl').show() }
    }

}


function toggleShowSubMenus(){
	var estabaVisible;
	// chequear si el submenu estaba visible:
	if(jQuery("ul", jQuery(this).parent()).css("display") == "none"){
		estabaVisible = false;
	} else {
		estabaVisible = true;
	}
	
	// ocultar todos los submenues:
	jQuery("#access ul.sf-menu > li ul").animate({height: 'hide', opacity: 'hide'}, 200);
	
	// si tiene submenu:
	if(jQuery(this).parent().children().size() > 1 ){
		if(estabaVisible == false){
			// si NO estaba visible: mostrar
			jQuery("ul", jQuery(this).parent()).animate({height: 'show', opacity: 'show'}, 200);
		} else{
			// si estaba visible: ocultar
			jQuery("ul", jQuery(this).parent()).animate({height: 'hide', opacity: 'hide'}, 200);
		}
		// no ir al link:
		return false;
	}
}


function hacerAparecerLosElementos(){
	if(getSegment(0) == ""){
		// si estamos en la home, hacer animaciones para todos los elementos:
		jQuery("#blog-title").animate({opacity: 'show'}, 800, function(){
			jQuery("#access").animate({opacity: 'show', paddingLeft: "-=20"}, 400, "easeOutQuart", function(){
				jQuery("#content").animate({opacity: 'show', paddingTop: "-=20"}, 800, "easeOutQuart");
				jQuery("#footer").animate({opacity: 'show', paddingLeft: "-=20"}, 400, "easeOutQuart");
				// hacer aparecer el submenu correspondiente:
				jQuery("li.current_page_ancestor ul").animate({opacity: 'show'}, 400, "easeOutQuart");
			});
		});
	} else{
		// si no estamos en la home, hacer aparecer instantaneamente todos los elementos
		jQuery("#blog-title").css({display: "block"});
		jQuery("#access").css({display: "block", paddingLeft: "0"});
		jQuery("#footer").css({display: "block", paddingLeft: "40px"});
		// y hacer apacer animado el contenido:
		jQuery("#content").animate({opacity: 'show', paddingTop: "-=20"}, 800, "easeOutQuart");
		// hacer aparecer el submenu correspondiente:
		jQuery("li.current_page_ancestor ul").show();
	}
	
	// resaltar la current page:
	jQuery(".sf-menu .current_page_item a").css({backgroundColor: "#ffffff"});
	jQuery(".sf-menu .current_page_item a").animate({backgroundColor: "#000000"}, 1000);
}

function hacerAparecerLosElementosIE(){
	jQuery("#blog-title").css({display: "block"});
	jQuery("#access").css({display: "block", paddingLeft: "0"});
	jQuery("#footer").css({display: "block", paddingLeft: "40px"});
	// y hacer apacer animado el contenido:
	jQuery("#content").css({display: 'block', paddingTop: "1px"});
	// hacer aparecer el submenu correspondiente:
	jQuery("li.current_page_ancestor ul").show();
}


// funcion util que nos devuelve los segmentos de la URL posteriores al dominio:
function getSegment(num) {
    num += 1;
    var segments = window.location.pathname.toString().split('/');
    if (segments[num] != undefined) {
        return segments[num];
    }
    return '';
}

