var apiKey = "756b563010dec7e5ed0f94c8defd5795";
var currentAppKey = "";
var application = null;

function init() {
        $('#tabs').tabs();
	$('#tabs').css('visibility','visible');

	var match = /view\/(.*)/.exec(window.location);
	currentAppKey = match[1].replace(/#.*/,'');
	currentAppKey = currentAppKey.replace(/\?.*/,'');
	var request = $.ajax(
		{
			url: '/service/'+apiKey+'/applications/'+currentAppKey,
			type:'get',
			dataType:'json',
			success: receiveApplicationData,
			error: failedRequest
		}
	);
}
function receiveVendorInfo(vendor) {

	html = '';

	html = "<h1>"+vendor['name']+"</h1><ul>";

	if (vendor["website"]) {
		html += "<li><a href='"+vendor['website']+"'>Website</a></li>";
	}
	if (vendor["supportemail"]) {
//		html += "<li><a href='mailto:"+vendor['supportemail']+"'>"+vendor['supportemail']+"</li>";
	}

	html += "</ul><br />";



	$('#vendorinfo').html(html);

	$('#otherapps_vendorname').html(vendor['name']);
	$('#otherapps').css('display','block');
}
function receiveVendorApps(apps) {
	apps = apps['rows'];

	html = "";

	for (idx = 0; idx < apps.length; idx++) {
		if (apps[idx]['id'] != currentAppKey) {
			html += '<li><a href="/view/'+apps[idx]['value']['shortname']+'">'+apps[idx]['value']['name']+'</a></li>';
		}
	}
	$('#otherappslist').html(html);
}

function receiveApplicationData(inData) {
	application = inData;

        var headhtml = "";
	var html = "";
	var faqhtml = "";
	var storehtml = "";
	var price = "";


	document.title = application['name'] + " on Tap4Help";

	if (application['price'] && application['price'] != "") {
		price = " - $" + application['price'];
	}
	if (application['listingimg'] && application['listingimg'] != "") {
		headhtml += "<img style='margin-right: 15px' src='http://assets.tap4help.com/productimgs/"+application['listingimg'] + "' />";
	}
	headhtml += "<h2>" + application['name'] + price + "</h2>";
	//html += "<h3><a href='#question'>Have a question?</a></h3>";

	if (application['itunesid'] && application['itunesid'] != "") {
		storehtml += '<a href="http://phobos.apple.com/WebObjects/MZStore.woa/wa/viewSoftware?id='+application['itunesid']+'&mt=8"><img src="/img/appstore.png" border="0" /></a>';
	}

	if (application['description'] && application['description'] != "") {
		html += "<p>"+application['description'].replace(/\n/g,"<br />")+"</p>";
	}

	if (application['faqs'] && application['faqs'] != "") {
		faqhtml += '<div id="faqaccordion" style="height: 400px">';
		for (idx = 0; idx < application['faqs'].length; idx++) {
			faqhtml += '<h3><a href="#">'+application['faqs'][idx]['question']+'</a></h3>';
			faqhtml += '<div><p>'+application['faqs'][idx]['answer']+'</p></div>';
		}
		faqhtml += '</div>';
	}

	if (application['videoembed'] && application['videoembed'] != "") {
		$('#videotabhead').css('display','block');
		$('#videotabcontent').html(application['videoembed']);
	}

	if (application['vendor'] && application['vendor'] != "") {
		var request = $.ajax(
			{
				url: '/service/'+apiKey+'/vendors/'+application['vendor'],
				type:'get',
				dataType:'json',
				success: receiveVendorInfo,
				error: failedRequest
			}
		);
		var request2 = $.ajax(
			{
				url: '/service/'+apiKey+'/vendors/'+application['vendor']+'/applications/',
				type:'get',
				dataType:'json',
				success: receiveVendorApps,
				error: failedRequest
			}
		);
	}

	$('#contentheading').html(headhtml);
	$('#faqblock').html(faqhtml);
	$('#faqaccordion').accordion( { autoHeight: false} );
	$('#storelinkblock').html(storehtml);
	$('#applisting').html(html);

	if (application['sidebarcontent'] && application['sidebarcontent'] != "") {
		var sbhtml = "<h1>Extra Information</h1>";
		sbhtml += application['sidebarcontent'];

		try {
			$('#extrasbcontent').html(sbhtml);
		} catch (e) {
			$('#extrasbcontent').html('');
		}
		$('#extrasbcontent').css('display','block');
	}

}

function failedRequest(transport) {
	//alert(transport);
	alert('Unknown Load Error');
}
