var tripVotes = null;
var ajax = new Ajax();

function flipFlop(parent){
	closeall();
	if(parent.id == "expand1"){
		if (parent.className == "open") {
			parent.className = "closed";
			var expand1arrow = document.getElementById('expand1arrow');
			if(expand1arrow)expand1arrow.className='closed_arrow';
			var listnumone = document.getElementById('listnumone');
			if(listnumone)listnumone.style.display='none';
		}
		else{
			parent.className = "open";
			var expand1arrow = document.getElementById('expand1arrow');
			if(expand1arrow)expand1arrow.className='open_arrow';
			var listnumone = document.getElementById('listnumone');
			if(listnumone)listnumone.style.display='block';
			
		}
	}
	else if(parent.id == "expand2"){
		if (parent.className == "open") {
			parent.className = "closed";
			var expand1arrow = document.getElementById('expand2arrow');
			if(expand1arrow)expand1arrow.className='closed_arrow';
			var listnumone = document.getElementById('listnumtwo');
			if(listnumone)listnumone.style.display='none';
		}
		else{
			parent.className = "open";
			var expand1arrow = document.getElementById('expand2arrow');
			if(expand1arrow)expand1arrow.className='open_arrow';
			var listnumone = document.getElementById('listnumtwo');
			if(listnumone)listnumone.style.display='block';			
		}
	}
	/*
	else if(parent.id == "expand3"){
		if (parent.className == "open") {
			parent.className = "closed";
			var expand1arrow = document.getElementById('expand3arrow');
			if(expand1arrow)expand1arrow.className='closed_arrow';
			var listnumone = document.getElementById('listnumthree');
			if(listnumone)listnumone.style.display='none';
		}
		else{
			parent.className = "open";
			var expand1arrow = document.getElementById('expand3arrow');
			if(expand1arrow)expand1arrow.className='open_arrow';
			var listnumone = document.getElementById('listnumthree');
			if(listnumone)listnumone.style.display='block';			
		}
	}
	*/
}
function not_over(el){
	if(el){
		el.className="not_over";
	}
}
function over(el){
	if(el){
		el.className="over";
	}
}
function set_black(el){
	if(el){
		clear_to_white();
		el.style.background="#000";
	}
}
function hideall(){
	var x1 = document.getElementById('b1');
	var x2 = document.getElementById('b2');
	var x3 = document.getElementById('b3');
	var x4 = document.getElementById('b4');
	var x5 = document.getElementById('b5');
	var x6 = document.getElementById('b6');
	var x7 = document.getElementById('b7');
	if(x1)x1.style.display="none";
	if(x2)x2.style.display="none";
	if(x3)x3.style.display="none";
	if(x4)x4.style.display="none";
	if(x5)x5.style.display="none";
	if(x6)x6.style.display="none";
	if(x7)x7.style.display="none";
	setArrowsClosed();
}
function setArrowsClosed(){
	var x1 = document.getElementById('a1');
	var x2 = document.getElementById('a2');
	var x3 = document.getElementById('a3');
	var x4 = document.getElementById('a4');
	var x5 = document.getElementById('a5');
	var x6 = document.getElementById('a6');
	var x7 = document.getElementById('a7');
	if(x1)x1.className="help_arrow_closed";
	if(x2)x2.className="help_arrow_closed";
	if(x3)x3.className="help_arrow_closed";
	if(x4)x4.className="help_arrow_closed";
	if(x5)x5.className="help_arrow_closed";
	if(x6)x6.className="help_arrow_closed";
	if(x7)x7.className="help_arrow_closed";
}
function set_stage(stage){
	hideallleft();
	if(stage == 1)document.getElementById('help_stage_1').style.display="block";
	else if(stage == 2)document.getElementById('help_stage_2').style.display="block";
	else if(stage == 3)document.getElementById('help_stage_3').style.display="block";
	else if(stage == 4)document.getElementById('help_stage_4').style.display="block";
}
function hideallleft(){
	document.getElementById('help_stage_1').style.display="none";
	document.getElementById('help_stage_2').style.display="none";
	document.getElementById('help_stage_3').style.display="none";
	document.getElementById('help_stage_4').style.display="none";
}
function flipper(el){
	if(el){
		hideall();
		res = cleanse(el);
		var a = "a" + res;
		var b = "b" + res;		
		if(document.getElementById(a))document.getElementById(a).className = "help_arrow_open";
		var el = document.getElementById(b);

		if(el)el.style.display = "block";
	}
}
function cleanse(string){
	return string.charAt(1);
}
function killPopUp(){
	hidePopup();
//$('#popup').fadeOut("normal");
//$('#popup_bg').fadeOut("normal");
	jQuery('#popup_bg').fadeOut("normal");
}
function size()
{
	var w = 0;
	var h = 0;

	//IE
	if(!window.innerWidth)
	{
		//strict mode
		if(!(document.documentElement.clientWidth == 0))
		{
			w = document.documentElement.clientWidth;
			h = document.documentElement.clientHeight;
		}
		//quirks mode
		else
		{
			w = document.body.clientWidth;
			h = document.body.clientHeight;
		}
	}
	//w3c
	else
	{
		w = window.innerWidth;
		h = window.innerHeight;
	}
	return {width:w,height:h};
}
function pop_up_box(msg){
	var tmp = document.getElementById('popUpBox');
	if(tmp.style.display != "none"){
		iekillPopUp();
	}
	var newEl = document.getElementById('popUpBox');
	if (newEl) {
		var size = this.size();
		var clientResWidth = size.width;
		var clientResHeight = size.height;
		var xpos = (clientResWidth / 2) - 168;
		var ypos = (clientResHeight / 2) - 100;
		newEl.style.display = "block";
		newEl.style.left = xpos + "px";
		newEl.style.top = ypos + "px";
	}
}
function iekillPopUp(){
	var el = document.getElementById('popUpBox');
	if(el){
		el.style.display = "none";
	}
}
function closeall(){
	var expand1 = document.getElementById('expand1');
	var expand2 = document.getElementById('expand2');
	var expand1arrow = document.getElementById('expand1arrow');
	var expand2arrow = document.getElementById('expand2arrow');
	var listnumone = document.getElementById('listnumone');
	var listnumtwo = document.getElementById('listnumtwo');
	//var expand3 = document.getElementById('expand3');
	//var expand3arrow = document.getElementById('expand3arrow');
	var listnumthree = document.getElementById('listnumthree');
	expand1.className = "closed";
	expand1arrow.className = "closed_arrow";
	listnumone.style.display = "none";
	if(expand2)expand2.className = "closed";
	if(expand2arrow)expand2arrow.className = "closed_arrow";
	if(listnumtwo)listnumtwo.style.display = "none";
	/*
	if(expand3)expand3.className = "closed";
	if(expand3arrow)expand3arrow.className = "closed_arrow";
	if(listnumthree)listnumthree.style.display = "none";
	*/
}
function highlightStars(num) {
  for(var i=1;i<=5; i++) {
     var elem = document.getElementById("star"+i);
     if(i <= num) {
        elem.src = "/images/toyota/star-full.png";
     } else {
        elem.src = "/images/toyota/star-empty.png";
     }   
  }  
}
function unhighlightStars(num) {
  for(var i=1;i<=5; i++) {
     var elem = document.getElementById("star"+i);
     var rem = num-Math.floor(num);

     if(Math.floor(num) >= i) {
        elem.src = "/images/toyota/star-full.png";
     } else if(rem >= .5 && (i-1) <= num) {
        elem.src = "/images/toyota/star-half.png";
     } else {
        elem.src = "/images/toyota/star-empty.png";
     }
  }
}
function voteAjax(num, tripId) {
  start = new Date();
  start = start.getTime();
  ajax.doGet('/trip/voteajax/?tripId='+tripId+'&start='+start+'&num='+num, voteResponse,"text");
}
function voteResponse(response) {

     response = response.split(",");
     var trip_votes_span = document.getElementById('trip_votes_span');

     numVote = parseInt(response[1]);
      if(numVote == -2) {
         alert("You can only vote for a trip once");
      } else if(numVote > 0) {
         trip_votes_span.innerHTML = numVote + " vote";
         if(numVote != 1) {
            trip_votes_span.innerHTML += "s";
         }         
         tripVotes = response[0];
         unhighlightStars(response[0]);
      } else if(numVote == -1) {
         //alert("Please log in or create an account to vote");
    	  var temp = popupController(2);
          jQuery.popup.show("hidden", temp);
      }
      /*
      if(voteButton && trip_votes_span && numVote >= 0) {
         trip_votes_span.removeChild(voteButton);
      }
      */
}
function vote(num, tripId) {
      if(!num) { num = 1; }

      voteAjax(num, tripId);
}
function popup(window_id, extra){
	var img1 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_top_left.gif";
	var img2 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_top_right.gif";
	var img3 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_top_center.gif";
	var img4 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_col_center.gif";
	var img5 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_col_right.gif";
	var img6 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_col_left.gif";
	var img7 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_bottom_center.gif";
	var img8 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_bottom_right.gif";
	var img9 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_bottom_left.gif";
	var img10 = new Image(0,0);
	img.src = "http://www.trailtracker.com/images/toyota/popup_embed_header_right.gif";
	
	
	
	if(extra == undefined){
		var template = popupController(window_id);
	} else {
		var template = popupController(window_id, extra);
	}
		
	var tmp = document.getElementById('popUpBox');
	if (tmp) {
		killPopUp();
	}
	var newEl = document.createElement('div');
	if (newEl) {
		var page_container = document.getElementById('page_container');
		if (page_container) 
			page_container.appendChild(newEl);
		var size = this.size();
		var clientResWidth = size.width;
		var clientResHeight = size.height;
		var xpos = (clientResWidth / 2) - 168;
		var ypos = (clientResHeight / 2) - 20;
		newEl.id = "popUp";
		newEl.style.display = "block";
		newEl.style.position = "absolute";
		newEl.style.left = xpos + "px";
		newEl.style.top = ypos + "px";
		stylelize(newEl);
		newEl.innerHTML = template;
	}
}
function stylelize(el){
	el.style.backgroundColor = "#A1A29D";
	el.style.backgroundImage = "url('/images/toyota/fancy_bg.gif')";
	el.setAttribute("rel", "corners");
	$("div[rel='corners']").corner("15px");
}
function popupController(window_id, extra){
	var baseClass="default";
	if(!extra){
		var extra = "";
	}
	switch(window_id){
		case 1:
		baseClass="popup_embed";
	}
	defaultClass="default"; //Global to access in response
	var defaultWidth=400;
	var defaultHeight=100;
	var default_header_template='<div class="'+ baseClass +'_float_left">'+
						   			'Header on left'+
						   		'</div>'+
						   		'<div class="'+ baseClass +'_float_right">'+
						   			'Header on right'+
						   		'</div>'+
						   		'<div class="clear"/>';
	var default_body_template = '<div class="'+ baseClass +'_float_left">'+
						   			'body goes here'+
						   		'</div>';
	//invite popup
	var invite_header_template = '<div class="'+ baseClass +'_invite_float_left">'+
							   		'<div class="popup_invite_header_left"></div>'+
						   		'</div>'+
						   		'<div class="'+ baseClass +'_invite_float_right cursor">'+
						   			'<div id="popup_close" class="popup_embed_text"\'>'+
										'<img src="/images/toyota/popup_embed_header_right.gif" border="0" onclick="killPopUp()">'+
									'</div>'+
						   		'</div>'+
						   		'<div class="clear"/>';
	var invite_body_template = '<div class="'+ baseClass +'_embed_body_container">'+
									'<div class="invite_body_text">Enter e-mail addresses separated by commas:</div>'+
									'<div class="invite_ta">'+
										'<form id="invite_email_form" name="invite_email_form">'+
										'<textarea name="invite_emails" id="invite_emails" cols="34" rows="4" style="width:280px;">'+
											extra +
									'</textarea></div></form>'+
									'</div>'+
									'<div class="invite_body_text">From:</div>'+
										'<form id="invite_from_form" name="invite_email_form">'+
										'<div class="invite_ta"><input id="invite_from" name="from" size="39" style="width:280px;"></div></form>'+
									'</div>'+
									'<div class="invite_body_text">Message:</div>'+
										'<form id="invite_msg_form" name="invite_email_form">'+
										'<div class="invite_ta"><textarea name="invite_message" id="invite_message" cols="34" rows="4" style="width:280px;" onclick="">'+
										extra +
										'A fellow off-roader has recommended you join the Trail Tracker. ' +
										'</textarea></div></form>'+
										'<div class="invite_submit_button" onclick="ajax_invite_friends()"></div>'+
									'</div>'+
									''+
									'';
	var invite_footer_template = '<div>'+
								
								'</div>';
	
	//login popup							

	var login_header_template = '<div class="'+ baseClass +'_login_float_left">'+
							   		'<div class="popup_login_header_left"/>'+
						   		'</div>'+
						   		'<div class="'+ baseClass +'_float_right_login cursor">'+
						   			'<div class="popup_join_text">'+
										'<a href="/user/create/"><div class="login_box_header"/></a>'+
									'</div>'+
						   		'</div>'+
						   		'<div class="clear"/>';
	var login_body_template = 	'<div class="'+ baseClass +'_login_body_container">'+
									'<div id="returnMessage"></div>'+
									'<div class="'+ baseClass +'_login_body_float_left">'+
							   			'<div style="margin-bottom:7px; font-size:14px; color:#fff; font-weight:bolder;">Username:</div>'+
										'<div style="margin-bottom:5px; font-size:14px; color:#fff; font-weight:bolder;">Password:</div>'+
							   		'</div>'+
									'<div class="'+ baseClass +'_login_body_float_right">'+
							   			'<form id="login_form" method="post" action="/user/login">'+
							   			'<input id="loginPage" type="hidden" value="" name="page">'+
										'<input type="text" id="loginUsername" name="username" style="margin-bottom:5px; width:130px;">'+
										'<br><input type="password" id="loginPassword" name="password" style="margin-bottom:5px; width:130px;">'+
							   		'</form>'+
									'</div>'+
									'<div class="clear"></div>'+
									'<div>'+
									'<input id="loginRemember" type="checkbox" name="remember">&nbsp;<label for="loginRemember">Keep me logged in</label>&nbsp;<br><a style="font-size:12px;" href="/user/forgotpass">> Forgot Password?</a><br><br>'+
									'</div>'+
								'</div>';
	
	var login_footer_template = '<div>'+
									'<input type="submit"';
									if($.browser.msie && $.browser.version <= 8){
										login_footer_template +='value="Login"';
									} 
									else{
									login_footer_template +='value=""';
									 }
									 login_footer_template += 'class="' + baseClass +'_login_footer cursor" onclick="loginSubmit(document.getElementById(\'login_form\'));">'+
								'</div>';
		//embed popup
	var embed_header_template = '<div class="'+ baseClass +'_login_float_left">'+
							   		'<div class="popup_embed_header_left"/>'+
						   		'</div>'+
						   		'<div class="'+ baseClass +'_float_right cursor">'+
						   			'<div id="popup_close" class="popup_embed_text"\'>'+
										'<img src="/images/toyota/popup_embed_header_right.gif" border="0" onclick="killPopUp()">'+
									'</div>'+
						   		'</div>'+
						   		'<div class="clear"/>';
	var embed_body_template = '<div class="'+ baseClass +'_embed_body_container">'+
									'<div class="'+ baseClass + '_body_text">Copy Code Below:</div>'+
									'<textarea cols="28" onclick="this.select();">'+
									extra +
									'</textarea>'+
								'</div>';
	var embed_footer_template = '<div>'+
									'<div class="' + baseClass +'_done_button cursor"  onclick="killPopUp()">'+
								'</div>';
	//background: url(/images/toyota/flag_header.gif);
			//flag popup
	var flag_header_template = '<div class="flagie6fix"><img src="/images/toyota/flag_header.gif" border="0" onclick="killPopUp()"></div>'+
		'<div class="flag_login_float_left">'+
							   		'<div class="flag_as_abuse"/>'+
						   		'</div>'+
						   		'<div class="flag_float_right cursor">'+
						   			'<div id="popup_close" class="popup_embed_text"\'>'+
										'<img src="/images/toyota/popup_embed_header_right.gif?v=' + Math.random()*11968 + '" border="0" onclick="killPopUp()">'+
									'</div>'+
						   		'</div>'+
						   		'<div class="clear"/>';
	var flag_body_template = '<div class="'+ baseClass +'_embed_body_container">'+
									'<div class="'+ baseClass + '_smaller_body_text">Please explain what the inappropriate content is:</div>'+
									'<textarea id="flagged_abuse" cols="34" rows="4">'+
									
									'</textarea>'+
								'</div>'+
								'<div class="flagged_cancel cursor" onclick="killPopUp()"></div>'+
								'<div class=\'flagged_report cursor\' onclick=\'sendReport("'+extra +'");\'></div>';
	var flag_footer_template = '<div>'+
									
								'</div>';
					//send to friend popup (s.t.f.)
	var stf_header_template = '<div class="'+ baseClass +'_invite_float_left">'+
							   		'<div class="stf_invite_header_left"/>'+
						   		'</div>'+
						   		'<div class="'+ baseClass +'_invite_float_right cursor">'+
						   			'<div id="popup_close" class="popup_embed_text"\'>'+
										'<img src="/images/toyota/popup_embed_header_right.gif" border="0" onclick="killPopUp()">'+
									'</div>'+
						   		'</div>'+
						   		'<div class="clear"/>';
	var stf_body_template = '<div class="'+ baseClass +'_embed_body_container">'+
									
									'<div class="invite_body_text">Enter e-mail addresses separated by commas:</div>'+
									'<div class="invite_ta">'+
										'<form id="send_trail_email_form" name="send_trail_email_form">'+
										'<textarea name="to" id="send_trail_to" cols="34" rows="4" style="width:300px;">'+
											
									'</textarea></form></div>'+
									'</div>'+
									'<div class="invite_body_text">From:</div>'+
										'<form id="send_trail_email_form" name="send_trail_email_form">'+
										'<div class="invite_ta"><input id="send_trail_from" name="from" size="39" style="width:300px;"></div></form>'+
									'</div>'+
									'<div class="invite_body_text">Message:</div>'+
										'<form id="send_trail_email_form" name="send_trail_email_form">'+
										'<div class="invite_ta"><textarea name="send_trail_message" id="send_trail_message" cols="34" rows="4" style="width:300px;" onclick="">'+
										'Your friend has sent you a trail they want you to check out on the Trail Tracker. ' +
										'</textarea></div></form>'+
										'<div class="stf_submit_button" onclick="ajax_stf(' +extra+')"></div>'+
									'</div>'+
									''+
									'';
									/*
	var stf_body_template = '<div class="'+ baseClass +'_embed_body_container">'+
									'<div class="invite_ta">'+
									'<form id="send_trail_email_form" name="send_trail_email_form">'+
									'<table><tr><td width="100">'+
										'<label class="stf_labels">To:   </label></td><td><input id="send_trail_to" name="to"   size="33"/></td></tr><tr><td>'+
										//'<labe class="stf_labels">Name: </label></td><td><input id="send_trail_name" name="name" size="33"/></td></tr><tr><td>'+
										'<label class="stf_labels">From: </label></td><td><input id="send_trail_from" name="from" size="33"/></td></tr><tr><td>'+
										'<label class="stf_labels">Message:   </label></td><td><textarea name="send_trail__message" id="send_trail__message" style="width:224px" rows="4" onclick="this.select();">'+
									'</textarea></td></tr>'+
										'</table>'+											
									'</form>'+
								'</div>'+
									'<div class="invite_submit_button" onclick="ajax_stf(' +extra+')"/>'+
								'</div>'+
								''+
								'';
								*/
	var stf_footer_template = '<div>'+								
								'</div>';

	//delete photo popup							

	var delphoto_header_template = '<div class="'+ baseClass +'_invite_float_left">'+
							   		'<div class="delete_photo_header"/>'+
						   		'</div>'+						   		
						   		'<div class="clear"/>';
	var delphoto_body_template = '<div class="delete_photo_cancel cursor" onclick="hidePopup();"></div>'+
								 '<div class="delete_photo_delete cursor" onclick="confirmed_delete_photo('+extra+');"></div>';	
	var delphoto_footer_template = '';
	var default_header_template='<div class="'+ baseClass +'_float_left">'+
									'<div class="add2favs">'+
									'</div>'+
								'</div>'+
								'<div class="'+ baseClass +'_float_right">'+
								'</div>'+
								'<div class="clear"/>';
	var addfav_body_template = '<div class="'+ baseClass +'_float_left">'+
								extra+
								'</div>';
	
	var addfav_footer_template = "";
	
	switch(window_id){
		case 1:
			var selected_header = embed_header_template;
			var selected_body = embed_body_template;
			var selected_footer = embed_footer_template
			defaultWidth=250;
			defaultHeight=10;
			//width any any other params can be overwritten here
			break;
		case 2:
			var selected_header = login_header_template;
			var selected_body = login_body_template;
			var selected_footer = login_footer_template;
			defaultWidth=225;
			defaultHeight=50;
			break;
		case 3:
			var selected_header = invite_header_template;
			var selected_body = invite_body_template;
			var selected_footer = invite_footer_template
			defaultWidth=305;
			defaultHeight=288;
			defaultClass='invite';
			//width any any other params can be overwritten here
			break;
		case 4:
			var selected_header = flag_header_template;
			var selected_body = flag_body_template;
			var selected_footer = flag_footer_template
			defaultWidth=220;
			defaultHeight=125;
			//defaultClass='invite';
			//width any any other params can be overwritten here
			break;
		case 5:
			var selected_header = stf_header_template;
			var selected_body = stf_body_template;
			var selected_footer = stf_footer_template
			defaultWidth=300;
			defaultHeight=120;
			//defaultClass='invite';
			//width any any other params can be overwritten here
			break;
		case 6:
			var selected_header = delphoto_header_template;
			var selected_body = delphoto_body_template;
			var selected_footer = delphoto_footer_template
			defaultWidth=200;
			defaultHeight=0;
			//defaultClass='invite';
			//width any any other params can be overwritten here
			break;
		case 7:
			var selected_header = default_header_template;
			var selected_body = addfav_body_template;
			var selected_footer = addfav_footer_template;
			defaultWidth=250;
			defaultHeight=100;
			//defaultClass='invite';
			//width any any other params can be overwritten here
			break;
		default:
			var selected_header = default_header_template;
			var selected_body = default_body_template;		
	}
	var popupContainer = 
		'<div id="popCont1">'+
			'<div id="popCont2">'+
				'<table cellpadding="0" cellspacing="0" border="0">'+
					'<tr valign="top">'+
						'<td class="' + defaultClass + '_top_left"></td>'+
						'<td class="' + defaultClass + '_top_center" style="width:' + defaultWidth + 'px;">'+  selected_header + '</td>'+
						'<td class="' + defaultClass + '_top_right"></td>'+
					'</tr><tr valign="top">'+
						'<td class="' + defaultClass + '_col_left"></td>'+
						'<td id="' + defaultClass + '_content" class="' + defaultClass + '_col_center" style="height:' + defaultHeight + 'px;"><div id="popupLoader" style="display:none; text-align: center;"><img src="/images/loading.gif" alt=""></div>'+ selected_body + '</td>'+
						'<td class="' + defaultClass + '_col_right"></td>'+
					'</tr><tr valign="top">'+
						'<td class="' + defaultClass + '_bottom_left"></td>'+
						'<td class="' + defaultClass + '_bottom_center">' + selected_footer + '</td>'+
						'<td class="' + defaultClass + '_bottom_right"></td>'+
					'</tr>'+
				'</table>'+
			'</div>'+
		'</div>';
	return popupContainer;
}
function removeTrip() {
  ajax.doGet("/trip/favoriteajax/?trip_id=" + tripId + "&remove=true",favoriteResponse);
}

