/*
 * WideTag - iPhone App JS
 * last revision 2009 01 06
 *
 * Copyright (C) 2009 by Davide 'Folletto' Casali <folletto AT gmail DOT com>
 *
 */
 
// widget code
var normalWidget = "%3C%21--%20%5C/%20WideNoise%20Widget%20Code%20--%3E%0A%3Cscript%20type%3D%22text/javascript%22%3E%0Adocument.write%28unescape%28%22%253Cscript%20src%3D%2527http%253A//widenoise.widetag.com/widget/wn-widget.js%2527%20type%3D%2527text/javascript%2527%253E%253C/script%253E%22%29%29%3B%0A%3C/script%3E%0A%3Cdiv%20id%3D%22wnwidget%22%20style%3D%22width%3AWIDGETWIDTHpx%3Bmargin%3A4px%200%200%200%3B%22%3E%3Cdiv%20id%3D%22wnwidget_top%22%3E%3Cscript%20type%3D%22text/javascript%22%3Etry%20%7BwnGenWidget%28WIDGETWIDTH%2C%20WIDGETHEIGHT%2C%20%22WIDGETSPIMEID%22%29%7D%20catch%28err%29%20%7B%7D%3C/script%3E%0A%3C/div%3E%3Cdiv%20id%3D%22wnwidget_bottom%22%20style%3D%22font-size%3A10px%3B%22%3E%3Cdiv%20id%3D%22wnwidget_bottom_left%22%20style%3D%22float%3Aleft%3Bmargin-top%3A1px%3B%22%3E%0A%3Ca%20href%3D%22http%3A//www.widetag.com/widenoise/widget%22%20target%3D%22_top%22%20title%3D%22Get%20your%20WideNoise%20widget%22%20alt%3D%22Get%20your%20WideNoise%20widget%22%20onmousedown%3D%22return%20false%3B%22%20style%3D%22color%3A%23797979%3Btext-decoration%3Anone%3Bfont-family%3Averdana%3B%22%3EGet%20your%20own%20WideNoise%20widget%21%3C/a%3E%3C/div%3E%0A%3Cdiv%20id%3D%22wnwidget_bottom_right%22%20style%3D%22float%3Aright%3Bmargin%3A0%3Bpadding%3A0%3Bcursor%3Apointer%3B%22%3E%3Ca%20href%3D%22http%3A//ax.itunes.apple.com/WebObjects/MZStore.woa/wa/browserRedirect%3Furl%3Ditms%25253A%25252F%25252Fitunes.apple.com%25252FWebObjects%25252FMZStore.woa%25252Fwa%25252FviewSoftware%25253Fid%25253D302052132%252526mt%25253D8%252526partnerId%25253D30%252526siteID%25253DDARO91t1GGA-VKyBm.WrcxDzeFzHYxBqtg%22%20target%3D%22_blank%22%20title%3D%22Get%20WideNoise%20on%20the%20iPhone%21%22%20alt%3D%22Get%20WideNoise%20on%20the%20iPhone%21%22%20onmousedown%3D%22return%20false%3B%22%20%3E%3Cimg%20src%3D%22http%3A//widenoise.widetag.com/widget/images/iphone.gif%22%20width%3D%2250%22%20height%3D%2215%22%20style%3D%22border%3A0%3B%22%20/%3E%3C/a%3E%3C/div%3E%0A%3C/div%3E%3Cdiv%20style%3D%22clear%3Aboth%3B%22%3E%3C/div%3E%3C/div%3E%0A%3C%21--%20/%5C%20WideNoise%20Widget%20Code%20--%3E";
var minimalWidet = "%3C%21--%20%5C/%20WideNoise%20Widget%20Code%20--%3E%0A%3Cscript%20type%3D%22text/javascript%22%3E%0Adocument.write%28unescape%28%22%253Cscript%20src%3D%2527http%253A//widenoise.widetag.com/widget/wn-widget.js%2527%20type%3D%2527text/javascript%2527%253E%253C/script%253E%22%29%29%3B%0A%3C/script%3E%0A%3Cdiv%20id%3D%22wnwidget%22%20style%3D%22width%3AWIDGETWIDTHpx%3Bmargin%3A4px%200%200%200%3B%22%3E%3Cdiv%20id%3D%22wnwidget_top%22%3E%3Cscript%20type%3D%22text/javascript%22%3Etry%20%7BwnGenWidget%28WIDGETWIDTH%2C%20WIDGETHEIGHT%2C%20%22WIDGETSPIMEID%22%2C%20%22minimal%22%29%7D%20catch%28err%29%20%7B%7D%3C/script%3E%0A%3C/div%3E%3Cdiv%20id%3D%22wnwidget_bottom%22%20style%3D%22font-size%3A10px%3B%22%3E%3Cdiv%20id%3D%22wnwidget_bottom_left%22%20style%3D%22float%3Aleft%3Bmargin-top%3A1px%3B%22%3E%0A%3Ca%20href%3D%22http%3A//www.widetag.com/widenoise/widget%22%20target%3D%22_top%22%20title%3D%22Get%20your%20WideNoise%20widget%22%20alt%3D%22Get%20your%20WideNoise%20widget%22%20onmousedown%3D%22return%20false%3B%22%20style%3D%22color%3A%23797979%3Btext-decoration%3Anone%3Bfont-family%3Averdana%3B%22%3EGet%20this%20widget%21%3C/a%3E%3C/div%3E%0A%3Cdiv%20id%3D%22wnwidget_bottom_right%22%20style%3D%22float%3Aright%3Bmargin%3A0%3Bpadding%3A0%3Bcursor%3Apointer%3B%22%3E%3Ca%20href%3D%22http%3A//ax.itunes.apple.com/WebObjects/MZStore.woa/wa/browserRedirect%3Furl%3Ditms%25253A%25252F%25252Fitunes.apple.com%25252FWebObjects%25252FMZStore.woa%25252Fwa%25252FviewSoftware%25253Fid%25253D302052132%252526mt%25253D8%252526partnerId%25253D30%252526siteID%25253DDARO91t1GGA-VKyBm.WrcxDzeFzHYxBqtg%22%20target%3D%22_blank%22%20title%3D%22Get%20WideNoise%20on%20the%20iPhone%21%22%20alt%3D%22Get%20WideNoise%20on%20the%20iPhone%21%22%20onmousedown%3D%22return%20false%3B%22%20%3E%3Cimg%20src%3D%22http%3A//widenoise.widetag.com/widget/images/iphone.gif%22%20width%3D%2250%22%20height%3D%2215%22%20style%3D%22border%3A0%3B%22%20/%3E%3C/a%3E%3C/div%3E%0A%3C/div%3E%3Cdiv%20style%3D%22clear%3Aboth%3B%22%3E%3C/div%3E%3C/div%3E%0A%3C%21--%20/%5C%20WideNoise%20Widget%20Code%20--%3E";
// max size for minimal
var minimalMaxWidth = 350;
var minimalMaxHeight = 200;

