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

var currentScreenShot = 0;
var totalScreenShots = 0;
var images = new Array();
var captions = new Array();

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

	var match = /http:\/\/([^/]*)/.exec(window.location);
	var site = match[1];
	var parts = match[1].split(".");
	if (parts.length > 2) {
		site = parts[parts.length-2] + "." + parts[parts.length-1];
	}

	if (site=="gettheprospector.com") {
		images.push("/img/prospector1.jpg");
		images.push("/img/prospector2.jpg");
		images.push("/img/prospector3.jpg");
		images.push("/img/prospector4.jpg");
		images.push("/img/prospector5.jpg");
		images.push("/img/prospector6.jpg");
		captions.push("Tap and hold on any information on a webpage to copy the information for Prospector to use.");
		captions.push("Prospector will automatically prompt you to associate the information with a new or existing sales prospect.");
		captions.push("Each piece of information can be set to a preset type (Name, Address, Phone, Email) or a custom type you specify.");
		captions.push("A convenient list of all prospects you are working on is always available.");
		captions.push("You can email a list of your prospects to yourself or an assistant to later import the data into Salesforce.com, SugarCRM or a spreadsheet.");
		captions.push("");
		totalScreenShots = images.length;
  	} else {
		$('body').css('background','white');
		$('body').css('color','black');
		$('a').css('color','blue');
		$('a').css('text-decoration','none');
		$('ul a').css('color','white');
		$('a').css('text-decoration','none');
		$('.ui-widget-content').css('background','white');
		$('.ui-widget-content').css('color','black');
		$('#copyright').attr('href','http://www.myhairdo.info');
		$('#copyright').html('Dean Cox');
		$('#purchasetabtext').html('Available soon');
		images.push("/img/ms_ss1.jpg");
		images.push("/img/ms_ss2.jpg");
		images.push("/img/ms_ss3.jpg");
		images.push("/img/ms_ss4.jpg");
		images.push("/img/ms_ss5.jpg");
		captions.push("Create a New Style,  Capture styles from magazines or friends or view your styles. Simply select from the main menu screen.");
		captions.push("When the stylist has finished your hair, take pictures using the iPhone camera of your latest style - using the simple interface - You can crop and re-size your photo in the application.");
		captions.push("Simply enter notes about your style so that you can remember how you achieved your fantastic look, log which colors were used, and other details about your style so you can recall the next time you visit the salon.");
		captions.push("Scroll through your styles with a flick of your finger and select the style you want to discuss with your stylist. You can choose from your styles or styles you have captured from magazines or friends.");
		captions.push("View information about your style and click on the photos to enlarge. You can also share your style on your facebook profile directly from the Application, so your friends can immediately comment or like your style. ");
		totalScreenShots = images.length;
	}
	var request = $.ajax(
		{
			url: '/service/'+apiKey+'/applications/bysite/'+site,
			type:'get',
			dataType:'json',
			success: receiveApplicationData,
			error: failedRequest
		}
	);

	setInterval ("nextScreenShot()", 12000);

		document.getElementById('scrollableArea').innerHTML = "";
		document.getElementById('scrollableArea2').innerHTML = "";
		html = "";
		html2 = "";
		for (idx = 0; idx < images.length; idx++) {
			html += '<div class="screenshotItem"><img width="160" height="240" class="screenshotImage" src="' + images[idx] + '"></div>';
			html2 += '<div class="captionItem">' + captions[idx] + '</div>';
		}
		document.getElementById('scrollableArea').innerHTML = html;
		document.getElementById('scrollableArea2').innerHTML = html2;
}
		function nextScreenShot() {
			currentScreenShot++;
			if (currentScreenShot == totalScreenShots) {
				currentScreenShot = 0;
			}
			img = document.getElementById('scrollableArea');

			img.style.marginLeft = (-160 * currentScreenShot) + "px";			

			cap = document.getElementById('scrollableArea2');

			cap.style.marginLeft = (-240 * currentScreenShot) + "px";			
			return;
		}
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['name'] && application['name'] != "") {
		$('#appname').html(application['name']);
	}

	if (application['listingimg'] && application['listingimg'] != "") {
		$('#icon').attr('src','http://assets.tap4help.com/productimgs/'+application['listingimg']);
	}

	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['price'] && application['price'] != "") {
		price = "$" + application['price'];
	}

	if (application['itunesid'] && application['itunesid'] != "") {
		purchasehtml = application['name'] + " is available now for " + price + " in the iPhone App Store.<br /><br />" + storehtml;
	} else {
		purchasehtml = application['name'] + " is available soon for " + price + " in the iPhone App Store.<br /><br />" + storehtml;
	}

	$('#purchasetabcontent').html(purchasehtml);

	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');
	}

	if (application['forumname'] && application['forumname'] != "") {
		var forumhtml = 'Coming soon...';
		$('#forumtabcontent').html(forumhtml);
//		$('#forumtabhead').css('display','block');
		var request = $.ajax(
		{
			url: '/service/'+apiKey+'/forums/'+application['forumname']+'/rss',
			type:'get',
			dataType:'xml',
			success: receiveForumData,
			error: function() {}
		}
		);
	}

}

function receiveForumData(data) {
	alert(data);
}

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