try{
	document.execCommand("BackgroundImageCache", false, true);
} catch(e) {}

function fixPNG(element) {
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
		var src;
		if (element.tagName == 'IMG') {
			if (/\.png$/.test(element.src)) {
				src = element.src;
				element.src = "img/s.gif";
			}
		} else {
			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i)
			if (src) {
				src = src[1];
				element.runtimeStyle.backgroundImage = "none";
			}
		}

		var re_scale_mode = /iesizing\-(\w+)/;
		var m = re_scale_mode.exec(element.className);

		var scale_mode = (m) ? m[1] : 'crop';

		if (src) {
			element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='" + scale_mode + "')";
		}
	}
}

// Fix heights
$(document)
	.ready(function() {
		// Add styles to CMS-built tables
		$('table[class!=layout] tr:even').addClass('even');
		$('tr:first', $('table').not('.layout')).addClass('header');

		if ($('#promo').length) {
			initPromo();
		}

		if ($.browser.msie) {
			$('.login-form input').css({position: 'relative', top: '-1px'});
		}

		if ($('#contact_us_container').length) {
			initContactUs();
		}

		fixHeights();
		// need to call it two times, otherwise FF3 sometimes resized element incorrectly
		// (depending on images inside elements). setting it to window.onload doesn't help either
		fixHeights();

		$('.top-level-item-v-sep, .top-level-item-h-sep').show();

		$('.corner-wrap').css('display', 'block'); // this is for IE6 to re-render the corners, otherwise they are left on old position

		initCaseStudiesScroller();

	});

function initCaseStudiesScroller()
{
	if (typeof(CaseStudies) == 'undefined') return;
	$("<img>").attr("src", 'img/prev_arrow_active.gif');
	$("<img>").attr("src", 'img/next_arrow_active.gif');
	$('#case_studies_scroller').scrollLeft(0);
	$('#prev_case_study').hide();
	$("#prev_case_study")
		.hover(function() {$(this).attr('src', "img/prev_arrow_active.gif")}, function() {$(this).attr('src', "img/prev_arrow.gif")})
		.click(function() {
			ScrollCaseStudies(195);
		});
	$("#next_case_study")
		.hover(function() {$(this).attr('src', "img/next_arrow_active.gif")}, function() {$(this).attr('src', "img/next_arrow.gif")})
		.click(function() {
			LoadCaseStudies();
			ScrollCaseStudies(-195);
		});
	if (CaseStudies.total > CaseStudies.displayed) {
		$("#next_case_study").show();
	}

}

function ScrollCaseStudies(offset)
{
	$('#case_studies_content').queue(function() {
		var target = $('#case_studies_content').position().left  + offset;
		$('#case_studies_content').animate({left: target}, 700, 'easeOutQuad');
		CaseStudies.current += offset < 0 ? 1 : -1;
		if (CaseStudies.current > 1) {
			$('#prev_case_study').show();
		}
		else {
			$('#prev_case_study').hide();
		}
		if (CaseStudies.total >= (CaseStudies.current + CaseStudies.displayed)) {
			$('#next_case_study').show();
		}
		else {
			$('#next_case_study').hide();
		}
		$(this).dequeue();
	});
}

function LoadCaseStudies()
{
	if (CaseStudies.loaded >= CaseStudies.total) {
		return; // everything is loaded
	}
	number = 4;
	$.get('../../load_case_studies.html?start='+CaseStudies.loaded+'&number='+number+'&mode='+CaseStudies.mode+'&tag_id='+CaseStudies.tag+'&related_page='+CaseStudies.related_page+'&ajax=yes', function(data) {
		CaseStudies.loaded += number;
		$('#case_studies_content').css('width', $('#case_studies_content').width() + number*195).append(data);
	});
}

function openCaseStudy(params)
{
	window.location.href = params.link + (params.mode != '' ? '?mode='+params.mode+'&tag_id='+params.tag+'&related_page='+params.related_page : '');
	return false;
}

