
var maxLimitText;
var mask_element;
function popover(link)
{
	var ajaximg= "<div class='content_wr' style='width:220px;margin:auto;'><img src='/images/ajax-loader.gif' style='margin:auto;' /></div>";
	if(!jQuery('#mask').length){
	  mask_div = "<div id='mask'>&nbsp;</div>";
      jQuery('body').append(mask_div);
	}
	
    jQuery('#popover_td').innerHTML = ajaximg;
	
  jQuery.post(link, function(data){
        jQuery('#popover_td').html(data);
        jQuery('#popover_div').fadeIn("slow");
       // test();
  });
  return false;
}

function load_default_popup(title){
    var popup_content = '<div class="popupouter"><div class="popupinner"><div class="headerblue"><span class="float_left" style="text-transform: capitalize;" >' + title + '</span> <a href="#" class="imgclose" onclick="popout();return false;"><img src="/images/clear.gif" alt="cross" /></a></div><div id="addhelp_contentpopup" class="addhelp_contentpopup"><span class="popup_loading">Loading...</span></div></div></div>';
    if(!jQuery('#mask').length){
	  mask_div = "<div id='mask'>&nbsp;</div>";
      jQuery('body').append(mask_div);      
	}
    jQuery('#popover_td').html(popup_content);
    jQuery('#popover_div').fadeIn("slow");
    jQuery('#mask').fadeIn("slow");
    jQuery("#popover_container").css({width:'608px'});
    jQuery(".popupouter").css({width:'535px'});
    jQuery(".popupinner").css({width:'533px'});
    jQuery(".headerblue").css({width:'518px'});
    jQuery("#addhelp_contentpopup").css({width:'504px'});    
    jQuery("#popover_div").css({position:'fixed'});
}

function popover_V2(link, title){
    load_default_popup(title);
  jQuery.post(link, function(data){
        jQuery('.addhelp_contentpopup').html(data);
        jQuery('#addhelp_contentpopup').hide();
        jQuery('#addhelp_contentpopup').slideDown();
        //jQuery('#mask').fadeIn("slow");
       // test();
  });
  return false;
}

function profile_popover_V2(link, title){
    load_default_popup(title);
        jQuery("#popover_container").css({width:'945px'});
        jQuery(".popupouter").css({width:'945px'});
        jQuery(".popupinner").css({width:'943px'});
        jQuery(".headerblue").css({width:'928px'});
        jQuery("#addhelp_contentpopup").css({width:'917px'});
        jQuery("#popover_div").css({position:'absolute'});       
        
        if(link.indexOf('everyone')>-1){
            new_link =link.replace('everyone', 'friend');
            new_text = "Switch to friends view";
        }else{
            new_link =link.replace('friend', 'everyone');
            new_text = "Switch to everyone view";
        }
        jQuery('.headerblue').append('<a href="'+new_link+'" onclick="switchLink(this);return false;" style="flaot:left;margin-left:25px;font-size:12px;font-style:italic;color:#FFF;">'+new_text+'</a>');
  jQuery.post(link, function(data){
        jQuery('.addhelp_contentpopup').html(data);
        jQuery('#addhelp_contentpopup').hide();
        jQuery('#addhelp_contentpopup').slideDown();        
  });
        
  return false;
}

function switchLink(obj){
    jQuery('.addhelp_contentpopup').html('Loading...');
    jQuery.post(jQuery(obj).attr('href'), '', function(data){
            jQuery('.addhelp_contentpopup').hide();
            jQuery('.addhelp_contentpopup').html(data);
            jQuery('.addhelp_contentpopup').slideDown();
    });
    if(jQuery(obj).attr('href').indexOf('everyone')>-1){
        new_link='/usr/profileView?view=friend';
        jQuery(obj).html('Switch to friend view');
        jQuery('.headerblue').find('.float_left').html('Profile View to Everyone');
    }else{
        new_link='/usr/profileView?view=everyone';
        jQuery(obj).html('Switch to everyone view');
        jQuery('.headerblue').find('.float_left').html('Profile View to Friends');
    }
    jQuery(obj).attr('href', new_link);    
}

function checkLetters(obj){
	if(jQuery(obj).val().length >= maxLimitText)
		jQuery(obj).val(jQuery(obj).val().substr(0, maxLimitText));
    if(jQuery('#remaining_count').length)
	   jQuery('#remaining_count').html(maxLimitText - (eval(jQuery(obj).val().length)));
}

function msg_popover()
{
  if(!jQuery('#mask').length){
	  mask_div = "<div id='mask'>&nbsp;</div>";
      jQuery('body').append(mask_div);
	}
  jQuery('#popover_div').fadeIn("slow");
}

function deleteMsg(link, obj)
{	obj = "#" + obj;
  jQuery.post(link, '', function(data){
			jQuery(obj).fadeOut('slow');
			setTimeout('jQuery('+obj+').remove();', 1000);	
	});
  return false;
}

function test()
{
	var bottomAccordion = new accordion('vertical_container');
	var verticalAccordions = $$('.accordion_toggle');
	verticalAccordions.each(function(accordion) {
		$(accordion.next(0)).setStyle({
		  height: '0px',
		  display: 'none'
		});
	});
}

