var g_IE = navigator.userAgent.toString().match(/MSIE\s*([\.0-9]+)/);

function windowopen(a,b,c) {
	nwindow=window.open(a,b,c);
	nwindow.focus();
}

function InstallPngFix() {
	if (($.browser.msie)&&(parseInt(jQuery.browser.version) < 7)){
		$('img[src*=png], .png').each(function(){
			$(this).css('behavior', 'url(js/iepngfix.htc)');
		});
	}
}
function InstallZebra(){
	$('.zebra tr:even').addClass('even');
	$('.zebra li:even').addClass('even');
	$('.zebra tr:odd').addClass('odd');
	$('.zebra li:odd').addClass('odd');
	
	$('.sortZebra tbody tr:even').addClass('even');
	$('.sortZebra tbody tr:odd').addClass('odd');
	
	$('.sortZebra tbody tr.none').removeClass('odd');
}

function loginError(message){
	if (message.length > 0) {
		$("#open").click();
		$('#errorContainer').html(message);
		$('#errorContainer').show();
	}
}

function PlusMinusDel(){
	$('table#buy img[alt=up]').click(function(){
		$('#recalButton').removeClass('greyButton').addClass('button');
		var currentInput = $(this).prevAll('input');
		var oldValue = parseInt(currentInput.attr('value'));
		if (oldValue<999){
			currentInput.val(oldValue+1);
		}
		
	})
	$('table#buy img[alt=down]').click(function(){
		$('#recalButton').removeClass('greyButton').addClass('button');
		var currentInput = $(this).prevAll('input');
		var oldValue = parseInt(currentInput.attr('value'));
		if (!(oldValue<=1)){
			currentInput.attr('value',oldValue-1);
		}
	})
	$('table#buy img[alt=delete]').click(function(){
		var currentTr = $(this).parents('tr:first');
		var currentInput = $(currentTr).children('td.counter').children('input');
		currentInput.val(0);
		parent.RemovedFromCart(currentInput.attr('name'));
		currentTr.hide();
		CartSubmit();
	})
}

function authorization(form)
{
	var defaultLoginValue = $('#hdnDefaultLoginValue').val();
	var defaultPasswordValue = $('#hdnDefaultPasswordValue').val();

	var loginValue = $('#txtLogin').val();
	var passwordValue = $('#txtPassword').val();


	if ( (loginValue == defaultLoginValue || !(loginValue.length > 0)) ||  (passwordValue == defaultPasswordValue || !(passwordValue.length > 0))  )
	{
		if (loginValue == defaultLoginValue || !(loginValue.length > 0))
			$('#txtLogin').css('border', '1px solid red');
		else
			$('#txtLogin').css('border', 'none');

		if (passwordValue == defaultPasswordValue || !(passwordValue.length > 0))
			$('#txtPassword').css('border', '1px solid red');
		else
			$('#txtPassword').css('border', 'none');


		loginError($('#hdnEmptyAuthirizationDataMessage').val());
		return false;
	}

	return true;
}

function focused(obj,defaultValue){
	call_focused = true;
	if (obj.attr('value') == defaultValue) {
		obj.attr('value','');
	}
}
function blured(obj,defaultValue){
	call_focused = false;
	Callback_timer();
	if (obj.attr('value').replace(/ /g,'')=='') {
		obj.attr('value',defaultValue);
	}
}

function noneScroll(){
	alert($(this).parent('div').find('ul').css('margin-left'))
}

function OnDelete(url, txt) {
	if (confirm(txt)) document.location.href = url;
}

