var MA_PS_3121_widgetRoot = 'http://mawidget.marketamerica.com';
function MA_PS_3121_includeCss(path)
{
    var linkElement  = document.createElement('link');
    linkElement.rel = 'stylesheet'
    linkElement.type = 'text/css';
    linkElement.href = path;
    document.getElementsByTagName('head')[0].appendChild(linkElement);
}
if(typeof(window.maJQueryCSSLoaded)=='undefined'){
  MA_PS_3121_includeCss(MA_PS_3121_widgetRoot + '/ext/jquery/css/ui-lightness/jquery-ui-1.8rc3.custom.css');
  window.maJQueryCSSLoaded=true;
}
if (typeof(window.jQuery) == 'undefined') {
    if(typeof(window.majQueryScriptOutputted)=='undefined'){
        //only output the script once..
        window.majQueryScriptOutputted = true;
        document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + MA_PS_3121_widgetRoot + "/ext/jquery/js/jquery-1.4.2.js" + "\"></scr" + "ipt>");
    }
}
if(typeof(window.majQueryUIScriptOutputted)=='undefined'){
    //only output the script once..
    window.majQueryUIScriptOutputted = true;
    document.write("<scr" + "ipt type=\"text/javascript\" src=\"" + MA_PS_3121_widgetRoot + "/ext/jquery/js/jquery-ui-1.8rc3.custom.min.js" + "\"></scr" + "ipt>");
}
document.write('<div id="MA_PS_3121_idMarketAmericaProductSearchWidgetPlaceholder"></div>');
var MA_PS_3121_searchWidgetParentId = 'MA_PS_3121_idMarketAmericaProductSearchWidgetPlaceholder';
var strMarketAmericaSearchWidgetContent = '\
<!-- Market America Search Widget -->\
<div id="MA_PS_3121_divMarketAmericaSearchWidget" class="divMarketAmericaSearchWidget" style="display: none;">\
    <div class="divMarketAmericaSearchWidgetContent">\
        <table width="95%">\
            <tr>\
                <td>\
					  <div id="MA_PS_3121_divMASearchWidgetFiller" class="divMASearchWidgetFiller" style="margin-left:3px;text-align:justify;">\
						<img id="MA_PS_3121_divMASearchWidgetFillerImg" class="divMASearchWidgetFillerImg" src="http://mawidget.marketamerica.com/data/ma_logo.jpg" alt="Market America Cashback Logo" border="0" />\
						<p>Welcome to Market America. Take advantage of a wide range of Internet shopping Web sites, latest news, fashion, weather, stocks, entertainment and more. When you make Market America your home page, one click will take you straight to e-mail, Internet search, and hundreds of shopping Web sites with our partner stores like Wal-Mart, Target and Barnes & Noble, to name a few.</p>\
						<p>Get 2-35% cashback for your qualifying purchases, and refer your friends to also receive 1/2% cashback on the purchases they make where the ma Cashback logo is displayed!</p>\
					  </div>\
                    <ul class="ulProductList maUlProductList" id="MA_PS_3121_maUlProductList">\
                    </ul>\
                </td>\
            </tr>\
        </table>\
    </div>\
    <table width="100%" cellspacing="0" cellpadding="0" style="text-align: center;" class="tabButtonDefault">\
        <tr>\
            <td width="5px" style="border-top: 1px solid #CCCCCC;"><nobr>&nbsp;&nbsp;</nobr></td>\
            <td width="0px" style="border-right: 1px solid #CCCCCC;">\
                <select id="MA_PS_3121_maPSCategories" class="maPSCategories" style="margin-right: 5px; width: 115px;visibility:visible" onchange="MA_PS_3121_ChangeCategory(this)">\
                    <option>All Dapartments</option>\
                    <option>- Anti-Aging</option>\
                    <option>- Apparel, Jewelry &amp; S...</option>\
                    <option>- Automotive &amp; Tools</option>\
                    <option>- Beauty &amp; Personal C...</option>\
                    <option>- Books, Movies &amp; Vid...</option>\
                    <option>- Cell Phones &amp; Cordl...</option>\
                    <option>- Electronics, Computers ...</option>\
                    <option>- Gifts &amp; Occasions</option>\
                    <option>- Grocery</option>\
                </select>\
            </td>\
            <td width="100%">\
                <input type="text" id="MA_PS_3121_maSearchText" class="maSearchText" style="width: 90%; font-size: 11px; border: 1px solid gray; height: 13px;visibility:visible" value="isotonix" onkeydown="if (event.keyCode == 13) {MA_PS_3121_Search(); return false;}"/>\
            </td>\
            <td width="30px">\
                <div class="searchSubmit maSearchSubmit" id="MA_PS_3121_maSearchSubmit" style="height: 13px; visibility:visible; width:45px;"><a href="#" onclick="MA_PS_3121_Search(); return false;">Search</a></div>\
            </td>\
            <td width="5px" style="border-top: 1px solid #CCCCCC;"><nobr>&nbsp;&nbsp;</nobr></td>\
        </tr>\
    </table>\
</div>\
<!-- End of Market America Search Widget -->';
document.write(strMarketAmericaSearchWidgetContent);
function php_urlencode (str) {
	str = escape(str);
	return str.replace(/[*+\/@]|%20/g,
		function (s) {
			switch (s) {
			case "*": s = "%2A"; break;
			case "+": s = "%2B"; break;
			case "/": s = "%2F"; break;
			case "@": s = "%40"; break;
			case "%20": s = "+"; break;
			}
			return s;
		}
	);
}
function MA_PS_3121_includeJS(path)
{
    var fileref=document.createElement("script");
    fileref.setAttribute("type","text/javascript");
    fileref.setAttribute("src", path);
    if (typeof fileref!="undefined")
        document.getElementsByTagName("head")[0].appendChild(fileref);
}
function MA_PS_3121_ChangeCategory(selectObject)
{
	var idstr = "#" + selectObject.id;
	var catValue = jQuery(idstr).val();
	var catId = '';
	if(!catValue) return;
	if(catValue=='0-0')
	{
		MA_PS_3121_LoadInitCat(idstr);
		return;
	}
	var catPart = catValue.toString().split('-');
	if(!catPart || catPart.length!=2) return;
	
	if(catPart[1]=='0')
	{
		catId = catPart[0];
		return;
	}
	else
	{
		if(catPart[0]!='0') return;
		catId = catPart[1];
	}
	
	jQuery(idstr).html("");
	var htmlData = '';
	htmlData += '<option value="0-0">All Departments</option>\n';
	var category = '';
	for (var i = 0; i < MA_PS_3121_maCatList.length; i++)
    {
		if(MA_PS_3121_maCatList[i].Id==catValue)
		{
			category = MA_PS_3121_maCatList[i];
			break;
		}
	}
	if(category)
	{
		htmlData += '<option value="' + catId + '-0">- All ' + category.Name + '</option>\n';
		if(category.SubList && category.SubList.length>0)
		{
			for (var i = 0; i < category.SubList.length; i++)
		    {
				var subCat = category.SubList[i];
				htmlData += '<option value="' + subCat.Id + '">-- ' + subCat.Name + '</option>\n';
			}
		}		
	}	
	jQuery(idstr).html(htmlData);
	jQuery(idstr).val('' + catId + '-0');
}
var MA_PS_3121_maCategoriesLoaded = false;
var MA_PS_3121_maCatList = null;
function MA_PS_3121_LoadCategory()
{
    var url = "http://mawidget.marketamerica.com/LC.aspx?format=json&callback=MA_PS_3121_ShowCategories";
    MA_PS_3121_includeJS(url);
}
function MA_PS_3121_ShowCategories(jsonData)
{
	if(MA_PS_3121_maCategoriesLoaded==true) return;
    var data = eval(jsonData);
    if(data==null|| data.length ==0) return;
    MA_PS_3121_maCatList = data;
    MA_PS_3121_LoadInitCat("#MA_PS_3121_maPSCategories");
    MA_PS_3121_maCategoriesLoaded = true;
}
function MA_PS_3121_LoadInitCat(idstr)
{
	jQuery(idstr).html("");
	var htmlData = '';
	htmlData += '<option value="0-0">All Departments</option>\n';
	for (var i = 0; i < MA_PS_3121_maCatList.length; i++)
    {
		htmlData += '<option value="' + MA_PS_3121_maCatList[i].Id + '">- ' + MA_PS_3121_maCatList[i].Name + '</option>\n';
	}
	jQuery(idstr).html(htmlData);
}
var MA_PS_3121_maSearchText = 'isotonix';
var MA_PS_3121_maCatId = '';
function MA_PS_3121_Search()
{
	MA_PS_3121_maSearchText = jQuery("#MA_PS_3121_maSearchText").val();
	var catValue = jQuery("#MA_PS_3121_maPSCategories").val();
	var catId = '';
	if(catValue)
	{
		var catPart = catValue.toString().split('-');
		if(catPart && catPart.length==2)
		{
			if(catPart[1]=='0')
				catId = catPart[0];
			else
				catId = catPart[1];
		}
	}
	MA_PS_3121_maCatId = catId;
	var url = 'http://mawidget.marketamerica.com/PSearch.aspx?wid=3121&q=' + php_urlencode(MA_PS_3121_maSearchText) + '&page=1&catID=' + catId + '&portalID=sevenstrong&PCID=5943577B54405D&refEmail=2A37310C2037393923082A342E37301F4B372822&prdCountry=USA&merchCountry=USA&format=json&callback=MA_PS_3121_ShowProducts';
  MA_PS_3121_includeJS(url);
	jQuery("#MA_PS_3121_divMASearchWidgetFiller").hide();
}
function MA_PS_3121_ShowProducts(jsonData)
{
	jQuery("#MA_PS_3121_maProductRange").html('');	
	jQuery("#MA_PS_3121_maProductPageLinks").html('');
	jQuery("#MA_PS_3121_maUlProductList").html('');
	if(!jsonData) return;
	var data = eval(jsonData);
  if(data==null) return;
  var rangeData='';
  if(data.TotalProduct>0) rangeData = data.Start + ' - ' + data.End + ' of <b>' + data.TotalProduct + '</b> results';
  jQuery("#MA_PS_3121_maProductRange").html(rangeData);
	jQuery("#MA_PS_3121_maProductPageLinks").html(data.TopLinks);
	//now show products
	var htmlData = '';
	if(data.ProductList==null|| data.ProductList.length ==0) return;
	for (var i = 0; i < data.ProductList.length; i++)
    {
		var product = data.ProductList[i];
		htmlData += '<li style="float: left;">\n';
		htmlData += '<a href="' + product.Url + '" target="_blank">\n';
		htmlData += '<table style="width: 133px;">\n';
		htmlData += '<tr>\n';
		htmlData += '<td align="center" valign="top">\n';
		htmlData += '<img src="' + product.Image + '" style="height:60px" alt="' + escape(product.Name) + '" />\n';
		htmlData += '<a href="' + product.Url + '" target="_blank"><div class="productSubmit">Buy now</div></a>\n';
		htmlData += '</td><td align="center" valign="top">\n';
		htmlData += '<span>' + product.Name + '<br /></span>\n';
		htmlData += '<span class="productPrice">' + product.Price + '</span>\n';
		if(product.HasCB)
		{
			htmlData += '<table cellpadding="0" cellspacing="0">\n';
			htmlData += '<tr><td>\n';
			htmlData += '<img src="http://mawidget.marketamerica.com/themes/gray/png/cashback.png" class="cashBack" alt="Cashback" />\n';
			htmlData += '</td><td>\n';
			htmlData += '<span class="spanCashBack">&nbsp;' + product.CB + '</span>\n';
			htmlData += '</td></tr></table>\n';
		}
		htmlData += '</td></tr></table>\n';
		htmlData += '</a></li>\n';
	}
	jQuery("#MA_PS_3121_maUlProductList").html(htmlData);
}
function MA_PS_3121_FetchPage(pageNum)
{	
	var url = 'http://mawidget.marketamerica.com/PSearch.aspx?wid=3121&q=' + php_urlencode(MA_PS_3121_maSearchText) + '&page=' + pageNum + '&catID=' + MA_PS_3121_maCatId + '&portalID=sevenstrong&PCID=5943577B54405D&refEmail=2A37310C2037393923082A342E37301F4B372822&prdCountry=USA&merchCountry=USA&format=json&callback=MA_PS_3121_ShowProducts';
    MA_PS_3121_includeJS(url);
}
/**
 * Lays-out the widget
 **/
