function explode (delimiter, string, limit) {
    // Splits a string on string separator and return array of components. If limit is positive only limit number of components is returned. If limit is negative all components except the last abs(limit) are returned.  
    // 
    // version: 909.322
    // discuss at: http://phpjs.org/functions/explode    // +     original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: kenneth
    // +     improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +     improved by: d3x
    // +     bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // *     example 1: explode(' ', 'Kevin van Zonneveld');
    // *     returns 1: {0: 'Kevin', 1: 'van', 2: 'Zonneveld'}
    // *     example 2: explode('=', 'a=bc=d', 2);
    // *     returns 2: ['a', 'bc=d']
     var emptyArray = { 0: '' };
    
    // third argument is not required
    if ( arguments.length < 2 ||
        typeof arguments[0] == 'undefined' ||        typeof arguments[1] == 'undefined' )
    {
        return null;
    }
     if ( delimiter === '' ||
        delimiter === false ||
        delimiter === null )
    {
        return false;    }
 
    if ( typeof delimiter == 'function' ||
        typeof delimiter == 'object' ||
        typeof string == 'function' ||        typeof string == 'object' )
    {
        return emptyArray;
    }
     if ( delimiter === true ) {
        delimiter = '1';
    }
    
    if (!limit) {        return string.toString().split(delimiter.toString());
    } else {
        // support for limit argument
        var splitted = string.toString().split(delimiter.toString());
        var partA = splitted.splice(0, limit - 1);        var partB = splitted.join(delimiter.toString());
        partA.push(partB);
        return partA;
    }
}

//Credits: Robert Penners easing equations (http://www.robertpenner.com/easing/).
jQuery.easing['BounceEaseOut'] = function(p, t, b, c, d) {
	if ((t/=d) < (1/2.75)) {
		return c*(7.5625*t*t) + b;
	} else if (t < (2/2.75)) {
		return c*(7.5625*(t-=(1.5/2.75))*t + .75) + b;
	} else if (t < (2.5/2.75)) {
		return c*(7.5625*(t-=(2.25/2.75))*t + .9375) + b;
	} else {
		return c*(7.5625*(t-=(2.625/2.75))*t + .984375) + b;
	}
};

/*************************************************************************************************************************************/
/*************************************************************************************************************************************/
/*************************************************************************************************************************************/

function moveElementLeft(Element,leftEndPosition)
{
	$(Element).animate( { left: leftEndPosition }, { queue:false, duration:1000 } );	
}


function showQuestion(titleH1)
{
	$("#qproduct h1").remove();
	$("#qproduct").prepend('<h1>'+titleH1+'</h1>');
	 moveElementLeft("#qproduct h1",25);
}

// odznaczenie wszystkich radio
function radioCheckedOff()
{
    QuestionStatus = '';
	$(".question div.radio").css("background-position","0 0");
    $("#butonSprawdz").unbind('click');
    $("#butonSprawdz").click(function() { $('#NoCheck').fadeIn('slow'); });
};

function radioCheckedOn(thisElement)						// zaznacz kliknięte radio
{
	radioCheckedOff();										// odznacz wszystkie poprzednio zaznaczone radio
	QuestionStatus = 'checked';								// oznacz stala zmienna jako kliknieto pytanie
	$('#NoCheck').fadeOut('slow');							// ukryj tekst brak zaznaczenia
	
	$(thisElement).find('div.radio').css("background-position","0 -22px");	// zaznacz klikniete radio
	
	$("#butonSprawdz").unbind('click');						// usun funkcje click poprzednio przypisana
	$("#butonSprawdz").click(								// przypisz click do butona sprawdz wywolujacego produkty 
		function() {
			showDivs( $(thisElement).attr('title'),thisElement );	
	});
}

function showDivs(wich,thisElement)
{
	if(QuestionStatus == 'checked')
	{
		$("#bgQplus").fadeOut(1500); 
		
		moveElementLeft("#qTitle",-800);
		moveElementLeft("#butonSprawdz",-800);
		moveElementLeft(".jcarousel-next",-800);
		moveElementLeft("#butonQBack",32);
		showQuestion($(thisElement).text());
		$('div.jcarousel-skin-tango, #mycarousel').fadeTo(1000, 0);	// ukrywamy kraruzele

		DIVS = explode(",", wich); // kolejnosc divow z title do tablicy
		for(var i=0; i < DIVS.length; i++)
		{
			$("#productBox"+DIVS[i]).prependTo("#qproduct").show().animate(
				{ left: eval(25+i*180) }, { queue:false, duration:1000 } 
			);
		}
	}
}

function backToQuestion()
{
	radioCheckedOff();										// odznacz wszystkie radio
	
	moveElementLeft("#qproduct h1",-800);
	moveElementLeft("#butonQBack",-800);
	moveElementLeft(".productBox",800);
	
	moveElementLeft("#butonSprawdz",2);
	moveElementLeft(".jcarousel-next",78);
	moveElementLeft("#qTitle",30);
	
	$("#bgQplus").fadeIn(2000);

	$('div.jcarousel-skin-tango, #mycarousel').fadeTo(1000, 1.0);		// pokaz karuzele pytan
}

/*************************************************************************************************************************************/

function carousel_1_beforeAnimation(carousel,element,i,status)
{
	var idx = carousel.index(i, carousel.options.size);
	carousel.add(i,carousel.get(idx).html())
}
    	
function carousel_1_afterAnimation(carousel,element,index,status)
{
	var idx = carousel.index(index, carousel.options.size);
	carousel.selected = idx;
	radioCheckedOff();
}

function carousel_1_itemVisibleOutCallback(carousel, item, i, state, evt)
{
	if (i>carousel.options.size || i<0)
	{
		carousel.remove(i);
	}
	carousel.startAuto();
};
/*************************************************************************************************************************************/

jQuery(document).ready(function()
{
    jQuery("#mycarousel").jcarousel({
        scroll: 1,
        wrap: 'circular',
    	
        buttonPrevHTML: null,
        easing: 'BounceEaseOut',
        animation: 800,
        itemVisibleInCallback: {
  			onBeforeAnimation: carousel_1_beforeAnimation,
  			onAfterAnimation: carousel_1_afterAnimation
		},
		itemVisibleOutCallback:carousel_1_itemVisibleOutCallback
    });

	$("#butonSprawdz").appendTo(".jcarousel-container.jcarousel-container-horizontal");
	
	moveElementLeft("#butonSprawdz",2);
	moveElementLeft(".jcarousel-next",78);
	moveElementLeft("#qTitle",30);
	$("#bgQplus").fadeIn(2000);
	
	$("#butonSprawdz").click(function() { $('#NoCheck').fadeIn('slow'); });
	
	$("#butonQBack").live('click', function() { backToQuestion() });	// button przypisanie funkcji pod click
	$(".question").live('click', function() { radioCheckedOn(this); });	// element P z pytaniem przypisanie funkcji pod click
	
	$(".buttonsQproduct,.jcarousel-next").hover(function() { $(this).css('background-position','0 -26px')}, function() {$(this).css('background-position','0 0')}) // podswietlanie sie przyciskow
});
