$.fn._fadeIn = function(speed, callback) {
	if ($.browser.msie && parseFloat($.browser.version) < 9) {
		this.show();
		if (typeof callback == 'function') {
			callback();
		}
	} else {
		this.fadeIn(speed, callback);
	}
}
$.fn._fadeOut = function(speed, callback) {
	if ($.browser.msie && parseFloat($.browser.version) < 9) {
		this.hide();
		if (typeof callback == 'function') {
			callback();
		}
	} else {
		this.fadeOut(speed, callback);
	}
}

function InstallCtrl() {
	if($(".navigation-ctrl").length != 1) return;
	var $n = $(".navigation-ctrl");
	
	$(document).bind("keydown", function(event){
		if (event.ctrlKey) {
			var link = null;
			switch (event.keyCode)
			{
				case 0x25:
					link = $(".prevLink", $n).attr("href");
					break;
				case 0x27:
					link = $(".nextLink", $n).attr("href");
					break;
			}
		
			if (link) document.location = link;
		}
	});
}
function InstallYandex(elem)
{
	if (typeof elem == "undefined") {
		if (!$("#yandex-map").length) {
			return;
		}
		elem = $("#yandex-map");
	}
	if(elem.data("yandex") == 1) { 
		return; 
	}
	
	var fYandexMap = function($yandexMap) {
		$yandexMap.html("");
		var map;
		var points = $yandexMap.attr("points").split(",");
		for(i in points) {
			points[i] = parseFloat(points[i]);
		}
		map = new YMaps.Map( $yandexMap[0] );
		map.setCenter( new YMaps.GeoPoint( points[0], points[1] ), 13, YMaps.MapType.MAP );
		Zoom = new YMaps.Zoom();
		TypeControl = new YMaps.TypeControl();
		TypeControl.removeType(YMaps.MapType.HYBRID);
				
		if ($yandexMap.attr("baloon").length) {
			var content = document.createElement('span');
			content.innerHTML = $yandexMap.attr("baloon");
			map.openBalloon(new YMaps.GeoPoint( points[0], points[1] ), content);
		}
				
		map.addControl(Zoom);
		map.addControl(TypeControl);
	}
	
	fYandexMap(elem);
	elem.data("yandex", 1);
}
function InstallFancybox() {
	$(".content").find("img[fancybox]").each(function(){
		if ($(this).closest("a").length) return;
		$(this).wrap("<a class='fancybox' href='"+$(this).attr("fancybox")+"' />");
		$(this).parent().fancybox({
			overlayShow: true,
			speedIn: '200',
			speedOut: '200'
		});
	});
}
function fixPNG(element){
	var src;
	var width;
	var height;
	if (typeof element != 'object') {
		element = this;
	}
	width = element.getAttribute('width');
	height = element.getAttribute('height');
	if (element.getAttribute('src')) {
		src = element.getAttribute('src');
		element.src = "/images/blank.gif";
		element.width = width;
		element.height = height;
	}
	else {
		element.runtimeStyle.backgroundImage = "";
		src = element.currentStyle.backgroundImage.match(/url\("(.+)"\)/i);
		if (src) {
			src = src[1];
			element.runtimeStyle.backgroundImage = "none";
		}
		else {
		}
	}
	if (src)
		element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
}
function initIeFix() {
	if ($.browser.msie) {
		//fix png
		$('.fixpng').each(fixPNG);
		if($.browser.version < 7) {
			$('.fixpng6').each(fixPNG);
		}
	}
}

function initHoverImageCell()
{
	var targets = $('#main-body .submenu .item')
		.bind('cellHover', function() {
			if ( ! $(this).is(':visible')) {
				targets._fadeOut(200);
				$(this)._fadeIn(300);
			}
		})
		.bind('cellClick', function() {
			//alert($(this).attr('data-link'));
			window.location = $(this).attr('data-link');
		});
	
	$('#main .image-cell a[rel^=#]').hover(function() {
			var $this = $(this);
			var id = $this.attr('rel');
			$this.data('timer', setTimeout(function() {
				$(id).triggerHandler('cellHover');
			}, 200));
		}, function() {
			clearTimeout($(this).data('timer'));
		});
}

function flashShellHover(id) {
	$('#'+id).triggerHandler('cellHover');
}

function flashShellClick(id) {
	$('#'+id).triggerHandler('cellClick');
}

function initLargeTableFix() {
	var cont = $('#main-body .content'),
		items = $('#main-body .content > .text > *'),
		min = cont.attr('clientWidth')
		max = min;
	
	items.each(function() {
		var item = $(this);
		if(max < item.attr('clientWidth')) max = item.attr('clientWidth');
	});
	
	if(max > min) {
		max = max+26+'px';
		cont.css('width', max);
	}
}


function embedThoughtScript() {
	$('.embed-this-thought-script').each(function() {
		$(this).replaceWith($(this).children());
	});
}

function initPrintButton() {
	$('a.print').bind('click', function() {
		window.print();
		return false;
	});
}

$(function() {
	embedThoughtScript();
	initIeFix();
	initHoverImageCell();
	initLargeTableFix();
	initPrintButton();
});