function popout()
{
  jQuery('#popover_div').fadeOut('slow');  
  if(jQuery('#mask').length)
	  setTimeout('jQuery("#mask").remove();', 500);
}

function sleep(millis)
{
  var date = new Date();
  var curDate = null;

  do { curDate = new Date(); }
  while(curDate-date < millis)
}

function deleteRecipient(toDelete)
{
  jQuery('#id_' + toDelete).html('');
  var str = jQuery('#alias').val().split(',');
  var i = 0;
  var newAlias='';
  for(i=0;i<str.length;i++)
  {
    if(toDelete != str[i])
    {
      if(newAlias == '')
      {
        newAlias = str[i] + ',';
      }
      else
      {
        newAlias +=  str[i] + ',';
      }
    }

  }
  jQuery('#alias').val(newAlias);
}

function addRecipient(vector,type)
{

  if(jQuery('#add_recip'+(type?'G':'')).val() == '') return;
  if(jQuery('#alias').val() == '')
  {
    jQuery('#alias').val( jQuery('#add_recip'+(type?'G':'')).val() + ',');
  }
  else
  {
    var str = jQuery('#alias').val().split(',');
    var i = 0;
    var values;
    for(i=0;i<str.length;i++)
    {
      if(str[i] == jQuery('#add_recip'+(type?'G':'')).val())
      {
        alert('You already choosed this recipient');
        return;
      }
      if(str[i] != '')
      {
        if(!values)
        {
          values = str[i];
        }
        else
        {
          values += ',' + str[i];
        }
      }
    }
    jQuery('#alias').val( values + ',' + jQuery('#add_recip'+(type?'G':'')).val());
  }
  var a = '<a href=\'#\' onClick=deleteRecipient(\'' + jQuery('#add_recip'+(type?'G':'')).val() + '\'); class = \'icon-link delete\'>';
  
  var b = vector[jQuery('#add_recip'+(type?'G':'')).val()] + '</a>';
 
  var str='#id_' + jQuery('#add_recip'+(type?'G':'')).val();
  jQuery(str).html(a + b);
}
function showSelect()
{
  jQuery('#add_select').show();
  jQuery('#add_link').hide();
}
function showSelectG()
{
  jQuery('#add_selectG').show();
  jQuery('#add_linkG').hide();
}
function hideSelect(vector)
{
  jQuery('#add_link').show();
  jQuery('#add_select').hide();
  addRecipient(vector, 0);
}
function hideSelectG(vector)
{
  jQuery('#add_linkG').show();
  jQuery('#add_selectG').hide();
  addRecipient(vector, 1);
}
function add_phone(cname)
{
  in_id = Math.round(Math.random()*10000);
  var input_name = Builder.node('input', { value: 'phone title', onfocus: "this.value='';", name: cname+'[phone_title][]', style: 'font-style:italic; color:#333; margin:1px;'} );
  var input_value = Builder.node('input', { value: 'phone number', onfocus: "this.value='';", name: cname+'[phone_number][]', style: 'font-style:italic; color:#333; margin:1px;'} );
  var remove_entry = Builder.node('span', { style: 'cursor:pointer;', onclick: "container = $('"+in_id+"'); $('"+cname+"_phones').removeChild(container); "}, '(-delete)')
  var container = Builder.node('div', {id: in_id});
  $(cname+'_phones').appendChild(container);
  container.appendChild(input_name);
  container.appendChild(input_value);
  container.appendChild(remove_entry);

}
function createCookie(name,value,days) {
  if (days) {
    var date = new Date();
    date.setTime(date.getTime()+(days*24*60*60*1000));
    var expires = "; expires="+date.toGMTString();
  }
  else var expires = "";
  document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
  var nameEQ = name + "=";
  var ca = document.cookie.split(';');
  for(var i=0;i < ca.length;i++) {
    var c = ca[i];
    while (c.charAt(0)==' ') c = c.substring(1,c.length);
    if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
  }
  return null;
}

tt_id = null;
tt_container = null;
tt_disabled = false;

function disable_tooltip()
{
  tt_disabled = true;
  createCookie('disable_tooltips', 'true', 30);
}

function hide_tooltip()
{
  if (tt_disabled) return false;
  if (tt_container)
  {
    tt_container.removeChild(document.getElementById(tt_id));
    tt_id = null;
    tt_container = null;
  }
}

