﻿// JavaScript Document
$(document).ready(function()
{
	
})


function init(images){


for (var i = 0; i<images.length; i++)
{
	$("#content").append('<a href="'+images[i].href+'"><span><img src="'+images[i].imgsrc+'" width="'+images[i].width+'" height="'+images[i].height+'" /></span></a>')
	
	images[i].tmargin = Math.floor(Math.random()*55)+40
	images[i].hmargin = Math.floor(Math.random()*45)+10
		
	if (i == parseInt(images.length/2)-4)	
		$("#content").append('<div class="clear"></div>')
	if (i == parseInt(images.length/2)-2)	{	
		images[i].tmargin = 40
		$("#content").append('<span class="logo"><img src="/images/logo.png" width="247" height="105" /></span>')	
		}
	if (i == parseInt(images.length/2)-0)	
		$("#content").append('<div class="clear"></div>')
		
	
}

var logo = $("#content").find(".logo")


	$("#content").find("a").each(function(i){
	
		$(this)
		.css({
			"width": images[i].width*.40+images[i].tmargin,
			"height": images[i].height*.40/2+images[i].hmargin
			})
			.find('span').css({
				"width": images[i].width*.40,
				"height": images[i].height*.40/2
				})
			.find('img').css({
				"width": images[i].width*.40,
				"height": images[i].height*.40
				})
				
				
			
				
				
	})
	
	
	
$left = $("#content").offset()["left"]
$wid = $("#content").width()
$top = $("#content").find("a:first").offset()["top"];
$max = 0;
var $items = [];

$("#content").find("a, .logo").each(function(i) {


	if (($(this).offset()["top"] == $top))
		{
			$items.push($(this));
		}
		else {
			for(var j = 0; j < $items.length; j++) {
			$max = Math.max($max, $items[j].height())
			}
			
			
			width = 0
			if (($items[0].offset()["top"]>=logo.offset()["top"])&&($items[0].offset()["top"]<=logo.offset()["top"]+logo.height())) 			
			{
				for(var j = 0; j < $items.length; j++){
					width = $wid
				}
			}
			else
			for(var j = 0; j < $items.length; j++){
					width += $items[j].width()
				}
				
			for(var j = 0; j < $items.length; j++){
				$items[j].height($max)
				$items[j].attr('left',($wid-width)/2)
				}
						
			$max = 0;
			
		$top = $(this).offset()["top"]
		$items = []
		$items.push($(this));
		}		
		
	})	
			width = 0	
			for(var j = 0; j < $items.length; j++){
				width += $items[j].width()
				}
				
			for(var j = 0; j < $items.length; j++){
				$items[j].attr('left',($wid-width)/2)
				}
	

	$("#content").find("a").each(function(i) {
		$(this).css({
			top: $(this).offset()["top"],
			left: $(this).offset()["left"] - $left + parseInt($(this).attr('left'))
		})
		.attr("rel",i)
		images[i].top = $(this).offset()["top"]
		images[i].left = $(this).offset()["left"] - $left + parseInt($(this).attr('left'))
		images[i].bottom = images[i].top + $(this).height()
		images[i].right = images[i].left + $(this).width()
	})	
	
	$("#content").find(".logo").each(function() {
		$(this).css({
			top: $(this).offset()["top"],
			left: $(this).offset()["left"] - $left,
			"position":"absolute"
		})
		})
	
	$("#content").find("a").each(function(i) {
		$(this).css({
			"position":"absolute"
		})
		
		if (!$(this).hasClass("logo"))
		$(this).hover(
			function(){			
				$(this).addClass('hover').css("z-index", "100")
					
				$(this).find('img')
						.css({
							"top": (images[i].height/2)*(-1)
						})
						.stop()
						.animate({
							"width": images[i].width,
							"height": images[i].height
						},200)
				$(this).find('span').stop().animate({
						"width": images[i].width,
						"height": images[i].height/2
						},200)
				$(this).stop().animate({
						"left": images[i].left-(images[i].width*.30),
						"top": images[i].top-(images[i].height*.30)/2,
							"width": images[i].width,
							"height": images[i].height/2
						},200, 
						function(){						
						
						})
						
					a = ({x:images[i].left-(images[i].width*.30), y:images[i].top-(images[i].height*.30)})
					b = ({x:a.x+parseInt(images[i].width), y:a.y+parseInt(images[i].height)})
					c1 = ({x:a.x+(b.x-a.x)/2, y:a.y+(b.y-a.y)/2}) //выбраный
					
							$items = []
							
				for (var j = 0; j<images.length; j++)
					{
					
					c = ({x:images[j].left, y:images[j].top})
					d = ({x:images[j].right, y:images[j].bottom})
					
						if (is_in_sq(a,b,c,d)&&(i!==j))
							{
					c2 = ({x:c.x+(d.x-c.x)/2, y:c.y+(d.y-c.y)/2}) //двигаемый
								if (c1.y>c2.y)
										move_top = 1;
									else
										move_top = -1
								if (c1.x>c2.x)
										move_left = 1;
									else
										move_left = -1
							left = 1
							
							h = 1
							
							$("#content").find("a:eq("+j+")")
								.addClass("move")
								.stop()
								.animate({									
									"left": images[j].left-(images[i].width*.30-20)*move_left*h,
									"top": images[j].top-(images[i].height*.10)*move_top
								})
							$items.push(j);
							}
					}
					for (var j = 0; j<$items.length; j++) {
							move($items[j],images,1)
				}
			},
			function(){
				$(this).removeClass('hover').css("z-index", "1")			
				
				$(this).find('img')
						.css({
							"top": 0
						})
						.stop().animate({
							"width": images[i].width*.40,
							"height": images[i].height*.40
						},200)
				$(this).find('span').stop().animate({
					"width": images[i].width*.40,
					"height": images[i].height*.40/2
					},200)
				$(this).stop().animate({
						"left":	images[i].left,//+(images[i].width*.125),
						"top": images[i].top//+(images[i].height*.125)
					},200, 
					function(){
						//clear() 
					})
					.css({										
						"width": images[i].width*.40,
						"height": images[i].height*.40/2
						})
					
							$("#content").find("a.move").each(function(){
								$(this).stop().animate({									
									"left": images[$(this).attr("rel")].left,
									"top": images[$(this).attr("rel")].top
								})
								.removeClass("move")
								})
								
					$("#content").find("a").css("border",'0')
			})		
	
		})
}




