var ajaxContent;

function makeScrollbar(content,scrollbar,handle,horizontal,ignoreMouse){

	var ie = navigator.appVersion.indexOf('MSIE') > 0;
	if(ie) {
		scrollbar.style.display = 'none';
		content.style.overflow = 'auto';
		content.style.overflowX = 'hidden';
		return;
	}
	
	// hide scrollbar if not needed
	if( content.getScrollSize().y <= content.getSize().y ) {
		scrollbar.style.display = 'none';
		return;
	}
	
	// resize the handle to give a more accurate impression
	handle.style.height = Math.round( content.getSize().y / content.getScrollSize().y * scrollbar.getSize().y ) + 'px';

	var steps = (horizontal?(content.getScrollSize().x - content.getSize().x):(content.getScrollSize().y - content.getSize().y))
	var slider = new Slider(scrollbar, handle, {	
		steps: steps,
		mode: (horizontal?'horizontal':'vertical'),
		onChange: function(step){
			// Scrolls the content element in x or y direction.
			var x = (horizontal?step:0);
			var y = (horizontal?0:step);
			content.scrollTo(x,y);
		}
	});
	if( !(ignoreMouse) ){
		// Scroll the content element when the mousewheel is used within the 
		// content or the scrollbar element.
		$$(content, scrollbar).addEvent('mousewheel', function(e){	
			e = new Event(e).stop();
			var step = slider.step - e.wheel * 30;	
			slider.set(step);					
		});
	}
	// Stops the handle dragging process when the mouse leaves the document body.
	$(document.body).addEvent('mouseleave',function(){slider.drag.stop()});
}

function initFancyBox() {
	fbInitObj = {
		'padding'				: 0,
		'overlayShow'			: true,
		'overlayOpacity'		: 0.8,
		'zoomSpeedIn'			: 600,
		'zoomSpeedOut'			: 500,
		'easingIn'				: 'easeOutBack',
		'easingOut'				: 'easeInBack',
		'easingChange'			: 'easeOutBack',
		'hideOnContentClick'	: false
	};
	
	jQuery(".portfoliopiclink").fancybox( fbInitObj );
	jQuery(".newslink").fancybox( fbInitObj );
	makeScrollbar( $('div-page-content'), $('scrollbar'), $('scroll-handle'), false, false );	
}

function loadContent(handle, lang, sessionid) {
	ajaxContent = getNewXMLHttp();
	$('body-content').style.display = 'none';
	$('body-content').innerHTML = '';
	$('body-content-working').style.display = 'block';

	currentContentLang = lang;
	
	str = "content-only=" + sessionid + "&newLang=" + ( lang == 'ro' ? 'en' : 'ro');
	url = "/" + lang + "/" + handle + "/";
	ajaxContent.open("POST", url, true);
	ajaxContent.onreadystatechange = loadContentComplete;
	ajaxContent.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
	ajaxContent.send(str);
}


function loadContentComplete() {
	if(ajaxContent.readyState != 4) return;

    var href = ajaxContent.responseText.substr(0, ajaxContent.responseText.indexOf('|'));
	var content = ajaxContent.responseText.substr(ajaxContent.responseText.indexOf('|') + 1);
	var nav = content.substr(0, content.indexOf('|'));
	content = content.substr(content.indexOf('|') + 1);

	$('nav-indicator').innerHTML = nav;
	$('lang-selector').innerHTML = href;
	$('body-content').innerHTML = content;
	$('body-content').style.display = 'block';
	$('body-content-working').style.display = 'none';
	
	initFancyBox();
}
