

function up(num, flag) {

    var id = flag+num;

	document.getElementById(id).className = "vis";

}

function hide(num, flag) {

    var id = flag+num;

	document.getElementById(id).className = "invis";

}







function tabClick(display,mask,cdisplay) {

	document.getElementById(display).className='flexTabs vis';

	for (i=0;i<mask.length;i++) {

		document.getElementById(mask[i]).className='flexTabs hid';

	}



	var dpView = '#'+display+'_loader';

	$(dpView).removeClass('ajax hid');

    $(dpView).addClass('ajax vis');

    $.ajax({

	    url:'http://'+location.hostname+'/ajax/index_tabs.php',

        type: 'GET',

        data: 'type=list&dest='+display,

        cache: false,

        success: function(table){

            var iHtm = '#'+cdisplay

            $(iHtm).html(table);

            $(dpView).removeClass('ajax vis');

            $(dpView).addClass('ajax hid');

        },

        error: function(){

          //  alert('ERROR');

        }

    });

}







function carmavote(type,id,value,spanid)

{

	var http = zXmlHttp.createRequest();

	http.open("get","http://"+location.hostname+"/ajaxvote.php?act=vote&id="+id+"&type="+type+"&value="+value);



	http.onreadystatechange = function () {

		if (http.readyState == 4)

		{

			document.getElementById(spanid).innerHTML = http.responseText;

		}

	}

	http.send(null);

	return false;

}





function starsVote(id,value,spanid,type)

{

	var http = zXmlHttp.createRequest();

	http.open("get","http://"+location.hostname+"/ajaxvote.php?act=vote&id="+id+"&type="+type+"&value="+value);



	http.onreadystatechange = function () {

		if (http.readyState == 4)

		{

			document.getElementById("ratingblock").innerHTML = "";

			document.getElementById(spanid).innerHTML = 'Рейтинг '+http.responseText;

		}

	}

	http.send(null);

	return false;

}



function updateMarks(type,mod) {

	var http = zXmlHttp.createRequest();

	http.open("get","http://"+location.hostname+"/ajax/auto_catalog.php?type="+type+"&mod="+mod);

	http.onreadystatechange = function () {

		if (http.readyState == 4) {

			document.getElementById('marksContainer').innerHTML = http.responseText;

		}

	}

	http.send(null);



	var hrefs = new Array('alphabet','country');

	for (i=0;i<hrefs.length;i++) {

		document.getElementById(hrefs[i]+'_href').className  = "def";

	}

	document.getElementById(mod+'_href').className  = "cli";



	var hhrefs = new Array('all','used','new');

	for (i=0;i<hhrefs.length;i++) {

		document.getElementById(hhrefs[i]+'_href').className  = "def";

	}

	document.getElementById('all_href').className = "cli";



}



function draw_comments(itemid,type,page) {





    var lUp = '#pager_loader_up';

    var lDw = '#pager_loader_down';

    $(lUp).removeClass('ajax hid');

    $(lUp).removeClass('hid');

    $(lUp).addClass('vis');

    $.ajax({

        url:'http://'+location.hostname+'/ajax/draw_comments.php',

        type: 'GET',

        data: "type="+type+"&itemid="+itemid+"&page="+page,

        cache: false,

        success: function(table){

            $('#comments_container').html(table);

            $(lUp).removeClass('vis');

            $(lUp).addClass('hid');



        },

        error: function(){

          //  alert('ERROR');

        }

    });





    /*

	new Request.HTML({

		url: 'http://'+location.hostname+'/ajax/draw_comments.php',

		method: 'get',

		data: "type="+type+"&itemid="+itemid+"&page="+page,

		update: $('comments_container'),

		onRequest: function() {

			$('pager_loader_up').className = 'ajax vis'

			$('pager_loader_down').className = 'ajax vis'

		},

		onComplete: function() {

			$('pager_loader_up').className = 'ajax hid'

			$('pager_loader_down').className = 'ajax hid'

		},

		evalScripts: false }).send();*/

}



function nl2br(text){

	var re_nlchar="";

	text = escape(text);

	if(text.indexOf('%0D%0A') > -1){

		re_nlchar = /%0D%0A/g ;

	}else if(text.indexOf('%0A') > -1){

		re_nlchar = /%0A/g ;

	}else if(text.indexOf('%0D') > -1){

		re_nlchar = /%0D/g ;

	}

	return unescape( text.replace(re_nlchar,'<br />') );

}

