swfobject.embedSWF('/public/flash/swfTop.swf', 'swfTop', '100%', '320', '10.0.0', '/public/flash/expressInstall.swf', false, {wmode: 'opaque', scale: 'noscale', salign: 'r'});

var addAngle = function(elem, fixedWidth)
{
	var el = $(elem);
	if(!fixedWidth) fixedWidth = true;
	el.prepend('<div class="mdlAlignAngle"></div>');
	var cont = elem.firstChild;
	var createFl = function(cs, w)
	{
		var fl = document.createElement('div');
		fl.className = cs;
		fl.style['height'] = '16px';
		fl.style['width'] = w+'px';
		return fl;
	};
	// первичное заполнение
	var cnt = Math.floor(el.height() / 16);
	for(var i=0; i<cnt; i++)
	{
		cont.appendChild(createFl('al', 5*(cnt-i-1)));
		cont.appendChild(createFl('ar', 5*i));
	}
	// дополняем
	if(!fixedWidth) return;
	var i = cnt;
	while(i<30 && Math.floor(el.height() / 16) > i)
	{
		var arm = 5;
		for(var j=0; j<cont.childNodes.length; j++)
		{
			var cur = cont.childNodes[j];
			if(cur.className!='ar') continue;
			cur.style['width'] = arm + 'px';
			arm += 5;
		}
		cont.insertBefore(createFl('ar', 0), cont.firstChild);
		cont.insertBefore(createFl('al', 5*i), cont.firstChild);
		i++;
	}
};

var updateAngle = function(elem)
{
	$(elem).find('.mdlAlignAngle').remove();
	addAngle(elem);
};

var tstAns = function(elem, score)
{
	var prn = $(elem).closest('.test');
	prnEl = prn.get(0);
	if(!prnEl.score) prnEl.score = 0;
	prnEl.score += score;
	var cont = prn.find('.testContent');
	cont.html('<p>Загрузка...</p>');
	$.post(elem.href, {'score' : prnEl.score},
		function(data)
		{
			cont.html(data);
			cont.find('.prmAngle').each(function(){ addAngle(this); });
			prn.css('padding-top', '1px'); // ie8
			prn.css('padding-top', '0px'); // ie8
		});
	return false;
};

var showMsg=function(str)
{
	return $('<div class="content"><p style="text-align: center; padding: 50px 10px 0px 10px;">'+str+'</p></div>')
		.dialog({ width: 170, show: 'puff', modal: true, close: function(){ $(this).remove(); $('.ui-effects-wrapper').remove(); } });
}

var qstShowThread = function(elem)
{
	var ldr=showMsg('Загрузка...');
	var win=$('<div></div>');
	$.post(elem.href, false,
			function(data){
				win.html(data);
				
				var img = new Image();
				img.onload = function()
				{
					ldr.remove();
					win.dialog({ width: this.width, modal: true, show: 'clip', hide: 'clip', close: function(){ $(this).remove();  $('.ui-effects-wrapper').remove(); } })
				};
				
				var imgElem = win.find('.img img');
				if(imgElem.length) img.src = imgElem.attr('src');
				else
				{
					ldr.remove();
					win.dialog({ width: 600, modal: true, show: 'clip', hide: 'clip', close: function(){ $(this).remove();  $('.ui-effects-wrapper').remove(); } })
				}
			});
	return false;
};

var showQuestForm = function(elem)
{
	$(elem).closest('.ui-dialog').find('.mdlQuestForm').slideToggle();
	return false;
};

var qstAddToThread = function(elem)
{
	var win = $(elem).closest('.ui-dialog-content');
	$.post(elem.href, $(elem).closest('.mdlQuestForm').serialize(), function(data){ win.html(data); });
	return false;
};

var niPrev=function(e)
{
	e.preventDefault();
	if(!this.belt) this.belt = $(this).closest('.mdlCarousel').find('.belt');
	var belt = this.belt;
	var be = belt.get(0);
	var cur = belt.scrollLeft();
	if(!be.itemSz) be.itemSz = belt.find('.item').outerWidth(true);
	if(be.cur==undefined) be.cur = cur;
	if(be.cur - be.itemSz >= 0) be.cur -= be.itemSz; else be.cur = 0;
	belt.stop();
	belt.animate({scrollLeft: be.cur}, 500);
	return false;
};


var niNext=function(e)
{
	e.preventDefault();
	if(!this.belt) this.belt = $(this).closest('.mdlCarousel').find('.belt');
	var belt = this.belt;
	var be = belt.get(0);
	var cur = belt.scrollLeft();
	if(!be.itemSz) be.itemSz = belt.find('.item').outerWidth(true);
	if(be.cur==undefined) be.cur = cur;
	var mx = be.scrollWidth - belt.width();
	if(be.cur <= mx) be.cur += be.itemSz;
	belt.stop();
	belt.animate({scrollLeft: be.cur}, 500);
	return false;
};


// *** darkbox ***
var darkbox_cb = function()
{
	var link = $(this);		
	if(!$('div.darkbox-frame').length) {					
		darkbox = $('<div class="darkbox-frame"><div class="darkbox-shadow"></div><div class="darkbox-canvas"><div class="darkbox-button"></div></div></div>').appendTo('body');
	}
	var frame = darkbox.clone().appendTo('body').addClass('darkbox-frame-on');
	var shadow = frame.find('div.darkbox-shadow').animate({opacity:0.6},300);
	var canvas = frame.find('div.darkbox-canvas');
	var button = frame.find('div.darkbox-button');
	var image = $('<img src="'+ link.attr('href') +'" alt="'+ link.attr('title') +'"/>');

	image.appendTo(canvas);
	image.load(function(){

		var imageWidth = image.width();
		var imageHeight = image.height();
		var frameWidth = frame.width()-40;
		var frameHeight = frame.height()-40;

		if(imageWidth > frameWidth) {
			imageWidth = frameWidth;
			image.width(imageWidth);					
			while(image.height() > frameHeight) {
				image.width(imageWidth);
				imageWidth--;
			}

			imageHeight = image.height();
		}

		if(imageHeight > frameHeight) {
			imageHeight = frameHeight;
			image.height(imageHeight);						
			while(image.width() > frameWidth) {
				image.height(imageHeight);
				imageHeight--;
			}

			imageWidth = image.width();
		}

		canvas.addClass('darkbox-canvas-load').animate({
			width:imageWidth,
			marginLeft:-imageWidth/2,
			height:imageHeight,
			marginTop:-imageHeight/2
		},500,function() {
			canvas.addClass('darkbox-canvas-done');
			button.addClass('darkbox-button-on');
			button.addClass(navigator.platform.toLowerCase().indexOf('mac')+1?'darkbox-button-left':'darkbox-button-right');

			image.animate({opacity:1},500,function() {
				shadow.click(closer);
				button.click(closer);
			});
		});
	});

	var closer = function() {
		canvas.remove();
		shadow.animate({opacity:0},300,function() {
			frame.remove();
		});
	}

	$(document).keydown(function(e) {
		if(e.which==27) closer();
	});

	return false;
};


$(function(){
	$('.prmAngle').each(function(){ addAngle(this); });
	$('.prmAngleFix').each(function(){ addAngle(this, false); });
	$('.prmAngleP p').each(function(){ addAngle(this); });
	$('.mdlCarousel .arwL').click(niPrev);
	$('.mdlCarousel .arwR').click(niNext);
	$('a.darkbox').click(darkbox_cb);
});
