$(document).ready(function(){
    /* positions */
	$("#UserPositionSpecializationsId").change(function() {
		$.ajax({
			url: "http://bezuradu.cz/zivotopis/get_position_checkbox/" + $(this).val(),
			cache: false,
			success: function(html) {
				$("#Positions").html(html);
                $("#Positions2").html(html);
			}
		});
	});

	$("input[rel=add-position]").live("change",  function() {
        if($("input#"+ $(this).attr("id")).attr("value") == 1) {
            $("." + "All" + $(this).attr("id")).remove();
            
            /*$("#PositionAll").append("<div class='All" + $(this).attr("id") + "'>" +
                $("label[for="+ $(this).attr("id")+"]").text() + 
                " - <a href='' rel='delete' class='All" + $(this).attr("id") + "'>Smazatsdf</a>" +
                "<input type='hidden' name='data[UserPositionAll]["+$("#UserPositionCount").val()+"][positions_id]' value='" + $(this).attr("class") + "' />" +        
                "</div>");*/

            $("table#PositionAll").append("<tr class='All" + $(this).attr("id") + "'><td class='added-first2'>" +
                $("label[for="+ $(this).attr("id")+"]:first").text() + 
                "</td><td class='added-second'><div class='right-float ared font-small'><a href='' rel='delete2' class='All" + $(this).attr("id") + "'>\{odebrat\}</a>" +
                "<input type='hidden' name='data[UserPositionAll]["+$("#UserPositionCount").val()+"][positions_id]' value='" + $(this).attr("class") + "' />" +        
                "</div></td></tr>");
            
            $("#UserPositionCount").val((parseInt($("#UserPositionCount").val()) + 1));
        } else {
            $("." + "All" + $(this).attr("id")).remove();
        }
		
		if($("input#"+ $(this).attr("id")).attr("value") == 0) $("input#"+ $(this).attr("id")).attr("value", 1);
		else $("input#"+ $(this).attr("id")).attr("value", 0);
	});
	
	$("a[rel=delete2]").live("click",  function() {
		$("tr." + $(this).attr("class")).remove();
		return false;
	});
    
    /* towns */
	$("#UserTownRegionId").change(function() {
		$.ajax({
			url: "http://bezuradu.cz/zivotopis/get_town_checkbox/" + $(this).val(),
			cache: false,
			success: function(html) {
                $("#Towns").html(html);
				$("#Towns2").html(html);
			}
		});
	});
    
	$("input[rel=add-town]").live("change",  function() {
        if($("input#"+ $(this).attr("id")).attr("value") == 1) {
            $("." + "All" + $(this).attr("id")).remove();
            
            /*("#TownAll").append("<div class='All" + $(this).attr("id") + "'>" +
                $("label[for="+ $(this).attr("id")+"]").text() + 
                " - <a href='' rel='delete' class='All" + $(this).attr("id") + "'>Smazat</a>" +
                "<input type='hidden' name='data[UserTownAll]["+$("#UserTownCount").val()+"][town_id]' value='" + $(this).attr("class") + "' />" +        
                "</div>");*/
			
            $("table#TownAll").append("<tr class='All" + $(this).attr("id") + "'><td class='added-first2'>" +
                $("label[for="+ $(this).attr("id")+"]:first").text() + 
                "</td><td class='added-second'><div class='right-float ared font-small'><a href='' rel='delete2' class='All" + $(this).attr("id") + "'>\{odebrat\}</a>" +
                "<input type='hidden' name='data[UserTownAll]["+$("#UserTownCount").val()+"][town_id]' value='" + $(this).attr("class") + "' />" +       
                "</div></td></tr>");
            
            $("#UserTownCount").val((parseInt($("#UserTownCount").val()) + 1));
        } else {
            $("." + "All" + $(this).attr("id")).remove();
        }
		
		if($("input#"+ $(this).attr("id")).attr("value") == 0) $("input#"+ $(this).attr("id")).attr("value", 1);
		else $("input#"+ $(this).attr("id")).attr("value", 0);
	});

	$("a[rel=delete]").live("click",  function() {
		$("." + $(this).attr("class")).remove();
		return false;
	});
    
	$("a[rel=show]").click(function() {
        if($("#" + $(this).attr("class")).attr("class") == "hidden") $("#" + $(this).attr("class")).attr("class", "show");
        else $("#" + $(this).attr("class")).attr("class", "hidden");

		return false;
	});
    
    $("a[rel=paging]").click(function() {
        $("form").attr("action", $(this).attr("href"));
        $("form").submit();
        return false;
    });
    
    $("#UserPositionId").keypress(function(e) {
        var newKey = $(this).val();
        
        if (e.which != 8) newKey += String.fromCharCode(e.which);
        else newKey = newKey.slice(0, -1);
        
		$.ajax({
			url: "http://bezuradu.cz/hledani/find_position/" + newKey,
			cache: false,
			success: function(html) {
                $("#SearchPositionResult").html(html);
                $("#SearchPositionResult").attr("class", "show");
			}
		});
    });
    
    $("#UserPositionId").click(function() {
		$.ajax({
			url: "http://bezuradu.cz/hledani/find_position/" + $("#UserPositionId").val(),
			cache: false,
			success: function(html) {
                $("#SearchPositionResult").html(html);
                $("#SearchPositionResult").attr("class", "show");
			}
		});
        
        return false;
    });
    
    $("html").click(function() {
        $("#SearchPositionResult").attr("class", "hidden");
    });
    
	$("a[rel=choose-position]").live("click",  function() {
        $("#UserPositionId").val($(this).text());
        $("#UserPositionIdChoose").val($(this).attr("class"));
        
		return false;
	});
    
    
    /* positions */
	$("#UserPositionLinkSpecializationsId").change(function() {
		$.ajax({
			url: "http://bezuradu.cz/zivotopis/get_position_link/" + $(this).val(),
			cache: false,
			success: function(html) {
                $("#Positions").html(html);
			}
		});
	});
	
	$("a[rel=choose-all]").click(function() { 
		$("input:checkbox").attr("checked", "checked");
        
		return false;
	});
	
	$("a[rel=choose-none]").click(function() {
		$("input:checkbox").removeAttr("checked");
        
		return false;
	});
	
	$("a.buy-submit").click(function() {
        $("form").attr("action", $(this).attr("href"));
        $("form").submit();
        
		return false;
	});
	
	$("a[rel=koupit_ajax]").live("click",  function() {
		$.ajax({
			url: "http://bezuradu.cz/firma/koupit_ajax/" + $(this).attr("id"),
			cache: false,
			success: function(html) {
				$("#MenuBasket").html(html);
				
				$.openDOMWindow({
					windowSourceID:'#cvsBoughtAddInfo',
					height: 30, 
					width: 200 
				});
		
				window.setTimeout("$.closeDOMWindow({windowSourceID:'#cvsBoughtAddInfo'})", 800, true);
			}
		});

		$(this).html("Odebrat z košíku");
		$(this).attr("rel", "odebrat_ajax");

		return false;
	});
	
	$("a[rel=odebrat_ajax]").live("click",  function() {
		$.ajax({
			url: "http://bezuradu.cz/firma/kosik_odebrat_ajax/" + $(this).attr("id"),
			cache: false,
			success: function(html) {
				$("#MenuBasket").html(html);
				
				$.openDOMWindow({
					windowSourceID:'#cvsBoughtRemoveInfo',
					height: 30, 
					width: 200 
				});
		
				window.setTimeout("$.closeDOMWindow({windowSourceID:'#cvsBoughtRemoveInfo'})", 800, true);
			}
		});
		
		$(this).html("Přidat do košíku");
		$(this).attr("rel", "koupit_ajax");
		
		return false;
	});
	
	$("a[rel=pridat_do_kosiku_ajax]").click(function() {
		$.ajax({
			type: 'POST',
			url: "http://bezuradu.cz/firma/pridat_do_kosiku_ajax/",
			data: $("form").serialize(),
			success: function(html) {
				$("#MenuBasket").html(html);
				alert("Živopisy byli přidány do košíku");
			}
		});

		return false;
	});
	
	$("a[rel=pridat_do_schranky_ajax]").click(function() {
		$.ajax({
			type: 'POST',
			url: "http://bezuradu.cz/firma/pridat_do_schranky_ajax/",
			data: $("form").serialize(),
			success: function(html) {
				alert("Živopisy byli přidány do schránky");
			}
		});

		return false;
	});

	$("a[rel=add-position-link]").live("click",  function() {
        $("#UserPositionId").val($(this).attr("class"));
        $("#UserPositionIdChoose").val($(this).attr("id"));

        $('.closeWindow').closeDOMWindow();
        
		return false;
	});
    
    $('.openWindow').openDOMWindow({ 
        eventType: 'click', 
        height: 400, 
        width: 700 
    });
    
    $('.closeWindow').closeDOMWindow({eventType:'click'});
	
	$("#datepicker").datepicker({
		showOn: 'button',
		buttonImage: '../css/cupertino/images/calendar.gif',
		buttonImageOnly: true,
		dateFormat: "yy-mm-dd",
		onSelect: function(dateText, inst) { $("input#UserNastup2").attr("checked", "checked"); }
	});
});