function is_in_sq( a, b, c, d ) {
	return (
		(in_sq(c, d, a)) || 
		(in_sq(c, d, b)) || 
		(in_sq(c, d, ({x:a.x, y:b.y}))) ||  
		(in_sq(c, d, ({x:b.x, y:a.y})))
	||	
		(in_sq(a, b, c)) || 
		(in_sq(a, b, d)) || 
		(in_sq(a, b, ({x:c.x, y:d.y}))) ||  
		(in_sq(a, b, ({x:d.x, y:c.y})))
)
}


function in_sq(a,b,c){
if ((a.x<=c.x)&&(b.x>=c.x)&&(a.y<=c.y)&&(b.y>=c.y)) return true
		return false
}


function move(i,images,rec) {

					a = ({x:images[i].left, y:images[i].top})
					b = ({x:images[i].right, y:images[i].bottom})
					c1 = ({x:a.x+(b.x-a.x)/2, y:a.y+(b.y-a.y)/2}) //выбраный
					
							$items = []
				for (var j = 0; j<images.length; j++)
					{
					
					c = ({x:images[j].left, y:images[j].top})
					d = ({x:images[j].right, y:images[j].bottom})
						if (is_in_sq(a,b,c,d)&&(i!==j))
							{
					c2 = ({x:c.x+(d.x-c.x)/2, y:c.y+(d.y-c.y)/2}) //двигаемый
								if (c1.y>c2.y)
										move_top = 1;
									else
										move_top = -1
								if (c1.x>c2.x)
										move_left = 1;
									else
										move_left = -1
							
							
							
							$("#content").find("a:eq("+j+")").each(function(){
								if ((!$(this).hasClass('move'))&&(!$(this).hasClass('hover'))) {
								$(this)
								.addClass("move")
								.stop()
								.animate({									
									"left": images[j].left-20*move_left,
									"top": images[j].top-10*move_top
								})
								
								}
							})
							$items.push(j);
							}
					}
}

