var reject_options = {  
     reject: { 
         all: false,
         msie5: true,msie6: true
     },  
     display: ['firefox','chrome','msie','safari','opera','gcf'],   
     browserInfo: {
         firefox: {  
             text: 'Firefox 3.5+', // Text below the icon  
             url: 'http://www.mozilla.com/firefox/' // URL For icon/text link  
         },  
         safari: {  
             text: 'Safari 3+',  
             url: 'http://www.apple.com/safari/download/'  
         },  
         opera: {  
             text: 'Opera 9+',  
             url: 'http://www.opera.com/download/'  
         },  
         chrome: {  
             text: 'Chrome 2+',  
             url: 'http://www.google.com/chrome/'  
         },  
         msie: {  
             text: 'Internet Explorer 7+',  
             url: 'http://www.microsoft.com/windows/Internet-explorer/default.aspx'  
         },  
         gcf: {  
             text: 'Google Chrome Frame',  
             url: 'http://code.google.com/chrome/chromeframe/',  
             allow: { all: false, msie: true } // This browser option will only be displayed for MSIE  
         }  
     },  
     header: 'Вы знаете что ваш Интернет браузер устарел?',
     paragraph1: 'Ваш браузер устарел и сайт может работать неправильно в данном браузере.',
     paragraph2: 'Скачать браузеры вы можете по следующим ссылкам:',
     close: true,
     closeMessage: 'Вы можете продолжить работу с сайтом закрыв окно, в этом случае Вы понимаете что сайт может отображаться и работать с ошибками.',   
     closeLink: 'Закрыть окно', 
     closeURL: '#', 
     closeESC: true, 
     closeCookie: false,   
     imagePath: '/img/browsers/', 
     overlayBgColor: '#000', 
     overlayOpacity: 0.8, 
     fadeOutTime: 'fast' 
};


	$(document).ready( function () {
		
		$.reject(reject_options);
		
		if($("#add-work-exp")) {
			$("#add-work-exp").click( function() {
				var addBlock = $("#work-div").html();
				$(".wrap-work-div").append( "<div>" + addBlock + "</div>" );
				
				reAttach();
				
				return false;
			});
			
			$("#add-edu-exp").click( function() {
				var addBlock = $("#edu-div").html();
				$(".wrap-edu-div").append( "<div>" + addBlock + "</div>" );
				
				reAttach();
				
				return false;
			});
			
			$("#add-cou-exp").click( function() {
				var addBlock = $("#cou-div").html();
				$(".wrap-cou-div").append( "<div>" + addBlock + "</div>" );
				
				reAttach();
				
				return false;
			});
			
			function reAttach() {
				$(".delete-work").click(function() {
					$(this).parent().parent().parent().html('');
					
					return false;
				});
				
				$(".delete-edu").click(function() {
					$(this).parent().parent().parent().html('');
					
					return false;
				});
				
				$(".delete-cou").click(function() {
					$(this).parent().parent().parent().html('');
					
					return false;
				});
			}
			
			reAttach();
		}
		
		/* ------ �������� "����� ��������" / "����� �����������" */
		if($("#search-selecta")) {
			$("#search-selecta-vac").click( function() {
				$(this).addClass('active');
				$("#search-selecta-ppl").removeClass('active');
				$("#advanced_search").attr("href", "/find_jobs/#search_form");
				$("#search-field").addClass('search-field-vac');
				$("#search-field")[0].setAttribute('action', '/search_results_jobs/');
				$("#search-listing-type").val('Job');
				$("#search-listing-type-second").val('Job');
				$("#search-field").removeClass('search-field-ppl');
				
				$("#add-resume-button").show();
				$("#add-vacancy-button").hide();
				
				$("#logo span").removeClass("resume");
				$("#logo span").addClass("vacancy");
				
				// для серой плашки с информационным окном (соискатель)
				$("a#window-opener span").html("Почему стоит разместить резюме?!");
				$("#info-for-employer").addClass('hidden');
				$("#info-for-jobseeker").removeClass('hidden');
				return false;
			});
			
			$("#search-selecta-ppl").click( function() {
				$(this).addClass('active');
				$("#search-selecta-vac").removeClass('active');
				$("#advanced_search").attr("href", "/search_resumes/#search_form");
				$("#search-field").addClass('search-field-ppl');
//				$("#search-field").attr('action', '/search_results_resumes');
				$("#search-listing-type").val('Resume');
				$("#search-listing-type-second").val('Resume');
				$("#search-field")[0].setAttribute('action', '/search_results_resumes/');
				$("#search-field").removeClass('search-field-vac');

				$("#add-resume-button").hide();
				$("#add-vacancy-button").show();
				
				$("#logo span").removeClass("vacancy");
				$("#logo span").addClass("resume");
				
				// для серой плашки с информационным окном (работодатель)
				$("a#window-opener span").html("Почему стоит разместить вакансию?!");
				$("#info-for-jobseeker").addClass('hidden');
				$("#info-for-employer").removeClass('hidden');

				return false;
			});
		}
		/* ------------------------------------------------------ */
		if($(".custom-select")) {
			$(".custom-select").bgiframe();
		}

		/* ------ кастом селект бокс */
		function doCountChecks(objct) {
//			return ($(objct).find('input[type="checkbox"]:checked').size());
			return ($(objct).find('input[type="checkbox"]:checked:not(.custom-select-cat > li > input)').size() + $("div.custom-select-cats ul.custom-select-cat>li:has(input:checked):not(:has(li))").size());
			return ($(objct).find('input[type="checkbox"]:checked:not(.custom-select-cat > li > input)').size());
			
		}
		
		function checkCountChecks(checkbox, object, max_counts) {
			var count = doCountChecks(object);
			if((max_counts == 0) || (max_counts >= count)) {
				$(object).parent().find('.custom-select-source').html('Выбрано категорий: ' + count);
				if (count>0)
				{
				  $(object).parent().find('.custom-select-count-source').val(count);
				  $("#OccupationsError").css("display", "none");
				}
				else
				{
				  $(object).parent().find('.custom-select-count-source').val(""); 
				  $("#OccupationsError").css("display", "block");
				}
			} else {
				$(checkbox).removeAttr("checked");
				alert('Не более ' + max_counts + ' категорий!!!');
			}
		}
		$('.custom-select-cat > li ul li input[type="checkbox"]').change( function () {
			if($(this).parent().parent().parent().find(">ul>li>input:checked").size() == 0) {
				$(this).parent().parent().parent().find("> input[type='checkbox']").attr( { checked: "" } );
				$(this).parent().parent().parent().find("> label").css( "font-weight", "normal" );
			} else {
				$(this).parent().parent().parent().find("> input[type='checkbox']").attr( { checked: "checked" } );
				$(this).parent().parent().parent().find("> label").css( "font-weight", "bold" );
			}
			checkCountChecks(this, $(this).parent().parent().parent().parent().parent().parent(), $(this).parent().parent().parent().parent().parent().parent().find('input.max_count_number').val());
		});
/*		
		$('.custom-select-cat > li ul li input[type="checkbox"]').click( function () {
			if($(this).parent().find('input[type="checkbox"]:checked') != $(this).parent().find('input[type="checkbox"]'))
				$(this).parent().parent().parent().find("> input[type='checkbox']").attr( { checked: "" } );
				
			doCountChecks($(this).parent().parent().parent().parent().parent().parent());
		});
*/		

		$(".custom-select-exp").click( function () {
			// make minus
			if( $(this).attr('src') == "/img/tree-plus.png" ) {
				$(this).attr( { src: "/img/tree-minus.png" } );
				
				$(this).parent().find('> ul').show();
			} else { //make plus 
				$(this).attr( { src: "/img/tree-plus.png" } );
				
				$(this).parent().find('> ul').hide();
			}
		});
		
		$(".custom-select-cat > li > input").click( function () {
			if($(this).parent().find('input[type="checkbox"]').size() > 1) {
				var flag = $(this).attr('checked');
				$(this).parent().find('input[type="checkbox"]').attr( { checked: flag } );
				$(this).parent().find('input[type="checkbox"]').trigger('change');
			}	
			checkCountChecks(this, $(this).parent().parent().parent().parent(), 0);
		});
		
		$(".custom-select-header-uncheck").click( function() {
			$(this).parent().parent().find('.custom-select-cats input[type="checkbox"]').attr( { checked: "" } );
			$(this).parent().parent().find("label").css( "font-weight", "normal" );
//			$(this).parent().parent().find('.custom-select-cats input[type="checkbox"]').trigger('change');
			checkCountChecks(this, $(this).parent().parent(), 0);
			if(typeof(clearValue) == 'function') {
			    clearValue();
			}
			return false;
		});
		
		$(".custom-select-source").click( function () {
			$(this).parent().find('.custom-select').toggle();
			checkCountChecks(this, $(this).parent().find('.custom-select'), 0);
		});
		
		$(".custom-select-close-btn").click( function() {
			checkCountChecks(this, $(this).parent().parent(), 0);
			
			$(this).parent().parent().hide();
			return false;
		});
		/* ------------------------------------------------------ */
		$('.searchText').AddXbutton({ img: '/img/x.gif' });
		$('.a-index-search-help-example').click( function() {
			$('#top_search-keywords').focus();
			$('#top_search-keywords').keyup();
			return false;
		});
		$('.b-index-search-help-example').click( function() {
			$('#top_search_region').focus();
			$('#top_search_region').keyup();
			return false;
		});
		
	});

	






