var mapSmall = null;
var currentATMbtn = null;

function zoomTo(geoArgs) {
	var geo = geoArgs.split(',');
	if (geo.length > 0) {
		var lat = geo[0];
		var long = geo[1];
		var zoomLevel = geo[2];
		//window.location = WEBPATH+"?lat="+lat+"&long="+long+"&zoomLevel="+zoomLevel;
		map.setCenter(new GLatLng(lat, long), parseInt(zoomLevel));
	}
}

function zoomMiniMapTo(geoArgs) {
	var geo = geoArgs.split(',');
	if (geo.length > 0) {
		var lat = geo[0];
		var long = geo[1];
		//var mapSmall = new GMap2(document.getElementById("map2"));
		mapSmall.setCenter(new GLatLng(lat, long), 14, G_HYBRID_MAP);
	}
}

function doLoadSmallMap() {
	if (typeof GBrowserIsCompatible == 'function' && GBrowserIsCompatible()) {
		$('map2').style.display='block';
		//var mapSmall = new GMap2(document.getElementById("map2"));
		//mapSmall.setCenter(new GLatLng(39, -95), 2, G_HYBRID_MAP);

		mapSmall = new google.maps.Map2(document.getElementById("map2"));
		mapSmall.setCenter(new GLatLng(39, -95), 2, G_HYBRID_MAP);
	}
}

function loadEditPicPopup(picID) {
	//Hide other popup types to avoid bugs;

	hidePopup();

	populatePopup('<h6 style="margin: 0;"><span>Edit picture</span></h6><div id=\"editPicContent\" style=\"width: 350px; text-align: center;\">Loading picture data...');

	new Ajax.Request('/photos/edit_pic/'+picID, {method: 'get', onSuccess: doLoadEditPic});

	showCenteredPopup(true);
}

var doLoadEditPic = function (t) {
	$('editPicContent').innerHTML = t.responseText;
	reCenterPopup();
	//popupLocked = true;
}

function hideEditPicPopup()
{
	hideDimmer(true);
}


function sendPictureData()
{
	var pic_id = document.getElementById('pic_id').value;
	var description = document.getElementById('description').value;
	var post_data = 'description='+ description;
	document.getElementById('edit_pic_msg').innerHTML = 'Loading please wait...';
	new Ajax.Request('/photos/edit_pic/'+pic_id, {method: 'post', postBody: post_data, onSuccess: doLoadEditPic});

}
function showOtherPopup(data, image) {
	hidePopup();
	if (typeof data == 'object') {
		imageCell = '';
		if (typeof image != 'undefined') {
			imageCell = "<td rowspan='6' style='width:auto !important;'><img src=\"/files/"+image+"\" /></td>";
		}else{
			image = '';
		}

		setOnScreen();

		modelProfileVisible = true;
		populatePopup('<table class="modelInfo"><tr>'+imageCell+(image!=''?'<td colspan="2">&nbsp;</td></tr><tr>':'')+'<th>Name:</th><td class="capitalise">'+data.name+'</td></tr>'+
		'<tr><th>Location:</th><td class="capitalise">'+data.location+'</td></tr>'+
		'<tr><th>Age:</th><td>'+data.age+'</td></tr>'+
		(image!=''?'<tr><td colspan="2">&nbsp;</td></tr>':'')+'</table>', true);
	}
}

function hideOtherPopup() {
	otherProfileVisible = false;
	hidePopup();
}

function showFavLocationsPopup(data)
{
	hidePopup();
	if ( typeof data == 'object' )
	{

		setOnScreen();
		modelProfileVisible = true;
		populatePopup('<table class="modelInfo"><tr><th>Name:</th><td class="capitalise">'+data.name+'</td></tr>'+
		'<tr><th>Type:</th><td>'+data.type+'</td></tr>'+
		'<tr><th>Category:</th><td>'+data.db+data.pb+data.atv+'</td></tr>'+
		'<tr><th>Fee:</th><td class="capitalise">'+data.paid+'</td></tr>'+
		'<tr><th>City:</th><td>'+data.city+'</td></tr>'+
		'<tr><th>'+(data.country == 'United States' ? 'State:' : 'Country:')+'</th><td>'+(data.country == 'United States' ? data.state : data.country)+'</td></tr>'+
		'</table>', true);
	}
}

