﻿function insertHTML(idthumb,posicion){
	html = '<img src="/Archivo.aspx?id='+idthumb+'" alt=""/>';
	html = '<p style="text-align: '+posicion+';">'+html+'</p>';
	
	tinyMCEPopup.execCommand('mceInsertContent',false,html);
	tinyMCEPopup.close();
};

function ModalAceptar(){
    window.location='/Default.aspx';
};

$(document).ready(function(){
    $("a[href*='http://']:not([href*='"+location.hostname+"'])").attr("target","_blank");
    $("a[href$='zip']").attr("target","_blank");
    $("a[href$='zip']").click (function (){
        pageTracker._trackPageview($(this).attr('href'));
    });
    $("a[href$='pdf']").attr("target","_blank");
    $("a[href$='pdf']").click (function (){
        pageTracker._trackPageview($(this).attr('href'));
    });
});

jQuery.fn.rotativo = function (limite, intervalo) {
        limite = limite || 3;
        intervalo = intervalo || 10000;
        
        return this.each(function () {
            var $list = $(this),
                items = [],
                currentItem = limite,
                total = 0,
                start = 0,
                height = $list.find('> div:first').height();
                
            $list.find('> div').each(function () {
                items.push('<div class="item" style="margin:0;padding:0;height:100px;">' + $(this).html() + '</div>');
            });

            total = items.length;
            $list.css({ height : height * limite });
            $list.find('> div').filter(':gt(' + (limite - 1) + ')').remove();

            function rotar() {
                var $insert = $(items[currentItem]).css({
                    height : 0,
                    opacity : 0,
                    display : 'none'
                }).appendTo($list);
                $list.find('> div:first').animate({ opacity : 0}, 1000, function () {
                    $insert.animate({ height : height }, 1000).animate({ opacity : 1 }, 1000);
                    $(this).animate({ height : 0 }, 1000, function () {
                        $(this).remove();
                    });
                });
                currentItem++;
                if (currentItem >= total) {
                    currentItem = 0;
                }
                setTimeout(rotar, intervalo);
            }
            if (start < 1) {
               setTimeout(rotar, intervalo);
               start++;
            } else {
                rotar();
            }
        });
    };

function limitChars(textclass, limit, infodiv) {
    var text = $(textclass).val(); 
    var textlength = text.length;
    if(textlength > limit){
        $('.'+infodiv).html(limit);
        $('.'+textclass).val(text.substr(0,limit));
        return false;
    }
    else {
        $('.'+infodiv).html(limit - textlength);
        return true;
    }
}

function add_modal(clase,html) {
    $('body').append('<div id="popup_overlay"></div>');

    $('#popup_overlay').css({
        position: 'absolute',
        zIndex: 9900,
        top: '0px',
        left: '0px',
        width: '100%',
        height: $("body").height() + 'px',
        background: '#000',
        opacity: '.7',
        filter: 'alpha(opacity=70)'
    });
    
    $('body').append(html);
                    
    var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed';
    
    if(clase=='imagen' && $.browser.msie){    
        $('.modal.'+clase).css({
            position: pos,
            zIndex: 9901,
            margin: 0
        });
    }else{
        $('.modal.'+clase).css({
            position: pos,
            zIndex: 9901,
            margin: 0,
            opacity: 0,
            filter: 'alpha(opacity=0)'
        }).animate({ opacity:1 }, 'slow');
    }
        
    var top = (($(window).height() / 2) - ($('.modal.'+clase).outerHeight() / 2)) + (-80);
    var left = (($(window).width() / 2) - ($('.modal.'+clase).outerWidth() / 2)) + 0;
        
    if( top < 0 ) top = 0;
    if( left < 0 ) left = 0;
    if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
        
    $('.modal.'+clase).css({
            top: top + 'px',
            left: left + 'px'
    });
    
    $('.modal.'+clase+' .cerrar').live('click',function() {
        $('.modal.'+clase).remove();
        $('#popup_overlay').remove();
    });
    
    $('.modal.'+clase+' .btncerrar').live('click',function() {
        $('.modal.'+clase).remove();
        $('#popup_overlay').remove();
    });
};

