var mx = 0;
var my= 0;
var nochange = 0;
$(document).ready(function() {
	var firsthover = 0;
	var max = $('#topframe_overlay').attr('class').split('_')[1];
	var homepagemode = $('#portfolio_list').attr('class');
	var height = parseInt(max) * hfig; 
	var current_frame = parseInt($('#topframe').attr('class').split('_')[1]);
	var next_frame = 1;
	var d = "-="+hfig+"px";
	$('#frame_arrows a.arrow').hide();
	$('#topframe_overlay').mouseout(function()  {
		//$('#debug').prepend('LEAVE<hr>');
		$('#frame_arrows').removeClass('goup');
		$('#frame_arrows').removeClass('godown');
		$('#frame_arrows a.arrow').hide();
	});
	
	$('#topframe_overlay').mousemove(function()  {
		if (nochange == 1) {
			$('#frame_arrows a').hide();
			return;
		}
		////$('#debug').prepend('HOVER<hr>');
		var arrow_show = $('#frame_arrows').attr('class');
		if ((my > 380) && (arrow_show != 'godown')) {
			$('#frame_arrows a.arrow').hide();
			//$('#debug').prepend('change<br>');
			//$('#debug').prepend(arrow_show+'<br>');
			$('#frame_arrows').addClass('godown');
			$('#frame_arrows').removeClass('goup');
			$('#frame_arrows a.arrow_down').fadeIn(200);
		 } else if ((my <= 380) && (arrow_show != 'goup')) {
			 $('#frame_arrows a.arrow').hide();
			 //$('#debug').prepend('change<br>');
			 //$('#debug').prepend(arrow_show+'<br>');
			 $('#frame_arrows').addClass('goup');
			 $('#frame_arrows').removeClass('godown');
			 $('#frame_arrows a.arrow_up').fadeIn(200);
		 }
		
	});
	$('.homepic').click(function() {
		var next_frame = $(this).attr('class').split('_')[1].split(' ')[0];
		//$('#debug').prepend(next_frame+'<hr>');
		//$('#debug').prepend(current_frame+'<hr>'); 
		if (next_frame > current_frame) {
			var d = parseInt(next_frame) - parseInt(current_frame);
			d = '+='+(parseInt(d)*hfig)+'px';
		}
		if (next_frame < current_frame) {
			var d =  current_frame - next_frame;
			d = '-='+(parseInt(d)*hfig)+'px';
		}
		//$('#debug').prepend(d+'<hr>'); 
		 current_frame = changeFrame(d,next_frame,current_frame,height,max);
		 //$('#debug').prepend(current_frame+'<hr>'); 
		return false;
	});
	$('#portfolio_prevnext_page a').click(function() {
		
		//$('#debug').prepend('CLICK<hr>');
		var id = $(this).attr('id');
		//$('#debug').prepend(id+'<br>');
		var from = ppn_current;
		//$('#debug').prepend('From '+ppn_current+'<br>');
		////$('#debug').prepend('MATH (DIV 2) '+(ppn_current/2)+'<br>');
		if ((id == 'ppn_previous') && (ppn_current > 1)) {
			ppn_current = ppn_current - 1;
			//$('#debug').prepend('-1<br>');
		} else if ((id == 'ppn_next') && (ppn_current < ppn_max)) {
			ppn_current = ppn_current + 1;
			//$('#debug').prepend('+1 ('+ppn_current+' is less than '+ppn_max+'<br>');
		}
		if (ppn_current == ppn_max) {
			$('#ppn_next').hide();
		} else {
			$('#ppn_next').show();
		}
		if (ppn_current == 1) {
			$('#ppn_previous').hide();
		} else {
			$('#ppn_previous').show();
		}
		if (ppn_current > ppn_max) {
			ppn_current = ppn_max;
		}  
		if (ppn_current < 1) {
			ppn_current = 1;
		}
		//$('#debug').prepend('To '+ppn_current+'<br>');
		changeTheThumbs(ppn_current,homepagemode,from);
		
		
		return false;
	});
	
	$('body').animate({opacity: 1},5000, function () {
		if (current_frame == 0) {
			d = "+="+hfig+"px";
			next_frame = 1;
			//current_frame = changeFrame(d,next_frame,current_frame,height,max);
		}
	});		
	
	$('#visitsite').hover(function()  {
		nochange = 1;
	}, function () {
		nochange = 0;
	});
	$('.portfolio_thumb_overlay').click(function() {
		
		var next_frame = $(this).children('a').attr('class').split('_')[1];
		if (next_frame > current_frame) {
			var d = parseInt(next_frame) - parseInt(current_frame);
			d = '+='+(parseInt(d)*hfig)+'px';
		}
		if (next_frame < current_frame) {
			var d =  current_frame - next_frame;
			d = '-='+(parseInt(d)*hfig)+'px';
		}
		//$('#debug').prepend(d+'<hr>'); 
		 current_frame = changeFrame(d,next_frame,current_frame,height,max);
		 //$('#debug').prepend('Current: '+current_frame+'<hr>'); 
		return false;
	});
	$('#topframe_overlay').mousedown(function(){
		if (nochange == 1) {
			return;
		}
		//$('#debug').prepend('Current: '+current_frame+'<hr>');
		//$('#debug').prepend('CLICK<br>');
		if (my > 380) {
			var next_frame = parseInt(current_frame) + 1 ;
			var d = "+="+hfig+"px";
		 } else {
			 var next_frame = parseInt(current_frame) - 1;
			 var d = "-="+hfig+"px";
		 }
		
		
		 if (next_frame > max) {
			 next_frame = 0;
			 d = '-'+(parseInt(height))+"px";
		 }
		 if (next_frame < 0) {
			 next_frame = max;
			 d = "0px";
		 }
		 //$('#debug').prepend('NEXT: '+next_frame+'<br>');
		 
		 var scroll_page_calc = Math.floor(((parseInt(next_frame)-1) / (parseInt(ppn_perpage))))+1;
		 //$('#debug').prepend('EQ: (('+next_frame+'-1) &divide; '+ppn_perpage+')+1<br>');
		 /*
		 if ((next_frame != 0) && (current_frame > 0)) {
			 var scroll_page_calc = Math.floor(((parseInt(current_frame) / parseInt(ppn_perpage))));
			 //$('#debug').prepend('EQ: '+current_frame+' &divide; '+ppn_perpage+'<br>');
		 } else if (next_frame == max) {
			 var scroll_page_calc = ppn_max;
			 //$('#debug').prepend('EQ: '+ppn_max+'<br>');
		 } else {
			 var scroll_page_calc = 1;
			 //$('#debug').prepend('EQ: 1<br>');
		 }
		 */
		 if (scroll_page_calc == 0) {
			 scroll_page_calc  = 1;
		 }
		 
		 //$('#debug').prepend('PP: '+ppn_perpage+'<br>');
		//$('#debug').prepend('CP: '+ppn_current+'<br>');
		//$('#debug').prepend('PPNM: '+ppn_max+'<br>');
		//$('#debug').prepend('CALC: '+scroll_page_calc +'<br>');
			if (scroll_page_calc != ppn_current) {
				//alert(homepagemode);
				ppn_current = scroll_page_calc;
				
				if (ppn_current == ppn_max) {
					$('#ppn_next').hide();
				} else {
					$('#ppn_next').show();
				}
				if (ppn_current == 1) {
					$('#ppn_previous').hide();
				} else {
					$('#ppn_previous').show();
				}
				
				
				
				//$('#debug').prepend('CCP: '+ppn_current+'<br>');
				changeTheThumbs(scroll_page_calc,homepagemode,ppn_current);
			}
				
		 //$('#debug').prepend(d+'<hr>'); 
		 current_frame = changeFrame(d,next_frame,current_frame,height,max);
		 //$('#debug').prepend('<hr>&gt;'+current_frame+'<br>'); 
		 
		 
		
	});
	 $('.homepic').click(function() {
			var next_frame = $(this).attr('class').split('_')[1].split(' ')[0];
			//$('#debug').prepend(next_frame+'<hr>');
			//$('#debug').prepend(current_frame+'<hr>'); 
			if (next_frame > current_frame) {
				var d = parseInt(next_frame) - parseInt(current_frame);
				d = '+='+(parseInt(d)*hfig)+'px';
			}
			if (next_frame < current_frame) {
				var d =  current_frame - next_frame;
				d = '-='+(parseInt(d)*hfig)+'px';
			}
			//$('#debug').prepend(d+'<hr>'); 
			 current_frame = changeFrame(d,next_frame,current_frame,height,max);
			 //$('#debug').prepend(current_frame+'<hr>'); 
			return false;
		});
 $(document).bind('mousemove',function(e){ 
		mx_m = mx;
		mx_y = my
	 	mx = e.pageX;
		my= e.pageY;
		 
	});
 function changeFrame(d,next_frame,current_frame,height,max) {
	 if (homepagemode == 'home') {
		 if (next_frame > 0) {
			 $('#visitsite').fadeIn(200);
			 $('#visitsite').attr('href','http://'+portfolio[next_frame]['url']);
		 } else {
			 $('#visitsite').fadeOut(200);
		 }
	 }
	 var speed = hfig;
	 $('#portfolio_list li#framesel_'+current_frame).removeClass('selected');
//$('#debug').prepend('change from '+current_frame+'<br>');
	$('#topframe_content .portfolioimage').animate({"top":d},speed);
 //$('#debug').prepend('x='+mx+' y='+my+'<br>');
 //$('#debug').prepend(max+' '+current_frame+' '+next_frame+'<br>');
 $('#topframe').removeClass('frame_'+current_frame);	 
 $('#topframe').addClass('frame_'+next_frame);
	$('#nowshowing').html(portfolio[next_frame]['name']);
 //$('#debug').prepend('Text Changed<br>');
 //$('#debug').prepend(portfolio[next_frame]['name']+'<br>');
	 $('body#welcome .content-right-top h1').html(portfolio[next_frame]['name']);
	 //$('h1').removeAttr('style');
	 var n = portfolio[next_frame]['name'].replace(/ /g,'-');
	 $('body#welcome .content-right-top h1').css('background-image','url("/headerimg/'+n+'_28_1f8c9b.png")');
 //$('#debug').prepend('Header Changed<br>');
	 $('#portfolio-about-'+current_frame).fadeOut('200');
	 $('#portfolio-about-'+next_frame).fadeIn('200');
//$('#debug').prepend('change to '+next_frame+'<br>');
	 $('#portfolio_list li#framesel_'+next_frame).addClass('selected');
	 return(next_frame);
}
 function changeTheThumbs(page,homepagemode,from) {
	 //$('#debug').prepend('move thumbs from: '+from+' to '+page+'<br>');
	 
		if (homepagemode == 'home') {
			var w = $('#portfolio_list').width();
			w = w - 639;
			if (page == 1) {
				$('#portfolio_list li').animate({
					'left': '0px'
				}, 500);
			} else if (page == (max/ppn_max)) {
				$('#portfolio_list li').animate({
					'left': '-'+w+'px'
				}, 500);
			}  else {
				var l = (page-1) * 639;
				$('#portfolio_list li').animate({
					'left': '-'+l+'px'
				}, 500);
			}
		} else {
			$('#portfolio_list li.page_'+page).hide('100');
		}
	}
});

