//<![CDATA[

function htmlEncode(s) {
	var str = new String(s);
	str = str.replace(/<.*?>/g, "");
	str = str.replace(/;/g, "");

	return str;
}

function textCounter(field,cntfield,maxlimit) {
	if (field.value.length > maxlimit)
		field.value = field.value.substring(0, maxlimit);
	else
		cntfield.value = maxlimit - field.value.length;
}
	
//show and hide the loading message
function loadingmessage(msg, show_hide){
	if(show_hide=="show"){
		$('#loader').show();
		$('#upload_status').show().html('<p><span id="loader"><img src="../logo/loader.gif" alt="Loading..."/></span> <span id="progress"></span></p>');
		$('#progress').show().text(msg);
		$('#uploaded_image').html('');
		$('#upload_link').hide();
	}else if(show_hide=="hide"){
		$('#loader').hide();
		$('#progress').text('').hide();
		$('#link_upload').remove();
	}else{
		$('#loader').hide();
		$('#progress').text('').hide();
		$('#uploaded_image').html('');
	}
}

function change_status(status){
	if (status == 1)
		$('#upload_status').show().html(text_gegevens);
	else if (status == 2)
		$('#upload_status').show().html(text_terug);
	else if (status == 3)
		$('#upload_status').show().html(val_naam);
	else if (status == 4)
		$('#upload_status').show().html(val_email);
	else if (status == 5)
		$('#upload_status').show().html(val_tekst);
	else if (status == 6)
		$('#upload_status').show().html(val_url);
	else if (status == 7)
		$('#upload_status').show().html(val_letter);
	else if (status == 8)
		$('#upload_status').show().html(val_selectie);
	else if (status == 9)
		$('#upload_status').show().html(val_disclaimer);
	else if (status == 10)
		$('#upload_status').show().html(val_email_er);
}

function checkEmail(email) {
	if (/^\w+([\.-]?\w+)*@\w+([\.-]?\w+)*(\.\w{2,3})+$/.test(email)){
		return (false)
	}
	return (true)
}

function checkUrl(url) { 
    var v = new RegExp(); 
    v.compile("^[A-Za-z]+://[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$"); 
    if (!v.test(url)) { 
        return true; 
    }
} 

function initMenus() {
  $('.menu ul').hide();
  $('.menu ul:first').show();
  $('.menu li a').click(
    function() {
      var checkElement = $(this).next();
      if((checkElement.is('ul')) && (checkElement.is(':visible'))) {
        return false;
        }
      if((checkElement.is('ul')) && (!checkElement.is(':visible'))) {
        $('.menu ul:visible').slideUp('normal');
        checkElement.slideDown('normal');
        return false;
        }
      }
    );
  }

//create a preview of the selection
function preview(img, selection) {
	//get width and height of the uploaded image.
	var current_width = $('#uploaded_image').find('#thumbnail').width();
	var current_height = $('#uploaded_image').find('#thumbnail').height();

	var scaleX = thumb_width / selection.width; 
	var scaleY = thumb_height / selection.height; 
	
	$('#upload_thumbnail').find('#thumbnail_preview').css({ 
		width: Math.round(scaleX * current_width) + 'px', 
		height: Math.round(scaleY * current_height) + 'px',
		marginLeft: '-' + Math.round(scaleX * selection.x1) + 'px', 
		marginTop: '-' + Math.round(scaleY * selection.y1) + 'px' 
	});
	$('#x1').val(selection.x1);
	$('#y1').val(selection.y1);
	$('#x2').val(selection.x2);
	$('#y2').val(selection.y2);
	$('#w').val(selection.width);
	$('#h').val(selection.height);
} 


