var CBS = function() {

	var ua = navigator.userAgent.toLowerCase(); // забираем полную строку с данными
	// функция возвращает позицию заданного параметра или -1 если не найден
	// потребуется в будущем для проверки на существование параметров в строке с данными получеными выше
	var is = function(t){
	return ua.indexOf(t) > -1;
	};
	
	// просто проверка - включена ли поддержка куки в браузера или нет
	var cookie = navigator.cookieEnabled ? 'iscookie' : 'nocookie';
	
	// движки браузеров
	var g = 'gecko'; // FF
	
	var w = 'webkit'; // Chrome
	
	var s = 'safari'; // Safari
	
	var h = document.getElementsByTagName('html')[0];
	
	var b = [(!(/opera|webtv/i.test(ua))&&/msie\s(\d)/.test(ua))?('ie ie'+RegExp.$1 + (is('trident')&&is('msie 7.0') ? ' compat':'')):is('firefox/2')?g+' ff2':is('firefox/3')?g+' ff3':is('firefox/4')?g+' ff5':is('firefox/5')?g+' ff5':is('gecko/')?g:/opera(\s|\/)(\d+)/.test(ua)?'opera opera'+RegExp.$2:is('konqueror')?'konqueror':is('chrome')?w+' '+s+' chrome':is('applewebkit/')?w+' '+s+(/version\/(\d+)/.test(ua)?' '+s+RegExp.$1:''):is('mozilla/')?g:'',is('j2me')?'mobile':is('iphone')?'iphone':is('ipod')?'ipod':is('mac')?'mac':is('darwin')?'mac':is('webtv')?'webtv':is('win')?'win'+(is('windows nt 5.1')?' xp':(is('windows nt 5.1')?' vista':(is('windows nt 6.1')?' win7':''))):is('freebsd')?'freebsd':(is('x11')||is('linux'))?'linux':'','js'];
	
	c = b.join(' ')+' '+cookie;
	
	h.className += ' ' + c;
	
	return c;
};
CBS()

var timeout    = 500;
var closetimer = 0;
var ddmenuitem = 0;

/*
function jsddm_open(){  
	jsddm_canceltimer();
	jsddm_close();
	//ddmenuitem = $(this).find('ul').show()
	$(this).addClass('hover')
	//.css('visibility', 'visible');
}
*/

/*
function jsddm_close(){  
		$('.menu li').removeClass('hover')
	if(ddmenuitem){
		ddmenuitem.hide()
		//$('.menu li').removeClass('hover')
	}
	//ddmenuitem.css('visibility', 'hidden');
}
*/

/*
function jsddm_timer(){
	closetimer = window.setTimeout(jsddm_close, timeout);
}
function jsddm_canceltimer(){
	if(closetimer){  
		window.clearTimeout(closetimer);
	  	closetimer = null;
	}
}
*/

$.preloadImages = function(){
   for(var i = 0; i<arguments.length; i++){
      $("<img />").attr("src", arguments[i]);
   }
}

/* this.tooltip = function(){ */
	/* CONFIG */		
			
	/* END CONFIG */		
/*
	$("a.tooltip").mouseover(function(e){	
		this.t = $(this).text();
		this.href =  $(this).attr('href')
		$("body").append("<div id='tooltip'><div class='wr1'><div class='wr2'><div class='wr3'><div class='wr4'><a href=\""+this.href+"\">"+ this.t +"</a></div></div></div></div></div>");
		$("#tooltip")
			.css("top",($(this).offset().top*1-5) + "px")
			.css("left",($(this).offset().left*1-8) + "px")
			.fadeIn("fast");
			$('#tooltip').mouseout(function(){$('#tooltip').remove()})
    })
*/
	/*function(){
		$("#tooltip").remove();
    };	*/		
/* }; */

