﻿jQuery.cookie = function(name, value, options) {
    if (typeof value != 'undefined') { // name and value given, set cookie
        options = options || {};
        if (value === null) {
            value = '';
            options.expires = -1;
        }
        var expires = '';
        if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
            var date;
            if (typeof options.expires == 'number') {
                date = new Date();
                date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
            } else {
                date = options.expires;
            }
            expires = '; expires=' + date.toUTCString(); // use expires attribute, max-age is not supported by IE
        }
        // CAUTION: Needed to parenthesize options.path and options.domain
        // in the following expressions, otherwise they evaluate to undefined
        // in the packed version for some reason...
        var path = options.path ? '; path=' + (options.path) : '';
		//var path='/';
        var domain = options.domain ? '; domain=' + (options.domain) : '';
        var secure = options.secure ? '; secure' : '';
        document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
    } else { // only name given, get cookie
        var cookieValue = null;
        if (document.cookie && document.cookie != '') {
            var cookies = document.cookie.split(';');
            for (var i = 0; i < cookies.length; i++) {
                var cookie = jQuery.trim(cookies[i]);
                // Does this cookie string begin with the name we want?
                if (cookie.substring(0, name.length + 1) == (name + '=')) {
                    cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                    break;
                }
            }
        }
        return cookieValue;
    }
};
	
	var sinres=0;
	
	var cel=0,cel_wait=0;
	var celx,cely;
	var created=0;
	var contener;
	var anim_run=0;
	var now='';
	var glob_kd=5;
	var z=-1,x,y;
	var cr=244;
	var cb=145;
	var grad=0;
	
	var ip=280;
	var lip=264;
	var ipold=0;
	var lipold=0;
	var x1=0,y1=0;
	var mode=1;
	var angl=280;
	var langl=264;
	
	function rad(grad){return (grad*(Math.PI/180));}

	function g_reset(){
		created=0;z=-1;x=0;y=0;cr=244;cb=145;grad=0;now='';
		ip=280;lip=264;ipold=0;lipold=0;x1=0,y1=0;mode=1;angl=280;langl=264;		
	}

	function create(){
		anim_run=1;
		var cel_pos=cel.position();
		cel_pos.left=cel_pos.left+(cel.width()/2);
		cel_pos.top=cel_pos.top+((cel.height()+15)/2)+parseFloat(cel.css('margin-top'));

		x=Math.round(cel_pos.left+(Math.cos(rad(grad))*62));
		y=Math.round(cel_pos.top-(Math.sin(rad(grad))*62));
		if((cr>250)){z=-1;}
		if((cr<234)){z=1;}
		cr=cr+(0.72*z);
		cb=cb+(3.7*z);
		$("#service").append('<div class="point" id="r'+grad+'"></div>').find("#r"+grad).css('left',x).css('top',y).css('background-color','rgb('+Math.round(cr)+','+Math.round(cb)+',19)');
		grad=grad+8;
		if(grad<=352){setTimeout('create()',glob_kd);}else{anim_run=0;if(cel_wait!=0){run(cel_wait);}cel_wait=0;}
	}

	function remove(){
		anim_run=1;
		$("#service #r"+grad).remove();
		grad=grad-8;
		if(grad>=0){setTimeout('remove()',glob_kd);}else{anim_run=0;g_reset();if(cel_wait!=0){run(cel_wait);}cel_wait=0;}
	}
	
	function loginin(txt){
		//$(".log").append('<p>ip='+ip+'; '+'lip='+lip+'; ipold='+ipold+'; lipold='+lipold+'; angl='+angl+'; langl='+langl+';</p>');
		$(".log").append('<p>'+sinres+'---'+txt+';</p>');
	}

	function reposition(){
		anim_run=1;
		var cel_pos=cel.position();
		cel_pos.left=cel_pos.left+(cel.width()/2);
		cel_pos.top=cel_pos.top+((cel.height()+15)/2)+parseFloat(cel.css('margin-top'));
		var lx=10;
		var ly=(cel_pos.top-cely)/((cel_pos.left-celx)/lx);
		
		if(mode==1){
			if(cel_pos.left-celx>0){
				if(ip>352){ip=0;}
				x1=x1+lx;
				y1=y1+ly;
				contener.find("#r"+ip).css('left',x1).css('top',y1+62);
				ip=ip+8;
				if(x1<cel_pos.left){setTimeout('reposition()',glob_kd);}else{mode=2;ipold=ip;setTimeout('reposition()',10);}
				return;
			}else{
				if(lip<0){lip=352;}
				x1=x1-lx;
				y1=y1-ly;
				contener.find("#r"+lip).css('left',x1).css('top',y1+62);
				lip=lip-8;			
				if(x1>cel_pos.left){setTimeout('reposition()',glob_kd);}else{mode=2;lipold=lip;setTimeout('reposition()',10);}
				return;				
			}
		}		
		
		if(mode==2){
			if(cel_pos.left-celx>0){
				if(ip>352){ip=0;}if(angl>352){angl=0;}
				var x=Math.round(cel_pos.left+(Math.cos(rad(angl))*62));
				var y=Math.round(cel_pos.top-(Math.sin(rad(angl))*62));
				contener.find("#r"+ip).css('left',x).css('top',y);
				ip=ip+8;angl=angl+8;
				if(ip!=ipold){setTimeout('reposition()',glob_kd);}else{ip=ipold;lip=ipold-16;mode=1;anim_run=0;if(cel_wait!=0){run(cel_wait);}cel_wait=0;}
				return;
			}else{
				if(lip<0){lip=352;}if(langl<0){langl=352;}
				var x=Math.round(cel_pos.left+(Math.cos(rad(langl))*62));
				var y=Math.round(cel_pos.top-(Math.sin(rad(langl))*62));
				contener.find("#r"+lip).css('left',x).css('top',y);
				lip=lip-8;langl=langl-8;
				if(lip!=lipold){setTimeout('reposition()',glob_kd);}else{lip=lipold;ip=lipold+16;mode=1;anim_run=0;if(cel_wait!=0){run(cel_wait);}cel_wait=0;}
				return;	
			}
		}
	}
	
	function sinhronize_pointer(){
		if(sinres<=0){
			var now_r=contener.find('.'+now).position();
			var left=now_r.left+(cel.width()/2);
			//contener.find('.pointer').each(function(index) {});
			for(grad=0;grad<=352;grad=grad+8){
				x=Math.round(left+(Math.cos(rad(grad))*62));
				$("#service").find("#r"+grad).css('left',x);
			}
			contener.find('.point').show();
		}else{sinres=sinres-1;setTimeout('sinhronize_pointer()',30);}
	}
	
	function circles(){
		if(created==0){create();created=1;}
		else{if(anim_run==0){reposition();}}
	}
	
	function run(run){
		if(run=='remove'){if(created==1){remove();}return;}
		if(!run.parent().hasClass(now)){
		if(cel!=0){celx=cel.position().left+(cel.width()/2);
					cely=cel.position().top+((cel.height()+15)/2)+parseFloat(cel.css('margin-top'));
					x1=celx;
					y1=cely;
					}
		cel=run.parent();
		now=run.parent().attr('class');
		if(now.indexOf(' ')!=-1){now=now.substring(0,now.indexOf(' '));}
		cel_wait=0;
		circles();
		}
	}