function add_modalAdv(clase,html) {
    $('body').append('<div id="popup_overlay"></div>');

    $('#popup_overlay').css({
        position: 'absolute',
        zIndex: 9900,
        top: '0px',
        left: '0px',
        width: '100%',
        height: $("body").height() + 'px',
        background: '#000',
        opacity: '.7',
        filter: 'alpha(opacity=70)'
    });
    
    $('body').append(html);
                    
    var pos = ($.browser.msie && parseInt($.browser.version) <= 6 ) ? 'absolute' : 'fixed';
    
    if(clase=='imagen' && $.browser.msie){    
        $('.modal.'+clase).css({
            position: pos,
            zIndex: 9901,
            margin: 0
        });
    }else{
        $('.modal.'+clase).css({
            position: pos,
            zIndex: 9901,
            margin: 0,
            opacity: 0,
            filter: 'alpha(opacity=0)'
        }).animate({ opacity:1 }, 'slow');
    }
        
    var top = (($(window).height() / 2) - ($('.modal.'+clase).outerHeight() / 2)) + (-80);
    var left = (($(window).width() / 2) - ($('.modal.'+clase).outerWidth() / 2)) + 0;
        
    if( top < 0 ) top = 0;
    if( left < 0 ) left = 0;
    if( $.browser.msie && parseInt($.browser.version) <= 6 ) top = top + $(window).scrollTop();
        
    $('.modal.'+clase).css({
        top: top + 'px',
        left: left + 'px'
    });
};

function slide(selector ,items){
	sel = selector;
    pcount = 0;
    pactual = 0;
    pasos = items;
    $(selector).each(function(){
        $(this).attr("id", "item" + pcount);
        pcount++;
    });
}

function slideUp(){
    if (pactual < (pcount - 1)) {
        var total = pasos;
        var alto = 0;
        while (total > 0 && pactual < (pcount - 1)) {
            alto += parseInt($("#item" + pactual).height()) + 10;
            pactual++;
            total--;
        }
        $(sel).each(function(){
            $(this).animate({
                "bottom": "+=" + alto + "px"
            }, 1000)
        });
    }
}

function slideDown(){
    if (pactual > 0) {
        var total = pasos;
        var alto = 0;
        while (total > 0 && pactual > 0) {
            pactual--;
           	alto += parseInt($("#item" + pactual).height()) + 10;
            total--;
        }
        $(sel).each(function(){
            $(this).animate({
                "bottom": "-=" + alto + "px"
            }, 1000)
        });
    }
}
function slideRight(){
    if (pactual < (pcount - 1)) {
        var total = pasos;
        var ancho = 0;
        while (total > 0 && pactual < (pcount - 1)) {
            ancho += parseInt($("#item" + pactual).width()) + 10;
            pactual++;
            total--;
        }
        $(sel).each(function(){
            $(this).animate({
                "left": "-=" + ancho + "px"
            }, 1000)
        });
    }
}

function slideLeft(){
    if (pactual > 0) {
        var total = pasos;
        var ancho = 0;
        while (total > 0 && pactual > 0) {
            pactual--;
           	ancho += parseInt($("#item" + pactual).width()) + 10;
            total--;
        }
        $(sel).each(function(){
            $(this).animate({
                "left": "+=" + ancho + "px"
            }, 1000)
        });
    }
}

$(document).ready(function() {		
    slideShow(5000);
    
    $('.watermark').each(function (i){
        if($(this).val() == '' || $(this).val() == $(this).attr('title'))
            $(this).addClass('on').val($(this).attr('title'));
        else
            $(this).removeClass('on');
    });
    $('.watermark').focus(function() {
        $(this).filter(function() {
            return $(this).val() == '' || $(this).val() == $(this).attr('title');
        }).removeClass('on').val('');
    });
    $('.watermark').blur(function() {
        $(this).filter(function() {
            return $(this).val() == ''
        }).addClass('on').val($(this).attr('title'));
    });
});

