﻿/**
*	Copyright: 2011 Mirre Svea Jensen
*	Author: Mirre Svea Jensen
*	Dependencies:
*		jquery v 1.4.2 and above
*		jquery-ui v 1.8.4 and above
*		curvycorners v 2.0.4 and above 
*			(not testet with older versions, but may be functional)
*			(for crossbrowser curved corners)
*/


(function($){   
  
    $.fn.extend({               
        //pass the options variable to the function   
        sideNote: function(options) 
        {   
            var self = this;
  
            //Set the default values, use comma to separate the settings, example:   
            var defaults = 
            {   
            	width: 100,
            	position: 'left', // 'left' or 'right'
            	topMargin: 200,
                positionFixed : true,  
                opacity : 0.8,
                curved : true,
                antiAlias : true,
                crossBrowserCurves : true,
                curveRadius : 8,
                showBorder: false,
                borderWidth: '7px',
				shiverOnHover: true,
				zIndex: 'auto'           
            }   
             
			     
            var options =  $.extend(defaults, options);   
            self.o = options;
			self.positionLeft = (self.o.position == 'left');
            
            self.sideNoteContainer = $("<div id='sideNoteContainer'></div>");
			$('body').prepend(self.sideNoteContainer);

            $(this).each(function(index)
            {
				
            	self.sideNoteWrap = $("<div class='sideNoteWrap'></div>");
            	$('body').prepend(self.sideNoteWrap);
	            self.sideNoteInner = $(this).detach();
				var shiverPadding;
				if(self.o.shiverOnHover)
				{
					shiverPadding = 35;
				}
				else
				{
					shiverPadding = 0;
				}
				self.sideNoteWrap.css('padding', shiverPadding + 'px').css('padding-left', '0px').css('z-index', self.o.zIndex);
	            self.sideNoteWrap.append(self.sideNoteInner).width(self.o.width)
	            	.css('position', 'absolute').append("<div style='clear: both;'></div>");  
				
				
				var defaultTopOffset;
				if($.browser == 'webkit')
				{
					defaultTopOffset = (self.sideNoteWrap.height() * -1) - 110
				}
				else
				{
					defaultTopOffset = (self.sideNoteWrap.height() * -1) - 10
				}
	            self.sideNoteWrap.css('margin-top',  defaultTopOffset + 'px');

	            if(self.positionLeft)
				{
					self.sideNoteWrap.css('left', 0);
				}
				else
				{
					self.sideNoteWrap.css('right', 0); 
				}
	          
	            self.sideNoteWrap.animate({opacity: self.o.opacity}, 1);
	            self.createCurves();
	            
				
            });
			if(self.o.shiverOnHover)
			{
            	self.CreateHoverBehave();
			}
			self.AnimateEaseIn();
        },
        
        CreateHoverBehave : function()
        {
	        var self = this;
			var notes = $('.sideNoteWrap');
			
        	notes.hover(function()
            {
            	$('.sideNoteWrap').css('position', 'absolute');
            	
            	$(this).effect("bounce", 
            	{ 
            		times: 5,
            		distance: 4,
            		direction: 'up'
            	}, 100);
            }, function ()
            {
            	$(this).stop();            
            });
        },
        
        AnimateEaseIn : function()
        {
        	var self = this;
        	
			var notes = $('.sideNoteWrap');
			var topOffset = 0;
			notes.each(function(index)
			{
				$(this).delay(index * 400).animate(
	        	{
	    			'margin-top': self.o.topMargin + topOffset  + 'px'
	   			}, 3000, 'easeOutElastic', function() {	$(this).css('position', 'absolute').css('top', '8px').css('left', '0px');}); 
	   			var tis = $(this);
	   			topOffset += tis.height() + 10;
	   		});
			
       },
        
        createCurves : function()
        {
        	var self = this;

        	if(self.o.curved)
            {
                var rad = self.o.curveRadius + 'px';
          		
				self.sideNoteInner.css(self.GetCssCurveSettings());      
                
          		if(self.o.crossBrowserCurves)
                {
					var v = parseFloat(jQuery.browser.version);
                    if($.browser.msie || ($.browser.opera && (v < 10)))
                    {
						if(typeof curvyCorners == 'function')
	                    	curvyCorners(self.GetCrossBrowserCurveSettings(), self.sideNoteInner[0]);
                    }
					
                }          
            }
        },
        
        GetCrossBrowserCurveSettings : function()
        {
        	var self = this;
			var settings; 			
			if(self.positionLeft)
			{
				settings =
	            {
	                tl: { radius: 0 },
	                bl: { radius: 0 },
	                tr: { radius: self.o.curveRadius },
	                br: { radius: self.o.curveRadius },
	                antiAlias: self.o.antiAlias
	            }             
	        }
            else
            {
            	settings =
	            {
	                tr: { radius: 0 },
	                br: { radius: 0 },
	                bl: { radius: self.o.curveRadius },
	                tl: { radius: self.o.curveRadius },
	                antiAlias: self.o.antiAlias
	            } 
            }
            return settings;
        },
        
        GetCssCurveSettings : function()
        {
	        var self = this;
			var rad = self.o.curveRadius + 'px';
        	if(self.positionLeft)
        	{
        		return { 
                	'-moz-border-radius-bottomright': rad, 
                	'-webkit-border-bottom-right-radius': rad, 
                	'border-bottom-right-radius': rad, 
                	'-moz-border-radius-topright': rad, 
                	'-webkit-border-top-right-radius': rad, 
                	'border-top-right-radius': rad 
                };
            }
            else
            {
            	return { 
                	'-moz-border-radius-bottomleft': rad, 
                	'-webkit-border-bottom-left-radius': rad, 
                	'border-bottom-left-radius': rad, 
                	'-moz-border-radius-topleft': rad, 
                	'-webkit-border-top-left-radius': rad, 
                	'border-top-left-radius': rad 
                };
            }
        },
        
        hideTrudy : function(trudyInner, h)
        {
        	var self = this;
        	trudyInner.animate(
        	{
    			'margin-top': '-' + h + 'px'
  			}, 1200, 'easeOutBounce', function() {	});
  			if (self.o.showFlapIcon)
	        {
	  			self.trudyFlap.find('div.icon').addClass(self.o.jQueryFlapDownIcon);
	  			self.trudyFlap.find('div.icon').removeClass(self.o.jQueryFlapUpIcon);
  			}
  			self.flapHover = true;
        }
         	  
    }); 
      
       
})(jQuery);  