function text_preview(mode){

	if (document.getElementById("htmlarea").value!="") {

		var http = zXmlHttp.createRequest();

		var params = "text="+document.getElementById("htmlarea").value+"&mode="+mode;

		http.open("POST", "http://"+location.hostname+"/ajax/comment_preview.php", true);

		http.setRequestHeader("Content-type", "application/x-www-form-urlencoded");

		http.setRequestHeader("Content-length", params.length);

		http.setRequestHeader("Connection", "close");

		http.onreadystatechange = function () {

			if (http.readyState == 4 && http.status == 200) {

				document.getElementById("preview_container").innerHTML=http.responseText;

				$("#preview_container").slideDown('slow');

			}

		}

		http.send(params);

	} else {

		$("#preview_container").slideUp('slow');

	}

}





function pddCheck(id,question,answer,tm,resultType) {

	if (answer == '') {
		alert('Необходимо выбрать ответ.');
		return;
	}
	if (tm == undefined) {
		var tm = 0;
		tm = $('#cntdwn').html();
		tm = tm.substring(5,2);
		tm = tm.substring(1,3);
	}

	if (resultType == undefined) {
		resultType = '2';
	}
	
	if (bonus == 1) {
		dopStr = '&bonus=1';
	} else {
		dopStr = '';
	}

    $.ajax({

        url:'/ajax/pdd.php',
        type: 'GET',
        data: "id="+id+'&question='+question+'&answer='+answer+'&tm='+tm+'&resultType='+resultType+dopStr,
        cache: false,

        success: function(table){
           $('#container').html(table);
           initRaioSet();
		   initHinting();
        },

        error: function(){
          //  alert('ERROR');
        }

    });

}


function hideErrorSave() {
	$('#errorSave').hide();
}

function hideBonusErrorSave() {
	$('#BonusError').hide();
}

function showBonusError() {
	$('#BonusError').show();
	$('#BonusError').html('<br> Чтобы пройти бонусный билет нужно допустить не более 2 ошибок в 5 билетах.<br>');
	setTimeout("hideBonusErrorSave()", 6000);
}

function saveMarafonResult() {
	
    $.ajax({

        url:'/ajax/pdd_marafon.php',
        type: 'GET',
        data: 'act=save',
        cache: false,
        success: function(table){
			if (table == 0) {
				alert("Произошла ошибка при сохранении.");
			} else if (table == 3) {
				$('#errorSave').show();
				$('#errorSave').html("Для сохранения необходимо зарегистрироваться.<br><br>");
				setTimeout("hideErrorSave()", 6000);
			} else {
				$('#errorSave').show();
				$('#errorSave').css('color', '#009900');
				$('#errorSave').html("Данные успешно сохранены.<br><br>");
				setTimeout("hideErrorSave()", 6000);
			}
        },
        error: function(){
          //  alert('ERROR');
        }
    });
	
}



function marafonCheckQue(question, questionId, answer) {
	
    $.ajax({

        url:'/ajax/pdd_marafon.php',
        type: 'GET',
        dataType : "json",
        data: 'check=1&question='+question+'&questionId='+questionId+'&answer='+answer,
        cache: false,
        success: function(table){
			if (table['result'] == 0) {
				$('#errorHint').html('<span style="color:#B51403;">Ошибка!</span><br><i>'+table['hint']+'</i>');
				$('#biletCount').html(table['count']);
				$('#htmlbut').html(table['htmlbuttom']);
				$('#errorHint').show();
				$('#errorContainer').show();
			}
        },
        error: function(){
          //  alert('ERROR');
        }
    });
}


function marafonNextQue(question, questionId, answer) {
	

	if (answer == '' && answer != 'next') {
		alert('Необходимо выбрать ответ.');
		return;
	}
	
	$('#htmlbut').html('&nbsp;');
	
    $.ajax({

        url:'/ajax/pdd_marafon.php',
        type: 'GET',
        data: 'marafon=1&question='+question+'&questionId='+questionId+'&answer='+answer,
        cache: false,

        success: function(table){
           $('#container').html(table);
           initRaioSet();
		   initHinting();
        },

        error: function(){
          //  alert('ERROR');
        }
    });
}



function pddRestart(id) {

	if (bonus == 1) {
		dopStr = '&bonus=1';
	} else {
		dopStr = '';
	}

    $.ajax({

        url:'http://'+location.hostname+'/ajax/pdd.php',
        type: 'GET',
        data: "id="+id+'&act=restart'+dopStr,
        cache: false,
        success: function(table){
           $('#container').html(table);
        },

        error: function(){
          //  alert('ERROR');
        }

    });



}


function pddRestartMarafon() {



    $.ajax({

        url:'http://'+location.hostname+'/ajax/pdd_marafon.php',
        type: 'GET',
        data: "act=restart",
        cache: false,
        success: function(table){
           $('#container').html(table);
        },

        error: function(){
          //  alert('ERROR');
        }

    });

}



