

var curZIndex = 12;
var autoCyclerInterval = 7000;
var paneFadeOutInProgress = false;
var cyclerStopped = false;

var stripUpdateInterval = {slow: 30, normal: 15, fast: 10};
var stripState = {element: false, paddingLeft: 0, paddingRight: 0, currentInterval: stripUpdateInterval.slow, direction: 1};

jQuery.fn.autoFade = function(params) {

	if(params === undefined)
		params = {
			fadeTimeout: 4000,
			fadeOutSpeed: 1000,
			fadeInSpeed: 1000
		}

	visibleElement = jQuery(this).find('div:visible'); //there should be only one
	allElements = jQuery(this).children();
	
	if(allElements.length == 0 || allElements.length == 1)
		return;
	
	if(visibleElement.length == 0) {
		jQuery(allElements[0]).show();
		visibleElement = jQuery(allElements[0]);
	} else {
		visibleElement = jQuery(visibleElement[0]);
	}
	
	timeout = params.fadeTimeout;
	target = jQuery(this);
	
	visibleElement.delay(params.fadeTimeout).fadeOut(params.fadeOutSpeed, function() {
		nextElem = jQuery(this).next('div:hidden');
		if(jQuery(this).next('div:hidden').length == 0 && jQuery(this).prev('div:hidden').length == 0) // there is only one element in the list
			nextElem = jQuery(this);
		else if(jQuery(this).next('div:hidden').length == 0) // this was the last element in the list
			nextElem = jQuery(this).parent().children('div:hidden')[0];
		jQuery(this).hide();
		jQuery(nextElem).fadeIn(params.fadeInSpeed, function() {
			target.autoFade(params);
		});
	});
	
	return this;
}

function setupUI() {
	//add support for the cycling menu
	jQuery('div.overallMenu a').click(function(e){
		e.preventDefault();
		cyclerStopped = true; // clicking stops the cycler
		if(jQuery(this).hasClass('selected'))
			return;
		jQuery('div.overallMenu a').removeClass('selected');
              //  Cufon.replace('div.overallMenu a', {color: '#FFFFFF'});
		jQuery(this).addClass('selected');
               // Cufon.replace(jQuery(this), {color: '#000000'});
		i = 0;
		selected = 0;
		jQuery.each(jQuery('div.overallMenu a'),function(i,v) {
			if(jQuery(v).hasClass('selected'))
				selected = i;
			i++;
		});
		
		cycleDiv(selected);
	});
	
	//add auto menu cycling
	setInterval("autoCycler();",autoCyclerInterval);
	
	//setup cycling companies strip
	if(jQuery('.client_logo').length != 0){
			createCompanyStrip('#logoScroller');
	}
}

function createCompanyStrip(selector) {
	elem = jQuery(selector);
	//console.log(elem);
	if(elem.length == 0)
		return;
	//hide links
	
	//add new div
	scrollerDiv = jQuery('<div class="verticalScroller"></div>');
	var imageElements = elem.find('a');
	totalWidth = 0;
	jQuery.each(imageElements, function(i, v) {
		totalWidth += jQuery(v).innerWidth();
	});
	lPad = jQuery(imageElements[0]).width(); //left padding is first elements width
	rPad = jQuery(imageElements[imageElements.length-1]).width(); //right padding is last elements width
	scrollerDiv.width(totalWidth);
	scrollerDiv.css({
		'text-align' : stripState.direction == -1 ? 'left' : 'right',
		'position' : 'relative',
		'left' : -400,
		'padding-left' : lPad + 'px',
		'padding-right' : rPad + 'px'
	});
	stripState.paddingLeft = lPad;
	stripState.paddingRight = rPad;
	elem.append(scrollerDiv.append(imageElements));
	stripState.element = scrollerDiv;
	moveStrip();
}

