$(document).ready(function() {
	html.links();
	html.search();
	html.print();
	html.form();
	html.highlight();
	promo.init();
	//gmap.init();
});

function slashTrim(string) {
	if (string.lastIndexOf("/") + 1 == string.length) {
		string = string.substring(0, string.length-1);
	}
	return string;
}

gmap = {
	init: function() {
		$("#map-contacts").each(function() {
			map = new google.maps.Map2(this);
			map.setCenter(new google.maps.LatLng(55.7440, 37.5616), 13);
			map.addControl(new GSmallMapControl());
			
			var healthIcon = new GIcon();
			healthIcon.image = "/images/marker.bmp";
			healthIcon.iconSize = new GSize(16, 16);
			healthIcon.iconAnchor = new GPoint(8, 8);
			healthIcon.infoWindowAnchor = new GPoint(12, 4);
			
			var point = new GLatLng(55.7440, 37.5616);
			var marker = new GMarker(point, { title: "Ашманов и партнеры" });
			map.addOverlay(marker);
		});
	}
}

promo = {
	banner: null,
	position: null,
	link: null,
	prev: null,
	next: null,
	
	init: function() {
		promo.banner = new Array();
		promo.position = [0, 0, 0];
		
		promo.banner[0] = $("div.banner-small.orange .banner-wrapper");
		promo.banner[1] = $("div.banner-small.cyan .banner-wrapper");
		promo.banner[2] = $("div.banner-small.green .banner-wrapper");

		promo.prev = $("div.banner-small a.banner-prev");
		promo.next = $("div.banner-small a.banner-next");
		
		promo.prev.each(function(i, item) {
			$(this).click(function() { promo.rotate(i, -1) });
		});
		promo.next.each(function(i, item) {
			$(this).click(function() { promo.rotate(i, 1) });
		});
	},
	
	rotate: function(i, direction) {
		current = promo.position[i];
		promo.banner[i].eq(current).hide();
		promo.position[i] = promo.getPosition(i, direction);
		promo.banner[i].eq(promo.position[i]).show();
	},
	
	getPosition: function(i, direction) {
		var position;
		if (promo.position[i] + direction > promo.banner[i].length - 1) {
			position = 0;
		}
		else if (promo.position[i] + direction < 0) {
			position = promo.banner[i].length - 1;
		}
		else {
			position = promo.position[i] + direction;
		}
		return position;
	}
}

