/* ---------------------------------- */

/* DK Plugins */

/*
 * AUTHOR
 * Ian Coyle
 * @link http://www.iancoyle.com
 *
 *
 * TABLE OF CONTENTS
 *
 * @DK:Crown
 * @DK:TouchEnable
 * @DK:OrientEnable
 * @DK:Content
 * @DK:Keyboard
 * @DK:Links
 *
 */


/* ---------------------------------- */

/* Crown */

(function($) {

  
   $.fn.Crown = function() {
   
     this.each(function() { 
      
        var $self   = $(this),
            $a      = $('a._CrownLink'),
            _text   = $self.html(),
            _vive   = 'This site is iPhone friendly. Use left/right on your keyboard to scroll content.',
            _active = false;
            
        
        $a.bind('click',on_click);
        
        function on_click(e){
        
          var html_text = _active ? _text : _vive;
        
          $self.html(html_text);
          
          _active = !_active;
          
          e.preventDefault();
        
        }
        
            
     });
     
    }
    

    
})(jQuery);

/* ---------------------------------- */

/* TouchEnable */

(function($) {

   $.fn.TouchEnable = function(settings) {
   
    var defaults = {
			threshold: {
				x: 30,
				y: 50
			},
			swipeLeft: function() { $(document).triggerHandler('KEYBOARD_RIGHT')}, 
			swipeRight: function() { $(document).triggerHandler('KEYBOARD_LEFT') },
			swiping: function(x) { $(document).triggerHandler('SWIPING',-x);} 
		};
		
		var options = $.extend(defaults, options);
		
		if (!this) return false;
		
		return this.each(function() {
			
			var me = $(this),
			    $self = $(this),
			    originalCoord = { x: 0, y: 0 },
			    finalCoord = { x: 0, y: 0 };
			
			function touch_start(event) {
				
				console.log('Starting swipe gesture...')
				
				originalCoord.x = event.targetTouches[0].pageX
				
				originalCoord.y = event.targetTouches[0].pageY
				
				$(document).triggerHandler('SWIPE_START',originalCoord.x)
				
			}
			
			function touch_move(event) {
			  	
				finalCoord.x = event.targetTouches[0].pageX; 
				
				finalCoord.y = event.targetTouches[0].pageY;
				
			}

			function touch_end(event) {
			
				changeY = originalCoord.y - finalCoord.y
				
				changeX = originalCoord.x - finalCoord.x
							
				if(changeY < defaults.threshold.y && changeY > (defaults.threshold.y*-1)) {
					
					changeX = originalCoord.x - finalCoord.x
				
					if(changeX > defaults.threshold.x) {
						defaults.swipeLeft()
					}
			
					if(changeX < (defaults.threshold.x*-1)) {
						defaults.swipeRight()
			
					}
				
				}
								
				$(document).triggerHandler('SWIPE_END');
				
			}
			
			function touch_cancel(event) { 


			}
			
			this.addEventListener("touchstart", touch_start, false);
			this.addEventListener("touchmove", touch_move, false);
			this.addEventListener("touchend", touch_end, false);
			this.addEventListener("touchcancel", touch_cancel, false);
			
		});

  }
   
})(jQuery);


/* ---------------------------------- */

/* Orientation */

(function($) {

   $.fn.OrientEnable = function(settings) {
   
    var config    = {},
        $body     = $('body'),
        _rotation = null;
 
    if (settings) $.extend(config, settings);
  
      this.each(function() {
      
        var $self     = $(this);
        
        setInterval(orientation_check,250);
        
        function orientation_check(e) {
        
          if(_rotation != window.orientation)
		        orientation_set();
        }
        
        function orientation_set() {
        

          switch(window.orientation){

            case 0:

              orient = 'portrait';
              $body.removeClass('landscape').addClass(orient);
              
            break;
          
            case -90:
            case 90:
            
              orient = 'landscape';
              $body.removeClass('portrait').addClass(orient);
              
            break;
        
          }
          
          _rotation = window.orientation;
          
          setTimeout(scrollTo,0,0,1);
        
        }
      
      });
   
   }
   
})(jQuery);



/* ---------------------------------- */

/* NAV */

(function($) {

   $.fn.DKColumn = function(settings) {

    var config = {};
    
    if (settings) $.extend(config, settings);
  
      this.each(function() {
      
        var $self   = $(this),
            $body   = $('body'),
            $media  = $self.find('.media'),
            $img    = $self.find('.media img'),
            _work   = $self.hasClass('_Portfolio'),
            _id     = config.id,
            _active = _id==0,
            _iphone = ((navigator.userAgent.match(/Android/i)) || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i))),
            _width  = _iphone || !_work ? 285 : 585;
        
        $body
        .bind('SEEK',on_seek)
        .bind('SEEK_START',on_seek_start);
            
        $self
        .bind('click',on_click);
          
        if (_id>0) 
          $self.css({opacity:.5})
        
        function on_click(e){
        
          
          
          if (!_active && _id!=0)
            $body.triggerHandler('PROXY',_id);
          
        }
        
        function on_seek(e,id){
        
          if ((id)==_id)
            activate();
          else
            deactivate();
        
        }
        
        function on_seek_start(e,id){
        
          //if ((id-1)==_id)
            deactivate();
        
        }
        
        function activate() {
          
          if (!_active) {
          
            $self
            .animate({opacity:1, width: _width},650,'easeInOutQuart')
            
            if ($media && !_iphone){
              
              $media.animate({width: 585, height: 585},650,'easeInOutQuart');
              //$img.animate({width: 585, height: 585},800,'easeInOutQuart');
            
            }
            
            _active = true
          
          }
            
        
        }
        
        function deactivate() {
        
          if (_active) {
          
            $self
            .animate({opacity:.5,width: 285},650,'easeOutQuart')

            if ($media && !_iphone) {
              
              $media.animate({width: 285, height: 285},650,'easeInOutQuart');
              //$img.animate({width: 285, height: 285},650,'easeInOutQuart');
            
            }
          
            _active = false;
          
          }
            
        
        
        }
        
        
        
      });
   
   }
   
})(jQuery);



