function PopupCenter(pageURL, title,w,h) {
var left = (screen.width/2)-(w/2);
var top = (screen.height/2)-(h/2);
var targetWin = window.open (pageURL, title, 'toolbar=no, location=no, directories=no, status=no, menubar=no, scrollbars=no, resizable=no, copyhistory=no, width='+w+', height='+h+', top='+top+', left='+left);
} 

function toggleTextHint(evt, fld, _txtHnt){
    _e = evt.keyCode || evt.type;
    if(_e=='focus'){
    if(fld.value == _txtHnt){
        fld.value = ''; $(fld).removeClass('text-hint'); // css class for design; ex italic or greyed color text
    }} else if(_e=='blur'){
        if(fld.value == '' || fld.value == _txtHnt){
            $(fld).addClass('text-hint'); fld.value = _txtHnt;
    } } 
}
function changeQuestion(){
	$(".survey").fadeOut();
	$.ajax({
		url:baseUrl+"ajax_utils/change_survey_question.php",
		type:"get",
		success:function(html){
			$(".survey").html(html).fadeIn();
		}
	});
}  

function submitSurvey(){
	if($("#survey_form input:radio:checked").val() == undefined && $("#survey_form input:text").val() == ""){
		return; //no values was selected
	}
	var ownanswer = $("#survey_form input:text").val() != "" ? true: false;
	if(ownanswer){
			var answer = $("#survey_form input:text").val();
	}else{
		var answer = $("#survey_form input:radio:checked").attr("xml:id");
	}


	var sid = $("#survey_form").attr('xml:sid'); 
	var data = "ajax=1&answer="+answer+"&own_answer="+ownanswer+"&sid="+sid;
		changeQuestion();
	$.ajax({
		url:baseUrl+"ajax_utils/survey_confirm.php",
		type:"post",
		data:data,
		success:function(html){
		}
	});
	PopupCenter(baseUrl+"ajax_utils/survey_confirm.php?sid="+sid,'Survey Answers',500, 300);

	

}



function isValidEmail(val)
{   var re=new RegExp(/^[a-zA-Z0-9]([a-zA-Z0-9_\-\.]*)@([a-zA-Z0-9_\-\.]*)(\.[a-zA-Z]{2,4}(\.[a-zA-Z]{2}){0,2})$/)

    return (!re.test(val)) ? false : true;
}

function isValidUrl(url) {
    var re = new RegExp("^[ftp://|http://|:https://]*[A-Za-z0-9-_]+\\.[A-Za-z0-9-_%&\?\/.=]+$");
    return (re.test(url)) ? true : false;
}

function isEmpty(text) {
   return text.replace(/^\s+/g, '').replace(/\s+$/g, '').length==0 ? true : false;
}

/* fetch new image code for captcha*/
function getNewSecurityCode(imgId, sess) {
	img = document.getElementById(imgId);
	img.src = baseUrl + 'include/securimage/securimage_show.php?sesscode=ADCONTACT&ts=' + new Date().getTime();
}

function become_a_fan(business_id) {
  var business_id;
  $.get(
      baseUrl+'ajax_utils/become_a_fan.php',
      {business_id:business_id},
      function(response) {
        if (response=='1') {
          $('.fan_button'+business_id).removeClass('is_not_fan').addClass('is_fan');
          $('.fan_button'+business_id+' span.count').text(parseInt($('.fan_button'+business_id+' span.count').text())+1);
        }
        else
          alert(response);
      }
    );
}

function unfan(business_id) {
  $.get(
      baseUrl+'ajax_utils/unfan.php',
      {business_id:business_id},
      function(response) {
        if (response=='1') {
          $('.fan_button'+business_id).removeClass('is_fan').addClass('is_not_fan');
          $('.fan_button'+business_id+' span.count').text(parseInt($('.fan_button'+business_id+' span.count').text())-1);
        }
        else
          alert(response);
      }
    );
}


Keyword_Creator = new function() {
           function _addValue(text) {
                _p = $(text).parents('.UI_Keyword_Creator');
                t = text.val();
                kw_vals = $(_p).find('.keyword_values');

                arr_kw = (kw_vals.val()!='') ? kw_vals.val().split(',') : [] ;

                arr_kw.push(t);
                kw_vals.val(arr_kw.join(','));

                // clear input
                //text.addClass('text-hint'); // make text hint grey again
                //text.val(text.attr('alt')); // show text hint from element's alt attribute
				text.val("");
				
                        $('<li></li>').appendTo($(_p).find('.keyword_list')).html('<span class="keyword_list_text">'+_trim(t)+'</span><a class="UI_Keword_Remove" onclick="Keyword_Creator.Remove(this, \''+_trim(t.replace("'","\\'"))+'\')" title="Remove keyword">[x]</a>');
                   }

               function _trim(string) {
                 return string.replace(/^\s*/, "").replace(/\s*$/, "");
               }

			   function _escape(string) {
					return string.replace("\\'", "");
			   }

        return {
            Add : function(el) {
               _p = $(el).parents('.UI_Keyword_Creator');
               input_text = _p.find('.UI_Input_Text');

               if(input_text.val()=='') {
                    alert('Please enter Keyword');
                    input_text.focus();
                    return false;
               }

               _addValue(input_text);
            },

            Remove : function(el, kw) {
               _p = $(el).parents('.UI_Keyword_Creator');
               kw_vals = $(_p).find('.keyword_values');
               arr_kw = kw_vals.val().split(',');
               arr_kw_temp = [];
               for(i=0; i<arr_kw.length; i++) {
                    if(_trim(arr_kw[i])!=kw) arr_kw_temp.push(arr_kw[i]);
               }

               $(el).parents('li').remove();
               kw_vals.val((arr_kw_temp));
            }
        }
    }

