$(document).ready( function() {
	// document create
	
	// init GA
	_ga.trackSocial();
	
	// open all href with rel=external in new window
	$("a[rel*='external']").click( function() {
		window.open($(this).attr('href')); 
		return false;
    });

	// main menu
	$('.js_mainmenu').each(function() {
		$(this).click( function() {
			var clickedObjId = 'sm_' + $(this).attr('id'); 
			$('.sub_mainmenu').each(function() {
				if (clickedObjId != $(this).attr('id')) {
					$(this).hide();
					$(this).parent().removeClass('active');
				}
			});
			$('#sm_'+$(this).attr('id')).toggle();
			if ($('#sm_'+$(this).attr('id')).css('display') == 'none') {
				$('#li_'+$(this).attr('id')).removeClass('active');
			} else {
				$('#li_'+$(this).attr('id')).addClass('active');
			}
			return false;
	    });
	});
	//
	$('.js_ret_false').each(function() {
		$(this).click( function() {
			return false;
	    });
	});
	
	$('.js_toggle').each(function() {
		$(this).click( function() {
			$('#block_'+$(this).attr('id')).toggle();
			return false;
	    });
	});
		
	$('.js_currency').change(function() {
		$('#currency_form').submit();
	});
	
	// Tabs
	$('#tabs').tabs();
	// ThickBox
	tb_init('a.thickbox, area.thickbox, input.thickbox');//pass where to apply thickbox
	imgLoader = new Image();// preload image
	imgLoader.src = tb_pathToImage;
	
	// if change some checkbox in condition form, form'll submited
	$('.js_conditions').change( function() {
		$('.js_condition_form').submit();
	});
	// Condition form make URL
	$('.js_condition_form').submit( function() {
		//
		var prevName = '';
		var tmp = [];
		var tmpStr = '';
		var url = '';
		var expr = new RegExp('/', 'ig');
		//var expr2 = new RegExp('\\+', 'ig');
		$('input:checkbox').each( function() {
			//
			if (prevName == '') prevName = $(this).attr('name');
			//
			if ($(this).attr('checked')) { 
				if (prevName != $(this).attr('name')) {
					if (tmp.length > 0) url = url + prevName + '=' + tmp.join(',') + '/'; 
					tmp = [];
					prevName = $(this).attr('name');
					 
				}
				tmpStr = $(this).attr('value');
				tmpStr = tmpStr.replace(expr, '%252F');
				//tmpStr = tmpStr.replace(expr2, '%253F');
				tmp.push(encodeURIComponent(tmpStr));
			}
		});
		if (prevName != '' && tmp.length > 0) url = url + prevName + '=' + tmp.join(',') + '/'; 		
		//
		if ($('#price_min').val() > 0 || $('#price_max').val() > 0) {
			url = url + 'price=' + $('#price_min').val() + ',' + $('#price_max').val() + '/'; 
		}
		//
		var newAction = ($('.js_condition_form').attr('action') + url);
		//$('.js_condition_form').attr('action', newAction);
		//alert(newAction);
		window.location = ''+newAction;
		//
		return false;
	});
	
	// All form than need confirm with checkbox
	$('.js_form_with_confirm').submit(function() {
		if ($('.js_form_confirm_checkbox').attr('checked')) {
			return true;
		} else {
			tb_show('', '/_f/confirm-law/', null);
			return false;
		}
	});
	
	// buy 
	$('.js_buy').each(function() {
		$(this).click( function() {
			//
			$('#form_'+$(this).attr('id')).submit();
			//
			return false;
	    });
	});
	
	// Hide all element that needs be hide
	$('.js_hide').each(function() {
		$(this).hide();
	});
	// CLEditors
	$('.jq_cleditor').each( function() {
		$(this).cleditor({
	    	 width:        ( $(this).width() > 200 ? $(this).width() : '100%' ), // width not including margins, borders or padding
	         height:       ( $(this).height() > 200 ? $(this).height() : 200), // height not including margins, borders or padding
	         controls:     // controls to add to the toolbar
	                       // "bold italic underline strikethrough | bullets numbering | outdent indent " +
	                       //"| alignleft center alignright justify | image link unlink | undo redo ",
	                       "bold italic strikethrough | bullets numbering | image link unlink",
	         docType:      // Document type contained within the editor
	                       '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">'
    	});
    });
    //
    $('.js_popup_messages').each( function() {
    	var addParam = '';
    	if ($(this).attr('width')) {
    		addParam = '?width=' + parseInt($(this).attr('width'));
    	}
    	
    	if ($(this).attr('height')) {
    		addParam = '?height=' + parseInt($(this).attr('height'));
    	}
    	tb_show('', '/_f/'+$(this).attr('value')+'/' + addParam, null);	
	});
	
	// work with select Region
	$('#js_country_id').change( function() {
		// clear
		$('#js_region_id').empty();
		$('#js_city_id').empty();
		$('#js_region_id').append( $('<option value="0">Выберите область</option>'));
		$('#js_city_id').append( $('<option value="0">Выберите населенный пункт</option>'));
		//
		// disabled two selects
		$('#js_region_id').attr('disabled', 'disabled');
		$('#js_city_id').attr('disabled', 'disabled');
		//
		$.getScript('/ajax/region/region/' + $("#js_country_id :selected").val() + '/', function(){
  			//
		});
	});
	
	$('#js_region_id').change( function() {
		// clear
		$('#js_city_id').empty();
		$('#js_city_id').append( $('<option value="0">Выберите населенный пункт</option>'));
		//
		// disabled two selects
		$('#js_city_id').attr('disabled', 'disabled');
		//
		if ($("#js_region_id :selected").val() > 0) {
			$.getScript('/ajax/region/city/' + $("#js_region_id :selected").val() + '/', function(){
	  			//
			});
		}
	});
	
	// are U shure
	$('.jq_are_you_sure').each( function() {  	
		$(this).click( function() {
    		if (confirm("Вы уверены?")) {
			    return true;
			}
			return false;
    	});
	});
	
});


