
base_url='';

ua = navigator.userAgent.toLowerCase(); 
isIE = ((ua.indexOf("msie") != -1) );
isIE6 = ((ua.indexOf("msie 6.0") != -1) );
isIE7 = ((ua.indexOf("msie 7.0") != -1) );
isOPERA = ((ua.indexOf("opera") != -1) );
isFF = ((ua.indexOf("firefox") != -1) );
isSafari = ((ua.indexOf("safari") != -1) );

$(document).ready(function(){

if(isIE6)
  {
  DD_belatedPNG.fix('img, #top h1.logo');  
  }   
  
init_regionselect();   

});

//-----------------------------------------------
function debug(txt)
{
try {
	console.debug(txt);
	}
catch(e){}
}
//-----------------------------------------------
function inputclickhandler(formid)
{
var mainform=('#'+formid);

if( $(mainform).length == 0 ) return;

$(mainform).find('input[type=text][title]').bind('focus', function(event)
  {    
  var title=$(this).attr('title');  
  if( $(this).val() == title ) $(this).val('');
  });
  
$(mainform).find('input[type=text][title]').bind('blur', function(event)
  {    
  var title=$(this).attr('title');  
  if( $(this).val() == '' ) $(this).val(title) ;
  });   
}
//-----------------------------------------------
function urlopen(url,blank)
{
if(blank==null) blank=false;

if(!blank)
	window.location.href=url;
else
	window.open(url);
}
//-----------------------------------------------
function Uploadfileobj(id, options)
{
var defaults = 
	{
	upload_url	: '',
	delimg_url	: '',
	flash_url	: 'assets/tools/swfupload/swfupload.swf',
	button_image_url : 'assets/images/icons/ico_file_upload-32.png',
	file_types : "*.jpg;*.jpeg;*.gif;*.png", 
	file_size_limit : "2 MB",
	session_id : '',
	buttontext : 'Tallózás...',	
	delimg : 'Valóban törli a képet',
	afterupload : function() {},
	afterdelete : function() {}
	};	

var opts = $.extend({}, defaults, options);
	
var mainelement=$('#'+id);
var swfu;
var self=this;

$(mainelement).find('.upload').click(function(event)
	{
	$(mainelement).find('.post_params :input').each( function()
		{
		swfu.addPostParam( $(this).attr('name'), $(this).val() );	
		});
		
	swfu.startUpload();	
	});
	
$(mainelement).find('.delimg').click(function(event)
	{
	deleteimage(this);
	});	

function deleteimage(sender)
{
if(confirm(opts.delimg))
	{
	$(mainelement).find('.imgformmsg').html('');	
	var data=$(mainelement).find('.post_params').serializeAnything('string');
	
	var imgbox=$(sender).parents('.imgbox');
	
	if( $(imgbox).find('input[name=imgid]').length>0 )
		{
		data+='&imgid='+$(imgbox).find('input[name=imgid]').val();			
		}
					
	$.ajax({
		type: 'POST',
		url: opts.delimg_url,
		dataType: "json",
		data: data,
		success: function(result)
			{
			opts.afterdelete(result, mainelement)
			}
		});
	}
}

this.deleteimage=deleteimage;
	
var settings={
	upload_url : opts.upload_url, 
	flash_url : opts.flash_url,
	
	post_params : { 'session' : opts.session_id }, 
	 	
	file_queue_limit : 1,
	file_size_limit : opts.file_size_limit,
	file_types : opts.file_types, 
	
	debug : false, 
	button_placeholder_id : "file_upload_placeholder",
	button_image_url : opts.button_image_url, 
	button_width : 120, 
	button_height : 32, 
	button_text_left_padding : 35, 
	button_text_top_padding : 5,
	button_text : "<span class=\"btn\">"+opts.buttontext+"</span>",
	button_text_style : ".btn { font-size: 15px; font-family: Verdana; }", 
	
	file_dialog_start_handler	: file_dialog_start,
	file_queue_error_handler		: file_queue_error,
	file_dialog_complete_handler	: file_dialog_complete,
	upload_start_handler : upload_start, 
	upload_progress_handler : upload_progress,
	upload_complete_handler : upload_complete,
	upload_success_handler : upload_success
}

swfu = new SWFUpload(settings); 

//$(mainelement).find('.progressbar').progressbar({value: 0});
$(mainelement).find('.progressbar').html('');

//Uploader Events

//---------------------------------------------------------
function file_dialog_start()
{
var stats=swfu.getStats();

//debug(stats);

var allfilesnmr=0;
for(idx in stats) allfilesnmr+=stats[idx]; 

for(var i=0;i<allfilesnmr;i++)
	{
	filedata=swfu.getFile(i);	
	swfu.cancelUpload(filedata.id);	
	//debug(filedata);
	}

$(mainelement).find('.filename').val('');	
}
//---------------------------------------------------------
function file_dialog_complete(selected_num, files_num, total_num)
{
var stats=swfu.getStats();

//debug(stats);
if( stats.files_queued==0 ) return;

var allfilesnmr=0;
for(idx in stats) allfilesnmr+=stats[idx]; 

for(var i=0;i<allfilesnmr;i++)
	{
	filedata=swfu.getFile(i);	
	if(filedata.filestatus!=SWFUpload.FILE_STATUS.QUEUED) continue; 	
	var sizestring=CalcFilesize(filedata.size);
	$(mainelement).find('.filename').val(filedata.name+' ('+sizestring+')');
	}	

//swfu.startUpload();
}
//---------------------------------------------------------
function file_queue_error(file, errorCode, message)
{
try {
	// Handle this error separately because we don't want to create a FileProgress element for it.
	switch (errorCode) {
	case SWFUpload.QUEUE_ERROR.QUEUE_LIMIT_EXCEEDED:
		alert("You have attempted to queue too many files.\n" + (message === 0 ? "You have reached the upload limit." : "You may select " + (message > 1 ? "up to " + message + " files." : "one file.")));
		return;
	case SWFUpload.QUEUE_ERROR.FILE_EXCEEDS_SIZE_LIMIT:
		alert("The file you selected is too big. "+file.name);
		this.debug("Error Code: File too big, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
		return;
	case SWFUpload.QUEUE_ERROR.ZERO_BYTE_FILE:
		alert("The file you selected is empty.  Please select another file.");
		this.debug("Error Code: Zero byte file, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
		return;
	case SWFUpload.QUEUE_ERROR.INVALID_FILETYPE:
		alert("The file you choose is not an allowed file type.");
		this.debug("Error Code: Invalid File Type, File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
		return;
	default:
		alert("An error occurred in the upload. Try again later.");
		this.debug("Error Code: " + errorCode + ", File name: " + file.name + ", File size: " + file.size + ", Message: " + message);
		return;
	}
} catch (e) {	}

}
//---------------------------------------------------------
function upload_start(file)
{
$(mainelement).find('.imgformmsg').html('');	
}
//---------------------------------------------------------
function upload_progress(file, bytesLoaded, bytesTotal) 
{

try {
	var percent = Math.ceil((bytesLoaded / bytesTotal) * 100);

	//$(mainelement).find('.progressbar').progressbar('option', 'value', percent);
	$(mainelement).find('.progressbar').html(percent+'%');

	} catch (e) { }
}
//---------------------------------------------------------
function upload_complete(file)
{

}
//---------------------------------------------------------
function upload_success(file, result, received_response)
{
var stats=swfu.getStats();

if (stats.files_queued!=0) return;

try
	{
	if (typeof (JSON) !== 'undefined' && 	typeof (JSON.parse) === 'function') result=JSON.parse(result);
	else result= eval('('+ result + ')' );
	}
catch(e) { debug(e); }


if( result.success==1 )	
	{
	$(mainelement).find('.progressbar').html('');
	$(mainelement).find('.imgformmsg').html( result.msg );
	$(mainelement).find('.filename').val('');	
	
	//ha több kép is van akkor az egész lista csere
	if( $(mainelement).find('.imglist').length>0 )
		{
		$(mainelement).find('.imglist').html(result.data.html);
		}
	else
		{
		$(mainelement).find('.delimg').css('display', 'inline');				
		$(mainelement).find('img.preview').attr('src', result.data.filesrc );			
		}
		
	opts.afterupload(result, mainelement);
	}
}
//---------------------------------------------------------
function CalcFilesize(size)
{
if(size < 1024*1024 ) return Math.round(size/1024)+'kb';
return (size/(1024*1024) ).toFixed(2)+'Mb';
}
//---------------------------------------------------------
this.destroy=function()
{
return swfu.destroy();
delete self;
}


//END;
}
//-----------------------------------------------
function initofficelist(id, options)
{
var defaults = 
	{
	confirmdelete : 'Delete?'
	};	

opts = $.extend({}, defaults, options);
	
var mainelement=$('#'+id);
var action=$(mainelement).attr('action');

$(mainelement).find('input[name=checkall]').bind('click', function()
	{
	var checked=$(this).attr('checked');
	
	if(checked)
      $(mainelement).find('.officetable tbody .td1 input').attr('checked', 'checked');
    else
      $(mainelement).find('.officetable tbody .td1 input').removeAttr('checked');
	});
	

$(mainelement).find('.functionpanel .defbutton.delete').bind('click', function()
	{
	if( $(mainelement).find('.officetable tbody .td1 input:checked').length>0 )
		{		
		if( confirm(opts.confirmdelete) )
			refresh('delete');
		}	
	});
		
		
$(mainelement).find('.defbutton.gotopage').bind('click', function()
	{	
	refresh();	
	});
				
		
$(mainelement).find('.navpage a[rel]').bind('click', function(event)
	{
	event.preventDefault();	
	var rel=$(this).attr('rel');		
	
	$(mainelement).find('input[name=gotopage]').val(rel);
	
	refresh();	
	});	
	
$(mainelement).find('table.officetable thead a[rel]').bind('click', function(event)
	{
	event.preventDefault();	
	var rel=$(this).attr('rel');	
	
	$(mainelement).find('input[name=sortname]').val(rel);
		
	refresh();
	});		
	
function refresh(option)
{
var data=$(mainelement).serialize();

if(option!=null) data+='&option='+option;
	
$.ajax({
	type: 'POST',
	url: action,
	dataType: "html",
	data: data,
	success: function(result)
		{
		$('#right').html(result);
		}
	});		
	
}

this.refresh=refresh;

//END;
}
//-----------------------------------------------
function initresultlist(id, options)
{
var defaults = 
	{
	target : 'body'
	};	

opts = $.extend({}, defaults, options);
	
var mainelement=$('#'+id);
var action=$(mainelement).attr('action');

$(mainelement).find('.defbutton.gotopage').bind('click', function()
	{	
	refresh();	
	});

$(mainelement).find('select[name=itemperpage]').bind('change', function(event)
	{
	refresh();	
	});	

$(mainelement).find('.navpage a[rel]').bind('click', function(event)
	{
	event.preventDefault();	
	//var rel=$(this).attr('rel');		
	
	//$(mainelement).find('input[name=gotopage]').val(rel);
	
	action=$(this).attr('href');
	
	refresh();	
	});	

function refresh(option)
{
var data=$(mainelement).serialize();

if(option!=null) data+='&option='+option;
	
$.ajax({
	type: 'POST',
	url: action,
	dataType: "html",
	data: data,
	success: function(result)
		{
		$(opts.target).html(result);
		}
	});		
	
}


//END;
}
//-----------------------------------------------
function init_geographydataform(id)
{
var mainelement=$('#'+id);


$(mainelement).find('.education_box a.close').click(function(event)
	{
	event.preventDefault();			
	$(this).parents('.education_box').remove();
	});
	
var box=$(mainelement).find('.education_box:first');	



$(box).find('a.close').css('display', 'none');


$(mainelement).find('.addmore').click(function(event)
	{
	event.preventDefault();	
	
	var boxcnt=$(mainelement).find('.education_box').length;

	
	var clone=$(mainelement).find('.education_box:first').clone(true);	
	$(clone).find('a.close').css('display', 'block');
	$(clone).removeClass('error');
	$(clone).removeClass('field_institution_0');
	
	$(clone).addClass('field_institution_'+boxcnt);
	
	$(clone).find(':input[name^=institution_name]').attr('name', 'institution_name['+boxcnt+']');
	$(clone).find(':input[name^=institution_type]').attr('name', 'institution_type['+boxcnt+']');
	$(clone).find(':input[name^=course]').attr('name', 'course['+boxcnt+']');
	$(clone).find(':input[name^=students]').attr('name', 'students['+boxcnt+']');
	$(clone).find(':input[name^=settlement]').attr('name', 'settlement['+boxcnt+']');
	
	$(clone).find('input').val('');
	$(clone).find('select').val(0);
	
	$(mainelement).find('.education_box:last').after(clone);	
	});



//END;
}
//-----------------------------------------------
function printopen(loadobject)
{
var prwin=window.open('', 'printwindow');
var prwindoc=prwin.document;

var fullhtml=$('<div>').append($(loadobject).clone()).remove().html();

//debug(fullhtml);

prwindoc.open();
prwindoc.write( '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> <head> 	<title>Print</title> <style type="text/css" title="currentStyle" media="all"> 	@import "'+base_url+'assets/css/printwindow.css"; 	</style> </head> <body> <div class="printcontent">'+fullhtml+'</div> </body> </html>');
prwindoc.close();

prwin.print();
}
//-----------------------------------------------
function init_regionselect()
{
$('.region_select select').bind('change', function()
	{
	var val=$(this).val();		
	if(val==0) return;			
	urlopen(val);
	});	
	
//END;
}
