(function($) {
	$.fn.bgSlider = function(options) {
		var settings = {event: 'click', interval: 4000, speed: 600, resumeInterval: 10000};
		$.extend(settings,options);

		var title = $('#content-hp .title span.level-3').length ? $('#content-hp .title span.level-3') : $('#content-hp .title');
		var comment = $('#content-hp .comment span.level-3').length ? $('#content-hp .comment span.level-3') : $('#content-hp .comment');
		//var text = $('#content-hp .text');
		var ref = $('#content-hp .red-btn');
		var imgs = $('#bg-slider img');
		var refs = $('#bg-slider-nav a');
		var cur = -1;
		var timer = null;
		var resumeTimer = null;
		
		if ( $(window).width() < 1280 ) {
			$('#bg-slider, #bg-slider img').css('height','900px');
			$('#content-hp').css('height','667px');
		}

		function btnClick() {
			refs.bind(settings.event, function() {
				clearTimeout(timer);
				clearTimeout(resumeTimer)
				
				var index = $(this).index();
				var isBlack = $(this).attr('rel')=='black'?true:false;
				var isWhite = $(this).attr('rel')=='white'?true:false;
				var temp = false;

				if ( cur != index ) {
					if ( cur > index ) {
						temp = cur;
						imgs.filter(':lt('+(temp)+'):gt('+index+')').css('display','none'); 
						imgs.filter(':eq('+temp+')').fadeOut(settings.speed);
					} else {
						temp = cur;
						imgs.filter(':eq('+index+')').fadeIn(settings.speed,function(){
							imgs.filter(':gt('+temp+'):lt('+index+')').css('display','inline');
						});
					}
			
					cur = index;
					refs.removeClass();
					$(this).addClass('cur');
					title.html($(refs.get(cur)).attr('title'));
					comment.html($(refs.get(cur)).attr('rev'));
					ref.attr('href',$(refs.get(cur)).attr('href'));
					//if (text.length) text.html($(refs.get(cur)).attr('coords'));
					
					if (isBlack) {//, #content-hp .text
						$('#header h1 a, #content-hp .title, #content-hp .comment').addClass('black');
						$('#header ul.first-level').addClass('alt');
						$('#most img').attr('src','/i/logo-most.png');
					} else {//, #content-hp .text
						$('#header h1 a, #content-hp .title, #content-hp .comment').removeClass('black');
						$('#header ul.first-level').removeClass('alt');
						$('#most img').attr('src','/i/logo-most-black.png');
						
						if (isWhite) {
							$('#header ul.first-level').addClass('alt');
						} else {
							$('#header ul.first-level').removeClass('alt');
						}
					}
				}
				
				resumeTimer = setTimeout(function() { slideShow(); }, settings.resumeInterval);

				return false;
			});
		}
		
		function slideShow() {
			cur++;
			refs.removeClass();

			var isBlack = $(refs.get(cur)).attr('rel')=='black'?true:false;
			var isWhite = $(refs.get(cur)).attr('rel')=='white'?true:false;

			if ( cur >= refs.length ) {
				cur = 0;
				imgs.filter(':gt(0):not(:last)').css('display','none');
				imgs.filter(':last').fadeOut(settings.speed);
			} else {
				$(imgs.get(cur)).fadeIn(settings.speed);
			}

			$(refs.get(cur)).addClass('cur');
			title.html($(refs.get(cur)).attr('title'));
			comment.html($(refs.get(cur)).attr('rev'));
			ref.attr('href',$(refs.get(cur)).attr('href'));
			//if (text.length) text.html($(refs.get(cur)).attr('coords'));
			
			if (isBlack) {//, #content-hp .text
				$('#header h1 a, #content-hp .title, #content-hp .comment').addClass('black');
				$('#header ul.first-level').addClass('alt');
				$('#most img').attr('src','/i/logo-most.png');
			} else {//, #content-hp .text
				$('#header h1 a, #content-hp .title, #content-hp .comment').removeClass('black');
				$('#header ul.first-level').removeClass('alt');
				$('#most img').attr('src','/i/logo-most-black.png');
				
				if (isWhite) {
					$('#header ul.first-level').addClass('alt');
				} else {
					$('#header ul.first-level').removeClass('alt');
				}
			}
			
			if (imgs.length < 2) return false;

			timer = setTimeout(function() { slideShow(); }, settings.interval);
		}

		slideShow();
		btnClick();
	};
	
	$.fn.catalogSlider = function(options) {
		var settings = {speed: 600};
		$.extend(settings,options);

		var target = $(this);
		var lis = $(this).children('li');
		var refs = target.find('li a.title');
		var imgs = $('#catalog-cats-ill img');
		
		if ( $(window).width() < 1280 ) {
			imgs.each(function(){
				$(this).css('height',Math.ceil(parseInt($(this).css('height'))*0.85)+'px');
			});
			$('#catalog-cats-ill').css('left','1310px');
			$('#content').css('min-height','630px');
			$('#footer').css('marginTop','20px');
		}

		imgs.eq(0).fadeIn(settings.speed);

		refs.bind('click', function() {
			var closedDiv = $(this).next();

			if ( closedDiv.is(':visible') ) {
				location.href = $(this).attr('href');
				return false;
			}
			
			var li = $(this).parent();
			var index = li.index();
			var img = imgs.eq(index);
			var openedDiv = lis.children('div:visible');

			img.siblings(':visible').fadeOut(settings.speed);
			img.fadeIn(settings.speed);

			openedDiv.slideUp(settings.speed);
			closedDiv.slideToggle(settings.speed);

			li.addClass('cur');
			openedDiv.parent().removeClass('cur');

			return false;
		});
	};
	
	$.fn.vacancySlider = function(options) {
		var settings = {speed: 600};
		$.extend(settings,options);

		var target = $(this);
		var lis = $(this).children('li');
		var refs = target.find('li a.title');
		
		refs.bind('click', function() {
			var closedDiv = $(this).next();
		
			if ( closedDiv.is(':visible') ) return false;
			
			var li = $(this).parent();
			var openedDiv = lis.children('div:visible');

			openedDiv.slideUp(settings.speed);
			closedDiv.slideToggle(settings.speed);

			li.addClass('cur').animate({paddingTop:'24px',paddingBottom:'29px'},settings.speed);
			openedDiv.parent().removeClass('cur').animate({paddingTop:'0',paddingBottom:'0'},settings.speed);

			return false;
		});
	};
	
	$.fn.marketsSlider = function(options) {
		var settings = {speed: 600};
		$.extend(settings,options);

		var target = $(this);
		var lis = $(this).children('li');
		var refs = target.find('li a.city');
		
		var infoRefs = target.find('li table a');
		var info = $('div.popup-market-info');
		var infoCloseBtn = info.find('a.close');
		var screenOverlay = $('#screen-overlay');
		
		refs.bind('click', function() {
			var closedDiv = $(this).next();
		
			if ( closedDiv.is(':visible') ) return false;
			
			var li = $(this).parent();
			var openedDiv = lis.children('div:visible');

			openedDiv.slideUp(settings.speed);
			closedDiv.slideToggle(settings.speed);

			$(this).animate({top:'14px'},settings.speed);
			openedDiv.prev().animate({top:'0'},settings.speed);
			
			li.addClass('cur').animate({paddingTop:'26px',paddingBottom:'35px'},settings.speed);
			openedDiv.parent().removeClass('cur').animate({paddingTop:'0',paddingBottom:'0'},settings.speed);

			return false;
		});
		
		infoRefs.bind('click', function() {
			infoShow($(this));
			return false;
		});
		
		infoCloseBtn.live('click', function() {
			infoHide();
			return false;
		});
		
		info.find('div.breadcrumbs a.cur').live('click', function() {
			infoHide();
			return false;
		});

		function infoShow(oThis) {
			info.html('&nbsp;').css('top',$(window).scrollTop()+Math.floor($(window).height()*0.09)).fadeIn(settings.speed);
			screenOverlay.fadeIn(settings.speed/2).bind('click', function() {
				infoHide();
				return false;
			});
			
			var path = $('.markets.at').length ? '/marketsat/get-market-info/market_id/' : '/markets/get-market-info/market_id/';

			$.ajax({
				url: path + oThis.attr('rel'),
				type: 'POST',
				dataType: 'json',
				error: function(XHR) {
					alert('Ошибка');
					return false;
				},
				success: function(data, status) {
					if ( data.code == '200' ) {
						info.html(data.html);
						var int = setTimeout(function() { initMap(); }, 1200);

						return true;
					}
				}
			});
			
			return false;
		}
		
		function infoHide() {
			info.fadeOut(settings.speed);
			screenOverlay.fadeOut(settings.speed/2).unbind('click');
			return false;
		}
		
		var illsNav = $('.popup-market-info .common-nav a');

		illsNav.live('click', function() { 
			if ($(this).parent().hasClass('cur')) return false;

			var ills = $('.popup-market-info div.ills').children();			
			var newPic = ills.eq($(this).attr('rel'));
			var oldPic = ills.not(newPic);
			
			var lis = $('.popup-market-info #.common-nav li');
			
			newPic.fadeIn();
			oldPic.fadeOut();
			
			lis.removeClass('cur');
			$(this).parent().addClass('cur')

			return false;
		});
	};
	
	$.fn.catalogListGallery = function(options) {
		var settings = {speed: 600};
		$.extend(settings,options);

		target = $(this);

		target.find('div.colors a.inactive').bind('click', function() {
			return false;
		});
		
		var refs = target.find('div.colors a:not(.inactive)');
		var items = $('#catalog-list li');
		var colors = ills = galleries = count = index = trueIndex = false;
		
		items.each(function(index){
			var colors = $(this).find('div.colors a:not(.inactive)');
			var ills = $(this).find('div.ill div');
			var galleries = $(this).find('div.gallery div');
			var count = colors.length;
			var index = Math.floor(Math.random() * (count));
			var trueIndex = colors.eq(index).index();
			var colorName = colors.eq(index).attr('alt');
			var colorNameCell = $(this).find('table td:eq(1)');

			$(colors).eq(index).addClass('cur');
			$(ills).eq(trueIndex).fadeIn(settings.speed);
			$(galleries).eq(trueIndex).fadeIn(settings.speed);
			colorNameCell.text(colorName);
		});

		refs.bind('click', function() {
			var index = $(this).index();
			var item = $(this).parent().parent().parent();
			var refs = $(this).siblings();
			var ills = item.find('div.ill div');
			var galleries = item.find('div.gallery div');
			var colorName = $(this).attr('alt');
			var colorNameCell = $(this).parent().parent().find('table td:eq(1)');
			
			refs.removeClass('cur');
			$(this).addClass('cur');
			item.siblings().removeClass('cur');
			item.addClass('cur');
			colorNameCell.text(colorName);

			if (ills.length) {
				ills.not(ills.get(index)).fadeOut(settings.speed);
				$(ills.get(index)).fadeIn(settings.speed);
			}
			
			if (galleries.length) {
				galleries.not(galleries.get(index)).fadeOut(settings.speed);
				$(galleries.get(index)).fadeIn(settings.speed);
			}

			return false;
		});
	};
	
	$.fn.catalogGallery = function(options) {
		var settings = {speed: 600};
		$.extend(settings,options);

		target = $(this);
		var colorNameCell = target.find('div.item-info table td:eq(1)');			
		var refs = target.find('div.item_nav div a');
		var colors = target.find('div.colors a:not(.inactive)');
		var curColor = target.find('div.colors a:not(.inactive):first');
		var index = curColor.index();
		var ill = $('#item-pic div.gallery:eq('+index+')');
		var illCurPic = $('#item-pic div.gallery:eq('+index+') div:first');
		var galleries = target.find('div.item_nav div:eq('+index+')');
		var galCurPic = galleries.children('a:first');

		target.find('div.colors a.inactive').bind('click', function() {
			return false;
		});

		$(curColor).addClass('cur');
		$(galCurPic).addClass('cur');
		$(galleries).fadeIn(settings.speed);
		$(ill).fadeIn(settings.speed);
		$(illCurPic).fadeIn(settings.speed);

		refs.bind('click', function() {
			var index = $(this).index();
			var colorIndex = target.find('div.colors a.cur').index();
			var item_pics = $('#item-pic div.gallery:eq('+colorIndex+') div');
			
			refs.removeClass('cur');
			$(this).addClass('cur');

			item_pics.not(item_pics.get(index)).fadeOut(settings.speed);
			$(item_pics.get(index)).fadeIn(settings.speed);

			return false;
		});
		
		colors.bind('click', function() {
			if ( $(this).hasClass('cur') ) return false;
		
			var index = $(this).index();

			var colorName = $(this).attr('alt');
			var refs = $(this).siblings();
			var ills = $('#item-pic div.gallery');
			var galleries = target.find('div.item_nav div');

			refs.removeClass('cur');
			$(this).addClass('cur');

			ills.fadeOut(settings.speed);
			ills.find('div').fadeOut(settings.speed);
			ills.not(ills.get(index)).fadeOut(settings.speed);
			$(ills.get(index)).fadeIn(settings.speed);
			$(ills.get(index)).find('div:first').fadeIn(settings.speed);
			
			galleries.not(galleries.get(index)).fadeOut(settings.speed);
			$(galleries.get(index)).fadeIn(settings.speed, function(){ galleries.find('a').not($(galleries.get(index)).find('a:first')).removeClass('cur'); });
			$(galleries.get(index)).find('a:first').addClass('cur');
			
			colorNameCell.text(colorName);

			return false;
		});
	};
	
	$.fn.catalogSliderAT = function(options) { 
		var settings = {speed: 600};
		$.extend(settings,options);

		var target = $(this),
			lis = $(this).children('li'),
			refs = target.find('li h2 a'),
			imgs = $('#catalog-cats-at img');
		
		imgs.eq(0).fadeIn(settings.speed);

		refs.bind('click', function(e) { 
			e.preventDefault();
		
			var closedDiv = $(this).parent().next();

			if ( closedDiv.is(':visible') ) {
				location.href = $(this).attr('href');
				return false;
			}
			
			var li = $(this).parent().parent(),
				index = li.index(),
				img = imgs.eq(index),
				openedDiv = lis.children('div:visible');

			imgs.not(img).fadeOut(settings.speed);
			img.fadeIn(settings.speed, function(){
				li.addClass('cur');
				openedDiv.parent().removeClass('cur');
			});

			openedDiv.slideUp(settings.speed);
			closedDiv.slideToggle(settings.speed);


			return false;
		});
	};
	
	$.fn.catalogListGalleryAT = function(options) {
		var settings = {speed: 600};
		$.extend(settings,options);

		var target = $(this),
			refs = target.find('div.colors a');

		target.find('div.pics a:first-child').fadeIn(settings.speed);
		target.find('div.colors a:first-child').addClass('cur');

		refs.bind('click', function() {
			var index = $(this).index(),
				curRefs = $(this).siblings(),
				curPics = $(this).parent().parent().parent().prev().children('a'),
				pic = $(this).parent().parent().parent().prev().children('a').eq(index);
			
			
			curRefs.removeClass('cur');
			$(this).addClass('cur');

			if (curPics.length) {
				curPics.not(pic).fadeOut(settings.speed);
				pic.fadeIn(settings.speed);
			}

			return false;
		});
	};
	
	$.fn.catalogListGalleryAT2 = function(options) {
		var settings = {speed: 600};
		$.extend(settings,options);

		var target = $(this);

		target.find('div.pics a:first-child').fadeIn(settings.speed);
	};
	
	$.fn.catalogGalleryAT = function(options) {
		var settings = {speed: 600};
		$.extend(settings,options);

		target = $(this);
		var colorNameCell = target.find('div.model-info table td:eq(1)');			
		var refs = target.find('div.model-nav div a');
		var colors = target.find('div.colors a');
		var curColor = target.find('div.colors a:first');
		var index = curColor.index();
		var ill = target.find('div.model-pic > div:eq('+index+')');
		var illCurPic = $('div.model-pic > div:eq('+index+') div:first');
		var galleries = target.find('div.model-nav div:eq('+index+')');
		var galCurPic = galleries.children('a:first');

		$(curColor).addClass('cur');
		$(galCurPic).addClass('cur');
		$(galleries).fadeIn(settings.speed);
		$(ill).fadeIn(settings.speed);
		$(illCurPic).fadeIn(settings.speed);

		refs.bind('click', function() {
			var index = $(this).index();
			var colorIndex = target.find('div.colors a.cur').index();
			var item_pics = target.find('div.model-pic > div:eq('+colorIndex+') div');
			
			refs.removeClass('cur');
			$(this).addClass('cur');

			item_pics.not(item_pics.get(index)).fadeOut(settings.speed);
			$(item_pics.get(index)).fadeIn(settings.speed);

			return false;
		});
		
		colors.bind('click', function() {
			if ( $(this).hasClass('cur') ) return false;
		
			var index = $(this).index();

			var colorName = $(this).attr('title');
			var refs = $(this).siblings();
			var ills = target.find('div.model-pic > div');
			var galleries = target.find('div.model-nav div');

			refs.removeClass('cur');
			$(this).addClass('cur');

			ills.fadeOut(settings.speed);
			ills.find('div').fadeOut(settings.speed);
			ills.not(ills.get(index)).fadeOut(settings.speed);
			$(ills.get(index)).fadeIn(settings.speed);
			$(ills.get(index)).find('div:first').fadeIn(settings.speed);
			
			galleries.not(galleries.get(index)).fadeOut(settings.speed);
			$(galleries.get(index)).fadeIn(settings.speed, function(){ galleries.find('a').not($(galleries.get(index)).find('a:first')).removeClass('cur'); });
			$(galleries.get(index)).find('a:first').addClass('cur');
			
			colorNameCell.text(colorName);

			return false;
		});
	};

})(jQuery);