///////////////////////////////////////////////////////////////////////////////////////////////////////


	function closeWindow(id) {
		$('#' + id).hide();
		$('#dark_background').hide();
	}
	
	function showWindow(id) {
		var top = $(window).scrollTop();
		$('#dark_background').show();
		$('#' + id).show();
		$('#loader_gif').hide();
//		$('#' + id).css('top', (top + 100) + 'px');
		$('#' + id).css('top', '100px');
		$('#' + id).css('position', 'fixed');
		
		
/*		
		$(window).scroll(function () {
			var top = $(window).scrollTop();
			$('#' + id).stop();
			$('#' + id).css('top', (parseInt(top) + 100) + 'px');
			
			$('#' + id).animate({
			top: (parseInt(top) + 100) + 'px'
			}, 1000);
			
		});	
*/
	}
	
	function showInfoWindow(id) {
		var top = $(window).scrollTop();

		$('#dark_background').show();
		$('#' + id).show();
		$('#loader_gif').hide();
		
		$('#' + id).css('top', '30px');
		$('#' + id).css('position', 'fixed');
	}
	
	function showPrevieWindow(id) {
		var scroll = (document.documentElement && document.documentElement.scrollTop) || (document.body && document.body.scrollTop);
		var h = $.browser.opera? window.innerHeight : $(window).height();
		var clientHeight = document.compatMode=='CSS1Compat' && !window.opera?document.documentElement.clientHeight:document.body.clientHeight;
		document.body.style.overflow = 'hidden';
		$('#dark_background').show();
		$('#' + id).slideDown(500) ;
		$('#loader_gif').hide();
		document.body.style.overflow = '';
		if (navigator.userAgent.toLowerCase().indexOf('chrome') > -1)
			$('#' + id).css('top', -(clientHeight/2 + clientHeight/6));
		else if (navigator.userAgent.toLowerCase().indexOf('msie') > -1)
			$('#' + id).css('top', -(clientHeight/2 + clientHeight/6));	
		else
			$('#' + id).css('top', -(h - 150));
		$('#' + id).css('position', 'relative');	
	}
	
	function openUrlInWindow(id, url) {
		var top = $(window).scrollTop();
		$('#dark_background').show();
		$('#loader_gif').show();
//		$('#' + id).css('top', (top + 100) + 'px');
		$('#' + id).css('top', '100px');
		$('#' + id).css('position', 'fixed');
/*		
		$(window).scroll(function () {
			var top = $(window).scrollTop();
			$('#' + id).stop();
			$('#' + id).css('top', (parseInt(top) + 100) + 'px');
			
			$('#' + id).animate({
			top: (parseInt(top) + 100) + 'px'
			}, 1000);
			
		});
*/
		$.get(url, function(data){
			$('#' + id).html(data);  
			$('#loader_gif').hide();
			$('#' + id).show();
		});
	}



