/* 

	Easy Scroll v1.0
	written by Alen Grakalic, provided by Css Globe (cssglobe.com)
	please visit http://cssglobe.com/post/1495/easy-scroll-accessible-content-scroller
	
*/

// This function inserts newNode after referenceNode
var obj = [];

function insertAfter( referenceNode, newNode )
{
    referenceNode.parentNode.insertBefore( newNode, referenceNode.nextSibling );
}
function easyscroll(id){
	// id of the container element 

	// navigation buttons text
	// var nav = ["Scroll Up", "Scroll Down", "Reset"];
        var nav = ["Up", " Down"];
	
	//	id for each navigation button (OPTIONAL)
	// var navId = ["btnUp", "btnDown", "btnReset"];
        var navId = [id + "-btnUp", id + "-btnDown"];

	// movement speed
	var speed = 5;
	
	// desired height of the container element (in pixels)
        //var cHeight = document.getElementById('content').offsetHeight;

	var height = ch;
       
	//
	// END CONFIG
	// do not edit below this line (unless you want to of course :) )
	//

	obj[id] = document.getElementById(id);
        
	
	obj[id].up = false;
	obj[id].down = false;
	obj[id].fast = false;

	var container = document.createElement("div");
	var parent = obj[id].parentNode;
	container.id=id + "-easyscroll";
	
        insertAfter(obj[id], container);


	parent.removeChild(obj[id]);	
        container.style.position = "relative";
	container.style.height = height + "px";
	container.style.overflow = "hidden";
	obj[id].style.position = "absolute";
	obj[id].style.top = "0";
	obj[id].style.left = "0";
	container.appendChild(obj[id]);
	
	var btns = new Array();
	var ul = document.createElement("ul");
	ul.id=id + "-scroll-nav";
	for (var i=0;i<nav.length;i++){
		var li = document.createElement("li");
		li.innerHTML = nav[i];
		li.id = navId[i];
		btns.push(li);
		ul.appendChild(li);
	};
        //parent.insertBefore(ul,container);
        insertAfter(container, ul);
	btns[0].onmouseover = function(){
		obj[id].up = true;
		this.className = "over";
	};
	btns[0].onmouseout = function(){
		obj[id].up = false;
		this.className = "";
	};		
	btns[1].onmouseover = function(){
		obj[id].down = true;
		this.className = "over";		
	};
	btns[1].onmouseout = function(){
		obj[id].down = false;
		this.className = "";
	};	
	
	
$(btns[0]).bind( "touchstart", function(e){
		obj[id].up = true;
		this.className = "over";
		obj[id].fast = true;
});
$(btns[0]).bind( "touchend", function(e){
		obj[id].up = false;
		this.className = "";
		obj[id].fast = true;
});
$(btns[1]).bind( "touchstart", function(e){
		obj[id].down = true;
		this.className = "over";		
		obj[id].fast = true;
});
$(btns[1]).bind( "touchend", function(e){
		obj[id].down = false;
		this.className = "";
		obj[id].fast = true;
});

		
	btns[0].onmousedown = btns[1].onmousedown = function(){
		obj[id].fast = true;
	};	
	btns[0].onmouseup = btns[1].onmouseup = function(){
		obj[id].fast = false;
	};
        /*
	btns[2].onmouseover = function(){ 		
		this.className = "over";
	};	
	btns[2].onmouseout = function(){ 		
		this.className = "";
	};		
	btns[2].onclick = function(){ 		
		obj[id].style.top = "0px";
	};
        */
		
	this.start = function(id){
		var newTop;
		var objHeight = obj[id].offsetHeight;
		var top = obj[id].offsetTop;

                if (top >= 0) $('#' + id + '-btnUp').attr('style', 'opacity:0.3');
                else $('#' + id + '-btnUp').attr('style', 'opacity:1');

                if (top <= (height - objHeight) ) $('#' + id + '-btnDown').attr('style', 'opacity:0.3');
                else $('#' + id + '-btnDown').attr('style', 'opacity:1');

		var fast = (obj[id].fast) ? 5 : 1;

		if(obj[id].down){		 
			newTop = ((objHeight+top) > height) ? top-(speed*fast) : top;	
			obj[id].style.top = newTop + "px";
		};
                
		if(obj[id].up){		 
			newTop = (top < 0) ? top+(speed*fast) : top;
			obj[id].style.top = newTop + "px";
		};
	};
	
	this.start_hide = function(id){
		var objHeight = obj[id].offsetHeight;

                if (objHeight <= height){
                    clearInterval(obj[id].interval);
                    $('#' + id + '-btnUp').parent('ul').remove();
                }

	};
	
	obj[id].interval = setInterval(function(){start(id);},50);		
		
};


//
// script initiates on page load. 
//

this.addEvent = function(objw,type,fn){
	if(objw.attachEvent){
		objw['e'+type+fn] = fn;
		objw[type+fn] = function(){objw['e'+type+fn](window.event );}
		objw.attachEvent('on'+type, objw[type+fn]);
	} else {
		objw.addEventListener(type,fn,false);
	};
};

$(function(){
	if (es) {
		$('#cont, #photo, #news').each(function(){
			var id = $(this).attr('id')
			easyscroll( id )
			start_hide( id )
		})
	}
})

//
// Прокрутка внутри блока для якорей
//
$(window).load(function(){
    $('#content a').click(function(){
        var hash = $(this).attr('href').substr(1);
            if ($('#content:has(a[name='+hash+'])').html())
            {
                var block = $('#easyscroll').offset().top;
                var finish = $('a[name=' + hash + ']').offset().top;
                var top = finish - block;
                $('#content').attr('style', 'top:-'+top + 'px');
                return false;
            }        
    });
});
