var doc;
var win;
var win2;
var par;

var list = [];
var existing_list = [];
var listNew = [];
var listDate = [];

var listings_map;
var mobile_browser;

if (!Array.prototype.indexOf)
{
  Array.prototype.indexOf = function(elt /*, from*/)
  {
    var len = this.length;

    var from = Number(arguments[1]) || 0;
    from = (from < 0)
         ? Math.ceil(from)
         : Math.floor(from);
    if (from < 0)
      from += len;

    for (; from < len; from++)
    {
      if (from in this &&
          this[from] === elt)
        return from;
    }
    return -1;
  };
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    };
  }
}

function closeWindow(event)
{
	var e = Event.element(event);
	Event.stop(event);

	while(e.nodeName != 'A') { e = e.parentNode; }

	$('refresh_url').value = e.readAttribute('href');

//	var win;
	if (par && par != '') {
		closeWindow2();
		//win = window.parent.win2
	} else {
		closeWindow1();
		//win = window.parent.win;
	}
	/*
	win.close();
	if (e.readAttribute('href') == '/reload') {
		window.parent.location.reload();
  } else if(e.readAttribute('href') != '#') {
		window.parent.location = e.href;
	}
	*/
}

function previewProfile(id, source, par, section)
{	
	source = '/source:'+source || '';
	par = par || false;
	section = section || false;
	
	if(par != false) {
		window.parent.viewPopup2('/profiles/preview/id:'+id+'/layout:popup'+source+'/par:'+par, 765, 710, section);
	} else {
		viewPopup('/profiles/preview/id:'+id+'/layout:popup'+source, 765, 710);
	}
	return;
}

function quickLink(event)
{
	var e = Event.element(event);
  if(e.nodeName == 'SPAN' || e.nodeName == 'IMG') { e = e.parentNode; }
  //while(e.nodeName != 'A') { e = e.parentNode; }
  if(e && ($(e).hasClassName('quick_link') || $(e).hasClassName('sidebar_quick_link_popup')))
  {
    // Load the href in a popup window
    Event.stop(event);
    viewPopup(e.href, e.readAttribute('popupWidth'), e.readAttribute('popupHeight'));
  }
}

