/*---------------------------------------------------------------------------*/
/* Play nice with Moo
/*---------------------------------------------------------------------------*/
jQuery.noConflict();
/*---------------------------------------------------------------------------*/
/* Helper functions
/*---------------------------------------------------------------------------*/
function resizeImage(el,_width,_height){
	jQuery(el).each(function (){
		if(_width!=138){
			var width = jQuery(this).width();
			var height = jQuery(this).height();

			if(width < height){
				if(width < _width){
					jQuery(this).css({'width':_width+'px'});
					//width = jQuery(this).width();
					//height = jQuery(this).height();
				}
			}
			if(width > height){
				if(height < _height){
					jQuery(this).css({'height':_height+'px'});
					//width = jQuery(this).width();
					//height = jQuery(this).height();
				}
			}
		}
		repositionImages(el,_width,_height);
	});
}
function repositionImages(el,_width,_height){
	jQuery(el).each(function (){
		var width = jQuery(this).width();
		var height = jQuery(this).height();

		if(width > height){
			var center = (width - _width) / 2;
			center = Math.round(center);
			jQuery(this).css({'marginLeft':'-'+abs(center)+'px'});
		}
		if(width < height){
			var center = (height - _height) / 2;
			center = Math.round(center);
			jQuery(this).css({'marginTop':'-'+abs(center)+'px'});
		}
		jQuery(this).show();
	});
}
function abs(n) {
	return ((n<0) ? n*-1 : n);
}
/*---------------------------------------------------------------------------*/
/* Create place Google hook-up
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function(){
	jQuery('#GetGoogleLocation').click(function (){
		var adresse = jQuery('input[name=adresse]');
		var postnummer = jQuery('input[name=postnummer]');
		var by = jQuery('input[name=by]');
		var valid = true;
		/*
		if(adresse.val() === ''){
			adresse.css({'border':'1px solid red'}).next().css({'display':'block','color':'red'}).show();
			valid = false;
		}else{
			adresse.css({'border':'0px solid red'}).next().hide();
		}
		if(postnummer.val() === ''){
			postnummer.css({'border':'1px solid red'}).next().css({'display':'block','color':'red'}).show();
			valid = false;
		}else{
			postnummer.css({'border':'0px solid red'}).next().hide();
		}
		if(by.val() === ''){
			by.css({'border':'1px solid red'}).next().css({'display':'block','color':'red'}).show();
			valid = false;
		}else{
			by.css({'border':'0px solid red'}).next().hide();
		}
		*/
		if (valid) findAddress(adresse.val(),postnummer.val(),by.val());
	});
});
/*---------------------------------------------------------------------------*/
/* Lightboxer hover image switch
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function(){
	var mainpiclink = jQuery('div.mainpic a');
	var mainpicimage = jQuery('div.mainpic a img');
	jQuery('div.thumbholder a').hover(
		function () {
			jQuery('#imageName').html('');
			jQuery('#imageDesc').html('');
			mainpicimage.attr('src',jQuery(this).attr('rev'));
			mainpiclink.attr('href',jQuery(this).attr('href'));
			jQuery('#imageName').html(jQuery(this).attr('name'));
			jQuery('#imageDesc').html(jQuery(this).attr('title'));
		},
		function () {
			jQuery(this).find("span:last").remove();
		}
	);
});
/*---------------------------------------------------------------------------*/
/* Book flipper
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function(){
	var bookflip_firstrun = true;
	if(bookflip_firstrun){
		var bookflip_books = new Array();
		var bookflip_currentbook = 0;
		var bookflip_bookList = jQuery('div[rel="BookList"]');
		var bookflip_bookInfo = jQuery('div[rel="BookInfo"]');
		var bookflip_maxbooks = bookflip_bookList.length-1;
		bookflip_bookList.hide();
		bookflip_bookInfo.hide();
		jQuery('#BookList' + bookflip_currentbook).show();
		jQuery('#BookInfo' + bookflip_currentbook).show();
		bookflip_firstrun = false;
	}
	
	jQuery('a[rev="bookflip"]').click(function (){
		switch(jQuery(this).attr('rel')){
			case 'previous':
				(bookflip_currentbook==0) ? bookflip_currentbook=bookflip_maxbooks : bookflip_currentbook--;
				break;
			case 'next':
				(bookflip_currentbook==bookflip_maxbooks) ? bookflip_currentbook=0 : bookflip_currentbook++;
				break;
		}
		bookflip_bookList.hide();
		bookflip_bookInfo.hide();
		jQuery('#BookList' + bookflip_currentbook).show();
		jQuery('#BookInfo' + bookflip_currentbook).show();
	});
});
/*---------------------------------------------------------------------------*/
/* Ordbog
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	jQuery('.sel_word').hide();
	jQuery('.wordlink').click(function (){
		jQuery('.wordlink').show();
		jQuery('.sel_word').hide();
		jQuery(this).next().show().end().hide();
		return false;
	});
});
/*---------------------------------------------------------------------------*/
/* Arrangementer
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	jQuery('.TilmeldDivShow').click(function (){
		//jQuery(this).fadeTo("fast", 0.25);
		jQuery('#TilmeldDiv').css({'display':'block'});
		return false;
	});
	jQuery('.TilmeldDivClose').click(function (){
		//jQuery('.TilmeldDivShow').fadeTo("fast", 1);
		jQuery('#TilmeldDiv').css({'display':'none'});
		return false;
	});
});
/*---------------------------------------------------------------------------*/
/* Mosaic hover image switch
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function(){
	jQuery('a.Mosaic img').hover(
		function () {
			jQuery(this).attr('src',jQuery(this).parent().attr('rev'));
		},
		function () {
			jQuery(this).attr('src',jQuery(this).parent().attr('rel'));
		}
	);
});
/*---------------------------------------------------------------------------*/
/* Image box wide
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function(){
	var actualPos = 0;
	var SliderCurrentPage = 1;

	var ElementCount = jQuery('.thumbholder[@rev a').size();
	Spare = ElementCount % 5;
	if (Spare > 0) {
		ElementCount -= Spare;
		parts = (ElementCount / 5)+1;
	} else {
		parts = ElementCount / 5;
	}
	
	jQuery('a[@rel="picNaviLeft"]').click(function () {
		_moveThumbs('left',this);
		return false;
	});
	jQuery('a[@rel="picNaviRight"]').click(function () {
		_moveThumbs('right',this);
		return false;
	});
	
	function _moveThumbs(direction,who){
		if (parts > 1) {
			ids = jQuery(this).attr('rev');
			if (direction == "right" && SliderCurrentPage < parts) SliderCurrentPage += 1;
			if (direction == "left" && SliderCurrentPage > 1) SliderCurrentPage -= 1;
			(SliderCurrentPage != 1) ? jQuery('#picNaviLeft'+ids).css({'display':'inline'}) : jQuery('#picNaviLeft'+ids).css({'display':'none'});
			(SliderCurrentPage != parts) ? jQuery('#picNaviRight'+ids).css({'display':'inline'}) : jQuery('#picNaviRight'+ids).css({'display':'none'});
			actualPos=(SliderCurrentPage-1)*458;
			jQuery('.thumbholder').css({'left':'-' + actualPos + 'px'});
			//alert(SliderCurrentPage);
			//resizeImage('.imageboxwidethumb',90,90);
			//repositionImages('.imageboxwidethumb',90,90);
		}
	}
});
/*---------------------------------------------------------------------------*/
/* Limit textarea
/*---------------------------------------------------------------------------*/
/* OLD */
/*
jQuery(document).ready(function (){
	var myConstrainTextLabel = jQuery('.constrainTextLabel');
	var myConstrainText = jQuery('.constrainText');
	if(myConstrainText.length > 0){
		var maxlimit = jQuery('.constrainText').attr('rel');
		var labeltext = myConstrainTextLabel.text();
		myConstrainTextLabel.text(labeltext.replace('%maxlimit%', maxlimit - myConstrainText.val().length));
		myConstrainText.keyup(function (){
			if(myConstrainText.val().length > maxlimit) {
				myConstrainText.val(myConstrainText.val().substring(0, maxlimit));
			}else{
				myConstrainTextLabel.text(labeltext.replace('%maxlimit%', maxlimit - myConstrainText.val().length));
			}
		})
	}
})
*/
/* OLD */
jQuery(document).ready(function (){
	var myConstrainText = jQuery('.constrainText');
	myConstrainText.each(function (){
		var maxlimit = jQuery(this).attr('maxlength');
		var label = jQuery('.'+jQuery(this).attr('title'));
		var labeltext = label.attr('title');
		label.text(labeltext.replace('%maxlimit%', maxlimit - jQuery(this).val().length));
	});

	myConstrainText.keyup(function (){
		var maxlimit = jQuery(this).attr('maxlength');
		var label = jQuery('.'+jQuery(this).attr('title'));
		var labeltext = label.attr('title');
		(jQuery(this).val().length > maxlimit) ? jQuery(this).val(jQuery(this).val().substring(0, maxlimit)) : label.text(labeltext.replace('%maxlimit%', maxlimit - jQuery(this).val().length));
	});
});
/*---------------------------------------------------------------------------*/
/* Magasine delivery address
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	jQuery('#debatmagasin').click(function (){
		(jQuery(this).is(":checked")) ? jQuery('#deliveryaddress').show() : jQuery('#deliveryaddress').hide()
	});
});
/*---------------------------------------------------------------------------*/
/* Profile overview images
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	/*
	jQuery('.profileoverviewimage').each(function (){
		var width = jQuery(this).width();
		var height = jQuery(this).height();
		
		if(width < height){
			if(width < 138){
				jQuery(this).css({'width':'138px'});
				width = jQuery(this).width();
				height = jQuery(this).height();
			}
		}
		if(width > height){
			if(height < 138){
				jQuery(this).css({'height':'138px'});
				width = jQuery(this).width();
				height = jQuery(this).height();
			}
		}
		jQuery(this).css({'display':'block'});
	});
	*/
	//resizeImage('.profileoverviewimage',138,138);
	//repositionImages('.profileoverviewimage',138,138);
});
/*---------------------------------------------------------------------------*/
/* Imagebox wide overview images
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	/*
	jQuery('.imageboxwidethumb').each(function (){
		var width = jQuery(this).width();
		var height = jQuery(this).height();
		
		if(width < height){
			if(width < 90){
				jQuery(this).css({'width':'90px'});
				width = jQuery(this).width();
				height = jQuery(this).height();
			}
		}
		if(width > height){
			if(height < 90){
				jQuery(this).css({'height':'90px'});
				width = jQuery(this).width();
				height = jQuery(this).height();
			}
		}
	});
	*/
	//resizeImage('.imageboxwidethumb',90,90);
	//repositionImages('.imageboxwidethumb',90,90);
});
/*---------------------------------------------------------------------------*/
/* Expand box to show all
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	jQuery('.dynamicShowAll').click(function (){
		jQuery(this).parent().siblings('.ctn').children('.dynamic2').children('.dynamicHidden').toggle();
		resizeImage('.imageboxwidethumb',90,90);
		//repositionImages('.placesboximage',90,90);
	});
});
/*---------------------------------------------------------------------------*/
/* Places image box
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	//resizeImage('.placesboximage',90,90);
	//repositionImages('.placesboximage',90,90);
});
jQuery(document).ready(function (){
	//resizeImage('.articleProfileImage',90,90);
	//repositionImages('.placesboximage',90,90);
});
jQuery(document).ready(function (){
	//resizeImage('.newestdebate',45,45);
	//repositionImages('.newestdebate',45,45);
});
/*---------------------------------------------------------------------------*/
/* Clear text in textbox
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	jQuery('.cleartext').focus(function (){
		if(jQuery(this).val()==jQuery(this).attr('alt')){
			jQuery(this).val('')
		}
	});
	jQuery('.cleartext').blur(function (){
		if(jQuery(this).val()==''){
			jQuery(this).val(jQuery(this).attr('alt'))
		}
	});
});
/*---------------------------------------------------------------------------*/
/* Upload image
/*---------------------------------------------------------------------------*/
/*
jQuery(document).ready(function (){
	jQuery('#upload1').change(function (){
		alert(jQuery(this).val(),jQuery(this).attr('value'));
		jQuery('#upload1image').attr('src',jQuery(this).val()).show();
	});
});
*/
/*---------------------------------------------------------------------------*/
/* Expand box to show all 2
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	jQuery('._toggle').click(function (){
		jQuery(this).parent().siblings('.ctn').children('.places_var2').children('.dynamicHidden').toggle();
		jQuery(this).toggle().siblings().toggle();
	});
});
/*---------------------------------------------------------------------------*/
/* Locations: Show expert comment
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	jQuery('#showExpertQA').click(function (){
		jQuery('#ExpertQA').toggle();
	});
});
/*---------------------------------------------------------------------------*/
/* Cloak email
/*---------------------------------------------------------------------------*/
jQuery(document).ready(function (){
	var at = / at /;
	var dot = / dot /g;
	jQuery('span.cloakEmail').each(function (){
		var addr = jQuery(this).text().replace(at,'@').replace(dot,'.');
		jQuery(this).after('<a href="mailto:'+addr+'" title="Send an email">'+ addr +'</a>').hover(function(){window.status='Send en email!';}, function(){window.status='';});
		jQuery(this).remove();
	});
});

jQuery.fn.highlight = function (text,o) {
	return this.each( function(){
		var orgtext = jQuery(this).html()
		var replace = o || '<span class="highlight">$1</span>';
		jQuery(this).html( orgtext.replace( new RegExp('('+text+'(?![\\w\\s?&.\\/;#~%"=-]*>))', "ig"), replace) );
	});
}

/* Text size init
/*--------------------------------------------------------------------------------*/
/*
jQuery(document).ready(function (){
	jQuery('#article_editor').css('font-size','14px');
	jQuery('#article_editor').css('line-height','18px');
});
*/