function initVideoPlayers() {
	var oThis = $(this);
	var infoRefs = $('#video-list li a');
	var info = $('#video-popup');
	var infoCloseBtn = info.find('a.close');
	var screenOverlay = $('#screen-overlay');
	
	infoRefs.bind('click', function() {
		infoShow($(this));
		return false;
	});
	
	infoCloseBtn.live('click', function() {
		infoHide();
		return false;
	});

	function infoShow(oThis) {
		info.html('&nbsp;').fadeIn(500);
		screenOverlay.fadeIn(500).bind('click', function() {
			infoHide();
			return false;
		});

		$.ajax({
			url: '/video/get-video-src/video_id/'+oThis.attr('rel'),
			type: 'POST',
			dataType: 'json',
			error: function(XHR) {
				alert('Ошибка');
				return false;
			},
			success: function(data, status) {
				if ( data.code == '200' ) {
					info.html(data.html);
					return true;
				}
			}
		});
		
		return false;
	}
	
	function infoHide() {
		info.fadeOut(500);
		screenOverlay.fadeOut(500).unbind('click');
		return false;
	}

	return false;
}

function headerMenuInit() {
	/*********************
	//* jQuery Drop Line Menu- By Dynamic Drive: http://www.dynamicdrive.com/
	//* Last updated: June 27th, 09'
	*********************/
	
	var droplinemenu={
		animateduration: {over: 250, out: 150}, //duration of slide in/ out animation, in milliseconds

		buildmenu: function(menuid) {
			jQuery(document).ready(function($) {
				var $mainmenu = $('#'+menuid+'>ul');
				var $headers = $mainmenu.find('ul').parent();
				var animateFlag = true;
				
				$headers.each(function(i){
					var $curobj = $(this);

					$curobj.hover(
						function(e){
							if ( !animateFlag ) 
								return false;
							
							animateFlag = false;
						
							var $targetul = $(this).children('ul:eq(0)');
							
							$targetul.slideDown(droplinemenu.animateduration.over, function(){
								animateFlag = true;
							});
							
							$mainmenu.addClass('cur');
						},
						function(e){
							var $targetul = $(this).children('ul:eq(0)');
							$targetul.slideUp(droplinemenu.animateduration.out);
							$mainmenu.removeClass('cur');
						}
					) //end hover
				}) //end $headers.each()
				$mainmenu.find('ul').css({display:'none', visibility:'visible'});
			}) //end document.ready
		}
	}
	
	droplinemenu.buildmenu('header');
	
	if ( $(window).width() < 1200 ) $('#header > ul').addClass('small');
}

