function forceInit(){
	init_lightBox();
	init_thumbnails();
	init_iefix();
	init_works();
	init_carousel();
	init_otherCarousels();
	init_dynamicNavigation();
	init_tabs();
	init_gallery();
	init_baloonTips();
	init_clients();
	init_noHander();
}

var latestClickedLink = null;
var bfCacheLink;

/**
 * INIT
 */
function init_lightBox(){
    $('.lightbox').lightbox({
        fitToScreen:true
    });

}



/**
 * MINIATURKI
 */
 function init_thumbnails(){
     $('.thumbnails li').hover(function(){
        var imageSrc = $(this).find('a').attr('href');
        $(this).parents('.gallery').find('.fullsizeImageHolder img').fadeOut(20, function(){
            $(this).attr('src', '').attr('src', imageSrc);
			var testImg = new Image;
			testImg.src = imageSrc;
			if ( testImg.complete ){
				$(this).fadeIn(300);
			} else {
				$(this).load(function(){
					$(this).fadeIn(300);
				});
			}
        });
    }, function(){
        
	});
}



/**
 * KARUZELA
 */
var caroSelector = '.carousel li';
var caroItems = $(caroSelector).size();
var caroCurrentItem = 0;
var caroInterval;
 function init_carousel(){
    caroSelector = '.carousel li';
    caroItems = $(caroSelector).size();
    caroCurrentItem = 0;

    caroInterval = setInterval( function(){
        nextCarouselItem();
    }, 3000 );

    if ( caroItems == 0 ){
        clearInterval( caroInterval );
    }

    nextCarouselItem();
}
//
function nextCarouselItem(){

	var current = $(caroSelector+':eq('+caroCurrentItem+')');

	var willSwitch = true;

	if ( current.has('img') ){
		var imageTest = new Image();
		imageTest.src = current.find('img').attr('src');
		willSwitch = ( imageTest.complete ) ? true : false;
	}

	if ( !willSwitch ){
		return false;
	}


	if ( (caroCurrentItem + 1) >= caroItems ){
		caroCurrentItem = 0;
	} else {
		caroCurrentItem++;
	}
	var next = $(caroSelector+':eq('+caroCurrentItem+')');

	current.fadeOut(500, function(){

		next.fadeIn(500);

	});

}
//


/**
* IE7 FIX
*/
function init_iefix(){
	if (!$.support.cssFloat) {
		$('#additionalMenu li').each(function(){
			var insideAnchorWidth = parseInt($(this).find('a').css('width').replace('px',''));
			$(this).css({'width':insideAnchorWidth});
		});
	}
}

	
	

/**
* KARUZELA REALIZACJI
*/
var workSlideSpeed;
var workSlideInterval;
var newMargin;
var totalWidth;
var containerWidth;
function init_works(){
	if ( $('#works').size() >= 1 ){
	
		workSlideSpeed = 20;
		workSlideInterval = null;
		newMargin = 0;
		totalWidth = parseInt( $('#worksRail li').size() * $('#worksRail li').css('width').replace('px','') );
		containerWidth = parseInt( $('#works').css('width').replace('px','') );
				
		$.browser.chrome = /chrome/.test(navigator.userAgent.toLowerCase());
		
		if ( $.browser.chrome ){
			workSlideSpeed = 10;
		}
				
		$('.workSlideRight').hover(function(){
			$('.workSlideRight:not(.hover)').addClass('hover');
			workSlideInterval = setInterval(function(){
				newMargin = parseInt( $('#worksRail').css('margin-left').replace('px', '') ) - workSlideSpeed;
				if ( Math.abs(newMargin) >= (totalWidth-containerWidth) ){
					$('.workSlideRight').removeClass('hover');
					return;
				}
				$('#worksRail').css('margin-left', newMargin);
			}, 5);
		}, function(){
			$('.workSlideRight').removeClass('hover');
			clearInterval( workSlideInterval );
		});
		
		$('.workSlideLeft').hover(function(){
			$('.workSlideLeft:not(.hover)').addClass('hover');
			workSlideInterval = setInterval(function(){
				newMargin = parseInt( $('#worksRail').css('margin-left').replace('px', '') ) + workSlideSpeed;
				if ( newMargin >= 0 ){
					$('.workSlideLeft').removeClass('hover');
					return;
				}
				$('#worksRail').css('margin-left', newMargin);
			}, 5);
		}, function(){
			$('.workSlideLeft').removeClass('hover');
			clearInterval( workSlideInterval );
		});
		
		$('#worksRail li img').hover(function(){
			$(this).fadeTo(100, 1);
		}, function(){
			if ( !$(this).parents('li').hasClass('focus') ){
				$(this).fadeTo(150, 0.3);
			}
		});

	}
}



