// JavaScript Document
$(document).ready(function () {
	switch(true) {
		case $.browser.msie: e="ie"; break;
		case $.browser.mozilla: var d=(/^1\.9\.1/.test($.browser.version)?"35":"30"); e="firefox firefox"+d; break;
		case $.browser.opera: var d=(/^9\.80/.test($.browser.version)?"10":"9"); e="opera opera"+d; break;
		case /chrome/i.test(navigator.userAgent): e="chrome"; break;
		case $.browser.safari:e="safari"; break;
	}
	$('.rur_indent').addClass(e);
	
	$('#top_menu DIV.menu').cornerz({
		corners : "tl tr",
		background: '#007aae',
		radius: 6,
		borderWidth: 1,
		borderColor: '#78bddb'
	});
	
	if ( $('#top_menu DIV').hasClass("selected") )
	{
		$('#top_menu DIV.selected').css('background','#fff').css('border-color','#fff');
		$('#top_menu DIV.selected A').css('color','#007eb4').css('text-decoration','none');
		$('#top_menu DIV.selected').cornerz({
			corners : "tl tr",
			background: '#007aae',
			radius: 6,
			borderWidth: 1,
			borderColor: '#fff'
		});
	}
	
	$('#top_menu DIV.addmessage').cornerz({
		corners : "tl tr",
		background: '#007aae',
		radius: 6,
		borderWidth: 1,
		borderColor: '#bfdb98'
	});
	
	$('.navigation').cornerz({radius: 6});
	$('.category').cornerz({radius: 6, borderWidth: 1, borderColor: '#8cc53e'});	
	$('#salescars').cornerz({radius: 6, borderWidth: 1, borderColor: '#8cc53e'});
	$('#salescars .inside_block').cornerz({radius: 6, corners : "bl br", background: '#8cc53e'});
	$('.column_block, .auto').cornerz({radius: 6});
	$('.list_category UL.header LI.date, .list_category UL.header LI.foto, .list_category UL.header LI.name, .list_category UL.header LI.price, .list_category UL.header LI.part, .list_category .even, .list_category .selected_annoucement, .last_annoucement UL.header LI.foto, .last_annoucement UL.header LI.model, .last_annoucement UL.header LI.year, .last_annoucement UL.header LI.note, .last_annoucement UL.header LI.date, .last_annoucement UL.header LI.price, .last_annoucement .even, .last_annoucement .selected_annoucement').cornerz({radius: 6});
	$('.column_block .inside_block, .auto .inside_block').cornerz({radius: 6, corners : "bl br", background: '#1187b9'});
	$('DIV.bottom_message, #livebroadcast').cornerz({radius: 6, background: '#fff'});
	$('DIV.bottom_message .inside').cornerz({radius: 6, background: '#8cc53e'});
	$('.top_livebroadcast .live').cornerz({radius: 5, corners : "tl tr", background: '#8cc53e'});
	$('.top_livebroadcast_new .live').cornerz({radius: 5, corners : "tl tr", background: '#8cc53e'});
	//$('#content_lb_new .comments .comment_text').cornerz({radius: 6, borderWidth: 1, borderColor: '#d5ecba'});
	//$('#content_lb DIV SPAN.comment').cornerz({radius: 5, background: '#fff', borderWidth: 1, borderColor: '#d5ecba'});
	//$('#hover_menu').cornerz({radius: 6, corners : "bl br", borderWidth: 1});
	//$('.bookmarks DIV A').cornerz({radius: 4, corners : "tl tr"});
	
	$('#add_hover').hover(function () {
		$(this).addClass('hover');
		$('#hover_menu, #hover_menu_back').css('display', 'block');
	});
	
	$('#hover_menu_back').mouseover(function () {
		$('#hover_menu, #hover_menu_back').css('display', 'none');
		$('#add_hover').removeClass('hover');
	});
	
	$('.checked_group SPAN').click(function () {
		var id_chek = $(this).attr('class');
		if($('#'+id_chek).attr('checked')==true) $('#'+id_chek).attr('checked','');
		else $('#'+id_chek).attr('checked','checked');
	});

	
	var show = false;
	$("#click_expand_search").click(function () {
		//if (show) {
			$('.big_search_form .bl').css('background-position','-56px -85px');
			$('.big_search_form .br').css('background-position','-63px -85px');
		//}
											  
		$("#expand_search").slideToggle(500, function () {
			if (show) {
				$('#click_expand_search').text('Расширенный поиск'); 
				show = false;
				$('.big_search_form .bl').css('background-position','0 -20px');
				$('.big_search_form .br').css('background-position','-7px -20px');
			}
			else {$('#click_expand_search').text('Стандартный поиск'); show = true;}
		});
    });
	
	$('.replace_img').each(function () {
		$(this).click(function () {
			var id_e = this.id;
			var id_element = id_e.replace('replace_', '');
			$('#menuphoto_'+id_element).fadeOut('10', function () {
				$('#fotofile_'+id_element).fadeIn('100').before('<span id="off_'+id_element+'" rel="'+id_element+'" class="off_replace" style="position: absolute; top:3px; left:-285px; cursor: pointer; z-index:1000;">отмена</span>');
				$('#fotofile_'+id_element+' INPUT').removeClass('id_foto_view');
			});
		});
	});
	
	$('#off_0').click(function () {
		alert(0);
		$(this).remove();
		$('#fotofile_'+id_element).fadeOut('10', function () {
			$('#fotofile_'+id_element+' INPUT').addClass('id_foto_view');
			$('#menuphoto_'+id_element).fadeIn('100');
		});
	});
	
	$('.jam A').attr('target', '_blank');
});

