swfobject.embedSWF('/public/flash/main.swf', 'swfIndex', '984', '501', '10.0.0', '/public/flash/expressInstall.swf', false, {wmode: 'opaque'});


// *** hiliteReqFields ***
var mdlFormInit = function()
{
	$('form.prmFormReq').submit(function(){ return hiliteReqFields(this); });
};

var hiliteReqFields = function(elem)
{
	//$(elem).find(':input:hidden:not([type="hidden"])').remove();
	//inhelpSubmitHandler.apply(elem);
	var reqList = $(elem).find('.req:visible').removeClass('no-fill');
	reqList = reqList.filter(function(){ return (this.value==''); });
	if(reqList.size()==0) return true;
	reqList.addClass('no-fill');
	alert('Не заполнены все обязательные поля!');
	return false;
};


// *** layfooter blocks ***
var footerBlockInit = function()
{
	var lst = $('body.prmIndex .layFooter .blocks .block-item .img');
	if(!lst.size()) return;
	var eachHandler = function()
	{
		var nxt = $(this).find('img:not(.noact):last').next('.noact');
		if(nxt.size()) nxt.fadeIn(800, function(){ $(this).removeClass('noact'); });
		else $(this).find('img:gt(0)').fadeOut(800, function(){ $(this).addClass('noact'); });
	};
	setInterval(function(){ lst.each(eachHandler); }, 3000);
};


// *** mdlCart ***
var mdlCartRecalc = function(elem)
{
	var cart = $(elem).closest('.mdlCart');
	var sum = 0, count = 0;
	var sumCur, countCur;
	var eachHandler = function()
	{
		countCur	= parseInt($(this).val());
		sumCur		= countCur * parseFloat($(this).closest('tr').find('.szprice').text()); 
		$(this).closest('tr').find('.priceall').html(Math.round(sumCur*100)/100);
		count	+= countCur;
		sum		+= sumCur;
	};
	cart.find('.count input.tf').each(eachHandler);
	
	sum = Math.round(sum*100)/100;
	cart.find('.itog .sum big').html(sum);
	cart.find('.itog .count big').html(count);
	
	cart.find('.save').removeClass('no-show');
	
	// discount
	discount = 0;
	for(var i=0; i<discTable.length-1; i++)
		if(sum>=discTable[i][0])
			discount = discTable[i][1];
	cart.find('.itog .discount span').html(discount);
	cart.find('.itog .sum-disc big').html(Math.round(sum*(100 - discount))/100);
	if(discount) cart.find('.disc').removeClass('no-show');
	else cart.find('.disc').addClass('no-show');
};

var mdlCartRemove = function(elem)
{
	var tr = $(elem).closest('tr');
	var table = tr.closest('.table');
	var id = $(elem).data('id');
	var main = false;
	if(tr.hasClass('main')) main = tr;
	else main = tr.prevAll('tr.main:first');
	
	var rowspan = parseInt(main.find('td[rowspan]').attr('rowspan'));
	if(rowspan==1) tr.remove();
	else
	{
		main.find('td[rowspan]').attr('rowspan', rowspan-1);
		if(main==tr)
		{
			var nxt = tr.nextAll('tr:first');
			main.find('.lvl').replaceWith(nxt.find('td'));
			nxt.remove();
		}
		else tr.remove();
	}
	
	table.append('<input type="hidden" name="del['+id+']" value="1" class="del"/>');
	mdlCartRecalc(table.get(0));
	return false;
};

var mdlCartSave = function(elem)
{
	var cart = $(elem).closest('.mdlCart');
	$.post(elem.href, cart.serialize(),
		function()
		{
			cart.find('.save').addClass('no-show');
			cart.find('input.del').remove();
		});
	return false;
};



// *** ui-dialog ***
var showMsg = function(str)
{
	return $('<div class="content"><p style="text-align: center; padding: 20px 0px; background-color: #000;">'+str+'</p></div>')
		.dialog({ width: 170, show: 'puff', modal: true, close: function(){ $(this).dialog('destroy'); $(this).remove(); } });
};

