/* 
 * imp_carousel javascript.
 * (c) 2009 Mykhailo Brusak <info@impulsis.com>
 */

var boxesArr = new Object();
var stop = 0;


function init(showCount,rootId,scrollDelay,inColumn){

    boxesArr[rootId] = new Object();
    if ($("#"+rootId+" .block").length && inColumn)
    	motionClass = " .block";
    else motionClass = " .box";
    boxesArr[rootId]["elCount"] = $("#"+rootId+motionClass).length;
    boxesArr[rootId]["showCount"] = showCount;
    boxesArr[rootId]["boxWidth"] = 100;//$("#"+rootId+" .box").width();
    boxesArr[rootId]["speed"] = -2;
    boxesArr[rootId]["items"] = new Object();
    boxesArr[rootId]["scrollDelay"] = scrollDelay;
	if ($("#"+rootId+" .titles").length)
		boxesArr[rootId]["titles"] = 1;

    $("#"+rootId+" .longDiv").css("width",boxesArr[rootId]["elCount"]*boxesArr[rootId]["boxWidth"]);
    $("#"+rootId+" .carrusel_content").css("width",boxesArr[rootId]["boxWidth"]*showCount);
    if (motionClass == " .block"){
    	$("#"+rootId+" .block").css("width",boxesArr[rootId]["boxWidth"]);
    	var boxHeight = inColumn*160;//$("#"+rootId+" .box").outerHeight(true);
    	$("#"+rootId+" .block").css("height",boxHeight+Math.round(boxHeight*0.1));
    	$("#"+rootId		  ).css("height",boxHeight+Math.round(boxHeight*0.1));
    	$("#"+rootId+" .carrusel_content").css("height",boxHeight+Math.round(boxHeight*0.1));
    }

    var items = boxesArr[rootId]["items"];
    var boxWidth = boxesArr[rootId]["boxWidth"];
    $("#"+rootId+motionClass).each(function(i){
        $(this).css({"left": boxWidth*i, "top": 0, "position":"absolute"});
        items[i] = this;
    });   
    $("#"+rootId+" .box").bind("mouseover click",rootId,function(event){
        if (boxesArr[event.data]){
        	if (event.type=="click"){
        		boxesArr[event.data]["stop"] = 2;
        		$("#"+rootId+" .box").find(".item_hover").css("display","");
        		$(event.currentTarget).find(".item_hover").css("display","block");
        	}
        	else{
				if (!boxesArr[event.data]["stop"]) boxesArr[event.data]["stop"] = 1;
				if (boxesArr[event.data]["titles"]){
					index = $("#"+rootId+" .box").index(event.currentTarget);
					$("#"+rootId+" .title").removeClass("act").get(index).className += " act";
				}
			}
        }
    }).bind("mouseout",rootId,function(event){
        if (boxesArr[event.data] && boxesArr[event.data]["stop"]!=2)
            boxesArr[event.data]["stop"] = 0;
    });
        
    

    $("#"+rootId+" .carrusel_left a").bind("mouseover",{rootId: rootId, direction: -1},event_mouseOver)
    .bind("mouseout",rootId,function(event){
        if (boxesArr[event.data]){
            boxesArr[event.data]["speed"] = -2;
            boxesArr[event.data]["userScroll"] = 0;
        }
    });

    $("#"+rootId+" .carrusel_right a").bind("mouseover",{rootId: rootId, direction: 1},event_mouseOver)
    .bind("mouseout",rootId,function(event){
        if (boxesArr[event.data]){
            boxesArr[event.data]["speed"] = 2;
            boxesArr[event.data]["userScroll"] = 0;
        }
    });

    boxesArr[rootId]["loop"] = setTimeout("callSlide('"+rootId+"')",boxesArr[rootId]["scrollDelay"]);
}

function callSlide(rootId){
    if (stop) return;
    if (boxesArr && boxesArr[rootId]){
    	var block = boxesArr[rootId];
    	var blockFinished = 0;
		var titleHighlight = 0;

    	if (!block["stop"]){
        	jQuery.each(block["items"],function(i,box){
	            //$box = $(box);
	            //var offset = parseInt($box.css("left"));
				var offset = parseInt(box.style.left);
	            if (block["speed"]<0 && offset<=((-block["boxWidth"])-block["speed"])){
	                offset += block["elCount"]*block["boxWidth"];
	                if (!block["userScroll"])
	                	blockFinished = 1;
	            }
	            
	            if (block["speed"]>0 && offset>=((block["elCount"]-1)*block["boxWidth"]-block["speed"])){
				offset -= block["elCount"]*block["boxWidth"];
	                if (!block["userScroll"])
	                	blockFinished = 1;
	            
	            }

				absOffset = Math.abs(offset);
				if (block["speed"]<0 && absOffset%block["boxWidth"] <= -block["speed"] && absOffset%block["boxWidth"]){ //
					speed = -(absOffset%block["boxWidth"]);
				}
				else if (block["speed"]>0 && absOffset%block["boxWidth"] >= block["boxWidth"]-block["speed"] && absOffset%block["boxWidth"]){ // 
					speed = block["boxWidth"]-absOffset%block["boxWidth"];
				}
				else speed = block["speed"];

				box.style.left = offset+speed+'px';
				if (block["titles"] && (offset+block["speed"]>=-10 && offset+block["speed"]<=10)){
					titleHighlight = box;
				}
	        });
        }
		
		if (titleHighlight && block["titles"]){
			index = $("#"+rootId+" .box").index(titleHighlight);
			$("#"+rootId+" .title").removeClass("act").get(index).className += " act";
		}
				
        if (blockFinished){
        	block["loop"] = setTimeout("callSlide('"+rootId+"')",boxesArr[rootId]["scrollDelay"]);
		}else block["loop"] = setTimeout("callSlide('"+rootId+"')",18);
    }
}


function event_mouseOver(event){
    if (boxesArr && boxesArr[event.data.rootId]){
    	boxesArr[event.data.rootId]["stop"] = 0;
        boxesArr[event.data.rootId]["speed"] = 2*event.data.direction;
        boxesArr[event.data.rootId]["userScroll"] = 1;
        clearTimeout(boxesArr[event.data.rootId]["loop"]);
        callSlide(event.data.rootId);
    }
}