﻿/*
	tim.notification
*/
// Apply some easing to the animations
jQuery.easing['jswing'] = jQuery.easing['swing'];
jQuery.extend(jQuery.easing,
{
	def: 'easeOutBack',
	swing: function (x, t, b, c, d) {
		return jQuery.easing[jQuery.easing.def](x, t, b, c, d);
	},
	easeInOutSine: function (x, t, b, c, d) {
		return -c/2 * (Math.cos(Math.PI*t/d) - 1) + b;
	},
	easeOutBack: function (x, t, b, c, d, s) {
		if (s == undefined) s = 1.70158;
		return c*((t=t/d-1)*t*((s+1)*t + s) + 1) + b;
	}
});

jQuery.fn.notification = function(options) 
{
	var nOptions = $.extend({}, $.fn.notification.defaults, options);

	var $notificationWrapper = ($('#' + nOptions.classname + '-wrapper').length)? $('#' + nOptions.classname + '-wrapper') : $('<div />')
			.attr('id',nOptions.classname + '-wrapper')
			.appendTo(nOptions.parentElement),
	
		$notification = $('<div />')
			.attr('class', nOptions.classname)
			.appendTo($notificationWrapper),
	
		$notificationContent = $('<div />')
			.attr('class',nOptions.contentClassname)
			.html(nOptions.html)
			.appendTo($notification),
			
		$notificationClose = $('<a />')
			.attr('class',nOptions.classname+'-close')
			.click(function(){
				$notification
					.stop(true)
					.animate({height:'0'}, "100", "easeInOutSine", function() {
						nOptions.onClose.call($notification[0]);
						$notification.remove();
					});
			})
			.appendTo($notification)
	
		height = $notification.outerHeight(),
		
		relativeSpeed = nOptions.speed + height;
	
	nOptions.onOpen.call($notification[0]);
	
	$notification
		.css({height:'0'})
		.stop()
		.animate({height:height+'px'}, relativeSpeed, "easeOutBack")
		.delay(nOptions.visibleTime)
		.animate({height:'0'}, relativeSpeed, "easeInOutSine", function() {
			nOptions.onClose.call($notification[0]);
			$notification.remove();
		});
	
	return $notification;
};

jQuery.fn.notification.defaults = 
{
	parentElement: 'body',
	visibleTime: 4000,
	speed: 200,
	classname: 'notification',
	contentClassname: 'notification-content',
	html: '',
	onOpen: function(){},
	onClose: function(){}
};

/*
	tim.tooltip
*/
$.fn.tooltip = function(options) 
{
	var pluginDefault = $.extend({}, $.fn.tooltip.defaults, options);
	
	return this.each(function() 
	{
		var $this = $(this);
		var tooltipContent = $this.attr('title');
		
		if(pluginDefault.text != 'from_title_attr')
		{
			tooltipContent = pluginDefault.text;
		}
		
		var $tooltip = ($('#' + pluginDefault.id).length)? $('#' + pluginDefault.id) : $('<div />', {
			id: pluginDefault.id,
			text: tooltipContent
		}).appendTo('body');
		
		var $arrow = ($('#tt-arrow').length)? $('#tt-arrow') : $('<span />', { id: 'tt-arrow' }).appendTo($tooltip);
		
		$this.removeAttr('title');
		
		function positionTooltip(e) 
		{
			var tWidth = $tooltip.outerWidth();
			var tHeight = $tooltip.outerHeight();
			
			pluginDefault.Offset['x'] = -(tWidth/2);

			var tPosX = e.pageX + pluginDefault.Offset['x'];
			var tPosY = e.pageY + pluginDefault.Offset['y'] - tHeight;
			
			var maxPosX =  $(document).width() - tWidth -5;
			
			if ( tPosX >= maxPosX ) {
				tPosX = maxPosX
			}
			
			$tooltip.css({top: tPosY, left: tPosX});
		}
		
		function showTooltip(e)
		{
			positionTooltip(e);
			
			$tooltip
				.css({ display:'block', opacity:0 })
				.text(tooltipContent)
				.animate({opacity:0},600,function(){ //.delay(800) was a bit buggy, using old method instead
					$tooltip.animate({ opacity:1 },400);
				});
				
				
			$arrow.appendTo($tooltip);
		}
		
		function hideTooltip()
		{
			$tooltip.css({ display:'none', opacity:0 }).stop(true);
		}
		
		$this.bind({
			mouseenter: showTooltip,
			mouseleave: hideTooltip,
			mousemove: positionTooltip
		});
	});
};

$.fn.tooltip.defaults = 
{
	id: 'tim-tooltip',
	text: 'from_title_attr',
	Offset: { x: 0, y: -11 }
};