function historySlider() {
	if ( $(window).width() < 1130 ) $('.paging').addClass('small');

	var margin = 220;

	//Get size of the image, how many images there are, then determin the size of the image reel.
	var imageWidth = $('div.window').width() + margin;
	var imageSum = $('div.image_reel>div').size();
	var imageReelWidth = imageWidth * imageSum;

	//Adjust the image reel to its new size
	$('div.image_reel>div').css({'width' : imageWidth-margin});
	$('div.image_reel').css({'width' : imageReelWidth});
	
	rotate = function(){
		var triggerID = $active.attr('rel') - 1; //Get number of times to slide
		//alert(triggerID);
		var image_reelPosition = triggerID * imageWidth; //Determines the distance the image reel needs to slide

		$('.paging a').removeClass('active'); //Remove all active class
		$active.addClass('active'); //Add active class (the $active is declared in the rotateSwitch function)

		//Slider Animation
		$('.image_reel').animate({
			left: -image_reelPosition
		}, 1650 );

	}; 
	
	$('div.paging a, div.wrap h2 a').click(function() {
		if ($(this).hasClass('next-page')||$(this).hasClass('prev-page')) 
			if ($(this).hasClass('next-page'))
				$active = $('div.paging a.active').attr('rel')!='1' ? $('div.paging a.active').next().next() : $('div.paging a:first');
			else
				$active = $('div.paging a.active').attr('rel')!='2' ? $('div.paging a.active').prev().prev() : $('div.paging a:last');
		else
			$active = $(this); //Activate the clicked paging

		rotate(); //Trigger rotation immediately
		return false; //Prevent browser jump to link anchor
	});
}