function favoriteTrip() {
  //var type = $("input:radio[@name=favoriteType]:checked").val();
  ajax.doGet("/trip/favoriteajax/?trip_id=" + tripId + "&type=like",favoriteResponse);
}

function favoriteResponse(res) {
  if(res == "added") {
     document.getElementById('favoriteSpan').innerHTML = "<a href=\"javascript:removeTrip();\">Remove from Favorites</a>";
  } else {
     document.getElementById('favoriteSpan').innerHTML = "&nbsp;&nbsp;&nbsp;Favorite removed";
  }
}

/*
function favoriteResponse2(res) {
	document.getElementById('favoriteSpan').innerHTML = "&nbsp;&nbsp;&nbsp;&nbsp;Favorite added";
}
*/

function getposOffset(overlay, offsettype){
  var totaloffset=(offsettype=="left")? overlay.offsetLeft : overlay.offsetTop;
  var parentEl=overlay.offsetParent;
  while (parentEl!=null){
     totaloffset=(offsettype=="left")? totaloffset+parentEl.offsetLeft : totaloffset+parentEl.offsetTop;
     parentEl=parentEl.offsetParent;
  }
  return totaloffset;
}

function overlay(){
	var html ='<div id="favoritePopup" class="panelgreen" style="position:absolute; width: 250px;  z-index:150;">';
	html += '<input type="radio" name="favoriteType" value="been" id="favoriteBeen"> I have been here';
	html += '<br>';
	html += '<input type="radio" name="favoriteType" value="want" id="favoriteWant"> I want to go here';
	html += '<br>';
	html += '<input type="radio" name="favoriteType" checked=true value="like" id="favoriteLike"> I like this trip';
	html += '<br>';
	html += '<div class="popup_favorite_cancel_button cursor" onclick="overlayclose(\'favoritePopup\');"></div>';
   	html += '<div class="popup_favorite_done_button cursor" onclick="favoriteTrip();overlayclose(\'favoritePopup\');"></div>';
	//html += '<input type=button value="add to favorites" onclick="favoriteTrip();overlayclose(\'favoritePopup\');" style="padding:2px">';
	//html += '<br>';
	//html += '<a href="javascript:overlayclose(\'favoritePopup\')"><b><u>cancel</u></b></a>';
	html += '</div>';

	  var temp = popupController(7,html);
	  $.popup.show("hidden", temp);
}