/* ---------------------------------- */

/* DKContent */

(function($) {

   $.DKContent = {
      
      version: '1.0.9',
      mobile:  false,
      tablet: false
      
   };
   
   $.fn.DKContent = function(settings) {
     
     var config          = {},
        $body            = $('body'),
        $html            = ($.browser.mozilla || $.browser.msie) ? $('html') : $body,
        $wrapper         = $('#wrapper'),
        _sadBrowser      = $.browser.opera || ($.browser.msie && $.browser.version.indexOf('7.')>-1),
        _iphone          = ((navigator.userAgent.match(/Android/i)) || (navigator.userAgent.match(/iPhone/i)) || (navigator.userAgent.match(/iPod/i)));
    
     if (settings) $.extend(config, settings);
  
     this.each(function() { 
 
          var $self            = $(this),
              $content         = config.CONTENT,
              $content_items   = $content.find('div._ContentItem'),
              _column_width    = config.COLUMN_WIDTH ? config.COLUMN_WIDTH : 285,
              _speed           = config.SPEED ? config.SPEED : 650,
              _ease            = config.EASE ? config.EASE : 'easeInOutQuart',
              _pagewidth       = 1,
              _swipe_offset    = 0,
              _initialized     = false,
              active_col       = 0,
              total_col        = $self.find('.column').length-1;

          if (config.WRAPPER)
            config.WRAPPER.css({overflowX: 'hidden'}); 
                    
          $.DKContent.self = $self;  
  
          $content_items.each(function(index){
          
            $(this).DKColumn({id:index})
          
          });
          
          $body
          .bind('PROXY',on_proxy);
                    
          $(document)
          .bind('KEYBOARD_RIGHT',on_key_right)
          .bind('KEYBOARD_LEFT',on_key_left)
          .bind('SWIPE_START',on_swipe_start)
          .bind('SWIPING',on_swiping);
          
          setTimeout(init_view,1);
          
          function init_view(){
          
            $body.triggerHandler('PROXY',0)
            
            _initialized = true;
          
          } 
          
          function on_proxy(e,id) {
            
            active_col = id;
            
            seek();
          
          }
          
          
          function seek(position) {
          
            if (_initialized)
              $body.triggerHandler('SEEK_START',active_col);
            else
              $body.triggerHandler('SEEK',active_col);
            
            var left = _column_width*_pagewidth*active_col;
            
           
              $content.stop().animate({marginLeft:-left},_speed,_ease,on_seek_complete);
              
            
          
          }
          
          function on_seek_complete() {
          
            $body.triggerHandler('SEEK',active_col);
          
          }
          
          function on_swipe_start() {
          
            _swipe_offset = $self.attr('scrollLeft')
          
          }
          
          function on_swiping(e,x) {
          
            $self.attr('scrollLeft', _swipe_offset + x)
          
          }
          
          function on_key_right(e) {
 
            active_col++;
           
            if (active_col>total_col)
              active_col = total_col;
              
            seek();
            
          }
          
          function on_key_left(e) {
        
            active_col--;
    
            if (active_col<0)
              active_col = 0;
            
            seek();
            
          }
      
          
      }); 
      
      return this;
    
    
  } // TFALPost
  
})(jQuery);

/* ---------------------------------- */

/* KEYBOARD */

(function($) {


   $.fn.DKKeyboard = function(settings) {
     
    var config = {};
 
    if (settings) $.extend(config, settings);
  
      this.each(function() { 
        
        var _active         = true,
            _commandControl = false;

        $(this)
        .bind('keydown',on_keydown)
        .bind('keyup',on_keyup);
        
        $(this)
        .bind('IGNORE_KEYBOARD',on_ignore)
        .bind('ACTIVATE_KEYBOARD',on_activate);
         
        function on_ignore(e) {
        
          _active = false;
          
        }
        
        function on_activate(e) {
        
          _active = true;
        
        }
        
        function on_keyup(e) {
        
          _commandControl = false;
          
          $(this).triggerHandler('KEYBOARD_RELEASE');
        
        }
        
        function on_keydown(e) {
    
          if (_active) {
          
    		  var key = e.charCode ? e.charCode : e.keyCode ? e.keyCode : 0;
    		  
          switch(key) {
           
             case 39: //right

              $(this).triggerHandler('KEYBOARD_RIGHT');
              e.preventDefault();
              
             break;
              
             case 37: //left
             
              $(this).triggerHandler('KEYBOARD_LEFT');
              e.preventDefault();
                        
             break;
             
          }//switch


        
          } //if
          
        }//keydown
      
        
        
            
      }); 
      
      return this;
    
    
  } // ARROW-KEYBOARD
  

})(jQuery);




/* ---------------------------------- */

/* LINKS */

(function($) {

  
   $.fn.BlankLink = function() {
   
     this.each(function() { 
      
        var $self = $(this);
            
        
        $self
        .attr('target','_blank')
        .bind('click',on_click);
        
        function on_click(e){
        
          
        
        }
        
            
     });
     
    }
    
    
    $.fn.ProxyLink = function() {
   
     this.each(function() { 
      
        var $self = $(this),
            $body = $('body'),
            _rel  = $self.attr('rel');
            
        
        $self
        .bind('click',on_click);
        
        function on_click(e){
          
          $body.triggerHandler('PROXY',_rel)
          
          e.preventDefault();
        
        }
        
            
     });
     
    }
    
})(jQuery);


