$("flashContainer.enterleave").mouseenter(function(){
      flashCallback("flUnpause");
    }).mouseleave(function(){
      flashCallback("flPause");
    });
    
if (document.location.href.indexOf("cyberena") != -1) {
  var g_debug = true;
} else {
  var g_debug = false;
}
  
var g_landing = false;      //global var set in influencer_page.php to indicate we are delaing with influencer landing page
var g_flashLoaded = false; //global var so social.js can know if flash has been loaded, NEED CLEAN UP so not global

function FastCompany() {
  var that = this;
  this.user = null;
  this.setUser = function(data){
	  this.user=data;
  };
  this.logo = function() {
    flashCallBack('flLogo');
  };
  
  this.updateHeader = function(){
   $("#header_text").hide();
   $("#logged_in_header").show();    
   $("#login_username").html(fc.user.first_name);
   $("#unique_url").html('<a href="' + fc.user.share_url + '" id="shortUrlHeader" onclick="fc.influence(); return false;">' + fc.user.share_url.substring(7, fc.user.share_url.length) + '</a>');
   $(".shortUrlPaste").html(fc.user.share_url);
  }
  
  this.flashReady = function() {
    $(".bottom_area").css("visibility", "visible");
    $(".header").css("visibility", "visible");
    $(".header").css("display", "block");
    $("#fwaBanner").hide();
    checkForUser();
    if (g_landing) {
      flashCallBack('flPause');
    }
  };
  
  this.toggleFAQ = function(faqID) {
    $("#" + faqID).toggle();
  };


  var g_atp_menu_showing = false;
  this.aboutTheProj = function() {
    g_atp_menu_showing = true;
    var x = utils.findPosX(document.getElementById("lnk_atp"));
    var y = utils.findPosY(document.getElementById("lnk_atp"));
    $("#atp").show().css("top", (y-156));
    $("#atp").show().css("left", x);
  };

//      utils.showModal("login_content");

//PROB NEEDS TO BE MOVED INTO SOME OTHER CLASS, PUTTING HERE FOR NOW
  this.fb_logged_in = function(data) {
    $("#header_text").hide();
    $(".modal_reg_link_area").hide();
    $("#logged_in_header").show();    
    $("#login_username").html(data["first_name"]);
    utils.hideModal("register_content");
    utils.hideModal("login_content");
    $("#lnk_my_inf").show();
    flashCallBack('flUnpause');
    data.pic = fbUserProfilePicture(data.id,'large');
    var toPost = JSON.stringify(data);
    $.post("/a/fbLogin", data, function(res) {  
  	    fc.setUser(res);
  	    fc.loadFlashUser();
  	    fc.updateHeader();
  	   //setWallHash(res.user_url,'self');
  	   fc.influence(null);
    }, 'json');
  };

  this.searchLetter = function(element) {
    window.location = "/search/l/" + element.innerHTML;
  };

  this.search = function() {
    flashCallBack('flPause');
    window.location = "/search/k/" + $("#search_for").val();
    return false;
  };
  
  this.faq = function() {
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    utils.showModal("faq_content");
    $("#atp").hide();
    g_atp_menu_showing = false;
  };

  this.rules = function() {
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    utils.showModal("rules_content");
    $("#atp").hide();
    g_atp_menu_showing = false;
  };

  this.measure = function() {
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    utils.showModal("measure_content");
    $("#atp").hide();
    g_atp_menu_showing = false;
  };

  this.video = function() {
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    utils.showModal("video_content");
    $("#atp").hide();
    g_atp_menu_showing = false;

    var flashvars = {};
    flashvars.v = "live";
    var params = {};
    params.wmode = "wmode";
    params.scale = "noscale";
    params.allowfullscreen = "true";
    params.allowscriptaccess = "sameDomain";
    var attributes = {};
    swfobject.embedSWF(
                    "/assets/swf/AboutVideoPlayer.swf", "about_video", 
                    "512px", "325px", "10", "/assets/swf/expressInstall.swf", flashvars, params, attributes);
  };

  this.aboutus = function() {
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    utils.showModal("aboutus_content");
    $("#atp").hide();
    g_atp_menu_showing = false;
  };
  
  this.cleanUrl = function(url){
	  cUrl = url.split('/');
	  if(cUrl.length > 1) cUrl= cUrl[(cUrl.length-1)]; 
	  return cUrl;
  }
  
  this.tweetthis = function(url) {
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    $("#tweetthis_thank_content").hide();
    $("#tweet_wrap").show();
    $('#twShareUrl').val(url)
    $('#twShortUrl').html()
    utils.showModal("tweetthis_content");
  };
  
  this.facebookthis = function(url) {
   flashCallBack('flPause');
   flashCallBack("flModalChange",true);
     utils.showModal("facebookthis_content");
    $("#fbShareUrl").val(url);
    $("#facebook_wrap").show();
	  $("#facebookthis_thank_content").hide();
  };
 
  this.emailthis = function(url) {
	    flashCallBack('flPause');
	    $("#emailthis_thank_content").hide();
	    $("#email_wrap").show();
	    utils.showModal("emailthis_content");
	    $("#emailShareUrl").val(url);
	  };
  
  this.privacy = function() {
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    utils.showModal("privacy_content");    
  };
  
  this.terms_and_conditions = function() {
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    utils.showModal("terms_condition_content");
  };

  this.profile = function() {
    flashCallBack('flPause');
    $("#dynamic_content").load('/profile', function() {
      utils.include_once("/assets/css/profile.css");
      utils.include_once("/assets/js/profile.js", "profile.init");
    });
   $("#atp").hide();
   g_atp_menu_showing = false;
  };

  this.register = function(fLoad) {
	  if (fLoad == null || typeof fLoad === 'undefined'){
		  fLoad = false;
	  }
  	if (this.user!=null && !fLoad) {
  		setWallHash(this.user.user_url,'self');
  	}
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    if (this.user == null || typeof this.user === 'undefined' || fLoad){
	    $("#dynamic_content").load('/registration', function() {
	    	if(fLoad == 2){
	    		reg.step_1();
	    		reg.step_2();
	    	}
        utils.include_once("/assets/css/registration.css");
        utils.include_once("/assets/js/registration.js", "reg.init");
	    });
	    $("#atp").hide();
	    g_atp_menu_showing = false;
    } else {
    	this.influence(null);
    }
  };

  this.uploadAgain = function(){
	  fc.register(2);
	 // reg.step_2();
  };
  
  this.login = function() {
    flashCallBack('flPause');
    flashCallBack("flModalChange",true);
    $("#dynamic_content").load('/login', function() {
      utils.include_once("/assets/css/login.css");
      utils.include_once("/assets/js/login.js", "login.init");
    });
  };
  


  this.influence = function(userUrl) {
	  if(g_flashLoaded){
		  var t = 'landing';
		  if (userUrl == null || typeof userUrl === 'undefined'){
			  t = 'self';
			  userUrl = fc.user.user_url;
		  }
			  
		  setWallHash(this.cleanUrl(userUrl),t);
	  } else {
		  var ustring = '';
		  if (userUrl != null){
			  ustring = '/view_modal/' + userUrl;
		  }
		  
	    flashCallBack('flPause');
	    flashCallBack("flModalChange",true);
	    $("#dynamic_content").load('/influencer' + ustring, function() {
	      utils.showModal("influence_content");
  		  if (!userUrl){
          $("#inf_prof_pic").attr("src", fc.user.images.small.src+"?"+(Math.floor(Math.random()*1100)));
        }
	    });
	  }
  };

  this.init = function() {
    $("#atp").hide();
    welcome.init();
    if (g_debug) {
      this.flashReady();
    }
    $("#shortUrlHeader").html($("#shortUrlHeader").html().replace("http://", ""))
  };
  
  this.tweetthis_submit = function() {
	  var postText =  $('#twPostText').val();
	  var shareUrl = $("#twShareUrl").val();
	  mekTwitter.api('statuses/update',{status:postText + ' ' + shareUrl});
    $("#tweet_wrap").hide();
    $("#tweetthis_thank_content").show();
  };
  
  this.facebookthis_submit = function() {
	  var postText =  $('#fbPostText').val();
	  var shareUrl = $("#fbShareUrl").val();
	  var mediaObj =	fbCreateSWFMedia(
				'http://influenceproject.fastcompany.com/assets/swf/FBVideoPlayer.swf',
				'http://influenceproject.fastcompany.com/assets/images/fb_video_thumb.jpg',
				'130','97','460','258');
	  var attach = fbCreateAttachment('Your influence',shareUrl,'Spread your influence!',[mediaObj]);
	  
	  fbPostToStream(postText + ' ' + shareUrl,attach,null,null,this.facebookthis_ok);  
  };
  
  this.facebookthis_ok = function(){
	  $("#facebook_wrap").hide();
	  $("#facebookthis_thank_content").show();
  }

  this.emailthis_submit = function() {
	  var toEmails =  $('#toEmails').val();
	  var fromEmail =  $('#fromEmail').val();
	  var shareUrl = $("#emailShareUrl").val();

	  $.post("/a/emailthis", {toEmails:toEmails,fromEmail:fromEmail,shareUrl:this.cleanUrl(shareUrl)}, function(data) {
		  if ( (data) && (data["errors"]) ) {
			  $("#emailthis_error").html(data['errors']);
		  } else {
			 
			  fc.emailthis_ok();
		  }
	    },'json');
	
  };
  
  this.emailthis_ok = function(){
	  $("#email_wrap").hide();
	  $("#emailthis_thank_content").show();
  } 
  
  var INFO_REQUIRED = "This information is required";
  
/*
  var errorMap = [];
  errorMap["Please enter your first_name"] = INFO_REQUIRED;
  errorMap["Please enter your last_name"] = INFO_REQUIRED;
  errorMap["Please enter your password"] = INFO_REQUIRED;
  errorMap["Please enter your passconf"] = INFO_REQUIRED;
  errorMap["Please enter your recaptcha_response_field"] = INFO_REQUIRED;
  errorMap["The password field does not match the passconf field."] = "Password does not match";
  errorMap["Please enter your email"] = "Not a valid email address";
  errorMap["Captch words do not match"] = INFO_REQUIRED;
*/

  var errorMap = [];
  errorMap["Please enter your first_name"] = "First name required";
  errorMap["Please enter your last_name"] = "Last name required";
  errorMap["Please enter your password"] = "Password required";
  errorMap["Please enter your passconf"] = "Password confirmation required";
  errorMap["Please enter your recaptcha_response_field"] = "Captcha field required";
  errorMap["The password field does not match the passconf field."] = "Password does not match";
  errorMap["Please enter your email"] = "Not a valid email address";
  errorMap["Captch words do not match"] = "Captcha words do not match";
    
  this.displayErrors = function(aErrors, drawAreaID) {
    var html = "";
    for (var i in aErrors) {
      if (typeof errorMap[aErrors[i]] != "undefined") {
        aErrors[i] = errorMap[aErrors[i]];
      }
      html += aErrors[i] + " | ";
    }
    html = html.substring(0, (html.length-3)); 
    
    $("#"+drawAreaID).css("visibility", "visible");
    $("#"+drawAreaID).html(html);
/* commenting out old error  hadnling method but saving in case they revert
    $(".field_error_marker").hide();
    $(".error_area").hide();
    for (var i in aErrors) {
      if (typeof errorMap[aErrors[i]] != "undefined") {
        aErrors[i] = errorMap[aErrors[i]];
      }
      $("#" + i + "_x img").show();
      $("#" + i + "_error_area").show();
      $("#" + i + "_error").html(aErrors[i]);
*/
//      $("#" + i).parent().append("<div class='field_error_msg field_error_marker' styl>" + aErrors[i] + "</div>");

  };


  this.helpMyInfluence = function(display) {
    if (display == "show") {
      var x = utils.findPosX(document.getElementById("lnk_myinf"));
      var y = utils.findPosY(document.getElementById("lnk_myinf"));
      $("#bubble2").show().css("top", (y-60));
      $("#bubble2").show().css("left", x-30);
    } else {
      $("#bubble2").hide();
    }
  };

  this.loadFlashUser = function(){
	  var wallObj = {
	  		  id: fc.user.uid,
			  fn: fc.user.first_name,
			  ln: fc.user.last_name,
			  b: unescape(fc.user.bio),
			  pu: "/influencer/view/" + fc.user.uid,
			  su: fc.user.share_url,
			  sc: fc.user.score,
			  p: fc.user.i_uid,
			  uu: fc.user.user_url,	  
			  i_medium: fc.user.images.medium.src,
			  i_medium_w: fc.user.images.medium.w,
			  i_medium_h: fc.user.images.medium.h,
			  i_large: fc.user.images.large.src,
			  i_large_w: fc.user.images.large.w,
			  i_large_h: fc.user.images.large.h
	  };
	  
	  flashCallBack('loadUser',wallObj);
   }
}

function setWallHash(user_url,target){
	window.location.hash = '/wall/focus/' + user_url + '/' + target;
}

var fc = new FastCompany();