$(document).ready(function () {
	initMenus();
	//select all the a tag with name equal to modal
	$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		//Get the window height and width
		var winH = $(window).height();
		var winW = $(window).width();
              
		//Set the popup window to center
		$(id).css('top',  winH/2-$(id).height()/2);
		$(id).css('left', winW/2-$(id).width()/2);
	
		//transition effect
		$(id).fadeIn(2000); 
		$('html, body').scrollTop(0);
	});
	
	//if close button is clicked
	$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
	$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			


		$('#loader').hide();
		$('#progress').hide();
		var myUpload = $('#upload_link').upload({
		   name: 'image',
		   action: '../logo/letter_functions.php',
		   enctype: 'multipart/form-data',
		   params: {upload:'Upload'},
		   autoSubmit: true,
		   onSubmit: function() {
		   		$('#upload_status').html('').hide();
				loadingmessage(text_loading, 'show');
		   },
		   onComplete: function(response) {
		   		loadingmessage('', 'hide');
				response = unescape(response);
				var response = response.split("|");
				var responseType = response[0];
				var responseMsg = response[1];
				if(responseType=="success"){
					var current_width = response[2];
					var current_height = response[3];
					//display message that the file has been uploaded
					$('#upload_status').show().html(text_uploaded);
					setTimeout('change_status(1)', 1000);
					//put the image in the appropriate div
					$('#uploaded_image').html('<img src="'+responseMsg+'" style="float: left; margin-right: 10px;" id="thumbnail" onmouseover="Tip(tool_thumb)" onmouseout="UnTip()" />')
					$('#upload_thumbnail').html('<div style="border:1px #e5e5e5 solid; position:relative; overflow:hidden; width:'+thumb_width+'px; height:'+thumb_height+'px;"><img src="'+responseMsg+'" style="position: relative;" id="thumbnail_preview" alt="Thumbnail Preview" /></div>')
					//find the image inserted above, and allow it to be cropped
					$('#uploaded_image').find('#thumbnail').imgAreaSelect({ aspectRatio: '1:'+(thumb_height/thumb_width)+'', onSelectChange: preview }); 
					//display the hidden form
					$('#upload_form').show();
					$('#upload_bottom').show();
				}else if(responseType=="error"){
					$('#upload_status').show().html('<p>error...'+error_unexpected + responseMsg+'</p>');
					$('#uploaded_image').html('');
					$('#upload_form').hide();
					$('#upload_bottom').hide();
				}else{
					$('#upload_status').show().html('<p>'+error_unexpected + responseMsg+'</p>');
					$('#uploaded_image').html('');
					$('#upload_form').hide();
					$('#upload_bottom').hide();
				}
		   }
		});
	
		$('#annuleren').click(function() {
			$.ajax({
				type: 'POST',
				url: image_handling_file,
				data: 'annuleren=annuleren',
				cache: false,
				success: function(){
				}
			});
		});
		

		
	//create the thumbnail
	
	$('#save_thumb').click(function() {
		var nieuwsbrief = $('#nieuwsbrief:checked').val();
		var disclaimer = $('#disclaimer:checked').val();
		var url = htmlEncode($('#url').val());
		var ipadres = $('#ipadres').val();
		var email = htmlEncode($('#email').val());
		var letter = $('#letter').val();
		var naam = htmlEncode($('#naam').val());
		var tekst = htmlEncode($('#tekst').val());
		var x1 = $('#x1').val();
		var y1 = $('#y1').val();
		var x2 = $('#x2').val();
		var y2 = $('#y2').val();
		var w = $('#w').val();
		var h = $('#h').val();
		if (naam=="") {
			change_status(3);
			$('html, body').scrollTop(0);
			return false;
		}else if(email==""){
			change_status(4);
			$('html, body').scrollTop(0);
			return false;
		}else if(checkEmail(email)){
			change_status(10);
			$('html, body').scrollTop(0);
			return false;
		}else if(url!=="http://" && url!=="" && checkUrl(url)){
			change_status(6);
			$('html, body').scrollTop(0);
			return false;
		}else if(letter==""){
			change_status(7);
			$('html, body').scrollTop(0);
			return false;
		}else if(x1=="" || y1=="" || x2=="" || y2=="" || w=="" || h==""){
			change_status(8);
			$('html, body').scrollTop(0);
			return false;
		}else if(disclaimer == null){
			change_status(9);
			$('html, body').scrollTop(0);
			return false;
		}else{
			if(url=="http://") {
				url = "";
			}
			if(nieuwsbrief == null) {
				nieuwsbrief = 0;
			} else {
				nieuwsbrief = 1;
			}
			//hide the selection and disable the imgareaselect plugin
			$('#uploaded_image').find('#thumbnail').imgAreaSelect({ disable: true, hide: true });
			$('#upload_form').hide();
			$('#upload_bottom').hide();
			loadingmessage(text_saving, 'show');
			$.ajax({
				type: 'POST',
				url: '../logo/letter_functions.php',
				data: 'save_thumb=Save Thumbnail&x1='+x1+'&y1='+y1+'&x2='+x2+'&y2='+y2+'&w='+w+'&h='+h+'&naam='+naam+'&tekst='+tekst+'&email='+email+'&url='+url+'&ipadres='+ipadres+'&letter='+letter+'&nieuwsbrief='+nieuwsbrief,
				cache: false,
				success: function(response){
					loadingmessage('', 'hide');
					response = unescape(response);
					var response = response.split("|");
					var responseType = response[0];
					var responseLargeImage = response[1];
					var responseThumbImage = response[2];
					if(responseType=="success"){
						$('html, body').scrollTop(0);
						$('#upload_status').show().html(text_saved);
						setTimeout('change_status(2)', 2000);
						setTimeout('window.location = "'+succes_url+'"', 4000);						
					}else{
						$('#upload_status').show().html(error_unexpected + response);
						//reactivate the imgareaselect plugin to allow another attempt.
						$('#uploaded_image').find('#thumbnail').imgAreaSelect({ aspectRatio: '1:'+(thumb_height/thumb_width)+'', onSelectChange: preview }); 
						$('#upload_form').show();
						$('#upload_bottom').show();
					}
				}
			});
			
			return false;
		}
	});
}); 


//]]>