function fixHeights()
{
	$('.fix-height').each(function(i) {
		var max = 0;
		$('> *', this)
			.each(function(ii) {
				if ($(this).outerHeight() > max) {
					max = $(this).outerHeight();
				}
			})
			.not('.dont-fix-height, :has(.dont-fix-height)').each(function(ii) {
				var outer = this;
//				alert('processing '+$(this).attr('className')+' '+$(this).text())
				if (!$('.inner-block', this).each(function(iii) {
					// expand the height of this inner block by the difference between max (outer height of most tall block) and outer height of outer block
					$(this).height( $(this).height() + max - $(outer).outerHeight() );
				}).length)
				{
					// of there is no inner block - expand the height of this block, but cut off its border and paddings (diff between outer and simple height)
					$(this).height(max - ($(this).outerHeight() - $(this).height()));
				}
			});
	});
}

function switchPromo(target_num, quick) {
	var target = $('#promo_title' + target_num);
	var current = $('#promo_title' + current_promo);

	/*if (target_num == 1 && current_promo == 4) {
		quick = 1;
	}*/

	var duration = promo_transition_effect;
	if ($('#promo_selector').css('display') == 'none') {
		duration = 1;
	}

	var target_top = target.position().top - 6;

	$('#promo_selector:hidden').css('top', target_top);

	if (quick) {
		$('#promo_selector').stop().show();
		fadeImg($('#promo_link img:visible'), promo_blocks[target_num].image, duration, null, target_num);
		fadeBackground('.promo-back-container:visible', promo_blocks[target_num].rightClass, duration);
		$('#promo_selector').css('top', target_top).show();
		target.animate({color: '#1b91b1'}, duration/2);

//		$('#promo_selector').stop().show().fadeOut(duration/2, function() {
//			fadeImg($('#promo_link img:visible'), promo_blocks[target_num].image, duration, null, target_num);
//			fadeBackground('.promo-back-container:visible', promo_blocks[target_num].rightClass, duration);
//			$(this).css('top', target_top).show(); //fadeIn(duration)
//			target.animate({color: '#1b91b1'}, duration/2);
//		} );

		current.animate({color: '#fff'}, duration/2);
	}
	else {
		current.animate({color: '#fff'}, duration/2);
		$('#promo_selector').stop().show().animate( { top: target_top }, duration, '', function() {

		} );
		target.animate({color: '#1b91b1'}, duration);
		fadeImg($('#promo_link img:visible'), promo_blocks[target_num].image, duration, null, target_num)
		fadeBackground('.promo-back-container:visible', promo_blocks[target_num].rightClass, duration);
	}
	$('.corner-wrap .transp-corner').css('display', 'block').css('z-index', 10); // this is for IE6 to re-render the corners, otherwise they are left on old position
	$('#promo_link').attr('href', promo_blocks[target_num].promoLink);
	$('#promo_link').attr('target', promo_blocks[target_num].openInNewWindow ? '_blank' : '_self');
	current_promo = parseInt(target_num);
}

function fadeImg(img, target_src, duration, callback, target_num)
{
	if ($(img).attr('src') == target_src) return;

	var the_img = $(img);

	tmp = $('[src='+target_src+']', img.parentNode);
	if (!tmp.length) {
		var tmp = the_img.clone().css({position: 'absolute', display: 'none'}).attr('src', target_src);
		the_img.before(tmp);
	}
	tmp.fadeIn(duration, callback).attr('alt', promo_blocks[target_num].name);
	the_img.fadeOut(duration);
}

function fadeBackground(elem, targetClass, duration)
{
	if ($(elem+':visible').hasClass(targetClass)) {
		$('.corner-wrap .transp-corner').css('display', 'none').css('display', 'block'); // this is for IE6 to re-render the corners, otherwise they are left on old position
		return;
	}

	var the_elem = $(elem);

	tmp = $('.'+targetClass, the_elem[0].parentNode);
	if (!tmp.length) {
		var tmp = the_elem.clone().empty().css({display: 'none'}).removeClass().addClass('promo-back-container '+targetClass);
		the_elem.after(tmp);
	}
	tmp.fadeIn(duration, function() {
		$('.corner-wrap').css('display', 'none').css('display', 'block'); // this is for IE6 to re-render the corners, otherwise they are left on old position
	});
	the_elem.fadeOut(duration, function() {
		$('.corner-wrap').css('display', 'none').css('display', 'block'); // this is for IE6 to re-render the corners, otherwise they are left on old position
	});
}

var promo_interval = null;
var promo_count = 0;

function startPromo()
{
	promo_interval = window.setInterval(
	function() {
		var next_num = current_promo+1;
		if (next_num > promo_count) {
			next_num = 1;
			switchPromo(next_num, 1);
		}
		else {
			switchPromo(next_num);
		}
	},
	promo_speed)
}

