/**
* @author Artjom Kurapov, Alari Rajande
* @since 9.09.11 9:54
*/
$(document).ready(function(){
	if($('#scrollblock02').outerHeight() < $('#scrollblock03').outerHeight()){
		$('#scrollblock01').prepend('<div class="scroller01" id="scroller01"><div id="scroller01a"><a href="#" id="scroller01b"></a></div></div>');
		$('#scroller01b').css('height',$('#scrollblock01').outerHeight() / $('#scrollblock03').outerHeight() * $('#scrollblock01').outerHeight() + 'px');
		$('#scrollblock02').scroll(function(e){
			var a = $('#scrollblock02').scrollTop() * 100 / ( $('#scrollblock03').outerHeight() - $('#scrollblock01').outerHeight() );
			var b = $('#scroller01').outerHeight() - $('#scroller01a').outerHeight();
			$('#scroller01a').css('marginTop',(a*b/100)+'px');
		});
		$('#scroller01b')
			.bind('mousedown',function(e){engage(e); })
			.bind('mouseup',function(e){ release(e); })
			.bind('click',function(e){ e.preventDefault(); })
		;
	}
	if($('#screenshots UL.slides LI').length > 1){
		//$('#screenshots').append('<a href="#" moveleft></a>');
		$('<a href="#" class="moveright"></a>')
			.click(function(){
				if( $('#screenshots UL.slides LI:animated').length == 0 ){
					if($('#screenshotdots A.active').parents('LI').eq(0).nextAll('LI').length > 0){
						$('#screenshotdots A.active').parents('LI').eq(0).next('LI').find('A').trigger('click');
					} else {
						$('#screenshotdots LI:first A').trigger('click');
					}
				}
				return false;
			})
			.appendTo('#screenshots')
		;
		$('<a href="#" class="moveleft"></a>')
			.click(function(){
				if( $('#screenshots UL.slides LI:animated').length == 0 ){
					if($('#screenshotdots A.active').parents('LI').eq(0).prevAll('LI').length > 0){
						$('#screenshotdots A.active').parents('LI').eq(0).prev('LI').find('A').trigger('click');
					} else {
						$('#screenshotdots LI:last A').trigger('click');
					}
				}
				return false;
			})
			.appendTo('#screenshots')
		;
		$('#screenshots').append('<ul id="screenshotdots"></ul>')
		$('#screenshots UL.slides').css('width',$('#screenshots UL.slides LI').length * $('#screenshots UL.slides LI:first').outerWidth() + 'px');
		$('#screenshots UL.slides LI').each(function(){
			$('<li><a href="#"></a></li>')
				.click(function(){})
				.appendTo('#screenshotdots')
			;
		});
		$('#screenshotdots A:first').addClass('active');
		$('#screenshotdots').css({ marginLeft: 0 - $('#screenshotdots').outerWidth()/2 + 'px' });
		$('#screenshotdots A').click(function(){
			if(!$(this).hasClass('active') && $('#screenshots UL.slides LI:animated').length == 0){
				$(this).parents('UL').eq(0).find('A.active').removeClass('active');
				$(this).addClass('active');
			}
			$('#screenshots UL.slides').animate({
				marginLeft: 0 - $(this).parents('LI').prevAll('LI').length * $('#screenshots UL.slides LI:first').outerWidth() + 'px'
			},{
				duration: 400,
				queue: false
			});
			return false;
		});
	}

	// alternative image viewer (for Books)
	if($('#book UL.slides LI').length > 1){
		//$('#screenshots').append('<a href="#" moveleft></a>');
		$('<a href="#" class="moveright"></a>')
			.click(function(){
				if( $('#book UL.slides LI:animated').length == 0 ){
					if($('#dots A.active').parents('LI').eq(0).nextAll('LI').length > 0){
						$('#dots A.active').parents('LI').eq(0).next('LI').find('A').trigger('click');
					} else {
						$('#dots LI:first A').trigger('click');
					}
				}
				return false;
			})
			.appendTo('#book')
		;
		$('<a href="#" class="moveleft"></a>')
			.click(function(){
				if( $('#book UL.slides LI:animated').length == 0 ){
					if($('#dots A.active').parents('LI').eq(0).prevAll('LI').length > 0){
						$('#dots A.active').parents('LI').eq(0).prev('LI').find('A').trigger('click');
					} else {
						$('#dots LI:last A').trigger('click');
					}
				}
				return false;
			})
			.appendTo('#book')
		;
		$('#book').append('<ul id="dots"></ul>')
		$('#book UL.slides').css('width',$('#book UL.slides LI').length * $('#book UL.slides LI:first').outerWidth() + 'px');
		$('#book UL.slides LI').each(function(){
			$('<li><a href="#"></a></li>')
				.click(function(){})
				.appendTo('#dots')
			;
		});
		$('#dots A:first').addClass('active');
		$('#dots').css({ marginLeft: 0 - $('#dots').outerWidth()/2 + 'px' });
		$('#dots A').click(function(){
			if(!$(this).hasClass('active') && $('#book UL.slides LI:animated').length == 0){
				$(this).parents('UL').eq(0).find('A.active').removeClass('active');
				$(this).addClass('active');
			}
			$('#book UL.slides').animate({
				marginLeft: 0 - $(this).parents('LI').prevAll('LI').length * $('#book UL.slides LI:first').outerWidth() + 'px'
			},{
				duration: 400,
				queue: false
			});
			return false;
		});
	}


	$('UL.faq01 LI.item').addClass('closed').find('P.question').click(function(){
		$(this).parents('LI.item').eq(0).toggleClass('closed');
	});

});

function engage(e) {
	offsetY = e.pageY - $('#scroller01a').offset().top;
	scrolltop = (document.documentElement.scrollTop ? document.documentElement.scrollTop : document.body.scrollTop);
	$(document).bind('mousemove',dragIt).bind('mouseup',release);
	e.preventDefault();
}

function release() {
	$(document).unbind('mousemove',dragIt).unbind('mouseup',release);
}

var scrolltop = 0;
var offsetY = 0;

function dragIt(event) {
	b = $('#scroller01');
	c = $('#scroller01a');
	d = b.offset().top - scrolltop;

	if( event.clientY - d - offsetY < 0 ){
		c.css('marginTop','0');
		moveListTo(0);
	} else if( event.clientY - d - offsetY + c.outerHeight() > b.outerHeight() ){
		c.css('marginTop',b.outerHeight() - c.outerHeight() + 'px');
		moveListTo(100);
	} else {
		c.css('marginTop',event.clientY - d - offsetY + 'px');
		moveListTo( (event.clientY - d - offsetY) * 100 / (b.outerHeight() - c.outerHeight()) );
	}
	return false;
}

function moveListTo(per){
	$('#scrollblock02').scrollTop(($('#scrollblock03').outerHeight() - $('#scrollblock01').outerHeight()) * per / 100);
	return false;
}
