/*
 *	ver = 1.1.0
 */

var my_pc_init = {
	forum_topic_id:null,
	cache:[],
	preload_images: function() {
		//my_pc_init.cache = [];
//		alert(typeof my_pc_init.cache);
		var args_len = arguments.length;
		for (var i = args_len; i--;) {
			var cacheImage = document.createElement('img');
			cacheImage.src = arguments[i];
			my_pc_init.cache.push(cacheImage);
		}
	},
	img_preload: function( ){
		// preload images
		my_pc_init.preload_images(
			"/img/ico_logout__h.png",
			"/img/btn_bg_b_c.png",
			"/img/btn_bg_b_l.png",
			"/img/btn_bg_b_r.png",
			"/img/btn_bg_b_ra.png",
			"/img/btn_bg_sb_l.png",
			"/img/btn_bg_sb_c.png",
			"/img/btn_bg_lb_r.png",
			"/img/btn_bg_sb_ra.png",
			"/img/btn_bg_sb_rau.png",
			"/img/btn_bg_sb_rad.png",
			"/img/btn_b_pf.png",
			"/img/btn_b_pl.png",
			"/img/btn_b_pn.png",
			"/img/btn_b_pp.png",
			"/img/btn_ssb_show.png",
			"/img/btn_ssb_hide.png",
			"/css/custom-theme/images/ui-icons_056b93_256x240.png",
			"/css/custom-theme/images/ui-bg_highlight-soft_30_848585_1x100.png"
		);
		
	},
	upload_progress: function( ){
		
		// Dialog
		$('#progressbar_dialog').dialog({
			title: 'Send e-mail to author',
			autoOpen: false,
			width: 350,
			modal: true
		});
		
		// progress
		$("#progressbar").progressbar({
			value: 0
		});
        
        // file
        $('#posterupload').change( function(){
//            $('#posterupload_em').val( $(this).val() );
        });
        $('#posterupload_em, #posterupload_emb').bind('click', function(){
//            $('#posterupload_em').val('');
            $('#posterupload').click();
        });
		
	},
	mail_to_author: function( ){
		
		// Dialog
		$('#mail_to_author_dialog').dialog({
			title: 'Send e-mail to author',
			autoOpen: false,
			width: 760,
			modal: true
		});

		// SENDING
		$('#info_dialog').dialog({
			title: 'Information',
			autoOpen: false,
			width: 300,
			modal: true
		});

		$("#sendForm input, #sendForm textarea").keyup( function(){
			$(this).closest('.field').removeClass('wrong_field');
			if( $("#sendForm .wrong_field").length == 0 )
				$('#error_infobox').slideUp();
			if( $(this).val().length == 0)
				$(this).closest('.field').addClass('empty_field');
			else
				$(this).closest('.field').removeClass('empty_field');
		});
		$('#sendForm input, #sendForm textarea').filter(function() { return $(this).val() == ""; }).closest('.field').addClass('empty_field');

		return false;
	},
	lost_password: function( ){
		
		// Dialog
		$('#lost_password_dialog').dialog({
			title: 'Lost password',
			autoOpen: false,
			width: 280,
			modal: true
		});
		// SENDING
		$('#info_dialog').dialog({
			title: 'Information',
			autoOpen: false,
			width: 300,
			modal: true
		});
		
		return false;
	},
	mail_to_cip: function( ){
		
		// Dialog
		$('#mail_to_cip_dialog').dialog({
			title: 'Mail to Elsevier',
			autoOpen: false,
			width: 760,
			modal: true
		});
		
		// Form
		$("#sendToCIPForm input, #sendToCIPForm textarea").keyup( function(){
			$(this).closest('.field').removeClass('wrong_field');
			if( $("#sendToCIPForm .wrong_field").length == 0 )
				$('#error_infobox').slideUp();
			if( $(this).val().length == 0)
				$(this).closest('.field').addClass('empty_field');
			else
				$(this).closest('.field').removeClass('empty_field');
		});
		$('#sendToCIPForm input, #sendToCIPForm textarea').filter(function() { return $(this).val() == ""; }).closest('.field').addClass('empty_field');
		
		// SENDING
		$('#info_dialog').dialog({
			title: 'Information',
			autoOpen: false,
			width: 300,
			modal: true
		});
		
		return false;
	},
	show_abstracts: function( ){
		
		// Dialog
		$('#show_abstracts').dialog({
			title: 'Abstract',
			autoOpen: false,
			width: 760,
			modal: true
		});
		// Dialog Graphics
		$('#show_abstracts_graphics').dialog({
			title: 'Graphics',
			autoOpen: false,
			width: 760,
			modal: true
		});
	},
	show_abstracts_21: function( ){
		
		// Dialog
		$('#show_abstracts_graphics').dialog({
			title: 'Graphics',
			autoOpen: false,
			width: 760,
			modal: true
		});
	}
}