function createScroll(){

		$('div.scrollRight').hover(
			function(){$(this).addClass('scrollRightHover')},
			function(){$(this).removeClass('scrollRightHover')}
		)

		$('div.scrollLeft').hover(
			function(){$(this).addClass('scrollLeftHover')},
			function(){$(this).removeClass('scrollLeftHover')}
		)

		$('div.scrollArea.Horizontal ul').each(function(e){
			if ($(this).find('li').length*$(this).find('li:first').width()<550){
				$(this).parent('div').prev('div.scrollRight').addClass('scrollNone')
									 .prev('div.scrollLeft').addClass('scrollNone');
			}else{
				$(this).parent().before('<div class="transpleft"></div>');
				$(this).parent().before('<div class="transpright"></div>');
			}
		})
	$('div.scrollRight').click(function(){
		var scrollArea = $(this).parent('div');
		var scrollUl = $(this).parent('div').find('ul');
		var curMarg = parseInt(scrollUl.css('margin-left'));
		var sizeItem =  parseInt(scrollUl.find('li:first').width());
		var items = scrollUl.find('li').length;
		var seeItem =  Math.ceil((parseInt($(this).nextAll('div.scrollArea').width()))/sizeItem);
		var el = $(this);

		if ((sizeItem*(items+1)+curMarg-550) >= sizeItem && !$(this).hasClass('scrollLock')) {
			if(!$(this).hasClass('scrollLock'))
				$(this).addClass('scrollLock');
			$(this).parent('div').find('ul').animate({
				"marginLeft": "-=" + sizeItem
			}, 600, function(){
				el.removeClass('scrollLock');
			});
		}

//		$('.console').append(' ss= '+(sizeItem*items+curMarg-550)+'<br>');
		$(this).parent().find('.scrollLeft').removeClass('scrollNone');
		if(sizeItem*items+curMarg-550<=sizeItem){
				$(this).parent().find('.scrollRight').addClass('scrollNone');
				//$(this).parent('div').find('ul').animate({
				//	"marginLeft": "0px"
				//}, "fast");

		}
	})

	$('div.scrollLeft').click(function(){
		var scrollUl = $(this).parent('div').find('ul');
		var curMarg = parseInt(scrollUl.css('margin-left'));
		var sizeItem =  parseInt(scrollUl.find('li:first').width());
		var items = scrollUl.find('li').length;
		var el = $(this);
		
		if (curMarg < -50 && !$(this).hasClass('scrollLock')) {
			if(!$(this).hasClass('scrollLock'))
				$(this).addClass('scrollLock');
			$(this).parent('div').find('ul').animate({
				"marginLeft": "+=" + sizeItem
			}, 600, function(){
				el.removeClass('scrollLock');
			});
		}
		if ((curMarg >=-sizeItem)&&(sizeItem*items>550)){
			$(this).parent().find('.scrollRight').removeClass('scrollNone');
			$(this).parent().find('.scrollLeft').addClass('scrollNone');
			var stophere=12;
				//$(this).parent('div').find('ul').animate({
				//	"marginLeft": -sizeItem*items+545+"px"
				//}, "fast");
		}

	})
	
	$('div.scrollLeft').click();
}



