/**
 * @author chrishenry
 */

Element.addMethods({
  
  _showV : function(element) {
    var el = $( element );
    el.setStyle({ visibility : 'visible' });
    return el;
  },

  _hideV : function(element) {
    var el = $( element );
    el.setStyle({ visibility : 'hidden' });
    return el;
  }
});

var Project = {
  
  baseUrl : window.protocol + window.host,
  
  addTo : function(action, url, title, from) {
  	var from = (from) ? from : 'top menu';
  	if (action == "bookmark") {
      this.bookmark(title, url);
  	} else {
  		window.open(VIRAL_ENDPOINT +'action='+action+'&url='+url+'&title='+title+'&from='+from, '_blank');
  	}
  },

  bookmark : function() {
		if (window.sidebar) { // Mozilla Firefox Bookmark
			window.sidebar.addPanel(title, baseLoc+url,"");
		} else if( window.external ) { // IE Favorite
			window.external.addFavorite(this.baseUrl+url, title); 
    } else { 
			$('toolError').innerHTML = "<p>&nbsp;</p><p>Safari does not support this feature.</p><p>Please hit CMD + D.</p>";
    }
  },
  
  appreciate : function (proj_id) {

    Stats.appreciate( 'project', proj_id );
    
    var sticker	    = $('proj-appreciate-sticker');
    sticker.src	    = 'http://behanceserved.com/images/appreciate_thanks.png';
    sticker.title	  = 'Thanks!';
    sticker.alt	    = 'Thanks!';
    
    $$('.proj-tools-appreciate').invoke('hide');
    
    var apprInfo = $('projApps' + proj_id);
    apprInfo.update( parseInt($('projApps' + proj_id).innerHTML) + 1)
    
  },
  
  positionInfo : function() {
    var info = $('proj-info');
    
    //get the position of the project info image.
    var img = $('proj-info-img');
    var imgOffset = img.cumulativeOffset();
    
    var posTop = imgOffset[1] + img.getHeight();
    var posLeft = ( imgOffset[0] + img.getWidth() ) - info.getWidth();

    info.setStyle({
      left : posLeft + 'px',
      top  : posTop + 'px'
    });

    info.show();
    
  },
  
  positionTools : function() {
    var tools = $('proj-tools');
    
    //get the position of the project tools image.
    var img = $('proj-tools-img');
    var imgOffset = img.cumulativeOffset();
    
    var posTop = imgOffset[1] + img.getHeight();
    var posLeft = ( imgOffset[0] + img.getWidth() ) - tools.getWidth();

    tools.setStyle({
      left : posLeft + 'px',
      top  : posTop + 'px'
    });

    tools.show();
  },
  
  positionBottomTools : function() {
    var tools = $('proj-tools-bottom');
    
    //get the position of the project tools image.
    var img = $('proj-tools-bottom-img');
    var imgOffset = img.cumulativeOffset();
    
    var posTop = imgOffset[1] + img.getHeight();
    var posLeft = imgOffset[0];

    tools.setStyle({
      left : posLeft + 'px',
      top  : posTop + 'px'
    });

    tools.show();
  },
  
  info : function( e ) { 
  
    // Get the x and y coords of the mouse.
    var x = Event.pointerX(e);
    var y = Event.pointerY(e);
  
    // Get the x and y ranges of the element.
    var infoElPos = this.getXYRange('proj-info-img');
    
    // Get the x and y ranges of the info popup
    var popupPos = this.getXYRange('proj-info');
    
    // See if the mouse is over the info Image
  	var overInfo = (  x >= infoElPos.xLeft &&	x <= infoElPos.xRight && 
                      y >= infoElPos.yTop  &&	y <= infoElPos.yBottom );
          
    // See if the mouse is over the info Popup
  	var overPopup = ( x >= popupPos.xLeft && x <= popupPos.xRight && 
                      y >= popupPos.yTop  && y <= popupPos.yBottom );
  
    if( overInfo ) {
    	this.openInfo();
  	} 
    
    if( !overInfo && !overPopup ) {
  		this.closeInfo();
    }
  
  },
  
  tools : function( e ) {
    // Get the x and y coords of the mouse.
    var x = Event.pointerX(e);
    var y = Event.pointerY(e);
  
    // Get the x and y ranges of the element.
    var toolsElPos = this.getXYRange('proj-tools-img');
    
    // Get the x and y ranges of the info popup
    var popupPos = this.getXYRange('proj-tools');
    
    // See if the mouse is over the info Image
  	var overTools = (  x >= toolsElPos.xLeft &&	x <= toolsElPos.xRight && 
                      y >= toolsElPos.yTop  &&	y <= toolsElPos.yBottom );
          
    // See if the mouse is over the info Popup
  	var overPopup = ( x >= popupPos.xLeft && x <= popupPos.xRight && 
                      y >= popupPos.yTop  && y <= popupPos.yBottom );
  
    if( overTools ) {
    	this.openTools();
  	} 
    
    if( !overTools && !overPopup ) {
  		this.closeTools();
    }
  },
  
  bottomTools : function( e ) {
    // Get the x and y coords of the mouse.
    var x = Event.pointerX(e);
    var y = Event.pointerY(e);
  
    // Get the x and y ranges of the element.
    var toolsElPos = this.getXYRange('proj-tools-bottom-img');
    
    // Get the x and y ranges of the info popup
    var popupPos = this.getXYRange('proj-tools-bottom');
    
    // See if the mouse is over the info Image
  	var overTools = (  x >= toolsElPos.xLeft &&	x <= toolsElPos.xRight && 
                      y >= toolsElPos.yTop  &&	y <= toolsElPos.yBottom );
          
    // See if the mouse is over the info Popup
  	var overPopup = ( x >= popupPos.xLeft && x <= popupPos.xRight && 
                      y >= popupPos.yTop  && y <= popupPos.yBottom );
  
    if( overTools ) {
    	this.openBottomTools();
  	} 
    
    if( !overTools && !overPopup ) {
  		this.closeBottomTools();
    }
  },
  
  getXYRange : function(el) {
    var el	= $(el);
  	var c	  = el.cumulativeOffset();
  
    return infoElPos = {
      xLeft	    : c[0],
      xRight	  : c[0] + el.getWidth(),
      yTop	    : c[1],
      yBottom	  : c[1] + el.getHeight()
    };
  },
  
  openInfo : function() {
    Project.positionInfo();
  	$('proj-info')._showV();
  	var imgObj = $('proj-info-img');
  	var imgSrc = imgObj.src;
  	imgObj.src = imgSrc.replace("Off", "On");
  },
  
  closeInfo : function() {
  	$('proj-info')._hideV();
  	var imgObj = $('proj-info-img');
  	var imgSrc = imgObj.src;
  	imgObj.src = imgSrc.replace("On", "Off");
  },
  
  openTools : function() {
  	Project.positionTools();
    $('proj-tools')._showV();
  	var imgObj = $('proj-tools-img');
  	var imgSrc = imgObj.src;
  	imgObj.src = imgSrc.replace("Off", "On"); 
  },
  
  closeTools : function() {
  	$('proj-tools')._hideV();
    $$('proj-tools-bookmarks').invoke('hide');
  	var imgObj = $('proj-tools-img');
  	var imgSrc = imgObj.src;
  	imgObj.src = imgSrc.replace("On", "Off");
  },
  
  openBottomTools : function() {
  	Project.positionBottomTools();
    $('proj-tools-bottom')._showV();
  	var imgObj = $('proj-tools-bottom-img');
  	var imgSrc = imgObj.src;
  	imgObj.src = imgSrc.replace("Off", "On"); 
  },
  
  closeBottomTools : function() {
  	$('proj-tools-bottom')._hideV();
    $$('proj-tools-bookmarks').invoke('hide');
  	var imgObj = $('proj-tools-bottom-img');
  	var imgSrc = imgObj.src;
  	imgObj.src = imgSrc.replace("On", "Off");
  },
  
  
  getSendPopup : function(proj_id) {
    
    Modalbox.show('/tools/send?proj_id='+proj_id, {title: 'Send to a friend', width: 600});
  },

  send : function() {
    
    var proj_id = $F('proj_id');
    var params = $H({
      yourName	  : $F('yourName'),
      yourEmail	  : $F('yourEmail'),
      shareEmails : $F('shareEmails')
    }).toQueryString();
    
    new Ajax.Request('/tools/sendMail?proj_id='+proj_id, {
        method	    : 'post',
        parameters	: params,
        onComplete : function() {
          Modalbox.hide();
        }
    });
  },
  
  getContactOwnerPopup : function(proj_id) {
    
    Modalbox.show('/tools/contact?proj_id='+proj_id, {title: 'Contact the owners', width: 600});
  },
  
  contactOwners : function() {
    
    var proj_id = $F('proj_id');
    var params = $H({
      yourName	  : $F('yourName'),
      yourEmail	  : $F('yourEmail'),
      yourMessage : $F('yourMessage')
    }).toQueryString();
    
    new Ajax.Request('/tools/contactOwners?proj_id='+proj_id, {
        method	    : 'post',
        parameters	: params,
        onComplete : function() {
          Modalbox.hide();
        }
    });
  }
  
}




function closeTools() {
	$('proj-info')._hideV();
	var imgObj = $('proj-info-img');
	var imgSrc = imgObj.src;
	imgObj.src = imgSrc.replace("On", "Off");
}



function openStats() {
	$('statOptions')._showV();
	var imgObj = $('infoImg');
	var imgSrc = imgObj.src;
	imgObj.src = imgSrc.replace("Off", "On");
	
}

function closeStats() {
	$('statOptions')._hidev();
	var imgObj = $('infoImg');
	var imgSrc = imgObj.src;
	imgObj.src = imgSrc.replace("On", "Off");
}

Event.observe(window, 'load', function() {
   Event.observe(document.body, 'mousemove', function( event ) { Project.info( event ); } );
   Event.observe(document.body, 'mousemove', function( event ) { Project.tools( event ); } );
   Event.observe(document.body, 'mousemove', function( event ) { Project.bottomTools( event ); } );
}); 





