// JavaScript Document

$(document).ready(function(){
						   /*
	$('#header-view').each(function(){
		$(this).delay(2000).animate({top:'-72'}, 500);
	});
	$('#header-view').mouseenter(function(ev){
		$(this).animate({top:'0'}, 200);
	});
	$('#header-view').mouseleave(function(ev){
		$(this).animate({top:'-72'}, 500);
	});
	*/
	$('div.slide-horr').each(function(){
		var content = $(this).children ('table');
		var contentWdth = content.width();
		
		$(this).mousemove(function(event){
			//if(event.pageX > $(this).offset().left && )
			if(event.pageX > ($(this).offset().left + contentWdth*2/3) && event.pageX < ($(this).offset().left + contentWdth) && content.position() < 0 )
				var off = content.offset();alert(off);
				off.left--;
				content.offset(off);
		});
	});
	
	/* CONTENT SLIDER */
	$('.view-toggle').each(function(){
		//$(this).data('visible', false);
		var button = $(this).find('.view-toggle-button');
		var content = $(this).find('.view-toggle-content');
		var session_name = $(this).data('session_name');
		//if($(this).data('direction') == 'horizontal'){
			if(!$(this).data('session_value'))content.hide(0);
		//}else{
			//if(!$(this).data('session_value'))content.slideUp(0);
		//}
		button.click(function(){
			if($(this).data('session_value')){
				$(this).data('session_value', false);
				//if($(this).data('direction') == 'horizontal'){
					content.hide(300);
					$(this).css('color', '#600');
				//}else{
					//content.animate({width:0});
				//}
			}else{
				$(this).data('session_value', true);
				//if($(this).data('direction') == 'horizontal'){
					content.show(300);
					$(this).css('color', '#ccc');
				//}else{
					//content.animate({width:0});
				//}
			}
			var session_value = $(this).data('session_value');
			$.ajax({
				  url:'/baza/_lib/_php/DB_session_update.php',
				  data:'session_name='+session_name+'&session_value='+session_value
				  })
		});
	});
	
	
	// full image view
	if($('div#image-full').size() > 0)
	{
		var hoover_text = 'klikni za pravu veličinu';
		if($('body').attr('lang') == 'en')
			hoover_text = 'click for real scale';
		$('body').append('<span id="image-full-hoover">'+hoover_text+'</span>');
		$('#image-full-hoover').css({'position':'absolute', 'background-color':'#333', 'opacity':0.7});
		$('#image-full-hoover').hide();
		$('div#image-full').each(function(){
			$(this).find('img').load(function(){
				var height = $(this).height();
				var pos = $(this).offset();
				var height_min = $(window).height() - pos.top - 5;
				
				var ratio = $(this).width()/$(this).height();
				if(ratio > $(window).width()/$(window).height())
					height_min = $(window).width()/ratio;
				
				$(this).height(height_min);
				
				$(this).toggle(	function(){
											$(this).animate({'height':height});
											$(this).data('full', true);
											$('#image-full-hoover').hide();
										},
										function(){
											$(this).animate({'height':height_min}, function(){
																									$('#image-full-hoover').show();
																									$('#image-full-hoover').offset({top:0, left:0});
																								}
																);
											$(this).data('full', false);
										}
												
									);
				$(this).hover(		function(){
											if( ! $(this).data('full'))
												$('#image-full-hoover').show();
										},
										function(){
											if( ! $(this).data('full'))
												$('#image-full-hoover').hide();
										});
				$(this).mousemove(function(event){
											$('#image-full-hoover').offset({top:event.pageY-20, left:event.pageX});
										});
			});
		});
	}	
	
	$('div#nodes').each(function(){rafael(this);});
});

function rafael(el){
	var width = $(document).width();
	var height = $(document).height() - 115;
	var m = Math.floor(Math.sqrt(917 * width / height));
	var m = 30;
	var n = Math.ceil(917 / m);
	var dx = width/m;
	var dy = height/n;
	
	var canvas = Raphael(el, width, height);
	var fellows = [];
	
	//alert(canvas);
	for($i=0; $i<917; $i++)
	{
		var fell = canvas.circle($i%m*dx + dx/2, Math.floor($i/m)*dy + dy/2, 2);
		//var fell = canvas.circle(100, Math.floor($i/m)*dy + dy/2 +100);
		fell.attr('fill', '#000');
		fellows.push(fell);
		if($i==0)
		{
			$(fell.node).click(function(){
				for($f=0; $f<fellows.length; $f++)
					fellows[$f].animate({cx: 100, cy: 100}, 1000);
			});
		}
		$(fell.node).html($i);
		//if($i==0)
			//$(fell.node).mouseover(function(){
				//for($f=0; $f<fellows.length; $f++)
				//	fell.animate({r: 10}, 200);
			//});
	}
	for($f=0; $f<fellows.length; $f++)
	{
		$(fellows[$f].node).mouseenter({raf:fellows[$f]}, function(event){
			event.data.raf.animate({r: 10}, 200);
		});
		$(fellows[$f].node).mouseleave({raf:fellows[$f]}, function(event){
			event.data.raf.animate({r: 2}, 200);
		});
	}
	/*
	//alert(m+' '+n+'-'+dx+' '+dy);
	$('.node').each(function(index){
		if(index == 0)
			$(this).click(function(){move();});
		if(index == 916)
			$(this).click(function(){move_back();});
		$(this).data('open', false);
		$(this).children('.name').css({opacity: '0'});
		var shiftX = index%m;
		var shiftY = Math.floor(index / m) ;
		$(this).css({left:((index%m)*dx + dx/2 + Math.random()*5), top: (Math.floor(index/m)*dy + dy/2 + Math.random()*5)});
	});
	
	$('.node').mouseenter(function(ev){
		//var node = this;
		if( ! $(this).data('open'))
		{
			//$(this).animate({width:'20', height:'20'}, 200, function(){$(this).data('open', true);});
			$(this).children('.name').css({opacity: '1'});
			$(this).css({width:10, height:10});
			$(this).data('open', true);
		}
		//$(this).data('open', true);
	});
	$('.node').mouseleave(function(ev){
								   //var node = this;
		if($(this).data('open'))
		{
			//$(this).animate({width:'3', height:'3'}, 200, function(){$(this).data('open', false);});
			$(this).children('.name').css({opacity: '0'});
			$(this).css({width:3, height:3});
			$(this).data('open', false);
		}
	});
	function move(){//alert('zuz');
		//var endX = endY = 200;
		$('.node').each(function(index){
			$(this).data('original_x', $(this).position().left);
			$(this).data('original_y', $(this).position().top);
			$(this).delay(index*2).animate({left:200, top:200}, 1000);
		});
	}
	function move_back(){//alert('zuz');
		//var endX = endY = 200;
		$('.node').each(function(index){
			//$(this).data('original_x', $(this).offset().left);
			//$(this).data('original_y', $(this).offset().top);
			$(this).delay(index*2).animate({left:$(this).data('original_x'), top:$(this).data('original_y')}, 1000);
		});
	}
	*/
	
}
