var timer;
var popup;
var trigger;
var timeoutToShow = 500;
var timeoutToHide = 700;
function showPopup(popup,trigger) {
 t = trigger.position().top,
 p = popup.height(),
 s = $(window).scrollTop(),
 w = $(window).height(),
 top_pos = (t + p) > (s + w) ? (s + w - p) : t - 10;
 
 popup.css({top: top_pos, left: trigger.position().left + 50})
 $(popup).fadeIn(300);
}
function showAskForm() {
    $('#price_form,#observe_form').hide();$('#question_form').toggle();
}
function showProposalForm() {
    $('#question_form,#observe_form').hide();$('#price_form').toggle();
}
function showObserveForm() {
    $('#question_form,#price_form').hide();$('#observe_form').toggle();
}
function showNotLoggedMsg() {
    $('#notLoggedInfo').fadeIn();
}
function showReservationForm() {
    $('#reservation_form').fadeIn('slow');
}
function showBuyForm() {
    $('.itemOptionsKup').addClass('itemOptionsKupH');
    $('#buy_form').fadeIn('slow');
}


function returnEl(id){return document.getElementById(id);}

function checkAddForm(ver) {
    wyslij = 1;
    if(ver=='add') {
        if(!(returnEl('_parameter_id_19_occur_0').value)) {
            returnEl('_parameter_id_19_occur_0').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(!(returnEl('_parameter_id_2_occur_0').value)) {
            returnEl('_parameter_id_2_occur_0').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(!(returnEl('_parameter_id_21_occur_0').value)) {
            returnEl('_parameter_id_21_occur_0').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(!(returnEl('desc').value)) {
            returnEl('desc').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(returnEl('_parameter_id_6_occur_1').value == 'brak') {
            returnEl('_parameter_id_6_occur_1').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(returnEl('_parameter_id_7_occur_1').value == 'brak') {
            returnEl('_parameter_id_7_occur_1').style.border  = '1px solid red';
            wyslij = 0;
        }
        
    }
    
    if(ver=='edit') {
        if(!(returnEl('_parameter_id_19_occur_0').value)) {
            returnEl('_parameter_id_19_occur_0').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(!(returnEl('_parameter_id_2_occur_0').value)) {
            returnEl('_parameter_id_2_occur_0').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(!(returnEl('_parameter_id_21_occur_0').value)) {
            returnEl('_parameter_id_21_occur_0').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(!(returnEl('desc').value)) {
            returnEl('desc').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(returnEl('_parameter_id_6_occur_1').value == 'brak') {
            returnEl('_parameter_id_6_occur_1').style.border  = '1px solid red';
            wyslij = 0;
        }
        if(returnEl('_parameter_id_7_occur_1').value == 'brak') {
            returnEl('_parameter_id_7_occur_1').style.border  = '1px solid red';
            wyslij = 0;
        }
        
    }
    

    if(wyslij == 1) 
        returnEl(ver).sent();
    else {
        alert('Należy poprawić błędy występujące w formularzu.');
        return false;
    }
}

$(document).ready(function() {
    $('h2.title.bar1, h2.title.bar2').each(function() {$(this).append('<span class="left" />').append('<span class="right" />') });
    $('.box1,.contentBorder').each(function() {$(this).append('<span class="bl" />').append('<span class="br" />');});
    // wspólne ready/resize
    $('h2.bar1').each(function() {$(this).width($(this).parent().width() - 30) });
    //$('#content > div').each(function() { $(this).equalHeights() });
    
   // $('#simpleSearchContainer').width($('#simpleSearchContainer').width() + 55);
    
    $('.text-shadow1').textShadow();
    
    $(':text').addClass('inputText');
    $(':password').addClass('inputPassword');
    $(':radio').addClass('inputRadio');
    $(':checkbox').addClass('inputCheckbox');
    $(':submit').addClass('inputSubmit');
    $(':reset').addClass('inputReset');
    $(':button').addClass('inputButton');
    //$('#pointMap').each(function() {$(this).width($(this).parent().width()) });;
    $('#pointMapSize').click(function() { $('#pointMap').css('height','500px'); $(this).remove(); return false; });
    
    //$('img.categoryImage').tipTip();
    //$('img.categoryImage').each(function() {alert($(this).attr('alt'))});
     $('div.categoryItem').hover(function() { $(this).addClass('categoryItemHover')},function() { $(this).removeClass('categoryItemHover')});
     $('img.categoryImage').each(function() { 
        $pt = parseInt(($(this).parent().parent().height() - $(this).height())/2);
        $(this).parent().parent().css('padding-top', $pt+'px' );
        $(this).parent().parent().css('height', ($(this).parent().parent().height() - $pt)+'px' );
        
     //console.log('rodzic / obraz '+ $(this).parent().height() +' / '+ $(this).height() + 'margin ' + parseInt(($(this).parent().height() - $(this).height())/2) +'px');
     });
     $('img.categoryImage').each(function() {        
        var trigger = $(this);
        var popup = $(this).parent().next('div.cip');
        trigger.bind('mouseenter',function() { 
            $('div.cip').hide();
            clearTimeout(timer);
            timer = setTimeout(function() { showPopup(popup,trigger);},timeoutToShow);
        }).bind('mouseleave',function() {
            clearTimeout(timer);
            timer = setTimeout(function() {popup.fadeOut(400);},timeoutToHide);
        });
        popup.bind('mouseenter',function() { 
            clearTimeout(timer);
        }).bind('mouseleave',function() {
            clearTimeout(timer);
            timer = setTimeout(function() {popup.fadeOut(400);},timeoutToHide);
        });
        });
    
    $('.cipii').click(function() {
        $img = $(this).attr('rel');
        $(this).parent('div').prev('a.categoryImage2').css('background-image',"url('" + $img + "')");
    });
    
    $('#simpleSearch1 input,#simpleSearch2 .inputText').hover(function() {$(this).addClass('inputTextHover')},function() {$(this).removeClass('inputTextHover')});

    $('a.fb').fancybox();
    $('a.tb').click(function() { 
            $medium = $(this).attr('href');
            $big = $(this).attr('rel');
            $('a#bigPhoto').attr('href',$big);
            $('a#bigPhoto').children('img').attr('src',$medium);
            return false;
    });
    $("a[rel=bigPhotos]").fancybox();
    
    $("select.dictionary[multiple=multiple]").multiselect({selectedList: 2, selectedText: '# wybranych', checkAllText: 'zaznacz wszystko', uncheckAllText: 'odznacz wszystko', minWidth: 340});

    $('#ie6dieclose').click(function() { $('#ie6die').fadeOut(800); });

});


function resizeaction() {
    //alert("I'm done resizing for the moment");
     $('h2.bar1').each(function() {$(this).width($(this).parent().width() - 30) });
};

var resizeTimer = null;
$(window).bind('resize', function() {
    if (resizeTimer) clearTimeout(resizeTimer);
    resizeTimer = setTimeout(resizeaction, 100);
});

//$(window).resize(function() {//    $('h2.bar1').each(function() {$(this).width($(this).parent().width() - 30) });})