function previewUploded(type) {



    $.ajax({

        url:'http://'+location.hostname+'/js/uploader/thumbnail.php',

        type: 'GET',

        data: 'type='+type,

        cache: false,

        success: function(table){

           $('#thumbnails').html(table);

        },

        error: function(){

          //  alert('ERROR');

        }

    });



}



function deleteCarphoto(id) {



    $.ajax({

        url:'http://'+location.hostname+'/ajax/editcar.php',

        type: 'GET',

        data: 'act=delete&id='+id,

        cache: false,

        success: function(table){

           $('#car_pics_list').html(table);

        },

        error: function(){}

    });



}

function number_format( number, decimals, dec_point, thousands_sep ) {

    var n = number, c = isNaN(decimals = Math.abs(decimals)) ? 2 : decimals;

    var d = dec_point == undefined ? "." : dec_point;

    var t = thousands_sep == undefined ? "," : thousands_sep, s = n < 0 ? "-" : "";

    var i = parseInt(n = Math.abs(+n || 0).toFixed(c)) + "", j = (j = i.length) > 3 ? j % 3 : 0;



    return s + (j ? i.substr(0, j) + t : "") + i.substr(j).replace(/(\d{3})(?=\d)/g, "$1" + t) + (c ? d + Math.abs(n - i).toFixed(c).slice(2) : "");

}



function deleteUploded(id, type, carid) {


	var str;
	if (type == 'carsell') {
		str = 'carsell_thumbnail.php';
	} else {
		str = 'thumbnail.php';
	}

    $.ajax({
 
        url:'http://'+location.hostname+'/js/uploader/'+str,
        type: 'GET',
        data: 'id=delete&pId='+id+'&type='+type+'&carid='+carid,
        cache: false,
        success: function(table){
           $('#thumbnails').html(table);
           drag_initiate();
           initDeleteButton();
        },

        error: function(){
          //  alert('ERROR');
        }

    });



}



function tag_it(id) {



	img = document.getElementById('pic_'+id);

	if (img.src) {

		var str = img.src;

		str = str.replace('/img.php?size=80&pic=', '/img.php?size=500&pic=');

		insert_text_img_blog(str);

	}

}













var limSwitcherRight = 4;

var limSwitcherLeft = 0;



function retMarkNews(marka, type, model) {

    if (type == 'right') {
        var swtch = limSwitcherRight;
    } else {
        var swtch = limSwitcherLeft;
    }

    var dopStr = '&marka='+marka+'';
    if (model != '' || model != 0) {
        dopStr+= '&model='+model;
    }

    $.ajax({

	    url:'/ajax/marksFunctional.php',
        type: 'GET',
        data: 'type=drawNews&lim='+swtch+dopStr,
        cache: false,
        success: function(table){

            $('#singeNewsContent').html(table);

            if (type == 'right') {

                limSwitcherLeft = limSwitcherRight-4;
                limSwitcherRight = limSwitcherRight+4;

                if (limSwitcherRight == 16) {
                    $('#lNewsSwitchRight').hide();
                }

            } else {

                $('#lNewsSwitchRight').show();
                limSwitcherRight = limSwitcherLeft+4;
                limSwitcherLeft = limSwitcherLeft-4;

            }



            if (limSwitcherLeft < 0) {

                $('#lNewsSwitchLeft').hide();

            } else {

                $('#lNewsSwitchLeft').show();

            }



        },

        error: function(){

            //  alert('ERROR');

        }

    });



}









function ajMarkSwitch(type, brand, alias, name) {



        $("#loadSpinner").ajaxStart(function(){
			$(this).html('<img alt="" src="/js/autocompleter/spinner.gif"/>');
        });

        $.ajax({

	    url:'/ajax/marksFunctional.php',
        type: 'GET',
        data: 'type=drawModels&gType='+type+'&brand='+brand+'&alias='+alias+'&name='+name,
        cache: false,

        success: function(table){
            if (jQuery.trim(table) != 'error') {
                $('#ajModels').html(table);
            } else {
                $("#loadSpinner").html('');
            }
        },

        error: function(){
            //  alert('ERROR');
        }
    });

}