function overlayclose(){
 	killPopUp();
}
/* follow user js functions */

function followUser(userId) {
   var ajax = new Ajax();
   ajax.doGet("/user/followuser/followed_id/"+userId, followResponse,"text");
}

function unfollowUser(userId) {
   var ajax = new Ajax();
   ajax.doGet("/user/followuser/followed_id/"+userId+"/remove/true", followResponse,"text");
}


function followResponse(response) {
   response = response.split(",");
   var error = parseInt(response[0]);
   var userId = parseInt(response[1]);
   killPopUp();
   var span = document.getElementById('followUserSpan');

   if(error == 1) {
      alert("You cannot subscribe to yourself");
   } else if(error == 2) {
      alert("You must be logged in to subscribe to this user");
   } else if(error == 3) {
      span.innerHTML = "You are now subscribing to this user";
   } else if(error == 4) {
      span.innerHTML = "<a href=\"javascript:followUser("+userId+");\">Subscribe to user</a>";
   }
}
function responseConfirmation(response) {
   jQuery('#'+defaultClass+'_content').html(response);
}
function goBack(page, trip_id){
	if(page==1)window.location="/trip/create/";
	else if(page==2 && trip_id)window.location="/trip/tellstory?trip_id=" + trip_id;
	else if(page==2 && !trip_id)window.location="/trip/tellstory/";
	else if(page==3)window.location="/trip/tagpictures?trip_id=" + trip_id;
	else if(trip_id != '')window.location="/trip/view?trip_id=" + trip_id;
	else history.go(-1);
	
}
function ajax_invite_friends(){
	jQuery('#popupLoader').css('display', 'block');
	var emails = document.invite_email_form[0].invite_emails.value;
	var msg = document.invite_email_form[2].invite_message.value;
	var from = document.invite_email_form[1].invite_from.value;
					//
									//
	var ajax = new Ajax();
   ajax.doPost("/user/invitefriends/", "sent=true&to="+emails+"&message="+msg+"&from_name="+from, responseConfirmation,"text");
   //killPopUp();
}
function ajax_stf(id){
	jQuery('#popupLoader').css('display', 'block');
	//var name = document.send_trail_email_form[1].value;
	var msg = document.send_trail_email_form[2].send_trail_message.value;
	var from = document.send_trail_email_form[1].send_trail_from.value;
	var to = document.send_trail_email_form[0].send_trail_to.value;
	var ajax = new Ajax();
   ajax.doPost("/user/sendtrailtofriend/", "sent=true&to="+to+"&message="+msg+"&from=" +from+"&id="+id, responseConfirmation,"text");
   //killPopUp();
}
function sendReport(type){
	jQuery('#popupLoader').css('display', 'block');
	var abuse = document.getElementById('flagged_abuse');
	if(abuse){
		var string = abuse.value;
		var string = string.toString();
		}
	else{
		abuse = "no comment was included";
	}		
    var query = 'flag='+string+'&type='+type+'&url='+window.location.href; //&content='+content;//matt come back to this
	var ajax = new Ajax();
   ajax.doPost("/user/flag/", query, responseConfirmation, "text");
   //killPopUp();
}
function nullResponce(){
	
}
function delPhoto(photo_id){
	  var temp = popupController(6, photo_id);
	  $.popup.show("hidden", temp);  
}
function confirmPhotoDelete(photo_id){
	
}
function confirmed_delete_photo(id){
	//var el = document.getElementById('photo'+id);
	//$('#photo'+id).empty();
	//el.style.display = "none";
	var ajax = new Ajax();
	ajax.doGet("/picture/delete?confirm=true&picture_id="+id, refreshPage,"text");
	killPopUp();
}

