﻿$(function() {
	
	$('.scroll-pane, .scroll-pane02').jScrollPane({
	animateTo:false,
	showArrows:true, 
	scrollbarWidth:9, 
	scrollbarMargin:20, 
	arrowSize:8,
	dragMinHeight:10,
	wheelSpeed : 18
	});
	
	$('.scroll-pane03').jScrollPane({
	animateTo:true,
	showArrows:true, 
	scrollbarWidth:9, 
	scrollbarMargin:20, 
	arrowSize:8,
	dragMinHeight:10,
	wheelSpeed : 18
	});
	
	if(typeof(Cache) == 'undefined'){
		var Cache = new Array();
	}
	
	function getHash(){
		var hashIndex = new String(document.location).indexOf("#");
		if(hashIndex > 0){
	    	page = new String(document.location).substring(hashIndex + 1);
	    	return page;
		}else{
			return false;
		}	
	}
	var hash = getHash();
	var element = $('.a'+hash);
	var currentPage = '';
	
	if(element.length>0){
		$('.scroll-pane, .scroll-pane02, .scroll-pane03')[0].scrollTo('.a'+hash);
		if($('#m-'+hash, '.virtual-menu').length>0){
			var linkId = hash;
			var linkHtml = $('a', '#m-'+hash).html();
			var innerText = linkHtml.substring(14, (linkHtml.length-17));
			Cache[linkId] = innerText;
			globalvar = innerText;
			removeSelection();
			$('a', '#m-'+hash).replaceWith('<span class="addon" id="span-'+linkId+'">'+innerText+'</span>');
		}
	}
	
	
	
	$('a', '.virtual-menu').live('click', function(){
		var eventObject = $(this);
		if(currentPage == ''){
			currentPage = $('.virtual-menu').attr('id');;
		}
		var linkId = eventObject.parent().parent().parent().attr('id').substr(2);
		var linkHtml = eventObject.html();
		var innerText = linkHtml.substring(14, (linkHtml.length-17));
		Cache[linkId] = innerText;
		scrollTo(linkId);
		removeSelection();
		eventObject.replaceWith('<span class="addon" id="span-'+linkId+'">'+innerText+'</span>');
		return false;
	});

	function removeSelection(){
		$('.a-sel').removeClass('a-sel');
		if($('.addon').attr('id') != undefined){
			var hash = $('.addon').attr('id').substr(5);
			$('.addon').replaceWith('<a href="/rus/'+currentPage+'#'+hash+'"><u><ins><span>'+Cache[hash]+'</span></ins></u></a>');
		}
	}
	
	function scrollTo(selector){
		var element = $('.a'+selector);
		if(element.length == 0){
		}
		if(element.length>0){
			$('.scroll-pane, .scroll-pane02, .scroll-pane03')[0].scrollTo('.a'+selector);
		}
	}
	
	
	
	var ScrollTimeout = null;
	
	function autoscroll(direction){
		if(direction == 'up'){
			var scrollPx = 1.5 * (-1);
		}else{
			var scrollPx = 1.5;
		}
		$('.scroll-pane, .scroll-pane02, .scroll-pane03')[0].scrollBy(scrollPx);
		ScrollTimeout = setTimeout(function(){autoscroll(direction)}, 100);
	}
	
	$('.jScrollArrowUp').mouseover(function(){
		autoscroll('up');
	});
	
	$('.jScrollArrowDown').mouseover(function(){
		autoscroll('down');
	});
	
	$('.jScrollArrowUp, .jScrollArrowDown').mouseout(function(){
		clearTimeout(ScrollTimeout);
	});
	
	$('.jScrollArrowUp, .jScrollArrowDown').click(function(){
		clearTimeout(ScrollTimeout);
	});
	
	jQuery('a.lightbox').attr('rel', 'shadowbox');
	Shadowbox.init();
	
});