function equalHeight(group) {
    tallest = 0;
    group.each(function() {
        thisHeight = $(this).height();
        if(thisHeight > tallest) {
        tallest = thisHeight;
        }
    });
    group.height(tallest);
}

/**------------------------------------------------------------------**/
/*
 * Findform
 * 
 */

$(document).ready(function(){
	if($.jqURL.get('category')>0) setdataform();
});

function catalogff_category()
{
	id = $('#category option:selected').val();
	$.ajax({
		url: '/listen/auto/catalogfindform.php?action=changecategory&id='+id ,
	    dataType : "text",        
	    success: function (data, textStatus) {
			$('#model').attr('disabled', 'disabled');
			$('#mark option').remove();
			$('#mark').append(data);
			$('#mark option:first').attr('selected', 'yes');
			//
			mark = $.jqURL.get('mark');
			if(mark!=null && $.jqURL.get('category')>0)
			{
				activefield('mark');
			}
			catalogff_mark();
	    }
	});	
}

function catalogff_mark()
{
	$('#model').attr('disabled', '');
	var id = $('#mark').val();
	if(id>0)
	{
		$.ajax({
		    url: '/listen/auto/catalogfindform.php?action=changemark&id='+id ,
		    dataType : "text",
		    success: function (data, textStatus) {
				$('#model option').remove();
				$('#model').append(data);
				$('#model option:first').attr('selected', 'yes');
				if($.jqURL.get('category')>0)activefield('model');
		    }
		});
	} else
	{
		$('#model option').remove();
		if($.jqURL.get('category')>0) $('#model').append('<option value="0">все</option>');
		else $('#model').append('<option value="0">не выбранно</option>');
		$('#model option:first').attr('selected', 'yes');
	}
}

function setdataform()
{
	activefield('category');
	catalogff_category();
	activefield('state');
	activefield('year_start');
	activefield('year_end');
	$('#price_start').attr('value', $.jqURL.get('price_start'));
	$('#price_end').attr('value', $.jqURL.get('price_end'));
	activefield('carbody');
	activefield('color');
	activefield('motortype');
	activefield('gear');
	activefield('kpp');
	activefield('rudder');
	$('#motor_start').attr('value', $.jqURL.get('motor_start'));
	$('#motor_end').attr('value', $.jqURL.get('motor_end'));
	if($.jqURL.get('isnew')!=null)
		$('#isnew').attr('checked', 'checked');
	if($.jqURL.get('isrun')!=null)
		$('#isrun').attr('checked', 'checked');

}

function activefield(name)
{
	value = $.jqURL.get(name)
	if(value!=null)
	$('#'+name+' OPTION').each(function () {
		if (this.value==value) this.selected='selected';
	})
}

function catalog_other()
{
	val = $('#model option:selected').val();
	if(val == -1) $('#modelother').removeAttr("disabled");
	else {
		$('#modelother').attr('value', '');
		$('#modelother').attr('disabled', 'disabled');
	}	
}

function beginwait(classname)
{
	
}

function endwait(classname)
{
	
}

/***notepad***/