var unload = 0;
document.observe('dom:loaded', function() {
	Event.observe(document.body, 'click', quickLink);
//	window.onunload = function() {
//		if(unload == 0) window.location = '/logout/session';
//	}

	if(ProtoCheck)
	{
		new ProtoCheck();
		new ProtoCheck({
			'errorClass':           'pc_small_error',
			'checkClass':           'pc_small_checkbox',
			'radioClass':           'pc_small_radiobutton',
			'checkOnClass':         'pc_small_check_checked',            // A checked checkbox
			'checkOffClass':        'pc_small_check_unchecked',          // An unchecked checkbox
			'checkOnDisabledClass': 'pc_small_check_checked_disabled',   // Disabled checked checkbox
			'checkOffDisabledClass':'pc_small_check_unchecked_disabled', // Disabled unchecked checkbox
			'checkOnErrorClass':     'pc_small_check_checked_error',
	        'checkOffErrorClass':    'pc_small_check_unchecked_error',
	        'radioOnErrorClass':     'pc_small_radio_checked_error',
	        'radioOffErrorClass':    'pc_small_radio_unchecked_error',
			'radioOnClass':         'pc_small_radio_checked',            // A checked radio button
			'radioOffClass':        'pc_small_radio_unchecked',          // An unchecked radio button
			'radioOnDisabledClass': 'pc_small_radio_checked_disabled',   // Disabled checked radio button
			'radioOffDisabledClass':'pc_small_radio_unchecked_disabled', // Disabled unchecked radio button
			'focusClass':           'pc_small_focus'                     // A checkbox/radio button with focus
		});
		new ProtoCheck({
			'errorClass':           'pc_orange_small_error',
			'checkClass':           'pc_orange_small_checkbox',
			'radioClass':           'pc_orange_small_radiobutton',
			'checkOnClass':         'pc_orange_small_check_checked',            // A checked checkbox
			'checkOffClass':        'pc_orange_small_check_unchecked',          // An unchecked checkbox
			'checkOnDisabledClass': 'pc_orange_small_check_checked_disabled',   // Disabled checked checkbox
			'checkOffDisabledClass':'pc_orange_small_check_unchecked_disabled', // Disabled unchecked checkbox
			'radioOnClass':         'pc_orange_small_radio_checked',            // A checked radio button
			'radioOffClass':        'pc_orange_small_radio_unchecked',          // An unchecked radio button
			'radioOnDisabledClass': 'pc_orange_small_radio_checked_disabled',   // Disabled checked radio button
			'radioOffDisabledClass':'pc_orange_small_radio_unchecked_disabled', // Disabled unchecked radio button
			'focusClass':           'pc_orange_small_focus'                     // A checkbox/radio button with focus
		});
		new ProtoCheck({
			'errorClass':           'pc_blue_error',
			'checkClass':           'pc_blue_checkbox',
			'radioClass':           'pc_blue_radiobutton',
			'checkOnClass':         'pc_blue_check_checked',            // A checked checkbox
			'checkOffClass':        'pc_blue_check_unchecked',          // An unchecked checkbox
			'checkOnDisabledClass': 'pc_blue_check_checked_disabled',   // Disabled checked checkbox
			'checkOffDisabledClass':'pc_blue_check_unchecked_disabled', // Disabled unchecked checkbox
			'radioOnClass':         'pc_blue_radio_checked',            // A checked radio button
			'radioOffClass':        'pc_blue_radio_unchecked',          // An unchecked radio button
			'radioOnDisabledClass': 'pc_blue_radio_checked_disabled',   // Disabled checked radio button
			'radioOffDisabledClass':'pc_blue_radio_unchecked_disabled', // Disabled unchecked radio button
			'focusClass':           'pc_blue_focus'                     // A checkbox/radio button with focus
		});
		new ProtoCheck({
			'errorClass':           'pc_blue_small_error',
			'checkClass':           'pc_blue_small_checkbox',
			'radioClass':           'pc_blue_small_radiobutton',
			'checkOnClass':         'pc_blue_small_check_checked',            // A checked checkbox
			'checkOffClass':        'pc_blue_small_check_unchecked',          // An unchecked checkbox
			'checkOnDisabledClass': 'pc_blue_small_check_checked_disabled',   // Disabled checked checkbox
			'checkOffDisabledClass':'pc_blue_small_check_unchecked_disabled', // Disabled unchecked checkbox
			'radioOnClass':         'pc_blue_small_radio_checked',            // A checked radio button
			'radioOffClass':        'pc_blue_small_radio_unchecked',          // An unchecked radio button
			'radioOnDisabledClass': 'pc_blue_small_radio_checked_disabled',   // Disabled checked radio button
			'radioOffDisabledClass':'pc_blue_small_radio_unchecked_disabled', // Disabled unchecked radio button
			'focusClass':           'pc_blue_small_focus'                     // A checkbox/radio button with focus
		});
		new ProtoCheck({
			'errorClass':           'pc_grey_small_error',
			'checkClass':           'pc_grey_small_checkbox',
			'radioClass':           'pc_grey_small_radiobutton',
			'checkOnClass':         'pc_grey_small_check_checked',            // A checked checkbox
			'checkOffClass':        'pc_grey_small_check_unchecked',          // An unchecked checkbox
			'checkOnDisabledClass': 'pc_grey_small_check_checked_disabled',   // Disabled checked checkbox
			'checkOffDisabledClass':'pc_grey_small_check_unchecked_disabled', // Disabled unchecked checkbox
			'radioOnClass':         'pc_grey_small_radio_checked',            // A checked radio button
			'radioOffClass':        'pc_grey_small_radio_unchecked',          // An unchecked radio button
			'radioOnDisabledClass': 'pc_grey_small_radio_checked_disabled',   // Disabled checked radio button
			'radioOffDisabledClass':'pc_grey_small_radio_unchecked_disabled', // Disabled unchecked radio button
			'focusClass':           'pc_grey_small_focus'                     // A checkbox/radio button with focus
		});
		new ProtoCheck({
			'errorClass':           'pc_large_error',
			'checkClass':           'pc_large_checkbox',
			'radioClass':           'pc_large_radiobutton',
			'checkOnClass':         'pc_large_check_checked',            // A checked checkbox
			'checkOffClass':        'pc_large_check_unchecked',          // An unchecked checkbox
			'checkOnDisabledClass': 'pc_large_check_checked_disabled',   // Disabled checked checkbox
			'checkOffDisabledClass':'pc_large_check_unchecked_disabled', // Disabled unchecked checkbox
			'radioOnClass':         'pc_large_radio_checked',            // A checked radio button
			'radioOffClass':        'pc_large_radio_unchecked',          // An unchecked radio button
			'radioOnDisabledClass': 'pc_large_radio_checked_disabled',   // Disabled checked radio button
			'radioOffDisabledClass':'pc_large_radio_unchecked_disabled', // Disabled unchecked radio button
			'focusClass':           'pc_large_focus'                     // A checkbox/radio button with focus
		});
	}

	$$('#main_body_table a[rel]').each(function(element) {
		if(element.hasClassName('security_code_tip')) {
			new Tip(element, element.rel, {
				title: 'Security code',
				hook: { tip: 'bottomLeft', target: 'topRight', mouse: true},
				showOn: 'click',
				hideOn: 'click',
				closeButton: true,
				stem: 'bottomLeft'
			});
		} else {
			new Tip(element, element.rel, {
				hook: { tip: 'bottomLeft', target: 'topRight', mouse: true},
				stem: 'bottomLeft'
			});
		}
	});	
	
	$$('input[alt]').each(function(element) {
		if(element.hasClassName('tip_on_enter')) {
			new Tip(element, element.alt, {
				hook: { tip: 'bottomLeft', target: 'topRight', mouse: true},
				stem: 'bottomLeft',
				showOn: 'focus',
				hideOn: 'blur'
			});
		} else if(element.hasClassName('tip_to_left')) {
			new Tip(element, element.alt, {
				hook: { tip: 'bottomRight', target: 'topLeft', mouse: true},
				stem: 'bottomRight'
			});
		} else if(element.hasClassName('tip_to_right')) {
			new Tip(element, element.alt, {
				hook: { tip: 'bottomLeft', target: 'topRight', mouse: true},
				stem: 'bottomLeft'
			});

		} else if(element.hasClassName('tip_to_center')) {
			new Tip(element, element.alt, {
				hook: { tip: 'bottomMiddle', target: 'topMiddle', mouse: true},
				stem: 'bottomMiddle'
			});
		} else {
			new Tip(element, element.alt, {
				hook: { tip: 'bottomLeft', target: 'topRight', mouse: true},
				stem: 'bottomLeft'			
			});
		}
		$(element).removeAttribute('alt');
	});


	$$('img[alt]').each(function(element) {		
		if(element.hasClassName('tooltip')) {
			new Tip(element, element.alt, {
				hook: { tip: 'bottomLeft', target: 'topRight', mouse: true},
				stem: 'bottomLeft'
			});
		}
		$(element).removeAttribute('alt');
	});

	/* send an sms popup tooltips */
	$$('.recipients_area div[title]').each(function(element) {
		new Tip(element, element.title, {
			hook: { tip: 'bottomMiddle', target: 'topMiddle', mouse: true},
			stem: 'bottomMiddle'
		});
		$(element).removeAttribute('title');
	});
	$$('.bottom_buttons a[rel]').each(function(element) {
		new Tip(element, element.rel, {
			hook: { tip: 'bottomLeft', target: 'topRight', mouse: true},
			stem: 'bottomLeft'
		});
	});	

	$$('.recipient_checkboxes a[rel]').each(function(element) {
		new Tip(element, element.rel, {
			hook: { tip: 'bottomMiddle', target: 'topMiddle', mouse: true},
			stem: 'bottomMiddle'
		});
	});	

	$$('div[title], span[title]').each(function(element) {
		if(element.hasClassName('tip_to_right')) {
			new Tip(element, element.title, {
				hook: { tip: 'bottomLeft', target: 'topRight', mouse: true},
				stem: 'bottomLeft'
			});
		} else if (element.hasClassName('tip_to_follow')) {
			new Tip(element, element.title, {
				hook: false,
				stem: 'bottomLeft',
				offset: {x:0, y:-90},
				viewport: true
			});
		} else if (element.hasClassName('gayshare_home_tip')) {
			new Tip(element, element.title, {
				className: 'home-tip',
				hook: { tip: 'bottomLeft', mouse: true },
				stem: { position: 'bottomLeft', width: 12, height: 15 },
				offset: {x:-10, y:-10},
				viewport: true
			});
		} else if (element.hasClassName('tip_to_left')) {
			new Tip(element, element.title, {
				hook: { tip: 'bottomLeft', target: 'topLeft', mouse: true},
				stem: 'bottomLeft'
			});
		} else if (element.hasClassName('tip_on_banner')) {
			new Tip(element, element.title, {
				//hook: { tip: 'topRight', target: 'topLeft', mouse: true},
				stem: 'topLeft',
				viewport: true
			});
		} else {
			new Tip(element, element.title, {
				hook: { tip: 'bottomRight', target: 'topLeft', mouse: true},
				stem: 'bottomRight'
			});
		}
		$(element).removeAttribute('title');
	});	

	/* wizard navigation links */
	$$('.wizard_list a[rel]').each(function(element) {
		new Tip(element, element.rel, {
			hook: { tip: 'bottomRight', target: 'topLeft', mouse: true},
			stem: 'bottomRight'
		});
	});
	$$('.wizard_list li[title]').each(function(element) {
		new Tip(element, element.title, {
			hook: { tip: 'bottomRight', target: 'topLeft', mouse: true},
			stem: 'bottomRight'
		});
		$(element).removeAttribute('title');
	});

	//$$('ProfileEmailPublic')
});

