// JavaScript Document
//var content_height = 651;
//var content_percentage = 194;
var window_index = 0;
var content_height=new Array();
var content_percentage=new Array();
var content_tops=new Array(0);
var is_heights_calculated=false;
var num_prj; 
var menu_ini_pos;
var content_hash=new Array();

function calulate_heights(){
	num_prj=$(".bigimage").size();
	$(".bigimage").each(function(i){
	  // alert($(this).height())
	   content_height[i]=$(this).height();
	   content_percentage[i]=parseInt(content_height[i]*30/100)
		
	  //alert(content_height[i]+"--"+content_percentage[i]);	
	});
	is_heights_calculated=true;
	
	}

$(document).ready(function() {


	$("#paging li.slash:last").remove();
		$('.imageslide').hover(
		function(){
			var id=$(this).attr("id").split("_")[1];
			$('#hovertext_'+id).fadeIn(); //mouseover
		},
		function(){
			 $('.hovertext').fadeOut(); // mouseout
		}
	);
	
	$('.hovertext').hover(
		function(){
		$(this).stop(true,true); //mouseover
		},
		function(){

		}
		);

$('a.scroll_anchor').each(function(i){
	
	$(this).click(function(e){
	$("#picts").show();
	// if(!is_heights_calculated)
	   calulate_heights();
	  
	    activate_item(i);
		$(window).unbind('scroll');
	  
		var index= $(this).index();
	
	    var offset=$(".bigimage").eq(i).offset();
	    if(checkipad())
			{
			$('html,body').stop(true).animate({scrollTop: offset.top}, 1,'linear',function(){
		 $("#menus").css({"top":offset.top});
				$(window).bind("scroll",windowscroll);
				
		});
				
         	}
	else
		{
		$('html,body').stop(true).animate({scrollTop: offset.top}, 200,'linear',function(){
		   // $('html,body').stop(true);
		 	$(window).bind("scroll",windowscroll);
			
		});
	 }
	    return false;
	});
	
	
	});
	
	  var document_hash=document.location.hash;
  if(document_hash!="")
  {  
	  var ah=jQuery.makeArray($("a.scroll_anchor"));
	  for(var j=0;j<ah.length;j++)
	    {
			if(document.location.href==ah[j])
			{
			 $('a.scroll_anchor').eq(j).trigger("click");
			 break;
			}
		}
  }
	
	
});

function activate_item(ind)
{
  if(ind<$(".bigimage").size())
  {
  //to avoid page jumping when document hash is assigned
    var cur_img=$(".bigimage").eq(ind);
    var hash= cur_img.attr('id');
	 cur_img.removeAttr('id');
	 document.location.hash=hash;
	 cur_img.attr('id',hash);  
	 
	
	$("a.scroll_anchor").removeClass("active");
	$("a.scroll_anchor").eq(ind).addClass("active");
		text_replacement();
  }
}

function windowscroll()
{
	var currentScrollTop = $(window).scrollTop();
	if(checkipad())
	 $("#menus").css({"top":currentScrollTop});
	
	//var new_index=parseInt((currentScrollTop+content_percentage)/content_height);
	var new_index=find_cur_pos(currentScrollTop);
	
//	$("#debug1").html(new_index + ":" +(currentScrollTop+197));
    if(new_index!=window_index)
	{ 
	  activate_item(new_index);
	}
	
	window_index=new_index;
 // alert(currentScrollTop);
}	

function find_cur_pos(currentScrollTop)
{
    var h=0;
	var upper_limit=0;
	 for(var j=0;j<content_height.length;j++)
	  {
	 	  upper_limit+=(content_height[j])+20;
	 	  if(currentScrollTop>=h && currentScrollTop<upper_limit-200) 
		   return j;
		
		   h=upper_limit-220;
	  }

return j;
}
function checkipad()
{
	if((navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)) || (navigator.userAgent.match(/iPad/i)))
	return true;
	else
	return false;
	
}
	