function createScroll_big(){

	var width = 730;
	
		$('div.scrollRight_big').hover(
			function(){$(this).addClass('scrollRightHover_big')},
			function(){$(this).removeClass('scrollRightHover_big')}
		)

		$('div.scrollLeft_big').hover(
			function(){$(this).addClass('scrollLeftHover_big')},
			function(){$(this).removeClass('scrollLeftHover_big')}
		)

		$('div.scrollArea_big ul').each(function(e){
			if ($(this).find('li').length*$(this).find('li:first').width()<width){
				$(this).parent('div').prev('div.scrollRight_big').addClass('scrollNone_big')
									 .prev('div.scrollLeft_big').addClass('scrollNone_big');
			}
		})

	$('div.scrollRight_big').click(function(){
		var curMarg = parseInt($(this).parent('div').find('ul').css('margin-left'));
		var sizeItem =  parseInt($(this).parent('div').find('ul').find('li:first').width());
		var items = $(this).parent('div').find('ul').find('li').length;
		var seeItem =  Math.ceil((parseInt($(this).nextAll('div.scrollArea').width()))/sizeItem);

		if ((sizeItem*(items+1)+curMarg-width) >= sizeItem) {
			$(this).parent('div').find('ul').animate({
				"marginLeft": "-=" + sizeItem
			}, 800);
		}

//		$('.console').append(' ss= '+(sizeItem*items+curMarg-550)+'<br>');
		if(sizeItem*items+curMarg-width<=0){
				$(this).parent('div').find('ul').animate({
				"marginLeft": "0px"
			}, "fast");

		}
	})

	$('div.scrollLeft_big').click(function(){
		var curMarg = parseInt($(this).parent('div').find('ul').css('margin-left'));
		var sizeItem =  parseInt($(this).parent('div').find('ul').find('li:first').width());
		var items = $(this).parent('div').find('ul').find('li').length;
		if (curMarg < -50) {
			$(this).parent('div').find('ul').animate({
				"marginLeft": "+=" + sizeItem
			}, 800);
		}
		if ((curMarg >-50)&&(sizeItem*items>width)){
			var stophere=12;
				$(this).parent('div').find('ul').animate({
				"marginLeft": -sizeItem*items+width-5+"px"
			}, "fast");
		}

	})
}

function CreateVerticalScroll() {
	$('div.scrollArea.Vertical ul').each(function(e){
		if ($(this).find('li').length*$(this).find('li:first').height()<$(this).parent().height()){
			$(this).siblings('div.scrollTop, div.scrollBottom').addClass('scrollNone');
		}else{
			$(this).parent().before('<div class="transptop"></div>');
			$(this).parent().after('<div class="transpbot"></div>');
		}
	})

	$('div.scrollBottom').click(function(){
		var height = $(this).siblings('div.scrollArea').height();
		var ul = $(this).siblings('div.scrollArea').children('ul');
		var curMarg = parseInt($(ul).css('margin-top'));
		var sizeItem =  parseInt($(ul).find('li:first').height());
		var items = $(ul).find('li').length;
		var el = $(this);
		
		if (curMarg <= -sizeItem && !$(this).hasClass('scrollLock')) {
			if(!$(this).hasClass('scrollLock'))
				$(this).addClass('scrollLock');
			$(ul).animate({
				"marginTop": "+=" + sizeItem
			}, 600, function(){
				el.removeClass('scrollLock');
			});
		}
		//else {
			//$(this).addClass('scrollNone');
			//$(ul).animate({
			//	"marginTop": -sizeItem*items+height+"px"
			//}, 600);

		//}
		if(curMarg>=-sizeItem){
			$(this).addClass('scrollNone');
		}else{
			$(this).parent().find('.scrollTop').removeClass('scrollNone');
		}
	})

	$('div.scrollTop').click(function(){
		var height = $(this).siblings('div.scrollArea').height();
		var ul = $(this).siblings('div.scrollArea').children('ul');
		var curMarg = parseInt($(ul).css('margin-top'));
		var sizeItem =  parseInt($(ul).find('li:first').height());
		var items = $(ul).find('li').length;
		var el = $(this);

		if (sizeItem*(items - 1) + curMarg >= height && !$(this).hasClass('scrollLock')) {
			if(!$(this).hasClass('scrollLock'))
				$(this).addClass('scrollLock');
			$(ul).animate({
				"marginTop": "-=" + sizeItem
			}, 600, function(){
				el.removeClass('scrollLock');
			});
		}
		//else {
			//$(this).addClass('scrollNone');
			//$(ul).animate({
			//	"marginTop": "0px"
			//}, 600);
		//}
		if(sizeItem*(items - 1) + curMarg <= height){
			$(this).addClass('scrollNone');
		}else{
			$(this).parent().find('.scrollBottom').removeClass('scrollNone');
		}

	})
	$('div.scrollBottom').click();
}


