jQuery(document).ready(function($) {
	/*
	// Popup de Grupos	
	$(".fancybox-groups").fancybox({
		'width'				: 700,
		'height'			: 560,
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'hideOnOverlayClick': false,
	    'hideOnContentClick': false,
	    'titleShow' 		: false,
	    'titlePosition' 	: 'over',
		'type'				: 'iframe',
		'onClosed': function() {
		    parent.location.reload(true);
		  }
	});	
	*/
	
	// Popup de Aviso Legal
	$(".fancybox-avisolegal").fancybox({
		'width'				: 700,
		'height'			: 570,
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'hideOnOverlayClick': false,
	    'hideOnContentClick': false,
	    'titleShow' 		: false,
	    'titlePosition' 	: 'over',
		'type'				: 'iframe',
		'modal'				: true,
		'onClosed': function() {
		    parent.location.reload(true);
		  }
	});
	
	// Popup de Pregunta a todos
	$(".fancybox-preguntatodos").fancybox({
		'width'				: 700,
		'height'			: 570,
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'hideOnOverlayClick': false,
	    'hideOnContentClick': false,
	    'titleShow' 		: false,
	    'titlePosition' 	: 'over',
		'type'				: 'iframe',
		'onClosed': function() {
		    parent.location.reload(true);
		  }
	});
	
	/* Popup de Friends
	$("#fancybox-friends").fancybox({
		'width'				: 700,
		'height'			: 560,
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'hideOnOverlayClick': false,
	    'hideOnContentClick': false,
	    'titleShow' 		: false,
	    'titlePosition' 	: 'over',
		'type'				: 'iframe'
	});*/
	
	// Popup de Testimonies
	$(".fancybox-testimonies").fancybox({
		'width'				: 700,
		'height'			: 570,
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'hideOnOverlayClick': false,
	    'hideOnContentClick': false,
	    'titleShow' 		: false,
	    'titlePosition' 	: 'over',
		'type'				: 'iframe',
		'onClosed': function() {
		    parent.location.reload(true);
		  }
	});
	
	// Popup de denunciar
	$(".fancybox-denuncias").fancybox({
		'width'				: 700,
		'height'			: 560,
		'autoScale'			: true,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'hideOnOverlayClick': false,
	    'hideOnContentClick': false,
	    'titleShow' 		: false,
	    'titlePosition' 	: 'over',
		'type'				: 'iframe'
	});

	// Popup del registro
	
	$(".fancybox-unete").fancybox({
		'width'				: 584,
		'height'			: 501,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'hideOnOverlayClick': false,
	    'hideOnContentClick': false,
	    'titleShow' 		: false,
	    'titlePosition' 	: 'over',
		'type'				: 'iframe'
	});
	
	
	$(".fancybox-video").fancybox({
		'width'				: 584,
		'height'			: 501,
		'autoScale'			: false,
		'transitionIn'		: 'none',
		'transitionOut'		: 'none',
		'hideOnOverlayClick': false,
	    'hideOnContentClick': false,
	    'titleShow' 		: false,
	    'titlePosition' 	: 'over',
		'type'				: 'iframe'
	});

	// Popup imagenes activity
	
	jQuery.CVP = jQuery.CVP || {};
	
	jQuery.CVP.imgFancy = function() {
		$(".fancybox-activity-images").fancybox({
			'width'				: 920,
			'height'			: 720,
			'autoScale'			: true,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'hideOnOverlayClick': true,
		    'hideOnContentClick': true,
		    'titleShow' 		: false,
		    'titlePosition' 	: 'inside',
			'type'				: 'image'
		});	
	};
	jQuery.CVP.imgFancy(); 
		
	if ($("#oauthConnect").length > 0) {
		
		$("#oauthConnect").fancybox({
			'width'				: 600,
			'height'			: 400,
			'autoScale'			: true,
			'transitionIn'		: 'none',
			'transitionOut'		: 'none',
			'hideOnOverlayClick': false,
		    'hideOnContentClick': false,
		    'titleShow' 		: false,
		    'titlePosition' 	: 'over',
			'type'				: 'iframe',
			'onClosed'			: function () {

				document.location.href = $("base").attr("href");
			}
		});
		
		$("#oauthConnect").trigger("click");
		
	}
	
	// Ayuda Modal y Me gusta
	if(typeof(jQuery.cluetip) == "object") {
	    
	    jQuery.cluetipCallback = function() {
        
	        $(this)
                   .addClass("cluetipped")
                   .cluetip({
                        hoverIntent: false,
                        sticky: true,
                        mouseOutClose: true,
                        closePosition: 'title',
                        escapeTitle: true,
                        cluetipClass: 'jtip',
                        dropShadow: false,
                        showTitle: false,
                        arrows: true,
                        clickThrough: true,
                        ajaxCache: false
                    });
	    };
	    
	    jQuery(".cluetip").each(jQuery.cluetipCallback);
		
	    jQuery(".cluetip-through").cluetip({
			/*
			width: '200px',
			height: '166px',
			*/			
			escapeTitle: true,
			cluetipClass: 'jtip',
			dropShadow: false,
			showTitle: false,
			arrows: true,
			clickThrough: true,
			ajaxCache: false
			/*
			positionBy: 'topRight',
			topOffset: 150
			*/
		});
	}
	
	$('.load-more').on('click',function(){
	    setTimeout(function(){
	        
	        $(".cluetip").cluetip({
	            hoverIntent: false,
	            sticky: true,
	            mouseOutClose: true,
	            closePosition: 'title',
	            escapeTitle: true,
	            cluetipClass: 'jtip',
	            dropShadow: false,
	            showTitle: false,
	            arrows: true,
	            clickThrough: true,
	            ajaxCache: false
	        });
	    },5000);
	    
    });

	// Filtro por Estado (bolitas)
	$("#IronCheck_familiares").live('click', function() {		
		$("#IronEstadoFilter").val('familiares');
		$("#IronEstadoForm").submit();
	});
	
	// Filtro por Estado (bolitas)
	$("#IronCheck_sintomaticos").live('click', function() {		
		$("#IronEstadoFilter").val('sintomaticos');
		$("#IronEstadoForm").submit();
	});
	
	// Filtro por Estado (bolitas)
	$("#IronCheck_reflexivos").live('click', function() {		
		$("#IronEstadoFilter").val('reflexivos');
		$("#IronEstadoForm").submit();
	});
	
	// Filtro por Estado (bolitas)
	$("#IronCheck_transgresores").live('click', function() {		
		$("#IronEstadoFilter").val('transgresores');
		$("#IronEstadoForm").submit();
	});
	
	// Privacity options
	$("#privacity-options").live('click', function() {		
		$(".bp-profile-privacy").slideToggle('normal');		
	});	
	
	$("#status-menu").bind("ocultar",function() {
		var curWidth = $(this).width();
		$(this).animate({
						width:'0px',
						opacity:'0'
						}, 300, function() {
							$(this).hide().css({width:curWidth+'px',opacity:'1'})
		});
	});
	
	var timeOut = false;
	
	// Estado de Animo en la frase de como estas hoy
	/*
	$(".userinfo-phrase").live('mouseenter', function() {
		window.clearTimeout(timeOut);
		$("#status-menu").show();
	});
	
	$(".userinfo-phrase").live('mouseleave', function() {
		timeOut = window.setTimeout(function() {
			$("#status-menu").trigger("ocultar");
		},300);
	});
	*/

		
	// Estado de Animo (mouseenter)
	$(".userinfo-button").on('mouseenter', function() {
		window.clearTimeout(timeOut);
		$("#status-menu").show();		
	});
	
	// Estado de Animo (mouseleave)
	$(".userinfo-button").on('mouseleave', function() {		
		timeOut = window.setTimeout(function() {
			$("#status-menu").trigger("ocultar");
		},300);		
	});
	
	// Estado de Animo (mouseleave)
	$("#status-menu").on('mouseleave', function() {
		window.clearTimeout(timeOut);
		timeOut = window.setTimeout(function() {
			$("#status-menu").trigger("ocultar");
		},300);
	});	
	
	// Estado de Animo (mouseenter)
	$("#status-menu").on('mouseenter', function() {
		window.clearTimeout(timeOut);
		$(this).show();
	});
	
	// Estado de Animo
	/*
	$("#userinfo-state").live('click', function() {
		$("#userinfo-state-ul").slideToggle('normal');
		return false;
	});
	*/
	
	// Comment Form (Esc key)
	/*
	$(".ac-input").live('keyup', function(e){
		if (e.keyCode == 27) {
			$('.ac-input').animate({height:'20px'});
		}
	}
	*/	
	
	// Comment Form (focus)
	/*
	$(".ac-form").live('focus', function(){
		$('.ac-form').hide();
		$('.ac-input').animate({height:'80px'});
		$(this).show();
		$(this).animate({height:'86px'});
		//$(this).animate({height:'112px'});
		//$('.ac-input').animate({height:'112px'});
		//$('.ac-form').animate({height:'112px'});
	});	

	// Comment Form (lostfocus)
	$(".ac-form").live('blur', function(){
		$('.ac-form').fadeIn();
		$('.ac-input').animate({height:'16px'});
		$(this).animate({height:'24px'});
		//$(this).animate({height:'20px'});
	});	
	*/

	$(".ac-input").live('focus', function(){
		var idform = $(this).attr('name').replace("ac_input_",""); 
		if ($("li[id='activity-"+idform+"'] input[type=submit]").attr('disabled')){
			$("li[id='activity-"+idform+"'] input[type=submit]").removeAttr('disabled')
			$("li[id='activity-"+idform+"'] div[id='message']").slideUp(function (){$('#message').remove()});
		}
		
		//$('.ac-form').hide();
		$(this).animate({height:'80px'});
		$(this).parent().parent().animate({height:'88px'});
		//$(this).show();
		//$(this).animate({height:'86px'});
		//$(this).animate({height:'112px'});
		//$('.ac-input').animate({height:'112px'});
		//$('.ac-form').animate({height:'112px'});
	});	

	// Comment Form (lostfocus)
	$(".ac-input").live('blur', function(){
		var idform = $(this).attr('name').replace("ac_input_",""); 
		if ($("li[id='activity-"+idform+"'] input[type=submit]").attr('disabled')){
			$("li[id='activity-"+idform+"'] input[type=submit]").removeAttr('disabled')
			$("li[id='activity-"+idform+"'] div[id='message']").slideUp(function (){$('#message').remove()});
		}
		//$('.ac-form').fadeIn();
		//$('.ac-input').animate({height:'16px'});		
		if ($(this).val() == '') {
			$(this).animate({height:'16px'});
			$(this).parent().parent().animate({height:'27px'});
			$("li[id='activity-"+idform+"'] input[type=submit]").removeAttr('disabled')
			$("li[id='activity-"+idform+"'] div[id='message']").slideUp(function (){$('#message').remove()});
		}
	});

	// Post Form (lostfocus)
	$("#whats-new").live('keyup', function(e){
		if (e.keyCode == 27) {
            colapseWhatsNew($);
            $('#activity-privacity').val('publico');
            $(".new-update").slideUp(function (){$('.new-update').remove()});
            $('.iron-activity-share-text .pop-status-content').slideUp('slow');
		}
	});
	
	// Post Form (focus)
	$("#whats-new").live('focus', function(){
		var $self = $(this);
		if (!$('#iron-share-div').hasClass('post-bar-open')) { 
			$('#iron-share-div').fadeOut('fast', function(){
				$self.css({height:'88px',width:'316px'});
				$('#activity-privacity').show();
				$('#share-options-priv').parent().show();
				$(this).css({height:'112px'})
					.removeClass('post-bar-close')
					.addClass('post-bar-open')
					.fadeIn();
			});
		}
		/*$('#iron-share-div').animate({height:'112px'});
		$('#iron-share-div').addClass('post-bar-open');

		$(this).animate({height:'88px',width:'316px'},300, function(){
			// Esto se ejecuta al terminar Animate !
			$('#activity-privacity').show();
			$('#share-options-priv').parent().show();
			
			$('#iron-share-div').removeClass('post-bar-close');		
		});*/	
	});		

	// Post Form (submit)
	$("#uploadform").live('submit', function() {
		//$("#upload-loading").show().fadeOut(1500);
		$("#activity-imagen").val($("#myfile").val());
		$("#upload-loading").hide();
	});

	// Post - Texto
	$("#share-options-text").live('click', function() {
		$("#add_data").hide();
		$("#activity-enlace").val('http://');
		$("#activity-imagen").val('');
		$("#activity-type").val('text');
		$("#dinamic_upload").hide();
	});
	
	
	$("#activity-enlace").live('change', function() {
	       
            $(this).val( 
            $(this).val()
               .replace(/http:\/\/http:\/\//, 'http:\/\/')
               .replace(/http:\/\/https:\/\//, 'https:\/\/')
            );
    });

	// Post - Imagen
	$("#share-options-image").live('click', function() {
		$("#dinamic_upload").slideDown();
		$("#activity-type").val('image');
	});
	
	// Post - Enlace
	$("#share-options-link").live('click', function() {
		$("#add_data").slideDown();
		$("#activity-type").val('link');
	});	
	
	$("#activity-enlace-cancel").click(function () {
		$("#add_data").slideUp();
		$("#activity-enlace").val('http://');
	});
	
	$("#imageFileuploader-cancel").click(function () {
		$("#dinamic_upload").slideUp();
		$("#activity-imagen").val('');
		$(".qq-upload-success").remove()
	});

    //function createUploader
	if(typeof(qq) == "object" && $("#imageFileuploader").length > 0) {
		/* lang upload file*/
		var nombreCookie = 'LANG';
		var x=0, band=0;
		var cadena=document.cookie;
		var cookies=new Array(4); var cookies2=new Array(2);
		cookies=cadena.split("; ");
		while(cookies[x] && band!=1)
		{
			cadena=cookies[x]; cookies2=cadena.split("=");
			if(cookies2[0]==nombreCookie) band=1;
			else x++;
		}
		
		if ( cookies2[1]  == 'es' ){
			var mesagge = 'Arrastre aquí su imagen';
		}
		if ( cookies2[1]  == 'eu' ){
			var mesagge = 'Zure irudia hona ekarri';
		}
		if ( cookies2[1]  == 'en' ){
			var mesagge = 'Drop files here to upload';
		}
		/* lang upload file*/

	    var uploader = new qq.FileUploader({
	        element: document.getElementById('imageFileuploader'),
	        action: $("base").attr("href") + "/wp-content/themes/cvp-irontec/_inc/muro/php.php",
	        multiple : false,
	        allowedExtensions : ["gif","png","jpeg","jpg"],
	        template : '<div class="qq-uploader">' + 
				        '<div class="qq-upload-drop-area"><span>' + mesagge + '</span></div>' +
				        '<div class="qq-upload-button"><span style="z-index:1000;">'+ $("#imageFileuploader").text() +'</span></div>' +
				        '<ul class="qq-upload-list"></ul>' + 
				       '</div>',
	        onComplete : function (id, fileName, responseJSON) {

	    		$("li.qq-upload-fail").remove();

	    		if(typeof(responseJSON.success) == "boolean") {
	
	    			if(responseJSON.success == true) {
	
	    				var lis = $("li.qq-upload-success");
	    				if(lis.length > 1) {
	
	    					lis.filter(":first").remove();
	    				}
	
	    				var uploadedImage = $("li.qq-upload-success:first");
	    				uploadedImage.html("");
	    				var imgPath = $("base").attr("href") + "/wp-content/uploads/activity" + responseJSON.filename.replace("\/","/");
	    				uploadedImage.append("<img src='"+imgPath+"' />");
	
	    				var imgName = imgPath.split("/");
	    				$("#activity-imagen").attr("value", imgName[imgName.length - 1].replace("thumb_",""));
	    			}
	    		}
	    	},
	        debug: false
	    });		
	}
	
	
	$('#content-middle').hover(function(){
	    
	    if ( $('#content-middle div').filter(':eq(1)').attr('id') != "members-dir-search" ){
	        
	        $('a').each(function() {
	            var $this = $(this),href = $this.attr('href');
	            
	            if (typeof $this.attr('href') != "undefined") {
	                
	                if ($this.attr('href').indexOf('www.kronikoensarea.net') == '-1') {
	                    $(this).attr( 'target','_blank' );
	                }
	                
	            }
	            
	        });
	    }
	    
	});
	
	$('#activity-privacity').change(function(){
		
		switch ($(this).val()) {
		case 'amigos':
			$('.iron-activity-share-text .pop-status-content').slideDown('slow')
		break;
		case 'publico':
			$('.iron-activity-share-text .pop-status-content').slideUp('slow')
		break;
		default:
			$('.iron-activity-share-text .pop-status-content').slideDow('slow')
		break;
		}
	});
	
	$('button.close').on('click', function(){
		$('.iron-activity-share-text .pop-status-content').slideUp('slow');
	});
	
	/**
	 * Comentarios de preguntas a todos y testimonios
	 */
	jQuery("form#commentform").submit(function() {
		
		if (jQuery("form#commentform textarea").val() == "" ) {
			return false;
		} else {
			return true;
		}
		
	});
	
});


function colapseWhatsNew ($, fn) {
	
	var fn = fn;
	var $self = $('#whats-new');
	$('#iron-share-div').slideUp('fast', function(){
		$self.css({height:'17px',width:'185px'});
		$('#activity-privacity').hide();
		$('#share-options-priv').parent().hide();
		$(this).css({height:'30px'})
			.addClass('post-bar-close')
			.removeClass('post-bar-open')
			.fadeIn('fast', function(){
				if (typeof fn == 'function')
					fn();
			});
	});
	
	
	/* $('#iron-share-div').animate({height:'30px'},200);					
	$('#iron-share-div').addClass('post-bar-close');
	$('#activity-privacity').hide();
	$('#share-options-priv').parent().hide();

	$("#add_data").hide();
	$("#dinamic_upload").hide();
	$("#activity-imagen").val('');
	$("#activity-enlace").val('');

	$('#whats-new').animate({height:'17px',width:'185px'},400, function(){
		// Esto se ejecuta al terminar Animate !
		$('#iron-share-div').removeClass('post-bar-open');				
	});*/
}
