/* Author: 
	Barnaby Walters, 2011
*/

var timeout;

function nextImage(timeDelay)
{
	if ($('.slideShowImage.visible').attr('id') !== 'img' + ($('.slideShowImage').length - 1))
	{
		// If 
		// Advance to the next image
		$('.slideShowImage.visible').removeClass('visible').addClass('hidden').next().removeClass('hidden').addClass('visible');
	}
	else
	{
		// Loop to the first image
		$('.slideShowImage').addClass('hidden').removeClass('visible').first().addClass('visible').removeClass('hidden');
	}
	
	clearTimeout(timeout);
	timeout = setTimeout('nextImage(' + timeDelay + ')', timeDelay);
}

if (window.navigator && window.navigator.loadPurpose === "preview")
{
	window.location.href = "http://waterpigs.co.uk/topsites_preview/";
}

$('#abcPigHelp').mouseover(function () { $('#abcPigInfo').show('fast'); }).mouseout(function () { $('#abcPigInfo').hide('fast'); });
$('.jsHidden').addClass('hidden'); // Hide all elements that can be shown via javascript

/* !eCards functions */

// JavaScript for Ecard UI

// Glob Vars

var pickedImage;
var submitSearchesWPSM;

// Tab Handling functions:

function showTab(tabId)
{
	$('#imgTabWrap').children().hide();
	$('#imgTabWrap').find('#'+tabId).show();
}

// AJAX WPSM Searching Functions

function getWPSMSearchResults()
{
	// First:  Store search string in var
	var searchString = $('#wpsmSearch').val();
	
	// Perform AJAX request and put results in wpsmSearchContainer
	
	$('#wpsmSearchContainer').load('ajax/search.php', "searchString="+searchString);
}

function getWPSMTune(tuneId)
{
	// Perform AJAX request that loads the ABC of the selected tune into #attachedAbc
	
	$.get('/wpsm/tune.php', {'id':tuneId}, function (data)
		 {
			$('#attachedAbc').val(data); 
		 });
	
	// Then return #wpsmSearchContainer to search state.
}

// Script that handles form events and calculations for the passive sensor calculator

/* !RCX Sensor Calculator Functions */

function moveArrow(offset)
{
	$('#arrowId').css('-webkit-transition', 'top 0.2s');
	$('#arrowId').css('top', offset);
}

function setUpCalc()
{
	// make object (assoc. array) from form elements
	
	fields = {}; // Use an object as it works better than an assoc. array.
	
	form = $('#passiveCalc');
	
	fields.ohms = form.find('#resistorVal');
	fields.raw = form.find('#rawVal');
	fields.light = form.find('#lightVal');
	fields.temp = form.find('#tempVal'); // Store our fields in an array
	
	// Assign onKeyUp handlers:
	
	fields.ohms.keyup(function () {calcFromOhms(this.value);});
	fields.raw.keyup(function () {calcFromRaw(this.value);});
	fields.light.keyup(function () {calcFromLight(this.value);});
	fields.temp.keyup(function () {calcFromTemp(this.value);});
	
	fields.ohms.focus(function () {moveArrow(0); calcFromOhms(this.value);}).blur(function () { calcFromOhms(this.value); });
	fields.raw.focus(function () {moveArrow(30); calcFromRaw(this.value)}).blur(function () { calcFromRaw(this.value) });
	fields.light.focus(function () {moveArrow(60); calcFromLight(this.value)}).blur(function () { calcFromLight(this.value) });
	fields.temp.focus(function () {moveArrow(90); calcFromTemp(this.value)}).blur(function () { calcFromTemp(this.value) });
}

// These functions are called by thir respective inputs.

function calcFromOhms(value)
{
	moveArrow(0);
	
	value = parseFloat(value);
	raw = Math.round((value*1023)/(value+10000));
	if (raw < 0) { raw = 'N/A'; } else if (raw > 1023) { raw = 1023; }
	fields.raw.val(raw);
	
	if (raw < 392) { light = 100; } else { light  = Math.round((1023-raw)*(25/156)); }
	if (light < 0) { light = 'N/A';}
	fields.light.val(light);
	
	temp = Math.round((785-raw)/8 *10)/10;
	if (temp < -20) { temp = -20; } else if (temp > 70) {temp = 70; }
	fields.temp.val(temp);
}

function calcFromRaw(value)
{
	moveArrow(30);
	
	raw = parseFloat(value);
	
	// Do calculations.  Tricky!
	
	ohms = Math.round((10000*raw)/(1023-raw));
	if (ohms < 0) { ohms = "N/A"; }
	fields.ohms.val(ohms);
	
	if (raw < 392) { light = 100; } else { light  = Math.round((1023-raw)*(25/156)); }
	if (light < 0) { light = 'N/A';}
	fields.light.val(light);
	
	temp = Math.round((785-raw)/8 *10)/10;
	if (temp < -20) { temp = -20; } else if (temp > 70) {temp = 70; }
	fields.temp.val(temp);
}

function calcFromLight(value)
{
	moveArrow(60);
	
	light = parseFloat(value);
	
	raw = Math.round(-1*((light/(25/156))-1023));
	if (raw < 0) {raw = "N/A";} else if (raw > 1023) {raw = 1023;}
	fields.raw.val(raw);
	
	ohms = Math.round((10000*raw)/(1023-raw));
	if (ohms < 0) { ohms = "N/A"; }
	fields.ohms.val(ohms);
	
	temp = Math.round((785-raw)/8 *10)/10;
	if (temp < -20) { temp = -20; } else if (temp > 70) {temp = 70; }
	fields.temp.val(temp);
}

function calcFromTemp(value)
{
	moveArrow(90);
	
	temp = parseFloat(value);
	
	raw = Math.round(755-(temp*8));
	if (raw < 0) { raw = 'N/A'; } else if (raw > 1023) { raw = 1023; }
	fields.raw.val(raw);
	
	ohms = Math.round((10000*raw)/(1023-raw));
	if (ohms < 0) { ohms = "N/A"; }
	fields.ohms.val(ohms);
	
	if (raw < 392) { light = 100; } else { light  = Math.round((1023-raw)*(25/156)); }
	if (light < 0) { light = 'N/A';}
	fields.light.val(light);
}
