/**********************************************/
function microtime() {  
	var now = new Date().getTime() / 1000;  
	return now;
}

function in_array(needle, haystack, argStrict) {
    var key = '', strict = !!argStrict;
 
    if (strict) {
        for (key in haystack) {
            if (haystack[key] === needle) {
                return true;
            }
        }
    } else {
        for (key in haystack) {
            if (haystack[key] == needle) {
                return true;
            }
        }
    }
    return false;
}
function tooltipAfterChange(){
			$('a.dateAct').tooltip({ 
				bodyHandler: function() {
					var _curD = $(this).attr('rel');
					return (dataTip[_curD]?dataTip[_curD]:'');
				},
				track: true,
				delay: 0,
				showURL: false,
				positionLeft: true
			});
};

function flashVersion() {
    // Internet Explorer
    var ua = navigator.userAgent.toLowerCase();
    var isIE = (ua.indexOf("msie") != -1 && ua.indexOf("opera") == -1 && ua.indexOf("webtv") == -1);
    // инициализация
    var version = 0;
    var lastVersion = 20; // c запасом
    var i;
    var plugin;
    if (isIE) { // browser == IE
        try {
          for (i = 3; i <= lastVersion; i++) {
            if (eval('new ActiveXObject("ShockwaveFlash.ShockwaveFlash.'+i+'")')) {
                  version = i;
            }
          }
        } catch(e) {}
    } else { // browser != IE
        for (i in navigator.plugins) {
            plugin = navigator.plugins[i];
            if (plugin.name == undefined) continue;
            if (plugin.name.indexOf('Flash') > -1) {
                version = /\d+/.exec(plugin.description);
                if (version == null) version = 0;
            }
        }
    }
    return version;
}

function addBrandListener(){
	$('select.chooseBrand').change(function() {
		var node = $(this).attr('node');
		if (this.value.length)
		{
			var type = document.getElementById('_type'+node).value;
			$('#'+$(this).attr('node')+'_model').load('/jquery/php/showdata.php?salt='+microtime()+'&data=1&type='+type+'&brand='+this.value+'&field='+node);
			$('#'+$(this).attr('node')+'_model'+':hidden').slideToggle('slow');
		}
		else
		{
			$('#'+node+'_model'+':visible').slideToggle('slow');
			document.getElementById(node).length = 0;
		}
	});
};
jQuery.preloadImages = function() {
	jQuery.each (arguments,function (e) {
		jQuery("<img>").attr("src", this);
	});
}
/**********************************************/
$(document).ready(function(){ 

	// Замена flash-кнопки
	if (!flashVersion() && document.getElementById('flashMenu571'))
	{
		var flashDiv = $("div#flashMenu571");
		var pic = flashDiv.attr('rel');
		var href = flashDiv.attr('href');

		$("div#menuDiv571").html("<a href='"+href+"'><img src='"+pic+"'></a>");
	}

	/**************/
	$('select.chooseType').change(function() {
		var node = $(this).attr('node');
		if (this.value.length)
		{
			$('#'+node+'_parent').load('/jquery/php/showdata.php?salt='+microtime()+'&data=4&type='+this.value+'&field='+node,addBrandListener);
			$('#'+node+'_parent'+':hidden').slideToggle('slow',addBrandListener);
			$('#'+node+'_model'+':visible').slideToggle('slow');
			document.getElementById(node).length = 0;
		}
		else
		{
			$('#'+node+'_model'+':visible').slideToggle('slow');
			$('#'+node+'_parent'+':visible').slideToggle('slow');
			document.getElementById('_parent'+node).length = 0;
			document.getElementById(node).length = 0;
		}
	});
	addBrandListener();
	/**************/
	if (document.getElementById('stars'))
	{
		$('div#stars').find('div').mouseover(function(){
			var point = $(this).attr('value');
			for (var $i = 1; $i<=point; $i++) document.getElementById('star'+$i).style.backgroundPosition = "0 -18px";
		});
		$('div#stars').find('div').mouseout(function(){
			var curPoint = Math.round(document.getElementById('Point').value);
			if (curPoint <= 5)
			{
				var from = (curPoint < 1 ? 1 : curPoint + 1);
				for (var $i = from; $i<=5; $i++) document.getElementById('star'+$i).style.backgroundPosition = "0 0";
			}
		});
		$('div#stars').find('div').click(function(){
			var point = Math.round($(this).attr('value'));
			document.getElementById('Point').value = point;
			if (point < 5)
			{
				for (var $i = point+1; $i<=5; $i++) document.getElementById('star'+$i).style.backgroundPosition = "0 0";
			}
		});
	}
	/**************/
	if (document.getElementById('citySelect')) $('td#citySelectTD').load('/jquery/php/showdata.php?data=2&country=' + $("select#f_country").val() + '&cur=' + $('tr#citySelect').attr('cur') );
	/**************/
	$('select#f_country').change(function() {
		if (this.value.length)
		{
			if (document.getElementById('f_OtherCity')) document.getElementById('f_OtherCity').value='';
			if (document.getElementById('f_city')) document.getElementById('f_city').value='';
			if (in_array(this.value, ['165', '19', '209']))
			{
				document.getElementById('cityText').style.display='none';
				document.getElementById('citySelect').style.display='';
				$('td#citySelectTD').load('/jquery/php/showdata.php?data=2&country='+this.value);
			}
			else
			{
				document.getElementById('citySelect').style.display='none';
				document.getElementById('cityText').style.display='';
			}

			$('div#cityInput:hidden').slideToggle('slow');
		}
		else $('div#cityInput:visible').slideToggle('slow');
	});
	/**************/
	if (document.getElementById('calendRight'))
	{
		$.get('/jquery/php/showdata.php?data=3', function(data) {
			dataArray = new Array();
			dataTip = new Array();
			eval(data);

			$('#calendRight').calendarLite({
				showYear: true,
				dateFormat: '{%yyyy}-{%m}-{%d}',
				prevArrow: '<img src="/jquery/calendar/calendPrev.gif" style="margin-top: 3px">',
				nextArrow: '<img src="/jquery/calendar/calendNext.gif" style="margin-top: 3px">',
				linkFormat: '/calendar/{%yyyy}/{%mm}/{%dd}/'
			});
			tooltipAfterChange();
		});
	}
	/**************/
	if (document.getElementById('enterLink') && document.getElementById('authBlock'))
	{
		$('a#enterLink').click(function(){
			$('div#authBlock').toggle();
			$('input#AUTH_USER').focus();
			return false;
		});
	}
	/**************/
	var _authImg = new Image();
	_authImg.src = "/images/motoxp/authField.gif";

	$("form#choosePayType").submit(function(e){
		var checked = $("input#acceptRules").is(':checked');
		if (!checked)
		{
			alert("Необходимо ознакомиться и согласиться с условиями предоставления услуг!");
			return false;
		}
	});
});
