function preloadImages(iarr, callback) {
	if (iarr.length > 0) {
		var path = iarr.pop(), img = new Image();
		img.onload = img.onerror = function() {
			preloadImages(iarr, callback);
		};
		img.src = path;
	} else {
		callback();
		return false;
	}
};
preloadImages([
		approot+'images/in/book_bottom.png',
		approot+'images/in/book_content.png',
		approot+'images/in/book_top.png',
		approot+'images/in/logo_rose.png',
		approot+'images/in/rosepart.png',
		approot+'images/gmenu.png',
		approot+'images/cofee.png',
		approot+'images/puzzle.png',
		approot+'images/logophoto.png'
	], function() {});
(function($) {jQuery(document).ready(function(){
	if (mp) {
		$.cookie('top_menu_ul', 0, {expires:-1});
		$.cookie('top_menu_link', 0, {expires:-1});
	}
	var
		gtmp,
		body = $('body'),
		head = $('#header'),
		topmenu = $('#topmenu'), topmenu_span, topmenu_ul, topmenu_t,
		menumap = $('#gmenumap area'),
		lmenu = $('#gmenu'),
		wilmenu = $('#inner_left ul#lmenu'),
		ilmenu = $('#inner_left ul#lmenu > li span'),
		almenu = $('#inner_left ul#lmenu a'),
		innercont = $('#inner_right'),
		tml,
		tmul = $.cookie('top_menu_ul')!=null ? $.cookie('top_menu_ul')*1 : 0,
		tmli = $.cookie('top_menu_link')!=null ? $.cookie('top_menu_link')*1 : (-1),
		breads = $('#breadcrumbs').show(),
		soput = $('#pslider'), soputarr, soputframes, soputwrap, soputpos = 0, soputmax, soputidle, soputtime,
		topblogo = $('#biglogo'),
		cururl = (document.location.href).split('/');

cururl = cururl.pop();	
	// ------------ LEFT MENU ----------------
	function saveLeftMenu() {
		var s = '';
		for(var i=0;i<ilmenu.length;i++) {
			s += (s!=''?',':'')+($(ilmenu[i]).parent().hasClass('opened')?'1':'0');
		}
		$.cookie('leftmenu', s, {expires:30});
	}
	
	if (ilmenu.length > 0) {
		$(wilmenu).css('visibility','hidden').find('ul').css('height','auto');
		setTimeout(function() {
			for(var i=0;i<ilmenu.length;i++) {
				$(ilmenu[i]).data('h' ,$(ilmenu[i]).parent().find('ul').height()).parent().find('li:last').addClass('last_item');
				$(ilmenu[i]).parent().find('a').attr('ulnum', i);
			}
			for(var i=0;i<almenu.length;i++) { $(almenu[i]).attr('linum', i); }
			almenu.click(function() {
				$.cookie('top_menu_ul', $(this).attr('ulnum'), {expires:30});
				$.cookie('top_menu_link', $(this).attr('linum'), {expires:30});
			});
			ilmenu.click(function() {
				var tmp = $(this).parent().find('> ul');
				if ($(this).parent().hasClass('opened')) {
					tmp.stop().animate({'height':'0', 'opacity':'0'}, 'slow','easeOutQuart', function() {
						$(this).parent().removeClass('opened');
						saveLeftMenu();
					});
				} else {
					tmp.stop().animate({'height':$(this).data('h')+'px', 'opacity':'1'}, 'slow','easeOutQuart', function() {
						$(this).parent().addClass('opened');
						saveLeftMenu();
					});
				}
			});
			$(wilmenu).fadeTo(1, 0, function() {
				$(this).css('visibility','visible').find('ul').removeAttr('style');
				var s = $.cookie('leftmenu')!=null ? ($.cookie('leftmenu')).split(',') : [];
				if (s.length == ilmenu.length) {
					for(var i=0;i<ilmenu.length;i++) {
						if (s[i] == '1') {
							$(ilmenu[i]).parent().addClass('opened');
						}
					}
				}				
				for(var i=0;i<ilmenu.length;i++) {
					if (!$(ilmenu[i]).parent().hasClass('opened')) {
						$(ilmenu[i]).parent().find('ul').fadeTo(1, 0);
					}
				}
				if (tmli >= 0) {
					$(almenu[tmli]).addClass('active');
				}
				$(this).fadeTo('slow', 1);
			});
		}, 500);
	}
	// ------------ TOP MENU -----------------
	gtmp = $(topmenu).html();
	$(topmenu).remove();
	$(body).append('<div id="topmenu" style="visibility:hidden;left:0;top:0;position:absolute;float:none;">'+gtmp+'</div>');
	topmenu = $('#topmenu');
	topmenu_span = topmenu.find('#topls span');
	for(var i=0;i<topmenu_span.length;i++) { $(topmenu_span[i]).data('num',i); }
	topmenu_ul = topmenu.find('#botls ul');
	for(var i=0;i<topmenu_ul.length;i++) { $(topmenu_ul[i]).data('num',i).find('a').attr('ulnum', i); }
	tml = topmenu.find('#botls a');
	for(var i=0;i<tml.length;i++) {
		$(tml[i]).data('lnum', i).attr('linknum', i);
	}
	topmenu_ul.fadeTo(1, 0, function() { $(this).hide(); });
	topmenu_span.mouseenter(function() {
		clearTimeout(topmenu_t);
		topmenu_ul.not(this).stop().css('z-index',220).fadeTo('slow', 0, function() { $(this).hide(); });
		$(topmenu_ul[$(this).data('num')]).stop().css('z-index',230).show().fadeTo('slow', 1);
		topmenu_span.removeClass('active');
		$(this).addClass('active');
	}).mouseleave(function() {
		//if (tmul == $(this).data('num')) { return false; } 
		clearTimeout(topmenu_t);
		topmenu_t = setTimeout(function() {
			$(topmenu_span[tmul]).mouseenter();
		}, 200);
	});
	topmenu_ul.mouseenter(function() {
		clearTimeout(topmenu_t);
	}).mouseleave(function() {
		topmenu_span.mouseleave();
	});
	tml.click(function() {
		$.cookie('top_menu_ul', $(this).attr('ulnum'), {expires:30});
		$.cookie('top_menu_link', $(this).data('lnum'), {expires:30});
		document.location.href = $(this).attr('href');
		return false;
	});
	$(topmenu_span).click(function() {
		var num = $(this).data('num')*1;
		if (num == 2) {
			$.cookie('top_menu_ul', 0, {expires:-1});
			$.cookie('top_menu_link', 0, {expires:-1});
			document.location.href = '/shop.html'; 
		} else {
			num = $(this).parent().parent().find('#botls ul').eq(num).find('a:first').attr('linknum')*1;
			if (!isNaN(num)) {
				$(tml[num]).click();
			}
		}
	});
	if (tmli >= 0) {
		$(tml[tmli]).addClass('active');
		$(topmenu_span[tmul]).mouseenter();
	}
	// ------------ LEFT MENU -----------------
	for (var i=0;i<menumap.length;i++) { $(menumap[i]).data('num',(i+1)); }
	menumap.mouseenter(function() {
		lmenu.addClass('gmenu_'+$(this).data('num'));
	}).mouseleave(function() {
		lmenu.removeClass('gmenu_'+$(this).data('num'));
	}).click(function() {
		var tmp = $(this).attr('anum')*1;
		switch (tmp) {
			case  0 :
				$.cookie('top_menu_ul', 0, {expires:30});
				$.cookie('top_menu_link', 1, {expires:30});
			break;
			case  3 :
				$.cookie('top_menu_ul', 3, {expires:30});
				$.cookie('top_menu_link', 12, {expires:30});
			break;
			case  5 :
				$.cookie('top_menu_ul', 0, {expires:30});
				$.cookie('top_menu_link', 2, {expires:30});
			break;
			default :
				$.cookie('top_menu_ul', 0, {expires:-1});
				$.cookie('top_menu_link', 0, {expires:-1});
			break;
		}
	});
	// ------------- Сопутствующие ------------
	function initSoputArrows() {
		if (soputmax < 1) {
			soputidle = false;
			soputarr.css('visibility','hidden');
		}
		if (soputpos == 0) {
			$(soputarr[0]).css('visibility','hidden');
			$(soputarr[1]).css('visibility','visible');
			soputidle = true;
		}
		if (soputpos == soputmax) {
			$(soputarr[1]).css('visibility','hidden');
			$(soputarr[0]).css('visibility','visible');
			soputidle = true;
		}
		if (soputpos > 0 && soputpos < soputmax) {
			soputarr.css('visibility','visible');
			soputidle = true;
		}
		soputarr.removeClass('psarra');
	}
	if (soput.length > 0) {
		soputarr = soput.find('.psarr');
		soputframes = soput.find('.pslide');
		soputwrap = soput.find('#pswrapper');
		soputpos = 0;
		soputtime = 300;
		soputmax = soputframes.length-4;
		soput.find('a').click(function() {return false;});
		initSoputArrows();
		$(soputarr[0]).click(function() {
			if (!soputidle) { return false; }
			soputidle = false;
			var old = soputpos, t;
			soputpos = soputpos-4;
			if (soputpos < 0) {
				soputpos = 0;
			}
			t = old-soputpos;
			soputarr.addClass('psarra');
			soputwrap.animate({'margin-left':'-'+(soputpos*150)+'px'}, t*soputtime, function() { initSoputArrows(); });
		});
		$(soputarr[1]).click(function() {
			if (!soputidle) { return false; }
			soputidle = false;
			var old = soputpos, t;
			soputpos = soputpos+4;
			if (soputpos > soputmax) {
				soputpos = soputmax;
			}
			t = soputpos-old;
			soputarr.addClass('psarra');
			soputwrap.animate({'margin-left':'-'+(soputpos*150)+'px'}, t*soputtime, function() { initSoputArrows(); });
		});
		soputframes.click(function() {
			if (!soputidle) { return false; }
			soputidle = false;
			var tmp = $(this).find('a');
			document.location.href = tmp.attr('href');
		});
	}
	// ------------- FUNCTIONS ----------------
	function initPositions() {
		var tmp = $(head).offset();
		if (topblogo.length > 0 && cururl != '' && cururl != '/' && cururl != 'index.php' && cururl != 'index.html') {
			topblogo.css('left',tmp.left+'px');
		}
		$(topmenu).css({'position':'absolute','left':(tmp.left+$(head).width()-$(topmenu).width())+'px','visibility':'visible'});
		tmp = innercont.offset();
		breads.css({'top':'120px', 'left':(tmp.left-10)+'px'});
	};
	// ------------- ACTIONS --------------------
	$(window).resize(function() {
		initPositions();
	}).unload(function() {
		/*
		$.cookie('top_menu_ul', tmul, {expires:30});
		$.cookie('top_menu_link', tmli, {expires:30});
		*/
	});
	// ------------- START ! ---------------------
	$('#askqu a').click(function() {
		$(this).parent().find('img').click();
		return false;
	});
	if (cururl != '' && cururl != '/' && cururl != 'index.php' && cururl != 'index.html') {
		switch (cururl) {
			case 'partner.html' :
				topblogo.addClass('logoin_rose');
			break;
			case 'business.html' :
			case 'presentation.html' :
			case 'join.html' :
				topblogo.addClass('logoin_cofee');
			break;
			default :
				if (ishop) {
					topblogo.addClass('logoin_photo');
				} else {
					topblogo.addClass('logoin_puzzle');
				}
			break;
		}
	}
	$(window).resize();	
	
	
	var clabels = $('#inner_right label').css('display','inline-block'), clabels_max = 0;
	if (clabels.length > 0) {
		for (var i=0;i<clabels.length;i++) {
			if ($(clabels[i]).width() > clabels_max) {
				clabels_max = $(clabels[i]).width();
			}
		}
		clabels.css('width',clabels_max+'px');
	}
	
}).keydown(function(event) {
	if (event.keyCode == '27') { event.preventDefault(); }
});})(jQuery)
