var goodline =
{
	form :
	{
		init : function()
		{
			var $m5 = jQuery.noConflict();
			var tarifs = Array();
			var shag = 0.96;
			var del = shag;
			var show = 0;

			
			if (!variant) var variant='json';
			
			if (variant=='xml')
			{
				$m5.ajax({
				type: "GET",
				url: "/tariffs.xml",
				dataType: "xml",
				error: function(XMLHttpRequest, textStatus, errorThrown){alert('Ошибка загрузки данных с goodline.ru');},
				success: function(xml) {
					
					var i = 0;
					var ul = '';
					$m5(xml).find('country').each(function(){
						tarifs[i] = $m5(this).find('title').text();
						ul += '<li value="'+i+'">'+tarifs[i]+'</li>';
						i++;
						tarifs[i] = $m5(this).find('vhod').text();
						i++;
						tarifs[i] = $m5(this).find('vihod').text();
						i++;
					});
					$m5('#gscroll').after('<ul>'+ul+'</ul>');
					$m5('#gSelect').find('li:odd').addClass('gbg');
				}
			});

			}
			
			if (variant=='php')
			{
				$m5.ajax({
				type: "GET",
				url: "/goodlinexml.php",
				dataType: "xml",
				error: function(XMLHttpRequest, textStatus, errorThrown){alert('Ошибка загрузки данных с goodline.ru');},
				success: function(xml) {
					
						var i = 0;
						var ul = '';
						$m5(xml).find('country').each(function(){
							tarifs[i] = $m5(this).find('title').text();
							ul += '<li value="'+i+'">'+tarifs[i]+'</li>';
							i++;
							tarifs[i] = $m5(this).find('vhod').text();
							i++;
							tarifs[i] = $m5(this).find('vihod').text();
							i++;
						});
						$m5('#gscroll').after('<ul>'+ul+'</ul>');
						$m5('#gSelect').find('li:odd').addClass('gbg');
					}
				});
			}
			
			if (variant=='json')
			{
				$m5.getJSON("http://goodline.ru/tarif/tariffs.php?callback=?",function(data){
			
			
					var i = 0;
					var ul = '';
					$m5.each(data, function(j,item){
           		
						tarifs[i] = item.title;
						ul += '<li value="'+i+'">'+tarifs[i]+'</li>';
						i++;
						tarifs[i] = item.vhod;
						i++;
						tarifs[i] =item.vihod;
						i++;
					
          			});
				
					$m5('#gscroll').after('<ul>'+ul+'</ul>');
					$m5('#gSelect').find('li:odd').addClass('gbg');
			
				}); 
			}
			
			var $m5form = $m5('#goodlineForm');
			$m5form.find('#radiusTop').html('<div class="gBrd topbrd3"></div><div class="gBrd topbrd2"></div><div class="gBrd topbrd1"></div>');
			$m5form.find('#radiusBottom').html('<div class="gBrd topbrd1"></div><div class="gBrd topbrd2"></div><div class="gBrd topbrd3"></div>');
			$m5('#selectCountry').click(function(){
				$m5('#gSelect').toggle();
				if (show == 0)
				{
					show_scroll();
					show = 1;
				}
				return false;	
			});
			$m5('#gSelect li').live('click', function(){
				$m5('#gSelect').hide();
				$m5('#selectCountry a').text($m5(this).text()).attr('alt',$m5(this).text()).attr('title',$m5(this).text());
				var value = $m5(this).attr('value');
				$m5('#gvhod').text(tarifs[value+1]);
				$m5('#gvixod').text(tarifs[value+2]);
			});
			
			if ($m5.browser.msie && $m5.browser.version.substr(0,1)<7) {
				$m5('#gSelect li').live('hover', function(){
					$m5('#gSelect li').css('text-decoration','none');
					$m5(this).css('text-decoration','underline');
				});
			}

			$m5('#goodlineForm').hover(function(){
					$m5('body').mousewheel(function(event, delta, deltaX, deltaY) {
						return false;
					});
				},function(){
					$m5('body').unbind('mousewheel');
			});

			function show_scroll()
			{
				var $m5scroll = $m5('#gsc');
				var $m5ul = $m5('#gSelect ul');
				var $m5ulHeight = $m5('#gSelect ul').height()-$m5('#gSelect').height();
				var $m5scrollBot = $m5scroll.parent().height()-1-$m5scroll.outerHeight();


				$m5('#gSelect').mousewheel(function(event, delta, deltaX, deltaY) {



					if (delta > 0) 
					{
						del -= shag;
						if(del == 0 || del <=shag) del = 1;
						$m5scroll.css({top:del});
						$m5ul.css({top:(((del-1)/($m5scrollBot-1)*100)*$m5ulHeight/100)*-1});
					}
					if (delta < 0)
					{
						del += shag;
						if(del >= $m5scrollBot) del = $m5scrollBot;
						$m5scroll.css({top:del});
						$m5ul.css({top:(((del-1)/($m5scrollBot-1)*100)*$m5ulHeight/100)*-1});
					}

				});

				$m5scroll.mousedown(function(e){
					var $m5scrollTop = $m5scroll.parent().offset().top;
					$m5(document).mousemove(function(e){
						var $m5top = e.pageY-$m5scrollTop;
						$m5top <= 1 ? $m5top = 1 : '';
						$m5top >= $m5scrollBot ? $m5top = $m5scrollBot : '';
						del = $m5top;
						$m5scroll.css({top:$m5top});
						$m5ul.css({top:((($m5top-1)/($m5scrollBot-1)*100)*$m5ulHeight/100)*-1});
					});
					return false;
				});
				$m5(document).mouseup(function(){
					$m5(document).unbind('mousemove');
				});
			}
				
		}
	}
}
