
var ie4 = document.all;
var ns4 = document.layers;
var ns6 = document.getElementById && !document.all;
var olderBowser;

if (window.XMLHttpRequest) {
	olderBowser = false;
} else {
	olderBowser = true;
}

var scrolling_elements = Array();
var city_autocomp;
var city_autocomp_regexp = /(.*, [A-Z]{2}) - [0-9]+/;
var about_us_drop_options = [
	{caption:'Features',link_url:'http://www.socialpulse.com/page/features-cms55'},
	{caption:'Why Join?',link_url:'http://www.socialpulse.com/page/why-join-cms61'},
	{caption:'Social Capital',link_url:'http://www.socialpulse.com/page/social-capital-cms58'},
	{caption:'Personal Branding',link_url:'http://www.socialpulse.com/page/personal-branding-cms57'},
	{caption:'FAQ',link_url:'http://www.socialpulse.com/page/faq-cms60'},
	{caption:'Members',link_url:'http://www.socialpulse.com/friends/index.php?mode=member_list'},
	{caption:'Shopping',link_url:'http://www.socialpulse.com/friends/index.php?mode=celebrity&act=list&cat_id=10'},
	{caption:'Current Events',link_url:'http://www.socialpulse.com/friends/index.php?mode=events'}
]

function start_scroll_element(element_id, axes, amount, start_scroll){
	if(start_scroll){
		var html_element = document.getElementById(element_id);
		scrolling_elements[element_id] = html_element;
	}
	else{
		var html_element = scrolling_elements[element_id];
	}
	
	if(typeof(html_element)=='object' && html_element){
		var horizontal_scroll = 0;
		var vertical_scroll = 0;
		
		if(axes=='horizontal'){
			html_element.scrollLeft = html_element.scrollLeft+amount;
		}
		else{ // vertical
			html_element.scrollLeft = html_element.scrollTop+amount;
		}
		
		setTimeout('start_scroll_element("'+element_id+'", "'+axes+'", '+amount+')',30);
		
	}
}

function stop_scroll_element(element_id){
	scrolling_elements[element_id] = false;
}

function show_header_city_search(){
	var current_city_header_container = document.getElementById('current_city_header_container');
	var current_city_header_search = document.getElementById('current_city_header_search');
	if(!city_autocomp){
		city_autocomp = document.getElementById('city_autocomp');
	}
	
	current_city_header_container.style.display = 'none';
	current_city_header_search.style.display = 'block';
	
	city_autocomp.focus();
	city_autocomp.select();
}

function check_header_city_search(){
	if(!city_autocomp){
		city_autocomp = document.getElementById('city_autocomp');
	}
	
	if(city_autocomp && city_autocomp.value.match(city_autocomp_regexp)){
		var city = city_autocomp.value.replace(/ - [0-9]+$/, '');
//		Set_Cookie('city_name',city,null,'/');
//		window.location.reload();
		window.location.href = SITE_URL+'/city/'+escape(city)+'/';
		return;
	}
	
	setTimeout('check_header_city_search()', 500);
}

function formsubmit(type){
	document.profile.redir.value=type;
	document.profile.submit();
}

function unset_current_city(){
	Set_Cookie('city_name','',null,'/');
	window.location.reload();
}

function Set_Cookie(name,value,expires,path,domain,secure) {
	if(typeof(expires)!='number'){
		expires = 315360000000; // 10 years
	}
	
	var today = new Date();
	var expires_date = new Date( today.getTime() + (expires) );
	if(!domain){
		var temp = window.location.href.match(/https?:\/\/.*?([a-z0-9\-_]+\.[a-z]{2,4})(\/.*)?$/);
		domain = temp[1];
	}
	
    var cookieString = name + "=" +escape(value) + 
       ( (expires) ? ";expires=" + expires_date.toGMTString() : "") + 
       ( (path) ? ";path=" + path : "") + 
       ( (domain) ? ";domain=" + domain : "") + 
       ( (secure) ? ";secure" : "");
    document.cookie = cookieString;
}


