//-------------------------------------------------------------------
// COMMON
//-------------------------------------------------------------------
function externalLinks() {
	$('a.external').attr('target', '_blank');
}
//-------------------------------------------------------------------
function isset(varname)  {
    if(typeof( window[ varname ] ) != "undefined") return true;
    else return false;
}
//-------------------------------------------------------------------
// FORM
//-------------------------------------------------------------------
function initSearch() {
	$('#searchbar').submit(function(){
		if ($('#searchbar').validationEngine('validate', {promptPosition : 'bottomLeft'})) {
			return true;
		}
		return false;
	});
}
//-------------------------------------------------------------------
// VALIDATION
//-------------------------------------------------------------------
function hideValidationPrompts() {
	if ($('#popup_form_help').length) $('#popup_form_help').validationEngine('hide');
	if ($('#popup_form_order').length) $('#popup_form_order').validationEngine('hide');
	if ($('#searchbar').length) $('#searchbar').validationEngine('hide');
}
//-------------------------------------------------------------------
// POPUP
//-------------------------------------------------------------------
function initPopup() {
	if ($('#popup_form_help').length) {
		$('#popup_form_help').submit(function(){

			if ($('#popup_form_order').length) $('#popup_form_order').validationEngine('hide');
			if ($('#searchbar').length) $('#searchbar').validationEngine('hide');

			if ($('#popup_form_help').validationEngine('validate', {promptPosition : 'bottomLeft'})) {
				popupHandler('help');
			}
			return false;
		});
		
		$('#bg-popup, .closewinpop, .closewinpop-text').click(function() {
			$('#popup_text').val('Текст');
			hideValidationPrompts();
		});
	}

	if ($('#popup_form_order').length) {
		$('#popup_form_order').submit(function(){
			
			if ($('#popup_form_help').length) $('#popup_form_help').validationEngine('hide');
			if ($('#searchbar').length) $('#searchbar').validationEngine('hide');

			if ($('#popup_form_order').validationEngine('validate', {promptPosition : 'bottomLeft'})) {
				popupHandler('order');
			}
			return false;
		});
		
		$('#bg-popup, .closewinpop, .closewinpop-text').click(function() {
			hideValidationPrompts();
		});
	}
}
//-------------------------------------------------------------------
function popupHandler(type) {
	if (!type) return false;
	var target = '#popup_form_' + type;
	var options = {
		target: target,
		dataType: 'json',
		beforeSubmit: checkIt,
		success: takeIt,
		timeout: 1000,
		url: '/popup/' + type,
		cache: false,
	};
	$(target).ajaxSubmit(options);
}
//-------------------------------------------------------------------
function checkIt(formData, jqForm, options) {
	return true; 
}
//-------------------------------------------------------------------
function takeIt(data, statusText) {
	if (data){
		if (data.type == 'ok') {
			$.jGrowl('close');
			$('#popup_form_help').validationEngine('hide');
			$('#bg-popup').fadeOut(500);
			$('#popup_text').val('Текст');
			$('.isPopUp').fadeOut(500);
			$.jGrowl('Ваше сообщение принято');
		} else {
			$.jGrowl('close');
			$('#popup_form_help').validationEngine('hide');
			if (data.context) 
				$.jGrowl(data.context, {'themeState' : 'error'});
			else 
				$.jGrowl('Ваше сообщение не отправлено', {'themeState' : 'error'});
		}
	}
}
//-------------------------------------------------------------------
// AUTOCOMPLETE
//-------------------------------------------------------------------
function initAutoSearch() {
	if ($('#search_value').length) {
		var ac = $('#search_value').autocomplete({
			serviceUrl: '/robot/search',
			minChars: 1,
			delimiter: /(,|;)\s*/,
			maxHeight: 400,
			width: 511,
			zIndex: 9999,
			deferRequestBy: 300,
			params: {},
			onSelect: function(data, value){ }
		});
		ac.enable();
	}
}
//-------------------------------------------------------------------
// CAPTCHA
//-------------------------------------------------------------------
function generateCaptcha() {
	$('#captchaSource').attr('src', '/captcha?' + microtime() + randomize(1000));
}
//-------------------------------------------------------------------
function microtime() {
	var now = new Date().getTime() / 1000;
	var s = parseInt(now);
	return (Math.round((now - s) * 1000) / 1000) + ' ' + s;
}
//-------------------------------------------------------------------
function randomize(n) {
	return Math.floor(Math.random() * (n + 1));
}
//-------------------------------------------------------------------
if (jQuery){
	$(document).ready(function(){
		initAutoSearch();
		externalLinks();
		initSearch();
		initPopup();
	});
}
//-------------------------------------------------------------------
