var nowSection = null;


var preloader = {
	
	show : function () {
		//pageState('index');
	},
	
	hide : function () {
		//pageState('second');
	}

}

var LoaderData = function(section, params, isPost) {

	var callback = function(data, status){

		if (data == null) {
			//pageState('index');
			return;
		}
		else {

			//Google
			//pageTracker._trackPageview(_root + section);
			$('#ajax').html(data);
			$('#ajax form').jqTransform();
			
			lightboxInit();
			bindCalculator();
			reinitLinks('ajax');
			preloader.hide();
			document.title = $(".html_title").text();
		}
		
		
		nowSection = section;
		return;
		
	}

	//if (!section) {
//		callback(null);
//		return;
//	}

	if (!$.browser.msie) {
		preloader.show();
	}

	if (!params) {
		params = {};
	}
	params['ajax'] = 1;

	if(isPost) {
		$.post(section, params, callback);
	}
	else {
		$.get(section, params, callback);
	}

	return false;
	
}



function GetContent(section, params, isPost) {

	if (params || isPost) {
		LoaderData(section, params, isPost);
	}
	else {
		
		section = section.split(_root);
		section = section[1];
		if(!section) {
			section = '#';
		}
		historyPage.GoTo(section);
		return false;
	}
}


function reinitLinks(idBody) {

	setEventsFormElement( $('body')[0] );
	
	$('#' + idBody + ' A').each(
		function() {

			var regExp = {
				doc: new RegExp('.*\.doc$', 'i'),
				xls: new RegExp('.*\.xls$', 'i'),
				jpg: new RegExp('.*\.jpg$', 'i'),
				gif: new RegExp('.*\.gif$', 'i'),
				png: new RegExp('.*\.png$', 'i')
			};
			
			for (var item in regExp) {
				if(regExp[item].test($(this).attr('href'))) {
					return;
				}
			}
			
			if ($(this).attr('target') == '_blank' || $(this).is('.no_ajax') || $(this).parents('.jqtransformdone').length) {
				return;		
			}
			
			$(this).click(function() {
				GetContent(this.href);
				return false;
			});
		}
	);
	
	$('form').each(function () {

		if ($(this).attr('onsubmit') || $(this).is('.no_ajax')) {
			return;
		}
		else {
			$(this).attr('onsubmit', 'return PostForm(this)');
		}
		
	});
}

function submit_search(link_search, text) {

	GetContent(link_search, {searchtext: text});
	$('.inputbox').val(text);
	
	return false;

}

function PostForm(form, onlyGet) {

	var json = {};

	$('input', form).each(function() {

		if (($(this).attr('type') == 'radio' || $(this).attr('type') == 'checkbox') && $(this).attr('checked')) {
			json[$(this).attr('name')] = $(this).val();
		}
		else if (($(this).attr('type') == 'radio' && $(this).attr('type') == 'checkbox')) {
			json[$(this).attr('name')] = $(this).val();
		}
		else if (($(this).attr('type') != 'radio' && $(this).attr('type') != 'checkbox')) {
			json[$(this).attr('name')] = $(this).val();
		}

	});
	
	$('textarea', form).each(function() {
		json[$(this).attr('name')] = $(this).val();
	});
	
	$('button', form).each(function() {
		json[$(this).attr('name')] = $(this).text();
	});
	
	$('select', form).each(function() {
		json[$(this).attr('name')] = $(this).val();
	});
	
	
	GetContent(nowSection, json, onlyGet ? false : true);
	
	return false;
}

function PostDelivery(f) {
	
	if (f.email.value && f.charset.value) {
			w=window.open('', 'feedback','height=160,width=220,maximize=0,minimize=0,center=1,resizable=0,status=0,top=200,left=300');
			w.focus();
			f.action = nowSection;
	}
	else{
		alert('Пожалуйста, заполните адрес электронной почты');
		return false;
	}

	return true;
	
}


/**
 * Функция для фикса бага с формами в IE6
 * при перезагрузки флэшки-пустышки пропадает фокус из поля
 */
var setEventsFormElement = function ( parent ) {
	
	if ( browser.isIE && navigator.appVersion.match('6') == 6 ) {
		
		var array = new Array();
		
		var funcFocus = function ( ) {
			
			historyPage.StopDisplatch();

		}
		
		var funcBlur = function ( ) {
			
			historyPage.StartDispatch();
			
		}
		
		// назначаем обработчик для input type="text"
		var element = parent.getElementsByTagName('input');
		
		for ( var i=0, length=element.length; i<length; i++ ) {
			
			if ( element[i].type == 'text' ) {
				
				element[i].onfocus = funcFocus;
				element[i].onblur = funcBlur;
				
			}
			
		}
		
		// назначаем обработчик для textarea
		var element = parent.getElementsByTagName('textarea');
		for ( var i=0, length=element.length; i<length; i++ ) {
			
			element[i].onfocus = funcFocus;
			element[i].onblur = funcBlur;

		}
		
	}
	
}


