
// Creating custom :external selector
jq.expr[':'].external = function(obj){
    return !obj.href.match(/^mailto\:/)
           && !obj.href.match(/^javascript\:/)
           && ((obj.hostname != location.hostname)
        	   || obj.href.match(/RSS$/)
			   || obj.href.match(/\.pdf$/));
};

jQuery.fn.autolink = function () {
	return this.each( function(){
		var re = /((http|https|ftp):\/\/[\w?=&.\/-;#~%-]+(?![\w\s?&.\/;#~%"=-]*>))/g;
		jq(this).html(jq(this).html().replace(re, '<a href="$1">$1</a>'));
	});
};

jQuery.fn.mailto = function () {
	return this.each( function() {
		var re = /(([a-z0-9*._+]){1,}\@(([a-z0-9]+[-]?){1,}[a-z0-9]+\.){1,}([a-z]{2,4}|museum)(?![\w\s?&.\/;#~%"=-]*>))/g
		jq(this).html(jq(this).html().replace( re, '<a href="mailto:$1";>$1</a>'));
	});
};

jq(document).ready(function() {
	// Add 'external' CSS class to all external links and target _blank
	jq('a:external').addClass('external');
	jq('a:external').attr('target', '_blank');

	//este es el parche para mostrar links a noticias dentro del slider
	jq("body.section-inicio div.slide-news-content h4").each( function(i, elem){

	  jq(this).css('cursor', 'pointer');

	  jq(this).click(function() {
	    window.location = portal_url + "/la-secretaria/prensa/noticias";
	  });

	} );

    jq("body.section-inicio div.row_1 div.block_2 h2:first-of-type").each( function(i, elem){
        jq(this).css('cursor', 'pointer');
        jq(this).click(function() {
            window.location = portal_url + "/especiales";
        });
    });

	jq("body.section-inicio div.row_2 h3").each( function(i, elem){
	  jq(this).css('cursor', 'pointer');
	  jq(this).click(function() {
	    window.location = portal_url + "/agenda";
	  });
	});

	jq("body.section-inicio div.row_3 div.block_1 h3").each( function(i, elem){

		  jq(this).css('cursor', 'pointer');

		  jq(this).click(function() {
		    window.location = portal_url + "/concursos-y-convocatorias";
		  });

		} );

	jq("body.section-inicio div.row_3 div.block_2 h3").each( function(i, elem){
        jq(this).css('cursor', 'pointer');
        jq(this).click(function() {
            window.location = portal_url + "/cursos-y-talleres";
        });
    });

	// Activando ceebox
    jq(".ceebox").ceebox({
         titles: false,
         videoGallery: false
    });

    jq('.commentActions .reportar a').prepOverlay({
        subtype:'ajax',
        urlmatch:'$', urlreplace:' #portal-column-content>*',
        formselector:'form',
        closeselector:'[name=form.buttons.Cancelar]',
        closeOnClick: false
    });

	jq("#form-buttons-Cancelar").click(function(e) {
		triggers.eq(1).overlay().close();
		return e.preventDefault();
	});

	jq(function(){
	    // jq("select:not([multiple='multiple'])").selectmenu();
		jq("select")
			.not(jq("#selected_categories_categories"))
			.not(jq("#subject_unselected"))
			.not(jq("#subject_selected"))
			.not(jq("form[action='criterion_edit_form'] select"))
			.not(jq("select[multiple='multiple']"))
			.not(jq("#fieldset-dates select"))
            .not(jq("#coleccion_selected"))
            .not(jq("#coleccion_unselected"))
			.selectmenu();
	});

	jq("ul.event-owner-menu").tabs();

	if (jq("body.print").length > 0) {
		window.print();
		window.back();
	};

    // campos requeridos que no podemos manejar por AT
    jq("#disco-base-edit #archetypes-fieldname-squareImage .formQuestion").after('<span title="Obligatorio" class="fieldRequired">(Obligatorio)</span>')
    jq("#disco-base-edit #archetypes-fieldname-relatedInternalSquareImage .formQuestion").after('<span title="Obligatorio" class="fieldRequired">(Obligatorio)</span>')
    jq("#disco-base-edit #archetypes-fieldname-relatedExternalSquareImage .formQuestion").after('<span title="Obligatorio" class="fieldRequired">(Obligatorio)</span>')

    jq("#libro-base-edit #archetypes-fieldname-image .formQuestion").after('<span title="Obligatorio" class="fieldRequired">(Obligatorio)</span>')
    jq("#libro-base-edit #archetypes-fieldname-relatedInternalImage .formQuestion").after('<span title="Obligatorio" class="fieldRequired">(Obligatorio)</span>')
    jq("#libro-base-edit #archetypes-fieldname-relatedExternalImage .formQuestion").after('<span title="Obligatorio" class="fieldRequired">(Obligatorio)</span>')

	// username por ajax
//	jq("#user-name span").load(portal_url + "/@@muni.cultura.username", function() {
//		jq("#user-name").show();
//	});
    
    jq.getJSON(portal_url + "/@@muni.cultura.username?callback=?", function(data) {
	    	jq("#user-name span").text(data);
	    	jq("#user-name").show();
    });    
});

function goToUrl(box) {
    url = box.options[box.selectedIndex].value;

    if (url.lastIndexOf(';') != -1) {           //  uso el ';' para separar la url de las opciones
        opciones = url.substring( url.lastIndexOf(";") + 1 );
        url = url.substring( 0, url.lastIndexOf(";") );
        window.open ( url , opciones );

    } else if (url != 'null') {                 //  si la opcion es  null no tengo que ir a ningun lado
        window.location = url;
    }

    return true;
}

