function fixLists() {
    $('ul[type], ol[type]').each(function(){
        var listType = $(this).attr('type');
        var caseValue = null;

        switch(listType) {
            case 'a':
                caseValue = 'list-lower-alpha';
            break;
            case 'A':
                caseValue = 'list-upper-alpha';
            break;
            case 'i':
                caseValue = 'list-lower-roman';
            break;
            case 'I':
                caseValue = 'list-upper-roman';
            break;
            case 'square':
                caseValue = 'list-square';
            break;
            case 'circle':
                caseValue = 'list-circle';
            break;
        }

        $(this).addClass(caseValue);
        caseValue = null;
    });
}

function smartMenu() {

    $('.smart-menu li:last-child').css('margin-right', 0).addClass('lastChild');

    var ignoreWidth = 0;
    $('.smart-menu li.ignore').each(function(){
        ignoreWidth = ignoreWidth + ($(this).width()+4);
    })
    
    var itemCount = $('.smart-menu li:not(.ignore)').length;
   
    var marginCount = (itemCount-1)*4;
    var itemWidth = Math.floor((952-ignoreWidth-marginCount)/itemCount);
    $('.smart-menu li:not(.ignore) a').css('width', (itemWidth-3));
    $('.smart-menu li:not(.ignore) a span').css('width', (itemWidth-20));
    
    var itemHeight = 0;
    $('.smart-menu li a span').each(function(){
        if($(this).height() > itemHeight) {
            itemHeight = $(this).height();
        }
    });
    
    $('.smart-menu li a span').css('height', itemHeight);

}

function fixMenu() {

    var beWidth = $('.ss li:last-child').width();

    var itemCount = ($('.ss li').length - 1);
    
    var itemWidth = (Math.floor((952-beWidth)/itemCount)+1);

    $('.ss li:last').addClass('lastChild');
    
    $('.ss li').each(function(){
        if ($(this).hasClass('lastChild') && $(this).hasClass('active'))
            $(this).addClass('lastActive');
    });

    $('.ss li:not(.lastChild), .ss li:not(.lastChild) a').css('width', itemWidth);
    
    var itemHeight = 0;
    $('.ss li').each(function(){
        if($(this).height() > itemHeight) {
            itemHeight = $(this).height();
        }
    });

    $('.ss li').css('height', itemHeight);
    $('.ss li a').each(function(){
        if ($(this).parent().hasClass('lastChild')) $(this).css('height', itemHeight-2);
        else $(this).css('height', itemHeight);
    });



     $('.ss li').hover(
        function(){
            if ($(this).hasClass('lastChild'))
                $(this).addClass('lastChild-iehover');
            else
                $(this).addClass('iehover');
        },
        function(){
            if ($(this).hasClass('lastChild'))
                $(this).removeClass('lastChild-iehover');
            else
                $(this).removeClass('iehover');
        }
    );
}

function fixBoxes() {
   
   if($('.old-jsBoxFix')) {
       var noRows = 0;
        
        $('.bph-h-cols > .bph-h-fleft').each(function(index, el){
            $('.bph-box-cc .bph-box2-content', this).each(function(index, el){
                $(this).addClass('bph-row'+(index+1));
                if ((index+1) > noRows) noRows = index+1;
            });
        });
        
        for (var i=1;i<=noRows;i++) {
            
            var boxes = $('.bph-row'+i);
            var itemHeight = 0;

            boxes.each(function(){
                if($(this).height() > itemHeight) {
                    itemHeight = $(this).height();
                }
            });
        
            if ($.browser.webkit) itemHeight = itemHeight + 100;
         
            boxes.css('height', itemHeight + 20 + 'px');
        
            var w = $('.bph-row'+i+' .bph-box-cc-more:first').width();
            
            $('.bph-row'+i+' .bph-box-cc-more').css('position', 'absolute').css('bottom', 0).css('left', 10).css('width', w + 'px');
        }
    }
    
    /*var boxHn = 0;
    var boxHc = 0;

    var columns = 3;
    
    var x = 0;
    var y = 1;
    var z = 1;

    $(".bph-box-cc2 .bph-box2").each(function() {
        $(this).addClass('bph-box2-row'+y);
        z++; if(z>columns) {z = 1; y++}
    });

    for(x=1;x<=columns;x++) {
        $(".bph-box-cc2 .bph-box2.bph-box2-row" + x).each(function() {
            var h4 = $(this).children(".bph-box2-top").children("div").children("h4").height();
            var content = $(this).children(".bph-box2-content").height();
            
            if ($.browser.webkit) content = content + 55;

            if(boxHn==0) boxHn = h4;
            else if(boxHn<h4) {
                boxHn = h4;
                $(".bph-box-cc2 .bph-box2.bph-box2-row" + x).each(function() {
                       var childHeader = $(this).children('div')[0];
                       childHeader = $(childHeader).children('h4')[0];
                       $(childHeader).height(boxHn);
                });

            }
            if(boxHc==0) boxHc = content;
            else if(boxHc<content) {
                boxHc = content;
                $(".bph-box-cc2 .bph-box2.bph-box2-row" + x).each(function() {
                       var childContent = $(this).children('div')[1];
                       $(childContent).height(boxHc);
                });
            }
        });

        $(".bph-box-cc2 .bph-box2.bph-box2-row" + (x) + " h4").height(boxHn);
        $(".bph-box-cc2 .bph-box2.bph-box2-row" + (x) + " .bph-box-content").height(boxHc);
        boxHn = 0;
        boxHc = 0;
    }
*/	
	
	$(".bph-box-sitemap .bph-box2").each(function(){
		
			$(this).width(230);
		
	});
	
	var maxLogBH = 0;
	
	$(".bph-loginbox .bph-box2-content").each(function(){
		if($(this).height() > maxLogBH) {
			maxLogBH = $(this).height();
		}
	});
	
	$(".bph-loginbox .bph-box2-content").each(function(){
		$(this).height(maxLogBH);
	});
  
}

