window.onload = function() {
	total_offset = document.getElementsByClassName('slideitem').length * 91;
	canclick = true;
	document.getElementsByClassName('leftar')[0].addClassName('active');
	document.getElementsByClassName('rightar')[0].addClassName('active');
	
	collect.init();
	

	function makemepop(el) {
		//console.log(findPos_howto(el));
		
		showdivs = $$('.showoffdiv');
		for (i=0; i<showdivs.length; i++) {
			showdivs[i].remove();
		}
		
		mypos = findPos_howto(el);
		daspan = el.childNodes;
		//console.log(daspan);
		for (var i=0; i<daspan.length; i++) {
			if (daspan[i].tagName == 'SPAN') {
				//console.log('gotit');
				toshow = daspan[i];
			}
		}
		
		//console.log(daspan);
		newdiv = document.createElement('div');
		newdiv.style.width = 'auto';
		newdiv.style.height = 'auto';
		newdiv.style.position = 'absolute';
		newdiv.style.left = mypos[0]+'px';
		newdiv.style.top = mypos[1]-50+'px';
		$(newdiv).addClassName('showoffdiv');
		$$('body')[0].appendChild(newdiv);
		newdiv.appendChild(toshow.cloneNode(true));
		bind_off_pop();
		
		newdiv.onclick = function() {
			movemefromclick(el);
			showdivs = $$('.showoffdiv');
			for (i=0; i<showdivs.length; i++) {
				showdivs[i].remove();
			}

		}
		
	
	}

	smallpopups = $$('.slideitem img');
	
		for (var i = 0; i < smallpopups.length; i++) {
			smallpopups[i].onmouseover = function() { makemepop(this.parentNode.parentNode)};
		}
	
	function findPos_howto(obj) {
		return [findPosX(obj),findPosY(obj)];
	}
	
		 function findPosX(obj)
	  {
		var curleft = 0;
		if(obj.offsetParent)
			while(1) 
			{
			  curleft += obj.offsetLeft;
			  if(!obj.offsetParent)
				break;
			  obj = obj.offsetParent;
			}
		else if(obj.x)
			curleft += obj.x;
		return curleft;
	  }
	
	  function findPosY(obj)
	  {
		var curtop = 0;
		if(obj.offsetParent)
			while(1)
			{
			  curtop += obj.offsetTop;
			  if(!obj.offsetParent)
				break;
			  obj = obj.offsetParent;
			}
		else if(obj.y)
			curtop += obj.y;
		return curtop;
	  }

	
	if (smallpopups.length < 7) { /*Woot This number is MAGIC bitches!*/
		document.getElementsByClassName('leftar')[0].style.display = 'none';
		document.getElementsByClassName('rightar')[0].style.display = 'none';
		$$('.ulcont')[0].style.margin = '0';
		$$('.howto_slide_nav')[0].style.marginLeft = '50px';
	}
	
	
		
	function bind_off_pop() {
		$$('.showoffdiv')[0].onmouseout = function() {
			showdivs = $$('.showoffdiv');
			for (i=0; i<showdivs.length; i++) {
				
				showdivs[i].remove();
			
			}
		}
	}
	
	var nextSlideLinks = document.getElementsByClassName("next_slide");

	for (var i = 0; i < nextSlideLinks.length; i++) {
		var el = nextSlideLinks[i];
		el.onclick = function() {
			//console.log('nxt clicked');
			nowslide = document.getElementsByClassName('current_slide')[0].id;

			//console.log(nowslide);
			nownumslide = nowslide.split('_')[2];
			//console.log(nownumslide);
			nownumslide++;
			//console.log('nownumslide setto'+nownumslide);
	
			//check if the next slide exists
			if (($$('#display_slide_'+nownumslide)).length != 1) {
				//console.log('having to intterate');
				//if it doesnt, we iterate until we find one that does
				while($$('#display_slide_'+nownumslide).length != 1) {
					nownumslide++;
					//console.log('incto'+nownumslide);
				}
			}
			//then we subtract one, becuase ChangeSlide wants the slide coming from
			nownumslide = nownumslide-1;

			//console.log('changing slides');
			changeSlide('display_slide_'+nownumslide, 'next');
			
			//console.log('nowslide is '+nowslide);
			gotoslide = nowslide.split('_')[2];
			gotoslide = gotoslide-4;
			
			if ($('imgmvr').style.left.split('p')[0] == '') {
				curpos = 0;
			}
			else {
				curpos = $('imgmvr').style.left.split('p')[0];
			}
			
			//console.log('requesting the '+gotoslide+'th item in the carousel');
			if (smallpopups.length > 6) { 
				if (gotoslide > 0 && curpos >= -((gotoslide)*91)) {
				  $('imgmvr').style.left = -((gotoslide)*91)+'px';
				  $('imgmvr').style.position = 'relative';
				  
				}
				else {
					//console.log('no need to move this shit');
				}
			}
			
			
			
			return false;
		}
		
		el.onMouseOver = function() {
			//console.log(this.id);
			
		}
	}

	function movemefromclick(guy) {
		var thisslide = guy.id;
		var thisslide = thisslide.split('_')[2];
		displayActiveSlide('display_slide_'+thisslide, false);
		
		setUrlToSlide(thisslide);
			
		
		smallSlides = document.getElementsByClassName('slideitem');	
		for (var i = 0; i < smallSlides.length; i++) {
			$(smallSlides[i]).removeClassName('current');
		}
		
		$(guy).addClassName('current');
		
		return false;

	
	}


	smallSlides = document.getElementsByClassName('slideitem');	
	for (var i = 0; i < smallSlides.length; i++) {
		var sslide = smallSlides[i];
		sslide.onclick = function() {
			movemefromclick(this);			
			
			
		}
		
		var smallimg = $(sslide).getElementsByTagName('a')[0].getElementsByTagName('img')[0].src;
		$(sslide).getElementsByTagName('a')[1].href = '#';
		$(sslide).getElementsByTagName('a')[1].onclick = 'return false;';
		smallimg = smallimg.split('_s.jpg')[0] + '_m.jpg';
		
		var newimg = document.createElement('img');
		newimg.src = smallimg;
		
		var newspan = document.createElement('span');
		$(sslide).appendChild(newspan);
		var newp = document.createElement('p');
		newp.innerHTML = $(sslide).getElementsByClassName('textlink')[0].innerHTML;
		newspan.appendChild(newimg);
		newspan.appendChild(newp);
		$(newimg).addClassName('ms-global-shadow-large-thumb');
		$(newspan).addClassName('popup');
	
	}
	


	gotoslide = document.location.hash.split('_')[1];
//	gotoslide = nowslide.split('_')[2];
	gotoslide = gotoslide-5; //this is one more than the 4 above because we already subtracted one above and need 2 here
	
	if ($('imgmvr').style.left.split('p')[0] == '') {
		curpos = 0;
	}
	else {
		curpos = $('imgmvr').style.left.split('p')[0];
	}
	
	//console.log('requesting the '+gotoslide+'th item in the carousel');
	if (smallpopups.length > 6) { 
		if (gotoslide > 0 && curpos >= -((gotoslide)*91)) {
		  $('imgmvr').style.left = -((gotoslide-1)*91)+'px';
		  $('imgmvr').style.position = 'relative';
		  if (($('imgmvr').style.left.split('p')[0] == -total_offset+(91*6))) {
					document.getElementsByClassName('rightar')[0].removeClassName('active');
				  }
		}
	}
	
	$$('.next_slide')[$$('.next_slide').length-1].style.display='none';

	ratings.toggle();
}



function move_step_right() {
	if (canclick) {
		if (($('imgmvr').style.left.split('p')[0] > -total_offset+(91*6))) {
			canclick = false;
			Effect.MoveBy( 'imgmvr', 0, -91, {duration:1, afterFinish: function() {canclick = true;
				document.getElementsByClassName('leftar')[0].addClassName('active');
				if (($('imgmvr').style.left.split('p')[0] == -total_offset+(91*6))) {
					document.getElementsByClassName('rightar')[0].removeClassName('active');
				}
			}} );
		}
		else {
			document.getElementsByClassName('rightar')[0].removeClassName('active');
		}
	}
}

function move_step_left() {
	if (canclick) {
		if (($('imgmvr').style.left.split('p')[0] < 0)) {
			canclick = false;
			Effect.MoveBy( 'imgmvr', 0, +91, {duration:1, afterFinish: function() {canclick = true;
				document.getElementsByClassName('rightar')[0].addClassName('active');
				if (($('imgmvr').style.left.split('p')[0] == 0)) {
					document.getElementsByClassName('leftar')[0].removeClassName('active');
				}
			}} );
		}
		else {
			document.getElementsByClassName('leftar')[0].removeClassName('active');
		}
	}
}