$(function(){
	$(window).resize();
	$(".background").fullBg();
	$('#main_menu a').click(function(){
		var href = $(this).attr('href').split('/raamwerk/')[1];
		$(this).addClass('active').parent().siblings().find('a.active').removeClass('active')
		$('header').addClass('open');
		$.ajax({
			type: 'post',
			url: '/site/ajax/content_page.php',
			data: 'url=' + href,
			success: function(result){
				if($('.headerintro:visible').length){
					$('.headerintro').hide()
				}
				if(!$('.headercontent').length){
					$('.headerintro').after('<div class="headercontent"></div>')
				}
				$('header').height($('.header_wrap').height())
				$('.headercontent').html(result)
				$('header').animate({
					height: $('.header_wrap').height()
				}, 300, function(){
					$('header').css({height: ''})
				})
				
				// contacformulier
				validateContactform();
			}
		})
		$.address.value(href); 
		return false;
	})
	validateContactform();
	$('header .close_header').click(function(){
		$('header').height($('.headercontent').height())
		$('.headercontent').remove();
		$('header').animate({
			height: $('.header_wrap').height()
		}, 300)
		$('.headerintro').fadeIn(300)
		$('header').removeClass('open').find('a.active').removeClass('active')
		$.address.value(''); 
		return false;
	})
	
	$.address.init(function(event){
	}).externalChange(function(event){
		if(event.value != '/'){
			$('.'+event.pathNames[0]).trigger('click')						
		} else {
			if($('.close_header').is(':visible')){
				$('.close_header').trigger('click');
			}
		}
	})
	

	var daynames_short = ['Zo', 'Ma', 'Di', 'Wo', 'Do', 'Vr', 'Za'];
	var monthnames_long = ['Januari','Februari','Maart','April','Mei','Juni','Juli','Augustus','September','Oktober','November','December'];

	// ajax upload
	if($('#upload').length){
		new AjaxUpload('upload', {
			action: '/site/ajax/upload_image.php',
			responseType: 'json',
			data: {
				'feed_id' : $('.article').attr('id')
			},
			onSubmit: function(file, ext){
				$('.images .fc').before('<a class="image_thumb loading" href="#"></a>')
			},
			onComplete: function(file, result){
				$('.images .loading').remove();
				if(result.success){
					$('.images').find('.fc').before('<a class="colorbox image_'+ result.size +'" href="'+ result.large +'" rel="feedimages"><span rel="'+ result.id +'" class="delete_img"></span><img src="'+ result.thumb +'"></a>')
				} else {
					alert(result.message);
				}
			}
		});
	}
	
	// ajax upload for attachments
	if($('#uploadfile').length){
		new AjaxUpload('uploadfile', {
			action: '/site/ajax/upload_attachment.php',
			responseType: 'json',
			data: {
				'feed_id' : $('.article').attr('id')
			},
			onSubmit: function(file){
				$('.attachments .fc').before('<a class="file_thumb loading" href="#"></a>')
			},
			onComplete: function(file, result){
				$('.attachments .loading').remove();
				if(result.success){
					var icon = unescape(result.icon)
					$('.attachments').find('.fc').before('<a class="attachment" href="'+ result.id +'"><span rel="'+ result.id +'" class="delete_attachment"></span>'+icon+'<span class="filename">'+result.filename +'</span></a>')
				} else {
					alert(result.message);
				}
			}
		});
	}
	
	// making comments scroll down
	$.localScroll.hash();
	
	// making stars
	addRating();
	
	//login & password reset colorbox
	$('.login, .reset').topbox({
		width: '400px',
		onComplete: function(){
			var type = this.url.split('show=');
			$('#topbox form').parent().addClass(type[1])
			$('.select_type input[data-class="'+type[1]+'"]').next().trigger('click')
			$('#login').validate({
				submitHandler: function(form){
					$(form).find("input[type='submit']").attr('disabled', 'disabled');
					$(form).find('.preloader').show();
					$.ajax({
						type: 'post',
						url: '/site/ajax/login_reset.php',
						data: $(form).serialize(),
						dataType: 'json',
						success: function(result){
							$(form).find('.preloader').hide();
							if(result.success){
								if(result.type == 'reset'){
									$(form).find('.top_error').html(result.message);									
									$(form).find("input[type='submit']").removeAttr('disabled');
								} else if(result.redirect) {
									window.location= result.redirect;
								} else {
									location.reload();
								}
							} else {
								$('#topbox').effect('shake', {}, 50, function(){
									$(form).find('.top_error').html(result.message);
									$(form).find("input[type='submit']").removeAttr('disabled');
								});
							}
						}
					})
				}
			})
		}
	})
	$('.signup').topbox({
		width: '400px',
		onComplete: function(){
/*
			var type = this.url.split('show=');
			$('#topbox form').parent().addClass(type[1])
			$('.select_type input[data-class="'+type[1]+'"]').next().trigger('click')
*/
			$('#signup').validate({
				rules: {
		   			email: {
		   				required: true,
		   				email: true,
		   				remote: '/site/ajax/dublicate_check.php'
		   			}
		  		},
		  		messages: {
		  			email: {
		  				remote: 'Dit email-adres is reeds in gebruik.'
		  			}
		  		},
				submitHandler: function(form){
					$(form).find("input[type='submit']").attr('disabled', 'disabled');
					$(form).find('.preloader').show();
					$.ajax({
						type: 'post',
						url: '/site/ajax/signup.php',
						data: $(form).serialize(),
						dataType: 'json',
						success: function(result){
							$(form).find('.preloader').hide();
							if(result.success){
								$(form).find('#form_content_container').fadeOut(300, function(){
									$(this).before('<div class="form_success">'+result.message+'</div>')
								})
							} else {
								$(form).find('#form_content_container').fadeOut(300, function(){
									$(this).before('<div class="form_failure">'+result.message+'</div>')
								})
							}
						}
					})
				}
			})
		}
	})

	// profiel colorbox
	$('.change_profile').topbox({
		onComplete: function(){
			var type = this.url.split('show=');
			$('#topbox form').parent().addClass(type[1] + ', hasimage')
			$('.select_type label:first').trigger('click')
			// upload profile image
			$('textarea').elastic();
			$('#profile_form').validate({
				rules: {
		   			geboren: {
		   				required: false,
		   				dateNL: true
		   			}
		  		},
				submitHandler: function(form){
					form.submit();
				}
			})
			addUploadImage();
		}
	})
	
	// commenting
	$('.opinions form').validate({
		submitHandler: function(form){
			$(form).find("input[type='submit']").attr('disabled', 'disabled');
			$(form).find('.preloader').show();
			$.ajax({
				type: 'post',
				url: '/site/ajax/postopinion.php',
				data: $(form).serialize(),
				dataType: 'json',
				success: function(result){
					$(form).find('.preloader').hide();
					if(result.success){
						$(form).find("input[type='submit']").removeAttr('disabled');
						$(form).before(result.opinion)
						$(form).find('textarea').val('');
					} else {
						$(form).find("input[type='submit']").removeAttr('disabled');				
					}
				}
			})
		}
	})
	$('.opinions textarea').elastic();

	$('#postopinion textarea').live('focus', function(){
		$(this).prev().fadeOut('50')
	})
	$('#postopinion textarea').live('blur', function(){
		if($(this).val() == ''){
			$(this).prev().show();
		}
	})
	$('#postopinion label').live('click', function(){
		$(this).next().trigger('focus')
	})

	// deleting comments 
	$('.trashicon').live('click', function(){
		element = $(this);
/*
		$.alerts.overlayOpacity = '0.2';
		$.alerts.overlayColor = '#000';
		jConfirm('Weet u zeker dat u deze mening wilt verwijderen?', 'Verwijder mening', function(r) {
			if(r){
*/
				$.ajax({
					type: 'get',
					url: element.attr('href'),
					success: function(result){
						element.closest('.opinion').fadeOut(300, function(){
							$(this).remove();
						});
					}
				})
/*
			}
		});
*/
		return false;
	})

	// making popups hide when the body is clicked
	$('body').click(function() {
		if($('#updates:visible').length){
			$('#updates').hide();
		}
		if($('.search_result:visible').length){
			$('.search_result').hide();
		}
	});
	 
	// updates
	$('.showupdates').click(function(){
		if(!$('#updates').length){
			$('.updatecount').fadeOut(100)
			var element = $(this);
			$.ajax({
				type: 'post',
				url: '/site/ajax/updates.php',
				success: function(result){
					element.parent().append(result)
					$('#updates').click(function(event){
						event.stopPropagation();
					}).find('li:last').addClass('last')
				}
			})			
		} else if($('#updates:hidden').length) {
			$('#updates').show();
		} else {
			$('#updates').hide();
		}
		return false;
	})
	
	// sticky remove
/*
	// to enable, load cookie plugin in head of document
	$('.close_sticky').click(function(){
		var article_id = $(this).closest('.article').attr('id');
		$.cookie('sticky', article_id, {expires: 356, path: '/'});
		$(this).closest('.article').fadeOut('300', function(){
			$(this).remove();	
		})
	})
*/

	// live search
	$('.live_search input').live('keyup', function(e){
		var search_result = $(this).parent().find('.search_result');
		var element = $(this);
		if(element.parent().attr('target')){
			var target = $(element).parent().attr('target');
			var search = true;
			var type = '';
		} else {
			var target = '/site/ajax/findrelated.php';
			var search = false;
			var type = '&type='+element.attr('rel');
		}
		if(search_result.is(':hidden')){
			search_result.show();
		}

		var key = (e.keyCode ? e.keyCode : e.which);
		switch(key){
			case 27:
				// esc
				$('.search_result').hide();
				return false;
				break;
			case 40:
				//down
				if(search_result.find('.selected').length){
					search_result.find('.selected').removeClass('selected').nextAll(':has(a):first').addClass('selected')
				} else {
					search_result.find('li:has(a):first').addClass('selected')
				}
				return false;
				break;
			case 38:	
				//up
				if(search_result.find('.selected').length){
					search_result.find('.selected').removeClass('selected').prevAll(':has(a):first').addClass('selected')
				} else {
					search_result.find('li:has(a):last').addClass('selected')
				}
				return false;
				break;
			case 13:
				//enter
				if(search){
					if(search_result.find('.selected:not(.bottom)').length){
						window.location = search_result.find('.selected a').attr('href');
					} else if(search_result.find('.selected.bottom').length){
						if($('.add_new_item').is('.disabled')){
							window.location=$('.add_new_item').attr('href')
						} else {
							$('.add_new_item').trigger('click')
						}
					}					
					search_result.hide();
				} else {
					search_result.find('.selected a').trigger('click')					
				}
				return false;
				break;
		}
	
		var search_val = $(this).val();

		if(search_val){
			$.ajax({
				type: 'get',
				url: target,
				data: 'q='+search_val+type,
				success: function(result){
					if(element.parent().find('.search_result').length){
						element.parent().find('.search_result ul').html(result)
					} else {
						element.after('<div class="search_result"><ul>'+ result +'</ul></div>')
						var maxHeight = $(window).height() - $('.search_result').offset().top - 20;
						$('.search_result ul').css({
							'max-height': maxHeight
						})
					}
					
					if(search){
						element.parent().find('.search_result li.bottom a').click(function(){
							if($('.add_new_item').is('.disabled')){
								window.location=$('.add_new_item').attr('href')
							} else {
								$('.add_new_item').trigger('click')
							}
						})
						element.parent().find('.search_result').click(function(event){
							event.stopPropagation();
						})
					} else {
						element.parent().find('.search_result a').click(function(){
							element.val($(this).text())
							element.nextAll('input[type="text"]').val($(this).attr('rel'))
							element.parent().find('.search_result').hide();
							return false;
						})
					}

				}
			})			
		} else if(search_result.is(':visible')){
			search_result.hide();
		}
	})
	$('#search').submit(function(){
		if($(this).find('.search_result').length){
			if(!$(this).find('.search_result .selected').length){
				if(!$(this).find('.search_result a:first').parent().is('.bottom')){
/* 					window.location = $(this).find('.search_result a:first').attr('href');				 */
				} else {
					if($('.add_new_item').is('.disabled')){
						window.location=$('.add_new_item').attr('href')
					} else {
						$('.add_new_item').trigger('click')
					}
				}
			}
		}
		return false;
	})
	$('.search_result li').live('mouseover', function(){
		$(this).addClass('selected')
	})
	$('.search_result li').live('mouseout', function(){
		$(this).removeClass('selected')
	})
	// new item
	$('.add_new_item, .add_new_suggestion, .change_item').not('.disabled').topbox({
		overlayClose: false,
		onComplete: function(){
			if($('.select_type input:checked').length){
				$('.select_type input:checked').next().trigger('click')
			} else {
				$('#new_item_cont').addClass('opening')
			}
			addUploadImage()
			$('textarea').elastic();
			// actual form validation and submit			
			$('#new_item').validate({
				invalidHandler: function(form){
				},
				submitHandler: function(form){
					$(form).find('.preloader').show();
					$.ajax({
						type: 'post',
						url: '/site/ajax/additem.php',
						data: $(form).serialize(),
						dataType: 'json',
						success: function(result){
							$(form).find('.preloader').hide();
							if(result.success){
								// Next step in the form
								if(result.more){
									$('#topboxContent').children().remove();
									$('#topboxContent').append(result.more).effect('highlight', {
										color: '#aaaaaa'
									}, 500);
									if($(document).scrollTop() != 0){			
										$.scrollTo(0, 200);
									}
								} else if(result.newitem) {
									$.fn.topbox.close()
									window.location='/feed/';
								} else {
									$.fn.topbox.close()
									location.reload();								
								}
							} 
						}
					})
				}
			})
		}
	});
	if(window.location.hash == '#edit'){
		$('.change_item:last').trigger('click')
	}
	$('.cancel').live('click', function(){
		$.fn.topbox.close()
	})
	
	// Next step in form, 
/*
	$('.extra_items').live('click', function(){
		var element = $(this);
		$.ajax({
			url: element.attr('href'),
			dataType: 'json',
			success: function(result){
				$('#topboxContent').children().remove();
				$('#topboxContent').append(result.output)
				$('.select_type input:checked').next().trigger('click')
				addUploadImage()
			}
		})
		return false;
	})
*/
	
	// submitting form without input submit button
	$('.submit').live('click', function(){
		if($(this).attr('disabled') != 'disabled'){
			$(this).closest('form').submit();
		}
		return false;
	})
	
	// datepicker
	$('.date_field input, .double_date input').live('focus', function(){
		$(this).datepicker({
			dateFormat: 'dd-mm-yy',
			dayNamesMin: daynames_short,
			monthNames: monthnames_long,
			firstDay: 1
		});
	})
	
	// clickable checkbox
	$('label.clickable_checkbox').live('click', function(){
		var checkbox = $(this).prev('input[type="checkbox"]');
		if(checkbox.attr('checked')){
			checkbox.attr('checked', '')
		} else {
			checkbox.attr('checked', 'checked')
		}
	})
	
	// toevoegen van input velden aan het formulier
	$('a.add_forms').live('click', function(){
		var form = $(this).closest('.field_container');
		var form_id = form.attr('id');
		var cloned_fields = $('#'+ form_id + '_template').clone()
		form.find('.inputs').append(cloned_fields)
		form.find('.hide').removeClass('hide')
		$.fn.colorbox.resize();
		return false;
	})
	var required = [];
	
	// wijzigen van type
	
	// checkboxes in formulier
	$('.radio_fields label').live('click', function(){
		$(this).parents('.select_type').find('.checked').removeClass('checked')
		$(this).addClass('checked').prev().attr('checked', 'checked')
		
		// required fields
		required['bericht'] = $('.form_name input, .form_content textarea');
		required['vacature'] = $('.form_name input, .form_content textarea');
		required['bijeenkomst'] = $('.form_name input, .form_content textarea');
		required['oproep'] = $('.form_name input, .form_content textarea');
		required['advertentie'] = $('.form_name input, .form_content textarea');
		required['nieuws'] = $('.form_name input, .form_content textarea');
		required['verslag'] = $('.form_name input, .form_content textarea');
		required['verdieping'] = $('.form_name input, .form_content textarea');
		required['inloggen'] = $('#email_container input, #password_container input');
		required['password_reset'] = $('#email_container input');
		required['profile_info'] = $('.form_name input');
		required['contact_info'] = $('.form_email input');
		
		// switch
		var input_element = $(this).prev();
		var container = $(this).closest('form').parent();
		container.removeAttr('class').addClass(input_element.data('class'))		
		if(input_element.data('hasimage')){
			container.addClass('hasimage')
		}
		if(input_element.data('hasdate')){
			container.addClass('hasdate')
		}
		if(input_element.data('hashighlight')){
			container.addClass('hashighlight')
		}
		var form = $(this).parents('form');
		form.find('.required').removeClass('required')
		form.find('label.error').remove();
		form.find('.error').removeClass('error')
		$(required[input_element.data('class')]).addClass('required').prevAll('label').addClass('required')
	})

	// change profile form
	$('.public_name input[type=radio]').live('click', function(){
		if($(this).val() == 1){
			$('.settings_cont').addClass('members_only')
		} else {
			$('.settings_cont').removeClass('members_only')
		}
	})
	$('#sendmail').live('change', function(){
		if($(this).is(':checked')){
			$('#sendmail_container').slideDown()
		} else {
			$('#sendmail_container').slideUp()
		}
	})
	
	
	// dropdown
	// if ul has .internal it doesn't leave the page
	$('.dropdown').each(function(){
		var ulWidth = $(this).outerWidth();
		if(ulWidth <= 110){
			ulWidth = 125;
		}
		$(this).width(ulWidth).find('li:last').addClass('last')
	});
	$('.dropdown.internal li a').live('click', function(){
		$(this).parents('ul').prev().find('span:first').text($(this).text())
		$(document).trigger('click');
		return false;
	})
	$(document).click(function(event) {
		var $target = $(event.target);
        if($target.parents('.dropdown').length){
			$('.dropdown').not($target.parents('.dropdown')).removeClass('open')
			$target.parents('.dropdown').toggleClass('open')
		} else if ($('.dropdown.open').length){
			$('.dropdown').removeClass('open')
		}              
    });
	
	// load more items in the feed
	$('#load_more').live('click', function(){
		var offset = $(this).attr('href').split('#');
		el = $(this);
		$.ajax({
			type: 'get',
			url: el.attr('href'),
			success: function(result){
				el.after(result);
				el.remove();
				addRating();
				$(window).trigger('resize')
			}
		})
		return false;
	})
		
	// load more on scrolling down
	if($('body').hasClass('feed')){
		var offset = ($('#content .article:last').offset().top - $(document).height()) * -1;
		var documentHeight = $(document).height() - $(window).height() - offset;
		var loaded_more = false;
		$(document).scroll(function(){
			if($(this).scrollTop() > documentHeight && loaded_more == false){
				$('#load_more').trigger('click');
				loaded_more = true;
			}
		})
		$(window).resize(function(){
			documentHeight = $(document).height() - $(window).height() - offset;
		})		
	}
		
	// colorbox on images
	$('.colorbox').live('mouseover', function(){
		$(this).colorbox({
			maxWidth: '90%',
			maxHeight: '90%'
		})
	})
	$('.colorbox').trigger('mouseover');

	$('.colorbox_text').colorbox({
		maxWidth: '600px',
		opacity: 0.2
	})
	// colorbox on videos
	$('.videobox').colorbox({
		iframe:true,
		innerWidth:425,
		innerHeight:344,
		current: 'video {current} van {total}',
		onLoad: function(){
			$('#cboxContent').addClass('iframe')
		}
	})
		
	// remove images
	$('.delete_img').live('click', function(e){
		var element = $(this);
		if(element.closest('.images_cont').find('input[value="'+ element.data('id') +'"]').length){
			// if the element has just been added
			element.closest('.images_cont').find('input[value="'+ element.data('id') +'"]').attr('name', 'delete_image[]')
		} else {
			// if the element was there before already
			$('.images_cont').append('<input type="hidden" name="delete_image[]" value="'+ element.data('id') +'" />');		
		}
		element.parent().fadeOut(300, function(){
			$(this).remove();
		})
		e.preventDefault()
	})
	$('.delete_attachment').live('click', function(e){
		var element = $(this);
		if(element.closest('.attachments').find('input[value="'+ element.data('id') +'"]').length){
			// if the element has just been added
			element.closest('.attachments').find('input[value="'+ element.data('id') +'"]').attr('name', 'delete_attachment[]')
		} else {
			// if the element was there before already
			$('.attachments').append('<input type="hidden" name="delete_attachment[]" value="'+ element.data('id') +'" />');		
		}
		element.parent().fadeOut(300, function(){
			$(this).remove();
		})
		e.preventDefault()
	})
	
	$('#social a').qtip({
		position: {
			corner: {
		    	target: 'topMiddle',
		        tooltip: 'bottomMiddle'
			}
		},
		style: { 
		    name: 'light',
			tip: 'bottomMiddle'
		}
	})

});

