var App = {
  user: "orionengleton",
  
  checkIfMobile: function(){
    var deviceAgent = navigator.userAgent.toLowerCase();
    var agentID = deviceAgent.match(/(iphone|ipod)/);
    if (agentID) {  
    }
  },
  setWelcome: function() {
    $(document).meow({
      message: $('#welcome-message'),
      icon: 'images/glyphicons_205_electricity.png'
    })
  },
  contactSuccess: function() {
    alert('Successful Posting')
  },
  
  ajaxInit: function(_param) {
    $(".loading").ajaxStart(function(){
       $(_param).show();
     });
  },

  sendFormInfo: function() {
    $('form#contact_form').submit( function() {
        
        $.ajax({
          type: 'POST',
          url: '/contactme',
          data: serialize($('#contact_form')) ,
          success: App.contactSuccess(),
          datatype: "html"
        })
      
      return false;
    })
},
  closeDraw: function() {
    $('a#close-blog').click(function(e){
        
       e.preventDefault();
       $('span.xbutton').hide()      
       $('#dream .box_blog #content').animate({
          width: "5px", marginLeft: "20px"
          // fontSize: "2em",
          // borderWidth: "10px"
        }, 200, 
       function() {
          $('#dream .box_blog #content #blogs').remove();
         }
       );
    });

  },

  openDraw :function() {
    $('a#dream-blog').click(function(e){
       e.preventDefault();
       $('span.xbutton').show();
       $('#dream .box_blog #content').animate({
          width: "400px", marginLeft: "20px"
        }, {
          duration: 200,
          specialEasing: {
          width: 'linear',
          height: 'easeOutBounce'
        }, 
        complete: function() {
         App.ajaxInit('#dream .box_blog #content .loading');   
         $.get('/blog_posts', function(data) { $('#dream .box_blog #content').html(data); } ); 
         }
       });
    });
  },
  
  callTwitter: function() {
    $(".twit").getTwitter({
		userName: this.user,
		numTweets: 4,
		loaderText: "Loading tweets...",
		slideIn: false,
		showHeading: true,
		headingText: "Latest Tweets",
		showProfileLink: false, 
		showTimestamp: false
	  });
  },
  
  sprocketIconfunc: function(){
    var design, paper;
    paper = Raphael(document.getElementById('canvas-top'), 170, 170);
    design = paper.path(window.sprocketIcon);
    design.attr({ fill: "#000", stroke: "none", scale: 10, opacity: 1 });
  
    _hoverFunc = function() {
      design.animate({fill: "#000", rotation: "300", opacity: 1 }, 1000, '<>') } 
    _hoverOut = function() {
      design.animate({fill: "#000", rotation: "200", opacity: 0.4}, 1000, '<>') }
    $(design.node).hover(_hoverFunc, _hoverOut); 
  },
  
  whereIconfunc: function(){
      var where = new Raphael('contact-icons', 40, 40);
      var icon = where.path( window.whereIcon )
      icon.attr({ 
                fill: "#fff"
                , stroke: "none"
                , scale: 1
              });
  },

  contactForm: function(dom_block){
    alert(' What the Hell!')
    var _d = dom_block;
    $.get('/contact', function(data) { 
      $(_d).html(data); 
    });
  },
  
};

App.ajaxGetandLoader = function(resource, target) {
  var thediv = target;
  var load = thediv + ' .loading';
  $(load).ajaxStart(function() {
    $(this).show();
  })
  $.get(resource , function(data) {
      $(thediv).html(data);
      // window.setTimeout(function () {  $(thediv).html(data); } , 5000);
      /* Remember to remove */ 
  })
}
//

function lowerDream() {
  
  if( !(this instanceof arguments.callee)) {
    return new arguments.callee(arguments)
  }
  var self = this;

  self.drawDotAndLabel  = function(xOffset) {
    if( $('#link-image').length == 1)  {
    var _orion = Raphael('link-image', 450, 250)
    var _image = _orion.rect( xOffset, 100, 300, 30)
        _image.attr({
          scale: 3
          , fill: '#999'
          , "fill-opacity" : 0.3
          , stroke: 'none'
      })
    }
  }
  
  self.addTextToPath = function(release) {
    
    var test = self.text( 10, 70, release).attr({
          "font-family" : 'Helvetica Neue, arial, sans-serif'
        , fill: '#fff'
        , "font-size": "123em"
        , 'text-anchor': 'start'
    });
  }
}



$(document).ready(function() {	
	if ($('.twit') !== null ) {
		App.callTwitter();
  }
  if ( $('#canvas-top').length == 1) { 
    App.sprocketIconfunc() 
  }
  
  App.bindBehaviors = function() {
    
    $('.acc_container').hide(); //Hide/close all containers
    $('.acc_trigger:first').addClass('active').next().show();

    $('.acc_trigger').live('click', function(){
    	  if( $(this).next().is(':hidden') ) { 
    		$('.acc_trigger').removeClass('active').next().slideUp(); 
    		$(this).toggleClass('active').next().slideDown().fadeIn("fast"); 
    	  }
    	  return false; //Prevent the browser jump to the link anchor
    });  	
    
    $('#contact-icons a img, a#get-download-pdf' ).map(function() {
      return $(this).tooltip({ effect: 'slide'} );
    });
    
    $('#get-email-form').click( function() { $.get('/contact', function(data) { 
        $('div.data').html(data); 
      });
    })

    $('#get-download-pdf').click( function() { $.get('/resume', function(data) { 
        $('div.data').html(data); 
      });
    })
    
    $('#get-about-me').click( function() { $.get('/about_orion', function(data) { 
        $('div.data').html(data);
        App.bindBehaviors();
      })
    });
    
    $('#get-video-portfolio').click(function() {
      $.get('/video', function(data){
        $('div#pagecontent').html(data); })
    })
    
    $('#get-portfolio').click(function() {
        App.ajaxGetandLoader('/portfolio', 'div#pagecontent');
				
      }
    );
  }
  
  /* --  App Object  -- */
  
  App.checkIfMobile();
  App.bindBehaviors();
    
  $('#get-tools').click( function() { $.get('/tools', function(data) { 
      $('div#pagecontent').html(data);
    })
  });
  
  $('#get-gallery').click(function() { $.get('/portfolio', function(data) { 
      $('div#pagecontent').html(data);
    })
  });

  $('#get-about').click(function() { $.get('/about', function(data) { 
      $('div#pagecontent').html(data);
      return false;
    })
  });

  $('#get-posts').click(function() { 
			App.setWelcome();
			// App. SetWelcome needs to be set before...
			$.get('/posts', function(data) { 	
      $('div#pagecontent').html(data);
      return false;
			
    })
  });

	// $('div#pagecontent').ajaxStart(function() {
	// 	App.setWelcome();
	// })
	
  // $('#close-blog').tooltip();
  App.openDraw();
  App.closeDraw();
	
  // App.setWelcome();
  if($('.blogpost')) {
		$('.blogpost').masonry({
			isAnimated: true,
			  animationOptions: {
			    duration: 750,
			    easing: 'linear',
			    queue: false
			  }
		})
	}
});

// 
