/*function OnLoadEvent(show){
	if(show == true){
		showLoading(true);
		$("#oscuroo").fadeIn(500);
	}else{
		showLoading(false);
		$("#oscuroo").fadeOut(500);
	}
}*/

var containerId = '#tabs-container';
var tabsId = '#tabs';
var loadingId = '#loading';

$(document).ready(function(){
//	OnLoadEvent(true);
	$(tabsId+' a').click(function(e){
		var url = $(this).attr('href');
		if( $(this).data('site') == 'profile' ){
			url = url+'true';
		}
		$(containerId).hide();
		$(loadingId).fadeIn(500);
		$(tabsId+' .current').removeClass('current');
		$(this).parent().addClass('current');
		if($(this).attr('id') != 'openBlog'){
			$(containerId).load(url,function(){
				$(loadingId).fadeOut(500, function(){ $(containerId).fadeIn(1000); });
			});
			e.preventDefault();
		}
	});
});



//Login
$(document).ready(function() {
	$('.signin').click(function(e){
		e.preventDefault();
		$('fieldset#signin_menu').toggle();
		$('.signin').toggleClass("menu-open");
	});
	$('fieldset#signin_menu').mouseup(function(){
		return false;
	});
	$(document).mouseup(function(e){
		if($(e.target).parent('a.signin').length==0){
			$('.signin').removeClass("mouse-open");
			$('fieldset#signin_menu').hide();
		}
	});			
});
		
		
		
		
//PopUp Bonito =)
function CreatePopUp(TITULO,CONTENIDO){
	showLoading(true);
	var currentTime = new Date()
	var time_class = currentTime.getTime();
	var tmp = "<div id=\"popupBox\" class=\"popup_"+time_class+"\"></div>";
	$("body").append(tmp);
	$("#popupBox[class='popup_"+time_class+"']").html('<div class="cabecera"><div class="titulo">'+TITULO+'</div><div class="botonera"><a href="#" class="popup_'+time_class+'"><img src="templates/ofs/images/icons/cancel.png"></a></div></div><div class="contenido">'+CONTENIDO+'</div>');
	var WHeight = $(window).height();
	var WWidth = $(window).width();
	var PHeight = $("#popupBox[class='popup_"+time_class+"']").outerHeight();
	var PWidth = $("#popupBox[class='popup_"+time_class+"']").outerWidth();
	var pos_left = (WWidth/2)-(PWidth/2);
	var pos_top = (WHeight/2)-(PHeight/2);
	$("#popupBox[class='popup_"+time_class+"']").css({'top':pos_top, 'left':pos_left});
	showLoading(false);
	$("#oscuroo").fadeIn(1000);
	$("#popupBox[class='popup_"+time_class+"']").fadeIn(1000);
	$("#popupBox[class='popup_"+time_class+"']").draggable({
		handle:	'.cabecera'
	});
	$("a[class='popup_"+time_class+"']").click(function(e){
		e.preventDefault();
		$("#popupBox[class='popup_"+time_class+" ui-draggable']").fadeOut(1000,function(){
			$("#popupBox[class='popup_"+time_class+" ui-draggable']").remove();
		});
		$("#oscuroo").fadeOut(1000);
	});
	return time_class;
}
$(document).ready(function(){
	$("#popupBox").draggable({
		handle:	'.cabecera'
	});
	$("#popupBox .cabecera a").click(function(e){
		e.preventDefault();
		$("#popupBox").fadeOut(1000);
		$("#oscuroo").fadeOut(1000);
	});
	
/*	$(document).mouseup(function(e){
		var t_id = $( e.target ).data('popup');
		if(t_id != 'popup'){
			$("#popupBox").fadeOut(1000);
		}
	});*/	
});
$(function() {
  $('#imagenloca').tipsy({gravity: 'n'});
	$('#videoloco').tipsy({gravity: 'n'});
	$('#equipo').tipsy({gravity: 's'});
	$('#desarrolladores').tipsy({gravity: 's'});
	$('#sponsors').tipsy({gravity: 's'});
	$('#faq').tipsy({gravity: 's'});
	$('#logo').tipsy({gravity: 'n'});
	$('#youtube').tipsy({gravity: 's'});
	$('#usuarioslogueados').tipsy({gravity: 's'});
	$('#add').tipsy({gravity: 'n'});
	$('#grupo').tipsy({gravity: 's'});
	$('#favoritos').tipsy({gravity: 'n'});
	$('#misofus').tipsy({gravity: 's'});
	$('#salida').tipsy({gravity: 'n'});
	$('#config').tipsy({gravity: 'n'});
	$('#juegos').tipsy({gravity: 'n'});
	$('#blogs').tipsy({gravity: 'n'});
	$('#perfil').tipsy({gravity: 'n'});
	$('#photo').tipsy({gravity: 'n'});
	$('#video').tipsy({gravity: 'n'});
	$('#mp').tipsy({gravity: 'n'});
	$('#rango').tipsy({gravity: 'n'});
	$('#ofuscones').tipsy({gravity: 'n'});
	$('#mensamuro').tipsy({gravity: 'n'});
	$('#visitas').tipsy({gravity: 'n'});
	$('#notis').tipsy({gravity: 'n'});
	$('#blogs').tipsy({gravity: 'n'});
	$('#tops').tipsy({gravity: 'n'});
	$('#visitas').tipsy({gravity: 'n'});
	$('#estado').tipsy({gravity: 'n'});
	$('#mensajes').tipsy({gravity: 'n'});
	$('#promovideo').tipsy({gravity: 's'});	  
	$('#chatpm').tipsy({gravity: 's'});	  
	$('#recargar').tipsy({gravity: 'n'});	  
	$('a.miembrosindex').tipsy({live: true});
	$('#perfiluno').tipsy({gravity: 'n'});
	$('#perfildos').tipsy({gravity: 'n'});
	$('#perfiltres').tipsy({gravity: 'n'});
	$('#perfilcuatro').tipsy({gravity: 'n'});
	$('#perfilcinco').tipsy({gravity: 'n'});
	$('#perfilseis').tipsy({gravity: 'n'});
	$('#userson').tipsy({gravity: 'n'});
	$('#usertrado').tipsy({gravity: 'n'});
	$('#fotoalbum').tipsy({gravity: 's'});
	$('#subirfotito').tipsy({gravity: 'n'});
	$('#bloquear_usuario').tipsy({gravity: 's'});
	$('#seguidoresindex').tipsy({gravity: 'n'});
	$('#amigosindex').tipsy({gravity: 'n'});
	$('#ofusindex').tipsy({gravity: 'n'});
	$('.avatares').tipsy({gravity: 's'});
	$('#editarbannerperfil').tipsy({gravity: 'e'});
	$('.horaactividad').tipsy({gravity: 'e'});
	$('.daemoticon').tipsy({gravity: 'n'});
	

	});

