jq(document).ready(function() {

    //portletFormatedStatic line wrapper
    function text_wrapper($this){
        var lh = $this.css('line-height').replace('px', '');
        var h = $this.height();
        var t = false;
        if (h/lh > 3 ){
            t = true;
        }
        return t;
    }
    jq('.portletFormatedStatic .portletItem .formated-static-description').each(function(){
        var text = jq(this).text().split(' ');
        var not_correct_height = true;
        while (not_correct_height) {
            not_correct_height = text_wrapper(jq(this));
            if (not_correct_height) {
                jq(this).text(text.slice(0,-2).join(' ')+'...');
                text = jq(this).text().split(' ');
            }
        }
    });

    //navbar
    jq("#portal-globalnav li:last").addClass('last-item');

    jq('#block-navigation .block').each(function(){
        jq(this).click(function(){
            window.location.href = jq('h3 a',jq(this)).attr('href');
        });
        jq(this).hover(
            function () {
                jq(this).addClass("fhover");
            },
            function () {
                jq(this).removeClass("fhover");
            }
        );
    });
    
    //workflow action enhacement
    bg_color = jq('#plone-contentmenu-workflow span[class|="state"]').css('background-color');
    jq('#plone-contentmenu-workflow').css('background-color', bg_color);
    jq('#plone-contentmenu-workflow .actionMenuHeader a').css('background-color', bg_color);



    //Custom exclude for portlets  in edit mode
    
    //exclude all innecesary portlets
    jq('option[value$="portlets.Classic"]').remove();
    
    //HOME
    var rotating_banner = 'allianz.portlet.banner.PortletBanner';
    var simple_banner = 'allianz.portlet.simplebanner.SimpleBanner';
    var home_layout = jq('.portletpage-home-page-edit');
    var internal_layout = jq('.portletpage-internal-page-edit');    
    //remove all the banner item portlets except the one in the toprow slot
    jq('option[value$=' + rotating_banner + ']').not('.portletpage-home-page-edit #portletmanager-collective-portletpage-toprow option').remove();
    if (home_layout[0] !== undefined) {
        //in the top slot, remove all options except the banner item portlet
        jq('.portletpage-home-page-edit #portletmanager-collective-portletpage-toprow option:not([value$=' + rotating_banner + '], [value="/@@portlets"])').remove();
    }
    //INTERNAL PAGES
    //remove all the simple banner item portlets except the one in the toprow slot
    var internal_exclude = jq.merge(jq('.portletpage-internal-page-edit option[value$=' + simple_banner + ']'),
                                    jq('.portletpage-home-page-edit #portletPageBottomRow option[value$=' + simple_banner + ']'));
    jq('option[value$=' + simple_banner + ']').not(internal_exclude).remove();   
    if (internal_layout[0] !== undefined) {
        //in the top slot, remove all options except the simple banner item portlet
        jq('.portletpage-internal-page-edit #portletmanager-collective-portletpage-toprow option').not('[value$=' + simple_banner + '], [value="/@@portlets"]').remove();
    }
    
    //HOME AND INTERNAL PAGES
    function oc(a) {
        var o = {};
        for(var i=0; i<a.length; i++) {
            o[a[i]]='';
        }
        return o;
    }    
    var portletpage_excluded = ['portlets.Calendar', 'portlets.Login', 'portlets.Navigation','allianz.portlet.allianznet.AllianzNetPortlet', 'portlets.Review', 'collective.portlet.sharethis.SocialLinks', 'allianz.portlet.ustedsabia.UstedSabiaPortlet', 'allianz.portlet.video.VideoPortlet', 'portlets.Search', 'portlets.rss', 'allianz.portlet.productosrelacionados.ProductosRelacionados', 'portlets.Events', 'portlets.News', 'portlets.Recent' ];
    var l = portletpage_excluded.length;
    jq('#portletPageColumns option').each(function(){
        i = 0;
        for (i = 0; i <= l; i++ ) {
            var cmp_text = portletpage_excluded[i]? portletpage_excluded[i] : null;
            if (jq(this).attr('value').match(cmp_text) !== null ) {
                jq(this).remove();
                continue;
            }
        }
    });


    //GENERAL MODIFICATIONS
    //list of links image change
    jq("#portal-column-content li a[class$=ernal-link]").parents('li').css('list-style-image', 'url("++resource++allianz.theme.images/list-arrow-link.png")');
    
    
    //DOWNLOAD ITEMS 
    jq(".download-item").hover(
        function(){
            $(this).addClass('hover');
        },
        function() {
            $(this).removeClass('hover');
        }
    )
    jq(".download-item").click(function(){
        window.location = $(this).find('a').attr('href');
    })
        
});

