

function disableAddToCart(elementId, mode, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;
	
	if (mode == "detail")
		$(element).html("<span>" + text + "</span>").toggleClass("disabled")
			.removeAttr("href").unbind('click').css("cursor", "default");
	else if (mode == "list")
		$(element).html(text).removeClass("catalog-item-buy").addClass("catalog-item-in-the-cart")
			.unbind('click').css("cursor", "default");
}

function addToCart(element, imageToFlyId, mode, text)
{
	if (!element || !element.href)
		return;

	var button = $(element);
	if (mode == "detail")
		button.toggleClass("disabled").unbind('click').css("cursor", "default");
	else if (mode == "list")
		button.removeClass("catalog-item-buy").addClass("catalog-item-in-the-cart").unbind('click').css("cursor", "default");

	$.get(
		element.href + "&ajax_buy=1",
		$.proxy(
			function(data) {

				if (this.mode == "detail")
					this.button.removeAttr("href").html("<span>" + text + "</span>");
				else if (this.mode == "list")
					this.button.removeAttr("href").html(text);

				var imageElement = document.getElementById(this.imageToFlyId);
				if (!imageElement)
				{
					$("#cart_line").html(data);
					return;
				}

				var hoverClassName = "";
				var wrapper = null;
				if (this.mode == "detail")
				{
					hoverClassName = "catalog-detail-hover";
					wrapper = this.button.parents("div.catalog-detail");
				}
				else if (this.mode == "list")
				{
					hoverClassName = "catalog-item-hover";
					wrapper = this.button.parents("div.catalog-item");
				}

				wrapper.unbind("mouseover").unbind("mouseout").removeClass(hoverClassName);

				var imageToFly = $(imageElement);
				var position = imageToFly.position();
				var flyImage = imageToFly.clone().insertBefore(imageToFly);

				flyImage.css({ "position": "absolute", "left": position.left, "top": position.top });
				flyImage.animate({ width: 10, height: 10, left: 948, top: -58 }, 500, 'linear');
				flyImage.data("hoverClassName", hoverClassName);
				flyImage.queue($.proxy(function() {

					this.flyImage.remove();
					$("#cart_line").html(data);

					if (this.wrapper.data("adminMode") === true)
					{
						var hoverClassName = "";
						if (this.mode == "detail")
							hoverClassName = "catalog-detail-hover";
						else if (this.mode == "list")
							hoverClassName = "catalog-item-hover";

						this.wrapper.addClass(hoverClassName).bind({
							mouseover: function() { $(this).removeClass(hoverClassName).addClass(hoverClassName); },
							mouseout: function() { $(this).removeClass(hoverClassName); }
						});
					}

				}, {"wrapper" : wrapper, "flyImage" : flyImage, "mode": this.mode}));

			}, { "button": button, "mode": mode, "imageToFlyId" : imageToFlyId }
		)
	);

	return false;
}

function disableAddToCompare(elementId, text)
{
	var	element = document.getElementById(elementId);
	if (!element)
		return;

	$(element)
		.removeClass("catalog-item-compare").addClass("catalog-item-compared")
		.text(text)
		.unbind('click').removeAttr("href")
		.css("cursor", "default");

	return false;
}

function addToCompare(element, text)
{
	if (!element || !element.href) 
		return;
	
	var href = element.href;
	var button = $(element);
	

	button.removeClass("catalog-item-compare").addClass("catalog-item-compared").unbind('click').removeAttr("href").css("cursor", "default");

	$.get(
		href + '&ajax_compare=1&backurl=' + decodeURIComponent(window.location.pathname),
		$.proxy(
			function(data) {

				//var compare = $("#compare");				
				var compare = $("div.compare-boxes");				
				compare.html(data);
				//console.log(data);
				
				this.text(text);				

				/*if (compare.css("display") == "none") {
					compare.css({ "display": "block", "height": "0" });
					compare.animate({ "height": "22px" }, 300);
				}*/
			}, button
		)
	);
	
	return false;
}