var popImg = null;
function popMinifolioImg(img) {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();

	showDimmer();
	populatePopup('<img src="'+getImage('progress')+'" />');
	showCenteredPopup();

	popImg = new Image();
	popImg.onload = popMinifolioImgOnload;
	popImg.src = '/files/'+img;
}

function popMinifolioImgOnload() {
	//alert('loaded');
	populatePopup('<img src="'+popImg.src+'" alt="big image" style="cursor: pointer; position:relative" onclick="hideDimmer(true)" />');
	showCenteredPopup();
}

function showPicInfo( id, description )
{
	hidePopup();
	setOnScreen();
	modelProfileVisible = true;
	if ( id.length > 0 )
	{
		populatePopup('<table><tr><td valign="top" align="right">ID:</td><td width="100" valign="top" align="left">' + id + '</td></tr><tr><td valign="top" align="right">Description:</td><td valign="top" align="left">' + description + '</td></tr></table>', true);
	}
	else if ( description.length > 0 )
	{
		populatePopup('<table><tr><td valign="middle" align="left">' + description + '</td></tr></table>', true);
	}
}

function hidePicInfo()
{
	hidePopup();
	modelProfileVisible = false;

}

function showNewMemberPopup( data, image )
{
	showOtherPopup( data, image );
}

function hideNewMemberPopup()
{
	hidePopup();
	modelProfileVisible = false;

}

function ReportLocation( loc_id ) {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();
	modelProfileVisible = false;
	var Report_HTML;
	Report_HTML = '<h5 style="margin: 0 0 10px 0; line-height: 11px">Send a report</h5>';
	Report_HTML += '<table align="center" cellpadding="5" style="position: relative; z-index: 1000;"><tr><td align="left"><input name="report" type="radio" id="fake" /> Fake</td><td align="right"><input name="report" type="radio" id="spam" /> Spam</td></tr>';
	Report_HTML += '<tr><td colspan="2"><div style="padding-bottom: 2px;">Description:</div><textarea name="report_description" id="report_description" style="width: 165px; height: 65px;"></textarea></td></tr>';
	Report_HTML += '<tr><td><img src="' + WEBPATH + 'images/submit.gif" onclick="SendLocationReport(' + loc_id + ');" /></td><td><img src="' + WEBPATH + 'images/cancel_off.gif" onclick="hideReportLocationPopup();" /></td></tr></table>';


	populatePopup(Report_HTML);
	showDimmer();
	showCenteredPopup();
}

function hideReportLocationPopup() {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();
	hideDimmer();
}

function SendLocationReport( loc_id ) {

	var type = 0;
	var description = document.getElementById( 'report_description' ).value;

	if ( document.getElementById( 'fake' ).checked )
		type = 5;
	if ( document.getElementById( 'spam' ).checked )
		type = 6;
	if ( type == 0 )
	{
		hideReportLocationPopup();
		return;
	}

	var url = '/locations/report/' + loc_id + '/' + type + '/' + description;
	new Ajax.Request(url, {method:'get', onSuccess: locationHandlerFunc});
}

function locationHandlerFunc(t) {
	populatePopup(t.responseText);
	setTimeout('hideDimmer(true)', 2*1000);
}

function GetWindowSize()
{
  var myWidth = 0, myHeight = 0;
  var result = Array();
  if( typeof( window.innerWidth ) == 'number' ) {
    //Non-IE
    myWidth = window.innerWidth;
    myHeight = window.innerHeight;
  } else if( document.documentElement && ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
    //IE 6+ in 'standards compliant mode'
    myWidth = document.documentElement.clientWidth;
    myHeight = document.documentElement.clientHeight;
  } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
    //IE 4 compatible
    myWidth = document.body.clientWidth;
    myHeight = document.body.clientHeight;
  }

  result[0] = parseInt( myWidth );
  result[1] = parseInt( myHeight );

  return result;
}

