var httpRequest = window.location.href;
var hsp = httpRequest.split("?");
var hcall = hsp[1];
var hgets = hsp[0].split("/");

if (typeof getPreReady == 'function')
  getPreReady();
$(document).ready(function (){
	if (typeof getPostReady == 'function')
	  getPostReady();
	if (!skipajax) {
	  $.address.change(function(event) {
		if (typeof getAddressChanged == 'function')
		  getAddressChanged();
		  httpRequest = window.location.href;
		  ajaxRequest = event.value;
		  
		  if (ajaxRequest == '/') {
			ajaxRequest = false;
		  }
		  
		  ajaxinit = true;
		  if (!ajaxRequest) {
			if (get_url) {
			  location.href = '#' + get_url;
			  $('html').empty();
			}
			else if(_gaq) {
			   //_gaq.push('_trackPageview',mainURL);
			}
	
			  if (typeof checkNewLayout == 'function')
				checkNewLayout($('div.layout').attr("ids"));
			mainURL = get_url;
		  }
		  else if (lastURL != ajaxRequest) {
			hajax = ajaxRequest.split("?");
			call = hajax[1];
			gets = hajax[0].split("/");
					
			getAjaxPrepare(gets);
			
			if (typeof getPreChangeLoad == 'function')
			  getPreChangeLoad();
			$.ajax({
			  url: ajaxRequest,
			  dataType: "json",
			  success: function( objResponse ){
				if (is_object(objResponse)) {
				  mainURL = ajaxRequest;
				  if (!mainURL)
					lastURL = ajaxRequest;
				  else
					lastURL = mainURL;
					
				  get_lparams['sec'] = objResponse['lsec'];
				  get_lparams['subsec'] = objResponse['lsubsec'];
				  get_lparams['cont'] = objResponse['lcont'];
				  get_params['sec'] = objResponse['sec'];
				  get_params['subsec'] = objResponse['subsec'];
				  get_params['cont'] = objResponse['cont'];
				  get_params['lastsec'] = objResponse['lastsec'];
				  
				  for (var il=0;il<objResponse['langs'].length;il++) {
					  $('a.lang_'+objResponse['langs'][il]).attr("href",objResponse['hrefs']['href_'+objResponse['langs'][il]]);
				  }
				  
				  getAjaxLoaded();
				  
	
					if (!get_params['sec'])
						get_params['sec'] = '';
						
					if (!get_params['subsec'])
						get_params['subsec'] = '';
						
					if (!get_params['cont'])
						get_params['cont'] = '';
						
					$('body').attr('class',"sec_"+get_params['sec']+" subsec_"+get_params['sec']+"_"+get_params['subsec']+" cont_"+get_params['sec']+"_"+get_params['subsec']+"_"+get_params['cont']);
				  sectionContainer.html(objResponse['content']);
				  bodyContainer.css({'visibility':''});
				  subsectionContainer=$('#main_subsection');
				  contentContainer=$('#main_content');
				  
				  if (objResponse['hooks'] != null) {
					  for (var hout in objResponse['hooks']) {
						  $('#'+hout).html(objResponse['hooks'][hout]);
					  }
				  }
				  
				  sectionContainer.removeClass('lastsec');
				  subsectionContainer.removeClass('lastsec');
				  contentContainer.removeClass('lastsec');
				  switch (get_params['lastsec']) {
					  case 'sec':
					  	sectionContainer.addClass('lastsec');
					  break;
					  case 'subsec':
					  	subsectionContainer.addClass('lastsec');
					  break;
					  case 'cont':
					  	contentContainer.addClass('lastsec');
					  break;
				  }
	
				  if (objResponse['js'] != '')
					jsContainer.html(objResponse['js']);
					
				  if (objResponse['title'] != '')
					$.address.title(objResponse['title']);
					
				  getAjaxDone();
				  
				  if (FB)
				  	FB.XFBML.parse(document.getElementById('body'));
				  
				  if (typeof updateInlineAdmin == 'function')
					  updateInlineAdmin();
					  
				  if (typeof getChangeLoaded == 'function')
					  getChangeLoaded();
				  
				  if (typeof checkNewLayout == 'function')
				  	checkNewLayout(objResponse['idslayout']);
					
				  _gaq.push(['_trackPageview',mainURL]);
				}
				else
				  getAjaxFailed();
			  },
			  error: function( objRequest ){
				getAjaxFailed();
			  }
			});
		  }
	  });
	}
});

function loadAlert(status,title,msg,submit) {
  $.alerts.dialogClass = status;
  jAlert(msg, title, submit, function() {
    $.alerts.dialogClass = null;
  });
}
function getX() {
  if( typeof( window.pageYOffset ) == 'number' ) {
    scrOfY = window.pageYOffset;
    scrOfX = window.pageXOffset;
  } else if( document.body && ( document.body.scrollLeft || document.body.scrollTop ) ) {
    scrOfY = document.body.scrollTop;
    scrOfX = document.body.scrollLeft;
  } else if( document.documentElement && ( document.documentElement.scrollLeft || document.documentElement.scrollTop ) ) {
    scrOfY = document.documentElement.scrollTop;
    scrOfX = document.documentElement.scrollLeft;
  }

  if( typeof( window.innerWidth ) == 'number' ) {
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }
}
function center2screen(source) {
  getX();
  h = source.height();
  w = source.width();
  if (h<=myHeight) {
    moveh = scrOfY + (myHeight - h)/2;
  }
  else {
    moveh = scrOfY + 20;
  }
  movex = (myWidth - w) / 2;
  source.css({"top":moveh,"left":movex});
}
function is_array(input){
  return typeof(input)=='object'&&(input instanceof Array);
}
function is_object (mixed_var){
    if (mixed_var instanceof Array) {return false;} else {return (mixed_var !== null) && (typeof( mixed_var ) == 'object');}
}