// ch-style
$(document).ready(function(){
    $('.login-close-but').click(
        function() {
            $('#login-form').css({display: 'none'});
        });
});






// autocomplete
var data = ["Aberdeen", "Ada", "Adamsville"];
var data2 = ["����", "������", "���������", "��������������", "������", "�����-���������", "�������", "���-����", "���������", "�����������", "������" ];

/*
$(document).ready(function(){
	if ( $('.suggest1').length )
	{
		$(".suggest1").focus().autocomplete(data, { width: 185 });
	}
});
*/
$(document).ready(function(){
//	if ( $('#search-keywords').length )
	{
		$("#search-keywords").focus().autocomplete(data);
		$("#search-keywords1").focus().autocomplete(data2);
	}
});




// scroll top
function scrollTop ()
{
	return self.pageYOffset || 
    (document.documentElement && document.documentElement.scrollTop) || 
    (document.body && document.body.scrollTop);
}

function delegate( that, thatMethod )
  {
    if(arguments.length > 2)
    {
      var _params = [];
      for(var n = 2; n < arguments.length; ++n) _params.push(arguments[n]);
      return function() { return thatMethod.apply(that,_params); }
    }
    else
      return function() { return thatMethod.call(that); }
  }


// callback function for message
function func_answer (answer)
{
	if (answer == 'yes')
	{
		alert ('yes');
	}
	if (answer == 'no')
	{
		alert ('no');
	}
}