//function setTooltips()
//{
	/*
	$$('input[rel]').each('function(element) {
		new Tip(element, element.rel);
	});
	*/
	/*
	$$('a.help').each( function(link) {
		new Tooltip(link, {mouseFollow: false, opacity: "1", backgroundColor: "#f0ffe0", borderColor: "#f0ffe0", textColor: "#65a22d", textShadowColor: "#FFF"});
	});
	$$("form input.help").each( function(input) {
		new Tooltip(input, {mouseFollow: false, opacity: "1", backgroundColor: "#f0ffe0", borderColor: "#f0ffe0", textColor: "#65a22d", textShadowColor: "#FFF"});
	});
	*/

/*
$$("ul .help").each( function(link) {
	new Tooltip(link, {mouseFollow: false});
});
$$("p .help").each( function(input) {
	new Tooltip(input, {backgroundColor: "#333", borderColor: "#333", textColor: "#FFF", textShadowColor: "#000"});
});
$$("form input.help").each( function(input) {
	new Tooltip(input, {backgroundColor: "#FC9", borderColor: "#C96", textColor: "#000", textShadowColor: "#FFF"});
});
*/
//}
//addLoadEvent(setTooltips);

function in_array(needle, haystack, strict) {
	// Checks if a value exists in an array
	// 
	// +    discuss at: http://kevin.vanzonneveld.net/techblog/article/javascript_equivalent_for_phps_in_array/
	// +       version: 804.1712
	// +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
	// *     example 1: in_array('van', ['Kevin', 'van', 'Zonneveld']);
	// *     returns 1: true
	var found = false, key, strict = !!strict;
	for (key in haystack) {
			if ((strict && haystack[key] === needle) || (!strict && haystack[key] == needle)) {
					found = true;
					break;
			}
	}
	return found;
}