var map;
function initMap() {
    try { 
		if ($('#google-map').size() > 0) {
			google.load('maps', '2', {'callback': function() {
				map = GMLoad();
			}});
		} else {
			map = GMLoad();
		}
    } catch (err) {};
}

var cl_timer = 0;
function clearElements() {
    try {
		$($('#google-map > div')[3]).addClass('hidden').hide();
		$($('#google-map > div')[4]).addClass('hidden').hide();
    } catch(err) {
		cl_timer = setTimeout(clearElements, 1500);
    }
}

function GMLoad() {
    if ($('#google-map').size() > 0 && GBrowserIsCompatible()) {
		var map = new google.maps.Map2($('#google-map')[0]);	
		var mapControl = new google.maps.MapTypeControl();
		map.addControl(mapControl);
		map.addControl(new google.maps.LargeMapControl());
		geocoder = new google.maps.ClientGeocoder();
		clearElements();
		
		if ($('#markets-city').size() > 0) {
			var mapX = 55.7633403;
			var mapY = 37.647658;
		
			if ($('#g-coordinates').val() != '') {
				var oPoint = $('#g-coordinates').val().split(';');
				mapX = oPoint[1];
				mapY = oPoint[0];
			}
			
			map.setCenter(new google.maps.LatLng(mapX,mapY), 15);
			var marker = new GMarker(new google.maps.LatLng(mapX,mapY), {draggable: true});
			
			GEvent.addListener(marker, 'dragstart', function() {
				map.closeInfoWindow();
			});
			GEvent.addListener(marker, 'dragend', function() {
				$('#g-coordinates').val(marker.getPoint().x + ';' + marker.getPoint().y);
			});
			map.addOverlay(marker);
			
			/*$('#city_id').bind('blur', function() { 
				var address = $('#city_id').val() + ' ' + $('#markets-address').val();
				map.clearOverlays();
				showAddress(address);
			});*/
			
			$('#markets-address').bind('change', function() {
				var address = $('#city_id').val() + ' ' + $('#markets-address').val();
				map.clearOverlays();//alert(address);
				showAddress(address);
			});
		} else {
			var oPoint = $('#g-coordinates').val().split(';');
			map.setCenter(new google.maps.LatLng(oPoint[1],oPoint[0]), 15);
			
			var map_type_ctrl=new GMapTypeControl();
			//var map_scale_ctrl=new GScaleControl();	
			map.addControl(map_type_ctrl);
			//map.addControl(map_scale_ctrl);
			
			var SamsoniteIcon = new GIcon();
			SamsoniteIcon.image = '/i/icon-google-map.png';
			//SamsoniteIcon.shadow = 'http://labs.google.com/ridefinder/images/mm_20_shadow.png';
			SamsoniteIcon.iconSize = new GSize(47, 43);
			//SamsoniteIcon.shadowSize = new GSize(22, 20);
			SamsoniteIcon.iconAnchor = new GPoint(13, 42);
			SamsoniteIcon.infoWindowAnchor = new GPoint(11, 1);
			
			var marker = new GMarker(new google.maps.LatLng(oPoint[1], oPoint[0]), {icon:SamsoniteIcon});
			map.addOverlay(marker);

			GEvent.addListener(marker, 'click', function() {
				marker.openInfoWindowHtml($('#marker-desc').html());
			});
		}
		return map;
    } else {
		return null;
    }
    
}

