// JavaScript Document
var height_increase=30;
var width_increase=10;
var animation_time=150;
var speed=1;
var movement='right';
var animate=true;

var total_images = 0;
var loaded_images = 0;
var slide_mid = undefined;
var original_ul = 0;
var copy_ul_left = undefined;
var copy_ul_right = undefined;
var total_li_width=0;
var image_width_arr=Array();
var image_height_arr=Array();
var j = jQuery.noConflict();
joriginal='';
jcopy='';
jall='';
var first='original';

j(document).ready(function(e) {
	actual_speed=speed;
	
	j('#scroll_slider li').each(function(index, element) {
        total_images++;
    });
	
	j('scroll_slider img').each(function(index, element) {
        j(this).attr('border','0');
    });
	
	function loaded(){
		loaded_images += 1;
		//if (loaded_images == total_images){
			start_placing();
			save_all_img_attr();
			slide_mid = j('#ImageScrollerDiv').position().left + (j('#ImageScrollerDiv').width()/2);
			setInterval(animate_slider,10);
		//}
	}
	
	if (j.browser.msie){
		document.onload=loaded();
	}
	else{
		j('img').load(function(){
			//console.log(j(this).width())
			loaded_images += 1;
			if (loaded_images == total_images){
				start_placing();
				save_all_img_attr();
				slide_mid = j('#ImageScrollerDiv').position().left + (j('#ImageScrollerDiv').width()/2);
				setInterval(animate_slider,10);
			}
		})
	}
	
	function start_placing() {
		var i_x = 0;
		j('#ImageScrollerDiv ul li').each(function(){
			jli = j(this);
			jli.css('display','block');
			jimg = jli.find('img');
			jli.css('width',jimg.width());
			total_li_width+=j(this).width();
			
			if (j(this).width()==0)
				j(this).width(140);
			jcopy+='<li id="copy">'+j(this).html()+'</li>';
		});
		margin_right=parseInt(j('#scroll_slider li').css('margin-right'));
		total_margin=margin_right*total_images;
		total_li_width+=total_margin;
		//j('#scroll_slider').width(total_li_width);
		joriginal=j('#scroll_slider').html();
		jcopy=joriginal.replace(/<li /g,'<li id="copy" ');
		j('#scroll_slider').append(jcopy);
		//j('#ImageScrollerDiv').append(j('#ImageScrollerDiv').html());
		
		copy_ul_left = j('#scroll_slider').position().left+total_li_width;
		copy_ul_left_org = copy_ul_left;
		copy_ul_right = copy_ul_left+total_li_width;
		jall=joriginal+jcopy;
		
		/*console.log("Original: "+joriginal);
		console.log("Copy: "+jcopy);
		console.log("all: "+jall);
		console.log("Copy_ul_left: "+copy_ul_left);
		console.log("copy_ul_right: "+copy_ul_right);*/
	}
	
	j('#ImageScrollerDiv').mousemove(function(e) {
		if (animate===true){
			var mouse_pos=e.pageX;
			if (mouse_pos==slide_mid)		movement='stop';
			else if (mouse_pos>slide_mid)	movement='left';
			else							movement='right';
			//speed=(mouse_pos-slide_mid)*0.026;
			speed=(mouse_pos-slide_mid)*0.0096;
			if (speed<0)
				speed*=-1;
				
		}
    });
	
	j('#ImageScrollerDiv').mouseleave(function(e){
		//speed=actual_speed;
		slow_speed=setInterval(slowDown,100);
	});
	
	function slowDown(){
		//alert('here');
		speed-=0.1;
		if (speed<=actual_speed){
			speed=actual_speed;
			clearInterval(slow_speed);
		}
	}

	j('#scroll_slider img').live('mouseenter',function(e) {
		//alert(j(this).height()+height_increase);
		j(this).animate({
			height: j(this).height()+height_increase,
			width: j(this).width()+width_increase
		},animation_time);
    })
	j('#scroll_slider img').live("mouseleave",function(e) {
		j(this).animate({
			height: image_height_arr[j(this).attr('src')],
			width: image_width_arr[j(this).attr('src')]
		},animation_time);
    })
});


function animate_slider(){
	jslider = j('#scroll_slider');
	switch(movement){
		case 'left':
			jslider.css('left',jslider.position().left - speed);
			copy_ul_left-=speed;
			//console.log(copy_ul_left);
			if (j('#scroll_slider').position().left<-total_li_width){place_copy('append',copy_ul_left);}
		break;
		case 'right':
			jslider.css('left',jslider.position().left + speed)
			if (j('#scroll_slider').position().left>0){
				place_copy('prepend');
			}
		break;
		case 'stop':
			// STOP movement
		break;
	}
}

function place_copy(type,left){
	if (type=='append'){
		if (first=='original'){
			jall=jall.replace(joriginal,'');
			//jall.append(joriginal);
			jall+=joriginal;
			
			//console.log(jall);
			first='copy';
		}
		else{
			jall=jall.replace(jcopy,'');
			jall+=jcopy;
			first='original';
		}
		j('#scroll_slider').html(jall);
		j('#scroll_slider').css('left',0);
		copy_ul_left = copy_ul_left_org;
	}
	else{
		if (first=='original'){
			jall=jall.replace(jcopy,'');
			jall=jcopy+jall;
			first='copy';
		}
		else{
			jall=jall.replace(joriginal,'');
			jall=joriginal+jall;
			first='original';
		}
		j('#scroll_slider').html(jall);
		j('#scroll_slider').css('left',0-total_li_width);
	}
	
	//console.log(j('#scroll_slider').html());
}

function save_all_img_attr(){
	j('#scroll_slider').find('li img').each(function(index, element) {
        image_width_arr[j(this).attr('src')]=j(this).width();
		image_height_arr[j(this).attr('src')]=j(this).height();
	});
}
