// JavaScript Document

var current_content, current_title, container, lastId='hm';
var dontListen = false;
var allowSheet = true, homeTM, sheetNumChildren;

 $(document).ready(function(){
	
	if ($(window).height()<800) {
		self.resizeTo($(window).width(), 900);
	}
	
   setListeners()
   
	container = "div#home";
	current_title=$('div#sheet');
	current_content=$(container);
  // startSheet();
   homeTM = setTimeout('startSheet(1);',5000);
   
   setContentHeight();
   
   // setScrollersPos()
   
   
   $(window).resize(function() {
	  setStuffPos();
	  setContentHeight();
	  
			$('#overtext_bg').css('height',$(document).height() );
			$('#overtext_main').css('height',$(document).height() );
			
	});
 });

function startSheet(n) {
	sheetNumChildren = $('div#sheet_content_in').children().length;
	
	$('div#sheet_content_in').animate( {'opacity': '0'}, 'slow', 'linear', function() {
																			$(this).after(sheetStartOver(n));
																			}  );
}
function sheetStartOver(n) {
	if (n>sheetNumChildren-1) n=0;
	var mt = -n*300;
	$('div#sheet_content_in').css('margin-top',mt+'px');
	$('div#sheet_content_in').animate( {'opacity': '1'}, 'slow' );
	if (allowSheet)
		homeTM = setTimeout('startSheet('+(n+1)+');',5000);
}

function setListeners() {
	menuReact( $("a.mn") );
	menuClickReact( $("a.mn") );
	bgChange();
	eyeReact( $("ul#eye") );
	setOTreactors();
	setFormListeners();
	//scrollerListeners();
}
/*function scrollerListeners() {
	$("div#up").mouseover(
		function(event) {
			scrollContent('+');
		}
	);
	$("div#down").mouseover(
		function(event) {
			scrollContent('-');
		}
	);
	$("div#up").mouseout(
		function(event) {
			$(container+" div").stop();
		}
	);
	$("div#down").mouseout(
		function(event) {
			$(container+" div").stop();
		}
	);
}
function scrollContent(sign) {
	
	
	
	
	if ( ( parseInt( $(container+" div").css('margin-top') ) >= 0 && sign=='+' ) ) {
		
		$(container+" div").stop();
		$(container+" div").animate({'margin-top':'0px'},'slow');
	} else if ( ( current_content.height()-parseInt( $(container+" div").css('margin-top') ) > $(container+" div").height() && sign=='-' ) ) {
		
		$(container+" div").stop();
		
	} else {
		
		$(container+" div").animate({ 'margin-top': sign+'=10' }, 100, 'linear', function() {
																					scrollContent(sign);
																				} );
	}
}
*/

function menuReact(e) {
	$(e).mouseover(
		function(event) {
			if ( $(event.currentTarget).parent().attr('class') != 'selected' ) {
				// animate the link
				$(event.currentTarget).animate( { height: '72px' }, 300, 'linear');
				
				// animate the selector
				var theSelector = $(event.currentTarget).parent().children('div');
				theSelector.animate({ height: '15px', opacity: '0.9' }, 300, 'linear' );
			}
		}
	);
	$(e).mouseout(
		function(event) {
			if ( $(event.currentTarget).parent().attr('class') != 'selected' ) {
				// animate the link
				$(event.currentTarget).animate( { height: '81px' }, 200, 'linear');
				
				// animate the selector
				var theSelector = $(event.currentTarget).parent().children('div');
				theSelector.animate({ height: '6px', opacity: '0.5' }, 200, 'linear' );
			}
		}
	);
}

function setBase(id) {
	if (id=='serv') {
		container = "div#services";
		title = "div#services_title";
	} else if (id=='pf') {
		container = "div#portfolio";
		title = "div#portfolio_title";
	} else if (id=='hm') {
		container = "div#home";
		title = "div#sheet";
	}
	
	$(container).css('display','block');
	$(container+" div").css('margin-top','0px');
	
	return title;
	
}

function setContentHeight() {
	$(container).css('height','auto' );
	var os = $('div#main_container').offset();
	var new_h = ( parseInt( $(document).height() ) - os.top - 130 );
	
	if (new_h < parseInt( $(container).height() ) ) new_h = parseInt( $(container).height() ) + 144;
	
	$(container).css('height', new_h + "px" );
}

function setScrollersPos(anim) {
	var os = $('#content_wrapper').offset();
	
	
	if (current_content.height() < $(container+" div").height() ) {
		$('#scrollers').css('display','block');
		if (anim) {
			$('#scrollers').animate( { top: (os.top+0)+'px' }, 'slow');
		} else {
			$('#scrollers').css('left',(os.left+800)+'px');
			$('#scrollers').css('top',(os.top+0)+'px');
		}
	} else {
		$('#scrollers').css('display','none');
		$(container+" div").css('margin-top','0px');
	}
	//alert('scrollers ok');
}

function menuClickReact(e) {
	$(e).click(
		function(event) {
			if (!dontListen) {
				
				// $('body').css('position','fixed');
				
				var theId = $(event.currentTarget).get(0).id;
				if (theId != lastId) {
					dontListen=true;
					lastId=theId;
					var title = setBase(theId);
					if (container.indexOf('home')>-1) {
						$("div#titles_placeholder").animate({ height: '340px' }, 500, 'linear' );
						allowSheet = true;
						homeTM = setTimeout('startSheet(0);',5000);
					} else {
						clearTimeout(homeTM);
						$("div#titles_placeholder").animate({ height: '165px' }, 500, 'linear' );
						allowSheet = false;
					}
					
					var os = $("div#header_container").offset();
					
					$(title).css('display','block');
					$(title).animate({ left: os.left, opacity: '1'}, 200, 'linear');
					
					current_title.css('left',os.left+'px');
					current_title.animate({ left: '2000', opacity: '0' }, 600, 'linear', function() {
																								$(this).css('left','-1000px');
																								$(this).after(goFor);
																							}  );
					
					current_title=$(title);
				}
				
				$("#menus *").removeClass("selected");
				$("a.mn").css('height','81px');
				$("div.selector").css('opacity','0.5');
				$("div.selector").css('height','6px');
				$(event.currentTarget).parent().addClass('selected');
				$(event.currentTarget).parent().children().css('height','20px');
				$(event.currentTarget).parent().children().css('opacity','1');
				$(event.currentTarget).css('height','67px');
			}
			
			event.preventDefault();
		}
	);
	
}


 
function goFor() {
 	
	//alert(current_content.get(0).id + ' next: ' + container);
	
	var tempContent = current_content;
	current_content = $(container);
	
	postH = current_content.css('height');
	//current_content.css('height',0);
	
	// setContentHeight();
	
	tempContent.animate({ height: 'toggle', opacity: '0'}, 450, 'linear', function() {
																			$(this).after(setStuffPos());
																			dontListen=false;
																		} );
	current_content.animate({ opacity: '1', height:postH}, 500);
}
function setStuffPos() {
	var os = $("div#header_container").offset();
	current_title.css('left',os.left+'px');
	$('body').css('position','relative');
	$('#content_wrapper').prepend(current_content);
	$('#content_wrapper').css('height','auto');
}

function highlightLogo(id) {
	$('#'+id).attr('src','imgs/clients/'+id+'_hover.png');
}
function blurLogo(id) {
	$('#'+id).attr('src','imgs/clients/'+id+'.png');
}