/*
	tim.modal-window
*/
$.fn.modalWindow = function(options) 
{
	var pluginDefault = $.extend({}, $.fn.modalWindow.defaults, options);
	
	var $thisMW = $('<div />')
			.attr('class',pluginDefault.classname),
			
		$content = $('<div />')
			.attr('class',pluginDefault.contentClassname)
			.appendTo($thisMW),
			
		$contentInner = $('<div />')
			.attr('class',pluginDefault.contentClassname+'-inner')
			.html(pluginDefault.html)
			.appendTo($content),
			
		$shadow = $('<div />')
			.attr('id','modal-shadow')
			.css('opacity','0.8')
			.appendTo('body'),
			
		$close = $('<a />')
			.attr('class','modal-close button')
			.attr('title','stäng')
			.appendTo($thisMW),
			
		$closeText = $('<span />')
			.text('stäng')
			.appendTo($close);
	
	if(/^(true)$/.test(pluginDefault.customCorners)) 
	{
		var $btr = $('<div />').attr('class','btr'),
			$bbr = $('<div />').attr('class','bbr'),
			$btl = $('<div />').attr('class','btl').appendTo($btr),
			$bbl = $('<div />').attr('class','bbl').appendTo($bbr);
		
		$thisMW.prepend($btr).append($bbr).addClass('custom-corners');
	}

	$thisMW.css('width',pluginDefault.contentWidth).prependTo('body');
	pluginDefault.onOpen.call($thisMW[0]);
	
	if(/^(true)$/.test(pluginDefault.center))
	{
		var width = $thisMW.outerWidth();
		var height = $thisMW.outerHeight();
		$thisMW.css({
			position:'absolute',
			top:		$(window).scrollTop(),
			left:		'50%',
			marginLeft:	'-' + (Math.ceil(width/2)) + 'px',
			marginTop:	(Math.ceil(height/2)) + 'px'
		});
		
	}
	
	$close.add($shadow).bind('click',function(){
		$thisMW.add($shadow).remove();
		pluginDefault.onClose.call($thisMW[0]);
	});
	
	return $thisMW;
};

$.fn.modalWindow.defaults = 
{
	classname: 'modal-window',
	contentClassname: 'modal-content',
	contentWidth: 'auto',
	center: true,
	customCorners: true,
	html: '',
	onOpen: function(){},
	onClose: function(){}
};


/*
	Simple-JS-Validation
*/
function validate_form ( e ) {

		var serialized = $(e.target).parents('form').serializeArray();
		var error_message = '';
		var error_field = '';
		
		for (var i = -1, len = serialized.length; ++i < len; ) {
		
			$('#'+serialized[i].name).removeClass('js-focus');
			
			if ( serialized[i].value.length == 0 ) {
				error_message = 'Se till att alla fält är korrekt ifyllda';
				error_field = $('#'+serialized[i].name);
				break;
			}
		}

		if ( serialized[1].value.length > 1 && ( serialized[1].name.indexOf('email') >= 0 ) ) {
			
			var hasAtSign = serialized[1].value.indexOf('@');
			var hasDotSign = serialized[1].value.indexOf('.');
			
			if ( hasAtSign < 0 || hasDotSign < 0 ) {
				error_message = 'Se till att du skrivit in rätt e-postadress';
				error_field = $('#'+serialized[1].name);
			}
		}
		
		if ( error_message.length == 0 ) {
			return;
		}
		
		else {
			$.fn.notification(
			{
				parentElement: e.data.notification_wrapper,
				classname: e.data.notification_classname,
				html: error_message
			});
		}
		
		error_field.addClass('js-focus');		
		error_field.focus().bind('focusout',function(){
			error_field.removeClass('js-focus');
		});
		
	return false;
		
}


/* #DOM-READY
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/
jQuery(function() {

	$('.post-meta .social a').attr('target','_blank');
	
	// Comment form js-enhancements
	$('#submit').bind( 'click', { 
		notification_wrapper: '#commentform', 
		notification_classname: 'comment-notification' 
	}, 
	validate_form );
	
	if(!$.browser.msie) {
		$('[title]').tooltip();
	}
	
	// Contact form js-enhancements
	$('#footer [id^=send_mail-contact_form]' ).bind( 'click', { 
		notification_wrapper: '#footer-widgets', 
		notification_classname: 'notification' 
	}, 
	validate_form );
	
	// Contact form js-enhancements
	$('#start-widgets [id^=send_mail-contact_form]' ).bind( 'click', { 
		notification_wrapper: '.widget_contact', 
		notification_classname: 'faq-notification' 
	}, 
	validate_form );
	
	// Show/hide klinik info
	/*
	$('div.klinik').find('.description').each(function(){
		var $this = $(this);
		var $toggle_link = $('<a />', {
			text: 'Mer info',
			'class': 'toggle-description',
			href: '#',
			click: function() {
				$this.slideToggle(200, 'linear');
				return false;
			}
		});
		
		$this.hide().before($toggle_link);
	});
	*/

	
	$('.klinik-map').bind('click',function()
	{
		// Open the modal window
		var href = $(this).attr('href');
		//href = "http://maps.google.com/maps?f=q&source=s_q&hl=sv&geocode=&q=Kanalgatan+59+skellefte%C3%A5&sll=39.024665,-108.515272&sspn=0.012502,0.027874&ie=UTF8&hq=&hnear=Kanalgatan+59,+Skellefte%C3%A5,+V%C3%A4sterbottens+L%C3%A4n,+Sverige&ll=64.751679,20.95891&spn=0.003432,0.018024&z=17";
		//html: '<iframe src="' + href + '" frameborder="0" scrolling="no" marginwidth="0" marginheight="0" width="500px" height="500px"></iframe>',
		$.fn.modalWindow(
		{
			html: '<iframe width="425" height="350" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="' + href + '&amp;output=embed"></iframe>',
			contentWidth: '485px',
			customCorners: true
		});
		
		return false;
	});

	//Cufon.replace('h2', { fontFamily: 'tim-thin', hover: true});
	
});
