$(document).ready(function() {
	confirmation = function(url,msg) {
		
		if(msg.length == 0) {
			msg = "Are you sure?";
		}
		
		var answer = confirm(msg);
		
		if (answer) {
			document.location = url;
		}
	};
	
	$(".flash_msg").fadeIn("fast").fadeTo(1500,1).fadeOut("slow",function(){
		$(this).remove();
	});
	
	$("ul#main-navigation li:first-child").css('background','none');
	$("div#post_buffet a:first-child").css('border','none');
	$("#_submit").click(function() {
		$(this).css('padding-right','30px');
		$(this).css("background","#74AF4C url(/assets/images/submit-loading.gif) 90% center no-repeat");
	});
	$("#dashboard-stats td.stat:last-child").css('border-right','0');
	 
	// rounded corners for IE
	DD_roundies.addRule('.post_dashboard_header', '8px 8px 0px 0px');
	DD_roundies.addRule('.post_dashboard', '0px 0px 8px 8px');
	//DD_roundies.addRule('#dashboard-topbar', '0px 0px 12px 12px',true);
	DD_roundies.addRule('.stats-bottom', '0px 0px 12px 12px',true);
	DD_roundies.addRule('#home-wrap', '8px');
	DD_roundies.addRule('#nav-dropdown ul', '0 0 8px 8px',true);
	DD_roundies.addRule('.dash-tags, .tag-label', '3px');
	DD_roundies.addRule('.dash-posts-topper p', '8px', true);
	DD_roundies.addRule('.dash-avatar', '4px', true);
	
	$('.select').selectbox();
	
	unfollow = function(follow_id)
	{
		$.ajax({
		  url: "/following/unfollow/"+follow_id,
		  cache: false,
		  success: function(html){
		    $("#unfollow_"+follow_id).unbind("click");
			$("#unfollow_"+follow_id).bind("click", function() {
				follow(follow_id);
			});
			$("#unfollow_"+follow_id).html("+ Follow");
		  }
		});
		
	}
	
	follow = function(follow_id,disappear)
	{
		$.ajax({
		  url: "/following/follow/"+follow_id,
		  cache: false,
		  success: function(html){
		    $("#unfollow_"+follow_id).unbind("click");
			if(disappear == 'yes')
			{
				$("#unfollow_"+follow_id).remove();
				return false;
			}
			$("#unfollow_"+follow_id).bind("click", function() {
				unfollow(follow_id);
			});
			$("#unfollow_"+follow_id).html("- Unfollow");
		  }
		});
	}
	
	$("#nav-dropdown").hover(function() {
		$(this).children("ul").show();
	}, function() {
		$(this).children("ul").hide();
	});
});

var maps = [];

function loadMap(lat,long,el,address) {
 
  if (GBrowserIsCompatible()) {
    maps[el] = new GMap2(document.getElementById(el));
    maps[el].setCenter(new GLatLng(lat, long),13);
	maps[el].addControl(new GSmallMapControl());
	maps[el].addControl(new GMapTypeControl());
	
    // Creates a marker at the given point
    // Clicking the marker will hide it
    function createMarker(latlng, address) {
      var marker = new GMarker(latlng);

      GEvent.addListener(marker,"click", function() {
        var myHtml = '<div style="font-size:11px; font-family:helvetica,arial;">' + address + "</div>";
        maps[el].openInfoWindowHtml(latlng, myHtml);
      });
      return marker;
	}

	var latlng = new GLatLng(lat,long);
	maps[el].addOverlay(createMarker(latlng,address));
  }
}

function toggleEditor() {
	$(document).ready(function() {
		if($("#postbody").length > 0) {
			id = "postbody";
		}

		if($("#description").length > 0) {
			id = "description";
		}

		if($("#biography").length > 0) {
			id = "biography";
		}

		if (!tinyMCE.get(id)) {
			tinyMCE.execCommand('mceAddControl', false, id);
			$("#"+id).css("width","100%");
			$("#"+id).css("height","250px");
			$(".showControlsLink").remove();
		} else {
			tinyMCE.execCommand('mceRemoveControl', false, id);
			$("#"+id).after('<div class="showControlsLink">Viewing Source: <a href="javascript:toggleEditor();">Show Editing Controls</a></div>');
			$("#"+id).css("width","100%");
			$("#"+id).css("height","250px");
		}
	});
}