$(window).resize(function(){
	$('.lines_bg').width($(window).width())
	$('.lines_bg').height($('#wrapper').outerHeight() + 30)
	$('.search_result').remove();
})

jQuery.validator.addMethod("dateNL", function(value, element, params) {
	return this.optional(element) || /^\d\d?\-\d\d?\-\d\d\d?\d?$/.test(value);
}, "Vul alstublieft een geldige datum in. (dd-mm-jjjj)");

// making stars
function addRating(){
	$('.rating ').find('input[type="submit"]').hide();
	$('.radio_rating').parent().stars({
		cancelShow: false,
		callback: function(ui, type, value)
				{
					ui.disable();
					var article_id = ui.$form.parents('.article').attr('id');
					$.post("/site/ajax/rating.php", {rating: value, id: article_id}, function(result)
					{
						ui.select(result.score);
						if(result.vote_amount <= 1){
							var vote_amount = '('+ result.vote_amount +' stem)'
						} else {
							var vote_amount = '('+ result.vote_amount +' stemmen)'							
						}
						ui.$form.parent().find('.vote_amount').text(vote_amount)											
					}, 'json');
				}

	});
}
function addUploadImage(){
	var target = $('#upload_colorbox').attr('href')
	var feed_id = $('#colorbox form input[name="id"]').attr('value')
	if(!feed_id){
		feed_id = '0';
	}
	new AjaxUpload('upload_colorbox', {
		action: target,
		responseType: 'json',
		data: {
			'feed_id' : $('#topbox form input[name="id"]').attr('value')
		},
		onSubmit: function(file){
			$('#topbox .submit').attr('disabled', 'disabled').addClass('disabled')
			if($('#topbox .image_thumb:empty:first').length){
				$('#topbox .image_thumb:empty:first').addClass('loading')			
			} else if($('#topbox .image_thumb').length){
				$('#topbox .images_cont .image_thumb:parent:last').after('<div class="image_thumb loading"></div>');
			} else {
				$('#topbox .images_cont').empty().append('<div class="image_thumb loading"></div>')
			}
		},
		onComplete: function(file, result){
			$('#topbox .submit').attr('disabled', '').removeClass('disabled')
			if(result.success){
				if(!result.profile){
					$('.images_cont .image_thumb:empty:first').remove();
					if(!$('.images_cont .image_thumb:parent').length){
						$('.images_cont').prepend('<div class="image_thumb"><span rel="'+ result.id +'" class="delete_img"></span><img src="'+ result.thumb +'"></div>')					
					} else {
						$('.images_cont .image_thumb:parent:last').after('<div class="image_thumb"><span data-id="'+ result.id +'" class="delete_img"></span><img src="'+ result.thumb +'"></div>')
					}
					$('.images_cont').append('<input type="hidden" name="image_id[]" value="'+ result.id +'" />')					
				} else {
					$('.profiel .image_large img').attr('src', result.thumb);
					$('#topbox .images_cont').empty().append('<img src="'+ result.thumb +'">');
				}

			} else {
				$('#topbox .loading').removeClass('loading')
				if(result.profile){
					$('#topbox .images_cont').empty().append('<img src="/site/gfx/default_profile_big.png">');
				}
				alert(result.message)
			}
		}
	});
	
	new AjaxUpload('uploadfile_colorbox', {
		action: '/site/ajax/upload_attachment.php',
		responseType: 'json',
		data: {
			'feed_id' : $('#topbox form input[name="id"]').attr('value')
		},
		onSubmit: function(file){
			$('#topbox .submit').attr('disabled', 'disabled')
			$('#topbox .submit').addClass('disabled')
			$('.attachments .fc').before('<a class="file_thumb loading" href="#"></a>')
		},
		onComplete: function(file, result){
			$('#topbox .submit').attr('disabled', '').removeClass('disabled')
			$('.attachments .loading').remove();
			if(result.success){
				debug.log(result)
				var icon = unescape(result.icon)
				$('#topbox .attachments').find('.fc').before('<div class="attachment"><span data-id="'+ result.id +'" class="delete_attachment"></span>'+icon+'<span class="filename">'+result.filename +'</span></div>')
				$('#topbox .attachments').append('<input type="hidden" name="attachment_id[]" value="'+ result.id +'" />')					
			} else {
				alert(result.message);
			}
		}
	});

}

function validateContactform(){
	if($('#contactform').length){
		$('#contactform').validate({
			invalidHandler: function(form){
			},
			submitHandler: function(form){
				$(form).find('input[type=submit]').attr('disabled', 'disabled')
				$.ajax({
					type: 'post',
					url: '/site/ajax/contactform.php',
					data: $(form).serialize(),
					success: function(result){
						if(result == 'success'){
							$(form).fadeOut(200, function(){
								$('#contact_thanks').fadeIn(100)
							})
						}
					}
				})
			}
		})
	}	
}
function confirmDialog(urllink, text){
    var retval=confirm(text);
    if(retval==true){
        window.location=urllink;
    } else {
        return;
    }
}

if(jQuery.browser.msie && parseInt(jQuery.browser.version, 10) == 6) {
	  try {
	    document.execCommand('BackgroundImageCache', false, true);
	  } catch(err) {}
	}
