Array.prototype.exists = function(o){
    for (var i = 0; i < this.length; i++) 
        if (this[i] === o) 
            return i;
    return -1;
}

function flip(element){
    var views = ($.cookie('views') != null) ? $.cookie('views').split("^") : [];
    
    if ($(element).css('display') == 'block') {
        $(element).hide("fast");
        if (views.exists(element) >= 0) {
            views.splice(views.exists(element), 1);
            $.cookie('views', views.join("^"));
        }
    }
    else {
        $(element).show("fast");
        if (views.exists(element) < 0) {
            views.push(element);
            $.cookie('views', views.join("^"));
        }
    }
}

function slideProfesores(items){
    pcount = 0;
    pactual = 0;
    pasos = items;
    $("#cuerpo .izquierda .profesores .item").each(function(){
        $(this).attr("id", "profesor" + pcount);
        pcount++;
    });
}

function slideUp(){
    if (pactual < (pcount - 1)) {
        var total = pasos;
        var alto = 0;
        while (total > 0 && pactual < (pcount - 1)) {
            alto += parseInt($("#cuerpo .izquierda #profesor" + pactual + " .detalle").height()) + 50;
            pactual++;
            total--;
        }
        $("#cuerpo .izquierda .profesores .item").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($("#cuerpo .izquierda #profesor" + pactual + " .detalle").height()) + 50;
            total--;
        }
        $("#cuerpo .izquierda .profesores .item").each(function(){
            $(this).animate({
                "bottom": "-=" + alto + "px"
            }, 1000)
        });
    }
}

jQuery.fn.news_ticker = function(limite, intervalo){
    limite = limite || 4;
    intervalo = intervalo || 6000;
    display = this.parent().css('display');
    
    return this.each(function(){
        var $list = $(this), items = [], currentItem = limite, total = 0, start = 0, heights = [];
        if (display == 'none') {
            $(this).parent().show();
        }
        $list.find('> div').each(function(){
            items.push('<div class="' + $(this).attr('class') + '">' + $(this).html() + '</div>');
            heights.push($(this).height());
        });
        if (display == 'none') {
            $(this).parent().hide();
        }
        total = items.length;
        $list.find('> div').filter(':gt(' + (limite - 1) + ')').remove();
        
        function rotar(){
			if (total > 4) {
				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: heights[(currentItem + total - 1) % total]
					}, 1000).animate({
						opacity: 1
					}, 1000);
					$(this).animate({
						height: 0
					}, 1000, function(){
						$(this).remove();
					});
				});
				currentItem = (currentItem + 1) % total;
				setTimeout(rotar, intervalo);
			}
        }
        if (start < 1) {
            setTimeout(rotar, intervalo);
            start++;
        }
        else {
            rotar();
        }
    });
};
$(document).ready(function(){
    var views = ($.cookie('views') != null) ? $.cookie('views').split("^") : $.cookie('session') == 'true' ? [] : [".derecha #news"];
    $.cookie('views', views.join("^"));
    $.cookie('session', 'true')
    for (var i in views) {
        $(views[i]).css('display', 'block');
    }
    $("#news .container").news_ticker();
    var fcount = 0;
    var params = {
        wmode: "opaque"
    };
    var attributes = {};
    $("#cuerpo .izquierda h1").each(function(){
        $(this).attr("id", "titulo" + fcount);
        var flashvars = {
            "contenido": $(this).html()
        };
        swfobject.embedSWF("swf/titulo.swf", "titulo" + fcount, $(this).width(), $(this).height(), "9", "./swf/expressInstall.swf", flashvars, params, attributes);
        fcount++;
    });
    $("#header h2").each(function(){
        $(this).attr("id", "menu" + fcount);
        var flashvars = {
            "contenido": $(this).html()
        };
    	var params = {
        	wmode: "transparent"
    	};
        swfobject.embedSWF("swf/menu.swf", "menu" + fcount, $(this).width(), $(this).height(), "9", "./swf/expressInstall.swf", flashvars, params, attributes);
        fcount++;
        var flashvars = {};
        var params = {
            wmode: "opaque",
			scalemode: "noscale"
        };
        var attributes = {};
        swfobject.embedSWF("swf/banner_top.swf", "banner_top", "880", "200", "9", "./swf/expressInstall.swf", flashvars, params, attributes);
        
    });
});