function moveStrip() {
	if(stripState.paddingLeft <=0) {
		//remove first image and shift it to the back
		w = jQuery(stripState.element.find('a')[0]).innerWidth();
		stripState.paddingLeft += w;
		stripState.paddingRight -= w;
		stripState.element.append(jQuery(stripState.element.find('a')[0]));
		stripState.element.css({
			'padding-left' : stripState.paddingLeft + 'px',
			'padding-right' : stripState.paddingRight + 'px'
		});
	} else if(stripState.paddingRight <= 0) {
		//remove last image and shift it to the back
		tmp = stripState.element.find('a');
		w = jQuery(stripState.element.find('a')[tmp.length-1]).innerWidth();
		stripState.paddingRight += w;
		stripState.paddingLeft -= w;
		stripState.element.prepend(jQuery(stripState.element.find('a')[tmp.length-1]));
		stripState.element.css({
			'padding-left' : stripState.paddingLeft + 'px',
			'padding-right' : stripState.paddingRight + 'px'
		});
	}
	
	stripState.paddingLeft += stripState.direction;
	stripState.paddingRight -= stripState.direction;
	stripState.element.css({
		'padding-left' : stripState.paddingLeft + 'px',
		'padding-right' : stripState.paddingRight + 'px'
	});
	
	setTimeout("moveStrip()",stripState.currentInterval);
	
}

function cycleDiv(selected) {

	currentDiv = jQuery('div.quick_shot').find('.front_photo_box:visible');
	currentDivCopy = currentDiv.clone();
	currentDivCopy.children('div.main_page_text').hide();
	currentDivCopy.removeAttr('id').css({
		'left' : currentDiv.offset().left,
		'top' : currentDiv.offset().top,
		'position': 'absolute',
		'z-index' : curZIndex++
	}).show();
	jQuery('body').append(currentDivCopy);
	currentDiv.hide();
	jQuery('#front_photo_' + selected).fadeIn(1200);
	currentDivCopy.fadeOut(1200, function(){
		currentDivCopy.remove();
	});
	
}

function autoCycler() {
	if(cyclerStopped === true) {
		cyclerStopped = false;
		return;
	}
	
	//get current div
	currentDiv = jQuery('div.quick_shot').find('.front_photo_box:visible');
	//get num number
	if(currentDiv.attr('id') != undefined){
        num = parseInt(currentDiv.attr('id').replace(/[a-zA-Z\-\_]/g, "")) + 1;
    	
    	//get total number of items
    	total = jQuery('div.quick_shot div.front_photo_box').length;
    	
    	//calculate number of next item
    	nextItemIndex = (num + 1 > total) ? 1 : num + 1;
    	jQuery('div.overallMenu a').removeClass('selected');
        //Cufon.replace('div.overallMenu a', {color: '#FFFFFF'});
        menuItem = jQuery('div.overallMenu div:nth-child('+nextItemIndex+') a').addClass('selected');
       // Cufon.replace('div.overallMenu div:nth-child('+nextItemIndex+') a', {color: '#000000'});
        cycleDiv(nextItemIndex-1);

    }
}

jQuery(window).load(function(){
//jQuery(document).ready(function(){

	setupUI();
    //setupPage();

	if(jQuery('#f_resource').length != 0)
	jQuery('#f_resource').autoFade({
		fadeTimeout: 7000,
		fadeOutSpeed: 1500,
		fadeInSpeed: 1500,
		name: 'bottom'
	});
    
   
    $("#okolica").mouseover(function() {
            $(".okolica").show();
          }).mouseout(function(){
            $(".okolica").hide();
    });
    
    $("#osoby").mouseover(function() {
            $(".osoby").show();
          }).mouseout(function(){
            $(".osoby").hide();
    });


    $("#mienie").mouseover(function() {
            $(".mienie").show();
          }).mouseout(function(){
            $(".mienie").hide();
    });

    $("#bezpiecznie").mouseover(function() {
            $(".bezpiecznie").show();
          }).mouseout(function(){
            $(".bezpiecznie").hide();
    });


    
	
});