html = {
	form: function() {
		
		var urls = {
			optimization: ["/inet/serv/promoreg", "/marketing/seo/why", "/marketing/seo/what", "/marketing/seo/how", "/marketing/audit/seoaudit"],
			context: ["/inet/serv/recl", "/marketing/context/why", "/marketing/context/what", "/marketing/context/how"],
			marketing: ["/marketing", "/marketing/media", "/marketing/smm", "/marketing/virus", "/marketing/complex"],
			pr: ["/marketing/pr"],
			consulting: ["/consulting", "/marketing/audit/usability", "/marketing/strategy", "/consulting/research"],
			technologies: ["/technologies", "/tech", "/tech/semantic", "/tech/semantic/demo", "/tech/search", "/tech/geo", "/tech/hypersearch"]
		};
		
		var sidebar = false;
		if ($("div.right #webform-client-form-28").length) {
			sidebar = true;
		}
		
		if ($("#webform-client-form-28").length) {
			$form = $("#webform-client-form-28");
			$submit = $("#webform-client-form-28 input[type=submit]");
			$name = $("#edit-submitted-imya");
			$site = $("#edit-submitted-site");
			$section = $("#webform-client-form-28 input[type=checkbox]");
			//$contacts = $("#edit-submitted-telefon-email");
			
			$phone = $("#edit-submitted-telefon");
			$email = $("#edit-submitted-email");
			
			$("#edit-submitted-telefon-wrapper label").append("<span>**</span>");
			$("#edit-submitted-email-wrapper label").append("<span>**</span>");
			
			if (sidebar) {
				
				$checkboxWrapper = $("#webform-client-form-28 div.form-checkboxes");
				$checkboxDivs = $("#webform-client-form-28 div.form-checkboxes div.form-item");
				
				$nameDiv = $("#webform-component-imya");
				$siteDiv = $("#webform-component-site");
				$commentsDiv = $("#webform-component-kommentarii_spisok_klyuchevyh_slov_opisanie_vashego_biznesa");
				
				$checkboxWrapper.after('<div id="form-more-checkboxes" class="form-more-link"><a href="javascript://">Другие услуги</a></div>');
				//$commentsDiv.after('<div id="form-more-textfields" class="form-more-link"><a href="javascript://">Дополнительная<br />информация</a></div>');
				
				$("#form-more-checkboxes a").live("click", function() {
					$checkboxDivs.show();
					$(this).parent().hide();
				});
	
				$("#form-more-textfields a").live("click", function() {
					$siteDiv.show();
					$commentsDiv.show();
					$(this).parent().hide();
				});
	
				//$siteDiv.hide();
				//$commentsDiv.hide();
				
				$checkboxDivs.hide();
	
				if (slashTrim(window.location.pathname) in oc(urls.optimization)) {
					$checkboxDivs.eq(0).show();
					$section.eq(0).attr({ checked: "checked" });
				}
				
				if (slashTrim(window.location.pathname) in oc(urls.context)) {
					$checkboxDivs.eq(1).show();
					$section.eq(1).attr({ checked: "checked" });
				}
				
				if (slashTrim(window.location.pathname) in oc(urls.marketing)) {
					$checkboxDivs.eq(2).show();
					$section.eq(2).attr({ checked: "checked" });
				}
				
				if (slashTrim(window.location.pathname) in oc(urls.pr)) {
					$checkboxDivs.eq(3).show();
					$section.eq(3).attr({ checked: "checked" });
				}
				
				if (slashTrim(window.location.pathname) in oc(urls.consulting)) {
					$checkboxDivs.eq(4).show();
					$section.eq(4).attr({ checked: "checked" });
				}
				
				if (slashTrim(window.location.pathname) in oc(urls.technologies)) {
					$checkboxDivs.eq(5).show();
					$section.eq(5).attr({ checked: "checked" });
				}
			}
			else {
				$section.eq(0).attr({ checked: "checked" });
			}
			
			$form.submit(function() {
				if ($name.val().length == 0 || ($phone.val().length == 0 && $email.val().length == 0)) {
					if ($(".form-message").length == 0) {
						$form.after('<div class="form-message red">Заполните все обязательные поля</div>');
					}
					else {
						$(".form-message").text('Заполните все обязательные поля');
						$(".form-message").addClass("red");
					}
					return false;
				}
				//return false;
			});
		}
	},
	links: function() {
		$("a[rel=external]").attr({target:"_blank"}).addClass("external");
	},
	search: function() {
		$("form.search-form input[type=submit]").attr({ value: "" });
		
		$searchInput = $("div.search div.text input");
		$searchValue = $searchInput.val();
		
		$searchInput.click(function() {
			if ($searchInput.val()==$searchValue)
				$searchInput.val("");
		}).blur(function() {
			if ($searchInput.val()=="") {
				$searchInput.val($searchValue);
			}
			$searchValue = $searchInput.val();
		});
	},
	print: function() {
		$("div.print a").click(function() {
			window.print();
		});
	},
	highlight: function() {
		$menuLi = $("div.menu li");
		$("div.banner-small").each(function(i, item) { 
			$(this).mouseover(function() { $menuLi.eq(i).addClass("selected"); });
			$(this).mouseout(function() { $menuLi.eq(i).removeClass("selected"); });
		});
	}
}

function oc(arguments)
{
  var o = {};
  for(var i=0;i<arguments.length;i++)
  {
    o[arguments[i]]=null;
  }
  return o;
}