var my_pc_app = {
	tmp:true,
	send_begin_at:null,
	upload_file: function( form_id ){

	if( $("#upl [name='poster']").val() != '' ){
		$("#upl").ajaxSubmit({
			dataType:'json',
			beforeSubmit: function(arr, form, options) {
                $("#upl, #progressbar_close_btn, #progressbar_success_text, #progressbar_error_infobox").hide();
                $("#progressbar_process_text").show();
				$('#progressbar_dialog').dialog("open");
				$("#progressbar").everyTime(1000, function(){
					$.post( '/poster/getUploadStatus/'+$('#progress_id').val(), 
						function(data){
							if( data.error){
							} else {
										$("#progressbar").progressbar({ value: data.percent });
							}
						}, "json");
				});
				
				return true;
			},
			success: function( response) { 
				$("#progressbar").stopTime();
                $("#progressbar_process_text").hide();
                if( response.error){
                    $("#progressbar_error_infobox .error_infobox_text").html(response.errorText);
                    $("#upl, #progressbar_error_infobox").show();
                } else {
                    $("#progressbar").progressbar({ value: 100 });
                    $("#poster_already_exist").hide();
                    $("#poster_submited, #progressbar_success_text").show();
                    $("#view_poster_button").removeClass("hide");
                    
                    $("#info_infobox .iifu_filename").html(response.pstUploadedFilename);
                    $("#info_infobox .iifu_filesize").html(response.pstUploadedFilesize);
                    $("#info_infobox .iifu_filedate").html(response.pstUploadedDate);
                    $("#info_infobox .iifu_approved").html( (response.pstApproved ? 'Yes' : 'No') );
                }
                    $("#progressbar_close_btn").show();
				//$('#progressbar_dialog').dialog("close");
				//$("#poster_submited").slideDown();
			}
		}); 
	} 



/*		$.post( $("#"+form_id).attr("action"), $("#"+form_id).serialize(), 
			function(data){
				my_pc_app.upload_file_complete(data);
			}, "json");*/
	},
	upload_file_complete: function( ){
	},
	dialog_mail_to_cip_open: function( ){
		$('#mail_to_cip_dialog').dialog("open");
	},
	
	dialog_mail_to_cip_sending: function( ){
		my_pc_app.form_in_dialog_sender( "mail_to_cip_dialog", "sendToCIPForm");
	},
	
	dialog_lost_password_open: function( ){
		$('.error_infobox').hide();
		$('#lostpassform input[name="email"]').val( $('#loginform input[name="email"]').val() );
		$('#lost_password_dialog').dialog("open");
	},
	dialog_lost_password_sending: function( ){
		
		my_pc_app.form_in_dialog_sender( "lost_password_dialog", "lostpassform");
		
	},
	form_in_dialog_sender: function( dialog_id, form_id ){
		my_pc_app.form_in_dialog_id = dialog_id;
		
		// check empty fields
		if( $('#'+form_id+' input, #'+form_id+' textarea').filter(function() { return $(this).val() == ""; }).length > 0 ){
			$('#'+form_id+' input, #'+form_id+' textarea').filter(function() { return $(this).val() == ""; }).closest('.field').addClass('wrong_field');
			$('#error_infobox .error_infobox_text').html('Message could not be sent. All fields must be filled.');
			$('#error_infobox').slideDown();
			return;
		}
		
		// send ajax request
		my_pc_app.send_begin_at = new Date().getTime() + 2000 ;
		$.post( $("#"+form_id).attr("action"), $("#"+form_id).serialize(), 
			function(data){
				my_pc_app.form_in_dialog_result_check("result",data);
			}, "json");
			
		var ad = $('#'+dialog_id).closest('.ui-dialog');
		var adp = ad.offset();
		$('#sendinig_waiter').width(ad.outerWidth()).height(ad.outerHeight()).css('left',adp.left+'px').css('top',adp.top+'px').fadeIn();
		
	},
	form_in_dialog_result_check: function( action, data){
		if( action == "result" ){
			my_pc_app.form_in_dialog_result = data;
			
			var delay_left = 0 - ( new Date().getTime() - my_pc_app.send_begin_at );
			if( delay_left > 0 )
				$("#sendinig_waiter").oneTime( delay_left, function() {
					$(this).fadeOut(function() {
						my_pc_app.form_in_dialog_result_check("analyze");
					});
				});
			else
				my_pc_app.form_in_dialog_result_check("analyze");
				
		} else if( action == "analyze"){
			
			if( typeof my_pc_app.form_in_dialog_result.error == 'undefined' ){
				$('#error_infobox').html('Unknown error.').slideDown();
				
			} else if( my_pc_app.form_in_dialog_result.error == 0 ){
				$('#'+my_pc_app.form_in_dialog_id).dialog( "close" );
				$('#info_dialog .info_text').html(my_pc_app.form_in_dialog_result.errorText);
				$('#info_dialog').dialog("open");
				
			} else if( my_pc_app.form_in_dialog_result.error == 1 ) {
				$('#error_infobox .error_infobox_text').html( my_pc_app.form_in_dialog_result.errorText );
				$('#error_infobox').slideDown();
				$("#"+my_pc_app.form_in_dialog_result.errorCode).closest('.field').addClass('wrong_field');
				
			}
			
		}
	},
	toggle_poster_list: function( con_id){
		$("[id^=con_][id!=con_"+con_id+"]").removeClass('active');
		$("[id^=con_pl_][id!=con_pl_"+con_id+"]").addClass('hide');
		
		$("#con_"+con_id).toggleClass('active');
		$("#con_pl_"+con_id).toggleClass('hide');
	},
	toggle_block: function( block_id ){
		if( $('#'+block_id).css('display') == 'block' )
		{
			$('#'+block_id+'_t').addClass('small_button_rad').find('.c').html('Show');
			$('#'+block_id).css('display','none');
		} else {
			$('#'+block_id+'_t').removeClass('small_button_rad').find('.c').html('Hide');
			$('#'+block_id).css('display','block');
		}
	},
	dialog_mail_to_author_recaptcha: function( ){
		var timestamp = new Date().getTime();
		$('#sf_captcha').html('<img width=200 height=45 src="/index.php/image/securimage/'+timestamp+'">');
/*		$('#sf_captcha').empty();
		var ca = document.createElement('img');
		ca.src = "/index.php/image/securimage/"+timestamp;
		document.getElementById('sf_captcha').appendChild(ca);*/
		$("#code").val('').closest('.field').addClass('empty_field');
	},
	dialog_mail_to_author_open: function( pstId, pstNum ){
		$('#sendForm .reempty').val('').closest('.field').addClass('empty_field');
		$(".field").removeClass('wrong_field');
		$('#error_infobox').hide();
		// ids
		$('#pstId').val(pstId);
		$('#toName').html($('#poster_'+pstNum+' .autName').html());
		// captcha
		my_pc_app.dialog_mail_to_author_recaptcha();
		$('#mail_to_author_dialog').dialog("open");
		
		return false;
	},
	dialog_info_close: function( id ){
		$('#info_dialog').dialog("close");
		return false;
	},
	dialog_mail_to_author_result_check: function(action,data){
		if( action == "result" ){
			my_pc_app.send_result = data;
			
			var delay_left = 0 - ( new Date().getTime() - my_pc_app.send_begin_at );
			if( delay_left > 0 )
				$("#sendinig_waiter").oneTime( delay_left, function() {
					$(this).fadeOut(function() {
						my_pc_app.dialog_mail_to_author_result_check("analyze");
					});
				});
			else
				my_pc_app.dialog_mail_to_author_result_check("analyze");
				
		} else if( action == "analyze"){
			
			if( typeof my_pc_app.send_result.error == 'undefined' ){
				$('#error_infobox').html('Unknown error.').slideDown();
			} else if( my_pc_app.send_result.error == 0 ){
				$('#mail_to_author_dialog').dialog("close");
				$('#info_dialog').dialog("open");
				
			} else if( my_pc_app.send_result.error == 1 ) {
				if( my_pc_app.send_result.errorCode == 'email'){
					$('#error_infobox').html('Incorrect e-mail address. Please reenter e-mail address.<br />Don`t forget enter Security code.').slideDown();
					$("#email").closest('.field').addClass('wrong_field');
				} else if( my_pc_app.send_result.errorCode == 'code'){
					$('#error_infobox').html('Incorrect Security code. Please reenter Security code.').slideDown();
					$("#code").closest('.field').addClass('wrong_field');
				} else if( my_pc_app.send_result.errorCode == 'system'){
					$('#error_infobox').html('Internal server error.').slideDown();
				}
				my_pc_app.dialog_mail_to_author_recaptcha();
				
			}
			
		}
	},
	dialog_mail_to_author_sending: function(){
		// remove all error markers
		$(".field").removeClass('wrong_field');
		$('#error_infobox').hide();
		
		// check empty fields
		if( $('#sendForm input, #sendForm textarea').filter(function() { return $(this).val() == ""; }).length > 0 ){
			$('#sendForm input, #sendForm textarea').filter(function() { return $(this).val() == ""; }).closest('.field').addClass('wrong_field');
			$('#error_infobox').html('Message could not be sent. All fields must be filled.').slideDown();
			return;
		}
		
		
		// send ajax request
		my_pc_app.send_begin_at = new Date().getTime() + 2000 ;
		$.post("/msg/send", $("#sendForm").serialize(), 
			function(data){
				my_pc_app.dialog_mail_to_author_result_check("result",data);
			}, "json");
			
		var ad = $('#mail_to_author_dialog').closest('.ui-dialog');
		var adp = ad.offset();
		$('#sendinig_waiter').width(ad.outerWidth()).height(ad.outerHeight()).css('left',adp.left+'px').css('top',adp.top+'px').fadeIn();

		return false;
	},
	dialog_abstracts_open: function( element_id){

		$('#abstractiframe').attr('src','/poster/ajax_getAbstracts/'+element_id+'/1');
		$('#abstract_print_button').unbind('click').click(function(){
			window.open ('/search/abstract2print/'+element_id,'PrintAbstract');
		});
		$('#abstract_pdf_button').unbind('click').click(function(){
			window.open ('/search/abstract2pdf/'+element_id,'AbstractPDF');
		});

		
		//alert( $('#abstractiframe').contents().find('html').html() );
		
		if( element_id > 0 ){

			$.post( "/poster/ajax_getAbstracts/"+element_id, 
				function(data){
					if( data.error > 0){
						alert( 'Ajax request error: ' + data.errorMessage );
					} else {
						$('#show_abstracts .number').html( data.absNum);
						$('#show_abstracts .title').html( data.absTitle);
						$('#show_abstracts .session').html( data.absSessno);
						$('#show_abstracts .category').html( data.absCategory);
						$('#show_abstracts .author').html( data.absAuthor);
						$('#show_abstracts .coauthor').html( data.absAuthorOthers);
						$('#show_abstracts .affiliation').html( data.absAffiliation);
						if(typeof data.absDOI != 'undefined')
							$('#show_abstracts .DOI a').html( data.absDOI).attr('href', data.absDOI);
						if( data.absGraphics != undefined && data.absGraphics != '' ){
							$('#show_abstracts .graphics').html( data.absGraphics );
							$('#abstracts_graphics_img').attr('src', data.absGraphics);
							$('#graphics').show();
						} else {
							$('#graphics').hide();
						}
						$('#show_abstracts').dialog('open');
					}
				},
				"json"
			);

		} else {

//			$('#show_abstracts').dialog('open');

		}
		
	},
	dialog_abstracts_close: function(){
		$('#show_abstracts').dialog('close');
		$('#abstractiframe').attr('src','javascript:""');
	},
	dialog_abstracts_open_21: function( element_id){

		$('#abstractiframe').attr('src','/poster/ajax_getAbstracts/'+element_id+'/1');
		$('#abstract_print_button').unbind('click').click(function(){
			window.open ('/search/abstract2print/'+element_id,'PrintAbstract');
		});
		$('#abstract_pdf_button').unbind('click').click(function(){
			window.open ('/search/abstract2pdf/'+element_id,'AbstractPDF');
		});
		
		if( element_id > 0 ){

			$.post( "/poster/ajax_getAbstracts/"+element_id, 
				function(data){
					if( data.error > 0){
						alert( 'Ajax request error: ' + data.errorMessage );
					} else {
						$('#show_abstracts .number').html( data.absNum);
						$('#show_abstracts .title').html( data.absTitle);
						$('#show_abstracts .session').html( data.absSessno);
						$('#show_abstracts .category').html( data.absCategory);
						$('#show_abstracts .author').html( data.absAuthor);
						$('#show_abstracts .coauthor').html( data.absAuthorOthers);
						$('#show_abstracts .affiliation').html( data.absAffiliation);
						if(typeof data.absDOI != 'undefined')
							$('#show_abstracts .DOI a').html( data.absDOI).attr('href', data.absDOI);
						if( data.absGraphics != undefined && data.absGraphics != '' ){
							$('#show_abstracts .graphics').html( data.absGraphics );
							$('#abstracts_graphics_img').attr('src', data.absGraphics);
							$('#graphics').show();
						} else {
							$('#graphics').hide();
						}
						$('#show_abstracts').dialog('open');
					}
				},
				"json"
			);

		} else {

//			$('#show_abstracts').dialog('open');

		}
		
	},
	dialog_abstracts_graphics_open_21: function( ){
		$('#show_abstracts_graphics').dialog('open');
		
	},
	dialog_abstracts_graphics_close_21: function(){
		$('#show_abstracts_graphics').dialog('close');
		$('#abstracts_graphics_img').attr('src','/img/pix.gif');
	}
}