function showLoading(status){
	var WHeight = $(window).height();
	var WWidth = $(window).width();
	var PHeight = $("#loadingGral").outerHeight();
	var PWidth = $("#loadingGral").outerWidth();
	var pos_left = (WWidth/2)-(PWidth/2);
	var pos_top = (WHeight/2)-(PHeight/2);
	if(status == true){
		$("#loadingGral").css({'top':pos_top, 'left':pos_left});
		$("#loadingGral").fadeIn(500);
	}else{
		$("#loadingGral").fadeOut(500);
	}
}

(function($) {

    $.fn.innerfade = function(options) {
        return this.each(function() {   
            $.innerfade(this, options);
        });
    };

    $.innerfade = function(container, options) {
        var settings = {
        		'animationtype':    'fade',
            'speed':            'normal',
            'type':             'sequence',
            'timeout':          2000,
            'containerheight':  'auto',
            'runningclass':     'innerfade',
            'children':         null
        };
        if (options)
            $.extend(settings, options);
        if (settings.children === null)
            var elements = $(container).children();
        else
            var elements = $(container).children(settings.children);
        if (elements.length > 1) {
            $(container).css('position', 'relative').css('height', settings.containerheight).addClass(settings.runningclass);
            for (var i = 0; i < elements.length; i++) {
                $(elements[i]).css('z-index', String(elements.length-i)).css('position', 'absolute').hide();
            };
            if (settings.type == "sequence") {
                setTimeout(function() {
                    $.innerfade.next(elements, settings, 1, 0);
                }, settings.timeout);
                $(elements[0]).show();
            } else if (settings.type == "random") {
            		var last = Math.floor ( Math.random () * ( elements.length ) );
                setTimeout(function() {
                    do { 
												current = Math.floor ( Math.random ( ) * ( elements.length ) );
										} while (last == current );             
										$.innerfade.next(elements, settings, current, last);
                }, settings.timeout);
                $(elements[last]).show();
						} else if ( settings.type == 'random_start' ) {
								settings.type = 'sequence';
								var current = Math.floor ( Math.random () * ( elements.length ) );
								setTimeout(function(){
									$.innerfade.next(elements, settings, (current + 1) %  elements.length, current);
								}, settings.timeout);
								$(elements[current]).show();
						}	else {
							alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
						}
				}
    };

    $.innerfade.next = function(elements, settings, current, last) {
        if (settings.animationtype == 'slide') {
            $(elements[last]).slideUp(settings.speed);
            $(elements[current]).slideDown(settings.speed);
        } else if (settings.animationtype == 'fade') {
            $(elements[last]).fadeOut(settings.speed);
            $(elements[current]).fadeIn(settings.speed, function() {
							removeFilter($(this)[0]);
						});
        } else
            alert('Innerfade-animationtype must either be \'slide\' or \'fade\'');
        if (settings.type == "sequence") {
            if ((current + 1) < elements.length) {
                current = current + 1;
                last = current - 1;
            } else {
                current = 0;
                last = elements.length - 1;
            }
        } else if (settings.type == "random") {
            last = current;
            while (current == last)
                current = Math.floor(Math.random() * elements.length);
        } else
            alert('Innerfade-Type must either be \'sequence\', \'random\' or \'random_start\'');
        setTimeout((function() {
            $.innerfade.next(elements, settings, current, last);
        }), settings.timeout);
    };

})(jQuery);

