
var periodicalID;
var periodicalID2;
var time_out = 5000;
var time_out2 = 1000;
var total_elements = 0;
var op = "0";

// artikl:
var rc2;
var dx = 250;

window.addEvent("domready",function() {
    if ($("nav")) handleNav();
    calcTotal();
    if ($("rotator")) {
        startPeriodical();
    }
    
    if ($("rotator2")) {
        rc2 = $("rotator-container2");
        handleThumbs();
    }
    
    if ($("header-up")) {
        $("header-up").addEvents({
            "mouseover" : function() {
                hideAllHorizontal();
            }
        })
    }
})


function handleThumbs() {
    $$(".thumb-img").each(function(el) {
        el.addEvents({
            "click": function() {
                rc2.tween('margin-left',  (-1 * el.get("rel")*dx) + "px");
            }
        })
    })
}

function calcTotal() {
    total_elements = 0;
    $$(".rotator-item").each(function(el){
        total_elements ++;
    })
}



function startPeriodical() {
	periodicalID = animateRotator.periodical(time_out);
}


function pausePeriodical() {
	$clear(periodicalID);
}


function startPeriodical2() {
	periodicalID2 = showDelayed.periodical(time_out2);
}


function pausePeriodical2() {
	$clear(periodicalID2);
}



function animateRotator() {
    if (total_elements == 1) {
        op = "1";
        calcTotal();
        startPeriodical2();
    } else {
        op = "0";
    }

    $$(".rotator-item")[total_elements-1].set('tween',  {duration: time_out2, transition: 'linear'});
    $$(".rotator-item")[total_elements-1].tween('opacity',  op);
    //console.log("total_elements: " + total_elements);
    if (op!="1") total_elements --;
}


function showDelayed() {
    $$(".rotator-item")[0].set('opacity', "1");
    $$(".rotator-item")[1].set('opacity', "1");
    $$(".rotator-item")[2].set('opacity', "1");
    pausePeriodical2();
}





function handleNav() {
	$$(".horizont").each(function(el){
		el.addEvents({
			"mouseover" : function() {
				// sakrij sve ostale:
				hideAllHorizontal();

				//pokazi odabrani:
				if ($("horizont-" + el.id)) $("horizont-" + el.id).setStyle("display", "block");
			}
		})
	})

	$$(".subs").each(function(el){
		el.addEvents({
			"mouseleave" : function() {
				el.setStyle("display", "none");
			}
		})
	})

	if ($("header")) {
        $("header").addEvents({
			"mouseover" : function() {
				//hideAllHorizontal();
			}
		})
    }
}

function hideAllHorizontal() {
	$$(".subs").each(function(el){
		el.setStyle("display", "none");
	})
}

