// -- instance of ProductSvcClass for use in page
var ProductSvc = new ProductSvcClass();

// -- bookmark on digg
function diggBookmark(url, title, body, media, topic) 
{
	var loc = location.href;
	var submitTitle = document.title;
	var body = body || '';
	if (url && url.length>0) loc = url;
	if (title && title.length>0) submitTitle = title;
	var apos = loc.indexOf('#');
	loc = (apos>0?loc.substring(0,apos):loc);
	var strloc = 'http://digg.com/submit?url=' + encodeURIComponent(loc)
	+ '&title=' + encodeURIComponent(submitTitle)
	+ '&bodytext=' + encodeURIComponent(body);
	if (media && media.length) strloc += '&media=' + encodeURIComponent(media)
	if (topic && topic.length) strloc += '&topic=TOPIC' + encodeURIComponent(topic);
	// location.href= strloc;
	window.open(strloc);
	return false;
}      

var currentFeature;

function displayFeature(data, current)
{                             
	
	if (currentFeature)
	{
		if (currentFeature == current) return;
	}
	currentFeature = current;
	
	var data = data || {};
	var date = data.date || '';
	var title = data.title || '';
	var image = data.image || '';
	var link = data.link || '';
	
	$('loader').setStyle('display', 'block');
	
	var myImage = new Asset.image(image, {alt: '', onerror: function() {}, onload: function() {
		this.fade('hide');
		this.inject($('feature-images'), 'top');
		this.fade('in');
		
		$('featureDate').innerHTML = ''; //date;
		$('featureTitle').innerHTML = title;    
		$('featureLink').href = link;
		$('loader').setStyle('display', 'none');
		
	}});
	
	return false;
}

function showSocialMenu() 
{
    $('socializeMenu').setStyle('display', 'block');
    
    return false;
}

function closeSocialMenu() 
{
    $('socializeMenu').setStyle('display', 'none');
    
    return false;
}

// -- Bookmark on Facebook
function facebookBookmark(targetURL, titleStr) 
{
	var loc = location.href;
	if (targetURL && targetURL.length>0) loc = targetURL;
	var apos = loc.indexOf('#');
	loc = (apos>0?loc.substring(0,apos):loc);
	var t = document.title;
	if (titleStr && titleStr.length) t = titleStr;
	var myhref='http://www.facebook.com/sharer.php?u=' 
	+ encodeURIComponent(loc)
	+ '&title='
	+ encodeURIComponent(t);
	window.open(myhref,'sharer','toolbar=0,status=0,width=626,height=436');
	return false;
}

/**
 * make ajax call to get product retailers
 */
function getProductRetailersByMaterialNumber(materialNumber)
{
	ProductSvc.search(ProductSvc.RETAILERS + materialNumber, {materialNumber: materialNumber}, onGetProductRetailersByMaterialNumber);
}

/**
 * call to grab retailers finished
 *
 * @return void
 */
function onGetProductRetailersByMaterialNumber(text, xml)
{
	$('retailers').innerHTML = text;
}

/**
 * make ajax call to get product view
 *
 * @param integer productId
 * @return void
 */
function getProductView(productId)
{
	ProductSvc.search(ProductSvc.PRODUCT, {id: productId}, onGetProductView);
}

/**
 * display returned markup
 *
 * @param string text
 * @param string xml
 * @return void
 */
function onGetProductView(text, xml)
{
	// console.log(text);
	var cont = $('lookbook-overlay-container');
	cont.innerHTML = text;
	if ($('lookbook-close'))
	{
		$('lookbook-close').addEvent('click', function() {
			// cont.innerHTML = '';
			
			// this is needed for ie7's lameness
			cont.dispose();
			cont = new Element('div', {id:'lookbook-overlay-container'});
			cont.inject($('lookbook-thing'), 'top');
		
			return false; // prevent "#" in url
		});
	}
}