
$(function(){

	$('#lang').change(lang_change);

	scroll_init();

	$('#up').hover(scroll_up,scroll_stop);
	$('#down').hover(scroll_down,scroll_stop);

	//fix_ff_wmp();

	$('#prn').click(print_click);
	$(window).unload(popen_clean);

	$('#prn').Tooltip();
	$('#scroll_tip').Tooltip();

});

function menu_click()
{
	var n = $(this).parent().attr('id').substr(1);
	if (contents && contents[n]) {
		$('#title').removeClass($('#title').attr('class')).addClass('title_'+n);
		$('#title > .title_wrap > h1').html(contents[n]['title']);
		$('#txt_content').html(contents[n]['txt']);
		$('#im').removeClass($('#im').attr('class')).addClass('im_'+n);
	}
}

function lang_change()
{
	var lang = $('#lang').val();
	var lang_def = $('#lang_def').val();
	if (lang == '') return;

	var url = location.href;
	var srch = location.search;

	if (url.indexOf('/shop/') != -1)
	{
		if (srch) srch = srch.replace(/[\?&]language=[a-z]+/g,'');
		else srch += '?';
		if (srch != '?') srch += '&';
		srch += 'language=' + lang;
		location.search = srch;
	}
	else
	{
		url = ( lang == lang_def ? '/' : '/' + lang + '/' ) + url.substr(url.lastIndexOf('/')+1);
		location.href = url;
	}
}

var containerHeight = 419; //513;//303;	// container height
var sliderHeight	= 414; //508;//298;	// slider zone of indicator height
var contentHeight;			// content height, it is calculated figure
var indicatorHeight;		// indicator height, it is calculated figure

function scroll_init()
{
	// calculate current content size
	contentSize = jQuery.iUtil.getSize($('#txt_cont').get(0));
	contentHeight = contentSize.hb;

	// init slider position and height properties
	$('#slider').css('top',245).css('left',559).css('height',sliderHeight);

	// calculate indicator height
	if (contentHeight < containerHeight)
	{
		// if content height less then conatiner, indicator will desapear
		indicatorHeight = 0;
	}
	else
	{
		indicatorHeight = Math.ceil(sliderHeight * containerHeight / contentHeight);
	}

	// init indicator height property
	$('#indicator').css('height', indicatorHeight);

	// init start position for indicator
	if ($('#txt_content')[0].scrollTop != 0)
	{
		var indicatorTop = Math.ceil(sliderHeight * $('#txt_content')[0].scrollTop / contentHeight);
		$('#indicator').css('top',indicatorTop);
	}

	// bind drop-down handler for indicator
	$('#slider').Slider(
		{
			accept : '#indicator',
			onSlide : function( cordx, cordy, x , y)
			{
				$('#txt_content')[0].scrollTop = (contentHeight - containerHeight) * cordy / 100;
			}
		}
	);

	$('#slider').show();
}

var scroll_th = null;
var scroll_step = 3;
var scroll_speed = 10;

function scroll_up()
{
	clearTimeout(scroll_th);
	var cur = $('#txt_content')[0].scrollTop;
	$('#txt_content')[0].scrollTop = cur - scroll_step;
	if ($('#txt_content')[0].scrollTop > 0) {
		scroll_th = setTimeout(scroll_up,scroll_speed);
		var sliderTop = Math.ceil(sliderHeight * $('#txt_content')[0].scrollTop / contentHeight);
		$('#indicator').css('top',sliderTop);
	}
	else
	{
		$('#indicator').css('top',0);
	}
}

function scroll_down()
{
	clearTimeout(scroll_th);
	var cur = $('#txt_content')[0].scrollTop;
	$('#txt_content')[0].scrollTop = cur + scroll_step;
	if (cur != $('#txt_content')[0].scrollTop) {
		scroll_th = setTimeout(scroll_down,scroll_speed);
		var sliderTop = Math.floor(sliderHeight * $('#txt_content')[0].scrollTop / contentHeight);
		$('#indicator').css('top',sliderTop);
	}
	else
	{
		$('#indicator').css('top',sliderHeight - indicatorHeight);
	}
}

function scroll_stop()
{
	clearTimeout(scroll_th);
}

function fix_ff_wmp()
{
	if (!$.browser.mozilla) return;
	$('#txt_content').wrap('<div style="overflow:hidden;"></div>');
	$('#txt_content').css('margin-right','-20px').css('overflow','auto');
}

function print_click()
{
	$('#slider').hide();
	html = $('#box').html();
	$('#slider').show();
	popen(null,'_prnt',{width:760,height:410,menubar:'yes',resizable:'yes'},
		'<html><head><link href="css/print.css" rel="stylesheet" type="text/css" />'
		+ '</head><body onload="if(window.print)window.print();">'
		+ html.replace(/type=\"submit\"/ig,'type="button"')
		+ '</body></html>');
}
