$(document).ready(function(){
    Shadowbox.init();

    $('#promoted-content .photos img').click(function(){
        var $imgSrc = $(this).attr('src');
        freezePlay();
        $('#promoted-content .big-photo img:visible').attr('src', $imgSrc);
    });

	$('a[rel="external"]').click(function(){
		var $url = $(this).attr('href');
		window.open($url);
		return false;
	});
});

var timer = null;
var active = 1;
var play = true;

function updatePriority(){
    $("#flats-selected tbody tr").each(function(key,value){
        $(value).find("td:first").text(key+1);
        $(value).find("input.priority").val(key+1);
        color = "#ffffff";
        switch (key+1){
            case 1:
                color = "#B7D064";
                break;
            case 2:
                color = "#CDE970";
                break;
            case 3:
                color = "#DFFB79";
                break;
        }
        $(value).find("td").css("background-color", color);
    })
    
}

function notifyServerOnChanges()
{
    url=jQuery('#update-flat-preference-form').attr('action');
    jQuery.ajax({
        type: 'POST',
        url: url,
        data: jQuery("#save-flats-form").serialize()
    });
}

function addToSelected(id){
    if ($("#flats-selected tbody tr").length < 3){
        $flat = $("#tr-flat-"+id);
        $flat.appendTo("#flats-selected tbody");
        updateAddToSelected($flat.find("td:last button"),id);
        updatePriority();
        notifyServerOnChanges();
    }else {
        alert("Mo\u017cesz wybrać maksymalnie 3 mieszkania");
    }
}

function updateAddToSelected($last,id){
    $last.text("Usu\u0144 z wybranych");
    $last.unbind("click");
    $last.click(function(){
        removeFromSelected(id);
        return false;
    });
}



function updateRemoveFromSelected($last,id){
    $last.text("Dodaj do wybranych");
    $last.unbind("click");
    $last.click(function(){
        addToSelected(id);
        return false;
    });
}

function removeFromSelected(id){
    $flat = $("#tr-flat-"+id);
    $flat.find("td").css("background-color", "#ffffff");
    $flat.appendTo("#flats-available tbody");
    $("#flats-available tbody").prepend($flat);
    id = $flat.find("input.id").val();
    $flat.find("td:first").text(id);
    updateRemoveFromSelected($flat.find("td:last button"),id);
    updatePriority();
    notifyServerOnChanges();
}

function showPhoto(id){
    active = id*1;
    $('#promoted-content .big-photo img').each(function(index, domElement){
        var $photo = jQuery(domElement);
        if ($photo.css("display") != "none"){
            $photo.fadeOut("normal")
        }
    });

    setTimeout(function(){
        $('#photo-'+id).fadeIn("normal");
    }, 0);

    if (play == true){
        clearTimeout(timer);
        autoPlay();
    }
}


function autoPlay(){
    play = true;
    if (active < 4){
        timer = setTimeout("showPhoto(active + 1)", 4000);
    }
}

function freezePlay(){
    play = false;
    clearTimeout(timer);
}