function ajModelSwitch(type, marka, model, modelRuname, modelId) {


        $("#loadSpinner").ajaxStart(function(){
			$(this).html('<img alt="" src="/js/autocompleter/spinner.gif"/>');
        });

        $.ajax({
	    url:'/ajax/marksFunctional.php',
        type: 'GET',
        data: 'type=drawGen&gType='+type+'&marka='+marka+'&model='+model+'&modelRuname='+modelRuname+'&modelId='+modelId,
        cache: false,

        success: function(table){
            if (jQuery.trim(table) != 'error') {
                $('#generationId').html(table);
            } else {
                $("#loadSpinner").html('');
            }
        },

        error: function(){
            //  alert('ERROR');
        }
    });
}





function hide_newsText() {

   if (document.body.clientWidth<1200) {

       $('.hideText').hide();

   } else {

       $('.hideText').show();

   }

}

function ajaxVoteMark(star, type, objectId) {

    $.ajax({

	    url:'/ajax/ajaxvote.php',
        type: 'GET',
        data: 'act=voteMark&type='+type+'&star='+star+'&objectId='+objectId,
        cache: false,
        success: function(table){
            $('#modelRating').html(table);
        },
        error: function(){
          //  alert('ERROR');
        }

    });

}


var mWidth = 0;

function mwt() {
	mWidth = $("#mw").width();
	if (jQuery.trim($('#usedA').html()) != '' && jQuery.trim($('#newA').html()) != '') {
    	if(mWidth < 1050) {
    		$("td.carPics div").width(mWidth - 680);
    		$("td.carPics div img").width(mWidth - 680);
    		$("#smallSize").addClass("show");
    		$("#bigSize").removeClass("show");
    	} else {
    		$("td.carPics div").width(400);
    		$("td.carPics div img").width(400);
    		$("#smallSize").removeClass("show");
    		$("#bigSize").addClass("show");
    	}
	}
}

$(document).ready(function(){
	/* rating */
	var xPos = 0;
	var yPos = 0;
	var ofs = 0;
	var pers = 0;
	if(navigator.userAgent.indexOf("Opera") !== -1) {
		$("#helper").addClass("hb");
	}


	$("div.changeRating strong").hover(
		function(){
			$("#helper").css("display","block");
		},
		function(){
			$("#helper").css("display","none");
		}
	);

	makeDynamique();

});


function makeDynamique(userRating) {

    if (userRating != '') {
		$("div.changeRating b").width(userRating);
		$("div.changeRating i").text(userRating);
		$("#hiddenVoteValue").attr("value", userRating);
    }

	$("div.changeRating strong").mousemove(function(e){
		xPos = e.pageX;
		yPos = e.pageY;
		ofs = $("div.changeRating strong").offset();
		pers = Math.floor(xPos - ofs.left);
		if (pers > 100) {
		    pers = 100;
		} else if (pers < 0) {
		    pers = 0;
		}

		$("div.changeRating b").width(pers);
		$("div.changeRating i").text(pers);
		$("#hiddenVoteValue").attr("value", pers);
		$("#helper").css("left", xPos - 50).css("top", yPos - 55);
	});
}


function voteModel(objectId, user, marka) {

    var value = $('#hiddenVoteValue').attr("value");
    $.ajax({
	    url:'/ajax/marksFunctional.php',
        type: 'GET',
        data: 'type=voteModel&value='+value+'&user='+user+'&objectId='+objectId+'&marka='+marka,
        cache: false,
        success: function(table){
            $('#modelVoteAj').html(table);
        },
        error: function(){
          //  alert('ERROR');
        }
    });

}

function activateHelper(id) {
    var realId = '#'+id;
    $(realId).mouseover(function(e) {
		xPos = e.pageX;
		yPos = e.pageY;
		$("#helper2").css("display","block");
        $("#helper2").css("left", xPos - 50).css("top", yPos - 55);
    })
}

function deactivateHelper(id) {
    $("#helper2").css("display","none");
}

function add2Chosen(objId, objType, objStrId, starType) {

    var strId = '#'+objStrId;
    var imgId = '#im_'+objId;

    $.ajax({
	    url:'/ajax/marksFunctional.php',
        type: 'GET',
        dataType : "json",
        data: 'add2Chosen=1&objId='+objId+'&objType='+objType+'&starType='+starType,
        cache: false,
        success: function(table){

            if (table['starColor']=='gold') {

                if (table['starType'] == 0) {
                    $(imgId).attr('src','/img/ico/star-gold.gif');
                } else {
                    $(imgId).attr('src','/img/ico/bigstar-gold.gif');
                }

            } else if (table['starColor']=='white') {

                if (table['starType'] == 0) {
                    $(imgId).attr('src','/img/ico/star-white.gif');
                } else {
                    $(imgId).attr('src','/img/ico/bigstar-white.gif');
                }

            }
           // $(strId).html(table);
        },
        error: function(){
          //  alert('ERROR');
        }
    });

}

