$.fn.equalCols = function(){
	//Array Sorter
	var sortNumber = function(a,b){return b - a;};
	var heights = [];
	//Push each height into an array
	$(this).each(function(){
		heights.push($(this).height());
	});
	heights.sort(sortNumber);
	var maxHeight = heights[0];
	return this.each(function(){
		//Set each column to the max height
		$(this).css({'height': maxHeight});
	});
};

function carousel_config(carousel) {
    $('#promo .nastepne').bind('click', function() {
        carousel.next();
        return false;
    });

    $('#promo .poprzednie').bind('click', function() {
        carousel.prev();
        return false;
    });
};

$(function($){
    $("#scrolling-content").jcarousel({
        scroll: 1,
        initCallback: carousel_config,
		wrap: 'both',
		auto: '6',
        // This tells jCarousel NOT to autobuild prev/next buttons
        buttonNextHTML: null,
        buttonPrevHTML: null
    });

	//Select the columns that need to be equal e.g
	if($('div.col').size() != 0) {
        $('div.col').equalCols();
	} else if($('div.newses').size() != 0) {
        $('div.newses, div.newses-sidebar').equalCols();
	}

	// Enable hover fading on brands in footer
    $("#footer .brands img").hover( function() {
        var hoverImg = HoverImgOf($(this).attr("src"));
        $(this).attr("src", hoverImg);
    }, function() {
        var normalImg = NormalImgOf($(this).attr("src"));
        $(this).attr("src", normalImg);
    });
    function HoverImgOf(filename) {
       var re = new RegExp("(.+)\\.(gif|png|jpg)", "g");
       return filename.replace(re, "$1_white.$2");
    }
    function NormalImgOf(filename) {
       var re = new RegExp("(.+)_white\\.(gif|png|jpg)", "g");
       return filename.replace(re, "$1.$2");
    }

    // Google maps
    if($('#google-map-address').size() != 0) {
        if (GBrowserIsCompatible()) {

          function createMarker(point,html) {
            var marker = new GMarker(point);
            GEvent.addListener(marker, "click", function() {
              marker.openInfoWindowHtml(html);
            });
            return marker;
          }

          // Display the map, with some controls and set the initial location
          var map = new GMap2(document.getElementById("google-map-address"));
          map.addControl(new GLargeMapControl());
          map.addControl(new GMapTypeControl());
          map.setCenter(new GLatLng(52.2292063,20.9704632),12);

          // Set up three markers with info windows

          var point = new GLatLng(52.2292063,20.9704632);
          var marker = createMarker(point,'ul. Kasprzaka 22<br>01-211 Warszawa')
          map.addOverlay(marker);
        } else {
          alert("Twoja przegladarka nie obsluguje Google Map.");
        }
    }

	$('div.col-pictures .pics a').lightBox();


});