function refreshPage() {
	document.location.reload();
}
function flagme(ele, type){
	var loc = window.location;
		if(ele.id){
			var name = "";//loc + " comment number: " + ele.id.toString() +" offends me because:";
		}
		else{
			var name = "";//loc + " offends me because:";
		}
		var temp = popupController(4, type);
	 	$.popup.show("hidden", temp); 
	 	if(window.ie6==1){
	 		document.getElementById('popup').setAttribute("rel", "corners");
	 		
//
	 	}
}

function marketingClick(lmp){
	if(lmp == 1){window.location = 'http://www.toyota.com/fjcruiser/';}
	else if(lmp == 2){window.location = 'http://www.toyota.com/4runner/';}
	else if(lmp == 3){window.location = 'http://www.toyota.com/tacoma/';}
}
function isLoggedForComments(){
	var el = document.getElementById('ad38e9c');
	if(el){
		if(el.innerHTML){
			temp = popupController(2);
	        jQuery.popup.show("hidden", temp);
		}else{
			jQuery('#postComment').submit();
		}
	}
}
function sendOmniture($pageName,$props,$evars){
	s.pageName = $pageName;
	if($props != null){
		for(var i in $props){
			var o = $props[i];
			s[i] = o;
		}
	}
	
	if($evars != null){
		for(var j in $evars){
			var v = $evars[j];
			s[j] = v;
		}
	}
	
	// this is the omniture function that sends tracking information.
	s.t(); 
}
function omniture(eventCode){
	var event = eventCode;
	this.init = function(){
		s.prop10 = "GM:Minisite";
		s.prop11 = "GM:AllVehicles:Minisite"; 
		s.prop32= "Upper";
		s.channel = "GM:AllVehicles"; 
		switch(eventCode){
		case 1: this.code1();break;
		case 2: this.code2();break;
		case 3: this.code3();break;
		case 4: this.code4();break;
		case 5: this.code5();break;
		case 6: this.code6();break;
		case 7: this.code7();break;
		case 8: this.code8();break;
		case 9: this.code9();break;
		case 10: this.code10();break;
		case 11: this.code11();break;
		case 12: this.code12();break;
		case 13: this.code13();break;
		case 130: this.code130();break;		
		case 14: this.code14();break;
		case 15: this.code15();break;
		case 16: this.code16();break;
		case 17: this.code17();break;
		case 18: this.code18();break;
		case 19: this.code19();break;
		case 20: this.code20();break;
		case 21: this.code21();break;
		case 22: this.code22();break;
		case 23: this.code23();break;
		case 24: this.code24();break;
		case 25: this.code25();break;
		case 26: this.code26();break;
		case 27: this.code27();break;
		case 28: this.code28();break;
		case 29: this.code29();break;
		case 30: this.code30();break;
		case 31: this.code31();break;
		case 32: this.code32();break;
		case 33: this.code33();break;
		case 34: this.code34();break;
		case 35: this.code35();break;
		case 36: this.code36();break;
		case 37: this.code37();break;
		case 38: this.code38();break;
		default: alert('invalid omniture route');
		}
	}
	this.code1 = function(){
	 	var pagename="GM:AllVehicles:Minisite:Trailtracker:Home";
	 	sendOmniture(pagename);//done
		}
		this.code2 = function(){
			var Pagename="GM:AllVehicles:Minisite:Trailtracker:Help";
			sendOmniture(Pagename);//done
		}
		this.code3 = function(){
			var Pagename="GM:AllVehicles:Minisite:Trailtracker:Privacy";
			sendOmniture(Pagename);//done
		}
		this.code4 = function(){
	 		var Pagename="GM:AllVehicles:Minisite:Trailtracker:Search";
	 		sendOmniture(Pagename);
		}
		this.code5 = function(){	
			var Pagename="GM:AllVehicles:Minisite:Trailtracker:Search";			
			sendOmniture(Pagename);//done2	
			setTimeout(function(){s.prop21 = window.q; s.t();}, 2000);
		}
		this.code6 = function(){
	 		var Pagename="GM:AllVehicles:Minisite:Trailtracker:Upload:Step1 GPS";
			sendOmniture(Pagename);//done2
		}
		this.code7 = function(){
	 		var Pagename="GM:AllVehicles:Minisite:Trailtracker:Upload:Step2 Trail Details";
			sendOmniture(Pagename);//done
		}
		this.code8 = function(){
			var Pagename="GM:AllVehicles:Minisite:Trailtracker:Upload:step3 Upload Photos";
			sendOmniture(Pagename);  //done
		}
		this.code9 = function(){
			var Pagename="GM:AllVehicles:Minisite:Trailtracker:Upload:Auto-Place Now";
			sendOmniture(Pagename); //wtf calls this????
		}
		this.code10 = function(){
			setTimeout(function(){var Pagename="GM:AllVehicles:Minisite:Trailtracker:View Trail:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;sendOmniture(Pagename);}, 3000); 
			//done2
		}
		this.code11 = function(){
			setTimeout(function(){var Pagename="GM:AllVehicles:Minisite:Trailtracker:View Trail:Slideshow:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;sendOmniture(Pagename);}, 3000); 
			//done
		}
		this.code12 = function(){
			var Pagename="GM:AllVehicles:Minisite:Trailtracker:Edit Trail:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
			sendOmniture(Pagename);//done
		}
		this.code13 = function(){
			setTimeout(function(){var Pagename="GM:AllVehicles:Minisite:Trailtracker:Delete Trail:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;sendOmniture(Pagename);}, 3000);
		}
		this.code130 = function(){
			setTimeout(function(){var Pagename="GM:AllVehicles:Minisite:Trailtracker:Registration";sendOmniture(Pagename);}, 3000);
		}
		this.code14 = function(){
			var Pagename="GM:AllVehicles:Minisite:Trailtracker:Login";
			sendOmniture(Pagename); //done
		}
		this.code15 = function(){
			setTimeout(function(){var Pagename="GM:AllVehicles:Minisite:Trailtracker:Forgot Password";sendOmniture(Pagename);}, 3000);			
		}
		this.code16 = function(){
			setTimeout(function(){var Pagename="GM:AllVehicles:Minisite:Trailtracker:My profile:" + window.user_id;sendOmniture(Pagename);}, 3000);			
			//done
		}
		this.code17 = function(){
			Pagename="GM:AllVehicles:Minisite:Trailtracker:Edit Profile";
			sendOmniture(Pagename); //done
		}
		this.code18 = function(){
			s.prop46 = "Upload File from GPX";
			s.t();//done2
			s.prop46 = null;
		}
		this.code19 = function(){
		 	s.prop46= "Import file from GPS device";
		 	s.t();
			s.prop46 = null;
		}
		this.code20 = function(){
			s.prop46 = "Trailtracker:SKIP PHOTOS:" + window.tripId + ":"  + window.tripState + ":" + window.tripCity;
			s.t();
			s.prop46 = null;
		}
		this.code21 = function(){
		 	s.prop46 = "Trailtracker:Upload Done:" + window.tripId + ":"  + window.tripState + ":" + window.tripCity;
		 	s.t();//done2
			s.prop46 = null;
		}
		this.code22 = function(){
			s.linkTrackVars="eVar35,events";
			s.eVar35 = "Trailtracker:Google Earth:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
		 	s.events="event41";
		 	s.tl();//done2 latte (its because evar is not working)
			s.eVar35 = null;
		}
		this.code23 = function(){
		 	s.prop46 = "Trailtracker:Embed:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
		 	s.t();
		 	s.prop46 = null;
		}
		this.code24 = function(){
			s.prop46 = "Trailtracker:Get Directions";
			s.t();
			s.prop46 = null;
		}
		this.code25 = function(){
		 	s.prop46 = "Trailtracker:Add to Favorites:" + window.tripId + ":"  + window.tripState + ":" + window.tripCity;
		 	s.t();
		 	s.prop46 = null;
		}
		this.code26 = function(){
			s.linkTrackVars="eVar13,events";
			s.eVar13="Trailtracker:Download to GPS:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
		 	s.events="event33";
		 	s.tl();//done2 latte
		 	s.eVar13 = null;
		 	s.events = null;
		}
		this.code27 = function(){
		 	s.prop46 = "Trailtracker:Embed:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
		 	s.t();
			s.prop46 = null;
		}
		this.code28 = function(){
			alert(6);
			s.prop14 = "Trailtracker:View All:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
			s.t();
			s.prop14 = null;
		}
		this.code29 = function(){
			var Pagename="GM:AllVehicles:Minisite:Trailtracker:Slideshow:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
			sendOmniture(Pagename);		
		}
		this.code30 = function(){
			s.prop14 = "Trailtracker:View All:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
			s.t();
			s.prop14 = null;
			//done2
		}
		this.code31 = function(){
			s.prop46 = "Trailtracker:Send Trail to a friend:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
			s.t();
			s.prop46 = null;
		}
		this.code32 = function(){
			s.prop46 = "Trailtracker:Embed:" + window.tripId + ":" + window.tripState + ":" + window.tripCity;
			s.t();
			s.prop46 = null;
		}
		this.code33 = function(){
			s.events = "event42";
			s.t();
			s.events = null;
		}
		this.code34 = function(){
			s.prop46 = "Trailtracker:INVITE FELLOW DRIVERS";
			s.t();
			s.prop46 = null;
		}
		this.code35 = function(){
			s.prop14 = "Trailtracker:POSTED";
			s.t();
			s.prop14 = null;
		}
		this.code36 = function(){
			s.prop14 = "Trailtracker:FAVORITES";
			s.t();
			s.prop14 = null;
		}
		this.code37 = function(){
			s.prop14 = "Trailtracker:RATED";
			s.t();
			s.prop14 = null;
		}
		this.code38 = function(){
			s.prop46 = "Trailtracker:Edit Profile Completed";
			s.t();
			s.prop46 = null;
		}	
	}