function getWndSize() {
 var wnd = {width: 0, height:0};
 if( typeof( window.innerWidth ) == 'number' ) {
     //Non-IE, normal browsers
  wnd.width = window.innerWidth;
  wnd.height = window.innerHeight;
 } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
     //IE 6+ in 'standards compliant mode'
  wnd.width = document.documentElement.clientWidth;
  wnd.height = document.documentElement.clientHeight;
 } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
  //IE compatible
  wnd.width = document.body.clientWidth;
  wnd.height = document.body.clientHeight;
 }
 return wnd;
}

function getDocHeight() {
    return Math.max(
        Math.max(document.body.scrollHeight, document.documentElement.scrollHeight),
        Math.max(document.body.offsetHeight, document.documentElement.offsetHeight),
        Math.max(document.body.clientHeight, document.documentElement.clientHeight)
    );
}

function getScrollXY() {
 var scroll = {x:0, y:0};
 if( typeof( window.pageYOffset ) == 'number' ) {
  //Netscape compliant
  scroll.y = window.pageYOffset;
  scroll.x = window.pageXOffset;
 } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
  //DOM compliant
  scroll.y = document.body.scrollTop;
  scroll.x = document.body.scrollLeft;
 } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
     //IE
  scroll.y = document.documentElement.scrollTop;
  scroll.x = document.documentElement.scrollLeft;
 }
 return scroll;
}

function logoutMe() {
	document.LoginForm.event.value = 'logout';
	document.LoginForm.submit();
}

// Create a cookie with the specified name and value.
function SetCookie(sName, sValue)
{
  document.cookie = sName + "=" + escape(sValue);
  // Expires the cookie in one month
  var date = new Date();
  date.setMonth(date.getMonth()+1);
  document.cookie += ("; expires=" + date.toUTCString()); 
}
// Retrieve the value of the cookie with the specified name.
function GetCookie(sName)
{
  // cookies are separated by semicolons
  var aCookie = document.cookie.split("; ");
  for (var i=0; i < aCookie.length; i++)
  {
    // a name/value pair (a crumb) is separated by an equal sign
    var aCrumb = aCookie[i].split("=");
    if (sName == aCrumb[0]) 
      return unescape(aCrumb[1]);
  }
  // a cookie with the requested name does not exist
  return null;
}
// Delete the cookie with the specified name.
function DelCookie(sName)
{
  document.cookie = sName + "=; expires=Fri, 31 Dec 1999 23:59:59 GMT;";
}

function tonumber(value){
	return validn(value.trim());
}

function validn(value)
{
	if(isNaN(value)) value = 1;
	value = Math.abs(Math.round(value));
	if(value == 0) value = 1;
	if(value > 999) value = 999;
	return value;
}

String.prototype.trim = function()
{
	return this.replace(/^\s+|\s+$/g,"");
}
String.prototype.ltrim = function()
{
	return this.replace(/^\s+/,"");
}
String.prototype.rtrim = function()
{
	return this.replace(/\s+$/,"");
}


function scroll_window(){
	var scrollTop = 0;
	if($.browser.safari) scrollTop = $('#bodyid')[0].scrollTop;
	else scrollTop = document.body.scrollTop ? document.body.scrollTop : document.documentElement.scrollTop;
	SetCookie('scrolltop', scrollTop);		
}

function scrollto(){	
	//alert($.browser.msie+" "+$.browser.opera+" "+$.browser.mozilla+" "+$.browser.safari+" "+$.browser.webkit);	
	var scrollTop = GetCookie('scrolltop');
	if(document.body.scrollTop){
		document.body.scrollTop = scrollTop;
	}else{
		if($.browser.safari) document.getElementById('bodyid').scrollTop = scrollTop;
		else document.documentElement.scrollTop = scrollTop;
	}		
}
window.onscroll = scroll_window;