// **** remove Opacity-Filter in ie ****
function removeFilter(element) {
	if(element.style.removeAttribute){
		element.style.removeAttribute('filter');
	}
}

(function($){
	$.fn.extend({
		autoResize: function(options){
			this.each(function(){
				if(!options){
					options = {};
				}
				var _options = {
					maxHeight: options.maxHeight || null,
					minHeight: options.minHeight || null,
					textHold: options.textHold || null,
					activeClass: options.activeClass || null
				};
				var $this = $(this);
				if($this.val() == "" && _options.textHold){
					$this.val(_options.textHold);
				}
				$this.initHeight = $this.css("height");
				if(_options.maxHeight){
					$this.css("overflow", "auto");
				}else{
					$this.css("overflow", "hidden");
				}
				var _value = null;
				var $clon = $this.clone(true);
				$clon.css({
					visibility: "hidden",
					position: "absolute",
					top: 0,
					left: 0,
					overflow: "hidden",
					width: parseInt($this.width())-10
				});
				$clon.attr("name","");
				$clon.attr("id", "");
				$this.parent().append($clon);
				var clon = $clon[0];
				var me = $this;
				$this.bind("keyup" , autoFit)
					.bind("focus", function(){
						if(_options.textHold){
							if(this.value == _options.textHold){
								this.value = "";
							}
						}
						if(_options.minHeight){
							me.css("height", _options.minHeight+"px");
							$clon.css("height", _options.minHeight+"px");
							autoFit(true);
						}
						if(_options.activeClass){
							me.addClass(_options.activeClass);
						}
					})
					.bind("blur", function(){
						if(_options.textHold){
							if(this.value == ""){
								this.value = _options.textHold;
								if(_options.minHeight && me.initHeight){
									$clon.css("height", me.initHeight);
									me.css("height", me.initHeight);
									autoFit();
								}
							}
						}else{
							if(_options.minHeight && me.initHeight){
								$clon.css("height", me.initHeight);
								me.css("height", me.initHeight);
								autoFit();
							}
						}
						if(_options.activeClass){
							me.removeClass(_options.activeClass);
						}
					});
				function autoFit(force){
					clon.value = me.val();
					if (_value != clon.value || force===true){
						_value = clon.value;
						var h = clon.scrollHeight;
						if(_options.maxHeight && h > _options.maxHeight){
							me.css("height", options.maxHeight + "px");
						}else{
							me.css("height", parseInt(h) + "px");
						}
					}
				}
			});
		}
	})
}(jQuery));




