function resize_image(img) {
    var max_width=180;
    var max_height=91;
    // jeśli szerokość obrazka jest większa niż dopuszczalna maksymalna szerokość
    if(img.width > max_width) {
        // obliczamy proporcje szerokość do wysokość
        factor = img.width / img.height;
        // obliczamy proporcjonalną wysokość, zaokrąglamy ją używając Math.floor();
        height = Math.floor(max_width / factor);
        // nadajemy obrazkowi nowe wymiary
        img.width = max_width;
        img.height = height;
    }
    if(img.height > max_height) {
		factor =  img.width/img.height;
		width = Math.floor(max_height * factor);
		img.width = width;
		img.height = max_height;
    }
}

function resize_image2(img) {
    var max_width=83;
    var max_height=85;
    // jeśli szerokość obrazka jest większa niż dopuszczalna maksymalna szerokość
    if(img.width > max_width) {
        // obliczamy proporcje szerokość do wysokość
        factor = img.width / img.height;
        // obliczamy proporcjonalną wysokość, zaokrąglamy ją używając Math.floor();
        height = Math.floor(max_width / factor);
        // nadajemy obrazkowi nowe wymiary
        img.width = max_width;
        img.height = height;
    }
    if(img.height > max_height) {
    factor = img.height / img.width;
    width = Math.floor(max_height / factor);
    img.width = width;
        img.height = max_height;
    }
}

function resize_image3(img) {
    var max_width=764;
    var max_height=103;
    // jeśli szerokość obrazka jest większa niż dopuszczalna maksymalna szerokość
    if(img.width > max_width) {
        // obliczamy proporcje szerokość do wysokość
        factor = img.width / img.height;
        // obliczamy proporcjonalną wysokość, zaokrąglamy ją używając Math.floor();
        height = Math.floor(max_width / factor);
        // nadajemy obrazkowi nowe wymiary
        img.width = max_width;
        img.height = height;
    }
    if(img.height > max_height) {
    factor = img.height / img.width;
    width = Math.floor(max_height / factor);
    img.width = width;
        img.height = max_height;
    }
}

function resize_image4(img) {
    var max_width=200;
    var max_height=150;
    // jeśli szerokość obrazka jest większa niż dopuszczalna maksymalna szerokość
    if(img.width > max_width) {
        // obliczamy proporcje szerokość do wysokość
        factor = img.width / img.height;
        // obliczamy proporcjonalną wysokość, zaokrąglamy ją używając Math.floor();
        height = Math.floor(max_width / factor);
        // nadajemy obrazkowi nowe wymiary
        img.width = max_width;
        img.height = height;
    }
    if(img.height > max_height) {
    factor = img.height / img.width;
    width = Math.floor(max_height / factor);
    img.width = width;
        img.height = max_height;
    }
}

function sendPoll(check,id){
    if(typeof check=='undefined'){
	return false;
    }
    $.ajax({
	url: ROOT_URL+'_ajax/poll.php',
	type: 'post',
	dataType: 'html',
	data: "field="+check+"&id="+id,
	success: function(msg){
	 $("#sonda").hide();
	 $("#sonda").html(msg);
	 $("#sonda").fadeIn('slow');
	 setCookie('sonda_'+id,id,365);
	}
    })
}

