//swfobject.embedSWF('url', 'id', '100%', '177', '8.0.0', '/public/flash/expressInstall.swf', false, {wmode: 'opaque'});

$(function()
{
	$('a.darkbox').click(darkbox_cb);
	$('.carousel .arwL').click(niPrev);
	$('.carousel .arwR').click(niNext);
	$('.carousel .belt img:lt(20)').each(function(){ this.src = this.name; });
	var i = new Image();
	i.src = '/public/images/loading.gif';
});


var niPrev=function(e)
{
	e.preventDefault();
	if(!this.belt) this.belt = $(this).closest('.carousel').find('.belt');
	var belt = this.belt;
	var be = belt.get(0);
	var cur = belt.scrollLeft();
	if(!be.itemSz) be.itemSz = belt.find('div').outerWidth(true);
	if(be.cur==undefined) be.cur = cur;
	if(be.cur - be.itemSz*10 >= 0) be.cur -= be.itemSz*10; else be.cur = 0;
	belt.stop();
	belt.animate({scrollLeft: be.cur}, 1000);
	//preload
	if(be.curItem) be.curItem -= 10;
	if(be.curItem < 0) be.curItem = 0;
	return false;
};


var niNext=function(e)
{
	e.preventDefault();
	if(!this.belt) this.belt = $(this).closest('.carousel').find('.belt');
	var belt = this.belt;
	var be = belt.get(0);
	var cur = belt.scrollLeft();
	if(!be.itemSz) be.itemSz = belt.find('div').outerWidth(true);
	if(be.cur==undefined) be.cur = cur;
	if(be.cur <= be.scrollWidth - belt.width()) be.cur += be.itemSz*10; else be.cur = be.scrollWidth - belt.width();
	belt.stop();
	belt.animate({scrollLeft: be.cur}, 1000);
	// preload
	be.curItem = be.curItem + 10 || 9;
	if(be.curItem > be.childNodes.length-10) be.curItem = be.childNodes.length-10;
	if(!be.curPreload || be.curPreload < be.curItem + 10)
	{
		be.curPreload = be.curItem + 10;
		belt.find('img:gt('+be.curPreload+'):lt(10)').each(function(){ this.src = this.name; });
	}
	return false;
};

var setImg = function(elem, video)
{
	var ctlg = $(elem).closest('.mdlCatalog');
	var img = ctlg.find('.img').get(0);
	var biggest = $(elem).attr('rel');
	
	img.style['backgroundImage'] = "url(/public/images/loading.gif)";
	if(video)
	{
		$(img).empty();
		$.post(elem.href, true, function(data){ img.style['backgroundImage'] = "none"; $(img).html(data) })
	}
	else
	{
		var i = new Image();
		i.onload = function(){ img.style['backgroundImage'] = "url("+elem.href+")"; if(biggest) img.parentNode.href = biggest; };
		i.src = elem.href;
	}
	ctlg.find('.belt .act').removeClass('act');
	$(elem).addClass('act');
	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);
	var protect = $('<img class="protect" src="/public/images/blank.gif" alt=""/>');
	protect.appendTo(canvas);
	image.load(function(){

		var imageWidth = image.width();
		protect.width(imageWidth);
		var imageHeight = image.height();
		protect.height(imageHeight);
		protect.css('marginTop', '-'+imageHeight+'px');
		var frameWidth = frame.width()-40;
		var frameHeight = frame.height()-40;

		if(imageWidth > frameWidth) {
			imageWidth = frameWidth;
			image.width(imageWidth);
			protect.width(imageWidth);
			while(image.height() > frameHeight) {
				image.width(imageWidth);
				protect.width(imageWidth);
				imageWidth--;
			}

			imageHeight = image.height();
		}

		if(imageHeight > frameHeight) {
			imageHeight = frameHeight;
			image.height(imageHeight);
			protect.height(imageHeight);
			protect.css('marginTop', '-'+imageHeight+'px');
			while(image.width() > frameWidth) {
				image.height(imageHeight);
				protect.height(imageHeight);
				protect.css('marginTop', '-'+imageHeight+'px');
				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;
};

