// JavaScript Document

$(document).ready(function()
{
	InitFields();

	$("select").change( 
		function(){
			calculate()
	});	

	$("#btnCalculate").click(function(){calculate();});

	//Default Value
	$("#cboChannels").val("4");
	$("#cboMotion").val("0.5");
	$("#cboDVRVersion").val("1.52");
	
	//Calculate based on initial defaults
	calculate();
});

function calculate()
{				 	
	var storage = 0;
	var channels = Number($("#cboChannels").selectedValues()[0]);
	var fps = Number($("#cboFrameRates").selectedValues()[0]);
	var res = Number($("#cboResolution").selectedValues()[0]);
	var hours = Number($("#cboHours").selectedValues()[0]);
	var days = Number($("#cboDays").selectedValues()[0]);
	var motion = 1;//Number($("#cboMotion").selectedValues()[0]);
	
	if( $("#cboResolution").selectedTexts()[0].search("Annexxus") >=0)
	{
		if( $("#cboDVRVersion").selectedValues()[0] == "1.53")
		{
			res = $("#cboResolution").selectedTexts()[0];
			res = ResolutionFPSToBitrate(getRes(res), fps);
		}
		fps = 1;
	}

	var storage = ( (channels * fps) * res * ((60 * 60) * hours)) * days * motion;
	
	if(storage <= 1048576)
	{
		formattedstorage = storage / 1024;
		formattedstorage = Math.round(formattedstorage, 2) + " MB";
	}
	else
	{
		formattedstorage = storage / 1048576;
		formattedstorage = Math.round(formattedstorage, 2) + " GB";
	}
		

	var bandwith = (channels * fps * res) * 8;
	bandwith = Math.round(bandwith, 2) + " Kb/S <span style='color:black;font-size:x-small'>(Kilobits per second)</span>";
	
	$("#lblResult").html(formattedstorage);

	$("#lblResultBandwidth").html(bandwith);
}


function InitFields()
{
	//Channels
	for (var c = 1; c <= 32; c++)
	{		
		$("#cboChannels").addOption(c,c);
	}
	$("#cboChannels").selectOptions("16");
	
	
	//FrameRate
	for (var i = 1; i <= 30; i++)
	{
		$("#cboFrameRates").addOption(i,i);
	}
	$("#cboFrameRates").selectOptions("7");

	//Resolution
	//Note: value is framesize in KB
	var resolutions = new Array();
	//========New numbers given by Young-jo Song
	resolutions[0] = "Analog SRX-Pro (360x240);5";
	resolutions[1] = "Analog SRX-Pro (720x240);9";
	resolutions[2] = "Analog SRX-Pro (720x480);17";
	//================================================
	
	//============Original Numbers ===================
	/*
	resolutions[0] = "Analog SRX-Pro (360x240);2.0325";
	resolutions[1] = "Analog SRX-Pro (720x240);5.495";
	resolutions[2] = "Analog SRX-Pro (720x480);6.22";
	*/
	//================================================

	
	//========New numbers given by Young-jo Song
	resolutions[3] = "Annexxus 301 (352x240);62.1";
	resolutions[4] = "Annexxus 301 (704x240);94.2";
	resolutions[5] = "Annexxus 301 (704x480);126.6";
	
	resolutions[6] = "Annexxus 304 (352x240);43.5";
	resolutions[7] = "Annexxus 304 (704x240);63.0";
	resolutions[8] = "Annexxus 304 (704x480);87.1";

	resolutions[9] = "Annexxus 301C2M (1280x720);192.6";
	resolutions[10] = "Annexxus 301C2M (1600x1200);254.9";	
	//===============================================

	//========Original Numbers	
	/*resolutions[3] = "Annexxus 301 (352x240);8.62";
	resolutions[4] = "Annexxus 301 (704x240);8.63";
	resolutions[5] = "Annexxus 301 (704x480);8.635";
	resolutions[6] = "Annexxus 304 (352x240);4.705";
	resolutions[7] = "Annexxus 304 (704x480);4.82";
	resolutions[8] = "Annexxus 301C2M (1600x1200);25.515";
	resolutions[9] = "IQ 2M (1600x1200);139.67";
	resolutions[10] = "Arecont (2560x1600);350.0475";
	*/
	//===============================================
	
	for(var i=0; i<resolutions.length; i++)
	{	
		var vars = resolutions[i].split(";");
		$("#cboResolution").addOption(vars[1],vars[0],i);
	}
	$("#cboResolution").selectOptions("2.0325");

	for (var h = 1; h <= 24; h++)
	{
		$("#cboHours").addOption(h,h);
	}
	$("#cboHours").selectOptions("24");

	var days = new Array(1, 2 , 3, 4, 5, 6, 7, 14, 21, 30, 60, 90, 120, 180, 365);
	for (d in days)
	{
		$("#cboDays").addOption(days[d],days[d]);
	}
	$("#cboDays").selectOptions("1");
	
	for (var p = 5; p <= 100; p += 5)
	{
		$("#cboMotion").addOption((p/100), p + "%");
	}
	$("#cboMotion").selectOptions("35");
}