function addToCompareSelect(element, text)
{
	if (!element || !element.href) 
		return;
	
	var href = element.href;
	var button = $(element);
	

	button.removeClass("catalog-item-compare").addClass("catalog-item-compared").unbind('click').removeAttr("href").css("cursor", "default");

	$.get(
		href + '&ajax_compare=1&backurl=' + decodeURIComponent(window.location.pathname),
		$.proxy(
			function(data) {

				//var compare = $("#compare");				
				var compare = $("div.compare-boxes");				
				compare.html(data);
				//console.log(data);
				
				this.text(text);				

				/*if (compare.css("display") == "none") {
					compare.css({ "display": "block", "height": "0" });
					compare.animate({ "height": "22px" }, 300);
				}*/
			}, button
		)
	);
	
	return false;
}

function fixPNG(element) {
	if (/MSIE (5\.5|6).+Win/.test(navigator.userAgent)) {
		var src;
		if (element.tagName == 'IMG') {
			if (/\.png$/.test(element.src)) {
				src = element.src;
				element.src = "bitrix/templates/avrora/images/bg/blank.gif";
			}
		}
		else {
			src = element.currentStyle.backgroundImage.match(/url\("(.+\.png)"\)/i);
			if (src) {
				src = src[1];
				element.runtimeStyle.backgroundImage = "none";
			}
		}
		if (src) element.runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + src + "',sizingMethod='scale')";
	}
}