function get_cookie(c_name){
	if (document.cookie.length>0){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){ 
			c_start=c_start + c_name.length+1; 
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		} 
	}
	return "";
}

function delete_cookie(name, path, domain) {
	if (get_cookie(name)){
		document.cookie = name + "=" +
		((path)?";path="+path:"") +
		((domain)?";domain="+domain:"") +
		";expires=Thu, 01-Jan-1970 00:00:01 GMT";
	}
}


//modify styles for IE
function makeScrollingBody(){
	if(ie4 && olderBowser){
		var html = document.getElementsByTagName('html');
		var bodyElem = document.getElementsByTagName('body');
		var picturePlaceholderDiv = document.getElementById('picturePlaceholderDiv').style;
		var wait_message_bg = document.getElementById('wait_message_bg').style;
		
		picturePlaceholderDiv.position='absolute';
		wait_message_bg.position='absolute';
		
		html[0].style.setAttribute('height','100%');
		html[0].style.setAttribute('overflow','auto');
		bodyElem[0].style.setAttribute('height','100%');
		bodyElem[0].style.setAttribute('overflow','auto');
	}
}

function makeUnScrollingBody(){
	if(ie4 && olderBowser){
		var html = document.getElementsByTagName('html');
		var bodyElem = document.getElementsByTagName('body');
		var picturePlaceholderDiv = document.getElementById('picturePlaceholderDiv').style;
		var wait_message_bg = document.getElementById('wait_message_bg').style;
		
		picturePlaceholderDiv.position='';
		wait_message_bg.position='';
		
		html[0].style.setAttribute('height','');
		html[0].style.setAttribute('overflow','');
		bodyElem[0].style.setAttribute('height','');
		bodyElem[0].style.setAttribute('overflow','');
	}
}

function hideWaitMessageBg(){
	hideElement('wait_message_bg');
}

function displayWaitMessageBg(){
	displayElement('wait_message_bg');
}


function hideElement(elementId){
	var wait_div=document.getElementById(elementId);
	if(wait_div){
		wait_div=wait_div.style;
		wait_div.display='none';
		wait_div.visibility='hidden';
	}
}

function displayElement(elementId){
	var wait_div=document.getElementById(elementId);
	if(wait_div){
		wait_div=wait_div.style;
		wait_div.display='block';
		wait_div.visibility='visible';
	}
}

function fieldTempValue(fieldElem, caption, restore){
	if(restore){
		if(!fieldElem.value){
			fieldElem.value = caption;
		}
	}
	else{
		if(fieldElem.value == caption){
			fieldElem.value = '';
		}
	}
}

function setFieldTempValue(fieldElem, caption, set_now){
	if(set_now){
		fieldElem.value = caption;
	}
	fieldElem.onfocus = function(){
		fieldTempValue(fieldElem,caption,false);
	}
	fieldElem.onblur = function(){
		fieldTempValue(fieldElem,caption,true);
	}
}

var selected_stars = 0;
function live_stars(active_stars, star_id_prefix, max_stars){
	for(var i=1; i<=max_stars; i++){
		var star = document.getElementById(star_id_prefix+i);
		var new_src = SITE_URL+'/images/icon/16/bookmark';
		if(active_stars < i){
			new_src += '_disabled';
		}
		new_src += '.png';
		
		star.src = new_src;
	}
}

function return2selected_stars(star_id_prefix, max_stars){
	live_stars(selected_stars, star_id_prefix, max_stars);
}

var login_clicked = false;
function xlc_login_successful(){
	var cookie_name = 'xlc_red_'+XLC.lcjs_user_data;
	var redireted_already = get_cookie(cookie_name);
	if(typeof(redireted_already)!='string' || !redireted_already || login_clicked){
		var current_url = window.location.href;
		var login_url = SITE_URL+'/xlc.php?redirect_url='+escape(current_url)+'&key='+XLC.lcjs_user_data;
		window.location = login_url;
		Set_Cookie(cookie_name, '1');
	}
}