/**
 * CAROUSELS
 */
 function init_otherCarousels(){

	$('#homeSlider li').each(function(){
        $('.dynamicDots').append( $('<li></li>') );
    });
	
    $('.jcarousel li:last').css({
        'border-right':'0px'
    });

    // FOLIO ZE STRONY GLOWNEJ
    $('#homeSlider .jcarousel').jcarousel({
        scroll:1,
        initCallback:externalControll,
        itemLoadCallback:dotChange,
		wrap:'both'
    });
	
	if ( $('#homeSlider').size() == 1 ){
		var homeCarousel = $('#homeSlider'); 
		var autoScrollInterval;
		autoScrollInterval = setInterval(function(){
			homeCarousel.find('.jcarousel-next').trigger('click');
		}, 5500);
		homeCarousel.hover(function(){
			clearInterval(autoScrollInterval);
		}, function(){
			autoScrollInterval = setInterval(function(){
				homeCarousel.find('.jcarousel-next').trigger('click');
			}, 5500);
		});
	}

    // ARTYKULY NA STRONIE GLOWNEJ
    $('#articleSlider .jcarousel').jcarousel({
        scroll:1,
		wrap:'both'
    });


    // PRACE W FOLIO
    var isWorkChanging = false;

    /*$('#works .jcarousel').jcarousel({
        itemLoadCallback:workChange,
        initCallback:workCarouselInit,
        scroll:1
    });

    function workCarouselInit(carousel, state){
        workListCarousel = carousel;
    }

    function workChange(carousel, state){
        workListCarousel = carousel;
    }*/
	
    $('#works .jcarousel li:first').addClass('focus');
    $('.work:first').show(0, function(){
		$(this).find('.thumbnails').jcarousel();
	});
	
    $('#works .jcarousel li').live('click', function(){
        if ( $(this).hasClass('focus') ){
            return false;
        }
        if ( isWorkChanging ){
            return false;
        }

        isWorkChanging = true;
        $('#works .jcarousel li.focus img').fadeTo(200, 0.3);
        $('#works .jcarousel li').removeClass('focus');
        $(this).addClass('focus').find('img').fadeTo(400, 1);
		
		var numberOf = $(this).index('#works .jcarousel li');

        var itemToShow = $('#workList li.work:eq('+numberOf+')');

        $('#workList li.work:visible').fadeOut(200, function(){
            itemToShow.fadeIn(300, function(){
                isWorkChanging = false;
				itemToShow.find('.thumbnails').jcarousel();
            });
        });

    });
 }
//
function externalControll(carousel) {
	$('.dynamicDots li').bind('click', function() {
		if ( $(this).hasClass('active') ){
			return false;
		}
		carousel.scroll( $(this).index('.dynamicDots li')+1 );
		return false;
	});
}
//
function dotChange( carousel, state ){
	$('.dynamicDots li').removeClass('active');
	$('.dynamicDots li:eq('+(carousel.first-1)+')').addClass('active');
}
//



/**
 * BEMA style links
 */
var absolutes = '.dotsHolder';
var allowCheat = false;

function init_dynamicNavigation(){
	
	$('.localLink').each(function(){
		if ( $(this).is('a') && !allowCheat ){
			return;
		}
		$(this).find('a').bind('click', function(){
			allowCheat = true;
			$(this).parents('.localLink').trigger('click');
			return false;
		});
	});
	
    $('.localLink').click(function(){
	
		var linkTo = null;
		var hasLink = true;
	
		if ( $(this).is('a') ){
			linkTo = $(this).attr('href');
		} else {
			if ( $(this).is(':has(a[href])') ){
				linkTo = $(this).find('a').attr('href');
			} else {
				hasLink = false;
			}
		}
		
		if ( !hasLink ){
			return false;
		}
		
		latestClickedLink = linkTo;
		
		$(window).unload(function() {
			bfCacheLink = latestClickedLink;
		});
		
        // $(absolutes).fadeOut(200);
        $('.insideBox').slideUp(900, function(){
            window.location.href = linkTo;
			bfCacheLink = linkTo;
        });
        $('#city').fadeOut(400);
        return false;
    });
}



