$(document).ready(function(){

	// init
	$("body").ScrollToAnchors(800);
	$("a.aWait").click(function(){ wait(); top.location = $(this).attr("href"); });
	
	InitStart();
    InitSelectColor();
    
    $(".basket3_next_step").click(function(){
        wait();
        url = $(this).attr("rel");
        setTimeout('top.location = url', 2000);
    });
    
    $(".back_step").click(function(){
        top.location = $(this).attr("rel");
    });
    
    $(".disabled").keydown(function(){ return false; });
    
    // ThickBox
    $('a.thickboxLink').lightBox({
        txtImage: 'Zdjęcie',
        txtOf: 'z'
    });
    
    $('a.thickboxFoto').lightBox({
        txtImage: 'Zdjęcie',
        txtOf: 'z'
    });
   
    function LockBody(){
        $.blockUI.defaults.overlayCSS.opacity = '0';
        $("body").block();
        $.blockUI.defaults.overlayCSS.opacity = '0.5';
    }
    
    function UnLockBody(){
        $("body").unblock();
    }
    
    $('#user_city, #ud_city, #gcity').autocomplete('' + baseURL + 'panel/suggest/city', {
        //width: 186,
        //max: 10,
        highlight: false,
        scroll: true,
        scrollHeight: 300,
        formatItem: function(data, i, n, value){
            return value;
        },
        formatResult: function(data, value){
            return value;
        }
    });
    
    $("#TABLE_USER_DELIVERY").tablesorter({
        headers: {
            0: {
    			sorter: false
            },
            3: {
            	sorter: false
            }
        }
    });
    
    $("#TABLE_USER_ORDERS").tablesorter({
    	headers: {
            0: {
                sorter: false
            },
            5: {
                sorter: false
            }
        },
        sortList: [[1, 1]]
    });
    
    function initLinks(){
        $('a.deleteLink').click(function(){
            var gotoUrl = $(this).attr("href");
            var question = $(this).attr("title");
            
            var root = $(this).attr("rel");
            
            if (root == 1) {
            	notify('Operacja niedozwolona.</p><p><span><b>Uwaga!</b> Domyślnie zabronione jest usuwanie elementów grupy nadrzędnej <b>root</b>','','error',0,'powrót')
                return false;
            }
            
            $.blockUI({message: '<div id="DIALOGBOX"><div id="DB1"><p><img src="' + baseURL + 'public/images/error.gif" align="left" alt="" />' + question + '</p><p><span><b>Uwaga!</b> Operacja ta jest nieodwracalna.</span></p></div><div id="DB2"><p><a style="float:left" id="yes" class="nul">TAK, <em>usuń</em>.</a> <a style="float:right" id="no" class="nul">NIE, <em>nie usuwaj</em>.</a></p></div></div>' });
            
            $('#yes').click(function(){
                wait();
            	$.ajax({
                    type: "GET",
                    url: gotoUrl,
                    error: function(msg){
                        $.unblockUI;
                        if (showErrors) 
                            alert("ERROR: " + msg);
                    },
                    success: function(msg){
                        if (msg.substr(0,3) != "OK!") {
                        	notify(msg.substr(7),'Naciśnij <b>OK</b>, aby kontynuować!','error',0,'powrót');
                        }
                        else {
                        	notify(msg.substr(4),'Naciśnij <b>OK</b>, aby kontynuować!','ok',1,'powrót');
                        }
                    }
                });
            });
            
            $('#no').click($.unblockUI);
            
            return false;
        });
    }
    initLinks();
    
    $('.pagesize').change(function(index){
        initLinks();
    });
    $('#pager img').click(function(index){
        initLinks();
    });
    $('.tablesorter').bind('sortEnd', function(){
        initLinks();
    });
    
    $.easing.elasout = function(x, t, b, c, d){
        var s = 1.70158;
        var p = 0;
        var a = c;
        if (t == 0) 
            return b;
        if ((t /= d) == 1) 
            return b + c;
        if (!p) 
            p = d * .3;
        if (a < Math.abs(c)) {
            a = c;
            var s = p / 4;
        }
        else 
            var s = p / (2 * Math.PI) * Math.asin(c / a);
        return a * Math.pow(2, -10 * t) * Math.sin((t * d - s) * (2 * Math.PI) / p) + c + b;
    };
    
   
    
    $("#uu").click(function(){
        $(this).val("");
        $(this).css("color", "#fff");
    });
    
    $("#up").click(function(){
        $(this).val("");
        $(this).css("color", "#fff");
    });

    $.dequeue = function(a, b){
        return $(a).dequeue(b);
    };
    
    // przesylki zagraniczne
    
    $("#newsarch").click(function(){
        $(".newsarchive").toggle();
    });
    
    $("#showLogIn").click(function(){
        $("#commentForm2").show();
    });
    
    $("#ei_show").dblclick(function(){
        $("#engine_info").show();
    });
    
    // FROM PAGE
    
    $(".submit").click(function(){
    	var tempForm = $(this).attr("title");
    	$("#"+tempForm).submit();
    });
   
    var optionsLoginForm = { 
        beforeSubmit: wait,  // pre-submit callback 
        success:       afterLogin,  // post-submit callback 
        clearForm: true        // clear all form fields after successful submit 
    }; 
    
    // bind form using 'ajaxForm' 
    $('#LOGOWANIE, #LOGOWANIE2').ajaxForm(optionsLoginForm);
 
});
