var myBigMap = false;

$(function()
{
	var myBlock = new  etu_radius('.catalogMenu ul',4,4,4,4);
	myBlock = new  etu_radius('.catalog',7,7,7,7);
	$('.catalogUp').css('opacity',0.7);
	myBlock = new ShowPics();
	
	$('.simpleText tr:even').css("background",'#313131');
	$('.simpleText tr:odd').css("background",'#181818');
	
	myContacts();
});

function myContacts()
{
	if (! $('.contactsBlock .item').size() ) return;
	
	$($('.contactsBlock .item')[0].parentNode).attr("class","sel");
	
	
	$('.contactsBlock .item').bind('click', function()
	{
		$('.contactsBlock li').attr("class","");
		$(this.parentNode).attr("class","sel");
		ShowCurentMap(this);
	});
	
	ShowCurentMap($('.contactsBlock .item')[0]);
}

function ShowCurentMap(myObj)
{
	var myCoords = $(myObj).attr("coord").split(",");
	var myText = $(myObj).attr("text");
	newMap(myCoords,myText);
}


function newMap(myCoords,myText)
{
	 var myDiv = $("#myBigMap")[0];
	 myDiv.innerHTML = '';
	 var map = new YMaps.Map(myDiv);
     map.setCenter(new YMaps.GeoPoint(myCoords[0],myCoords[1]), 15, YMaps.MapType.MAP);
     map.addControl(new YMaps.Zoom());
      map.addControl(new YMaps.ToolBar());
        map.addControl(new YMaps.TypeControl([
            YMaps.MapType.MAP,
            YMaps.MapType.SATELLITE,
            YMaps.MapType.HYBRID,
            YMaps.MapType.PMAP
        ], [0, 1, 2, 3]));    
       map.openBalloon(new YMaps.GeoPoint(myCoords[0],myCoords[1]), myText,{});
}


var ShowPics = function()
{
//.BigItem .smallPreview img
	
	var myPic = $("#big").size() ? $("#big")[0] : false;
	
	if (! myPic) return;
	this.myPic = myPic;
	this.myWidth = parseInt($(myPic).attr("width"));
	this.myHeight = parseInt($(myPic).attr("height"));
	this.myPics = $('.BigItem .smallPreview img');
	
	this.myBig = false;
	this.bottom = 0;
		
	
	this.UpdateBig = function()
	{
		
		var myObj = this;
		
		$(this.myPic)
			.unbind('click')
			.bind('click', function()
			{
				var myBottom = $(this).offset().top - 4;
				if ($('.table table').size())
				{
					myBottom = $($('.table table')[0]).offset().top;
				}
				myObj.bottom = myBottom;
				myObj.myBig = new Image();
				myObj.myBig.src = '/file/preview/900-900-'+myObj.UpdatePic($(this).attr("src"));
				myObj.InsertPic();
				return(false);
			});
	}
	
	this.InsertPic = function()
	{
		var myObj = this;
		if (! $(this.myBig).attr("complete"))
		{
			setTimeout(function() { myObj.InsertPic(); },1000);
			return(false);
		}
		var myW = $(this.myBig).attr("width");
		var myH= $(this.myBig).attr("height");
		$('#ShowBigPic').remove();
		
		$('<div id="ShowBigPic"></div>')
			.appendTo("body")
			.css({
				'position':'absolute',
				'top' : this.bottom,
				'left': '50%',
				'width' : myW,
				'height' : myH,
				"marginLeft": 0 - (myW/2)
			})
			.hide();
		$(this.myBig).appendTo("#ShowBigPic");
		$("#ShowBigPic").fadeIn("normal");
		
		$('body').bind('click',function(event) {
			myObj.removePic();
		});
		
	}
	
	this.removePic = function()
	{
		$('body').unbind('click');
		$('#ShowBigPic').remove();
	}
	
	
	
	this.UpdateSmall = function()
	{
		if (! this.myPics.size()) return;
		var myPic = this.myPics[0];
		var myWidth = parseInt($(myPic).attr("width"));
		var myHeight = parseInt($(myPic).attr("height"));
		
		
		var mySrc = this.UpdatePic($(this.myPic).attr("src"));
		var myObj = this;
				
		this.myPics.each(function()
		{
			$(this)
				.unbind('click')
				.bind('click', function()
				{
					var myOld = myObj.UpdatePic($(this).attr("src"));
					$(myObj.myPic).attr("src",'/file/crop/'+myObj.myWidth+'-'+myObj.myHeight+'-'+myOld);
					$(this).attr("src",'/file/crop/'+myWidth + '-' + myHeight + '-' + mySrc);
					
					myObj.UpdateBig();
					myObj.UpdateSmall();
				});
		});
		
	}
	
	this.UpdatePic = function(mySrc)
	{
		mySrc = mySrc.split("/");
		mySrc = mySrc[mySrc.length - 1].split("-")
		var myResult = new Array();
		for (var i = 0; i < mySrc.length; i++)
			if (i > 1) myResult[myResult.length] = mySrc[i];
		myResult = myResult.join("-");
		return myResult;
	}
	
	this.UpdateSmall();
	this.UpdateBig();
}