function showAddress(address) {
    if (geocoder) {
        geocoder.getLatLng(
			address,
			function(point) {
				if (!point) {
					$('#google-message').removeClass().addClass('error').html(address + '. Не найдено.').show();
					var address = $('#report-country_id').find('option:selected').html();
					map.clearOverlays();
					showAddress(address);
				} else {
					map.setCenter(point, 15);
					var marker = new GMarker(point, {draggable: true});
					
					GEvent.addListener(marker, 'dragstart', function() {
						map.closeInfoWindow();
					});
					
					GEvent.addListener(marker, 'dragend', function() {
						$('#g-coordinates').val(marker.getPoint().x + ';' + marker.getPoint().y);
					});
					
					map.addOverlay(marker);
					//marker.openInfoWindowHtml(address);
					
					$('#g-coordinates').val(marker.getPoint().x + ';' + marker.getPoint().y);
					$('#google-message').hide();
				}
			}
        );
    }
}

function createMarker(point) {
    var marker = new GMarker(point);
    return marker;
};

function initContacts () {
	//initMap();
	
	illsNav = $('ul.common-nav a');
	
	illsNav.live('click', function() { 
		if ($(this).parent().hasClass('cur')) return false;

		var ills = $('#contacts-ills').children();			
		var newPic = ills.eq($(this).attr('rel'));
		var oldPic = ills.not(newPic);
		
		var lis = $('ul.common-nav li');
		
		newPic.fadeIn();
		oldPic.fadeOut();
		
		lis.removeClass('cur');
		$(this).parent().addClass('cur')

		return false;
	});
}