function xlc_logout_successful(){
	var cookie_name = 'xlc_red_'+XLC.lcjs_user_data;
	delete_cookie(cookie_name, '');
	
	var logout_url = site_root_stripped+'/index.php?mode=login&act=logout';
	window.location = logout_url;
}



function select_all(class_name){
	$('.'+class_name).attr('checked', true);
}

function select_none(class_name){
	$('.'+class_name).attr('checked', false);
}



function set_max_input_length(input_elem, max_length){
	input_elem.onkeyup = append_code2function(input_elem.onkeyup, function(){trim_input_value(input_elem, max_length)});
	input_elem.onchange = append_code2function(input_elem.onchange, function(){trim_input_value(input_elem, max_length)});
	
	trim_input_value(input_elem, max_length);
}

function trim_input_value(input_elem, max_length){
	if(input_elem.value.length > max_length){
		input_elem.value = input_elem.value.substring(0, max_length);
	}
}

function append_code2function(old_func, function2append){
	var reurn_function = function(evt){
		if(typeof(old_func)=='function'){
			try{
				old_func(evt);
			}
			catch(E){}
		}
		
		function2append(evt);
	}
	
	return reurn_function;
}



function open_inner_window(url, title, width, height){
	if(typeof(width)!='number'){
		width = 900;
	}
	if(typeof(height)!='number'){
		height = 600;
	}
	
	var iframe_elem = document.createElement('iframe');
	iframe_elem.width = width;
	iframe_elem.height = height;
	iframe_elem.setAttribute('frameborder', '0');
	iframe_elem.style.border = '0px';
	
	var frame_src = url;
	
	iframe_elem.src = frame_src;
	
	fan_show_transparent_bg();
	new fan_window(document.body, title, iframe_elem, {onclose:function(){fan_hide_transparent_bg();}});
}

function setRedirectUrl(redirect_url){
	if(typeof(redirect_url)!='string' || !redirect_url.match(/^https?:\/\/([a-z0-9_\-]+\.)+[a-z]{2,4}(\/.*)?$/)){
		redirect_url = window.location.href;
	}
	
	Set_Cookie('redirect_url',redirect_url,null, '/');
}

function admin_post_on_twitter(message){
	var iframe_elem = document.createElement('iframe');
	iframe_elem.width = 400;
	iframe_elem.height = 160;
	iframe_elem.setAttribute('frameborder', '0');
	iframe_elem.style.border = '0px';
	
	var frame_src = SITE_URL+'/index.php?mode=admin_post_on_twitter&message='+encodeURIComponent(message);
	
	var window_name = 'Post on Twitter';

	iframe_elem.src = frame_src;
	
	fan_show_transparent_bg();
	new fan_window(document.body, window_name, iframe_elem, {onclose:function(){fan_hide_transparent_bg()}});
	return false;
}