//********************************************************************************************************************************************************************
$(document).ready(function(){


	$('#content .sels').addClass('sels_js');
	$('#content .sels li>*').not('img, h2, h3').hide();
	$('#content .sels li h2:not(:has(>a)), #content .sels li h3').wrapInner("<span></span>").find('span').click(function(){
			if($(this).parent().next().css('display')=='block'){
				$(this).parent().parent().children().not('img, h2, h3, span').hide();
			}else{
				$(this).parent().parent().children().show();
			}
		});
	

	contener=$("#service");
	contener.find('li.sel').css('background','none');
	if((contener.find('li.sel').size()>0)&(!contener.hasClass('no'))){if(anim_run==0){run(contener.find('li.sel img'));}else{cel_wait=contener.find('li.sel img');}contener.find('li.sel').css('background','none');}
	
	contener.find('li a').hover(function(){if((anim_run==0)&(!contener.hasClass('no'))){run($(this));}else{cel_wait=$(this);}}
								,function(){});
	
	contener.hover(function(){},function(){
											if((contener.find('li.sel').size()>0)&(!contener.hasClass('no'))){if(anim_run==0){run(contener.find('li.sel img'));}else{cel_wait=contener.find('li.sel img');}
											}else{if(anim_run==0){remove();}else{cel_wait='remove';}}
										  });

	function closeforms() {
		$('#kabinet').removeClass('loging');
		$('#kabinet .form, #kabinet .l div, #kabinet .r div, #kabinet .bot2').hide()/*IE7*/;
	}
	
	contener.find('.close').show();

	contener.find('.webcam a').attr("target", "_blank");

	
	contener.find('.close').click(
		function () {
			$('#logoshadow').removeClass('expand');
			closeforms();
			if(!$(this).parent().hasClass('no')){
				
				cel_wait='remove';
				$("#service .point").remove();
				g_reset();
				
				$(this).parent().find('ul').slideUp(300).end().addClass('no');
				$.cookie("service_display",0,{ path: '/'});
			}else{
					$(this).parent().find('ul').slideDown(300,function(){if((contener.find('li.sel').size()>0)&(!contener.hasClass('no'))){if(anim_run==0){run(contener.find('li.sel img'));}else{cel_wait=contener.find('li.sel img');}contener.find('li.sel').css('background','none');}else{created=0;}}).end().removeClass('no');
					$.cookie("service_display",1,{ path: '/'});
				}
			return false;	
	});
	
	$('#kabinet input[type=text], #kabinet input[type=password]').each(function(){
			var inputText = $(this);
			inputText.val(inputText.attr('title'));
			inputText.focus(function(){if(inputText.val() == inputText.attr('title')) inputText.val('').css("color","#000");});
			inputText.blur(function(){if(inputText.val() == '') inputText.val(inputText.attr('title')).css("color","#dcdcdc");});
			});

	$('#selreg').click(
		function () {
			closeforms();
			var expand = $('#logoshadow');
			if(expand.hasClass('expand')){expand.removeClass('expand');}else{expand.addClass('expand');}
			return false;	
	});

	$('#logoshadow .close').click(function (){$('#logoshadow').removeClass('expand');return false;});
	
	$('#print_page').click(function (){window.print();return false;});

	$('#kabinet .enterkey a').click(
		function () {
			$('#logoshadow').removeClass('expand');
			var expand = $('#kabinet');
			if(expand.hasClass('loging')){expand.removeClass('loging');$('#kabinet .form, #kabinet .l div, #kabinet .r div, #kabinet .bot2').hide()/*IE7*/;}
			else{expand.addClass('loging');
				$('#kabinet div').css('display','block')/*IE7*/;
				}
			return false;	
	});

	$('#content .slide').addClass('js').find('li').not('.sel').find('p, ul, ol, table').hide();

	$('#content .slide h2').click(
		function () {
			if($(this).parent().hasClass('sel')){}
			else{
			$(this).parent().parent().find('.sel').removeClass('sel').find('p, ul, ol, table').slideUp(300);
			$(this).parent().addClass('sel').find('p, ul, ol, table').slideDown(300);}
			return false;	
	});
	
	
	var id=0;
	
	$('#sity').prepend('<li id="sitylist"><span class="frs">Ваш город:</span></li>');

	$('#sity h2').each(function (a){
		$(this).hide().parent().attr('id','sl'+id).hide();
		$('#sitylist').append('<span id="l'+id+'">'+$(this).text()+'</span>');
		id++;
		
		$(this).parent().find('.tariffs').prepend('<li class="list"></li>');	
		
		$(this).parent().find('h3').each(function (a){
			$(this).hide().parent().attr('id','tl'+id).hide().parent().find('.list').append('<span id="l'+id+'">'+$(this).text()+'</span>');
			id++;
		});
		
	});
	
	
	$('#sitylist').append('<span class="note">(сменить город можно в меню в левом верхнем углу)</span>');

	$('#sitylist span').not('.frs, .note').bind("click", function (){sitychange($(this));return false;});
	$('#sity .list span').bind("click", function (){tariffchange($(this));return false;});
	$('#sitylist span[id=l0]').addClass('sel').parent().next().show();
	$('#sity .list').find('span:first').addClass('sel').parent().next().show();

	function sitychange(a){
		$('#s'+$('#sitylist .sel').attr('id')).hide();
		$('#sitylist .sel').removeClass('sel');
		a.addClass('sel');
		$('#s'+a.attr('id')).show();		
	}

	function tariffchange(a){
		$('#t'+a.parent().find('.sel').attr('id')).hide();
		a.parent().find('.sel').removeClass('sel');
		a.addClass('sel');
		$('#t'+a.attr('id')).show();
	}
	
	$('#faq li').find('p:first').wrapInner("<span></span>").parent().find('div').hide();
	$('#faq span').toggle(function () {$(this).parent().next().show();},function () {$(this).parent().next().hide();});
	$('#addanswer a').addClass('addjs').click(function () {$('#window').show().find('textarea').focus();return false;});
	$('#window .close').click(function (){$(this).parent().hide();return false;});
	
	$('#content .news .heading').show().find('span').click(function (){if($(this).attr('id')=='all'){$('#content .news li').show();}
														  	else{$('#content .news li').show().not('.month,.heading, .'+$(this).attr('id')).hide();}
															$(this).parent().find('.sel').removeClass('sel');$(this).addClass('sel');});
	
	
	$('#content .search').addClass('run').find('#es1').show();
	$('#content .search .example span').click(function (){ $('#search').attr('value',$(this).text());});
	$('#content .search .type span').click(function (){ if($(this).hasClass('sel')){}
														else{
															$('#t'+$(this).attr('id')).attr('checked','checked');
															$('#content .search .example span').hide();
															$('#e'+$(this).attr('id')).show();
															$(this).parent().find('.sel').removeClass('sel');$(this).addClass('sel');
															
															if($(this).attr('id')=='s2'){$(this).parent().parent().find('.tags_menu').show();}
															else{$(this).parent().parent().find('.tags_menu').hide();}
															}});
	
	$('#ts2:checked').parent().find('.sel').removeClass('sel').end().find('#s2').addClass('sel');
	$('#ts2:checked').parent().parent().find('.example span').hide().end().find('.tags_menu').show().end().find('#es2').show();

	
	$('#content .myform .click span').click(function (){
													  var edit = $(this).parent().parent().find('input');
													  if(edit.attr('value')==''){edit.attr('value',$(this).text());}
													  else{edit.attr('value',edit.attr('value')+', '+$(this).text());}
														return false;
													  });

	$('#content .search .tags_menu p span').click(function (){$('#search').attr('value',$('#search').attr('value')+$(this).text()+', ');});
	
	$('#content #all').toggle(function (){$('#content .tags_menu').addClass('open');},function (){$('#content .tags_menu').removeClass('open');});

	$('#content .information .getnext').show().click(function (){
																  var re = /q(\d+)/;
																  var m = re.exec($(this).parent().find('.m h1').attr('id'));
																 // $(this).addClass('load').parent().find('.m').load('quote.php?id='+parseInt(m[1]),'',function(){$('#content .information .getnext').removeClass('load');});
																  $(this).addClass('load').parent().find('.m').load('/quote/random/'+parseInt(m[1]),'',function(){$('#content .information .getnext').removeClass('load');});

																  return false; 
															   });
															   
															   
															   
	$('#radio_pult ul li a').click(function(){
			
			$('#radio_pult').parent().find('.radiolist li').show().not('.radio_'+$(this).attr('class')).hide();
			$(this).parent().parent().find('.sel').removeClass('sel').end().end().addClass('sel');
			return false;
		});
	
	function getTop() {
		var wrap = $('#window');
		middleHDoc = $(window).height() / 2;
		middleHFeed = wrap.height() / 2;
		if ( middleHFeed > middleHDoc )
			middleHFeed = middleHDoc;   
		var topF = middleHDoc - middleHFeed;
		
		middleWDoc = $('#main').width() / 2;
		middleWFeed = wrap.width() / 2;
		if ( middleWFeed > middleWDoc )
			middleWFeed = middleWDoc;   
		var leftF = middleWDoc - middleWFeed;
		
		if( window.pageYOffset ) 
		{	topF = topF + window.pageYOffset;
			leftF = leftF + window.pageXOffset;
		} 
		else if( document.documentElement.scrollTop ) 
		{	topF = topF + document.documentElement.scrollTop;	
			leftF = leftF + document.documentElement.scrollLeft;
		} 
		else 
		{	topF = topF + document.body.scrollTop;
			leftF = leftF + document.body.scrollLeft;	
		}
		topF = (topF < 0) ? 0 : topF;
		leftF = (leftF < 0) ? 0 : leftF;
		wrap.css('top', topF + 'px');
		wrap.css('left', leftF + 'px');

		return true;
	}
	getTop();
	$(window).scroll(function(){getTop();});
	$(window).resize(function(){getTop();
								if(sinres==0){contener.find('.point').hide();sinres=10;setTimeout('sinhronize_pointer()',50);}else{sinres=10;}
								
								});

});