function getRes(res)
{
	var reg = new RegExp(/[0-9]+[\s]*[x|X][\s]*[0-9]+/);
	var m = reg.exec(res);
	if (m == null) {
	   res = "";
	} else {
		res = (m[0]);
	}

	//console.log(res);
	return res;
	//res = ResolutionFPSToBitrate(res,"15");
	//var res = Number(res);
	//res = Math.round(Number(res)*100)/100;
	//console.log("Res = " + res + " KB");
}

function ResolutionFPSToBitrate(resolution, fps)
{
	 //if(Annexxus304)fps = fps/3;

	 var bNTSC = true;
	 var nLowestBitRate = 0; //bit
	 var nIncrease_PerFrame = 0; //bit
	 var nFrameSize_PerSecond = 0;
	
	
	 var CIF = "1";
	 var CIF2 = "1";
	 var D1 = "1";

	 switch(resolution)
	 {
	  // 352 x 240 - CIF
	  case "352x240":
	   nLowestBitRate = 131072; // 128 * 1024 = 131072
	   nIncrease_PerFrame = 13559; // (512-128) * 1024 /(30-1) = 13559
	   break;
	 
	  // 704 x 240 - 2CIF
	  case "704x240":
	   nLowestBitRate = 196608; // 192 * 1024 = 196608
	   nIncrease_PerFrame = 20339; // (768-192) * 1024 /(30-1) = 20339
	   break;
	 
	  // 704 x 480 - D1  
	  case "704x480":
	   nLowestBitRate = 262144; // 256 * 1024 = 262144
	   nIncrease_PerFrame = 27118; // (1024-256) * 1024 /(30-1) = 27118
	   break;
	 
	  //1280x720
	  case "1280x720": 
	   
		//Check if it is using 60Hz(NTSC) or 50Hz(PAL)
		//with resolution 1280x720, maximum frame rate is 15 for 60Hz(NTSC), 25 for 50Hz(PAL)

		nLowestBitRate = 393216; // 384 * 1024 = 393216
		if(bNTSC)
		{
		 //maximum frame rate is 15

		 if(fps > 15)
			fps = 15;
	 
		 nIncrease_PerFrame = 84261; // (1536-384) * 1024 /(15-1) = 84261
		}
		else
		{
		 //maximum frame rate is 25
	 
		 if(fps > 25)
			fps = 25;

		 nIncrease_PerFrame = 49152; // (1536-384) * 1024 /(25-1) = 49152
		}
	   
	   break;
	 
	  //1600x1200
	  case "1600x1200": 
	   nLowestBitRate = 524288; // 512 * 1024 = 524288
	   nIncrease_PerFrame = 174763; // (2048-512) * 1024 /(10-1) = 174763
	 
	   //for 1600x1200, if frame rate is 12, still use 10 for calcuation
	   if(fps > 10)
		fps = 10;
	 
	   break;
	 
	  default:
	   //assume it is QCIF
	   nLowestBitRate = 32768; // 32 * 1024 = 32768
	   nIncrease_PerFrame = 3389; // (128-32) * 1024 /(30-1) = 3389
	 }
	
	//bit
	 nFrameSize_PerSecond = (nLowestBitRate + (fps - 1) * nIncrease_PerFrame);
	 
	//B
	//nFrameSize_PerSecond = nFrameSize_PerSecond / 8;	
	
	//KB
	nFrameSize_PerSecond = nFrameSize_PerSecond / 8 / 1024;

	return nFrameSize_PerSecond;
}