var current_topics_drop_options = [{"caption":"Arts","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Art_Artists-144"},{"caption":"Autos","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Automobile_Marine-99"},{"caption":"Banks","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Banks_Finance_Insurance-18"},{"caption":"Books","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Books-89"},{"caption":"Business","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Business_Groups_Organizations-448"},{"caption":"Celebrity","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Movies_Music_Film_Stars_Celebrities-1"},{"caption":"Children","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Students_Pupils-403"},{"caption":"Cities","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Cities-33"},{"caption":"Companies","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Corporations_Companies-19"},{"caption":"Education","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Education_Books_Reference_Students-88"},{"caption":"Entertainment","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Home_Entertainment_Gadgets-444"},{"caption":"Faith\/Religion","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Faith_Religion_Clergy-69"},{"caption":"Finance","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Economy_Finance-407"},{"caption":"Food","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Food_Grocery_Wine-31"},{"caption":"Games","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Sports_Athletics_Exercise-9"},{"caption":"Government","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Governments-72"},{"caption":"Green","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Nature_Environment-40"},{"caption":"Health","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Health_Medicine_Diseases-12"},{"caption":"History","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-History_Civics-420"},{"caption":"Jobs","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Jobs_Employment-122"},{"caption":"Media","link_url":"http:\/\/www.socialpulse.com\/friendsprofile-category-Media_Journalists_Events-4"},{"caption":"Movies","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Hollywood_Movies-64"},{"caption":"Music","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Music_Singers-3"},{"caption":"Newspapers","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Newspapers-14"},{"caption":"Non-Profit","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Organizations_Non_Profit_Charity-11"},{"caption":"People","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-People_Leaders_Legends-839"},{"caption":"Politics","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Politics_Elections-108"},{"caption":"Real Estate","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Real_Estate_Housing-28"},{"caption":"Schools","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-US_Schools-453"},{"caption":"Science","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Science_Technology_Computer-68"},{"caption":"Services","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Services_Utilities_Safety-13"},{"caption":"Television","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-TV_Shows_Serials_Sitcoms-133"},{"caption":"Shopping","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Shopping_Merchants-20"},{"caption":"Sports","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Sports_Athletics_Exercise-9"},{"caption":"Technology","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Science_Technology_Computer-68"},{"caption":"Travel","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Travel_Transportation_Tourism-42"},{"caption":"Universities","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-University-95"},{"caption":"World","link_url":"http:\/\/www.socialpulse.com\/friends\/profile-category-Countries-35"}];


/**
 * displays a drop menu
 * 
 * @param element menu_btn
 * @param Array menu_elements [{caption:'link 1', link_url:'http://example.com'}]
 * @param object menu_options {menu_align:'left', menu_columns:1, column_width:120}
 */
function costom_drop_menu(menu_btn, menu_elements, menu_options){
	var container = menu_btn.parentNode;
	container.style.position = 'relative';
	
	if(typeof(menu_options)!='object' || !menu_options){
		menu_options = {};
	}
	if(typeof(menu_options.xOffset)!='number'){
		menu_options.xOffset = menu_btn.offsetLeft;
	}
	if(typeof(menu_options.yOffset)!='number'){
		menu_options.yOffset = menu_btn.offsetTop + menu_btn.offsetHeight;
	}
	if(typeof(menu_options.column_width)!='number'){
		menu_options.column_width = 120;
	}
	if(typeof(menu_options.menu_columns)!='number' || menu_options.menu_columns<1){
		menu_options.menu_columns = 1;
	}
	
	var menu_container = document.createElement('div');
	menu_container.className = 'menu_container';
	menu_container.style.width = ''+(menu_options.column_width*menu_options.menu_columns)+'px';
	
	var menu_columns = Array();
	
	for(var i = 0; i < menu_options.menu_columns; i++){
		menu_columns[i] = document.createElement('div');
		menu_columns[i].style.left = ''+(i*menu_options.column_width)+'px';
		menu_columns[i].className = 'costom_drop_menu_column';
		menu_columns[i].style.width = ''+menu_options.column_width+'px';
		menu_container.appendChild(menu_columns[i]);
	}
	
	for(var i in menu_elements){
		var temp_menu_option = document.createElement('a');
		temp_menu_option.innerHTML = menu_elements[i].caption;
		
		if(typeof(menu_elements[i].link_url)=='string' && menu_elements[i].link_url){
			temp_menu_option.href = menu_elements[i].link_url;
		}
		
		if(typeof(menu_elements[i].onclick)=='function'){
			temp_menu_option.onclick = menu_elements[i].onclick;
		}
		
		var current_column = i % menu_options.menu_columns;
		
		if(current_column>0){
			temp_menu_option.style.borderLeft = '1px solid transparent';
		}
		
		menu_columns[current_column].appendChild(temp_menu_option);
	}
	
	
	var _self = this;
	this.menu_container = menu_container;
	
	/**
	 * hides this menu
	 */
	this.hide = function(){
		_self.menu_container.style.display = 'none';
	}
	
	menu_btn.hide_menu = this.hide;
	
	if(typeof(costom_drop_menu.active_menu)=='object' && costom_drop_menu.active_menu){
		try{
			costom_drop_menu.active_menu.hide();
		}
		catch(E){}
	}
	costom_drop_menu.active_menu = this;
	
	if(typeof(this.menu_container.style.display)!='string' || this.menu_container.style.display=='' || this.menu_container.style.display=='none'){
		this.menu_container.style.top = ''+menu_options.yOffset+'px';
		
		if(typeof(menu_options.menu_align)=='string' && menu_options.menu_align=='right'){
			this.menu_container.style.right = ''+(container.offsetWidth - (menu_btn.offsetLeft + menu_btn.offsetWidth))+'px';
		}
		else{
			this.menu_container.style.left = ''+menu_options.xOffset+'px';
		}
		
		this.menu_container.style.display = 'block';
	}
	else if(typeof(no_hide)!='boolean' || !no_hide){
		menu_btn.hide_menu();
	}
	
	this.distroy_timeout = null;
	this.menu_container.onmouseout = function(){
		_self.distroy_timeout = setTimeout(menu_btn.hide_menu, 1000);
	}
	this.menu_container.onmouseover = function(){
		try{
			clearTimeout(_self.distroy_timeout);
		}
		catch(E){}
	}
	_self.distroy_timeout = setTimeout(menu_btn.hide_menu, 5000);
	
	
	container.appendChild(menu_container);
}