function AddNotepad(id)
{
	id_click_view = id;
	var options = { 
	    	beforeSubmit: BeforeNotepad,
	    	error: ErrorNotepad,
	    	success: ResponseNotepad,
	    	dataType:  'json',
	    	timeout: 3000 
  	};
	$("#note"+id).ajaxSubmit(options);
}

function BeforeNotepad()
{
	$('#click_view_'+id_click_view).fadeOut('10');
	//alert('BeforeNotepad');
}

function ErrorNotepad()
{
	alert('Приносим свои извинения, попробуйте позже');
}

function ResponseNotepad(data)
{
	if(data['result']>0)
	{
		$('#notepadcount').text('Блокнот ('+data['result']+')');
		$('#click_view_'+id_click_view).fadeIn('100').css('text-decoration','none').attr('onclick', '').html('<b style="color:#8cc53e;">Добавлено</b>');
	} else 
	{
		$('#click_view_'+id_click_view).fadeIn('100').css('text-decoration','none').attr('onclick', '').html('<b style="color:#F7941D;">Было добавлено ранее</b>');
	}
}

function delNotepad(part, element)
{
	var options = { 
	    	beforeSubmit: BeforeDelNotepad,
	    	error: ErrorDelNotepad ,
	    	success: ResponseDelNotepad, 
	    	dataType:  'json',
	    	timeout: 10000
  	};
	$("#note_"+part+"_"+element).ajaxSubmit(options);
	$("#"+part+"_"+element).css('display', 'none');
}

function BeforeDelNotepad()
{
	//alert('BeforeNotepad');
}

function ErrorDelNotepad()
{
	alert('Приносим свои извинения, попробуйте позже');
}

function ResponseDelNotepad(data)
{
	$('#notepadcount').text('Блокнот ('+data['result']+')');
}

/*********************************/



/********** del photo ************/

function delphoto(number)
{
	id= 'photo_'+number;
	var options = { 
	    	beforeSubmit: BeforeDelPhoto,
	    	error: ErrorDelPhoto ,
	    	success: ResponseDelPhoto, 
	    	dataType:  'json',
	    	timeout: 3000
  	};
	ans=confirm('Вы действительно хотите удалить фото?');
	if(ans) $("#"+id).ajaxSubmit(options);
}

function BeforeDelPhoto() { }

function ErrorDelPhoto()
{
	alert('Приносим свои извинения, попробуйте позже');
}

function ResponseDelPhoto(data)
{
	if(data['result']==1)
	{
		$("#menuphoto_"+data['number']).fadeOut('10');
		$("#fotofile_"+data['number']).fadeIn('100');
		$("#fotofile_"+data['number']+" INPUT").removeClass('id_foto_view');
	}
}

/*********************************/

/********** del auto ************/

function delauto(id)
{
	if(id>0)id= 'delauto'+id;
	else id= 'delauto';
	var options = { 
	    	error: ErrorDelAuto ,
	    	success: ResponseDelAuto, 
	    	dataType:  'json',
	    	timeout: 3000
  	};
	ans=confirm('Вы действительно хотите удалить объявление ?');
	if(ans) $("#"+id).ajaxSubmit(options);
}

function ErrorDelAuto()
{
	alert('Приносим свои извинения, попробуйте позже');
}

function ResponseDelAuto(data)
{
	if(data['result']==1)
	{
		alert('Ваше объявление удалено');
		document.location.href= '/my.html';
	}
}

/*********************************/

/********** del do ************/

function deldo(id)
{
	if(id>0)id= 'deldo'+id;
	else id = 'deldo';
	var options = { 
	    	error: ErrorDelDo ,
	    	success: ResponseDelDo, 
	    	dataType:  'json',
	    	timeout: 3000
  	};
	ans=confirm('Вы действительно хотите удалить объявление ?');
	if(ans) $("#"+id).ajaxSubmit(options);
}

function ErrorDelDo()
{
	alert('Приносим свои извинения, попробуйте позже');
}

function ResponseDelDo(data)
{
	if(data['result']==1)
	{
		alert('Ваше объявление удалено');
		document.location.href= '/my.html';
	} else alert('операция прошла не успешно');
}

/*********************************/
	
/**************SCROLL ANCKOR*****************/
anchor = {
		init : function()  {
			$("a.anchorLink").click(function () {	
				elementClick = $(this).attr("href")
				destination = $(elementClick).offset().top;
				$("html:not(:animated),body:not(:animated)").animate({ scrollTop: destination}, 1100 );
			  	return false;
			})
		}
	}