function initPromo()
{
	pos = $('#promo_right_box').position();
	w = $('#promo_right_box').width();
	h = $('#promo_right_box').height();
	$('.promo-back-container').css({left: pos.left, top: pos.top, width: w, height: h}).show();
	$('#promo_right_box').removeClass().addClass('promo-choice')

	// preload images and count promo_block
	for (var i in promo_blocks) {
		$("<img>").attr("src", promo_blocks[i].image);
		promo_count++;
	}
	switchPromo(1);

	$('.promo-item').click(function() {
		switchPromo(this.id.replace(/^promo_title/, ''));
	});

	startPromo();

	$(window).resize(function() {
		$('.promo-back-container').css('left', $('#promo_right_box').position().left)
	})

	$('#promo')
		.mouseover(function() {
			clearInterval(promo_interval);
		})
		.mouseout(startPromo);
}

var initial_form_left = false;

function initContactUs() {
	if ($.browser.msie) {
		$('#contact_us_form input, #contact_us_form textarea').css({position: 'relative', top: '-1px'});
	}

	$(window).resize(function() {
		var button_offset = $('#contact_us_button').offset();
		initial_form_left = button_offset.left - $('#contact_us_container').width() -2 ;
	})

	$('#contact_us_container').remove().appendTo('body'); // otherwise it will be below other elements in FF2

	$('#contact_us_button, #contact_us_button_active, .contact-trigger').click(
		function ($event) {
			toggle_form('toggle');
		}
	);

	$('#contact_us_close').click(function() {toggle_form('close')});

	$('#contact_us_container *:input')
	.focus(
		function ($event) {
			if($(this).val() == default_values[this.id]) {
				$(this).val('');
			}
		}
	)
	.blur(
		function ($event) {
			if($(this).val() == '') {
				$(this).val( default_values[this.id] );
			}
		}
	);

	var button_offset = $('#contact_us_button').offset();
	initial_form_left = button_offset.left - $('#contact_us_container').width() -2 ;

	$('#send_button').click(
		function ($event) {
			$.post(
				$('#contact_us_form').attr('action'),
				$('#contact_us_form').serializeArray(),
				function ($data) {
					$data = eval('('+$data+')');
					if ($data.status == 'ok') {
						toggle_form('close');
						$('#contact_us_button').attr('src', $('#contact_us_button').attr('src').replace(/(.*img\/contact_us)([0-9]+)\.gif/, "$1_thank_you$2.gif")).unbind('click');
						try {
							pageTracker._trackPageview("contact-us/submitted.html");
						} catch(err) {}
					}
					else {
						var first_set = false;
						for (i in $data.errors) {
							if ($('#'+i).length) {
								$('#'+i)
								.addClass('contact-form-error')
								.attr('title', $data.errors[i])
								.focus(
									function ($event) {
										$(this)
										.removeClass('contact-form-error')
										.attr('title', '');
									}
								);
								if (!first_set) {
									$('#contact_us_error').html($data.errors[i]);
									$('#'+i).focus(function() {
										$('#contact_us_error').html('');
									})
									first_set = true;
								}
							}
						}
					}
				}
			)
		}
	);
}

function toggle_form(mode) {
	var button_offset = $('#contact_us_button').offset();
	var form_offset = $('#contact_us_container').offset();

	var left = button_offset.left - $('#contact_us_container').width() -2 ;
	var top = button_offset.top + $('#contact_us_button').height() - $('#contact_us_container').height();
	$('#contact_us_container').css({left : left+'px', top : top+'px'});
	$('#contact_us_button_active').css({left: button_offset.left - 2, top: button_offset.top});

	if (mode == 'close' || $('#contact_us_container').attr('opened') == 1) {
		$('#contact_us_container').stop().animate({left: (initial_form_left)+'px', width: '0px'}, 'normal', '', function() {
			$('#contact_us_button_active').hide();
			$(this).hide();
		}).attr('opened', 0);
	}
	else {
		try {
			pageTracker._trackPageview("contact-us/opened-form.html");
		} catch(err) {}

		$('#contact_us_button_active').show();
		$('#contact_us_container')
		.stop()
		.animate({left: (initial_form_left-13)+'px', width: '15px'}, 10)
		.animate({left: (initial_form_left-330)+'px', width: '330px'})
		.attr('opened', 1);
	}
}