$(window).load(function() {
    
    $('.tag').each(function(i) {
        setTimeout(function() {
            $('.tag:eq('+i+')').css({ display: 'block', opacity: 0 }).stop().animate({ opacity: 1 }, 'easeInOutExpo'); 
        }, 150 * (i + 1))
    });

    $('.tag').hover(function() {
        $(this).stop().animate({ paddingRight: ($('.tag_count', this).outerWidth() - 5) }, 'easeInOutExpo');
    }, function() {
        $(this).stop().animate({ paddingRight: 5 }, 'easeInOutExpo');
    });

    $('.tag').click(false);

});



(function($){
    $.countdown=function(el,options){
        var base    =this;
        base.$el    =$(el);
        base.el     =el;

        base.$el.data("CountDown",base);

        base.init=function(){

            base.options=$.extend({},$.countdown.defaults,options);

            var current=typeof base.options.date=='object'?base.options.date:new Date();
            
            var input=base.options.source !="inner" ? base.$el.attr(base.options.source):base.$el.text(); //@todo: do better

            switch(base.options.format){
                case'timestamp':
                    base.date=new Date(input*1000);
                    break;
                case'Y-m-d H:i:s':
                    base.date=base.parse(input);
                    break;

                case'function': //Call user function
                    base.date=base.options.parse(input);
                    break;
            }
            var diff=(base.date.getTime()-current.getTime())/1000;
            
            if(diff>0){

                base.days=Math.floor(diff/86400)

                diff=diff-(86400*base.days);

                base.hours=Math.floor(diff/3600);

                diff=diff-(3600*base.hours);

                base.minutes=Math.floor(diff/60);

                diff=diff-(60*base.minutes);

                base.seconds=Math.floor(diff);
                
                base.print();

                base.interval=setInterval(function(){
                    
                    base.update()

                },1000);

                return;
            }

            return;
        };

        base.parse=function(date){
            var regex=/^([0-9]{2,4})-([0-1][0-9])-([0-3][0-9]) (?:([0-2][0-9]):([0-5][0-9]):([0-5][0-9]))?$/;
            var parts=date.replace(regex,"$1 $2 $3 $4 $5 $6").split(' ');
            return new Date(parts[0],parts[1]-1,parts[2],parts[3],parts[4],parts[5]);
        };

        base.print=function(){
            
            base.$el.html('');

            base.$el.append(base.options.contentBefore);

            if(base.days>0){
                base.$el.append('<span class="days">'+base.days+'</span>');
                base.$el.append(base.options.txtAfterDays);
            }

            hours   = base.hours<10?'0'+base.hours:base.hours;
            minutes = base.minutes<10?'0'+base.minutes:base.minutes;
            seconds = base.seconds<10?'0'+base.seconds:base.seconds;

            base.$el.append('<span class="hours">'+hours+'</span>');
            base.$el.append(base.options.txtAfterHours);
            base.$el.append('<span class="minutes">'+minutes+'</span>');
            base.$el.append(base.options.txtAfterMinutes);
            base.$el.append('<span class="seconds">'+seconds+'</span>');
            base.$el.append(base.options.txtAfterSeconds);
            base.$el.append(base.options.contentAfter);
        };

        base.update=function(){

            base.seconds--;

            if(base.seconds<0){
                base.seconds=59;
                base.minutes--
                if(base.minutes<0){
                    base.minutes=59;
                    base.hours--;
                    if(base.hours<0){
                        base.days--;
                        base.hours=23;
                    }
                }
            }

            base.print();
             
            if(base.seconds == 0 && base.minutes == 0 && base.hours == 0 && base.days == 0){

                clearInterval(base.interval);

                if(typeof(base.options.onExpires)=="function"){

                    base.options.onExpires(base.$el);
                }
            }
        }

        base.init();
    };

    $.countdown.defaults={
        date:'',                // Date Object
        txtAfterDays:'d / ',    //HTML Content
        txtAfterHours:':',      //HTML Content
        txtAfterMinutes:':',    //HTML Content
        txtAfterSeconds:'',     //HTML Content
        format:'timestamp',     //Source format 'datetime', 'timestamp' or 'function' [need to set parse pram]
        source:'title',         //Get datetime from attribute ('title', 'datetime' [html5]) or from 'inner' text,
        contentBefore:'',       //HTML Content
        contentAfter:'',        //HTML Content
        parse:'',               //Call function(elemnt)
        onExpires:''            //Call function(elemnt)
    };

$.fn.countdown=function(options){

    if(typeof(options)=="object"){

        this.each(function(i){
            
            new $.countdown(this,options);
        });
    }
};

})(jQuery);

