//СОБЫТИЯ
$(function(){
	$.fn.uitabs = $.fn.tabs;
	delete $.fn.tabs;
	
	/**
	* jQuery Cookie plugin
	*
	* Copyright (c) 2010 Klaus Hartl (stilbuero.de)
	* Dual licensed under the MIT and GPL licenses:
	* http://www.opensource.org/licenses/mit-license.php
	* http://www.gnu.org/licenses/gpl.html
	*
	*/
	$.cookie = function (key, value, options) {

		// key and at least value given, set cookie...
		if (arguments.length > 1 && String(value) !== "[object Object]") {
			options = jQuery.extend({}, options);

			if (value === null || value === undefined) {
				options.expires = -1;
			}

			if (typeof options.expires === 'number') {
				var days = options.expires, t = options.expires = new Date();
				t.setDate(t.getDate() + days);
			}

			value = String(value);

			return (document.cookie = [
				encodeURIComponent(key), '=',
				options.raw ? value : encodeURIComponent(value),
				options.expires ? '; expires=' + options.expires.toUTCString() : '', // use expires attribute, max-age is not supported by IE
				options.path ? '; path=' + options.path : '',
				options.domain ? '; domain=' + options.domain : '',
				options.secure ? '; secure' : ''
			].join(''));
		}

		// key and possibly options given, get cookie...
		options = value || {};
		var result, decode = options.raw ? function (s) { return s; } : decodeURIComponent;
		return (result = new RegExp('(?:^|; )' + encodeURIComponent(key) + '=([^;]*)').exec(document.cookie)) ? decode(result[1]) : null;
	};
	
	$.datepicker.setDefaults({
		firstDay: 1,
		dateFormat: 'yy-mm-dd',
		dayNames: ['Воскресенье', 'Понедельник', 'Вторник', 'Среда', 'Четверг', 'Пятница', 'Суббота'],
		dayNamesMin: ['Вс', 'Пн', 'Вт', 'Ср', 'Чт', 'Пт', 'Сб'],
		monthNames: ['Январь', 'Февраль', 'Март', 'Апрель', 'Май', 'Июнь', 'Июль', 'Август', 'Сентябрь', 'Октябрь', 'Ноябрь', 'Декабрь'],
		duration: 'fast',
		changeYear: true
		
	});
	
	// FEEDBACK
	$("#feedback").fancybox({
		'scrolling'  : 'no',
		'titleShow'  : false
	});
	
	$('.fancy').fancybox();
	
	//SUBSCRIBE
	$('#subscribe-add').click(function(){
		var vars = {
			ru : {
				errors : {
					emptyMail : 'Введите e-mail',
					wrongMail : 'E-mail некорректен'
				},
				loading : 'Загрузка',
				enter : 'Подписаться'
			},
			en : {
				errors : {
					emptyMail : 'Enter e-mail',
					wrongMail : 'E-mail is incorrect'
				},
				loading : 'Loading',
				enter : 'Subscribe'
			}
		};
		var that = this;
		var el = $('#subscribe-mail');
		var val = el.val();
		var msg = [];
		if( val=='' || val==el.attr('title') ) msg.push(vars[_LANG_].errors.emptyMail);
		else if( !val.match(/^[\d\w\.-]+@([\d\w-]+)((\.[\w\d-]+)+)?\.\w{2,6}$/) ) msg.push(vars[_LANG_].errors.wrongMail);
		
		if(msg.length>0){
			alert(msg.join("\n"));
			return false;
		}
		$(this).text(vars[_LANG_].loading).attr('disabled', 'disabled');
		$.get(ajaxFile, {go:'subscribe_add', mail:val}, function(html){
			$(that).text(vars[_LANG_].enter).removeAttr('disabled');
			el.val(el.attr('title'));
			alert(html);
		});
	});
	
	if (window.PIE) {
        $('.pie').each(function() {
            PIE.attach(this);
        });
    }
    $("#anyClass1").jCarouselLite({
        btnNext: ".jnext",
        btnPrev: ".jprev",
		speed: 400,
		visible: 1
    });	
	// select #flowplanes and make it scrollable. use circular and navigator plugins
	$("#flowpanes").scrollable({ circular: true }).navigator({
		navi: "#flowtabs",
		naviItem: 'a',
		activeClass: 'active',
		history: true
	});
	
	Cufon.replace('.tng', {
		fontFamily: 'Tenge'
	});
	Cufon.replace('.counter_case span', {
		fontFamily: 'Arial Black',
		color: '-linear-gradient(#8abf9a, #3c9949, #03484c, #03484c)'
	});
	/* Вот тут последние скрипты */
	Cufon.replace('.counter_works span', {
		fontFamily: 'Arial Black',
		color: '-linear-gradient(#8abf9a, #3c9949, #03484c, #03484c)'
	});	
	Cufon.replace('.support_date font span', {
		fontFamily: 'Arial Black'
	});
    $(".solution_box .img_port span img").click(function(){
        window.location=$(this).parent().parent().parent().find("a.img_port").attr("href"); return false;
    });
	
	$('#adress_change').live('click',function(){
		var country=$(this).attr('href').substr(1);
		$.post('/ajax.php',{go: 'getadress',c: country},function(data){
			if($.trim(data)!=''){
				$('.info_block .address').html(data);
			}
		});
		return false;
	})
	
	/*$('.db_link').click(function(){
		$('.type_list').hide();
		$('.db_link').removeClass("on");
		$(this).addClass("on");
		$($(this).attr("href")).show();
		return false;
	});*/
	
	/*var sliderx = $('#slider_img').bxSlider({
		pager: true,
		controls: true,
		speed: 1000,
		mode: 'fade'
	});
	$('#gonext').click(function(){
		sliderx.goToNextSlide();
		return false;
	});*/

	/*$('.jcarouse .jprev').live('mouseup',function(){
		var curObj=$('.desc_port .active');
		var liObjs=$('.desc_port ul li');
		var fId=liObjs[0].id;
		$(this).attr('onclick','return false;').delay(500).removeAttr('onclick');
		curObj.css('display','none');
		curObj.removeClass('active');
		var prevEl=curObj.prev();
		if(curObj.attr('id')==fId){
			prevEl=$('.desc_port > ul > li:last');
		}
		prevEl.css('display','block').addClass('active');
	});
	$('.jcarouse .jnext').live('mouseup',function(){
		var curObj=$('.desc_port .active');
		var liObjs=$('.desc_port ul li');
		var lastIndx=Number(liObjs.length)-1;
		var lastId=liObjs[lastIndx].id;
		$(this).attr('onclick','return false;').delay(500).removeAttr('onclick');
		curObj.css('display','none');
		curObj.removeClass('active');
		var nextEl=curObj.next();
		if(curObj.attr('id')==lastId){
			nextEl=$('.desc_port > ul > li:first');
		}
		nextEl.css('display','block').addClass('active');
	});*/
});

