Cufon.replace('h1, h2, h3, .cufon, #menu-left li', {
	hover: true
});

/************************************************************* */

//_initAjaxRequest();

$(document).ready(_initPage);
window.onresize = _onResize;

$(window).load(function () {
	$('.scroll-panel').jScrollPane({
		scrollbarWidth: 16,
		dragMinHeight: 68,
		dragMaxHeight: 450,
		reinitialiseOnImageLoad: true
	});

	$('.scroll-panel-cat').jScrollPane({
		scrollbarWidth: 16,
		dragMinHeight: 68,
		dragMaxHeight: 450,
		reinitialiseOnImageLoad: false
	});
});

function _onResize()
{
	$('#content-holder').css('margin-top', ( ($(window).height() - 540) / 2 ) );
	$('#pre').hide();
}

function _initPage()
{
	_onResize();
	
	// centrowanie obrazow w pionie wewnatrze kontenera
	$('.vAlign').vAlign();
	// opisu pol w formularzach
	$('.clearField').clearField();
	
	// obrazy uruchamiane w lightboxie
	$("a[rel^='fancybox']").fancybox({
		'titlePosition'	: 'over',
		'overlayOpacity': .5,
		'showNavArrows'	: false,
		'hideOnContentClick': true
	});
	
	// linki obslugiwane za pomoca XMLHTTPRequest
	//$("a[rel^='ajaxRequest']").sevEnAjaxRequest({
	//	reInitCallback	: _reinitPage
	//});

	$('.toolTip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " -=- ",
		fade: 250
	});
	
	$('#history-back').click(function() {
		history.back();
		return false;
	});
}

function _reinitPage()
{
	$('.vAlign').vAlign();
	$('.clearField').clearField();
	$("a[rel^='fancybox']").fancybox();

	$("a[rel^='ajaxRequest']").sevEnAjaxRequest({
		reInitCallback	: _reinitPage
	});

	$('.toolTip').tooltip({
		track: true,
		delay: 0,
		showURL: false,
		showBody: " -=- ",
		fade: 250
	});

	//curvyCorners(settings, obj);
}

/************************************************************* */

var puzzleIndex	= 17;
var puzzleRun = false;
var puzzleSide = 'back';
var puzzleInterval;
var puzzleActiveScreen = 0;

function _getPuzzleImage(__index)	{return arrImages[__index][0];}

function _getPuzzleUrl(__puzzleIndex)
{
	if (__puzzleIndex == -1) __puzzleIndex = puzzleIndex;

	if (arrImages[__puzzleIndex][1] != '') return ' style="cursor: pointer;" onClick="document.location=\'' + arrImages[__puzzleIndex][1] + '\';"';
	else return '';
}

function _getPuzzleSide()
{
	if (puzzleSide == 'back') puzzleSide = 'front';
	else puzzleSide = 'back';

	return puzzleSide;
}

function _getNextIndex()
{
	if (puzzleIndex == (arrImages.length - 1)) puzzleIndex = -1;

	puzzleIndex++;

	return puzzleIndex;
}

function _initHomePuzzle()
{
	var screenCount = arrImages.length / 9;

	// adding counter buttons
	for (i = 0; i < screenCount; i++)
	{
		$('#control-counter').append('<a href="#" class="pos" id="screen-' + i + '" rel="' + i + '" >' + (i + 1) + '</a>');
	}

	$('#control-counter').append('<a href="#" id="control-but"></a>');
	$('#control-but').click(_puzzleControlClick);

	$('#control-counter a.pos').click(_puzzleCounterClick);

	$('#screen-0').addClass('active');

	var innerImg;

	for (i = 0; i < 9; i++)
	{
		_puzzleImgPrepend('front', i, i);
		_puzzleImgPrepend('back', i, i+9);

		$('#piece-' + i + ' .back').hide();

		innerImg = $('#piece-' + i + ' .front').find('img');
		innerImg.css('opacity', 0);

		innerImg.delay(i * 150).fadeTo(600, 1);
	}

	_timeoutPuzzleFlip();
}

function _timeoutPuzzleFlip()
{
	puzzleInterval = setInterval(function(){
		_switchPuzzle(_getPuzzleSide(), false, -2);
	}, 10000);
}