function addToMyLocations( location_id )
{
	new Ajax.Request( '/locations/addtomy/' + location_id, { method: 'get', onSuccess: addToMyResult } );
}

function addToMyResult( t )
{
	var result = t.responseText.split( '#' );
	if ( result[1] == 'success' )
	{
		currentATMbtn.style.display = 'none';
		currentATMbtn = null;
	}
	hideModelPopup();
	hideOtherPopup();
	hideDimmer();
	populatePopup( "<div class=\"flashmsg " + result[1] + "\"><img src=\"http://breakabone.localhost/images/ico_" + result[1] + ".png\" alt=\"\" align=\"absmiddle\" class=\"msgIco\" />&nbsp;" + result[0] + "</div>" );
	showCenteredPopup();
	setTimeout( 'hidePopup()', 4000 );
}

var current_open_image = 0;
function popBookImg(img, image_id) {
	//Hide other popup types to avoid bugs;
	hideModelPopup();
	hideOtherPopup();

	current_open_image = image_id;

	showDimmer();
	populatePopup('<img src="'+getImage('progress')+'" />');
	showCenteredPopup();

	popImg = new Image();
	popImg.onload = popBookImgOnload;
	popImg.src = '/files/'+img;
}

function popBookImgOnload() {
	//alert('loaded');
	populatePopup('<img src="'+popImg.src+'" alt="big image" style="cursor: pointer; position:relative;" onclick="hideDimmer(true)" /><br /><div style=" width:100%; padding-top: 3px"><a href="javascript:;" onclick="hideDimmer(true)" style="float:left"><img src="/images/icons/close_off.gif" alt="Close" title="Report" onmouseover="this.src=\'/images/icons/close_on.gif\'" onmouseout="this.src=\'/images/icons/close_off.gif\'" /></a> <a href="javascript:;" onclick="reportPhoto()" style="float:right"><img src="/images/icons/report_off.gif" alt="Report" title="Report"  onmouseover="this.src=\'/images/icons/report_on.gif\'" onmouseout="this.src=\'/images/icons/report_off.gif\'" /></a></div>');
	showCenteredPopup();
}

function reportPhoto() {
	hidePops();

	fillPopup('Loading report controls...');
	showCenteredPopup();
	new Ajax.Request('/reports/image_controls/'+current_open_image, {method: 'GET', onSuccess: showReportControls})
}

var showReportControls = function (t) {
	fillPopup(t.responseText);
	reCenterPopup();
}

function hidePops() {
	hideModelPopup();
	hideOtherPopup();
}

function SendReport(type, cap ) {
	if (typeof current_open_image != 'undefined') image_id = current_open_image;
	var url = '/photos/report/'+image_id+'/'+type;
	if (cap!="undefined") url+='/'+cap;
	new Ajax.Request(url, {method:'get', onSuccess: handlerFunc});
}

function handlerFunc(t) {
	populatePopup(t.responseText);
	setTimeout('hideDimmer(true)', 2*1000);
}


function showPanel(obj) {

	var el = document.getElementById(obj);
	el.style.display = el.style.display ? '':'none';

}