$(function(){
	$('.head .menu li').bind('mouseenter', function(){
		if($(this).find('ul').length)
			$(this).find('ul').fadeIn('fast');
	})
	$('.head .menu li').bind('mouseleave', function(){
		if($(this).find('ul').length)
			$(this).find('ul').fadeOut('fast');
	})
/*
	$('.head .menu>ul>li:has(ul)').bind('mouseover', jsddm_open)
	$('.head .menu>ul>li').bind('mouseout', jsddm_timer)
	document.onclick = jsddm_close;		   
*/
	
/*
	if($('a.tooltip').length>0){
		tooltip();
	}
*/
	// 
	if($('img.add_hover').length>0){
		$.each($('img.add_hover'),function(i,n){
			src = $(n).attr('src').split('.')
			$.preloadImages(src[0]+'_hover.'+src[1]);								   
		})
		$('img.add_hover').hover(function(){
			src = $(this).attr('src').split('.')
			$(this).attr('src',src[0]+'_hover.'+src[1])	
		},function(){
			src = $(this).attr('src').replace(/_hover/,'')
		
			$(this).attr('src',src)
		})
	}
	
	$('select.replace').rSelect()
	
	if($('form.placeholder').size() > 0){
    	$('form.placeholder input[type="text"], form.placeholder textarea').focus(function(){
    		if(!$(this).val()){
    			$(this).parent().find('label[for="'+$(this).attr('id')+'"]').fadeOut('fast');
    		}
    	}).blur(function(){
    		if (!$(this).val()){
				$(this).parent().find('label[for="'+$(this).attr('id')+'"]').fadeIn('fast');
    		}else{
				$(this).parent().find('label[for="'+$(this).attr('id')+'"]').hide()
			}
		});
		$('form.placeholder input[type="text"]').trigger('blur')
	}
	
	if($('.citate p').length>0){
		$('.citate p').append('<span class="vline"></span><span class="vline_over"></span>')
	}
	if($('.vacancies_list .item').length>0){
		$('.vacancies_list .item .title a').click(function(){
			$this = $(this)
			$this.toggleClass('active')
			$this.parent().parent().parent().find('.text').slideToggle()
			return false;
		})
	}
	
	if($('.personal_list .item,.tentders_list .item').length>0){
		$('.item .foot>a').click(function(){
			$this = $(this)
			$this.toggleClass('active')
			if($.browser.msie){
				if($this.parent().find('.text').css('display')!='block')
					$this.parent().find('.text').show();
				else
					$this.parent().find('.text').hide();
			}
			else{
				$this.parent().find('.text').slideToggle()
			}
			return false;
		})
	}
	
	$('.citate p .data').wrap('<span class="data_wrap"></span>')
	
	if($('.arhive').length>0){
		$('.arhive>li>a').click(function(){
			$('.arhive>li>a.active').parent().find('ul').slideToggle()
			$('.arhive>li>a.active').removeClass('active')
			
			
			if(!$(this).hasClass('active')){
				$(this).parent().find('ul').slideToggle()
				$(this).addClass('active')
			}
			return false;
		})
	}
	
	if($('.index_partners').length>0){
		$('.index_partners img').css({opacity:0.5}).hover(function(){
				$(this).css({opacity:1})
		},function(){
				$(this).css({opacity:0.5})
		})
	
	}
	if($('.history_list').length>0){
		$.each($('.history_list .data'),function(i,n){
			h = $(n).height()
			$(n).css({'margin-top':-1*h/2+'px'})
	   })
	}
	if($('.partners_list').length>0){
		$('.partners_list a.img img').css({opacity:0.5})
		$('.partners_list a.img ').click(function(){
			$('.partners_list a.active').parent().find('.text').hide();
			$('.partners_list a.active img').css({opacity:0.5})
			$('.partners_list a.active').removeClass('active')
			
			
			if(!$(this).hasClass('active')){
				$(this).parent().find('.text').show()
				$(this).find('img').css({opacity:1})
				$(this).addClass('active')
			}
			return false
		}).hover(function(){
			if(!$(this).hasClass('active')){
				$(this).find('img').css({opacity:1})
			}
		},function(){
			if(!$(this).hasClass('active')){
				$(this).find('img').css({opacity:0.5})
			}
		})
	
	}
	if($('.magazines').length>0){
		$('.magazines .item').hover(function(){
			$(this).find('.text_wrap').show()									 
		},function(){
			$(this).find('.text_wrap').hide()									 
		})
	
	}
	
})

jQuery.fn.rSelect = function() {
	return this.each(function(){
		$this = $(this);
		
		var opt = $this.find('option');
		var selected = $this.find('option:selected')
		
//		$this.hide();
		var selectBox = $('<a>', {html: '<span>'+selected.html()+'</span>', 'class': 'custom_select'});
		
		if ($this.css('width') == '100%')
			selectBox.css({width: '100%'})

		
		
		var dbList = '';
		
		opt.each(function(i,n){
			dbList += '<a>'+$(this).html()+'</a>';
			if ($(this).val()=='def' && i==0){dbList='';}
		})
		
		var dropBox = $('<div>', {html: '<div>'+dbList+'</div>', 'class': 'select_dropdown'});
		
		dropBox.mouseleave(function(){
			$(this).find('div').slideUp('fast',function(){dropBox.hide(); $(this).show()});
		});
		
		dropBox.data({sb: $this});
		
		var bdLinks = dropBox.find('a');
		
		bdLinks.click(function(){
			dropBox.data().sb.find('option:selected').removeAttr('selected');
			
			selectBox.find('span').html($(this).html())
			
			dropBox.data().sb.find('option:eq('+bdLinks.index(this)+')').attr('selected', true);
			
			$(this).parent().slideUp('fast',function(){dropBox.hide(); $(this).show()});
		});
		
		selectBox.data({sb: $this, db: dropBox});
		
		selectBox.click(function(){
			$this = $(this);
			
			var db = $this.data().db;
			
			db.show().css({width: 'auto'});
			
			db.css({top: '0%'});

			if (db.width() < $this.outerWidth()){
				//alert()
				db.css({width: $this.outerWidth()+'px'})
				}
			
			db.css({top: $this.offset().top+'px', left: $this.offset().left+'px', paddingTop: $this.height()+'px'}).find('div').hide().slideDown('fast');
		})
		
		$this.hide();
		selectBox.insertAfter($this);
		dropBox.appendTo('body');
    });
}
	
	
	function reloadTenders(url){
		$.ajax({
			url: url,
			success: function( data ) {
				$('#reloadTenders').fadeOut('fast').html(data).fadeIn();
			}
		});	
	}
	
	$(window).ready(function(){
		
		// подключаем лайтбок для всех ссылок у которых rel="lightbox"
		if($('a[rel*=lightbox]').size()>0)
			$('a[rel*=lightbox]').lightBox();
		
		if($('a.tooltip').size()>0){
			$('a.tooltip').bind('mouseenter', function(){
				this.t = $(this).text();
				this.href =  $(this).attr('href')
				$("body").append("<div id='tooltip' class='leave_tooltip'><div class='wr1'><div class='wr2'><div class='wr3'><div class='wr4'><a href=\""+this.href+"\">"+ this.t +"</a></div></div></div></div></div>");
				$("#tooltip")
					.css("top",($(this).offset().top*1-5) + "px")
					.css("left",($(this).offset().left*1-8) + "px")
					.fadeIn("fast");
			});
			$('#tooltip').live('mouseleave',function(){
				$('.leave_tooltip').remove();
			});
		}
		
		$('#show_sites').bind('click', function(){
			$('#site_list').fadeIn('fast');
		});
		$('#site_list').bind('mouseleave', function(){
			$(this).hide();
		})
	});
