﻿jQuery.noConflict();
jQuery(document).ready(function() {
    /* showes the popup on img hover */
    jQuery('ul.mainNavigation li').bind("mouseenter", function() {
        jQuery(this).children('span').addClass('hover');
    }).bind("mouseleave", function() {
        jQuery(this).children('span').removeClass('hover');
    });

    //Link Arrow    
    jQuery('.readmore').arrowBefore();
    jQuery('.paging-buttons .prev').pagingArrowBefore();
    jQuery('.paging-buttons .next').pagingArrowAfter();

    //Empty searchfield
    jQuery('#quickSearch .textfield').click(function() {
        jQuery(this).val("");
    });

    //Tabel rows
    jQuery('table tr:even').addClass('even');

    //startpage leftarea portlets
    jQuery('.startPage #leftContentPortlets .x-portlet').addClass('noBottom');
    jQuery('.startPage #leftContentPortlets .last-portlet').removeClass('noBottom');

    //Put class on first "normal" news
    if (jQuery('.startPage .newslist div:first').hasClass('normal')) {
        jQuery('.startPage .newslist div:first').addClass('firstNormal');
    }

    jQuery('ul.headerLinks li:first').addClass('first');

    //Quick search clearer
    var searchText = jQuery('.quickSearch input.textfield').val().length;
    if (searchText == "0") {
        jQuery('.quickSearch .textfield').val("Sök");
    }
    jQuery('.quickSearch input.textfield').focus(function() {
        var searchOrd = jQuery('.quickSearch input.textfield').val();
        if (searchOrd == "Sök") {
            jQuery('.quickSearch input.textfield').val("");
        }
    });

    /* coworker list */
    // make list-portlet, cases tag-cloud portlet - support clickables divs
//    jQuery(".coworker-portlet .memory-item")
//        .has("a")
//        .hover(
//            function() {
//            jQuery(this)
//                    .css("cursor", "pointer")
//                    .addClass("hover");
//            },
//            function() {
//            jQuery(this).removeClass("hover");
//            }
//        )
//        .click(
//            function() {

//            document.location.href = jQuery(this).find("a:first").attr("href");
//            }
//        );
            if (!(jQuery.browser.msie) || (jQuery.browser.msie && jQuery.browser.version > 5.5)) {

                jQuery(".memory-portlet").memoryShow();
        }
});

jQuery.fn.arrowBefore = function(){
    if(this.size() > 0) {
        this.each(function(){
            jQuery(this).prepend("<span class='arrow'>&rsaquo;&nbsp;</span>");
        });
    }
};
jQuery.fn.arrowAfter = function(){
    if(this.size() > 0) {
        this.each(function(){
            jQuery(this).append("<span class='arrow'>&nbsp;&lsaquo;</span>");
        });
    }
};

jQuery.fn.pagingArrowBefore = function(){
    if(this.size() > 0) {
        this.each(function(){
            jQuery(this).prepend("<span class='arrow'>&lsaquo;&nbsp;</span>");
        });
    }
};
jQuery.fn.pagingArrowAfter = function(){
    if(this.size() > 0) {
        this.each(function(){
            jQuery(this).append("<span class='arrow'>&nbsp;&rsaquo;</span>");
        });
    }
};

jQuery.fn.memoryShow = function() {
    var cont = this;
    var items = this.find(".memory-item");
    var itemsCnt = items.length - 1;

    items.each(function(i) {

        var first = jQuery(this).find(".first");
        var second = jQuery(this).find(".second");
        var itemDesc = jQuery(this).find(".item-desc");
        var itemHeading = jQuery(this).find("h3");
        var itemLink = jQuery(this).find("a").not(jQuery("p a"));

        var isCases = jQuery(this).closest(".cases").size() > 0;

        if (itemsCnt == i) {
            cont.addClass("done");
        }

        if (first.attr("src") == undefined && second.attr("src") == undefined) {
            itemDesc.css("width", "100%");
        }

        if (first.size() > 0) {
            if (second.size() > 0) {
                if (!isCases)
                    jQuery(this).css("cursor", "pointer");

                jQuery(this).hover(function() {
                    first.fadeOut(100, function() {
                        if (!first.is(":visible:animated"))
                            second.fadeIn(100)
                    });
                },
				function() {
				    second.fadeOut(100, function() {
				        if (!second.is(":visible:animated"))
				            first.fadeIn(100);

				        // dont hide if page is 'cases'
				        if (!isCases) {
				            itemDesc.hide();
				            itemHeading.hide()
				            itemLink.hide();
				        }
				    });
				});

                if (!isCases) {
                    jQuery(this).click(function() {
                        second.hide();
                        itemDesc.show();
                        itemHeading.show();
                        itemLink.show();
                    });
                }

            }
            else /*if (itemHeading.size() > 0)*/ {
                //jQuery(this).css("cursor","default");
                jQuery(this).hover(function() {
                    first.hide();
                    itemDesc.addClass("hover");
                    itemDesc.show();
                    itemHeading.show();
                    itemLink.show();

                },
				function() {
				    first.show();
				    itemDesc.removeClass("hover");
				    itemDesc.hide();
				    itemHeading.hide()
				    itemLink.hide();
				});
            }
        }
    });
}

