Creative = {

	projectDefaults : {   row:2,
                        per_page:8,
                        paging:'ajax',
                        callback:'Creative.pageProjects'
                      },
                      
  popInstance    : null,

  changeProjectsOrder : function( creative_id, browse ) {
    var defCopy          = Creative.projectDefaults;
    defCopy.browse	     = browse;
    defCopy.creative	   = creative_id;

    Creative.pageProjects( defCopy );
  },
  
  pageProjects : function(params) {
		
    var param_str = $H(params).toQueryString();
    
    var a = new Ajax.Request('/projects', {
			method : 'get',
      parameters : param_str,
      onSuccess : function( transport ) {
      	$('profile-projects').update(transport.responseText);
        Creative.loadBackgrounds();
      }
		});
	},
  
  inviteToCircle : function(id) {

    var pop_instance = PopUpMgr.add();
    
    if (typeof(AjaxList) == 'undefined') {
      setTimeout('Creative.inviteToCircle('+id+')',100);
      return;
    }
    
    var params   = $H({id:id});
    var paramStr = params.toQueryString();

    var a = new Ajax.Request('index.php/profile/inviteToCircle', {
      parameters : paramStr,
      onSuccess : function(transport) {
        PopUpMgr.update(pop_instance, transport.responseText);
      }
    });
  },
  
  inviteToIC : function(inviting) {
    var selected = $('search_circle_selections').select('li');
    if (selected.length > 0) {
      search_circles.removeSelection(selected[0],true);
    }
    
    if (inviting) {
      search_circles.makeForeignSelection($F('inner_circle_id'), $F('inner_circle_name'));
    }
  },
  
  checkCircleInvite : function() {
    if (this.getNumSelected() >= this._limit) {
      this._field.disabled = true;
      this._field.value    = 'You may only invite a user to one circle at a time.';
    } else {
      this._field.disabled = false;
      this.refocusField();
      $('search_circle_field').setStyle({border:'solid 0px #FF0000'});
      $('search_circle_fieldError').hide();
    }
    PopUpMgr.position(PopUpMgr.getInstance('pop'));
  },
  
  sendCircleInvite : function() {
    var validation = new ValidationMgr();
    var circle_id  = search_circles.getDelimited();
    validation.add('creative_id','Int',$H({required:1}));
    validation.add('search_circle_field','Generic',$H());
    
    var validForm = validation.validate();
    
    if (circle_id == '') {
      $('search_circle_field').setStyle({border:'solid 2px #FF0000'});
      validForm = false;
    }
    

    
    if (!validForm) { return false }
    
    var pop_instance = PopUpMgr.getInstance('pop');
    var params = $H({
      search_circle_field : circle_id,
      creative_id : $F('creative_id')
    });
    
    $('submitInvite').hide();
    $('sendingInvite').show();
    
    var a = new Ajax.Request('/index.php/circles/sendCreativeInvite', {
      parameters : params.toQueryString(),
      onSuccess : function(transport) {
        var myJSON = jsonTransport(transport);
        if (myJSON.valid == 'no') {
          $('sendingInvite').hide();
          $('submitInvite').show();
          validation.handleExtendedErrors(myJSON);
          PopUpMgr.position(pop_instance);
          return;
        }
        if (myJSON.valid == 'yes') {
          PopUpMgr.update(pop_instance, myJSON.html);
        }
      }
    });
    
    return false;
  },
  
  report : function(id) {
    var pop_instance = PopUpMgr.add();
    
    if (typeof(Validation) == 'undefined') {
      setTimeout('Creative.report('+id+')',100);
      return;
    }
    
    var params   = $H({id:id});
    var paramStr = params.toQueryString();

    var a = new Ajax.Request('/index.php/profile/report', {
      parameters : paramStr,
      onSuccess : function(transport) {
        PopUpMgr.update(pop_instance, transport.responseText);
      }
    });
  },
  
  sendReport : function() {
    var validation = new ValidationMgr();
    validation.add('creative_id','Int',$H({required:1}));
    validation.add('spam_reason','Generic',$H({required:1}));
    var validForm = validation.validate();
    if (!validForm) { return false }
      
    var pop_instance = PopUpMgr.getInstance('pop');
    var params = $H({});
    params.set('id',$F('creative_id'));
    params.set('spam_reason',$F('spam_reason'));
  
    $('submitFeedback').hide();
    $('sendingFeedback').show();
    
    new Ajax.Request('/index.php/profile/sendReport', {
      parameters : params.toQueryString(),
      onSuccess : function(transport) {
        
        var myJSON = jsonTransport(transport);
        if (myJSON.valid == 'no') {
          $('sendingFeedback').hide();
          $('submitFeedback').show();
          validation.handleExtendedErrors(myJSON);
          PopUpMgr.position(pop_instance);
          return;
        }
        if (myJSON.valid == 'yes') {
          PopUpMgr.update(pop_instance, myJSON.html);
        }
        
      }
    });
    
    return false;
  },
  
  contact : function(id) {
    
    var pop_instance = PopUpMgr.add();

    var a = new Ajax.Request('/index.php/profile/contact?id='+id, {
      method    : 'get',
      onSuccess : function(transport) {
        PopUpMgr.update(pop_instance, transport.responseText);
      }
    });
  },
  
  loginCircle : function() {
    var pop_instance = PopUpMgr.add($H({input:'<div class="popup-inner-pad">You must <a href="/Log_In?return=1">logged in</a> as a Creative Professional to invite a user to circles.<div style="text-align: center; padding-top: 5px;"><span class="blue hover-underline" onclick="PopUpMgr.removeAll();">Close</span></div></div>'}));
  },
  
  loginReport : function() {
    var pop_instance = PopUpMgr.add($H({input:'<div class="popup-inner-pad">You must <a href="/Log_In?return=1">logged in</a> as a Creative Professional to report a user.<div style="text-align: center; padding-top: 5px;"><span class="blue hover-underline" onclick="PopUpMgr.removeAll();">Close</span></div></div>'}));
  },
  
  loginWatchlist : function() {
    var pop_instance = PopUpMgr.add($H({input:'<div class="popup-inner-pad">You must <a href="/Log_In?return=1">logged in</a> as a Creative Professional or Watcher to add a user to your Watchlist.<div style="text-align: center; padding-top: 5px;"><span class="blue hover-underline" onclick="PopUpMgr.removeAll();">Close</span></div></div>'}));
  },
  
  isUserWatchlist : function() {
    var pop_instance = PopUpMgr.add($H({input:'<div class="popup-inner-pad">You may not add yourself to your Watchlist.<div style="text-align: center; padding-top: 5px;"><span class="blue hover-underline" onclick="PopUpMgr.removeAll();">Close</span></div></div>'}));
  },
  
  isUserCircle : function() {
    var pop_instance = PopUpMgr.add($H({input:'<div class="popup-inner-pad">You may not invite yourself to a circle.<div style="text-align: center; padding-top: 5px;"><span class="blue hover-underline" onclick="PopUpMgr.removeAll();">Close</span></div></div>'}));
  },
  
  isUserReport : function() {
    var pop_instance = PopUpMgr.add($H({input:'<div class="popup-inner-pad">You may not report yourself.<div style="text-align: center; padding-top: 5px;"><span class="blue hover-underline" onclick="PopUpMgr.removeAll();">Close</span></div></div>'}));
  },
  
  isUserMessage : function() {
    var pop_instance = PopUpMgr.add($H({input:'<div class="popup-inner-pad">You may not message yourself.<div style="text-align: center; padding-top: 5px;"><span class="blue hover-underline" onclick="PopUpMgr.removeAll();">Close</span></div></div>'}));
  },
  
  toggleWatchlist : function(id) {
    var params   = $H({id:id});
    var paramStr = params.toQueryString();
    
    var a = new Ajax.Request('index.php/profile/toggleWatchlist', {
      parameters : paramStr,
      onSuccess : function(transport) {
        var myJSON = jsonTransport(transport);
        if (myJSON.valid == 'yes') {
          $('add-to-watchlist').update(myJSON.txt);
        }
      }
    });
  },
  
  toggleWatcherWatchlist : function(id) {
    var a = new Ajax.Request('index.php/watcher/toggleCreativeWatchlist', {
      method	    : 'post',
      parameters	: 'watch_id='+id,
      onSuccess   : function(transport) {
        $('add-to-watchlist').replace(transport.responseText);
      }
    });
  },
  
  loadBackgrounds : function() {
    Event.observe(window,'load',Creative.loadBackgrounds); // run again on load for safari
    
    //first-child handling for ie6
    if ($('info-collection-wrap')) {
      var collections = $('info-collection-wrap').select('div.collection');
      
      if (collections.length > 0) {
        collections[0].setStyle({paddingTop:'16px',border:'0px',display:'inline-block'});
      }
    }

    // dynamic for as many modules as we want to have backgrounds
    $('left-info','right-info').each(function(info_el) { 
      $(info_el).select('div.info-wrap').each(function(el) {
        var section = $(el).select('div.section');
        if (section.length > 0) { section[0].setStyle({paddingTop:'0px'}); } // //first-child handling for ie6
        var wrap_id = $(el).id;
        var back_id = wrap_id.replace('wrap','background');
        $(back_id).clonePosition($(wrap_id));
        $(back_id).setStyle({zIndex:5});
        $(wrap_id).setStyle({position:'relative',zIndex:10});
      });
    });
  },
  
  loadOverlays : function() {
    Event.observe(window,'load',Creative.loadOverlays); // run again on load for ie6
    Overlays.init($('inner-circle-members').select('li'));

    Event.observe(window, 'scroll',Overlays.position);
    Event.observe(window, 'resize',Overlays.position);
  },
  
  contactMe : function(id) {
    $('submitButton').up().update('Sending...');
    var a = new Ajax.Request('/index.php/profile/contact?id=' + id, {
      method : 'post',
      parameters : $('sendFriendForm').serialize(true),
      onSuccess : function (transport) {
        var pop_instance = PopUpMgr.getInstance('pop');
        PopUpMgr.update(pop_instance, transport.responseText);
        PopUpMgr.position(pop_instance);
      }
    });
    return false;
  },
  
  toggleCircleLists : function() {
    var txt = ($('circles-toggle').innerHTML == 'View All') ? 'Hide' : 'View All';
    $('circles-toggle').update(txt);
    $('all-my-circles').toggle();
    $('some-my-circles').toggle();
    Creative.loadBackgrounds();
  },
  
  toggleTags  : function() {
    var txt = ($('tags-toggle').innerHTML == 'View All') ? 'Hide' : 'View All';
    $('tags-toggle').update(txt);
    $('all-tags').toggle();
    $('some-tags').toggle();
    Creative.loadBackgrounds();
  },
  
  pageComments : function(id, page) {
    var a = new Ajax.Request('/profile/comments', {
      method : 'get',
      parameters : 'id='+id+'&page='+page,
      onSuccess : function(transport) {
        $('commentsAjax').update(transport.responseText);
      }
    });
  },
  
  deleteCommentPop : function(id) {
    var params = $H({
      comment_id   : id
    });
    
    var pop_instance = PopUpMgr.add();
    
    var a = new Ajax.Request('/index.php/profile/deleteCommentPop', {
      parameters : params.toQueryString(),
      onSuccess : function(transport) {
        PopUpMgr.update(pop_instance,transport.responseText);
      }
    });
  },
  
  postComment : function() {
    var validation = new ValidationMgr();
    validation.add('comment_field','Generic',$H({required:1}));
    var validForm = validation.validate();
    if (validForm) {
    
      var params = $H({
        'comment_foreign_id' : $F('comment_foreign_id'),
        'comment_field'      : $F('comment_field')
      });
      
      $('submit_comment').hide();
      $('submitting_comment').show();
      
      var a = new Ajax.Request('/index.php/profile/postComment', {
        parameters : params.toQueryString(),
        onSuccess  : function(transport) {
          var myJSON = jsonTransport(transport);
          
          if (myJSON.valid == 'no') {
            $('submitting_comment').hide();
            $('submit_comment').show();
            validation.handleExtendedErrors(myJSON);
            return;
          }
          
          if (myJSON.valid == 'yes') {
            $('comment_field').value = '';
            $('commentBlock').hide();
            $('commentsAjax').update(myJSON.html);
            $('submitting_comment').hide();
            $('submit_comment').show();
          }
        }
      });
    }
    
  },
  
  deleteComment : function(id) {
    var params = $H({
      comment_id   : id
    });
    
    $('deleteCommentButtons').update('Deleting...');
    
    var a = new Ajax.Request('/index.php/profile/deleteComment', {
      parameters : params.toQueryString(),
      onSuccess : function(transport) {
        PopUpMgr.remove(PopUpMgr.getInstance('pop'));
        $('commentsAjax').update(transport.responseText);
      }
    });
  }
  
}; // Creative