$(document).ready(function(){

	if($(".nasz-pracodawca").size()){
		$(".nasz-pracodawca").find('img').css("margin-top",function(){
			var box = $(this).parents(".nasz-pracodawca").height()/2;
			var img = $(this).height()/2;
			return box-img;
		});
	}


    $("#strefa_pracodawcy").mouseleave(function(){
        if($("#strefa_pracodawcy").hasClass('hover')){
            $("#strefa_pracodawcy").removeClass('hoverOn');
            $("#strefa_pracodawcy").addClass('hoverOff');
        }
        $(document).bind("mouseleave", function(){
            $("#drop_down_menu").hide();
            if($("#strefa_pracodawcy").hasClass('hover')){
                $("#strefa_pracodawcy").removeClass('hoverOn');
                $("#strefa_pracodawcy").addClass('hoverOff');
            }
        });
        $("#drop_down_menu").hide();
    });
    
    $("#drop_down_menu").mouseenter(function(){
        $(document).unbind("mouseenter");
        $(this).show();
        if($("#strefa_pracodawcy").hasClass('hover')){
            $("#strefa_pracodawcy").removeClass('hoverOff');
            $("#strefa_pracodawcy").addClass('hoverOn');
        }
    });
    $("#drop_down_menu").mouseleave(function(){
        $(document).bind("mouseleave", function(){
            $("#drop_down_menu").hide();
            if($("#strefa_pracodawcy").hasClass('hover')){
                $("#strefa_pracodawcy").removeClass('hoverOn');
                $("#strefa_pracodawcy").addClass('hoverOff');
            }
            
        });
        $("#drop_down_menu").hide();
        if($("#strefa_pracodawcy").hasClass('hover')){
            $("#strefa_pracodawcy").removeClass('hoverOn');
            $("#strefa_pracodawcy").addClass('hoverOff');
        }
    });
    $(document).mouseenter(function(){
        $("#drop_down_menu").hide();
        if($("#strefa_pracodawcy").hasClass('hover')){
            $("#strefa_pracodawcy").removeClass('hoverOn');
            $("#strefa_pracodawcy").addClass('hoverOff');
        }
    });

    $("#strefa_pracodawcy").mouseenter(function(){
       $("#drop_down_menu").show();
       $(document).unbind("mouseenter");
       if($("#strefa_pracodawcy").hasClass('hover')){
            $("#strefa_pracodawcy").removeClass('hoverOff');
            $("#strefa_pracodawcy").addClass('hoverOn');
        }
    });
    

    $('tr.show').mouseenter(function(){
        var id=$(this).children().children("input:first").val();
        var offset = $(this).offset();
        var height = $(this).height();
        $('#pop'+id).css('top', offset.top+height+'px');
        $('#pop'+id).show();
    });
    $('tr.show').mouseleave(function(){
        var id=$(this).children().children("input:first").val();
        $('#pop'+id).hide();
    });
    $("#dodaj_uczelnie").click(function(e){
        e.preventDefault();
        var star = '';
        if($('input[name="rodzaj_uczelni[]"]').length < 1) star = '*';
        if($('input[name="rodzaj_uczelni[]"]:last').val()=='') return;
        var toAppend='<tr><td><label style="color: #507513;">'+star+'Nazwa uczelni:</label></td><td><input class="green212" type="text" size="35" maxlength="255" value="" name="rodzaj_uczelni[]"></td><td><label style="color: #507513;">Data rozpoczęcia:</label></td><td><select class="miesiac" name="miesiac_rozpoczecia_studiow[]"><OPTION value="00" >-MM-</OPTION>';
        for(i=12;i>0;i--){
            toAppend+='<OPTION value="'+i+'" >'+i+'</OPTION>\n';
        }
        toAppend+='</select></td><td><select class="miesiac" name="rok_rozpoczecia_studiow[]"><OPTION value="0000" >-RRRR-</OPTION>';
        for(i=2020;i>1940;i--){
            toAppend+='<OPTION value="'+i+'" >'+i+'</OPTION>\n';
        }
        toAppend+='</select></td><td rowspan=2"><a href="#" onclick="usun_wyksz($(this));return false;"><img src="'+ROOT_URL+'_tpl/_img/usun.png" class="usun-wyksz" alt="Usuń"/></a></td></tr><tr><td><label style="color: #507513;">'+star+'Kierunek studiów:</label></td><td><input class="green212" type="text" size="35" maxlength="255" value="" name="kierunek_uczelni[]"></td><td><label style="color: #507513;">Data zakończenia:</label></td><td><select class="miesiac" name="miesiac_zakonczenia_studiow[]"><OPTION value="00" >-MM-</OPTION>';
        for(i=12;i>0;i--){
            toAppend+='<OPTION value="'+i+'" >'+i+'</OPTION>\n';
        }
        toAppend+='</select></td><td><select class="miesiac" name="rok_zakonczenia_studiow[]"><OPTION value="0000" >-RRRR-</OPTION>';
        for(i=2020;i>1940;i--){
            toAppend+='<OPTION value="'+i+'" >'+i+'</OPTION>\n';
        }
        toAppend+='</select></td></tr><tr><td colspan="5"><div class="line"></div></td></tr>';
        $('#wyksztalcenie_table').append(toAppend);
    });
    $("#dodaj_doswiadczenie").click(function(e){
        e.preventDefault();
        var star = '';
        if($('input[name="nazwa_firmy[]"]').length < 1) star = '*';
            
        if($('input[name="nazwa_firmy[]"]:last').val()=='') return;
        var toAppend='<tr><td><label style="color: #507513;">'+star+'Nazwa firmy:</label></td><td><input class="green212" type="text" size="35" maxlength="255" value="" name="nazwa_firmy[]"></td><td><label style="color: #507513;">Data rozpoczęcia:</label></td><td><select class="miesiac" name="miesiac_rozpoczecia_pracy[]"><OPTION value="00" >-MM-</OPTION>';
        for(i=12;i>0;i--){
            toAppend+='<OPTION value="'+i+'" >'+i+'</OPTION>\n';
        }
        toAppend+='</select></td><td><select class="miesiac" name="rok_rozpoczecia_pracy[]"><OPTION value="0000" $selected>-RRRR-</OPTION>';
        for(i=2020;i>1940;i--){
            toAppend+='<OPTION value="'+i+'" >'+i+'</OPTION>\n';
        }

        toAppend+='</select></td><td rowspan="2"><a href="#" onclick="usun_dosw($(this));return false;"><img src="'+ROOT_URL+'/_tpl/_img/usun.png" class="usun-dosw" /></a></td></tr><tr><td><label style="color: #507513;">'+star+'Stanowisko:</label></td><td><input class="green212" type="text" size="35" maxlength="255" value="" name="stanowisko[]"></td><td><label style="color: #507513;">Data zakończenia:</label></td><td><select class="miesiac" name="miesiac_zakonczenia_pracy[]"><OPTION value="0" >-MM-</OPTION>';
        for(i=12;i>0;i--){
            toAppend+='<OPTION value="'+i+'" >'+i+'</OPTION>\n';
        }
        toAppend+='</select></td><td><select class="miesiac" name="rok_zakonczenia_pracy[]"><OPTION value="0000" >-RRRR-</OPTION>';
        for(i=2020;i>1940;i--){
            toAppend+='<OPTION value="'+i+'" >'+i+'</OPTION>\n';
        }
        var ile = $('input[name="nazwa_firmy[]"]').length+1;
        var nazwa = 'zakres_obowiazkow'+ile;
        toAppend+='</select></td></tr><tr><td>'+star+'Lokalizacja:</td><td><input class="green212" type="text" size="35" maxlength="255" value="" name="lokalizacja[]"></td><td></td><td></td><td></td></tr><tr><td>'+star+'Zakres obowiązków</td><td colspan="4"><textarea name="zakres_obowiazkow'+ile+'" class="zakres_obowiazkow"></textarea></td></tr><tr><td colspan="5"><div class="line"></div></td></tr>';
        $('#doswiadczenie_table').append(toAppend);
        
        CKEDITOR.replace( nazwa );
    });
});

