var url = '';
var showFile = false;
var view = null;

// starting the script on page load
$(document).ready(function(){

if(FlashDetect.installed && FlashDetect.versionAtLeast(9, 0, 124)){
    
	$('#fileholder').fileUpload({ 
		  'uploader':  'fileadmin/templates/grafik/uploader.swf', 
		  'script':    url,
		  'cancelImg': 'fileadmin/templates/grafik/cancel.png',
		  'scriptData': {"ic_job_application_pi1[posting]": "1", "ic_job_application_pi1[mode]": "cvattach"},
		  'buttonImg': 'fileadmin/templates/grafik/input/uploadify_button.gif',
		  'width': '197',
		  'height': '20',
		  'auto': true,
		  'onComplete': function(event, queueID, fileObj, response, data){
			  var fileid = 0;
			  var resp = response.split( ';' );
			  fileid = resp[1];
			  var link = url+'?ic_job_application_pi1[download]=1&ic_job_application_pi1[fileid]='+fileid+'&ic_job_application_pi1[file]='+fileObj.name;
			  var former = document.getElementById('job_application');
			  former.elements[1].value = fileid;
			  former.elements[2].value = link;
			
			//Red border for error
		  	$('#upload_holder').removeClass('uploadify_error');
		  	$('#upload_holder').addClass('uploadify');
		  	
		  	//Change error image
		  	var src = 'fileadmin/templates/grafik/questionmark.gif';
		  	$('#cv_help > span.help > img').attr('src', src);
		  	$('#uploadify_show_message').fadeIn( 'slow' ).html('<a href="'+link+'" target="_blank">'+fileObj.name+'</a>');
		  	
		  }
	});
	
	if(showFile) {
		var former = document.getElementById('job_application');
		  
		var fileid = former.elements[1].value;
		var filename = former.elements[2].value;
		
		$('#uploadify_show_message').show().html('<a href="'+url+'?ic_job_application_pi1[download]=1&ic_job_application_pi1[fileid]='+fileid+'&ic_job_application_pi1[file]='+filename+'" target="_blank">'+filename+'</a>');
	}
}else{
    $('#uploadify_show_message').show().html( '<p>Din flash version er for &aelig;ldet!<br/><a href="http://get.adobe.com/flashplayer/" target="_blank">Klik her for at hente en ny udgave</a></p>' );    
}

//Show the right form after a submit
if (window.location.href.indexOf('tx_icjobseek_pi1[poserr]=1') >= 0) {
	showChoice(view);
} else {
	//$('.job_seek_choices :radio'
	showChoice(1);
}


$('.button').click(function(){
	
	var whichView = 1;
	if($('.job_seek_choices :radio').val() == 0){
		whichView = 0;
	}
	
	$('input[name="ic_job_application_pi1[view]"]').val(whichView);
	
});

});


function seekjob(url) {
	
	window.location = '/'+url;
}

function printjob(url){
	
	var a = window.open(url+'&print=1&no_cache=1');
	
}

function showChoice(choice) {
	if(choice == 0){
		document.getElementById('seekformlogin_0').style.display = 'block';
		document.getElementById('seekformlogin_1').style.display = 'none';
	}else if(choice == 1 && document.getElementById('seekformlogin_0').className != 'errorprone') {
		document.getElementById('seekformlogin_0').style.display = 'none';
		document.getElementById('seekformlogin_1').style.display = 'block';
	}
}

function setTask(taskurl){
	
	$.ajax( {
		type :'post',
		url :'/'+taskurl,
		success : function(html) {
			document.getElementById('jobseek_login').submit();
		}

	});// Close .ajax
}
