//// scroll gallery ////
//// by raphaelhofer.com
//// leave this lines + it's free to use for non commerical projects


scroll_gallery = {
	//--------------------------------html parsing params------------------------------------------------------------
	//params container
	parse_area: "div", 	//conf!
	parse_id: "gallery-", 	//conf!
	img_max_count: 3,	//conf!
	memo_img: new Array(),
	memo_container: new Array(),
	memo_counter: 0,

	//--------------------------------scroller params----------------------------------------------------------------
	interval: 25,
	interval_mac: 35,	//reserve
	scroll_timer: null,
	tempo: new Array(),
	tempo_factor: 3,	//conf! alt 6
	pos_x: 0,
	pos_y: 0,
	booted: new Array(),
	scroll_x: new Array(),
	scroll_y: new Array(),
	scroll_height: 105, //conf!
	scroll_left: 0, 
	scroll_right: 578, 	//width: conf!
	scroll_activ: -1,
	scroll_count: 0,
	scroll_body_id: "min_body", //conf!
	
	//--------------------------------html parsing-------------------------------------------------------------------
		
	parse_container: function() {
		//get all scroll containers
		var container = document.getElementsByTagName(scroll_gallery.parse_area);
		//check all	
		for (x=0;x<container.length;x++) {	
			//check for id
			if (container[x].getAttribute("id") && container[x].getAttribute("id").indexOf(scroll_gallery.parse_id) > -1) {
				//check if qualifies for gallery
				//get all img
				var img_count = document.getElementById(container[x].getAttribute("id")).getElementsByTagName("img").length -1;
				//check if count > min_count
				if (img_count > scroll_gallery.img_max_count) {		
					//set id memo
					scroll_gallery.memo_container[scroll_gallery.memo_counter] = container[x].getAttribute("id");
					//extract number
					scroll_gallery.memo_container[scroll_gallery.memo_counter] = scroll_gallery.memo_container[scroll_gallery.memo_counter].replace(scroll_gallery.parse_id,"");
					scroll_gallery.memo_container[scroll_gallery.memo_counter] = parseInt(scroll_gallery.memo_container[scroll_gallery.memo_counter]);
					//raise counter
					scroll_gallery.memo_counter = scroll_gallery.memo_counter + 1;
				}	
			}
		}
		//boot: parse elements
		scroll_gallery.scroll_prepare();
	},
		
	//--------------------------------scroller----------------------------------------------------------------
	
	//mouseover start	
	scroll_start: function(scrollnumber) {
		if (scroll_gallery.booted[scrollnumber] == "no") {
			scroll_gallery.scroll_activ = scrollnumber;
			//memo: tempo nach browser?
			scroll_gallery.scroll_timer = window.setInterval('scroll_gallery.scroll_mover()',scroll_gallery.interval);						
			scroll_gallery.booted[scrollnumber] = "yes";
		}
	},
	//stop on klick
	scroll_stop: function(scrollnumber) {
		//no id
		if (scrollnumber != parseInt(scrollnumber)) {
			scrollnumber = scroll_gallery.scroll_activ;
		}
		scroll_gallery.tempo[scrollnumber] = 0;
		if (scrollnumber == scroll_gallery.scroll_activ) {
			window.clearInterval(scroll_gallery.scroll_timer);
		}
		scroll_gallery.booted[scrollnumber] = "no";
	},
	scroll_mover: function() {
		//for all containers???
		//debug:alert(scroll_gallery.scroll_activ);
		if (document.getElementById) {
			//PARAM: _x:[1] ID:-1-
			//set temp vars
			var strip_width = document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-0').offsetWidth;
			scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] + scroll_gallery.tempo[scroll_gallery.scroll_activ];
			scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] + scroll_gallery.tempo[scroll_gallery.scroll_activ];
			scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] + scroll_gallery.tempo[scroll_gallery.scroll_activ];		
			//set post
			document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-0').style.left = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] + "px";
			document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-1').style.left = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] + "px";
			document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-2').style.left = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] + "px";
			//check if left to right
			if (scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] < -1*(strip_width+10)) {scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] + (3*strip_width); document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-0').style.left = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] + "px";}
			if (scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] < -1*(strip_width+10)) {scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] + (3*strip_width); document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-1').style.left = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] + "px";}
			if (scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] < -1*(strip_width+10)) {scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] + (3*strip_width); document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-2').style.left = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] + "px";}
			//check if right to left
			if (scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] > ((scroll_gallery.scroll_right-scroll_gallery.scroll_left)+10)) {scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] - (3*strip_width); document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-0').style.left = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][0] + "px";}
			if (scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] > ((scroll_gallery.scroll_right-scroll_gallery.scroll_left)+10)) {scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] - (3*strip_width); document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-1').style.left = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][1] + "px";}
			if (scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] > ((scroll_gallery.scroll_right-scroll_gallery.scroll_left)+10)) {scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] - (3*strip_width); document.getElementById('gallerystrip-'+scroll_gallery.scroll_activ+'-2').style.left = scroll_gallery.scroll_x[scroll_gallery.scroll_activ][2] + "px";}
			//kick mac for mouse pos
			if (navigator.platform.substr(0,3)=='Mac') {scroll_gallery.get_mouse_pos;}
		}	
		
	},
	scroll_prepare: function() {
		if (scroll_gallery.memo_counter > 0) {
			//boot scroller
			//set scrollercount
			scroll_gallery.scroll_count = scroll_gallery.memo_counter;
			//set scroll area x
			var obj = document.getElementById('gallerybox-'+scroll_gallery.memo_container[0]);
			scroll_gallery.scroll_left = scroll_gallery.scroll_find_pos(obj)[0];
			scroll_gallery.scroll_right = scroll_gallery.scroll_left + scroll_gallery.scroll_right;
			//set each galley
			for (x=0;x<scroll_gallery.memo_counter;x++) {				
					//get strip width
					strip_width = parseInt(document.getElementById('gallerystrip-'+scroll_gallery.memo_container[x]+'-0').offsetWidth);
					//set scroller info
					scroll_gallery.scroll_x[ scroll_gallery.memo_container[x] ] = new Array();
					scroll_gallery.scroll_x[ scroll_gallery.memo_container[x] ][0] = -1*document.getElementById('gallerystrip-'+scroll_gallery.memo_container[x]+'-0').offsetWidth;
					scroll_gallery.scroll_x[ scroll_gallery.memo_container[x] ][1] = 0;
					scroll_gallery.scroll_x[ scroll_gallery.memo_container[x] ][2] = document.getElementById('gallerystrip-'+scroll_gallery.memo_container[x]+'-0').offsetWidth;
					var obj = document.getElementById('gallerystrip-'+scroll_gallery.memo_container[x]+'-0');
					scroll_gallery.scroll_y[ scroll_gallery.memo_container[x] ] = scroll_gallery.scroll_find_pos(obj)[1];
					//get in position
					document.getElementById('gallerystrip-'+scroll_gallery.memo_container[x]+'-0').style.left = scroll_gallery.scroll_x[ scroll_gallery.memo_container[x] ][0] + "px"; //[0] to the left
					document.getElementById('gallerystrip-'+scroll_gallery.memo_container[x]+'-2').style.left = scroll_gallery.scroll_x[ scroll_gallery.memo_container[x] ][2] + "px"; //[2] to the right			
					//set scroll properties
					scroll_gallery.tempo[ scroll_gallery.memo_container[x] ] = 0;
					scroll_gallery.booted[ scroll_gallery.memo_container[x] ] = "no";								
			}
			//boot it
			scroll_gallery.boot_mouse_checker();
		}	
	},
	boot_mouse_checker: function() {
		//boot mouse sniffer
		if(document.getElementById || document.all) {
			document.onmousemove = scroll_gallery.get_mouse_pos;
		}	
	},
	get_mouse_pos: function(whatever) {
		if(document.getElementById || document.all) {
			if(document.all){
				var whatever = window.event;
			}
			scroll_gallery.pos_x = whatever.clientX; 
			scroll_gallery.pos_y = whatever.clientY;
			scroll_gallery.check_mouse_pos();
		}	
	},	
	check_mouse_pos: function() {
		//check pos vertical slot
		if (scroll_gallery.pos_x >= scroll_gallery.scroll_left && scroll_gallery.pos_x <= scroll_gallery.scroll_right) {	
			//check pos height for all containers
			for (c=0;c < scroll_gallery.scroll_count;c++) {
				//fix page scroll y offset
				if (scroll_gallery.pos_y >= (scroll_gallery.scroll_y[ scroll_gallery.memo_container[c] ] - scroll_gallery.scroll_page_offset()[1]) && scroll_gallery.pos_y <= (scroll_gallery.scroll_y[ scroll_gallery.memo_container[c] ] + scroll_gallery.scroll_height  - scroll_gallery.scroll_page_offset()[1])) {
					//speed - fix offset x + scroll offset y												
					scroll_gallery.tempo[ scroll_gallery.memo_container[c] ] = Math.cos(((scroll_gallery.pos_x-scroll_gallery.scroll_left)/(scroll_gallery.scroll_right-scroll_gallery.scroll_left))*Math.PI)*scroll_gallery.tempo_factor;
					if (scroll_gallery.tempo[ scroll_gallery.memo_container[c] ] < 1 && scroll_gallery.tempo[ scroll_gallery.memo_container[c] ] > -1) {
						scroll_gallery.tempo[ scroll_gallery.memo_container[c] ] = 0; //scroll_gallery.scroll_stop();
					}			
				}
				else {
					scroll_gallery.tempo[ scroll_gallery.memo_container[c] ] = 0; 
					scroll_gallery.scroll_stop(scroll_gallery.memo_container[c]);
				}
			}
		//reset to zero
		} else {
			for (cc=0;cc < scroll_gallery.scroll_count;cc++) {
				scroll_gallery.tempo[ scroll_gallery.memo_container[cc] ] = 0;
				scroll_gallery.scroll_stop(scroll_gallery.memo_container[cc]);
			}
		}	
	},
	scroll_find_pos: function(obj) {
		var curleft = curtop = 0;
		if (obj.offsetParent) {
			do {
				curleft += obj.offsetLeft;
				curtop += obj.offsetTop;
			} while (obj = obj.offsetParent);
		}
		return [curleft,curtop];
	},
	scroll_page_offset: function() {
		var page_offset_x;
		var page_offset_y;
		
		//page_offset_x = document.getElementById(scroll_body_id).scrollLeft;			
		//page_offset_y = document.getElementById(scroll_body_id).scrollTop;	

		if (self.pageYOffset) // all except Explorer
		{
			page_offset_x = self.pageXOffset;
			page_offset_y = self.pageYOffset;
		}
		else if (document.documentElement && document.documentElement.scrollTop)
			// Explorer 6 Strict
		{
			page_offset_x = document.documentElement.scrollLeft;
			page_offset_y = document.documentElement.scrollTop;
		}
		else if (document.body) // all other Explorers
		{
			page_offset_x = document.body.scrollLeft;
			page_offset_y = document.body.scrollTop;
		}
			
		return [page_offset_x,page_offset_y];
	}			
};