$(function(){
    $("#aktualnosci").tabs({
        select: function(event, ui) {
            $('#aktualnosci .tab').removeClass('tab-on');
            $('#aktualnosci .tab').addClass('tab-off');
            $('#aktualnosci .tab').each(function(){
                if($(this).attr('href') == ('#'+ui.panel.id)){
                    $(this).removeClass('tab-off');
                    $(this).addClass('tab-on');
                }
            });
        },
        fx: { opacity: 'toggle' }
    });

    $("#oferty_tygodnia").tabs({
        select: function(event, ui) {
            $('#oferty_tygodnia ul img').attr('src', ROOT_URL+'_tpl/_img/tygodnia_button_off.png');
            $('#oferty_tygodnia ul li img').each(function(){
                if($(this).attr('alt') == ui.panel.id){
                    $(this).attr('src', ROOT_URL+'_tpl/_img/tygodnia_button_on.png');
                }
            });
        },
        fx: { opacity: 'toggle' }
    });
    $("#oferty_tygodnia").tabs("rotate", 3000, false);
    $("#aktualnosci").tabs("rotate", 5000, false);

    $("#search-box #branza").selectbox();
    $("#search-box #jezyk").selectbox();
    $("#search-box #region").selectbox();
    
    
    $('#search-box .jquery-selectbox span.jquery-selectbox-currentItem').css('background', 'url("'+ROOT_URL+'_tpl/_img/branza.png") #fff no-repeat 12px 11px').css('padding-left', '30px');
    $('#search-box .jquery-selectbox').children('.jquery-selectbox-list').css('width', '250px');
    
    $('#search-box .jquery-selectbox:last span.jquery-selectbox-currentItem').css('background', 'url("'+ROOT_URL+'_tpl/_img/region.png") #fff no-repeat 12px 9px').css('padding-left', '36px');
    $('#search-box .jquery-selectbox:last').children('.jquery-selectbox-list').css('width', '167px');
    $('#search-box .jquery-selectbox:last').css('margin-right', '0px');
    
    $('#search-box .jquery-selectbox:first span.jquery-selectbox-currentItem').css('margin-right', '0px').css('background', 'url("'+ROOT_URL+'_tpl/_img/wybor_jezyka.png") #fff no-repeat 12px 12px').css('padding-left', '31px');
    $('#search-box .jquery-selectbox:first').children('.jquery-selectbox-list').css('width', '142px');
    
    
    
    /*wyszukiwarka pracodawcy*/
    
    $("#search-box-pracodawca #poziom_wyksztalcenia").selectbox();
    $("#search-box-pracodawca #jezyk").selectbox();
    $("#search-box-pracodawca #poziom_jezyka").selectbox();
    $("#search-box-pracodawca #region").selectbox();
    $("#search-box-pracodawca #specjalizacja").selectbox();
    $("#search-box-pracodawca .jquery-selectbox:last span.jquery-selectbox-currentItem").css('background', 'url("'+ROOT_URL+'_tpl/_img/region.png") no-repeat 12px 9px').css('padding-left', '40px');
    
    /*wyszukiwarka pracodawcy end*/
    
    $("#ogloszenia #order").selectbox();

    $('#keyword').focus(function(){
       if($(this).val()=='Wpisz słowo kluczowe') $(this).val(''); 
    });
    $('#keyword').blur(function(){
       if($(this).val()=='') $(this).val('Wpisz słowo kluczowe'); 
    });

    $('#lupa').click(function(e){
        e.preventDefault();
        if($('#keyword').val()=='Wpisz słowo kluczowe') $('#keyword').val('');
        $('#search-box form').submit();
    });
    
    $('#search-pracodawca-big #lupa').click(function(e){
        e.preventDefault();
        $('#search-pracodawca-big form').submit();
    });

    $('#praca_linki').cycle({
        fx:'none',
        //timeout: 2000,
        //easing: 'bounceout',
        delay:  1000
    });

    $(".usun-wyksz").click(function(){
        usun_wyksz($(this));
    });
    $(".usun-dosw").click(function(){
        usun_dosw($(this));
    });
    
    $("img.relokacja").mouseenter(function(){
       var offset = $(this).offset();
       var height = $(this).height();
       $('div.relokacja').css('top', offset.top-25+'px');
       $("div.relokacja").css('left', offset.left-$("div.relokacja").width()+5+'px');
       $('div.relokacja').show();
       $(document).unbind("mouseenter");
    });
    $("img.relokacja").mouseleave(function(){
        $(document).bind("mouseleave", function(){
            $("div.relokacja").hide();
        });
        $("div.relokacja").hide();
    });
    $("img.has-cv").mouseenter(function(){
       var offset = $(this).offset();
       var height = $(this).height();
       $('div.has-cv').css('top', offset.top-24+'px');
       $("div.has-cv").css('left', offset.left-$("div.has-cv").width()+20+'px');
       $('div.has-cv').show();
       $(document).unbind("mouseenter");
    });
    $("div.has-cv").mouseenter(function(){
       $(this).show();
       $(document).unbind("mouseenter");
    });
    $("div.has-cv").mouseleave(function(){
       $(document).bind("mouseleave", function(){
            $("div.has-cv").hide();
        });
        $(this).hide();
    });
    $("img.has-cv").mouseleave(function(){
        $(document).bind("mouseleave", function(){
            $("div.has-cv").hide();
        });
        $("div.has-cv").hide();
    });
    
    $("#sondasubmit").click(function(e){
        e.preventDefault();
        var id = $('#sonda input[name="sonda_id"]').val();
        var checked = $('#sonda input:checked').val();
        sendPoll(checked, id);
    });
    
    //var fontEl=document.getElementsByTagName("font");
    var fontEl=$("#center-right font");
    if(fontEl.length < 1) fontEl=$("#center font");
    for(var i=0;i<fontEl.length;i++) {
        var f = fontEl[i];
        if(f.size){
            if (f.size == 1) {
                f.style.fontSize = 11+'px';
                f.style.lineHeight = 14+'px';
            } else if (f.size == 2) {
                f.style.fontSize = 14+'px';
                f.style.lineHeight = 16+'px';
            } else if (f.size == 3) {
                f.style.fontSize = 18+'px';
                f.style.lineHeight = 20+'px';
            } else if (f.size == 4) {
                f.style.fontSize = 15+'px';
            } else if (f.size == 5) {
                f.style.fontSize = 20+'px';
            } else if (f.size == 6) {
                f.style.fontSize = 25+'px';
            }
        }
        if(f.face)
            f.style.fontFamily=f.face;
        if(f.color)
            f.style.color=f.color;
    }

});

function usun_dosw(ptr){
    var items = ptr.parent().parent().siblings();
    var ile = items.length-4;
    var name='';
    items.each(function(ind, tmp){
        if(ind >=ile){
            if((name=$(this).children().children("textarea").attr('name'))!=''){
                if (CKEDITOR.instances[name]) { delete CKEDITOR.instances[name] }
            }
            $(this).remove();
        }
    });
    ptr.parent().parent().remove();
}

function usun_wyksz(ptr){
    var items = ptr.parent().parent().siblings();
    var ile = items.length-2;
    var name = '';
    items.each(function(ind, tmp){
        if(ind >=ile) $(this).remove();
    });
    ptr.parent().parent().remove();
}
