/* Copyright (c) 2011 mezz, Inc.  All Rights Reserved
*/
var mz_url = "http://l.mezz.com/m/v1/feed/card";
var mz_item = null;
var mz_latitude = null;
var mz_longitude = null;

/*
 * Internal function to get the feed.
 */
function getCard(cardid, divid)
{
	$.get(mz_url, 
			{ "cid": cardid
			},
			function(data) {
            	$(divid).append(makeCardDetail(data));
            	// Reparse newly added HTML to make sure FB Like and Twitter buttons are displayed properly.
            	reparseSocialMediaLinks();
			},
			"jsonp");
}

/*
 * Create the card structure.
 */
function makeCardDetail(item)
{
	var heart = "like";
	if (item.like_count > 0) heart = "liked";
	
	mz_item = item;

	var dist = "Location";
	if (mz_latitude != null && mz_longitude != null) {
		dist = distance(mz_latitude, mz_longitude, mz_item.latitude, mz_item.longitude, true);
	}
	
	var profilepicurl = "";
	var username = "";
	
	if (item.author.user_object != null) {
		if (item.author.user_object.profile_pic_url != null) {
			profilepicurl = item.author.user_object.profile_pic_url;	
		}
		if (item.author.user_object.username != null) {
			username = item.author.user_object.username;	
		}
	}
	var userurl = "/u/" + username;

	return $('<div>').attr('class', 'mzdet_card').attr('id', 'mzdet_card').html(''
			+ '    <img class="mzdet_photo" src="' + item.image_url_original + '" alt="" />'
			+ '    <div class="mzdet_desc">'+ item.text.parseURL() + '</div>'
			+ '    <a href="' + userurl + '"><img class="mzdet_author" src="' + profilepicurl + '" alt="" /></a>'
			+ '    <a href="http://www.mezz.com" target="_blank"><img class="mzdet_logo" src="/image/mezzlogo.png"></a>'
			+ '    <div class="mzdet_user"><a href="' + userurl + '">' + username + '</a></div>'
			+ '    <div class="mzdet_followers">' + item.author.user_object.follower_count + ' followers</div>'
			+ '    <img src="/image/placemark.png" class="mzdet_locicon">'
			+ '    <div class="mzdet_location" id="mzdet_location">' + dist + '</div>'
			+ '    <img class="mzdet_likeimage" src="/image/' + heart + '.png" alt="" border="0"/>'
			+ '    <div class="mzdet_like">' + item.like_count + '</div>'
			+ '    <div class="mzdet_clock"><img class="mzdet_clockimage" src="/image/clock.png"></div>'
			+ '    <div class="mzdet_time">' + item.timeDisplay + '</div>'
			+ '    <div class="mzdet_mapcontainer">'
			+ '     <a href="http://maps.google.com/?q=' + item.latitude + ',' + item.longitude + '" target="blank">'      
			+ '    	 <img class="mzdet_map" id="mzdet_map" src="' + getMapUrl() + '" />'
			+ '     </a>'
			+ '    </div>'
			+ '    <div class="mzdet_promo">' 
			+ '      ' + item.author.user_object.username + ' shared this using <a href="http://www.mezz.com/">mezz</a>'
			+ '      &mdash; A mobile app to discover and share what\'s happening nearby. Discover fun and interesting things, share with everyone locally and get popular. '
			+ '    </div>	'
			+ '    <div class="mzdet_appstorebadge"><a href="http://www.mezz.com/download" target="_blank"><img src="/image/appstorebadge.gif" border="0" class="mzdet_appstoreicon"/></a></div>'
			
			+ '    <div class="mzdet_twitter"><a href="https://twitter.com/share" class="twitter-share-button" data-url="' + item.mezz_url + '" data-text="' + item.text.replace(/"/g,"\'") + '" data-count="none" data-via="mezzapp">Tweet</a></div>'
			
			+ '    <div class="mzdet_facebook">'
			+ '    <div class="fb-like" href="' + item.mezz_url + '"data-send="false" data-layout="button_count" data-width="50" data-show-faces="false"></div>'
			+ '    </div>'
			);

}

function setLocation(lat, lon)
{
	mz_latitude = lat;
	mz_longitude = lon;
	
	var loc = document.getElementById('mz_location');
	if (mz_item == null || loc == null) return;

	loc.innerHTML = distance(lat, lon, mz_item.latitude, mz_item.longitude, true);

}

function getMapUrl()
{
	if (mz_item == null) return "";
	
    var mapurl = 'http://maps.googleapis.com/maps/api/staticmap?center=' + mz_item.latitude + ',' + mz_item.longitude + '&zoom=15&sensor=false&markers=color:red%7Clabel:A%7C' +
    				mz_item.latitude + ',' + mz_item.longitude + '&size=';
    
	if (document.documentElement.clientWidth <= 510) { 
		return mapurl + "60x60";
	} else if (document.documentElement.clientWidth <= 880) { 
		return mapurl + "480x150";
	} else if (document.documentElement.clientWidth <= 1140) { 
		return mapurl + "335x150";
	}
	return mapurl + "400x250";
}

function adjustMap()
{
	var map = document.getElementById('mz_map');
	if (map == null) return;
	
	var newurl = getMapUrl();
	if (newurl != map.src) {
		map.src = getMapUrl();
		window.scrollTo(0, 1);
	}
}

