var current_loc = '';
var current_height = 0;
var flash = false;

var setHeight = function(h)
{
	current_height = h > document.body.clientHeight ? h : 0;
	flash.style.height = h > document.body.clientHeight ? h+'px' : '100%';
};

var setAnc=function(name){ document.location.hash=name; current_loc=document.location.hash; };

var checkLocation = function()
{
	var hash = document.location.hash;
    if (hash!==current_loc)
	{
		current_loc = hash;
		var obj={};
		hash.replace(/(\w+)=([\w\/.]+)/g, function(s, k, v){ obj[k]=v; });
		flash.retAnc(obj);
    }
};

var resizeHandler = function()
{
	if(document.body.clientHeight > current_height) flash.style.height = '100%';
};

var init = function()
{
	var MSIE = (navigator.userAgent.indexOf("Microsoft") != -1) && !window.opera;
	flash = MSIE ? window['flash'] : document['flash'];
	window.onresize = resizeHandler;
	checkLocation();
	var watchAnc = setInterval(checkLocation, 500);
};

var showMsg=function(str)
{
	return $('<div class="content"><div style="text-align: center; padding: 30px 20px 10px 20px;">'+str+'</div></div>')
		.dialog({ width: 300, show: 'puff', modal: true, close: function(){ $(this).remove(); $('.ui-effects-wrapper').remove(); } });
};

var showModel = function(elem)
{
	var ldr=showMsg('Загрузка...');
	var win=$('<div></div>');
	$.post(elem.href, true,
		function(data){
			ldr.remove();
			win.html(data);
			win.dialog({ width: '70%', modal: true, show: 'clip', hide: 'clip', close: function(){ $(this).remove();  $('.ui-effects-wrapper').remove(); } })
		});
	return false;
};

var setColor = function(elem)
{
	$('.ui-dialog .img img').attr('src', elem.href);
	return false;
};

var scrollTo = function(idx)
{
	var B = $('.mdlCatalog .belt');
	var b = B.get(0);
	b.cur = idx;
	$('.mdlCatalogLister a').removeClass('act');
	$('.mdlCatalogLister a:eq('+idx+')').addClass('act');
	B.stop();
	B.animate({scrollLeft: idx*B.width()}, 500);
	return false;
};

var scrollRel = function(rel)
{
	var B = $('.mdlCatalog .belt');
	var b = B.get(0);
	var to = b.cur+rel;
	if(to>=b.cnt || to<0) return;
	scrollTo(to);
};

var mdlCatalogListerInit = function()
{
	var B = $('.mdlCatalog .belt');
	var b = B.get(0);
	b.cnt = Math.ceil(b.scrollWidth/B.width());
	b.cur = Math.floor(B.scrollLeft()/b.scrollWidth);
	var insHtml = '';
	for(var i=0; i<b.cnt; i++)
		insHtml += '<a href="#" '+(i==b.cur ? 'class="act"' : '')+' onclick="return scrollTo('+i+');">'+(i+1)+'</a>';
	$('.mdlCatalogLister .arwl').after(insHtml);
	$('.mdlCatalogLister .arwl').click(function(){ scrollRel(-1); });
	$('.mdlCatalogLister .arwr').click(function(){ scrollRel(1); });
};


// *** autoshow ***
var autoShowInit = function()
{
	var autoshow = $('#autoshow');
	if(autoshow.size()) showMsg(autoshow.html());
};


// *** init ***
if(typeof($)!='undefined') $(function(){
	mdlCatalogListerInit();
	autoShowInit();
});

swfobject.embedSWF('/preloader.swf', 'flash', '100%', '100%', '9.0.0', '/public/flash/expressInstall.swf', false,
	{
		'allowFullScreen'	: true,
		/*'allowScriptAccess' : 'sameDomain',*/
		'salign'			: 'lt',
		'scale'				: 'noScale',
		'wmode'				: 'window'
	});
