$(document).ready(function(){
	
    if($('.priceArea').size() > 0) $('.galeriaContainer li:eq(0) a').click();

    $('select.prodFeaturesSelect').change(function(){
        $('#artId').attr('value',$(this).find('option:selected').val());
    });

    jQuery.event.add(window, "load", resizeFrame);
    jQuery.event.add(window, "resize", resizeFrame);

    function resizeFrame()
    {
        var winHeight  = $(window).height();
		
        var divIns = $('#sliderWrapper').height();
		
        var newHeight = (winHeight-divIns-158);
		
        $('#sliderWrapper').css("height", newHeight+divIns+"px");
    }

    $('.checkbox').click(function(){
        $(this).toggleClass('checked');
    });

    $('.buttonClose').live('click',function(){
        $('.img_lightbox').fadeOut('fast');
        $('.background').addClass('hide');
    });

    $('#forma_platnosci .radio').click(function(){
        if($(this).parent().find('input').val() == 3) $('#platnoscityp').slideDown('slow');
        else
        {
            $('#platnoscityp').find('input').removeAttr("checked");
            $('#platnoscityp').slideUp('slow');
        }
    });

    //        $('.basketBox select').change(function(){
    //            filterOptions();
    //        });

    $('.remind.step1 .buttonPrzejdzDalej').live('click',function(){
        var email = $('.remind input[name="email"]').val();
        email = jQuery.trim(email);

        $('.remind .message').text("Wysyłam nowe hasło...");

        var action = $('#registrationForm').attr('action');

        if(email.length==0 || email ==''){
            $('.remind .message').empty().text("Nie podałeś adresu e-mail");
            return false;
        }
         
        $.ajax({
            type: "POST",
            url: '/wyslij-haslo',
            cache: false,
            dataType: "json",
            data: {
                email: email
            },
            success: function(data){
                if(data.status == 'false'){
                    $('.remind .message').empty().text(data.message);
                    $('.remind input[name="userId"]').val('');
                }
                if(data.status == 'true' && data.userid!=null){
                    $('.remind .message').empty().text(data.message);
                    $('.remind input[name="userId"]').val(data.userid);
                    $('.buttonPrzejdzDalej').hide();
                    $('.buttonZmienHaslo').show();
                 
                }
            }
        })
    })
    $('.buttonZmienHaslo').live('click',function(){
        var id = $('.remind input[name="userId"]').val();
        if(id.length>0){
            popupResponse('/zmien-haslo/'+id,'Zmiana hasła');
        }else{
             $('.remind .message').empty().text('Błąd id');
        }

    });
    $('.remind.step2 .buttonPrzejdzDalej').live('click',function(){
        var oldPass =  jQuery.trim($('.remind input[name="odlPass"]').val());
        var newPass =  jQuery.trim($('.remind input[name="newPass"]').val());
        var newPass1 = jQuery.trim($('.remind input[name="newPass1"]').val());
        var action = $('#registrationForm').attr('action');

        if(oldPass.length<=0){
             $('.remind .message').empty().text('Wpisz hasło otrzymane e-mailem');
             return false;
        }

        if( newPass.length<=0 || newPass1.length<=0){
             $('.remind .message').empty().text('Uzupełnij wszystkie pola');
              return false;
        }
        
        if((newPass.length>0 || newPass1.length>0) && (newPass!=newPass1)){
             $('.remind .message').empty().text('Hasła róznią się od siebie');
              return false;
        }
        if(newPass.length>0 && newPass1.length>0 && newPass==newPass1){
            if(newPass.length<5){
                  $('.remind .message').empty().text('Nowe hasło jest zbyt krótkie. Musi mieć co najmniej 5 znaków');
              return false;
            }
        }

        $.ajax({
            type: "POST",
            url: action,
            cache: false,
            dataType: "json",
            data: {
                oldPass: oldPass,
                newPass: newPass,
                newPass1:newPass1
            },
            success: function(data){
                if(data.status =='false'){
                      $('.remind .message').empty().text(data.message);
                }
                if(data.status == 'true'){
                      $('.remind .message').empty().text(data.message);
                      $('.buttonPrzejdzDalej').hide();
                      $('.buttonZamknij').show();
                }
            }
        })
    })
    $('.buttonZamknij').live('click',function(){
       $('#popup').hide();
    })
	
        
    //wymuszenie logowania dla artykułów dostępnych po zalogowaniu 
    $('.mustLogIn').click(function(){
        addMessage('Treść dostępna po zalogowaniu');
        displayLoginBox();
    })
    
    
    //wyświetlenie komunikatu z aktywacji uzytownika
    if($('.activeMessage').length>0){
         addMessage($('.activeMessage').text());
    }
    
    $('.pages .sep').click(function(){
        $('form[name=pagination] input[name=page]').val($(this).children('.value').text());
        $('form[name=pagination]').submit()
    })
    
});