function LoadGalleria(id, pos) {
	pos = pos || 1;
	if ($('#galleria_container').length == 0) {
		jQuery("<div>", {
		    id: "galleria_container"
		}).appendTo("body");
		jQuery("<script>", {
		    type: "text/javascript",
		    src: "/js/galleria.js"
		}).appendTo("body");
		// Load theme
		Galleria.loadTheme('/js/galleria/themes/classic/galleria.classic.js');
	}
	$.get('/galleria/load/' + id + '/' + pos, function(data) {
		$('#galleria_container').html(data);
		RenderGallery();
	});
}

function LoadGalleriaFromArticle(id, pos) {
	pos = pos || 1;
	if ($('#galleria_container').length == 0) {
		jQuery("<div>", {
		    id: "galleria_container"
		}).appendTo("body");
		jQuery("<script>", {
		    type: "text/javascript",
		    src: "/js/galleria.js"
		}).appendTo("body");
		// Load theme
		Galleria.loadTheme('/js/galleria/themes/classic/galleria.classic.js');
	}
	$.get('/galleria/loadfromarticle/' + id + '/' + pos, function(data) {
		$('#galleria_container').html(data);
		RenderGallery();
	});
}

function RenderGallery() {
    $('#galleria_' + GalleryID).galleria({
        data_source: GalleryData,
        thumb_crop: 'height',
        show: GalleryPosition
    }); 
}

function RemovedFromCart(id) {
	var parent = $('.' + id);
	if ($(parent).hasClass('bought')) {
		$(parent).removeClass('bought');
		var text = $(parent).find('.buy_text');
		if (text.length > 0) {
			$(text).addClass('updatingtextinprogress');
			$.get('/' + language_code + '/callback/AjaxGetNotBoughtText/' + id, function(data){
				var obj = $('.updatingtextinprogress');
				$(obj).text(data);
				$(obj).removeClass('updatingtextinprogress');
			});
		}
	}
}

//Mark bought goods online
$('.topPrice_big a, .topPrice a').live('click', function(){
	var parent = $(this).parent();
	if ($(parent).hasClass('bought')) return;
	$(parent).addClass('bought');
	var text = $(this).find('.buy_text');
	if (text.length == 1) {
		$(text).addClass('updatingtextinprogress');
		$.get('/' + language_code + '/callback/AjaxGetBoughtText', function(data){
			var obj = $('.updatingtextinprogress');
			$(obj).text(data);
			$(obj).removeClass('updatingtextinprogress');
		});
	}
});

function setScrollPos(val){
	scrollposition = val;
	return;
}

function ResizeCart(top,ignor) {
	window.cart_resize_timeout = 0;
	$('#cboxWrapper').css('height', '100%');
	$('#cboxContent')[0].style.height = null;
	$('#cboxLoadedContent').css('height', '100%');
	var curr_height = $('#cboxIframe').height();
	var cart = window[$('#cboxIframe').attr('name')];
	//var H_height = cart.document.body.clientHeight;
	if (cart !== undefined) {
		//For mozilla
		if (cart.scrollMaxY !== undefined) {
			var cart_wnd_scroll = cart.scrollMaxY;
			if (cart_wnd_scroll > 0) {
				$('#cboxIframe').css('height', curr_height + cart_wnd_scroll + 'px');
			}
			$('#colorbox').css('height', curr_height + cart_wnd_scroll + 100 + 'px');
		}
		//For IE and webkit
		if (cart.document.body.clientHeight != curr_height) {
			$('#cboxIframe').css('height', cart.document.body.clientHeight + 'px');
			$('#cboxContent').css('height', cart.document.body.clientHeight + 40 + 'px');
			
		}
		$('#colorbox').css('height', cart.document.body.clientHeight + 100 +'px');
	}else{
		$('#cboxIframe').css('height', '434px');
	}
	$('#cboxMiddleLeft').css('height', $('#cboxContent').height() + 'px');
	$('#cboxMiddleRight').css('height', $('#cboxContent').height() + 'px');
	var bottom = $('#cboxContent').height() + $('#cboxContent').offset().top;
	var wnd_size = getWndSize();
	if(ignor!==undefined)
		ignor = 0;
	else
		if(scrollposition != null)
			return;
	var wnd_scroll = $(window).scrollTop();
	if (window.cart_scrollback == -1) window.cart_scrollback = wnd_scroll;
	if(top!==undefined)
		$(window).scrollTop($('#cboxContent').offset().top-50);
	else
		$(window).scrollTop(bottom - wnd_size.height + 50);

}

