﻿/*
 * This carousel is writen for FLASH90 BY SBZ_SOFT(c)
 * admin@sbz.co.il
 */
 
 
        function setCarouselItems(data, carousel){
            data = data.replace(/(<div[^>]*>)|(<\/div>)/ig, "");
            //data = data.replace(/(\/fotoweb)/ig, "http://212.179.112.125$1"); 
            data = data.replace(/alt=/, "title=");
            var arr = data.split("<a");
            var j=0;
            var images = new Array();
            for(var i=0; i<arr.length; i++)
                if(!arr[i].match(/<img/)) continue;
                else images[j++] = "<a" + arr[i];
            
            //if(!carousel) carousel = activateCarousel();
            //if(!carousel.size()) carousel.size(0);
            //var carousel_lastItemIndex = carousel ? carousel.size() : 0;
            //carousel.size(carousel_lastItemIndex + images.length);
            var size, w, h, st;
            for (var i=0; i<images.length; i++){
                size = images[i].match(/width=\"(\d+)px\" height=\"(\d+)px\"/);
                w = parseInt(size[1]);
                h = parseInt(size[2]);
                //w = w > h ? 115 : 70;
               // st = "style=\"width: " + w + "px; height: 75px\"";
               // images[i] = images[i].replace(/width=\"(\d+)px\" height=\"(\d+)px\"/, st);
                carousel.add(images[i], w);
            }
        }
 
        function findPos(obj) {
	        var curleft = curtop = 0;
            if(obj.offsetParent){
                do {
			        curleft += obj.offsetLeft;
			        curtop += obj.offsetTop;
			    }while(obj = obj.offsetParent);
			}
			return [curleft, curtop];
		}
 
        function carousel(_wrapper){
           var _clip = document.createElement("DIV");
           _clip.className = "clip";
           _clip.style.overflow = 'hidden';
           _wrapper.appendChild(_clip);
           var _clipDimentions = findPos(_clip);
           var nextLeft = 0;
           var els = new Array();
           var itemCount = 0;
           var max_width = 0;
           var MOVE_SPEED = 1;
           var MOVE_TIMEOUT = 30;
           var SAFE_PADDING = 0;
           this.timer = null;
           
           this.add = function(el, width){
                var div = document.createElement("DIV");
                div.className = "carousel_item";
                _clip.appendChild(div);
                div.style.left = nextLeft+"px";
                div.style.top = _clipDimentions[1] - findPos(div)[1] + "px";
                div.innerHTML = el;
                div.sbz_width = width;
                nextLeft += width;
                els[itemCount] =  div;
                itemCount++;
                max_width = max_width < width ? width : max_width;
           }
           
           
           this.animateStart = function(){
                if (this.timer != null)
                    return;
                SAFE_PADDING = max_width * 2;
                var self = this;
                _clip.onmouseover = function(){self.animateStop();};
                _clip.onmouseout = function(){self.animateStart();};
                this.timer = setInterval(function() { self.scroll(); }, MOVE_TIMEOUT);
           }
           
           this.animateStop = function(){
                if (this.timer == null)
                    return;
                clearInterval(this.timer);
                this.timer = null;
           }
           
           this.scroll = function(){
                for(var i=0; i<itemCount; i++){
                    if(parseInt(els[i].style.left) + SAFE_PADDING < 0){
                        els[i].style.left = nextLeft + "px";
                        nextLeft += els[i].sbz_width;
                    }
                    els[i].style.left = (parseInt(els[i].style.left) - MOVE_SPEED) + "px";
                }
                nextLeft -= MOVE_SPEED;
           }
        }
