jQuery.easing.easeOutQuart = function (x, t, b, c, d) {
	return -c * ((t=t/d-1)*t*t*t - 1) + b;
};

//LOGIN
loginUser = function() {
    
	$.post("/a_auth.php", $("#loginBoxForm").serialize(), function(data){
		if(data=='ok') {
			jQuery.ajax({
				url: '/a_userMenu.php',
				type: 'GET',
				dataType: 'html',
				timeout: 10000,
				error: function(){
					alert('Błąd podczas logowania. Proszę spróbować później.');
					window.location = '/index.php';
				},
				success: function(html){
					$("#loginBoxHolder").html(html);
					//window.location = '/index.php';
					window.location.reload();
				}
			});
		} 
		else if(data=='wrong') {
			alert('Nieprawidłowy użytkownik lub hasło.');
		} 
		else if(data=='klient') {
		} 
		else {}
	});			
};

//RIGHT BOX FUNCTION
boxPanel = function(handler, type) {
	
    var data = boxdata[type];
    var hand = handler;
    var img = $('img', hand);
    var index = 0;
    var name = '';
    var kod1 = '';
    var kod2 = '';
    
    if(data.length == 0) return;

    var fadeIn = function(){
        img.fadeIn('fast');
    }
    var fadeOut = function(){
        $('.boxProductName', hand).html(name);
        $('.boxProductID', hand).html(kod2);
        img.attr("src", '/photos/produkty/t/' + kod1 + '.jpg');
        img.load(fadeIn);
    }
    var loadNextProduct = function(){
        if(++index >= data.length)index = 0;    
        name = data[index][2];
        kod1 = String(data[index][0]);
        kod2 = '';
        for(var i = 0; i < kod1.length; i++) kod2 += kod1.charAt(i) + ' ';
        img.fadeOut('fast', fadeOut);
    }
    $('.boxNextProduct', handler).bind('click',  loadNextProduct);
    $('.boxPrevProduct', handler).bind('click',  function(){
        if((index-=2) < 0)index = data.length + index;
        loadNextProduct();
    });
    $('.boxProductPhoto', handler).bind('click',  function(){
        parent.GB_showCenter('', '/a_product.php?rodzina=' + data[index][1] + "&kod=" + data[index][0], 601, 801);
    });
    
    loadNextProduct();
    setInterval(loadNextProduct, 5000);
}

//NAVIGATOR
var navigatorData = [];
var navigatorTitle = '';
var navigatorInitValue = [['home', 'Spokey', '/index.php', null]];

navigatorAdd = function(id, title, url, js){
    navigatorData[navigatorData.length] = [id, title, url, js];
}

navigatorDraw = function(){
    
    var vText = '';
    var nText = $('#navigatorText');
    
    if(navigatorData.length == 0){
        nText.html('');
        return;
    }
    
    for(var i = 0; i < navigatorData.length; i++){
        if(navigatorTitle.length > 0){
        
          var elem = $(document.createElement('a'));
          vText += '<a class="navigatorLink" href="' + navigatorData[i][2] + '" onclick="' + navigatorData[i][3] + '">' + navigatorData[i][1] + '</a><div class="navigatorSep"></div>';
        }
        else{
            if(i < navigatorData.length - 1)
                vText += '<a class="navigatorLink" href="' + navigatorData[i][2] + '" onclick="' + navigatorData[i][3] + '">' + navigatorData[i][1] + '</a><div class="navigatorSep"></div>';
            else
                vText += '<div class="navigatorTitle">' + navigatorData[i][1] + '</div>';
        }
    }
    
    if(navigatorTitle.length > 0)
        vText += '<div class="navigatorTitle">' + navigatorTitle + '</div>';
    
    nText.html(vText);
}

navigatorMove = function(id, text, link, js){
    
    var found = false;
    for(var i = 0; i < navigatorData.length; i++){
        if(navigatorData[i][0] == id){
            found = true;
            navigatorData.splice(i+1, navigatorData.length-i);
            break;
        }
    }

    if(found == false){
        navigatorAdd(id, text, link, js);
    }
    navigatorDraw();
}

navigatorReset = function(){
    navigatorData = navigatorInitValue.slice();
    navigatorTitle = '';
    navigatorDraw();
}


//DOCUMENT.READY
$(document).ready(function(){
    
    //TOP MENU
    $('#topMenu').each(function(index){
        var timerId = 0;
        var selItem = 0;
		$('.tm_menu', this).bind('mouseenter', function(){
		    cancelTimer();
		    closeItem();
            selItem = $('.tm_submenu', this).css('visibility', 'visible');
		});
		$('.tm_menu', this).bind('mouseleave',  function(){
            timerId = window.setTimeout(closeItem, 300); // timeout
		});
		cancelTimer = function(){	
		    if(timerId) window.clearTimeout(timerId);
			timerId = null;
	    }
	    closeItem = function(){
	        if(selItem) selItem.css('visibility', 'hidden');
	    }
    });
    
    // LEFT MENU
    $('#jGlide_000').jGlideMenu({
	    tileSource	: '.jGlide_000_tiles',
	    initialTile : 'tile_000'
    }).show();

    //RIGHT BOXES
    if($('#boxNowosci').is('*'))  boxPanel($('#boxNowosci'), 'nowosci');
    if($('#boxPromocje').is('*')) boxPanel($('#boxPromocje'), 'wyprzedaz');
    if($('#boxPolecamy').is('*')) boxPanel($('#boxPolecamy'), 'polecamy');
    
    //NAVIGATOR INIT
    navigatorReset();

    /* LOGIN */		
    $('#loginSubmit').click(function(){
	    loginUser();
    });
    
    $("#loginBoxForm").keydown(function(event){
        if(event.keyCode == 13){
            loginUser();
            return false;
        }
    });
    
    /* SCROLL */
    $('#scrollContent').onImagesLoad({
        callbackIfNoImagesExist: true,
        selectorCallback: function(selector){
            $('#scrollContent').jScrollPane();
        }
    });

}); // END OF DOCUMENT.READY
