jQuery(document).ready(function() {
	jQuery("ul.sf-menu").superfish();
	jQuery("a.lightbox,a[href$=.jpg],a[href$=.png],a[href$=.gif]").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	600,
		'speedOut'		:	200
		/*'titlePosition'	:	'over'*/
	});
});

/**
 * AJAX load rates for a given fax number.
 */
function pf_get_rate_for_number(frm)
{
	$('#rates_table_loading').hide();
	$('#rates_result').hide();
    var fax_number = $('#fax_number').val();

    if('' == fax_number)
    {
        $('#please_enter_fax_number').fadeIn();
        return;
    }
    $('#please_enter_fax_number').hide();

    $('#rates_table_loading').show();
	$.post('/api.php?function=NumberInfo/GetPagePrice', {'faxnumber' : fax_number}, function(d) {
		//console.debug(NumberInfo);
		if(NumberInfo.result.code != "success")
			html = NumberInfo.result.message;
		else
		{
			html = "<br/><table><tr>";
			html += "<td nowrap=\"nowrap\">" + NumberInfo.Price.country_code + " " + NumberInfo.Price.description + " " + NumberInfo.Price.number_type + "</td>";
			html += "</tr></table>";
			html += "<br/>";
			html += "<table width='100%' cellpadding=10><tr>";
			html += "<tr>";
			html += "<td nowrap=\"nowrap\" align=center><h1>" + pamfax_current_currency + " " + (Math.round((NumberInfo.Price.priceperpage * 0.685) * 1000) / 1000) + " to " + pamfax_current_currency + " " + NumberInfo.Price.priceperpage + " per page</h1><br/><a href='/shop/pamfax_credit_packs'><img src='/shop/images/small/pamfax20-proplan.png' border=0 /><br>Buy some PamFax Credit now (NO subscription)</a></td>";
			html += "<td nowrap=\"nowrap\" align=center><h1>" + pamfax_current_currency + " " + (Math.round((NumberInfo.Price.priceperpage * 0.685) * 1000) / 1000) + " per page</h1><br/><a href='/shop/pamfax_plans'><img src='/shop/images/small/pamfax20-proplan.png' border=0 /><br>Buy PamFax Plan incl. personal fax-in number now</a></td>";
			html += "</tr></table>";
			$('#fax_number').val(NumberInfo.Price.number)
		}
		$("#rates_result").html(html);
		$('#rates_table_loading').hide();
		$('#rates_result').fadeIn();
	}, "script");
}

var uicontainerid = 0;
function ShowFaxInInfo(title, countryprefix, countrycode, require_pi, state)
{
	if(countrycode == "US" && !state)
	{
		state = $("#statesdivUS select:first").val();
		if( !state || typeof(state)=='undefined' )
			state = "General";
	}
	$.post('/api.php?function=Shopping/ListFaxInAreacodes', {'country_code' : countrycode, 'state' : state}, function(d) {
//		console.debug(d);

		var html = "<p>" + _websiteStrings.TXT_NUMBER_INFO_1 + "</p>";

		if(Shopping["AreaCodes"].content.length == 0)
			html += _websiteStrings.ERR_NO_FAXIN_NUMBER_IN_COUNTRY;
		else
		{
			if($("#statesdiv" + countrycode).length > 0)
			{
				html += "<p><div style='width:40%;float:left'>" +_websiteStrings.TXT_STATE + "</div>"
				html += $("#statesdiv" + countrycode).html().replace(/<select/i, "<select id=\"state_select\" onchange=\"$('#message_container_" + uicontainerid + "').dialog('destroy'); ShowFaxInInfo('" + title + "', '" + countryprefix + "', '" + countrycode + "', " + require_pi + ", this.value)\"");
				html += "</p><br class='clearer'/>";
			}
			if(Shopping["AreaCodes"].content.length  > 20)
			{
				html += "<div style='width:40%;float:left'>" + _websiteStrings.TXT_GROUP_SEARCH + "</div>";
				html += "<input id=\"inpacfilter\" onkeyup=\"inpacfilter_keyup('message_container_" + uicontainerid + "')\" type=\"text\" style=\"width: 200px\" />";
			}
			html += "<div style=\"height: 150px; overflow: auto; border: 1px solid gray; margin-top: 5px; margin-bottom: 5px; padding: 3px\">";
			for(k in Shopping["AreaCodes"].content)
			{
				ac = Shopping["AreaCodes"].content[k];
				html += "<div class=\"country_info_entry\">" + ac.city + " <font color='gray'>(+" + countryprefix + " " + ac.areacode + "...)</font>";
				if(require_pi)
					html += " *";
				html += "</div>";
			}
			html += "</div>";
			html += "<p>" +_websiteStrings.TXT_NUMBER_INFO_2 + "</p>";
		}
		var dialog_content   = $('<div></div>').attr('id',"message_container_content").html(html);
		var dialog_container = $('<div></div>').attr('id','message_container_' + uicontainerid);
		$('body').append(dialog_container);
		dialog_container.attr('title', title);
		dialog_container.append(dialog_content);

		var buts={};
		buts[_websiteStrings.BTN_ABORT] = function(){$(this).dialog('destroy');};

		$("#message_container_" + uicontainerid).dialog({
			modal: true,
			width: 550,
			resizable: false,
			draggable: false,
			buttons: buts,
			closeOnEscape : true,
			open : function() { $('#message_container_' + uicontainerid + ' #state_select').val(state); $('#message_container_' + uicontainerid + ' #inpacfilter').focus(); }
		});
//		$(".ui-button-text:last").css("border", "1px solid red");
		$(".ui-dialog-buttonpane").append("<a href=\"/shop/pamfax_professional_fax_plan\"><img class=\"buynowimgdlg\" src=\"/images/sign_up_now_" + site_language + ".png\" /></a>")
		uicontainerid ++;
	}, "script");

}

function inpacfilter_keyup(dlgid)
{
	$('.country_info_entry').each( function(){
		if( $(this).text().toLowerCase().indexOf($('#' + dlgid + ' #inpacfilter').val().toLowerCase()) > -1 )
			$(this).show();
		else
			$(this).hide();
	});
}
