function objFromRel(selector) {
	var obj = {};
	var el = $(selector);
	var str = el.attr('rel');
	var lines = str.split(';');
	for (line in lines) {
		var v = lines[line].split(':');
		obj[v[0]] = v[1];
	}
	return obj;
}

var links = {};

function nextPrev(direction) {
	if (links[direction].length)
		showWindow(links[direction]);
}

function showWindow(link) {
	links = {
		next: $(link).next('a.lightwindow'),
		prev: $(link).prev('a.lightwindow')
	}
	$('#lightwindow').parent().dialog('destroy');
	$('#lightwindow').remove();
	$('<div title="' + $(link).attr('title') + '" id="lightwindow"><div id="lightwindowButtons"></div></div>')
		.appendTo(document.body)
		.hide();
	$('<img src="' + $(link).attr('href') + '" style="margin: 0 auto; display: block;"/>')
		.prependTo('#lightwindow')
		.load(function() {
			$('#lightwindow').show();

			var buttonsHeight = 0;
			if(links.next.length || links.prev.length){
				buttonsHeight = 50;
			}
			var viewportHeight = $(window).height() - 100 - buttonsHeight;
			var imgHeight = $(this).height();
			if (viewportHeight < imgHeight) {
				$(this).height(viewportHeight);
			}
			$('#lightwindow').width($(this).width());
			$.sfWindow.open('#lightwindow');
		})
	if(links.prev.length){
		$('<a class="fl ui-state-default ui-corner-all ui-icon-prev" href="javascript:nextPrev(\'prev\')"><span class="ui-icon ui-icon-circle-triangle-w"/>' + i18n.prev + '</a>')
			.appendTo('#lightwindowButtons')
	}
	if(links.next.length){
		$('<a class="fr ui-state-default ui-corner-all ui-icon-next" href="javascript:nextPrev(\'next\')">' + i18n.next + '<span class="ui-icon ui-icon-circle-triangle-e"/></a>')
			.appendTo('#lightwindowButtons')
	}

	return false;
}

$(function() {
	$('a.window').livequery('click', function(){
		var anchor = $(this).attr('href').split('#')[1];
		$.sfWindow.open('#'+anchor, {
			width: 550
		});
		return false;
	})

	$('a.lightwindow').click(function(){
		showWindow(this);
		return false;
	});

	$('a.swfwindow').click(function() {
		$('#lightwindow').parent().dialog('destroy');
		$('#lightwindow').remove();
		var obj = objFromRel(this);
		var lightwindow = $('<div title="' + $(this).attr('title') + '" id="lightwindow" />')
			.appendTo(document.body)
			.hide()
			.css({
				width: parseInt(obj.width),
				height: parseInt(obj.height)
			});
		var so = new SWFObject($(this).attr('href'), 'lightwindowid', obj.width, obj.height, '8', '#ffffff');
		so.addParam('allowScriptAccess', 'sameDomain');
		so.addParam('quality', 'high');
		so.addParam('wmode', 'opaque');
		so.write('lightwindow');
		$.sfWindow.open('#lightwindow');
		return false;
	});

	if ($('#banner_topdesign').length && !$.cookie('topdent')) {
		$.sfWindow.open('banner_topdesign', {
			width: 560
		});
		$.cookie('topdent', 'tak');
	}
	$('#footer div.footer_menu a').click(function() {
		if (!$('#seo_content').length)
			$('<div id="seo_content" />').appendTo('body');
		$('#seo_content').load($(this).attr('href'), function() {
			$.sfWindow.open('#seo_content', {width: 600})
		});
		return false;
	});
	$('#content_box a.iso9001Cert').click(function() {
		if ($('#iso_cert').remove())
		$('<img id="iso_cert" title="ISO 9001" />')
			.appendTo('body')
			.hide()
			.load(function() {
				if ($(this).height() > $(window).height()-100)
					$(this).css('height', $(window).height()-100);
				$.sfWindow.open('#iso_cert', {width: $(this).width()});
			});
		$('#iso_cert').attr('src', $(this).attr('href'));
		return false;
	});
	$('#cycle').cycle({
		slideExpr: '.cycleItem',
		pager: '#pager',
		prev: '#prev',
		next: '#next',
		pause: 1,
		pauseOnPagerHover: 1,
		timeout: 6000
	});
});


