// JavaScript Document
function slideSwitch() {
    var $active = $('#slider img.active');

    if ( $active.length == 0 ) $active = $('#slider img:last');

    // Orden de aparicion
    var $next =  $active.next().length ? $active.next()
        : $('#slider img:first');

    // Orden aleatorio
    
     //var $sibs  = $active.siblings();
     //var rndNum = Math.floor(Math.random() * $sibs.length );
     //var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active')
        .fadeOut(1000, function(){
            $(this).css({'display':'block','opacity':0.0});
        });
        
    $next.delay(1200)
        .css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
    
}

function slideIntSwitch() {
    var $active = $('#sliderInt img.active');

    if ( $active.length == 0 ) $active = $('#sliderInt img:last');

    // Orden de aparicion
    var $next =  $active.next().length ? $active.next()
        : $('#sliderInt img:first');

    // Orden aleatorio
    
     //var $sibs  = $active.siblings();
     //var rndNum = Math.floor(Math.random() * $sibs.length );
     //var $next  = $( $sibs[ rndNum ] );


    $active.addClass('last-active')
        .fadeOut(1000, function(){
            $(this).css({'display':'block','opacity':0.0});
        });
        
    $next.delay(1000)
        .css({opacity: 0.0})
        .addClass('active')
        .animate({opacity: 1.0}, 1000, function() {
            $active.removeClass('active last-active');
        });
    
}
   
$(document).ready(function(){
    
    /// NavInfo //////////////////////////////////////////////////////////////////////////////////////////////////
    function informacion_navegador() {
        this.nombre = navigator.appName;
        this.codigo_nombre = navigator.appCodeName;
        this.version = navigator.appVersion.substring(0,1);
        this.plataforma = navigator.platform;
        this.javaEnabled = navigator.javaEnabled();
        this.pantalla_ancho = screen.width;
        this.pantalla_alto = screen.height;
    }
    var datos= new informacion_navegador()
    if(datos.nombre == 'Microsoft Internet Explorer') {
        $('#navInfo').html('<a href="javascript:;" id="cerrarNavInfo">Cerrar</a><p>El navegador que utiliza est&aacute; obsoleto y por ello no est&aacute; soportado en este sitio web.<br/>Por favor, actual&iacute;celo a alguno de los siguientes:</p><ul><li><a href="http://www.mozilla.com/es-ES/firefox/" target="_blank"><img src="layout/firefox.jpg" alt="Mozilla Firefox" /></a></li><li><a href="http://www.microsoft.com/windows/internet-explorer/default.aspx" target="_blank"><img src="layout/iexplorer.jpg" alt="Internet Explorer" /></a></li><li><a href="http://www.google.com/chrome" target="_blank"><img src="layout/chrome.jpg" alt="Google Chrome"/></a></li><li><a href="http://www.opera.com/browser/" target="_blank"><img src="layout/opera.jpg" alt="Opera" /></a></li><li><a href="http://www.apple.com/safari/" target="_blank"><img src="layout/safari.jpg" alt="Safari" /></a></li></ul>');
        $('#navInfo').css('display','block');
    };
    $('#cerrarNavInfo').click(function(){
        $('#navInfo').css('display','none');
    });
    
    
    /// Preloader ////////////////////////////////////////////////////////////////////////////////////////////////
    $.fn.preload = function() {
        this.each(function(){
            $('<img/>')[0].src = this;
        });
    }

    $(['doc/images/slider/1.jpg','doc/images/slider/2.jpg','doc/images/slider/3.jpg',
        'doc/images/slider/4.jpg','doc/images/slider/5.jpg','doc/images/slider/6.jpg',
        'doc/images/slider/7.jpg','doc/images/slider/8.jpg','doc/images/slider/9.jpg',
        'doc/images/slider/10.jpg','doc/images/slider/11.jpg']).preload();

    /// Fader ////////////////////////////////////////////////////////////////////////////////////////////////////
    setInterval("slideSwitch()", 4000 );
    setInterval("slideIntSwitch()", 3000 );
    
    /// Menu Activo ////////////////////////////////////////////////////////////////////////////////////////////////////
    var pagUrl2 = $("#pagUrl2").val();
    $("a[name='"+pagUrl2+"']").addClass("menuActivo");
        
    /// Tabs / Slider ///////////////////////////////////////////////////////////////////////////////////////////////////
    $(".contImgSlider").hide();
	$(".tabSlider li:first").addClass("tabActivo").show();
	$(".contImgSlider:first").show();

	$(".tabSlider li").click(function()
       {
		$(".tabSlider li").removeClass("tabActivo");
		$(this).addClass("tabActivo");
		$(".contImgSlider").hide();

		var activeTab = $(this).find("a").attr("href");
		$(activeTab).fadeIn();
		return false;
	});
    
    /// Imagen  ///////////////////////////////////////////////////////////////////////////////////////////////////
    $(".imgProyA").click(function(){
        var imgId = $(this).attr('id');
        $(".imgProyB").attr('src','doc/images/proyectos/'+imgId+'b.jpg');
    });
    
    /// Lightbox ///////////////////////////////////////////////////////////////////////////////////////////////////
    function formatTitle(title, currentArray, currentIndex, currentOpts) {
        return '<div id="imgProyC-title"><a id="imgProyC-close" href="javascript:;" onclick="$.fancybox.close();"></a><a id="imgProyC-next" href="javascript:;" onclick="$.fancybox.next();"></a><a id="imgProyC-prev" href="javascript:;" onclick="$.fancybox.prev();"></a>' + (title && title.length ? '<b>' + title + '</b>' : '' ) + '</div>';
    }
    $(".imgProyA").fancybox({
        'showCloseButton'	: false,
        'showNavArrows'     : true,
        'cyclic'            : true,
        'padding'           : 3,
        'overlayOpacity'    : 0.8,
        'overlayColor'      : '#000',
    	'titlePosition' 	: 'inside',
    	'titleFormat'		: formatTitle
    });
    
    /// Paginacion ////////////////////////////////////////////////////////////////////////////////////////////////////
    initPagination();
    
});

/**
 * Callback function that displays the content.
 *
 * Gets called every time the user clicks on a pagination link.
 *
 * @param {int} page_index New Page index
 * @param {jQuery} jq the container with the pagination links as a jQuery object
 */
function pageselectCallback(page_index, jq){
    var new_content = jQuery('#hiddenresult div.result:eq('+page_index+')').clone();
    $('#Searchresult').empty().append(new_content);
    return false;
}

/** 
 * Initialisation function for pagination
 */
function initPagination() {
    // count entries inside the hidden content
    var num_entries = jQuery('#hiddenresult div.result').length;
    // Create content inside pagination element
    $("#Pagination").pagination(num_entries, {
        callback: pageselectCallback,
        items_per_page: 1, //Show only one item per page
        next_text: '>',
        prev_text: '<'
    });
 }