$(function(){
	$('.needClear')
		 .focus(function(){
			  if(this.value==this.title) this.value='';
		 })
		 .blur(function(){
			  if(this.value=='') this.value=this.title;
		 });
});

function checkSearchForm(f){
	var msg = [];
	
	if(f['what'].value=='поиск' || f['what'].value=='') msg.push('Введите слово для поиска');
	else if(f['what'].value.length<3) msg.push('Слово для поиска должно быть длиннее 3-х символов');
	
	if(msg.length>0){
		alert(msg.join("\n"));
		return false;
	}
	return true;
}

function voteIt(btn, voting_id, answer_id){
	var btn = $(btn).attr({disabled:'disabled'});
	$.get(ajaxFile, {vote:voting_id, a_id:answer_id}, function(html){
		$('#voting-backup').html( $('#voting-screen').html() );
		$('#voting-screen').html( html );
		btn.removeAttr('disabled');
		if(answer_id==false) $('#back-to-vote').show();
	});
	return false;
}

function showVoting(){
	$('#voting-screen').html( $('#voting-backup').html() );
	$('#back-to-vote').hide();
	return false;
}

function newCaptcha(img){
	var randDig=(Math.random()*100000)+1|0;
	img.attr('src', '/cms/files/appends/kcaptcha/index.php?rand='+randDig);
	return false;
}

// Проверка E-MAIL
function EmailCheck(value)
{ 
  var re = /^\w+([\.-]?\w+)*@(((([a-z0-9]{2,})|([a-z0-9][-][a-z0-9]+))[\.][a-z0-9])|([a-z0-9]+[-]?))+[a-z0-9]+\.([a-z]{2}|(com|net|org|edu|int|mil|gov|arpa|biz|aero|name|coop|info|pro|museum))$/i; 

  if(re.test(value))
  {
    return true; 
  } else { 
    return false; 
  } 
}

function domainCheck(value)
{ 
  var re = /^[a-zA-Z\-_0-9а-яА-я]+\.[a-zA-Z]{2,5}$/i; 

  if(re.test(value))
  {
    return true; 
  } else { 
    return false; 
  } 
}