function MA_PS_3121_marketAmericaSearchWidgetLayout()
{
    jQuery('.divMarketAmericaSearchWidgetContent').each(function (index, item) {
        jQuery(item).height(jQuery('#MA_PS_3121_divMarketAmericaSearchWidget').height() - 20);
    });
    jQuery('.divMarketAmericaSearchWidgetScrollableContent').each(function(index, item) {
        jQuery(item).parents('td:first').each(function(indexTr, itemTr) {
            if (itemTr.clientHeight) {
                jQuery(item).height(100);
                jQuery(item).height(itemTr.clientHeight);
                jQuery(item).height(itemTr.clientHeight);
                jQuery(item).height(itemTr.clientHeight);
                jQuery(item).height(itemTr.clientHeight);
            }
        })
    });
}
function MA_PS_3121_initJQuery()
{
    //if the jQuery object isn't available
    if (typeof(jQuery) == 'undefined') {
        setTimeout("MA_PS_3121_initJQuery()", 50);
    } else {                        
        jQuery(function() {
			jQuery(document).ready(function() {
				if (jQuery.browser.msie) {
					MA_PS_3121_includeCss(MA_PS_3121_widgetRoot + '/themes/gray/ie_s.css');
				}
				if (jQuery.browser.safari) {
				    MA_PS_3121_includeCss(MA_PS_3121_widgetRoot + '/themes/gray/safari_s.css');
				}
				if (jQuery.browser.mozilla) {
				    MA_PS_3121_includeCss(MA_PS_3121_widgetRoot + '/themes/gray/mozilla_s.css');
				}
			    // Show the widget div
			    jQuery('#MA_PS_3121_divMarketAmericaSearchWidget').style = '';
			    // Initialize the floating window
			    jQuery('#MA_PS_3121_divMarketAmericaSearchWidget').dialog(
			        {
			        position : [0,0],
			        height: 228,
			        width: 313,
			        minHeight: 206,
			        minWidth: 313,
			        maxHeight: 295,
			        maxWidth: 407,
			        draggable: false,
			        resizable: false,
			        resize: function(event, ui) {
			            MA_PS_3121_marketAmericaSearchWidgetLayout();
			        }
			    });
			    // Initialize the floating window header
			    // and mark the header using the CSS class
			    var divMarketAmericaSearchWidgetContainer = jQuery('#MA_PS_3121_divMarketAmericaSearchWidget').parent();
			    divMarketAmericaSearchWidgetContainer.attr('id', 'MA_PS_3121_divMarketAmericaSearchWidgetContainer');
			    divMarketAmericaSearchWidgetContainer.addClass('divMarketAmericaSearchWidgetContainer');
			    // Floating window header content
			    var divMarketAmericaSearchWidgetHeaderContent = divMarketAmericaSearchWidgetContainer.children(":first");
			    divMarketAmericaSearchWidgetHeaderContent.empty();
			    // Floating window header tab control
			    var divMarketAmericaSearchWidgetHeaderTabs = '';
			    divMarketAmericaSearchWidgetHeaderTabs += '<table width="100%" cellspacing="0" cellpadding="0" style="text-align: center;">\
			        <tr>\
			            <td width="125px" style="border-top: 1px solid #CCCCCC; border-right: 1px solid #CCCCCC;">\
			                <div class=\"divMarketAmericaSearchWidgetHeaderLogo\">&nbsp;</div>\
			            </td>\
			            <td>\
			                <span id="MA_PS_3121_maProductRange" class="maProductRange">&nbsp;</span>\
						</td>\
						<td>\
							<span id="MA_PS_3121_maProductPageLinks" class="maProductPageLinks">\
							</span>\
			            </td>\
			        </tr>\
			    </table>';
			    divMarketAmericaSearchWidgetHeaderContent.append(divMarketAmericaSearchWidgetHeaderTabs);
			    if (null != MA_PS_3121_searchWidgetParentId)
			    {
			        // Move widget's container to the widget's placeholder
			        jQuery('#MA_PS_3121_divMarketAmericaSearchWidgetContainer').each(function (index, value)
			        {
			            jQuery(value).appendTo(jQuery('#' + MA_PS_3121_searchWidgetParentId));
			        });
			        // Make widget's container position relative
			        jQuery('#MA_PS_3121_divMarketAmericaSearchWidgetContainer').each(function (index, value)
			        {
			            jQuery(value).css('position', 'relative');
			            jQuery(value).css('top', '0');
			        });
			    }
			    MA_PS_3121_marketAmericaSearchWidgetLayout();
				MA_PS_3121_LoadCategory();
			});
		});
	}
}
if(typeof(window.maPSWidgetCSSLoaded)=='undefined'){
  MA_PS_3121_includeCss(MA_PS_3121_widgetRoot + '/themes/gray/theme_s.css');
  window.maPSWidgetCSSLoaded=true;
}
MA_PS_3121_initJQuery();

