var fadeTime = 6000;
var arrBackgrounds = new Array("/templates/images/bg_1.jpg","/templates/images/bg_2.jpg","/templates/images/bg_3.jpg","/templates/images/bg_4.jpg");
var background_i =0;


function randOrd(){
	return (Math.round(Math.random())-0.5); 
} 
arrBackgrounds.sort( randOrd );


objImage = new Image();
for(i = 0; i < arrBackgrounds.length; i++) 
	objImage.src = arrBackgrounds[i];    

$(document).ready(function() {

	// Меню
	
	$("img[im1!=]").each(function() {
		heavyImage = new Image(); 
		heavyImage.src = $(this).attr("im2");
	});
	$("img[im1!=]").hover(function() { 
		heavyImage = new Image(); 
		heavyImage.src = $(this).attr("im2");	
		$(this).attr("src", $(this).attr("im2")) 
	}, function() { 
		heavyImage = new Image(); 
		heavyImage.src = $(this).attr("im1");	
		$(this).attr("src", $(this).attr("im1")) 
	});


	// Фоны
	for(i=0; i < arrBackgrounds.length; i++)
		$("body").prepend("<div class='background'></div>").find(".background").eq(0).css("background", "url(" + arrBackgrounds[i] + ") no-repeat top left");
	
	$("body").find(".background").eq(0).show();
	
	setInterval(changeBackground, fadeTime * 2);
	
	// Контент
	setOverflowContent();
	$(window).resize(function() {
		setOverflowContent();
	});
	
	// Валидация отправки отзыва
	$("form[class=message]").submit(function(){		
		if($(this).find("input[name=name]").val() == "")
		{
			alert("Необходимо указать Ваше имя");
			return false;
		}
		else if($(this).find("*[name=message]").val() == "")
		{
			alert("Необходимо написать отзыв");	
			return false;			
		}
		else
		{
			return true;
		}
	});	
	
	$("#send").css("cursor", "pointer").click(function() {
		$(this).next().click();
		return;
	});	
	
	$(".g_image").click(function() {
		$(this).blur();
		$(".g_image").find("img").css("border", "2px solid #222");
		$(this).find("img").css("border", "2px solid #dbb165");
		$(".big_image").html("<img src=\"" + $(this).attr("href") + "\" />");
		return false;
	});
	
	$("a[rel=video]").click(function() {
	try {
		$('.big_image').parent().find("p").remove();
		$('.big_image').media({ 
			width:     440, 
			height:    330, 
			autoplay:  true, 
			src:       $(this).attr("href"), 
			attrs:     { attr1:  'attrValue1',  attr2:  'attrValue2' },  // object/embed attrs 
			params:    { param1: 'paramValue1', param2: 'paramValue2' }, // object params/embed attrs 
			caption:   false // supress caption text 
		}); 
		
		$('.big_image').after("<p>"+$(this).attr("title")+"</p>");

	} catch(e){}
	});
	
	$(".g_image").eq(0).click();
	$(".picture_list li").eq(0).width(40);
	
	try
	{
		$("a[rel=fancybox]").fancybox({
		'speedIn'		:	400, 
		'speedOut'		:	400, 
		'overlayShow'	:	false,
		'transitionIn'	: 'elastic',
		'transitionOut'	: 'elastic'
		}).find("img").css("cursor", "url(/templates/images/magnify.cur)");

	} catch(e) {}	
	
});

function changeBackground()
{
	background_i++;

	$("body").find(".background").fadeOut(fadeTime);
	$("body").find(".background").eq(background_i).fadeIn(fadeTime);
	
	if(background_i == (arrBackgrounds.length - 1))
		background_i = -1;
}

function setOverflowContent()
{
	height = $(document).height();
	if($(".data").height() > height)
		height = $(".data").height(); 

	$(".internal").height($(window).height());
	$(".data").height(height - 123);
}

    function initialize() {
	try
	{
      if (GBrowserIsCompatible()) {
        var map = new GMap2(document.getElementById("map_canvas"));
		var x = "55.768985";
		var y = "37.645130";
        map.setCenter(new GLatLng(x,y), 16);
        map.setUIToDefault();
        

        var blueIcon = new GIcon(G_DEFAULT_ICON);
		blueIcon.iconSize = new GSize(24, 24); 
        blueIcon.image = "http://misato.ru/templates/images/logo-map.png";
		

		markerOptions = { icon:blueIcon };


        var bounds = map.getBounds();
        var southWest = bounds.getSouthWest();
        var northEast = bounds.getNorthEast();
   
          var latlng = new GLatLng(x,y);
          map.addOverlay(new GMarker(latlng, markerOptions));

      }
	 } catch(e){}
    }
