﻿var _divanSelMaxWidth = 140;
var _divanSelMinWidth = 100;
var _canvasReloadTimeout = 3000;

$(document).ready(function() {

    init_divan();
});


function init_divan() {
    init_events();
    setTimeout(canvasLoadTimer, 1500);
    bedMouseTimer();
}


function init_events() {
    $("#canvas").load(

	        function() {

	            if ($("#canvas").css("display") == "none") {
	                $("#canvas").css("display", "block");
	            }

	            var divOffset = document.getElementById("divan").offsetLeft + (document.getElementById("divan").offsetWidth / 2);
	            this.style.left = (divOffset - this.offsetWidth / 2) + "px";
	            this.style.top = 20 + "px";
	            setTimeout(canvasLoadTimer, _canvasReloadTimeout);


	            $("#canvas").css("z-index", "1");

	        }
	    );

    $(".bedSelector").mouseover
	    (
	        function() {
	            $("#" + this.id).attr("over", '1');
	        }
        );

    $(".bedSelector").mouseout
        (
            function() {
                $("#" + this.id).attr("over", '0');
            }
	    );
}

function bedMouseTimer() {

    var elems = $(".bedSelector[over='1']");
    for (var i = 0; i < elems.length; i++) {
        var elem = elems[i];
        if (elem.offsetWidth < _divanSelMaxWidth) {
            var a = (elem.offsetWidth - _divanSelMinWidth) + 0.0000000001;
            var b = (_divanSelMaxWidth - _divanSelMinWidth) + 0.0000000001;
            var position = a / b;
            elem.style.margin = Math.floor(20 * (1 - position)) + "px 0px 0px 0px";
            elem.style.width = (elem.offsetWidth + 5) + "px";
        }

    }

    var elems = $(".bedSelector[over='0']");
    for (var i = 0; i < elems.length; i++) {
        var elem = elems[i];
        if (elem.offsetWidth > _divanSelMinWidth) {
            var a = (elem.offsetWidth - _divanSelMinWidth) + 0.0000000001;
            var b = (_divanSelMaxWidth - _divanSelMinWidth) + 0.0000000001;
            var position = a / b;
            elem.style.margin = 20 * (1 - position) + "px 0px 0px 0px";

            elem.style.width = (elem.offsetWidth - 5) + "px";
        }
    }
    setTimeout(bedMouseTimer, 25);
}

var _globalCounter = 0;

function canvasLoadTimer() {
    $("#canvas").attr("src", "CanvasPreview.aspx?i=" + _globalCounter++);
}


function setDivan(img) {
    $("#divan").attr("src", "canvaspreview/interiers/" + img);
}
	