function doInit(entryHash) {
	if ($('#getcode')) {
		$("#getcode").click(genCode);
		$("#code").click(function() {$("#code").select();});
	}
	wnhome.init(entryHash);
}
function genCode() {
	// check wanted size
	if (isNaN($("#width").val())) {
		alert("The desired width should be an integer number of pixels.");
		$("#width").focus();
		return false;
	}
	if (isNaN($("#height").val())) {
		alert("The desired height should be an integer number of pixels.");
		$("#height").focus();
		return false;
	}
	var spimeId = $("#spimeid").val();
	if (spimeId.length > 2) {
		// format, uppercase
		spimeId = spimeId.toUpperCase();
		// check alphanumerical
		if (alphanumeric(spimeId) == false) {
			alert("The SpimeID can only contain alphanumerical and dash characters.");
			return false;
		}
		
		// spime id entered, ask for confirmation
		if (!confirm("Are you sure that your SpimeID is '" + spimeId + "'?\n\nIf this is incorrect, your personal detections will not be shown on the map.")) {
			return false;
		}
		// format, add -
		if (spimeId.substr(1,1) != '-') {
			spimeId = spimeId.substr(0,1) + '-' + spimeId.substr(1);
		}
	} else {
		spimeId = '';
	}
	// get widget type
	var mWidget = '';
	if ($("#width").val() < minimalMaxWidth || $("#height").val() < minimalMaxHeight) {
		// minimal
		mWidget = minimalWidet;
	} else {
		// normal
		mWidget = normalWidget;
	}	
	// generate code
	mWidget = mWidget.replace(/WIDGETWIDTH/g, $("#width").val().toString());
	mWidget = mWidget.replace(/WIDGETHEIGHT/g, $("#height").val().toString());
	mWidget = mWidget.replace(/WIDGETSPIMEID/g, spimeId);
	// paste code
	$("#code").text(unescape(mWidget));
}
function alphanumeric(alphane) {
	var numaric = alphane;
	for(var j=0; j<numaric.length; j++)
		{
		  var alphaa = numaric.charAt(j);
		  var hh = alphaa.charCodeAt(0);
		  if(hh == 45 || (hh > 47 && hh<58) || (hh > 64 && hh<91) || (hh > 96 && hh<123))
		  {
		  }
		else	{
			 return false;
		  }
		}
	return true;
}
function copyToClipboard()

{
	var codeDiv = document.getElementById('code');
   codeDiv.focus();

   codeDiv.select();

   CopiedTxt = document.selection.createRange();

   CopiedTxt.execCommand("Copy");

}
function setSize(item) {
	if (item && item.value) {
		// ****** Selection WxH
		if ($('#custom-size').css('display') != 'none') {$('#custom-size').slideUp();}
		size = item.value.split("x");
		$('#width').val(size[0]);
		$('#height').val(size[1]);
	} else {
		// ****** Custom
		if ($('#custom-size').css('display') == 'none') {$('#custom-size').slideDown();}
	}
}

var al = {
  screen: function(loc, num) {
    $('#screenshot img').attr("src", loc + "/widenoise/images/widenoise-screenshot-" + num + ".jpg");
    $('#screenshotselector li').removeClass("on").eq(num - 1).addClass("on");
  }
};
