$("#regionsSelectorHat").bind('change', function(){
	window.location = $(this).attr('value');
});

function collapseThis(element){
	
	$(".collapseText").each(function(){
		id = $(this).attr('id').replace('collapseBlock_','');
//		if( $("#collapseBlock_" + id).css('display') != 'block')
			$("#collapseBlock_" + id).slideUp(250, function(){ajustModulesContainers();});
			$("#collapseBlockTitle_" + id).removeClass('collapseTitleBlockActive');
	});
	
	if( $("#collapseBlock_" + element).css('display') != 'block'){
		$("#collapseBlock_" + element).slideDown(250, function(){
			ajustModulesContainers();
			where = $("#collapseBlock_" + element).offset();
			where = where.top - 50;
			$.scrollTo(where, 1000);
		});
		$("#collapseBlockTitle_" + element).addClass('collapseTitleBlockActive');
	}
	
	ajustGrayingElementHeight('loadPanels');
	
}

function ajustModulesContainers(){
	
	if(typeof(admin_view_right)!='undefined'){
		
		var mdls = document.getElementsByClassName("adminPluginContainer");
		
		for(i = 0; i < mdls.length; i++) {
			var mdlId = mdls[i].id;
			mdlId = mdlId.replace('plugin_data_','');
			ajustModuleAdminControls(mdlId);
			
		}
		
	}
	
}

function elementsCreator( type, id, params )
{
	var elt = document.createElement( type );
	
	if( document.getElementById( id ) != 'undefined' );
	
	elt.id = id;
	
	for( param in params )
	{
		elt.setAttribute( param, params[param] );
	}
	
	return elt;
	
}

elementsCreator( 'div' )

function hideAllCollapsableBlocks(){
	
	$(".collapseText").hide();
	
}

function ajustSometimes(){
	l = setTimeout("ajustSometimesDo()", 100);
}

function ajustSometimesDo(){
	ajustModulesContainers();
	ajustSometimes();
}

ajustSometimes()

function ajustGrayingElementHeight(mode){

	if(mode == 'onload'){
		
		if($(window).height() > ( $('#container').height() + 300 ) ){
			$('#graying').height($(window).height());
		}else{
			$('#graying').height( $('#container').height() + 300 );
		}
		
	}
	
	if(mode == 'loadPanels'){
	
		if($(window).height() < ( $('#pageEditWindow').height() + $('#footer').height() + 100)){
		
			$('#graying').height( $('#pageEditWindow').height() + $('#footer').height() + 200);	
		
		}
	}
}

function showLoginForm(){
	showOrHideEditElements('show','small');
	xajax_login('loadWindow',window.location.pathname);
}

function closeEditWindow(){
	$('object').show();
	$('#graying').hide();
	$('#loginWindow').hide();
	$('#editWindowData').hide();
	$("#editWindowLoader").show();
	changeEditWindowClass('small');
	positionEditWindow();
	destroyTinyMces();
	
}

function destroyTinyMces(){
	$("textarea").each(function(){
		element = $(this).attr('id');
		tinyMCE.execCommand('mceRemoveControl', false,element);
	});
	
}

$(window).bind('scroll', function(){
	editWindowHeight = editWindow.height();
	viewportHeight = $(window).height();
	if( viewportHeight > editWindowHeight ){
		positionEditWindow();
	}
});

function positionEditWindow(){
	
	editWindow = $('#editWindow');
	
	editWindowHeight = editWindow.height();
	viewportHeight = $(window).height();
	
	
	r = $(document).scrollTop();
	
	if(viewportHeight < editWindowHeight)
		offset = 10;
		else
		offset = ((viewportHeight - editWindowHeight)/2);
	
	editWindow.css({'margin-top': r + offset + 'px'});
	
}

function changeEditWindowClass(activeClass){
	
	var editWindow = $('#editWindow');
	var editWindowData = $('#editWindowData');
	var classes = new Array('small','medium','large','bigger','full');
	
	$.each(classes, function(currentClass){
		if(classes[currentClass] == activeClass){
			editWindow.addClass(activeClass + '-edit-window');
		}else{
			editWindow.removeClass(classes[currentClass] + '-edit-window');
		}
	});
	
}

function showOrHideEditElements(action,windowSize){
	
	var graying = $('#graying');
	var editWindow = $('#editWindow');
	var editWindowLoader = $('#editWindowLoader');
	var editWindowData = $('#editWindowData');
	
	if(action == 'show'){
		changeEditWindowClass(windowSize);
		$("#graying").show();
		$("#editWindow").show();
		$('object').hide();
		positionEditWindow();
	}
	if(action == 'loader'){
		changeEditWindowClass('small');
		editWindowLoader.show();
		editWindowData.hide();
		editWindowData.innerHTML="";
	}
	if(action == 'hide'){
		graying.hide();
		editWindow.hide();
		editWindowLoader.show();
		editWindowData.hide();
		editWindowData.innerHTML="";
	}
	
	ajustGrayingElementHeight('onload');
	
}

function showEditWindowData(){
	$('#editWindowData').show();
}
ajustModulesContainers();
function doOnLoad(){
	xajax_events('generateCalendar', '1,2012','next');
	var graying = $('#graying');
	var editWindow = $('#editWindow');
	var editWindowLoader = $('#editWindowLoader');
	var editWindowData = $('#editWindowData');
	
	ajustGrayingElementHeight('onload'); 
	hideAllCollapsableBlocks();
	positionEditWindow();
	ajustModulesContainers();
	
	// Bind hot keys
	$(document).bind('keydown', 'esc',function(){ closeEditWindow()});
	$(document).bind('keydown', 'Ctrl+l',function(){ showLoginForm(); });
	
	/*
	var blockHeight = $("#m3").height();
	var blockOffset = $("#m3").offset();
	
	$(document).bind('scroll', function(){
		
		if( $(document).height() > 2000 ){
			var windowOffset = $(window).scrollTop();
			if( ( windowOffset + 20 ) > blockOffset.top ){
				value = windowOffset - blockOffset.top;
				$("#m3").css({'padding-top':value + 20 + 'px'});
			}else{
				$("#m3").css({'padding-top':'0px'});
			}
		}
		
	});
	*/
}

$(window).resize(function(){
	positionEditWindow();
	ajustGrayingElementHeight('onload');
});

function initAgenciesEditorList()
{
	$('#agenciesList').sortable({'containment':'.graying',
															 'update':function()
															 {
															 	xajax_agenciesEditor('reorder',$('#agenciesList').sortable('serialize'),agenciescurcitie,'');
															 }});
}