function _switchPuzzle(__side, __force, __setIndex)
{
	if (puzzleRun) {
		setTimeout(function() {
			_switchPuzzle(__side, __force, __setIndex);
		}, 300);
		return;
	}

	if (__setIndex != -2)
	{
		//alert(__setIndex);
		puzzleIndex = __setIndex;
	}

	puzzleRun = true;

	var side1;
	var side2;

	switch (__side)
	{
		case 'front':
			side1 = 'front';
			side2 = 'back';
			break;

		case 'back':
			side1 = 'back';
			side2 = 'front';
			break;
	}

	if(__force)
	{
		for (i = 0; i < 9; i++)
		{
			$('#piece-' + i + ' .' + side2).empty();
			_puzzleImgPrepend(side2, i, _getNextIndex());
		}
	}

	else
	{
		$('#control-counter a.pos').removeClass('active');
		$('#screen-' + (((puzzleIndex + 1) / 9) - 1)).addClass('active');
		puzzleActiveScreen = ((puzzleIndex + 1) / 9) - 1;
	}

	for (i = 0; i < 9; i++)
	{
		$('#piece-' + i + ' .' + side1).delay(i * 200).fadeTo(550, 0, function(){
			$(this).hide().css('opacity', '1').empty();

			_puzzleImgPrepend(this, -1, _getNextIndex());
		});

		$('#piece-' + i + ' .' + side2).delay(i * 200).show(500);
	}

	setTimeout(function(){
		puzzleRun = false;
	}, 2300);
}

function _puzzleControlClick()
{
	$('#control-but').css('display', 'none');

	puzzleActiveScreen++;
	if (puzzleActiveScreen == (arrImages.length / 9)) puzzleActiveScreen = 0;

	$('#control-counter a.pos').removeClass('active');
	$('#screen-' + puzzleActiveScreen).addClass('active');

	_switchPuzzle(_getPuzzleSide(), true, (puzzleActiveScreen * 9) - 1);

	_timeoutPuzzleFlip();
}

function _puzzleCounterClick()
{
	// stopping puzzleInterval
	clearInterval(puzzleInterval);

	$('#control-but').css('display', 'block');

	$('#control-counter a.pos').removeClass('active');
	$(this).addClass('active');

	puzzleActiveScreen = $(this).attr('rel');

	_switchPuzzle(_getPuzzleSide(), true, (($(this).attr('rel') * 9) - 1));
}

function _puzzleImgPrepend(__el, __imgIndex, __puzzleIndex)
{
	if (__imgIndex != -1) {
		$('#piece-' + (__imgIndex) + ' .' + __el).prepend('<img src="' + _getPuzzleImage(__puzzleIndex) + '" alt="piece" ' + _getPuzzleUrl(__puzzleIndex) + ' />');
	}

	else {
		$(__el).prepend('<img src="' + _getPuzzleImage(__puzzleIndex) + '" alt="piece" ' + _getPuzzleUrl(-1) + ' />');
	}
}

/************************************************************* */

$(function(){

	var div = $('#fryzjerstwo-holder div.scroll-menu'),
		ul = $('#fryzjerstwo-holder ul.scroll-menu'),
		menu = $('#fryzjerstwo-menu'),
		holder = $('#fryzjerstwo-holder'),
		ulPadding = 5;
	
	//Get menu height
	var divHeight = div.height();
	div.css({'overflow': 'hidden'});
	var lastLi = ul.find('li:last-child');
	
	holder.hide(200);
	
	div.mousemove(function(e){
		var ulHeight = lastLi[0].offsetTop + lastLi.outerHeight() + ulPadding;	
		var top = (e.pageY - div.offset().top) * (ulHeight-divHeight) / divHeight;
		div.scrollTop(top);
	});

	menu.mouseover(function(e){ holder.show(200, function() {
		holder.css('opacity', .9);
	} ); });

	holder.mouseleave(function(e){ holder.hide(200); });
	
	$('#menu-left > li > a').not(menu).mouseover(function(){ holder.hide(200); });
});

$(function(){

	var div = $('#kosmetyka-holder div.scroll-menu'),
		ul = $('#kosmetyka-holder ul.scroll-menu'),
		menu = $('#kosmetyka-menu'),
		holder = $('#kosmetyka-holder'),
		ulPadding = 5;
	
	//Get menu height
	var divHeight = div.height();
	div.css({'overflow': 'hidden'});
	var lastLi = ul.find('li:last-child');
	
	holder.hide(200);
	
	div.mousemove(function(e){
		var ulHeight = lastLi[0].offsetTop + lastLi.outerHeight() + ulPadding;	
		var top = (e.pageY - div.offset().top) * (ulHeight-divHeight) / divHeight;
		div.scrollTop(top);
	});
	
	menu.mouseover(function(e){ holder.show(200, function() {
		holder.css('opacity', .9);
	} ); });
	
	holder.mouseleave(function(e){ holder.hide(200); });
	
	$('#menu-left > li > a').not(menu).mouseover(function(){ holder.hide(200); });
});