function show_tooltip(tt_for, tt_title, tt_description)
{
  if (tt_disabled || (readCookie('disable_tooltips') == "true")) return false;

  if (tt_id != null)
  {
    hide_tooltip();
  }

  tt_id = Math.round(Math.random()*100000);
  tt_container = tt_for.parentNode;

  if (tt_for.id == 'menuMyNeighborhood')
  {
    tt_top = '_flipped';
    tt_left = '50px';
  }
  else
  {
    tt_top = '';
    tt_left = '-200px';
  }

  var tt_top = Builder.node('div', { style: "background:url(/images/tooltip_top"+tt_top+".png) no-repeat "+(tt_top != '' ? '-2px' : '')+"; height:41px;"});
  var tt_bottom = Builder.node('div', { style: "background:url(/images/tooltip_bottom.png) no-repeat; height:22px;"});
  var tt_middle = Builder.node('div', { style: "background:url(/images/tooltip_middle.png) repeat-y; padding:0 20px;"}, [Builder.node('strong', tt_title), Builder.node('br'), tt_description]);
  var tt_close = Builder.node('img', { src: '/images/tooltip_close.gif', style: 'cursor:pointer; position:relative; top:30px; right:15px;', align: 'right', onclick: 'hide_tooltip();'});

  var tt_main = Builder.node('div', { id: tt_id, style: 'width:292px; position:absolute; z-index:999; margin-top:-10px; margin-left:'+tt_left+';'});
  tt_top.appendChild(tt_close);
  tt_top.appendChild(Builder.node('a', { onclick: "hide_tooltip(); disable_tooltip(); return false;", style: 'color: blue; margin:0; padding:0; float:right; cursor:pointer; position:relative; top:30px; right:19px; display:block; '}, 'Disable Tooltips'));
  //tt_middle.appendChild(Builder.node('br', {clear: 'all', style: 'clear:both; line-height:0; font-size:0; height:0;'}));
  tt_main.appendChild(tt_top);
  tt_main.appendChild(tt_middle)
  tt_main.appendChild(tt_bottom);

  //  alert(tt_container.tagName);
  tt_container.appendChild(Builder.node('br', {clear: 'all', style: 'clear:both; line-height:0; font-size:0; height:0;'}));
  tt_container.appendChild(tt_main);
}
function detailDisableTool(image_tag)
{
  var toDisable = new Array('#detail-type', '#detail-value', '#privacy_1', '#privacy_0', '#privacy_null', '#add-close', '#add-continue', '#cancel-button');
  for(var i = 0; i<toDisable.length; i++)
  {
    jQuery(toDisable[i]).attr({disabled: false});
  }
  jQuery('#ajax-loader').html(image_tag);
  jQuery('#close').hide();
}
function detailEnableTool()
{
  var toEnable = new Array('#detail-type', '#detail-value', '#privacy_1', '#privacy_0', '#privacy_null', '#add-close', '#add-continue', '#cancel-button');
  for(var i = 0; i < toEnable.length; i++)
  {
    jQuery(toEnable[i]).attr({disabled: false});
  }
  jQuery('#ajax-loader').html('');
  jQuery('#close').show();

}
function detailSendValues()
{
  jQuery('#valueToSend').val(jQuery('#detail-value').val());
  jQuery('#detailToSend').val(jQuery('#detail-type').val());

}
function detailSetPrivacyValue(privacyValue)
{
  jQuery('#privacyToSend').val(privacyValue);
}

function zipsearch_form_submit(auth,maptext){
	var login='';
	var msg= '';
	//var maptext='shri';
	if(!maptext.length)
	{
		maptext = "Enlarged";
	}
	if(!auth){
		login ='<a href="/usr/signup" title="Sign up" style="float:right;margin-right:7px;font-size:14px;"><strong>ENROLL NOW</strong></a>';
		msg = '<span style="float:left;font-style:italic;margin-left:310px;">200 CLOSEST USERS TO YOU!</span>';
	}
    if(!jQuery('#mask').length){
	  mask_div = "<div id='mask'>&nbsp;</div>";
      jQuery('body').append(mask_div);
	}
	jQuery('#popover_td').html('<div class="popupouter" style="width:880px;"><div style="width:878px;" class="popupinner"><div style="width:863px;" class="headerblue"><span class="float_left" style="text-transform: capitalize;" >'+maptext+'</span> <a href="#" class="imgclose" onclick="custom_popout();return false;"><img src="/images/clear.gif" alt="cross" /></a></div><div id="popup_map" class="addhelp_contentpopup"><span class="popup_loading">Loading...</span></div></div></div>');
	jQuery('#popup_map').html(jQuery('#neighborhoodMap'));
	jQuery('#neighborhoodMap2').html(jQuery('#popup_map').html());
	para = {height:'497px', width:'850px'}    
	jQuery('#neighborhoodMap').css(para);
	jQuery('.global_popup').css({width:'850px', height:'525px'});
	jQuery('.global_popup_body').css({width:'auto', padding:'0'});
	jQuery('.popupheaderbg').css({width:'auto'});
	
	jQuery('#popover_div').fadeIn('slow');
	return false;
}

function custom_popout(){
	jQuery('#neighborhoodMap2').html('');
	jQuery('#neighborhoodMap2').append(jQuery('#neighborhoodMap'));
	var hei ='320px';
	if(document.getElementsByName('mapfrm').length > 0)
		hei =  newUi ?'340px':'426px';
	jQuery('#neighborhoodMap').css({height:hei});
    
    jQuery('#neighborhoodMap').css({width:'411px'});
	jQuery('.popover-header').css({width:'540px'});
	jQuery('.popover').css({width:'550px', height:'auto'});
	jQuery('.pop_gray_out').css({width:'600px', height:'auto'});
		
	popout();
}