var loadModel = function(elem)
{
	// prev-next
	var item = $(elem).closest('.item');
	var iPrev = item.prev('.item').attr('id');
	var iNext = item.next('.item').attr('id');
	
	var ldr=showMsg('<img src="/public/images/graphics/spinner.gif" alt=""/>');
	var ui = $(elem).closest('.ui-dialog-content').dialog('close');
	var win=$('<div></div>');
	$.post(elem.href+'?template=3', true,
		function(data){
			win.html(data);
			win.find('a.darkbox').click(darkbox_cb);
			win.find('a[rel="colorbox"]').colorbox();
			var preload = new Image();
			preload.onload = function()
			{
				ldr.dialog('close');
				
				// prev-next
				if(!iPrev) win.find('.near-prev').css('display', 'none');
				else win.find('.near-prev').data('to', iPrev);
				if(!iNext) win.find('.near-next').css('display', 'none');
				else win.find('.near-next').data('to', iNext);
				
				win.find('.img').hover(mdlItemZoomShow, mdlItemZoomHide).mousemove(mdlItemZoomMove);
				win.dialog({ width: '767px', modal: true, show: 'clip', close: function(){ $(this).dialog('destroy'); $(this).remove(); }});
			};
			preload.src = win.find('.body .img img').attr('src');
		});
	return false;
};

var toModel = function(elem)
{
	var id = $(elem).data('to');
	if(!id) return false;
	$(elem).closest('.ui-dialog-content').dialog('close');
	$('#'+id).find('.img a').click();
	return false;
};


var addToCart = function(elem)
{
	var btn = $(elem).closest('p');
	var form = $(elem).closest('.body');
	if(form.find('.colors input:checked').size()<1){ alert('Выберите хотя бы один цвет!'); return false; }
	btn.addClass('wait').find('.out').empty();
	$.post(elem.href+'&template=3', form.serialize(),
		function(data)
		{
			btn.removeClass('wait').find('.out').html(data);
			updateAuthBlock();
		});
	return false;
};


var updateAuthBlock = function()
{
	$.post('/modules/cache.html?template=3&nc_ctpl=10', true,
		function(data)
		{
			$('.mdlCartBlock').replaceWith(data);
		});
};


var mdlItemSetImg = function(elem)
{
	var cont = $(elem).closest('.body');
	cont.find('.img a').attr('href', elem.href).find('img').attr('src', elem.href);
	return false;
};

var mdlItemZoomShow = function()
{
	var cont = $(this).closest('.main');
	var img = cont.find('.img');
	
	var i = img.get(0);
	i.zoom = $('<div class="zoom"><img src="'+$(this).find('img').attr('src')+'" alt=""/></div>');
	i.zimg = i.zoom.find('img');
	cont.append(i.zoom);
	
	i.s = img.width()/i.zimg.width();
	i.win = $('<div class="win"></div>');
	i.win.click(function(){ img.find('a').click(); });
	i.win.width(i.s*i.zoom.width());
	i.win.height(i.s*i.zoom.height());
	img.append(i.win);
	
	i.sw = img.width() - i.win.width();
	i.sh = img.height() - i.win.height();
};

var mdlItemZoomHide = function()
{
	var cont = $(this).closest('.main');
	cont.find('.zoom').remove();
	cont.find('.img .win').remove();
	
	this.win = this.zoom = this.zimg = this.sw = this.sh = this.offset = false;
};

var mdlItemZoomMove = function(e)
{
	if(!this.win) return false;
	this.offset = this.offset ? this.offset : $(this).offset();
	var posX = e.pageX - this.offset.left	- (this.win.width()>>1);
	var posY = e.pageY - this.offset.top	- (this.win.height()>>1);
	if(posX<0) posX = 0;	if(posX>this.sw) posX = this.sw;
	if(posY<0) posY = 0;	if(posY>this.sh) posY = this.sh;
	
	this.win.css({left: posX+'px', top: posY+'px'});
	this.zoom.scrollLeft(posX/this.s);
	this.zoom.scrollTop(posY/this.s);
};



// *** mdlFastOrder ***
var mdlFastOrderSub = function(elem)
{
	var sub = $(elem).closest('.sub').toggleClass('open');
	if(sub.hasClass('needload') && !sub.hasClass('loaded')) sub.addClass('loaded').find('.cont-ins').load(elem.href);
	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;
};



// *** init ***
$(function()
{
	footerBlockInit();
	mdlFormInit();
	$('a.darkbox').click(darkbox_cb);
	$('a[rel="colorbox"]').colorbox();
});