/*
//patch for Opera
if (!Node) {
//if (!Node.prototype) {
	var node=document.createTextNode('');
	var Node = node.constructor;
}
// removeNode itself
if (window.Node) Node.prototype.removeNode = function(removeChildren) {
	var self = this;
	if (Boolean(removeChildren))
		return this.parentNode.removeChild( self );
	else {
		var r=document.createRange();
		r.selectNodeContents(self);
		return this.parentNode.replaceChild(r.extractContents(),self);
	}
}
*/

function viewPopup(theURL, theWIDTH, theHEIGHT) {
	win = new Window('modal', {className: "bare", width:theWIDTH, height:theHEIGHT, zIndex:150, opacity:1, draggable: true, resizable: false, showEffect:Effect.Appear, showEffectOptions: {duration:0.5}, hideEffect: Effect.Fade, hideEffectOptions: {duration:0.5}, url: theURL});
	
	if (mobile_browser)
	{
		win.showCenter(true);
	}
	else if(theHEIGHT >= 600) {
		left_offset = (screen.width / 2) - (theWIDTH);
		//left = (screen.width / 2) - (theWIDTH / 2);
		win.setLocation(40, left_offset);
		win.show(true);	
	} else {
		if(!listings_map) listings_map = 0;
		if(listings_map == 1)
		{
			// summary popup page to popup to the left and higher than the view profile popup so you can read the location above the profile popup
			//left = (screen.width / 2) - (theWIDTH / 2) - 135;
			
			left_offset = (screen.width / 2) - (theWIDTH);
			//left = (screen.width / 2) - (theWIDTH / 2);
			
			//var top = $('map').offsetTop - 22;
			var top  = 193;
			
			//top = 8;
			win.setLocation(top, left_offset);
			win.show(true);
		}
		else
		{
			win.showCenter(true);
		}
	}

	win.setDestroyOnClose();
	Window.keepMultiModalWindow = true;

	setObserve();
	return;
}
function viewPopup2(theURL, theWIDTH, theHEIGHT, section) {
	win2 = new Window('modal2', {className: "bare", width:theWIDTH, height:theHEIGHT, zIndex:150, opacity:1, draggable: true, resizable: false, showEffect:Effect.Appear, showEffectOptions: {duration:0.5}, hideEffect: Effect.Fade, hideEffectOptions: {duration:0.5}, url: theURL});
	if(theHEIGHT >= 600 && mobile_browser == 0) {
		var left_offset = ((screen.width / 2) - (theWIDTH / 2));
		//var left = (screen.width / 2) - (theWIDTH / 2) + (200);
		// shift 2nd popup window to the right 200px to cascade the windows. testing..
		//left = (screen.width / 2) - (theWIDTH / 2) + (200);
		var top = 40;
		
		if (section == 'listings_sneak_preview')
		{
			top = $('previews').offsetTop;
		}
		
		win2.setLocation(top, left_offset);
		win2.show(true);	
	} else {
		win2.showCenter(true);
	}
	win2.setDestroyOnClose();

	setObserve2();
	return;
}