user_activity_flag = 1;
function set_last_activity_time() {
  setTimeout(
      function() {
        $.post(
            baseUrl+'ajax_utils/user-activity.php',
            { auth: user_activity_flag_hash,flag:user_activity_flag },
            function() {
              set_last_activity_time();
            }
          );
      }
      ,
      5000
    );
}
var idle_timeout;
function on_user_activity() {
  if (idle_timeout)
    clearTimeout(idle_timeout);

  user_activity_flag = 1;

  setTimeout(
      function() {
        user_activity_flag = -1;
      }
      ,
      60000
    );
}
if (window.user_activity_flag_hash!==undefined) {
  set_last_activity_time();
}

var set_idle_timeout;
$(window).mousemove(on_user_activity);


function textarea_update_height(obj) {
  var $obj = $(obj);
  var val = $obj.val();
  var char_count = val.length;
  var font_size = parseInt($obj.css('font-size'));
  var chars_per_line = Math.round(($obj.width()/font_size)*(50/31));
  var line_height = parseInt($obj.css('line-height'));
  var fittable_lines = Math.floor($obj.height()/line_height);
  var lines = val.match(/.+$/gm);
  var line_count = val.replace(/\s+$/g,"").split("\n").length;  
  //var line_count = lines.length || 1;

  for (x in lines) {
    if (lines[x].length>chars_per_line) {
      line_count+=Math.ceil(lines[x].length/chars_per_line);
    }
  }

  if (line_count > fittable_lines) {
    $obj.height($obj.height()+line_height);
  }

  var $form = $obj.parents('form');
  if ($form.length && !$form.hasClass('textarea-updating-height')) {
    $obj.addClass('textarea-updating-height');
    $form.submit(
        function() {
          $obj.css('height','');
        }
      );
    $form.addClass('textarea-updating-height');
  }
}


$(document).ready(
  function() {
    $('#login-password').hide().siblings('#login-pseudo-password').show()
    $('#login-pseudo-password').focus(
        function() {
          $(this).hide().siblings('#login-password').show()
          $(this).siblings('input[type=password]').focus();
        }
      )
      .click(function(){$(this).focus()});
    $('#login-password').blur(
        function() {
          if (!$(this).val())
            $(this).hide().siblings('#login-pseudo-password').show()
        }
      );
    $('#login-username')
      .focus(
          function(e) {
            var $obj = $(this);
            toggleTextHint(e,this,$obj.attr('xml:default_value'));
            $obj.addClass('editted');
          }
        )
      .blur(
          function(e) {
            var $obj = $(this);
            toggleTextHint(e,this,$obj.attr('xml:default_value'));
            if ($obj.val()==$obj.attr('xml:default_value'))
              $obj.removeClass('editted');
          }
        );
    $('.add-as-friend').click(
        function() {
          var $this = $(this);
		  $(this).html("Loading...");
          $.get(
              baseUrl+'ajax_utils/friend-invite.php',
              {user_id:$this.attr('xml:user_id'),auth:$this.attr('xml:auth')},
              function(response) {
                if (response=='1') {
                  $this.replaceWith("<span class='friend-request-sent'><span>Request sent</span></span>");
                }
                else
                  alert(response);
              }
            );
        }
      );
    $('form *[xml\\\:default_value]')
      .focus(
          function(e) {
            var $obj = $(this);
            toggleTextHint(e,this,$obj.attr('xml:default_value'));
          }
        )
      .blur(
          function(e) {
            var $obj = $(this);
            toggleTextHint(e,this,$obj.attr('xml:default_value'));
          }
        )
      .parents('form').submit(
        function() {
          $(this).find('*[xml\\\:default_value]').each(
              function() {
                var $this = $(this);
                if ($this.val()==$this.attr('xml:default_value')) {
                  $this.val('');
                }
              }
            );
        }
      );
  
	// user/member likes
	  $('.User_Like').click(function(e){
			// redirect if it's login/signup page
			if(this.href.match('/join|login|signup/')) return;
			$el = $(this);
			e.preventDefault();
			$.get(
				this.href,{ajax:1},
				function(response) {
					$el.parents('.ui_like_box').html(response);
				}				
			);
			
	  })
  
  
  }  
);

