
//formularz logowania
var p = 'Hasło';
var l = 'Login lub e-mail';


$j(document).ready(function($){


    $j('.rabat').tooltip({
        track: true,
        delay: 0,
        showURL: false,
        showBody: " - ",
        extraClass: "pretty",
        fixPNG: true,
        opacity: 1.00
    });

    $('.ready').css('display','block');

    $('#news_nav a').click(function(){
        var id = $(this).text();
        $('.news_box:visible').fadeOut(
            'slow',
            function()
            {
                $('#news_box_'+id).fadeIn('slow');
                $('#news_nav a').removeClass('fnewsactive');
                $('#news_nav a').eq((id-1)).addClass('fnewsactive');
            }
            );
    });


    //if(document.getElementById('lpassword')) document.getElementById('lpassword').type = 'text';
     

    $('#llogin').val(l);
    $('#lpassword').val(p);

    $('div.photoitems_info').fadeTo(200,0.75);
		
    $('div.photoitems').hover(
        function(){
            $(this).find('.photoitems_info').animate({
                marginTop: "0"
            },
            {
                queue:false,
                duration:300
            }
            );
        },
        function(){
            $(this).find('.photoitems_info').animate({
                marginTop: "181px"
            },
            {
                queue:false,
                duration:300
            }
            );
        }
        );
		
    $('a.fngal').fancybox({
        'zoomSpeedIn': 300,
        'zoomSpeedOut': 300,
        'overlayShow': true
    });


    $('form#login_form').bind('submit',function(){  
        if($('div#loginmsg').length == 0){
            $('input#loginProcess').parent().prepend('<div id="loginmsg"></div>');
        }
        $.ajax({
            type: 'POST',
            url: '/loguj',
            data: 'llogin='+$('#llogin').val()+'&lpassword='+$('#lpassword').val(),//+'&l_no_csrf='+$('#l_no_csrf').val(),
            dataType: 'json',
            beforeSend: function(){
                $('div#loginmsg').text('Trwa logowanie...');
            },
            success: function(response){
                //                if(response.new_no_csrf){
                //                    $('#l_no_csrf').val(response.new_no_csrf);
                //                }
                if(response.status == 'error'){
                    $('div#loginmsg').text('Niepoprawny login lub hasło');
                } else if(response.status == 'ok'){
                    location.assign(document.location);
                }
            }
        });
        return false;
    });
					
    $('input').blur(function(){
        if($(this).val() != '' && $(this).next().is('ul.errors')){
            $(this).next().remove();
        }
        if($(this).val() != '' && $(this).next().next().is('ul.errors')){
            $(this).next().next().remove();
        }
    });
		
    $('select').change(function(){
        if($(this).selectedValues() != '' && $(this).next().is('ul.errors')){
            $(this).next().remove();
        }
        if($(this).selectedValues() != '' && $(this).next().next().is('ul.errors')){
            $(this).next().next().remove();
        }
    });
				
    $(':radio').click(function(){
        if($(this).parent().parent().hasClass('radiobox_area')){
            $(this).parent().parent().find('ul.errors').remove();
        }
    });

    $(':checkbox').click(function(){
        if($(this).parent().parent().hasClass('checkbox_area')){
            $(this).parent().parent().find('ul.errors').remove();
        }
    });

		
    $('textarea').blur(function(){
        if($(this).val() != '' && $(this).next().is('ul.errors')){
            $(this).next().remove();
        }
        if($(this).val() != '' && $(this).next().next().is('ul.errors')){
            $(this).next().next().remove();
        }
    });
		

    var navigationPath = $('div.userarea ul.navigation');
    navigationPath.find('li').has('ul').click(function(){
        navigationPath.find('li').not(this).find('ul').slideUp();
        $(this).find('ul:first').slideDown();
    });

    /*
    $('#simc_province_id').change(function(){
        if($('#simc_province_id').selectedValues() == ''){
            var simc_pow = $('#simc_pow');
            simc_pow.removeOption(/./);
            simc_pow.addOption({
                '':'--wybierz--'
            });
            var simc_id = $('#simc_id');
            simc_id.removeOption(/./);
            simc_id.addOption({
                '':'--wybierz--'
            });
            return false;
        }
        $.ajax({
            type: 'POST',
            url: '/Simc/data/province/',
            data: 'province='+$(this).selectedValues(),
            dataType: 'json',
            beforeSend: function(){
                $('<img style="margin-left: 10px;" id="province_loading" src="/images/loading.gif" alt="" />').insertAfter($('#simc_province_id'));
                $('#simc_pow').attr('disabled',true);
            },
            success: function(data){
                $('#province_loading').remove();
                var simc_pow = $('#simc_pow');
                simc_pow.attr('disabled',false);
                simc_pow.removeOption(/./);
                simc_pow.addOption(data);
                simc_pow.selectOptions('');
                if(simc_pow.next().is('ul.errors')){
                    simc_pow.next().remove();
                }
                var simc_id = $('#simc_id');
                simc_id.removeOption(/./);
                simc_id.addOption({
                    '':'--wybierz--'
                });
            }
        });
    });
*/

    $('#simc_province_id').change(function(){
        //    $('#simc_pow').change(function(){
        if($(this).selectedValues() == ''){
            var simc_id = $('#simc_id');
            simc_id.removeOption(/./);
            simc_id.addOption({
                '':'--wybierz--'
            });
            return false;
        }
        $.ajax({
            type: 'POST',
            url: '/Simc/data/province/',
            data: 'province='+$('#simc_province_id').selectedValues()+'&pow='+$(this).selectedValues(),
            dataType: 'json',
            beforeSend: function(){
                $('<img style="margin-left: 10px;" id="pow_loading" src="/images/loading.gif" alt="" />').insertAfter($('#simc_id'));
                $('#simc_id').attr('disabled',true);
            },
            success: function(data){
                $('#pow_loading').remove();
                var simc_id = $('#simc_id');
                simc_id.attr('disabled',false);
                simc_id.removeOption(/./);
                simc_id.addOption(data);
                simc_id.selectOptions('');
                if(simc_id.next().is('ul.errors')){
                    simc_id.next().remove();
                }
            }
        });
    });

    $('#marka').sortOptions();
    $('#cena,#cena_to,#moc,#moc_to').numeric();
		
    $('#marka').change(function(){
        if($(this).selectedValues() == ''){
            var model = $('#model');
            model.removeOption(/./);
            model.addOption({
                '':'--wybierz--'
            });
            return false;
        }
        $.ajax({
            type: 'POST',
            url: '/Card/public/getchildparams',
            data: 'id='+$(this).selectedValues(),
            dataType: 'json',
            beforeSend: function(){
                $('<img style="margin-left: 10px;" id="model_loading" src="/images/loading.gif" alt="" />').insertAfter($('#marka'));
                $('#model').attr('disabled',true);
            },
            success: function(data){
                $('#model_loading').remove();
                var model = $('#model');
                model.attr('disabled',false);
                model.removeOption(/./);
                model.addOption(data);
                model.selectOptions('');
                if(model.next().is('ul.errors')){
                    model.next().remove();
                }
            }
        });
    });

    
    $j('#lpassword').focus(function(){
        if($j('#lpassword').val() == p || $j('#llogin').val()=='')
        {
            //if(document.getElementById('lpassword')) document.getElementById('lpassword').type = 'password';
            $j('#lpassword').val('');
        }
    });
    $j('#llogin').focus(function(){
        if($j('#llogin').val() == l || $j('#llogin').val()=='')
            $j('#llogin').val('');
    });
				
});
 