///////////////////////////////////////////////////////////////////////
function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      oldonload();
      func();
    }
  }
}

function prepareInputsForHints() {
  var inputs = document.getElementsByTagName("input");
  for (var i=0; i<inputs.length; i++){
    inputs[i].onfocus = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
    }
    inputs[i].onblur = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
    }
  }
  var selects = document.getElementsByTagName("select");
  for (var k=0; k<selects.length; k++){
    selects[k].onfocus = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "inline";
    }
    selects[k].onblur = function () {
      this.parentNode.getElementsByTagName("span")[0].style.display = "none";
    }
  }
}

// addLoadEvent(prepareInputsForHints);



//расчет суммы по быстрому выбору кол-ва вакансий 
function recalculateAdv( count, price, div ) {
		$('#vac_count_adv').val(count);
		sum = parseInt(count)*parseInt(price);

		calculateDiscountAdv( sum );
		
		($('#regions_adv').hasClass('selected')) ? $('#' + div).html(parseInt(price/100*30)) : $('#' + div).html(price); 
}

function recalculateGuru( count, price, div ) {
	$('#vac_count_guru').val(count);
	sum = parseInt(count)*parseInt(price);

	calculateDiscountGuru( sum );
	
	($('#regions_guru').hasClass('selected')) ? $('#' + div).html(parseInt(price/100*30)) : $('#' + div).html(price); 
}

function calculatePriceAdv() {
		if ( $('#regions_adv').hasClass('selected') ) {
			$('#adv').html('700');
			$('#adv-1').html('630');
			$('#adv-2').html('560');
			$('#adv-3').html('400');
		}
		else {
			$('#adv').html('1000');
			$('#adv-1').html('900');
			$('#adv-2').html('800');
			$('#adv-3').html('500');
		}
}

function calculatePriceGuru() {
	if ( $('#regions_guru').hasClass('selected') ) {
		$('#guru').html('1750');
		$('#guru-1').html('1575');
		$('#guru-2').html('1400');
	}
	else {
		$('#guru').html('2500');
		$('#guru-1').html('2250');
		$('#guru-2').html('2000');
	}
}
	
// расчет скидки (для Остальные регионы)
function calculateDiscountAdv( sum ) {
		var discount = ( $('#regions_adv').hasClass('selected') ) ? 30 : 0; 
		
		$('#discount_adv').val( discount );
		$('#full_sum_adv').val( sum );

		if( $('#regions_adv').hasClass('selected') ) {
			total = parseInt($('#full_sum_adv').val()) - sum/100*30;
		}
		else {
			total = $('#full_sum_adv').val();
		}

		$('#total_sum_adv').val( total );
		if (total) { $('#sum_adv').html( total + ' руб.' ); }
}

function calculateDiscountGuru( sum ) {
	var discount = ( $('#regions_guru').hasClass('selected') ) ? 30 : 0; 
	
	$('#discount_guru').val( discount );
	$('#full_sum_guru').val( sum );

	if( $('#regions_guru').hasClass('selected') ) {
		total = parseInt($('#full_sum_guru').val()) - sum/100*30;
	}
	else {
		total = $('#full_sum_guru').val();
	}

	$('#total_sum_guru').val( total );
	if (total) { $('#sum_guru').html( total + ' руб.' ); }
}