var cart_scrollback = -1;

$('.colorbox').live('click', function(event){
	event.preventDefault();
	$().colorbox({
			href: $(this).attr('href'),
			width:"800px",
			iframe:true,
			onComplete:function(){
				setposition(null);
				var cart = window[$('#cboxIframe').attr('name')];
				$(cart).load(function() {
					parent.ResizeCart();
				});
			},
			onClosed:function(){
				$(window).scrollTop(window.cart_scrollback);
				window.cart_scrollback = -1;
			}
		}
	);
});

$('.colorboxsmall').live('click', function(event){
	event.preventDefault();
	$().colorbox({
			href: $(this).attr('href'),
			width:"510px",
			iframe:true,
			onComplete:function(){
				var cart = window[$('#cboxIframe').attr('name')];
				$(cart).load(function() {
					parent.ResizeCart();
				});
			},
			onClosed:function(){
				$(window).scrollTop(window.cart_scrollback);
				window.cart_scrollback = -1;
			}
		}
	);
});
$('.colorboxinline').live('click', function(event){
	event.preventDefault();
	$().colorbox({
			href: $(this).attr('href'),
			width:"510px",
			height:"280px",
			inline:true,
			onComplete:function(){
				var cart = window[$('#cboxIframe').attr('name')];
				console.log(cart);
				$(cart).load(function() {
					parent.ResizeCart();
				});
			},
			onClosed:function(){
				$(window).scrollTop(window.cart_scrollback);
				window.cart_scrollback = -1;
			}
		}
	);
});

$(window).resize(function() {
	if (this.cart_resize_timeout > 0) {
		clearTimeout(this.cart_resize_timeout);
	}
	this.cart_resize_timeout = setTimeout(ResizeCart, 100);
});

var GetElementUnderTheMouse = function() {
	return null;
}
function setposition(val){
	scrollposition = val;
}

function GetAllEmpikShopsWithCityName(lng) {
    $.get('/catalog/getshops/' + lng + '/', function(data) {
		$('#emshops').html(data);
	});
}