$(document).ready(function(){
	/*$('#mmenu li a.root-item').parent().each(function(){
		var ewidth = $(this).width()-10;
		$('ul',this).css({'width':ewidth+'px','margin-left':'-1px'});
	});*/
	$('img#ilit_main').click(function(){
		if($('#ilikeit div').css('height')==0+'px'){
			$('#ilikeit div').animate({height:'100px',opacity:1},"slow")
		}else{
			$('#ilikeit div').animate({height:'0px',opacity:0},"slow")
		}
	});
	
	$('a#deschref').click(function(e){e.preventDefault();$('div#desc_short').hide();$('div#desc_full').show().parent().css({'height':'auto'})});
	
	
	//$('.add_to_compare a').click(function(e){e.preventDefault();$('div#desc_short').hide();$('div#desc_full').show().parent().css({'height':'auto'})});
	



	$( ".compare_form" ).dialog({
			autoOpen: false,
			/*height: 423,*/
			width: 650,
			modal: true,
			close: function() {
				
			}
		});
		
	$( ".popupcompare" ).dialog({
			autoOpen: false,
			/*height: 430,*/
			width: 660,
			modal: true,
			close: function() {
				
			}
		});
		
	$( "#compare_all_box" ).dialog({
			autoOpen: false,
			/*height: 430,*/
			width: 350,
			modal: true,
			close: function() {
				
			}
		});
		
	$('span.count').click(function(){
		
		$.get(
			'/scripts/compare.manager.php',
			$.proxy(
				function(data) {

					//var compare = $("#compare");				
					var compare = $("#compare_all_box");				
					compare.html(data);
					//console.log(data);
					$("#compare_all_box").dialog( "open" );
					//this.text(text);				

					/*if (compare.css("display") == "none") {
						compare.css({ "display": "block", "height": "0" });
						compare.animate({ "height": "22px" }, 300);
					}*/
				}
			)
		);
		
		return false;
	});

		$( ".add_to_compare a" ).click(function(e) {
				e.preventDefault();
				var id = $(this).attr('data');
				$( "#dialog-form"+id ).dialog( "open" );
			});
			
		$( "a.catalog-item-compare" ).live("click",function(e){
			if($(this).attr('data')){
				var carid = $(this).attr('data');
			}else{
				var carid = '';
			}
			var ids='';
			$('ul#kompl'+carid+' b.checked').each(function(index){
				var komplid = $(this).attr('id').substr(10);
				ids+='id[]='+komplid+'&';
				
			})
			$('ul#popupkomplul b.checked').each(function(index){
				var komplid = $(this).attr('id').substr(11);
				ids+='id[]='+komplid+'&';
				
			})
			$.get(
				'/scripts/compare.handler.php?' + ids + 'action=ADD_TO_COMPARE_LIST',
				$.proxy(
					function(data) {

						//var compare = $("#compare");				
						var compare = $("div.addtext"+carid+', div.popupaddtext');				
						compare.html(data);
						//console.log(data);
						
						//this.text(text);				

						/*if (compare.css("display") == "none") {
							compare.css({ "display": "block", "height": "0" });
							compare.animate({ "height": "22px" }, 300);
						}*/
									$.get(
										'/scripts/header.compare.count.php?pr=Y',
										$.proxy(
											function(data) {

												//var compare = $("#compare");				
												var compare = $("span.count");				
												compare.html(data);
												//console.log(data);
												
												//this.text(text);				

												/*if (compare.css("display") == "none") {
													compare.css({ "display": "block", "height": "0" });
													compare.animate({ "height": "22px" }, 300);
												}*/
											}
										)
									);
					}
				)
			);
			//alert(ids);
			return false;
		});
		
		$( "a.compare-add" ).live("click",function(e){
			if($(this).attr('data')){
				var carid = $(this).attr('data');
			}else{
				var carid = '';
			}
			var ids='';
			$('form.pretty ul b.checked').each(function(index){
				var komplid = $(this).attr('id').substr(10);
				ids+='id[]='+komplid+'&';
				
			})
			$.get(
				'/scripts/compare.handler.new.php?' + ids + 'action=ADD_TO_COMPARE_LIST&ids='+$(this).attr('rel'),
				$.proxy(
					function(data) {

						//var compare = $("#compare");				
						var compare = $("div.compare-box");				
						compare.html(data);
						compare.css({'display':'block'});
						//console.log(data);
						$.get(
							'/scripts/header.compare.count.php?pr=Y',
							$.proxy(
								function(data) {

									//var compare = $("#compare");				
									var compare = $("span.count");				
									compare.html(data);
									//console.log(data);
									
									//this.text(text);				

									/*if (compare.css("display") == "none") {
										compare.css({ "display": "block", "height": "0" });
										compare.animate({ "height": "22px" }, 300);
									}*/
								}
							)
						);
						//this.text(text);				

						/*if (compare.css("display") == "none") {
							compare.css({ "display": "block", "height": "0" });
							compare.animate({ "height": "22px" }, 300);
						}*/
					}
				)
			);
			//alert(ids);
			return false;
		});
		
		//Remove from compare
		$( ".compare-box a.compare-delete-item" ).live("click",function(e){
			if($(this).attr('data')){
				var carid = $(this).attr('data');
			}else{
				var carid = '';
			}
			$.get(
				'/cars/compare/?ajax_compare=1&action=DELETE_FROM_COMPARE_RESULT&ID[]=' + carid,
				$.proxy(
					function(data) {

						//var compare = $("#compare");				
						//var compare = $("div.compare-box");				
						//compare.html(data);
						//compare.css({'display':'hidden'});
						//console.log(data);
						
						//this.text(text);				

						/*if (compare.css("display") == "none") {
							compare.css({ "display": "block", "height": "0" });
							compare.animate({ "height": "22px" }, 300);
						}*/
						
						window.location.reload();
					}
				)
			);
			//alert(ids);
			return false;
		});
		
		//Remove from compare all
		$( ".compare_block a.catalog-clear" ).live("click",function(e){
			if($(this).attr('data')){
				var carid = $(this).attr('data');
			}else{
				var carid = '';
			}
			var ids='';
			$('form.pretty ul b').each(function(index){
				var komplid = $(this).attr('id').substr(10);
				ids+='id[]='+komplid+'&';
				
			})
			$.get(
				'/scripts/compare.handler.new.php?action=DELETE_FROM_COMPARE_LIST&' + ids,
				$.proxy(
					function(data) {

						//var compare = $("#compare");				
						var compare = $("div.compare-box");				
						//compare.html(data);
						compare.css({'display':'none'});
						//console.log(data);
						
						//this.text(text);				

						/*if (compare.css("display") == "none") {
							compare.css({ "display": "block", "height": "0" });
							compare.animate({ "height": "22px" }, 300);
						}*/
						//window.location.reload();
						$.get(
							'/scripts/header.compare.count.php?pr=Y',
							$.proxy(
								function(data) {

									//var compare = $("#compare");				
									var compare = $("span.count");				
									compare.html(data);
									//console.log(data);
									
									//this.text(text);				

									/*if (compare.css("display") == "none") {
										compare.css({ "display": "block", "height": "0" });
										compare.animate({ "height": "22px" }, 300);
									}*/
								}
							)
						);
					}
				)
			);
			//alert(ids);
			return false;
		});
		
		//Remove from compare all 2
		$( "#compare_all_box .compare_block a.catalog-clear" ).live("click",function(e){
			if($(this).attr('data')){
				var carid = $(this).attr('data');
			}else{
				var carid = '';
			}
			var ids='';
			$('#compare_all_box .compared a.compare-delete-item').each(function(index){
				var komplid = $(this).attr('data');
				ids+='id[]='+komplid+'&';
				
			})
			$.get(
				'/scripts/compare.handler.new.php?action=DELETE_FROM_COMPARE_LIST&' + ids,
				$.proxy(
					function(data) {

						//var compare = $("#compare");				
						var compare = $("div.compare-box");				
						//compare.html(data);
						//compare.css({'display':'none'});
						//console.log(data);
						
						//this.text(text);				

						/*if (compare.css("display") == "none") {
							compare.css({ "display": "block", "height": "0" });
							compare.animate({ "height": "22px" }, 300);
						}*/
						//window.location.reload();
						$.get(
							'/scripts/header.compare.count.php?pr=Y',
							$.proxy(
								function(data) {

									//var compare = $("#compare");				
									var compare = $("span.count");				
									compare.html(data);
									//console.log(data);
									$("#compare_all_box").dialog( "close" );
									//this.text(text);				

									/*if (compare.css("display") == "none") {
										compare.css({ "display": "block", "height": "0" });
										compare.animate({ "height": "22px" }, 300);
									}*/
								}
							)
						);
					}
				)
			);

			//alert(ids);
			return false;
		});
		
		$( "a.catalog-compare" ).live("click",function(e){
			var href = $(this).attr('href');
			location=''+href;
			//alert(ids);
			return false;
		});
		
		$( "#popup a.popupcompr" ).live("click",function(e){
			var elem = $(this);
			if($(this).attr('data')){
				var carid = $(this).attr('data');
			}else{
				var carid = '';
			}
			var ids='';
			$('ul#kompl'+carid+' b.checked').each(function(index){
				var komplid = $(this).attr('id').substr(10);
				ids+='id[]='+komplid+'&';
				
			})
			$('ul#popupkomplul b.checked').each(function(index){
				var komplid = $(this).attr('id').substr(11);
				ids+='id[]='+komplid+'&';
				
			})
			$.get(
				'/scripts/compare.handler.php?' + ids + 'action=ADD_TO_COMPARE_LIST',
				$.proxy(
					function(data) {

						//var compare = $("#compare");				
						var compare = $("div.addtext"+carid+', div.popupaddtext');				
						compare.html(data);
						//console.log(data);
						
						//this.text(text);				
						var href = elem.attr('href');
						location=''+href;
						/*if (compare.css("display") == "none") {
							compare.css({ "display": "block", "height": "0" });
							compare.animate({ "height": "22px" }, 300);
						}*/
					}
				)
			);
			//alert(ids);
			return false;
		});
		
	$('div#popup td.brand').click(function(){
		var brand = $(this).attr('data');
		$.get(
			'/scripts/popup.cars.php?brand=' + brand,
			$.proxy(
				function(data) {

					//var compare = $("#compare");				
					var compare = $("div#popup div#model_carousel");				
					compare.html(data);
					var compare = $("div#popup div#popupkompl");
					compare.html(' ');
				}
			)
		);
	});
	
	$('div#popup td.car').live('click',function(){
		var car = $(this).attr('data');
		$.get(
			'/scripts/popup.cars.php?car=' + car,
			$.proxy(
				function(data) {

					//var compare = $("#compare");				
					var compare = $("div#popup div#popupkompl");				
					compare.html(data);

				}
			)
		);
	});
	$('form.allforms').NiceIt();
	$('form.allforms input[name="datetime"]').removeAttr('disabled')
	$('form.allforms input[type=text]').focus(function(){if($(this).val()==$(this).attr('data')){$(this).val(' ').css({'color':'black'});} $(this).css({'color':'black'});})
	$('form.allforms input[type=text]').blur(function(){if($(this).val()==' ' || $(this).val()==''){$(this).val($(this).attr('data')).css({'color':'#CDCDCD'});} })
    // Submitting
	var options = { 
        target:        '#allforms_output',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest,  // pre-submit callback 
        success:       showResponse,  // post-submit callback 
		/*resetForm:true,*/
		error: showResponse,
		type: 'post'
    }; 
 
    // bind to the form's submit event 
    $('form.allforms').live('submit',function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options); 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
	
	function showRequest(formData, jqForm, options) { 
	/*if(jqForm.dialog("isOpen")){
		jqForm.dialog("close");
	}*/
	$.get(
		'/scripts/restart.captcha.php',
		$.proxy(
			function(data) {

				//var compare = $("#compare");				
				$('input[name="captcha_sid"]',jqForm).val(data);
				$('img.code',jqForm).attr('src','/bitrix/tools/captcha2.php?captcha_sid='+data)

			}
		)
	);
	$(options.target).dialog({
			autoOpen: false,
			width: 615,
			modal: true,
			close: function() {
				
			}
			
		});
    return true; 
	} 
	//post submit
	function showResponse(responseText, statusText, xhr, $form)  { 
		$(options.target).dialog("open");
	}
 
 function addHelpDeskAction()
{
	jQuery(document).keyup(function(e)
		{
			// CTRL + Enter
			if (e.ctrlKey && (e.keyCode==10 || e.keyCode == 13))
			// Opera + Shift + Enter
			//if ($.browser.opera && e.shiftKey && (e.keyCode==10 || e.keyCode == 13))
			//if (e.shiftKey && (e.keyCode==10 || e.keyCode == 13))
			{
				var text = xpGetSelectedText();
				var from = location.protocol + "//" + window.location.host + "" + window.location.pathname + "" + window.location.search;
					$.get(
						"/scripts/mistakes.php?text=" + encodeURIComponent(text) + "&from=" + encodeURIComponent(from) + "&name=" + encodeURIComponent($('title').html()),
						$.proxy(
							function(data) {

								var compare = $("#mistakes");				
								compare.html(data)
								compare.dialog( "open" );
							}
						)
					);
			}
		});
}
function xpGetSelectedText()
{
	var t = '';
	if(window.getSelection)
	{
		t = window.getSelection();
	}
	else if(document.getSelection)
	{
		t = document.getSelection();
	}
	else if(document.selection)
	{
		t = document.selection.createRange().text;
	}
	return t;
}
addHelpDeskAction()

	var options1 = { 
        target:        '#allforms_output',   // target element(s) to be updated with server response 
        beforeSubmit:  showRequest1,  // pre-submit callback 
        success:       showResponse1,  // post-submit callback 
		/*resetForm:true,*/
		error: showResponse1,
		type: 'post'
    }; 
	$('#mistakes form').live('submit',function() { 
        // inside event callbacks 'this' is the DOM element so we first 
        // wrap it in a jQuery object and then invoke ajaxSubmit 
        $(this).ajaxSubmit(options1); 
        // !!! Important !!! 
        // always return false to prevent standard browser submit and page navigation 
        return false; 
    }); 
	
	function showRequest1(formData, jqForm, options1) { 
		/*if(jqForm.dialog("isOpen")){
			jqForm.dialog("close");
		}*/
	$.get(
		'/scripts/restart.captcha.php',
		$.proxy(
			function(data) {

				//var compare = $("#compare");				
				$('input[name="captcha_sid"]',jqForm).val(data);
				$('img.code',jqForm).attr('src','/bitrix/tools/captcha2.php?captcha_sid='+data)

			}
		)
	);
	$(options1.target).dialog({
			autoOpen: false,
			width: 615,
			modal: true,
			close: function() {
				
			}
			
		});
    return true; 
	} 
	//post submit
	function showResponse1(responseText, statusText, xhr, $form)  { 
		$(options1.target).dialog("open");
	}	
	
	
	
	
});
