swfobject.embedSWF('/public/flash/index.swf', 'swfIndex', '905', '408', '9.0.0', '/public/flash/expressInstall.swf', false, {wmode: 'opaque'});
swfobject.embedSWF('/public/flash/logo.swf', 'swfLogo', '235', '80', '9.0.0', '/public/flash/expressInstall.swf', false, {wmode: 'opaque'});

var layTextInit = function()
{
	var layTextArwTClickHandler = function(e)
	{
		e.preventDefault();
		if(!this.belt) this.belt = $(this).closest('.layText').find('.content');
		var belt = this.belt;
		var be = belt.get(0);
		var cur = belt.scrollTop();
		if(be.cur==undefined) be.cur = cur;
		if(be.cur - 320 >= 0) be.cur -= 320; else be.cur = 0;
		belt.stop();
		belt.animate({scrollTop: be.cur}, 500);
		return false;
	};
	var layTextArwBClickHandler = function(e)
	{
		e.preventDefault();
		if(!this.belt) this.belt = $(this).closest('.layText').find('.content');
		var belt = this.belt;
		var be = belt.get(0);
		var cur = belt.scrollTop();
		if(be.cur==undefined) be.cur = cur;
		if(be.cur <= be.scrollHeight - belt.height()) be.cur += 320;
		belt.stop();
		belt.animate({scrollTop: be.cur}, 500);
		return false;
	};
	
	$('.layText .arwT').click(layTextArwTClickHandler);
	$('.layText .arwB').click(layTextArwBClickHandler);
};

var layCatalogItemInit = function()
{
	var belt = $('.layCatalogItem .belt');
	if(!belt.size()) return;
	var drag = $('.layCatalogItem .drag');
	var sw = (belt.get(0).scrollWidth - belt.width()) / 835;
	var syncScrollPanel = function(){ drag.css('left', Math.floor(belt.scrollLeft() / sw) + 'px'); };
	syncScrollPanel();
	var dragHandler = function(event, ui){ belt.scrollLeft(ui.position.left * sw); };
	drag.draggable({'axis': 'x', 'containment': 'parent', 'drag': dragHandler});
	$('.layCatalogItem .scroll .arwL').click(function(){ belt.scrollLeft(belt.scrollLeft() - 200); syncScrollPanel(); });
	$('.layCatalogItem .scroll .arwR').click(function(){ belt.scrollLeft(belt.scrollLeft() + 200); syncScrollPanel(); });
};


// *** 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()
{
	$('a.darkbox').click(darkbox_cb);
	$("a[rel='colorbox']").colorbox({transition:"fade"});
	layTextInit();
	$(window).load(function()
	{
		layCatalogItemInit();
		if(document.location.search.indexOf('act=print')!=-1) window.print();
	});
});