$(document).ready(function(e) {
	$(window).scroll(function(){
		setposition($(window).scrollTop());
	});
	try {
		$('body').mouseover(function(evt){
			//If only for IE.
			if (evt.srcElement) var element = evt.srcElement;
			else var element = evt.originalTarget;
			GetElementUnderTheMouse = function() {
				return element;
			}
		});
		$('input:text, input:password, textarea, .inCenter select').addClass('inText');
		InstallPngFix();
		InstallZebra();
		PlusMinusDel();
		//createScroll();
		//createScroll_big();
		//CreateVerticalScroll();
		$('div.scrollArea.Horizontal ul').addClass('jcarousel-skin-tango').jcarousel({
			scroll:1
		});
		//$('div.jcarousel-skin-tango ul').before('<div class="transpleft"></div>');
		//$('div.jcarousel-skin-tango ul').before('<div class="transpright"></div>');
		$('div.scrollArea.Vertical ul').addClass('jcarousel-skin').jcarousel({
			scroll:1,
			vertical: true
		});
		//$('div.jcarousel-skin ul').before('<div class="transptop"></div>');
		//$('div.jcarousel-skin ul').before('<div class="transpbot"></div>');
		
		$('#changeLang').hover(
			function(){$(this).addClass('active')},
			function(){$(this).removeClass('active')}
		)

        $('.shac').click(function(){
            $(this).prev().toggle('slow');
            $(this).next().toggle('slow');
            if($(this).html()=='&lt;&lt; Скрыть') $(this).html('Показать все &gt;&gt;');
                else $(this).html('&lt;&lt; Скрыть');
        })

		$('.button #LoginButton').click(function(){
			loginError();
			return false;
		});

		// Expand Panel
		$("#open").click(function(){
			$("#LoginPanel").slideDown("normal");
		});

		// Collapse Panel
		$("#close").click(function(){
			$("#LoginPanel").slideUp("normal");
		});

		// Switch buttons from "Log In | Register" to "Close Panel" on click
		$("#toggle a").click(function () {
			$("#toggle a").toggle();
		});

		if ($('#hdnIpBlocked').val() == 'true')
		{
			loginError($('#hdnIpBlockedMessage').val());
		}
		else
		if ($('#hdnIncorrectSignIn').val() == 'true')
		{
			var message = $('#hdnIncorrectSignInMessage').val() + '<br /><br />' + $('#hdnTrySignInCountInfo').val();
			loginError(message);
		}
		else
		if ($('#hdnAuth_not_validated_user').val() == 'true')
		{
			var message = $('#hdnAuth_not_validated_user_message').val() + '<br />';
			loginError(message);
		}
		else
		if ($('#hdnAuth_denied_from').val() !== undefined){
			if ($('#hdnAuth_denied_from').val() != 'false')
			{
				var message = $('#hdnAuth_denied_message').val();
				loginError(message);
			}
		}
		
		//Hints
		var timer = 0;
		$('.scrollArea li img, .has_hint img').hover(function(){
			if (!timer) {
				var hint = $(this).closest('li, .has_hint').children('.hint').eq(0);
			} else {
				var tigger_later = this;
				setTimeout(function(){
					$(tigger_later).trigger('mouseenter');
				}, 200);
			}
			if (hint !== undefined && hint.length == 1) {//If element has a hint
				var animation_time = 200;
				if (g_IE != null) animation_time = 0;
				var img = this;
				var HandleHint = function() {
					var elem = GetElementUnderTheMouse();
					var hint_layout = $('#hint_layout');
					//Mouse is on the image or hint
					if (elem == img || jQuery.inArray(elem, $(hint_layout).find('*')) > -1) {
						if ($(hint_layout).length == 0) {//No hint yet
							//Create layout for hint
							hint_layout = jQuery("<div>", {
						    	id: 'hint_layout'
							});
							//Add class is only for IE, it can't be defined directly in code before due to some IE's developers madness.
							$(hint_layout).addClass('hint');
							//Copy it
							$(hint_layout).html($(hint).html());
							//Colorbox();
							//Show image (previously hidden to reduce onload traffic)
							var big = $(hint_layout).find('img.hint_big');
							$(big).attr('src', $(big).attr('rel'));
							//Maintain ids
							$(hint_layout).find('.inactive_id').each(function(i, el){
								$(el).attr('id', $(el).attr('id').toString().substr(9));
							});
							$(hint_layout).appendTo('body');
							//Locate proper position to show hint
							var wnd = getWndSize();
							if ($(img).offset().left + $(img).width() + 308 < wnd.width) {
								var x = $(img).offset().left + $(img).width() - 82;
								$(hint_layout).addClass('right');
							} else {
								var x = $(img).offset().left - 308;
							}
							var y = $(img).offset().top + $(img).height() -18;
							$(hint_layout).css('left', x + 'px');
							$(hint_layout).css('top', y + 'px');
							$(hint_layout).fadeIn(animation_time);
						}
						timer = setTimeout(HandleHint, 400);
					} else {//Mose were moved out: hide hint
						clearTimeout(timer);
						timer = 0;
						$(hint_layout).fadeOut(animation_time, function(){
							//store element's html in hidden container since it might have been changed.
							$(hint).html($(hint_layout).html());
							//Remove layout
							$(hint_layout).detach();
						});
					}
				};
				timer = setTimeout(HandleHint, 400);
			}
		}, function(){});

	} catch(e) {
//		alert(e);
	}

});