function displayContact(first_name, last_name, phone, email, address, city, zip, state, country) {
	hidePops();
	fillPopup('<div style="positon:relative;"><img src="/images/frame/frame_tfp_close.png" alt="close" style="position:absolute; right: 0; top: 0;cursor: pointer" onclick="hideDimmer(true)" /><table>'+
		'<tr><td>&nbsp;</td></tr>'+
		'<tr><th align="left" width="120"><strong>First Name:</strong></th><td>'+first_name+'</td></tr>'+
		'<tr><th align="left"><strong>Last Name:</strong></th><td>'+last_name+'</td></tr>'+
		'<tr><th align="left"><strong>Email:</strong></th><td><a href="mailto:'+email+'" class="pinkGreen">'+email+'</a></td></tr>'+
		'<tr><td>&nbsp;</td></tr>'+
		'<tr><th align="left"><strong>Phone:</strong></th><td>'+phone+'</td></tr>'+
		'<tr><th align="left"><strong>Address:</strong></th><td>'+address+'</td></tr>'+
		'<tr><th align="left"><strong>City:</strong></th><td>'+city+'</td></tr>'+
		'<tr><th align="left"><strong>State:</strong></th><td>'+state+'</td></tr>'+
		'<tr><th align="left"><strong>Country:</strong></th><td>'+country+'</td></tr>'+
	'</table></div>');
	showCenteredPopup(true);
}


function showHideCasting(plus, cast_id) {
	if(document.getElementById(cast_id).style.display == 'none') {
		plus.innerHTML = '<img src="/images/str_on.gif">';
		document.getElementById(cast_id).style.display = "block";
	} else {
		plus.innerHTML = '<img src="/images/str.gif">';
		document.getElementById(cast_id).style.display = "none";
	}
}


function move_down(elmnt) {
	var current_id = elmnt.id;
	var insert_here = false;
	var insert_html = null;

	for (i=0; i< $('sort_body').childNodes.length; i++) {
		if (current_id == $('sort_body').childNodes[i].id) {
			if (!$('sort_body').childNodes[i].nextSibling) return;

			insert_html = elmnt.cloneNode(true);
			elmnt.parentNode.removeChild(elmnt);

			insertAfter(insert_html, $('sort_body').childNodes[i]);

			break;
		}
	}

	save_sort();
}

function move_up(elmnt) {
	var current_id = elmnt.id;
	var insert_here = false;
	var insert_html = null;
	for (i=0; i< $('sort_body').childNodes.length; i++) {
		if (current_id == $('sort_body').childNodes[i].id) {
			if (!$('sort_body').childNodes[i].previousSibling) return;

			insert_html = elmnt.cloneNode(true);

			$('sort_body').insertBefore(insert_html, $('sort_body').childNodes[i].previousSibling);

			elmnt.parentNode.removeChild(elmnt);
			//alert('founr');
			break;
		}
	}

	save_sort();
}

function minimize(elmnt, source) {
	if (elmnt.style.display == 'none') {
		elmnt.style.display = 'block';
		source.firstChild.src = '/images/sort/min.gif';
		source.firstChild.alt = 'Minimize';
	}else{
		elmnt.style.display ='none';
		source.firstChild.src = '/images/sort/max.gif';
		source.firstChild.alt = 'Maximize';
	}

	save_sort();
}

//create function, it expects 2 values.
function insertAfter(newElement,targetElement) {
      //target is what you want it to go after. Look for this elements parent.
      var parent = targetElement.parentNode;

      //if the parents lastchild is the targetElement...
      if(parent.lastchild == targetElement) {
      	//add the newElement after the target element.
     	 parent.appendChild(newElement);
      } else {
	      // else the target has siblings, insert the new element between the target and it's next sibling.
	      parent.insertBefore(newElement, targetElement.nextSibling);
      }
}

function save_sort() {
	var order = '';
	for (i=0; i < $('sort_body').childNodes.length; i++) {
		fld = $('sort_body').childNodes[i].id.replace("sort_", '');
		order += $('sort_body').childNodes[i].id+":"+($('min_'+fld).style.display=='block'?"1":"0")+";";
	}

	new Ajax.Request('/users/save_welcome_order?order='+encodeURIComponent(order), {method: "GET"});
}

function feedPopUp(target_path) {
	modelProfileVisible = true;
	fillPopup('<img src="' + target_path + '" alt="" />', true);
}
function feedPopOut() {
	modelProfileVisible = false;
	hidePopup();
}