/*
 * preload
 */

var iz = $('<img />');
var iz_load = false;

iz.load(function(){
	iz_load = true;
	$(document).ready(function(){
		$('div.iz img').show();
		/*$('div.iz').hover(function(){
			$(this).find('.ly').stop().animate({
				opacity: 0
			},1000);
			var i = $(this).find('img');
			i.stop().animate({
				top: 100
			},3000,'',function(){
				i.animate({
					top: -100
				},6000,'',function(){
					i.animate({
						top: 0
					},3000);
				});
			});
		},function(){
			$(this).find('img').stop().animate({
				top: 0
			},4000);
			$(this).find('.ly').stop().animate({
				opacity: 1
			},1000);
		});
		*/
	});
});
iz.attr('src','/template/i/iz.png');

$(function(){
	var ie = $.browser.msie;

	$('div.eform input.text')
	.blur(function(){
		$(this).val($(this).val() == '' ? $(this).attr('title') : $(this).val());
	}).focus(function(){
		$(this).val($(this).val() == $(this).attr('title') ? '' : $(this).val());
	}).blur();

	$('div.eform form').submit(function(){
		$(this).find('input.text').each(function(){
			if ($(this).attr('title') != '' && $(this).val() == $(this).attr('title')){
				$(this).val('');
			}
		});
	});
	
	if ($('div.geo-sp').length){
		$('div.geo-sp div').hover(function(){
			var w = $(this).width();
			var l = $(this).offset().left;
			var lr = $('div.r-indicator').offset().left;
			$('div.r-indicator span').stop().animate({
				width: (l+w/2)-lr
			},500);
		},function(){
			$('div.r-indicator span').stop().animate({
				width: 0
			},500);
		});
	}
	
	/*
	 * fb-form
	 */

	if ($('div.fb-form').length){
		$('div.footer div.contacts a.dh').click(function(){
			if ($('div.order-form').hasClass('order-show')) {
				$('div.order-form a.close').click();
			}
			if (!$('div.fb-form').hasClass('fb-show')){
				if (ie){
					$('div.fb-form').stop().css({
						display: 'block',
						bottom: 120
					}).animate({
						bottom: 150
					},300,'',function(){
						$('div.fb-form').addClass('fb-show');
					});
				}else{
					$('div.fb-form').stop().css({
						display: 'block',
						opacity: 0,
						bottom: 120
					}).animate({
						bottom: 150,
						opacity: 1
					},300,'',function(){
						$('div.fb-form').addClass('fb-show');
					});
				}
			}else{
				if (ie){
					$('div.fb-form').stop().css({
						bottom: 150
					}).animate({
						bottom: 120
					},300,'', function(){
						$('div.fb-form').hide().removeClass('fb-show');
					});
				}else{
					$('div.fb-form').stop().css({
						opacity: 1,
						bottom: 150
					}).animate({
						bottom: 120,
						opacity: 0
					},300,'', function(){
						$('div.fb-form').hide().removeClass('fb-show');
					});
				}
			}
			return false;
		});
		
		$('div.fb-form a.close').click(function(){
			if (ie){
				$('div.fb-form').stop().css({
					bottom: 150
				}).animate({
					bottom: 120
				},300,'',function(){
					$('div.fb-form').hide().removeClass('fb-show');
				});
			}else{
				$('div.fb-form').stop().css({
					opacity: 1,
					bottom: 150
				}).animate({
					bottom: 120,
					opacity: 0
				},300,'',function(){
					$('div.fb-form').hide().removeClass('fb-show');
				});
			}
			return false;
		});
		
		var l = document.location.href;
		var ind = l.indexOf('#fb');
		if (ind > 0){
			$('div.fb-form').css({
				opacity: 1,
				bottom: 150
			}).addClass('fb-show').show();
		}
	}
	
	if ($('div.order-form').length){
		$('div.title .order a.dh').click(function(){
			$('div.header div.contacts a.dh').click();
			return false;
		});
		$('div.header div.contacts a.dh').click(function(){
			$('div.title .order').fadeOut(300);
			if ($('div.fb-form').hasClass('fb-show')) {
				$('div.fb-form a.close').click();
			}
			if (!$('div.order-form').hasClass('order-show')){
				if (ie){
					$('div.order-form').stop().css({
						display: 'block',
						top: 90
					}).animate({
						top: 130
					},300,'',function(){
						$('div.order-form').addClass('order-show');
					});
				}else{
					$('div.order-form').stop().css({
						display: 'block',
						opacity: 0,
						top: 90
					}).animate({
						top: 130,
						opacity: 1
					},300,'',function(){
						$('div.order-form').addClass('order-show');
					});
				}
			}else{
				if (ie){
					$('div.order-form').stop().css({
						top: 130
					}).animate({
						top: 90
					},300,'',function(){
						$('div.order-form').hide().removeClass('order-show');
					});
				}else{
					$('div.order-form').stop().css({
						opacity: 1,
						top: 130
					}).animate({
						top: 90,
						opacity: 0
					},300,'',function(){
						$('div.order-form').hide().removeClass('order-show');
					});
				}
			}
			return false;
		});
		
		$('div.order-form a.close').click(function(){
			if (ie){
				$('div.order-form').stop().css({
					top: 130
				}).animate({
					top: 90
				},300,'',function(){
					$('div.order-form').hide().removeClass('order-show');
				});
			}else{
				$('div.order-form').stop().css({
					opacity: 1,
					top: 130
				}).animate({
					top: 90,
					opacity: 0
				},300,'',function(){
					$('div.order-form').hide().removeClass('order-show');
				});
			}
			return false;
		});
		
		var l = document.location.href;
		var ind = l.indexOf('#order');
		if (ind > 0){
			if (ie) {
				$('div.order-form').css({
					top: 130
				}).addClass('order-show').show();
			}else{
				$('div.order-form').css({
					opacity: 1,
					top: 130
				}).addClass('order-show').show();
			}
		}
	}

	/*
	 * iz
	 */

	if (!iz_load){
		$('div.iz img').hide();
	}
	
	/*
	 * services
	 */

	$('div.services').show();
	var services_total = $('div.services a.service').length;

	$('div.services a.service').hide();
	$('div.services a.service').eq(Math.round(Math.random()*(services_total-1))).css('display', '');
	
	function update_bt(i){
		i += 15;
		if (i > 270){
			i = 0;
		}
		$('div.services a.update').css({
			'background-position': '0 -' + i + 'px'
		});
		if (i != 0) {
			setTimeout(function(){
				update_bt(i);
			},15);
		}else{
			$('div.services a.update').css({
				'background-position': ''
			});
		}
	}
	
	$('div.services a.update').click(function(){
		if (!$(this).hasClass('disabled')){
			$(this).addClass('disabled');
			var el = $('div.services a.service:hidden').eq(Math.round(Math.random()*(services_total-2)));
			$('div.services a.service:visible').animate({
				'width': 0
			},200,'',function(){
				$(this).hide();
				var w = el.css({
					'display': '',
					'visibility': 'hidden',
					'width': ''
				}).width();
				el.css({
					'width': 0,
					'visibility': ''
				}).animate({
					'width': w
				},200,'',function(){
					$(this).css('width', 'auto');
					$('div.services a.update').removeClass('disabled');
				});
			});
			update_bt(0);
		}
		return false;
	});
	
	/*
	 * company
	 */
	
	if ($('div.geo-small').length){
		var gs = Raphael($('div.geo-small').get(0), 420, 530);
		var gpath = [
			'M88.4,400l-10,130',
			'M88.4,400l26-10',
			'M114.4,390.3l67-2',
			'M206.4,416.3l-25-28',
			'M114.4,390.3l15-74',
			'M238.4,270.3l-109,46',
			'M129.4,316.3l-12-155',
			'M273.4,170.3l-35,100',
			'M82.4,66.3l35,95',
			'M82.4,66.3l-75,38',
			'M80.4,4.3l2,62',
			'M273.4,170.3l96-65',
			'M369.4,105.3l34-16',
			'M135.4,140.3l-18,21',
			'M82.4,169.3l35-8',
			'M255.4,295.3l-17-25',
			'M258.4,147.3l15,23',
			'M350.4,85.3l19,20'
		];
		var gcircle = [
			['117.4','160.3'],
			['273.4','170.3'],
			['369.4','105.3'],
			['238.4','270.3'],
			['129.4','316.3'],
			['114.4','390.3'],
			['181.4','388.3'],
			['7.4','104.3'],
			['206.4','416.3'],
			['82.4','66.3'],
			['80.4','4.3'],
			['88.4','400']
		];
		var g_path = [];
		$.map(gpath,function(el,i){
			g_path[i] = gs.path(el);
			g_path[i].attr({
				'stroke': '#ccc',
				'stroke-width': '1px'
			});
		});

		var g_circle = [];
		$.map(gcircle,function(el,i){
			g_circle[i] = gs.circle(el[0],el[1],3.5);
			g_circle[i].attr({
				'stroke': '#ccc',
				'stroke-width': '1px',
				'fill': '#fff'
			});
		});

		g_circle[11].attr({
			'stroke': '#069'
		});

		var c_path = [
			[1,4,6],
			[1,4,5,7],
			[1,4,5,7,11],
			[1,4,5],
			[1,4],
			[1],
			[1,2],
			[1,4,6,8,9],
			[1,2,3],
			[1,4,6,8],
			[1,4,6,8,10]
		];

		var c_circle = [
			[5,4,0],
			[5,4,3,1],
			[5,4,3,1,2],
			[5,4,3],
			[5,4],
			[5],
			[5,6],
			[5,4,0,9,7],
			[5,6,8],
			[5,4,0,9],
			[5,4,0,9,10]
		];

		for (var i=0;i<=10;i++){
			$(g_circle[i].node).mousemove(function(){
				var t = [];
				$.map(g_circle,function(e){
					t.push(e.node);
				}); 
				var index = $.inArray(this,t);
				
				var a = [1,2,3,4,5,6,7,8,9,10,11];
				var b = [];
				$.map(a,function(e){
					if ($.inArray(e,c_path[index]) == -1){b.push(e);}
				});
				$.map(b,function(e){
					g_path[e].animate({'stroke': '#ccc'},300);
				});
				$.map(c_path[index],function(e){
					g_path[e].animate({'stroke': '#069'},300);
				});
				
				var a = [0,1,2,3,4,5,6,7,8,9,10];
				var b = [];
				$.map(a,function(e){
					if ($.inArray(e,c_circle[index]) == -1){b.push(e);}
				});
				$.map(b,function(e){
					g_circle[e].animate({'stroke': '#ccc'},300);
				});
				$.map(c_circle[index],function(e){
					g_circle[e].animate({'stroke': '#069'},300);
				});
				
				$('div.geo-small .vc:not(.vc11)').not('.vc'+index).stop().animate({
					color: '#ccc'
				},300);
				$('div.geo-small .vc'+index).stop().animate({
					color: '#069'
				},300);
			});
		}
		$('div.geo-small .vc').mousemove(function(){
			var i = $('div.geo-small .vc').index(this);
			$(g_circle[i].node).mousemove();
		});
	}

	/*
	 * a
	 */

	if ($('div.a').length){
		var paper = Raphael($('div.a div.wr').get(0), 90, 90);
		/*
		var c = paper.path('M45,30c8.284,0,15,6.716,15,15s-6.716,15-15,15s-15-6.716-15-15S36.716,30,45,30z')
		.attr({
			'stroke': '#0095d9',
			'stroke-width': '1.2px'
		});
		*/
		var cv = paper.path('M45,25c11.046,0,20,8.954,20,20c0,11.045-8.954,20-20,20c-11.045,0-20-8.955-20-20C25,33.954,33.955,25,45,25z')
		.attr({
			'stroke': '#fff',
			'stroke-width': '4px',
			'stroke-opacity': 0
		});
	
		var f1 = paper.path("M45 30L45 20")
		.attr({
			'stroke': '#0095d9',
			'stroke-opacity': .7
		});
		var f2 = f1.clone();
		
		var center_pos = {}, cursor_pos = {};
		var ep = paper.path();
		var ag = paper.text(0, 0, '');
	
		$(document).mousemove(function(e){
			cursor_pos.top = e.pageY;
			cursor_pos.left = e.pageX;
			center_pos = $('div.a div.wrapper').offset();
			center_pos.top = center_pos.top + 45;
			center_pos.left = center_pos.left + 45;
			
			var angle = Raphael.angle(center_pos.left, center_pos.top, cursor_pos.left, cursor_pos.top);
			angle -= 90;
			if (angle < 0){
				angle = 360 + angle; 
			}
			
			var w = 0;
			var h = 0;
			
			var total = cv.getTotalLength();
			var path = cv.getSubpath(0, total/360 * angle);
	
			ep.remove();
			ep = paper.path(path).attr({
				'stroke': '#0095d9',
				'stroke-width': '4px',
				'stroke-opacity': .7
			});
			
			f2.remove();
			f2 = f1.clone().rotate(angle, 45, 45);
			ag.remove();
			
			angle_c = angle - 90;
			if (angle_c < 0){
				angle_c = 360 + angle_c; 
			}
			//ag = paper.text(40,10,Math.round(angle)+'°')
			ag = paper.text(45+Math.cos(Raphael.rad(angle_c))*33, 45+Math.sin(Raphael.rad(angle_c))*33,Math.round(angle)+'°')
			.attr({
				fill: '#069'
			});
		});
	}
});