this.tooltip = function(){	
	/* CONFIG */		
		xOffset = 10;
		yOffset = 20;		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result		
	/* END CONFIG */		
	$(".tooltip").hover(function(e){											  
		this.t = this.title;
		this.title = "";									  
		$("body").append("<p id='tooltip'>"+ this.t +"</p>");
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.css("z-index",10)
			.fadeIn("fast");		
    },
	function(){
		this.title = this.t;		
		$("#tooltip").remove();
    });	
	$(".tooltip").mousemove(function(e){
		$("#tooltip")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};


var font = 0;

function toggleFontSize(size) {
    
    $('body').removeClass('font-size1');
    $('body').removeClass('font-size2');
    $('body').removeClass('font-size3');
    
    font = font + 1;
    if ( font == 4 ) {
        font = 0;
    } else {
        $('body').addClass('font-size' + font);
    }
    
    return false;

}

function bphinit() {
    
    $('a[rel*=blank]').click( function() {
		window.open(this.href);
		return false;
	});

    
    tooltip();
    
    smartMenu()
    
    $('#bph-font-switcher, .bph-font-switcher').html('<span><a href="#" onclick="return toggleFontSize(\'small\')" style="font-size: 9px" title="mała">A</a> <a href="#" onclick="return toggleFontSize(\'medium\')" title="średnia">A</a> <a href="#" onclick="return toggleFontSize(\'large\')" style="font-size: 12px" title="duża">A</a></span>');
    $('#bph-font-switcher span, .bph-font-switcher span').click(function(){
        toggleFontSize();
    }).hover(function() {$(this).css('cursor','pointer');}, function() {$(this).css('cursor','default');});
    
    $('#bph-carousel').jcarousel({
        scroll: 1,
        start: 1,
        initCallback: bphcarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    
    $('select.styled').customStyle();
    
    /*$('.bph-tabs-nav a').click(function(){
        Cufon.refresh('.bph-tabs-nav li:not([class*=ui-tabs-selected]) a');
        Cufon.refresh('.bph-tabs-nav li.ui-tabs-selected a');
    });*/
    
    $('.bph-list-dl dd').css('display', 'none');
    
    $('.bph-list-dl dt a').click(function(){
        
        if ($(this).parent().parent().hasClass('bph-career')) return;
        
        $(this).toggleClass('bph-selected');
        $(this).parent().next().toggle('fast');
        return false;
    
    });
    
    if ($.datepicker) $(".datepicker-instance input[type=hidden]").datepicker({
        showOn: 'button', 
        buttonImage: '/bphportal/static/img/icon-calendar.gif', 
        buttonImageOnly: true,
        monthNames: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'],
        dayNames: ['Niedziela', 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota'],
        dayNamesMin: ['Nd', 'Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So'],
        onSelect: function(dateText, inst) { 
            
            var select_year = $(this).prev();
            var select_month = select_year.prev();
            var select_day = select_month.prev();
            
            var selected_date = dateText.split('/');
            var selected_month = selected_date[0];
            var selected_day = selected_date[1];
            var selected_year = selected_date[2];

            select_day.val(selected_day);
            select_month.val(selected_month);
            select_year.val(selected_year);
            
            
        }
    });
    
}
function bphinitForTabs() {
    
    $('a[rel*=blank]').click( function() {
		window.open(this.href);
		return false;
	});

    
    tooltip();
    
    smartMenu()
    
    $('#bph-font-switcher, .bph-font-switcher').html('<span><a href="#" onclick="return toggleFontSize(\'small\')" style="font-size: 9px" title="mała">A</a> <a href="#" onclick="return toggleFontSize(\'medium\')" title="średnia">A</a> <a href="#" onclick="return toggleFontSize(\'large\')" style="font-size: 12px" title="duża">A</a></span>');
    $('#bph-font-switcher span, .bph-font-switcher span').click(function(){
        toggleFontSize();
    }).hover(function() {$(this).css('cursor','pointer');}, function() {$(this).css('cursor','default');});
    
    $('#bph-carousel').jcarousel({
        scroll: 1,
        start: 1,
        initCallback: bphcarousel_initCallback,
        buttonNextHTML: null,
        buttonPrevHTML: null
    });
    
      
    /*$('.bph-tabs-nav a').click(function(){
        Cufon.refresh('.bph-tabs-nav li:not([class*=ui-tabs-selected]) a');
        Cufon.refresh('.bph-tabs-nav li.ui-tabs-selected a');
    });*/
    
    $('.bph-list-dl dd').css('display', 'none');
    
    $('.bph-list-dl dt a').click(function(){
        
        if ($(this).parent().parent().hasClass('bph-career')) return;
        
        $(this).toggleClass('bph-selected');
        $(this).parent().next().toggle('fast');
        return false;
    
    });
    
    if ($.datepicker) $(".datepicker-instance input[type=hidden]").datepicker({
        showOn: 'button', 
        buttonImage: '/bphportal/static/img/icon-calendar.gif', 
        buttonImageOnly: true,
        monthNames: ['Styczeń', 'Luty', 'Marzec', 'Kwiecień', 'Maj', 'Czerwiec', 'Lipiec', 'Sierpień', 'Wrzesień', 'Październik', 'Listopad', 'Grudzień'],
        dayNames: ['Niedziela', 'Poniedziałek', 'Wtorek', 'Środa', 'Czwartek', 'Piątek', 'Sobota'],
        dayNamesMin: ['Nd', 'Pn', 'Wt', 'Śr', 'Cz', 'Pt', 'So'],
        onSelect: function(dateText, inst) { 
            
            var select_year = $(this).prev();
            var select_month = select_year.prev();
            var select_day = select_month.prev();
            
            var selected_date = dateText.split('/');
            var selected_month = selected_date[0];
            var selected_day = selected_date[1];
            var selected_year = selected_date[2];

            select_day.val(selected_day);
            select_month.val(selected_month);
            select_year.val(selected_year);
            
            
        }
    });
    
}
function bphcarousel_initCallback(carousel) {
                        
    var navlinks = $('#bph-carousel-nav a');

    navlinks.each(function(index, el){
        if ((index+1) == carousel.options.start) $(el).addClass('bph-selected');
    });

    navlinks.click(function(){
        navlinks.removeClass('bph-selected');
        $(this).addClass('bph-selected');
        carousel.scroll($.jcarousel.intval($(this).text()));
        return false;
    });
                        
}

function fixColN(ile) {

	 var col = ile;

	 var idr = 1;

	 $(".bph-box2").each(function(index) {
	 $(this).addClass("box-row"+idr);
	 if(((index+1)%col)==0) {$(this).removeClass("m10");idr++;}
	 });

	 var el = $(".bph-box2").size();
	 

	 var dpt = Math.ceil(el/col);

	 var maxH = new Array();
	 
	 while (dpt>0) {
	 var maxHtemp = 0;
	 $(".box-row"+dpt).each(function (index) {
		if($(this).height()>maxHtemp) {
	      maxHtemp=$(this).height();
	  	}
	 });
	 $(".box-row"+dpt).height(maxHtemp);
	 
	 $(".box-row"+dpt).each(function (index) {

	 	$(this).children(".bph-box2-content").height(maxHtemp-$(this).children(".bph-box2-top").height()-$(this).children(".bph-box2-bottom2").height());
	 });
	 maxHtemp=0;
	 dpt--;
	 }
}

function fixColN2(ile) {

	 var col = ile;

	 var idr = 1;

	 $(".bph-box2").each(function(index) {
	 $(this).addClass("box-row"+idr);
	 if(((index+1)%col)==0) {$(this).removeClass("m10");idr++;}
	 });

	 var el = $(".bph-box2").size();
	 

	 var dpt = Math.ceil(el/col);

	 var maxH = new Array();
	 
	 while (dpt>0) {
	 var maxHtemp = 0;
	 $(".box-row"+dpt).each(function (index) {
		if($(this).height()>maxHtemp) {
	      maxHtemp=$(this).height();
	  	}
	 });
	 $(".box-row"+dpt).height(maxHtemp);
	 
	 $(".box-row"+dpt).each(function (index) {

	 	$(this).children(".bph-box2-content").height(maxHtemp-$(this).children(".bph-box2-top").height()-$(this).children(".bph-box2-bottom").height());
	 });
	 maxHtemp=0;
	 dpt--;
	 }
}


$(document).ready(function(){
	
    fixBoxes();
    fixLists();

    if($.browser.msie && 6 == parseInt($.browser.version)) {
        $('div.bph-nav3 ul li a, div.bph-nav3 ol li a').hover(
            function(){
                if($(this).parent().hasClass('bph-ib-link')) {
                    $(this).parent().addClass('hoveriblink');
                } else {
                    $(this).parent().addClass('hover');
                }
            }, 
            
            function(){
                if($(this).parent().hasClass('bph-ib-link')) {
                    $(this).parent().removeClass('hoveriblink');
                } else {
                    $(this).parent().removeClass('hover');
                }
            }
        );
    }
});