function closeWindow1() {
	win.close();

	var refresh_url = $('refresh_url').value;

	switch(refresh_url)
	{
		case '/reload':
			window.location.reload();
			break;

		case '':
		case "#":
			break;

		default:
			window.location.href = refresh_url;
			break;
	}

	$('refresh_url').value = '';
}
function closeWindow2() { win2.close(); setObserve(); }

closeWin1Bind = closeWindow1.bindAsEventListener();
closeWin2Bind = closeWindow2.bindAsEventListener();

function setObserve() {
	$('overlay_modal').stopObserving('click', closeWin2Bind);
	$('overlay_modal').observe('click', closeWin1Bind);
}
function setObserve2() {
	$('overlay_modal').stopObserving('click', closeWin1Bind);
	$('overlay_modal').observe('click', closeWin2Bind);
}
function spinner_start(text)
{
	// Start the spinner
	$('overlay').show();
	$('overlay_image').show();
	if(text) $('overlay_text').update(text);
	$('overlay_text').show();
}
function spinner_stop()
{
	// Stop the spinner
	$('overlay').hide();
	$('overlay_image').hide();
	$('overlay_text').hide();
}

function convertDate(d)
{
	var now         = new Date(d);
	var month       = now.getMonth()+1;
	var day         = now.getDate();
	var year        = now.getYear();
	if(year < 2000) { year = year + 1900; }
	var dateString = year +
									 '-' +
									 month +
									 '-' +
									 day;
	return dateString;
}
function updateDate(p, name)
{
	var d = new Date(p.value);
	document.getElementById(name).value = convertDate(d);
}

function contactCustomerService() {
	viewPopup('/feedbacks/customer_service_popup', 475, 345);
	return;
}