/**
 * TABSY
 */
 var tabChanging = false;
 function init_tabs(){
    if ( $('.tabMenu li[title]').size() > 0 ){

        $('.tabOptionName').text( $('.tabMenu li[title]:eq(0)').attr('title') );
        $('.tabContents li:eq(0)').css({'height':'auto'}).show(0);

    }

    var tempTitle = '';
    $('.tabMenu li').hover(function(){
        if ( $(this).hasClass('active') ){
            return false;
        }
        tempTitle = $('.tabOptionName').text();
        $('.tabOptionName').text( $(this).attr('title') );
    }, function(){
        $('.tabOptionName').text( tempTitle );
    });

    $('.tabMenu li').click(function(){
        if ( $(this).hasClass('active') ){
            return false;
        }
        if ( tabChanging ){
            return false;
        }

        tabChanging = true;

        $('.tabMenu li.active').removeClass('active');

        $(this).addClass('active');

        tempTitle = $(this).attr('title');
        $('.tabOptionName').text( tempTitle );

        var elementToShow = $('.tabContents li.'+$(this).attr('id'));

        $('.tabContents li.tab:visible').fadeOut(300, function(){

            elementToShow.css({'height':'auto'}).fadeIn(500, function(){

                tabChanging = false;

            });

        })

    });
 }


/**
* GALERIA
*/
function init_gallery(){
	if ( $('.galleryTab').size() >= 1 ){
		$('#galleryCarousel').jcarousel();
	}
}


/**
* FUNKCJONALNOSCI
*/
function init_baloonTips(){
	$(".bigDotsList li.item").hoverIntent( function(){
		$(this).find('.baloonBackground, .baloonContent').hide().fadeIn(300);
	}, function(){
		$(this).find('.baloonBackground, .baloonContent').fadeOut(300);
	} );
}



/**
* KLIENCI
*/
function init_clients(){
	$('#clientList li').hover(function(){
		$(this).find('.colored').hide().fadeIn(400);
	}, function(){
		$(this).find('.colored').hide().fadeOut(200);
	});
}


/**
* BEZ LAPKI
*/
function init_noHander(){
	$('.localLink:not(a)').each(function(){
		if ( $(this).is(':not(:has(a[href]))') ){
			$(this).addClass('noHand');
		}
	});
}



/**
* AKCJE
*/
function onload_fadeIn( object ){
	object.fadeIn('slow');
}
function onload_display( object ){
	object.show(0);
}




/**
*
*/
$(document).ready(function(){

	$('body').click(function(){
		window.console && console.log( $('.articlePaginationRail').height() );
	});

	if ( $('.articlePaginationCarousel').size() ){
				
		var activeLiItem = $(this).find('.articlePaginationRail li.active');
		var activeUlItem = activeLiItem.parent().parent();
		var activeUlItemIndex = activeUlItem.index();
								
		var startOffset = activeUlItemIndex * 239;
		startOffset = '-' + startOffset + 'px';
				
		$('.articlePaginationRail').css({
			'left' : startOffset
		});
		
		setTimeout(function(){
			$('.articlePaginationRail').height( $('.articlePaginationRail').height() ).css({'position':'absolute'});
			$('.articlePaginationCarousel').height( $('.articlePaginationRail').height()+15 );
		}, 500);
		
		window.console && console.log( $('.articlePaginationRail').height() );
						
		var carouselItems = $('.articlePaginationCarousel').find('.carouselRailItem').size();
			
		for ( var i = 1; i <= carouselItems; i++ ){
			
			var liItem = $('<li>' + i + '</li>');
		
			$('#articleCarouselPagination').append( liItem );
		
		}
		
		$('#articleCarouselPagination li:first').addClass('active');
		
		if ( activeUlItem ){
			$('#articleCarouselPagination li.active').removeClass('active');
			$('#articleCarouselPagination li:eq('+activeUlItemIndex+')').addClass('active');
		}
		
	}
	
	
	
	$('#articleCarouselPagination li').live('click', function(){
		
		if ( $(this).hasClass('active') ){
			return false;
		}
		
		$('#articleCarouselPagination li.active').removeClass('active');
		
		$(this).addClass('active');
		
		var clickLiIndex = $(this).index();
				
		var newOffset = clickLiIndex * 239;
		newOffset = '-' + newOffset + 'px';
				
		$('.articlePaginationRail').animate({
			'left' : newOffset
		}, 500);
	
	});

});