function initFranciseForm() {
	$('#part-contacts.questionnaire p.btn-submit a').bind('click',function(){
		var form = $(this).parent().parent();
	
		$.ajax({
			url: '/contacts/sendrequest',
			data: form.serializeArray(),
			type: 'POST',
			dataType: 'json',
			error: function(XHR) {
				alert('Извините, произошла ошибка. Попробуйте повторить попытку позднее или связаться с нами по телефону.');
				return false;
			},
			success: function(data, status) {
				if ( data.code == '200' ) {
					form.find('input:not(:submit), textarea').val('');
					location.href = '/franchise/questionnaire/request/success';
					return true;
				}
			}
		});
		
		return false;
	});
}


$(document).ready(function(){
	$('#screen-overlay').css('opacity','.8');
	if ($('#header ul.first-level').length) headerMenuInit();
	if ($('#bg-slider').length) $('#bg-slider').bgSlider({ event: 'click', interval: 6000, speed: 900, resumeInterval: 10000 });
	$('#wrapper #catalog-cats').catalogSlider({ speed: 900 });
	$('#catalog-list li').catalogListGallery();
	$('#item').catalogGallery();
	$('#vacancy-list').vacancySlider({ speed: 900 });
	$('#markets-list').marketsSlider({ speed: 900 });
	if ($('#video-list').length) initVideoPlayers();
	if ($('div.main_view').length) historySlider();
	if ($('#contacts-ills').length) initContacts();
	if ($('#google-map').length) initMap();
	
	if ($('#news-list').length) {
		$('#news-list li').each(function(){
			var img = $(this).find('div img');
			$(this).find('div a.red-btn').css('left',img.width()+25+'px');
		});
	}
	
	if ($('#part-contacts.questionnaire').length) initFranciseForm();

	if ( $('.common-nav.franchise').length && $(window).width() < 1200 ) $('.common-nav.franchise').addClass('small');
	
	if ($('#catalog-cats-at').length) $('#catalog-cats-at').catalogSliderAT();
	if ($('#collectionsat.model').length) $('#collectionsat').catalogListGalleryAT();
	if ($('#collectionsat:not(.model)').length) $('#collectionsat').catalogListGalleryAT2();
	if ($('#model').length) $('#model').catalogGalleryAT();
});