var ask4avatar_window_obj;
function ask4avatar(){
	var iframe_elem = document.createElement('iframe');
	iframe_elem.width = 330;
	iframe_elem.height = 90;
	iframe_elem.setAttribute('frameborder', '0');
	iframe_elem.style.border = '0px';
	
	var frame_src = SITE_URL+'/index.php?mode=set_avatar';
	
	var window_name = 'Set avatar';

	iframe_elem.src = frame_src;
	
	ask4avatar_window_obj = new fan_window(document.body, window_name, iframe_elem, {});
	return false;
}

var ask4username_window_obj;
function ask4username(){
	var iframe_elem = document.createElement('iframe');
	iframe_elem.width = 330;
	iframe_elem.height = 90;
	iframe_elem.setAttribute('frameborder', '0');
	iframe_elem.style.border = '0px';
	
	var frame_src = SITE_URL+'/index.php?mode=set_username';
	
	var window_name = 'Set username';

	iframe_elem.src = frame_src;
	
	ask4avatar_window_obj = new fan_window(document.body, window_name, iframe_elem, {});
	return false;
}

function show_user_visits(p_id, container_id){
	var request_url = SITE_URL+'/index.php?mode=user_visits&p_id='+p_id+'&callback=?';
	$.getJSON(
		request_url,
		function(data){show_user_visits_callback(data, container_id)}
	);
}
function show_user_visits_callback(data, container_id){
	var user_visits = 1;
	
	if(typeof(data)=='number'){
		user_visits = data;
	}

	document.getElementById(container_id).innerHTML = user_visits;
}

function show_latest_members(clicked_link){
	clicked_link.href = 'http://www.socialpulse.com/friends/index.php?mode=member_list';
	clicked_link.onclick = function(){};
	
	$("#latest_members_container").slideDown("slow");
	
	return false;
}

function show_latest_profiles(clicked_link){
	clicked_link.href = 'http://www.socialpulse.com/friends/profile-category#latest_profiles';
	clicked_link.onclick = function(){};
	
	$("#latest_profiless_container").slideDown("slow");
	
	return false;
}