$(document).ready(function() {
    /* PRIMERAS DOS PALABRAS
     $('.item p:first').each(function(){
         var mystring = $(this).html(); 
         var firstword = mystring.replace(/(^\w\S+ \w\S+)/,'<span class="pCap">$1</span>');
         $(this).html(firstword);
     };)*/
     
    //* FANCYBOX
    $('.column2 a img').each(function (index, that){
        //$(that).parent('a').rel.val() = $(that).parent('a').parent('p').parent('div').prev('div').children('h2').val();
        if(!$(that).parent('a').attr('rel')){
            $(that).parent('a').attr('rel', $(that).parent('a').parent('p').children('span.h').html());
        }
    });

    $('.categorias .column2 a img').each(function (index, that){
        //$(that).parent('a').rel.val() = $(that).parent('a').parent('p').parent('div').prev('div').children('h2').val();
        $(that).parent('a').attr('rel', $(that).parent('a').parent('p').parent('div').prev('div.column1').children('h2').html());
    });
    
    $(".column2 a img").parent('a').fancybox({
        'zoomSpeedIn': 500,
        'zoomSpeedOut': 400,
        'overlayOpacity': 0.8
    });

    //* Plegar /desplegar subMenues
    $('ul#menu li ul').hide();
    //    $('ul#menu li a').click(function(e){
    //        $(this).parent('li').children('ul').toggle('fast');
    //        //e.preventDefault();
    //    });

    //* Plegar /desplegar calendarios
    $('ol.calendario').hide();
    $('a.cal').click(function(e){
        $('ol.calendario').hide('fast');
        $('ol.'+$(this).children('span').html()).toggle("fast");
        e.preventDefault();
    });
    
$(".links .fold").parent('li').children('ul').hide();
$(".links .fold").parent('li:first').children('ul').show();
$('.links a.fold').click(function(e){
        var list = $(this).parent('li').children('ul');
        list.toggle('fast');
        e.preventDefault();
    });

    //*area de click mas grande


    $("div#newsWrap [class^='novedad'] p a").bigTarget();
    $(".novHist a.leermas").bigTarget();


});

// bigTarget.js - A jQuery Plugin
// Version 1.0.1
// Written by Leevi Graham - Technical Director - Newism Web Design & Development
// http://newism.com.au
// Notes: Tooltip code from fitted.js - http://www.trovster.com/lab/plugins/fitted/

// create closure
(function($) {
    // plugin definition
    $.fn.bigTarget = function(options) {
        debug(this);
        // build main options before element iteration
        var opts = $.extend({}, $.fn.bigTarget.defaults, options);
        // iterate and reformat each matched element
        return this.each(function() {
            // set the anchor attributes
            var $a = $(this);
            var href = $a.attr('href');
            var title = $a.attr('title');
            // build element specific options
            var o = $.meta ? $.extend({}, opts, $a.data()) : opts;
            // update element styles
            $a.parents(o.clickZone)
            .hover(function() {
                $h = $(this);
                $h.addClass(o.hoverClass);
                if(typeof o.title != 'undefined' && o.title === true && title != '') {
                    $h.attr('title',title);
                }
            }, function() {
                $h = $(this);
                $h.removeClass(o.hoverClass);
                if(typeof o.title != 'undefined' && o.title === true && title != '') {
                    $h.removeAttr('title');
                }
            })
            // click
            .click(function() {
                if(getSelectedText() == "")
                {
                    if($a.is('[rel*=external]')){
                        window.open(href);
                        return false;
                    }
                    else {
                        //$a.click(); $a.trigger('click');
                        window.location = href;
                    }
                }
            });
        });
    };
    // private function for debugging
    function debug($obj) {
        if (window.console && window.console.log)
            window.console.log('bigTarget selection count: ' + $obj.size());
    };
    // get selected text
    function getSelectedText(){
        if(window.getSelection){
            return window.getSelection().toString();
        }
        else if(document.getSelection){
            return document.getSelection();
        }
        else if(document.selection){
            return document.selection.createRange().text;
        }
    };
    // plugin defaults
    $.fn.bigTarget.defaults = {
        hoverClass  : 'over',
        clickZone : 'div:eq(0)',
        title   : true
    };
// end of closure
})(jQuery);