function slideShow(speed) {
    $('ul.slideshow').append('<li id="slideshow-caption" class="caption"><div class="slideshow-caption-container"><div class="fecha"></div><p><span class="titulo"></span> <span class="user"></span></p><img src="images/fondos/home/destacado_fondo.gif" alt="fondo" class="fondo"/></li>');
    $('ul.slideshow li').css({opacity: 0.0});
    $('ul.slideshow li:first').css({opacity: 1.0});
    $('#slideshow-caption .fecha').html($('ul.slideshow li:first').find('span.fecha').html());
    $('#slideshow-caption .titulo').html($('ul.slideshow li:first').find('span.titulo').html());
    $('#slideshow-caption .user').html($('ul.slideshow li:first').find('span.user').html());
    $('#slideshow-caption').css({opacity: 1.0, bottom:0});
    var timer = setInterval('gallery()',speed);
    $('ul.slideshow').hover(
        function () {
	        clearInterval(timer);	
        }, 	
        function () {
	        timer = setInterval('gallery()',speed);			
        }
    );
	
}

function gallery() {
    var current = ($('ul.slideshow li.show')?  $('ul.slideshow li.show') : $('#ul.slideshow li:first'));
    var next = ((current.next().length) ? ((current.next().attr('id') == 'slideshow-caption')? $('ul.slideshow li:first') :current.next()) : $('ul.slideshow li:first'));
    var fecha = next.find('span.fecha').html();
    var title = next.find('span.titulo').html();	
    var user = next.find('span.user').html();
    next.css({opacity: 0.0}).addClass('show').animate({opacity: 1.0}, 1000);
    $('#slideshow-caption').animate({bottom:-70}, 300, function () {
	        $(this).find('.fecha').html(fecha);
	        $('#slideshow-caption .titulo').html(title);
	        $('#slideshow-caption .user').html(user);
	        $('#slideshow-caption').animate({bottom:0}, 500);	
    });		
    current.animate({opacity: 0.0}, 1000).removeClass('show');
}

function pDate(date_str) {
    var time = new Date(('' + date_str).replace(/-/g,"/").replace(/[TZ]/g," ")).getTime() / 1000;
    var date = new Date(('' + date_str).replace(/-/g,"/").replace(/[TZ]/g," "));
    var hora = date.toLocaleTimeString().split(':');
    var now = Math.round(new Date().getTime() / 1000);
    var diff = (time < now ? now - time : -1);
    var text = "";
    if (diff < 0) {
        text = "No estoy seguro!!"
    } else {
        var seg = diff % 60;
        diff = (diff - seg) / 60;
        var min = diff % 60;
        diff = (diff - min) / 60;
        var hrs = diff % 24;
        diff = (diff - hrs)   / 24;
        var dias = diff;
        text = (dias > 31 ? "El "+date.getDayName()+" "+date.asString("dd mmm")+" a las "+hora[0]+":"+hora[1]: "Hace ");
        text += (dias < 31 && dias > 0 ? dias + " día"+ (dias == 1 ? "" : "s") + " ": "");
        text += (dias == 0 && hrs > 0 ? hrs + " hora" + (hrs == 1 ? "" : "s") + " ": "");
        text += (dias == 0 && min > 0 ? min + " minuto" + (min == 1 ? "" : "s") + " ": "");
        text += (dias == 0 && hrs == 0 && min == 0 && seg > 0 ? seg + " segundo" + (seg == 1 ? "" : "s") + " ": "");
    }
    return text;
}
$.fn.pDate = function(){
    return $(this).each(function(){
	    var date = pDate($(this).attr("title"));
	    if(date)
	        $(this).text(date);
    });
}