$(function(){

	var div = $('#spa-holder div.scroll-menu'),
		ul = $('#spa-holder ul.scroll-menu'),
		menu = $('#spa-menu'),
		holder = $('#spa-holder'),
		ulPadding = 5;
	
	//Get menu height
	var divHeight = div.height();
	div.css({'overflow': 'hidden'});
	var lastLi = ul.find('li:last-child');
	
	holder.hide(200);
	
	div.mousemove(function(e){
		var ulHeight = lastLi[0].offsetTop + lastLi.outerHeight() + ulPadding;	
		var top = (e.pageY - div.offset().top) * (ulHeight-divHeight) / divHeight;
		div.scrollTop(top);
	});

	menu.mouseover(function(e){ holder.show(200, function() {
		holder.css('opacity', .9);
	} ); });

	holder.mouseleave(function(e){ holder.hide(200); });
	
	$('#menu-left > li > a').not(menu).mouseover(function(){ holder.hide(200); });
});

$(function(){

	var div = $('#kolory-holder div.scroll-menu'),
		ul = $('#kolory-holder ul.scroll-menu'),
		menu = $('#kolory-menu'),
		holder = $('#kolory-holder'),
		ulPadding = 2;

	//Get menu height
	var divHeight = div.height();
	div.css({'overflow': 'hidden'});
	var lastLi = ul.find('li:last-child');

	holder.hide(200);

	div.mousemove(function(e){
		var ulHeight = lastLi[0].offsetTop + lastLi.outerHeight() + ulPadding;
		var top = (e.pageY - div.offset().top) * (ulHeight-divHeight) / divHeight;
		div.scrollTop(top);
	});

	menu.mouseover(function(e){ holder.show(200, function() {
		holder.css('opacity', .9);
	} ); });

	holder.mouseleave(function(e){ holder.hide(200); });

	$('#menu-left > li > a').not(menu).mouseover(function(){ holder.hide(200); });
});

/************************************************************* */

function pokazPozycje(obiektId)		{$("#" + obiektId).slideToggle("fast");}

/************************************************************* */

function _initAjaxRequest()
{
	// zastapienie funkcji w rsh
	window.dhtmlHistory.create( {
		debugMode : false,
		toJSON:		function(o) {return $.toJSON(o);},
		fromJSON:	function(s) {return $.parseJSON(s);}
	});

	window.onload = function() {
		dhtmlHistory.initialize();
		dhtmlHistory.addListener($().sevEnAjaxRequest.handleAjaxRequestHistory);
	};
}

/************************************************************* */

function policz_rate(koszyk)	
{
	if (koszyk > 100)
	{
		window.open('https://www.eraty.pl/symulator/oblicz.php?numerSklepu=1157967&wariantSklepu=1&typProduktu=0&wartoscTowarow='+koszyk, 'Policz_rate', 'width=630,height=500,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');

	//window.open('http://www.zagiel.com.pl/kalkulator/index_smart.php?action=getklientdet_si_rata&shopNo=01157924&goodsValue='+koszyk, 'Policz_rate', 'width=630,height=500,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no');
	}
	else alert("Zakupy na raty dostępne dla zamówień powyżej 100zł.")
}

function raty_warunki() 
{ 
	window.open('https://www.zagiel.com.pl/kalkulator/jak_kupic.html', 'Raty - warunki', 'width=600,height=500,directories=no,location=no,menubar=no,resizable=yes,scrollbars=yes,status=no,toolbar=no'); 
}

function koszyk_validate()
{
	if (document.koszyk.raty_zgoda.checked == false)
	{
		alert ("Zanim złożysz zamówienie, zapoznaj się z procedurą udzielenia kredytu ratalnego eKredyt Żagiel.");
		return false;
	}
	else return true;
}

function koszyk_validate_leasing()
{ 
	if (document.koszyk.raty_zgoda.checked == false)
	{ 
		alert ("Zanim złożysz zamówienie, zapoznaj się z procedurą udzielenia leasingu Grenke Leasing.");
		return false; 
	} 
	else return true;
}