function addToCart(id, nazwa, div) {
    $('.basketBox input#artId').remove();
    $('.basketBox').append('<input type="hidden" id="artId" value="'+id+'">');
    $('.basketBox select.prodFeaturesSelect').remove();
    $('.basketBox').find('input#ilosc').val('1');
    $('.basketBox h2').html(nazwa);

    $('.basketBox').find('.prodFeatures').remove();
    if(div.hasClass('prodDescButton'))
        $('.basketBox .inputArea').append($('.prodFeaturesMain').clone(true).show());
    else
        $('.basketBox .inputArea').append(div.parents('.productBox').find('.prodFeatures').clone(true).show());

    $('.basketBox').fadeIn('slow');
    $('.background').removeClass('hide');
}

function addToCartSubmit() {
    $('.basketBox').fadeOut('slow');
    $('.background').addClass('hide');
    var ilosc = $('#ilosc').val();
    var artId = $('#artId').val();
    var prevArtId = $('#prevArtId').val();
    var edit = $('#edit').val();

    if(edit == '1')
    { 
        $.post('/koszyk-usun', {
            id: prevArtId
        }, function(data){
            $.post('/koszyk-dodaj', {
                id: artId,
                ilosc: ilosc
            }, function(data){
                location.reload();
            }, "json");
        }, "json");

        return false;
    }

    $.post('/koszyk-dodaj', {
        id: artId,
        ilosc: ilosc
    }, function(data){
        $('#basket span').remove();
        $('#basket').append("<span>"+data.quantity+" szt "+data.price+" zł</span>");
    }, "json");

    return false;
}



function changeProductFeatures(id, ilosc, div)
{
    $('.basketBox').find('input#artId').remove();
    $('.basketBox').find('input#prevArtId').remove();
    $('.basketBox').find('input#edit').remove();
    $('.basketBox .inputArea .prodFeatures').remove();

    $('.basketBox').append('<input type="hidden" id="artId" value="'+id+'">');
    $('.basketBox').append('<input type="hidden" id="prevArtId" value="'+id+'">');
    $('.basketBox').append('<input type="hidden" id="edit" value="1">');
    $('.basketBox .inputArea').append(div.parents('.prodRow').find('.prodFeatures').clone(true).show());
    $('.basketBox .inputArea .prodFeatures select').val(id);

    $('.basketBox #ilosc').attr('value',ilosc);
    $('.basketBox').fadeIn('slow');
    $('.background').removeClass('hide');
    return false;
}

function showImage(id)
{ 
    if($('.priceArea').size() > 0)
    {
        $.each($($('ul.galeriaContainerBigImg input')), function(){
            if($(this).val() == id)
            {
                $('.img_main img').remove();
                $(this).parent().find('img').clone().appendTo('.img_main');
            }
        });
    }
    else
    {
        $.each($($('ul.galeriaContainerBigImg input')), function(){
            if($(this).val() == id)
            {
                $('.img_lightbox img').remove();
                $('.img_lightbox span').remove();
                $(this).parent().find('img').clone().appendTo('.img_lightbox');
                $(this).parent().find('span').clone().appendTo('.img_lightbox');
                $('.background').removeClass('hide');
                $('.img_lightbox').fadeIn('slow');
                $('#file_id').val(id);

                var width = $('.img_lightbox img').width();
                var height = $('.img_lightbox img').height();
                $('.img_lightbox').css('width', width+160+'px');
                $('.img_lightbox').css('margin', '-'+height/2+'px -'+(width/2+70)+'px');
                $('.img_lightbox .arrows').css('margin-bottom', height/2+'px');
            }
        });
    }
}



function prevImg()
{
    var id = $('#file_id').val();
    var img = $('.galeriaContainer li a#'+id).parent().prev().find('a');
    if(img.size() == 0) img = $('.galeriaContainer li:last a');
    img.click();
}

function nextImg()
{
    var id = $('#file_id').val();
    var img = $('.galeriaContainer li a#'+id).parent().next().find('a');
    if(img.size() == 0) img = $('.galeriaContainer li:eq(0) a');
    img.click();
}

function addMessage(message){
    $('#messageBox').remove();
    $('body').append('<div id="messageBox"><div class="closeWrap"><span class="closeBtn" title="zamknij" onclick="messageClose(this)">&nbsp;</span></div>'+message+'</div>');
}
function messageClose(obj){
    $(obj).parent().parent().remove();
    
}




