Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/js/karma.js
blob: 3d29073b9a933b18b709542145f57a60b0152281 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
/* Documentation Note:
 *   Public methods and properties are commented with /** some text *\/
 *   and private methods and properties are commented with //
 *   
 *   Please leave it that way to keep this documentation sane
 */


/*
*	Karma Framework
*	http://karmaeducation.org
*	
*	Copyright (c)  2009
*	Bryan W Berry		bryan@olenepal.org
* 	Felipe López Toledo	zer.subzero@gmail.com
*      
*	Under MIT License:
*	Permission is hereby granted, free of charge, to any person
*	obtaining a copy of this software and associated documentation
*	files (the "Software"), to deal in the Software without
*	restriction, including without limitation the rights to use,
*	copy, modify, merge, publish, distribute, sublicense, and/or sell
*	copies of the Software, and to permit persons to whom the
*	Software is furnished to do so, subject to the following
*	conditions:
*	
*	The above copyright notice and this permission notice shall be
*	included in all copies or substantial portions of the Software.	
* 
*	THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
*	EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
*	OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
*	NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
*	HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
*	WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
*	FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
*	OTHER DEALINGS IN THE SOFTWARE.
*/

/**
* @fileOverview Contains karma library
* @author Bryan Berry <bryan@olenepal.org> 
* @author Felipe Lopez Toledo <zer.subzero@gmail.com>
*/


//common.js modules use exports object
if(!this.exports) {
    exports = {};
}



/** Karma is the namespace for the Karma library and Karma() is the constructor 
 * function for the Karma library object Karma. 
 * Karma() checks if the current document type is set to HTML 5, throws
 * an error if not. Otherwise, initializes the karma object and returns
 * a reference to that object.
 * @namespace Global namespace for Karma library
 * @constructor
 * @param {Object} [options={}] options for intializing Karma library
 * @param {String} [options.locale=''] sets current locale Not Yet Implemented
 * @param {Array} [options.image=[]] array of images to be converted into a collection
 * @param {Array} [options.audio=[]] array of audio to be converted into a collection
 * @param {Array} [options.video=[]] NYI array of videos to be converted into a collection
 * @param {Array} [options.svg=[]] array of SVG elements to be 
 * converted into a collection. Each SVG element must already exist in the html document
 * @param {Array} [options.canvas=[]] array of canvas elements 
 * to be converted into a collection. Each canvas element must already exist in the 
 * html document and width and height of each element must be set as attributes
 * @throws {Error} if the document type declaration is not set to HTML 5, e.g. 
 * <!DOCTYPE html>
 * @throws {Error} If any of the initialization parameters are invalid values
 * @returns {Object} Karma -- reference to the initialized Karma library
 * @example
 * 
 * var k = Karma({ 
 *                 image: [ 
 *                    {name: "ninja", file: "ninja.png"}, 
 *                    {name: "cowboy", file: "cowboy.png"}
 *                         ],
 *                 audio: [
 *                    {name: "woosh", file: "woosh.ogg"},
 *                    {name: "yeehaw", file: "yeehaw.ogg"}
 *                         ],
 *                 video: [  //Not Yet Implemented
 *                    {name: "attack", file: "attack.ogv"},
 *                    {name: "ride", file: "ride.ogv"}
 *                         ]
 *                 canvas: [
 *                    {name: "ninja", domId: "ninjaCanvas"},
 *                    {name: "cowboy", domId: "cowboyCanvas"}
 *                         ],
 *                 svg: [ 
 *                    {name: "ninja", domId: "ninjaSvg"},
 *                    {name: "cowboy", domId: "cowboySvg"}
 *                         ],
 *                 });
 * Next, call the ready function with a callback to your program code
 * 
 * k.ready(function () { ... your application code . . . }                       
 * 
 * after that you can access each asset like so
 * k.image.ninja;
 * k.svg.cowboy;
 * k.audio.yeehaw.play();
 * k.canvas.ninja.drawImage(k.image.ninja, 0, 0);
 * 
 */	
var Karma = exports.Karma  = function (options) {
    Karma._isHtml5(document.doctype.nodeName);

    if ( Karma._initialized === true ) {
	return Karma;
    } else {
	return Karma._init(options);
    }
};


//helper functions

/**This emulates the Object.create method in ecmascript 5 spec
 * This isn't a full implementation as it doesn't support an all of Object.create's features
 * This has the same functionality as Crockford's beget method
 * and this primary building block for prototypal inheritance in
 * this library
 * @param {Object} parent that the new object's prototype should point to
 * @returns {Object} a new object whose prototype is parent
 * @example
 * 
 * var ninja = { weapon : "sword" };
 * var ninja1 = Karma.create(ninja);
 * ninja1.weapon === "sword"
 */
Karma.create = function (parent){
    function F () {};
    F.prototype = parent;
    return new F();
};

/** Returns a shallow copy of the passed in object
 * @param {Object} target to be copied
 * @returns {Object} a shallow copy of target
 */
Karma.clone = function (target){
    var copy = {};
    for ( var i in target ) {
	if(target.hasOwnProperty(i)){
	    copy[i] = target[i];
	}
    }
    return copy;
};

/** Extends properties of the target object with those of 
 * the source object
 * @param {Object} target object to be extended 
 * @param {Object} source whose properties will extend target
 * @returns {Object} target extended by source
 */
Karma.objectPlus = function (target, source){
    for ( var i in source){
	if (source.hasOwnProperty(i)){
	    target[i] = source[i];
	}
    }
    return target;
};

Karma.extend = Karma.objectPlus;

/** Creates a new object that is a prototype of the first argument
 * then extends it with the properties of the second argument
 * @param {Object} parent1 will be prototype of returned object
 * @param {Object} parent2 will extend properties of returned object
 * @returns {Object} object that whose prototype is parent1 and has 
 * been extended with properties of parent2
 */ 
Karma.copyObjectPlus = function (parent1, parent2){
    function F () {};
    F.prototype = parent1;
    var G = new F();
    return Karma.objectPlus(G, parent2);
};


//Throws big ugly error if doctype isn't html5
Karma._isHtml5 = function (doctype){
    var regex = new RegExp('^html$', 'i');
    if(!regex.test(doctype)){
	var errorMsg =  "ERROR: The doctype must be set to <!DOCTYPE html> " +
	    "in order to use Karma. Karma require you use html5";
	var errorElem = document.createElement('div');
	errorElem.setAttribute('id', 'errorDoctype');
	errorElem.innerText = errorMsg;
	document.body.appendChild(errorElem);
	   throw new Error(errorMsg);
	}
};

/**
 * Shuffles an array of items randomly
 * @param {Array} oldList of choices to be shuffled
 * @returns {Array} newlist of choices randomly reordered 
 */
Karma.shuffle = function (oldList) {
    var newList = oldList.slice(0);
    for (var i = newList.length - 1; i > 0; i -= 1) {
        var j = Karma.rand(0, i);
        var t = newList[i];
        newList[i] = newList[j];
        newList[j] = t;
    }
    return newList;
};


/**
 * Converts a number to numerals in the specified locale. Currently only
 * supports Nepali
 * @param {Number} Number to be converted
 * @param {locale} locale that number should be converted to
 * @returns {String} Unicode string for localized numeral 
 */
Karma.convertNumToLocale = function(num, locale){
    locale = locale || Karma.locale;
    //48 is the base for western numerals
    var convertDigit = function(digit){
	
	var numBase = 48;
	var prefix = "u00";
	
	if (locale === "ne"){
	    prefix = "u0";
	    numBase = 2406;
	}
	
	return '\\' + prefix + 
	    (numBase + parseInt(digit)).toString(16);
    };
    
    var charArray = num.toString().split("").map(convertDigit);
    return eval('"' + charArray.join('') + '"');
};

/**
 * @name Karma._n
 * @function
 * @public
 * Alias for Karma.convertNumToLocale. Converts a number to numerals to 
 * Karma.locale or to specified locale. Currently only supports Nepali
 * @param {Number} Number to be converted
 * @param {locale} locale that number should be converted to
 * @returns {String} Unicode string for localized numeral 
 */
Karma._n = Karma.convertNumToLocale;

/* Scales the dimensions of document.body to the innerHeight and innerWidth
 * of the viewport, i.e. browser window, with a minor offset to the height to 
 * make sure the scrollbars do not appear
 */
Karma.scaleToViewport = function(){
    var width = window.innerWidth;
    var height = window.innerHeight;
    
    //hack to ensure scrollbars don't appear
    if (height === 900){
	height = "" + 900 + "px";
    } else {
	height = "" + (height - 13) + "px";
    }
    
    document.body.style.width = "" + width + "px";
    document.body.style.height = height;
};

Karma.scaleWindow = function(){
    var width = "1200px";
    var height = "900px";
    var viewportHeight = "760px";
    var $body = $('body');
    var $kMain = $('#kMain');

    if (window.innerWidth < 1150){
	width = "950px";
	height = "600px";
	viewportHeight = "460px";
	$body.css('border', '2px solid black');

	//  460/760 * 16 = 9.6
	$kMain.css('font-size', '9.6px');
    } 

    $body.css({border: '2px solid black', width: width, height: height});
    $kMain.css({width: width, height: viewportHeight});


};

    // Below are geometry and math helper methods
    
/**
 * Converts a value from degrees to radians.
 * @param {Number} angle The angle in degrees
 * @returns {Number} The angle in radians 
 */
Karma.radians = function( angle ){
	return ( angle / 180 ) * Math.PI;
};

/**
 *  Gets the square of the Euclidian (ordinary) distance between 2 points.
 * @param {Object} Point No. 0
 * @param {Number} Point0.x
 * @param {Number} Point0.y
 * @param {Object} Point No. 1
 * @param {Number} Point1.x
 * @param {Number} Point1.y
 * @returns {Number} The square of the Euclidian distance 
 * @example
 * 
 * p0 = {x:0, y:1};
 * p1 = {x:50, y:70};
 * var d = distance2(p0, p1);
 * 
 */
Karma.distance2 = function ( p0, p1 ) {
    return   (p1.x - p0.x) * (p1.x - p0.x) + (p1.y - p1.y) * (p1.y - p1.y); 
};

/**
 * Gets the Euclidian (ordinary) distance between 2 points.<br>
 * <b>Warning:</b> It's slower than distance2 function
 * @param {Object} Point No. 0
 * @param {Number} Point0.x
 * @param {Number} Point0.y
 * @param {Object} Point No. 1
 * @param {Number} Point1.x
 * @param {Number} Point1.y
 * @returns {Number} The Euclidian distance 
 * @example
 * 
 * p0 = {x:0, y:1};
 * p1 = {x:50, y:70};
 * var d = distance2(p0, p1);
 * 
 */
Karma.distance = function ( p0, p1 ) {
	return   Math.sqrt( this.distance2( p0, p1 ) ); 
};

/** Returns a random number within the range provided
 * @param {Number} lower limit of the range, lowest number that can be returned
 * @param {Number} upper limit of the range, highest number that can be returned
 * @returns {Number} number that is >= lower and <= upper
 * @example
 * 
 * var num = rand(0, 10);
 * 
 * //num could be 0, 1, 2, 3 ... or 10
 * 
 */
Karma.rand = function ( lower, upper ){
  return Math.floor(Math.random() * (upper - lower + 1) + lower);  
};


Karma.extend(Karma, {      
    /** This is the global locale as passed to Karma(),
     * such as "en", "es_SP"
     * @fieldOf Karma
     * @property {string} locale This is the global locale as passed to Karma()
     * @default 'en'
     */
    locale : 'en',
    /** Collection of images with special helper
     * methods added to each reference
     * @fieldOf Karma
     * @type object
     * @default empty object
     */
    image : {},
    /** Collection of audio files with special helper
     * methods added to each reference
     * @fieldOf Karma
     * @type object
     * @default empty object
     */
    audio : {},
    /** Collection of html 5 canvases with special helper
     * methods added to each reference
     * @fieldOf Karma
     * @type object
     * @default empty object
     */
    canvas : {},
    /** Collection of svgs with special helper
     * methods added to each reference
     * @fieldOf Karma
     * @type object
     * @default empty object
     */
    svg : {},
    /** Collection of videos with special helper
     * methods added to each reference
     * @fieldOf Karma
     * @type object
     * @default empty object
     */
    video : {},
    _localized : false,
    _assetPath : "assets/",
    _localePath : "",
    _initialized : false,
    _statusDiv: undefined,
    _loaderDiv : undefined,
    _counters : { total : 0, errors : 0, loaded : 0},

    //This constructs the Karma object per values provided by the user
    _init: function(options) {
	this._initialized = true;
	
	//set up message that show count of assets loaded
	//and has an ordered list to append error messages to
	var _statusDiv = this._statusDiv = document.createElement('div');
	this._loaderDiv = this._loaderDiv = document.createElement('div');	
	var errorList = document.createElement('ol');

	_statusDiv.setAttribute('id', 'karma-status');
	_statusDiv.setAttribute('style', 'position:absolute;');
	_statusDiv.innerHTML = 'Karma is loading ...';
	this._loaderDiv.setAttribute('id', 'karma-loader');
	this._loaderDiv.setAttribute('class', 'status');
	errorList.setAttribute('id', 'errorList');

	_statusDiv.appendChild(this._loaderDiv);
	this._statusDiv.appendChild(errorList);
	document.body.appendChild(_statusDiv);

	//regular expression that matches the name of aprivate property
	// the karma object
	var regexPrivate = new RegExp('^_.*');
	
	for ( var option in options ) {
	    if (options.hasOwnProperty(option)){
		if (option === "image" || option === "audio" || option === 
		    "svg" || option === "video" || option === "canvas"){ 
		    
		    if(!(options[option] instanceof Array)){
			throw new Error("" + option + " must be an array");
		    } else if (options[option].length === 0){
			continue;
		    }
		} else if (regexPrivate.test(option)){
		    //don't overwrite a private property of karma object
		    continue;
		}
		
		switch (option){
		case "locale":

		    if (this._isValidLocale(options[option])){
			this.locale = this._normalizeLocale(options[option]);
			this._localized = true;
			this._localePath = Karma._computeLocalePath(this.locale);
		    } else {
			throw new Error("locale provided to karma._init() is invalid");
		    }
		    
		    break;
		case "image":
		    options[option]._type = 'image';
		    Karma._makeCollection(options[option], 'image');
		    break;
		case "audio":
		    options[option]._type = 'audio';
		    Karma._makeCollection(options[option], 'audio');
		    break;
		case "video":
		    options[option]._type = 'video';
		    Karma._makeCollection(options[option], 'video');
		    break;
		case "svg":
		    options[option]._type = 'svg';
		    Karma._makeCollection(options[option], 'svg');
		    break;
		case "canvas":
		    options[option]._type = 'canvas';
		    Karma._makeCollection(options[option], 'canvas');
		    break;
		}
	    }
	}



	return this;
    },
    
    /** Waits until all assets loaded(ready), then calls callback cb
     * @memberOf Karma
     * @param {Function} [cb] callback function
     * @returns this
     * @throws {Error} if Karma is not initialized with the 
     * Karma({ options }) function
     * @example
     * 
     * var k = Karma({ . . . your assets here . . . });
     * k.ready(function(){ .. your code here . . .});
     * 
     * your code will not be called until all assets have been loaded
     * into collections
     * 
     */
    ready : function( cb ) {
	var that = this;
	if (Karma._initialized !== true){
	    throw new Error("Karma not initialized");
	}

	if (this._counters.loaded !== this._counters.total){
	    setTimeout(function(){ that.ready(cb);}, 5);
	} else if (cb) {
	    //hide the "Karma is loading..." message
	    this._statusDiv.setAttribute('style', 'display:none;');

	     cb();
	} else if (!cb) {
	    //hide the "Karma is loading..." message
	    this._statusDiv.setAttribute('style', 'display:none;');
	    
	    //if no options passed, show it works message
	    this._showStarterMessage();
	}
	
	
	   

	return this;
    },

    //Display Apache-like "It works" message if no options
    _showStarterMessage : function (){
	var starterMsg = document.createElement('div');
	starterMsg.setAttribute('id', 'starterMsg');
	starterMsg.innerHTML = "<h1>It Works</h1>";
	document.body.appendChild(starterMsg);
    },

    //Updates visible counter of how many assets are loaded
    _updateStatus : function (errorMsg) {
	var loaded = this._counters.loaded;
	var total = this._counters.total;
	var errors = this._counters.errors;
	this._loaderDiv.innerHTML = "Loaded " + loaded + " / " + total + 
	    "" + (errors > 0 ? " Errors [ " + errors +" ]" : '');
	if (errorMsg) {
	    var liError = document.createElement('li');
	    liError.innerHTML = errorMsg;
	    var errorList = document.getElementById('errorList');
	    errorList.appendChild(liError);  
	}
    },	    

    //matches 2 letter country code then optionally
    //a dash or underscore followed by a country or language identifier
    //i currently only allow a language identifier 2-3 chars long
    _isValidLocale : function (locale) {
	var localeRegex = new RegExp('^[a-zA-Z][a-zA-Z]([-_][a-zA-z]{2,3})?$');
	return localeRegex.test(locale);
    },

    _normalizeLocale : function(locale) {
	var lang = "";
	var country = "";
	var divider = "";

	lang = locale.slice(0, 2).toLowerCase();
	divider = "_";
	country = locale.slice(3, 6).toUpperCase();
	
	return locale.length > 2 ? "" + lang + divider + country : lang;
    },
    

    
});

//Helper functions for creating assets
Karma._isLocalized = function (boolLocalized) {
    if (typeof boolLocalized === "boolean" ) {
	if(boolLocalized === true && 
	   Karma.locale === undefined){
	    throw new Error("You cannot localize a media asset" +
			    " if the global locale for Karma isn't set");
	} else {
	    return boolLocalized;
	}
    } else if (typeof boolLocalized === undefined){
	return false;
    } else{ 
	throw new Error("This is not a valid value for the localized option");
    }
};

Karma._computeLocalePath = function(locale) {
    return Karma._assetPath + locale + "/";
};




Karma._makeCollection = function (configs, type){
    var makeAsset = function (config){
	var asset = undefined;
	var target = undefined;
	switch(type){
	    case "image":
		target = Karma.kImage;
		break;
	    case "audio":
		target = Karma.kAudio;
		break;
	    case "video":
		target = Karma.kVideo;
		break;
	    case "svg":
		target = Karma.kSvg;
		break;
	    case "canvas":
		target = Karma.kCanvas;
		break;
	}

	asset = Karma.create(target)._init(config);
	Karma[type][config.name] = asset;
    };
		       
    configs.forEach(function(config){ makeAsset(config);});
};





//Prototype objects for assets


/** Prototype object for images
 *  @class This object is the prototype for images submitted to Karma in the
 *  Karma() method
 *  @ throws {Error} if the image asset is set to be localized but 
 *  the global locale is not set on the Karma object
 *  @ throws {Error} if the name and file properties are not supplied
 *  @example
 *  kImage is the prototype object for images. This 'media' asset is loaded 
 *  in a distinctly different way from the canvas or svg assets.    
 *
 */
Karma.kImage = 
    {
    /** file location of image
     * @type String
     * @default ""
     */
    file : "",
    /** media object
     * @type Image
     * @default undefined 
     */	
    media : undefined,
    //actual path to the file
    _path : "",
    //if using localized version of this image
    _localized : false,
    _type : "image", 
    //initializes kImage instance with values provided by user
    _init : function (image) {
	image._localized = image._localized || false;
	Karma._counters.total++;

	if (image.name === undefined || image.file === undefined){
	    throw new Error("properties name and file have to be defined");
	} else {
	    this.name = image.name;
	    this.file = image.file;
	}

	this.media = new Image(); 
	
	if(Karma._isLocalized(image._localized)){
	    this._localized = image._localized;
	    this._path = Karma._localePath + "image/";
	} else {
	    this._path = Karma._assetPath + "image/";
	}

	//IMPORTANT: This one magic line loads the file
	this.media.src = this.src = this._path + this.file;
	
	//add event handlers
	this._addEventHandlers();

	
	return this;
    },
    //Adds event handlers to update the counters when 
    //the image is successfully or unsuccessfully loaded
    _addEventHandlers : function () {
	var that = this;

	that.media.addEventListener(
	    "load", 
	    function (e) { 
		Karma._counters.loaded++;
		Karma._updateStatus();
		that.status = "loaded";}, false);
	
	that.media.addEventListener(
	    "error", 
	    function (e) { 
		Karma._counters.errors++;
		that.status = "error";
		var errorMsg = "Error: " + that._type.toUpperCase() +
		    " " + that.name + " cannot be loaded."; 
		Karma._updateStatus(errorMsg);
	    }, 
	    false);
	that.media.addEventListener(
	    "abort", 
	    function (e) { 
		Karma._counters.total++;
		that.status = "aborted";
		var errorMsg = "ABORT: " + that._type.toUpperCase() +
		    " " + that.name + " loading was aborted."; 
		Karma._updateStatus(errorMsg);

	    }, false);
    }
    
};

/** Prototype object for audio files 
 *  @class This object is the prototype for audio files submitted to Karma in the
 * Karma() method
 *  @ throws {Error} if the individual audio asset is set to be localized but 
 *  the globale locale is not set on the Karma object
 *  @ throws {Error} if the name and file properties are not supplied
 *  @example
 *  kAudio is the prototype object for audio
 *  The audio assets are loaded in a distinctly different way
 *  from the canvas or svg assets. They also have distinctly different
 *  helper methods 
 *  
 *  You initialize the kAudio assets by passing an array of objects
 */
Karma.kAudio = {
    /** file location of asset
     * @type String
     * @default ""
     */
    file : "",
    /**  Media object. You can access the src, autobuffer, autoplay, loop, and 
     * controls attributes 
     * via the media property of kAudio. Read more about the properties of the <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/video.html#media-element-attributes">HTML 5 media element</a>
     * @type Audio
     * @default undefined
     */	
    media : undefined,
    //actual path to the file
    _path : "",
    //if using localized version of this asset
    _localized : false,
    _type : "audio", 
    //initializes kAudio instance with values provided by user
    _init : function (audio) {
	audio._localized = audio._localized || false;
	Karma._counters.total++;

	if (audio.name === undefined || audio.file === undefined){
	    throw new Error("properties name and file have to be defined");
	} else {
	    this.name = audio.name;
	    this.file = audio.file;
	}

	this.media = new Audio(); 
	
	if(Karma._isLocalized(audio._localized)){
	    this._localized = audio._localized;
	    this._path = Karma._localePath  + "audio/";
	} else {
	    this._path = Karma._assetPath + "audio/";
	}


	//IMPORTANT: This one magic line loads the file
	this.media.src = this.src = this._path + this.file;
	
	//add event handlers
	this._addEventHandlers();

	this.media.autobuffer = true;
	this.media.load();

	
	return this;
    },
    //Adds event handlers to update the counters when 
    //the asset is successfully or unsuccessfully loaded
    _addEventHandlers : function () {
	var that = this;
	//'canplaythrough' event is a Browser Hack recommended by chromium devs
	//http://code.google.com/p/chromium/issues/detail?id=20251&q=loading%20audio&colspec=ID%20Stars%20Pri%20Area%20Type%20Status%20Summary%20Modified%20Owner%20Mstone%20OS#c4

	that.media.addEventListener(
	    "canplaythrough", 
	    function (e) { 
		Karma._counters.loaded++;
		Karma._updateStatus();
		that.status = "loaded";}, false);
	
	that.media.addEventListener(
	    "error", 
	    function (e) { 
		Karma._counters.errors++;
		that.status = "error";
		var errorMsg = "Error: " + that._type.toUpperCase() +
		    " " + that.name + " cannot be loaded."; 
		Karma._updateStatus(errorMsg);
	    }, 
	    false);
	that.media.addEventListener(
	    "abort", 
	    function (e) { 
		Karma._counters.total++;
		that.status = "aborted";
		var errorMsg = "ABORT: " + that._type.toUpperCase() +
		    " " + that.name + " loading was aborted."; 
		Karma._updateStatus(errorMsg);

	    }, false);

    },
    /** Plays the audio file  */
    play : function () {
	    this.media.play();  
    }
    
};

/** NYI:Prototype object for Video files 
 *  @class Not Yet Implemented:This object is the prototype for video files submitted 
 * to Karma in the Karma() method
 *  @ throws {Error} if the individual video asset is set to be localized but 
 *  the globale locale is not set on the Karma object
 *  @ throws {Error} if the name and file properties are not supplied
 */
Karma.kVideo = {
    /** file location of asset
     * @type String
     * @default ""
     */
    file : "",
    /** media object
     * @type Video
     * @default undefined 
     */	
    media : undefined,
    //actual path to the file
    _path : "",
    //if using localized version of this asset
    _localized : false,
    _type : "video", 
    //initializes kVideo instance with values provided by user
    _init : function (video) {
	//Not Yet Implemented
	Karma._counters.errors++;
	throw new Error("Video is not Yet Implemented");

	video._localized = video._localized || false;
	Karma._counters.total++;

	if (video.name === undefined || video.file === undefined){
	    throw new Error("properties name and file have to be defined");
	} else {
	    this.name = video.name;
	    this.file = video.file;
	}

	this.media = new Video(); 
	
	if(Karma._isLocalized(video._localized)){
	    this._localized = video._localized;
	    this._path = Karma._localePath  + "video/";
	} else {
	    this._path = Karma._assetPath + "video/";
	}


	//IMPORTANT: This one magic line loads the file
	this.media.src = this.src = this._path + this.file;
	
	//add event handlers
	this._addEventHandlers();

	return this;
    },
    //Adds event handlers to update the counters when 
    //the asset is successfully or unsuccessfully loaded
    _addEventHandlers : function () {
	var that = this;
	//'canplaythrough' event is a Browser Hack recommended by chromium devs
	//http://code.google.com/p/chromium/issues/detail?id=20251&q=loading%20audio&colspec=ID%20Stars%20Pri%20Area%20Type%20Status%20Summary%20Modified%20Owner%20Mstone%20OS#c4

	that.media.addEventListener(
	    "canplaythrough", 
	    function (e) { 
		Karma._counters.loaded++;
		Karma._updateStatus();
		that.status = "loaded";}, false);
	
	that.media.addEventListener(
	    "error", 
	    function (e) { 
		Karma._counters.errors++;
		that.status = "error";
		var errorMsg = "Error: " + that._type.toUpperCase() +
		    " " + that.name + " cannot be loaded."; 
		Karma._updateStatus(errorMsg);
	    }, 
	    false);
	that.media.addEventListener(
	    "abort", 
	    function (e) { 
		Karma._counters.total++;
		that.status = "aborted";
		var errorMsg = "ABORT: " + that._type.toUpperCase() +
		    " " + that.name + " loading was aborted."; 
		Karma._updateStatus(errorMsg);

	    }, false);

    }
    
};



/** Prototype object for each canvas element submitted to Karma in the
 * Karma() method
 * @throws {Error} if the name and domId for the canvas element are not specified
 * @thows {Error} if the supplied domId does not match an element in the DOM
 * @class This object is the prototype for each canvas element submitted to Karma in the
 * Karma() method
 */
Karma.kCanvas = {
    /** Name of the canvas, used internally by karma.js
     * @type String
     * @default ''
     */
    name : '',
    /** Width of canvas element
     * @type Number
     * @default 0
     */
    width: 0,
    /** Height of canvas element
     * @type Number
     * @default 0
     */
    height: 0,
    /**  Whether canvas is visible
     * @type boolean
     * @default true
     */
    visible: true,
    /** Element ID for canvas element in html document. This value is read-only
     * @type String
     * @default undefined
     */
    domId: undefined,
    /** Reference to the DOM element
     * @type DOMElement
     * @default undefined
     * @example
     * //You can access all properties and methods of the underlying DOM element
     * //using the 'node' property
     * Karma.canvas.someCanvas.node.dispatchEvent( ... some event ...);
     * var stuff = Karma.canvas.someCanvas.node.innerHTML;
     * 
     */
    node: undefined,
    /** The 2 Dimensional Rendering context property for this canvas
     * @type 2DRenderingContext
     * @default undefined
     * @example
     * //Almost all of the context attributes and methods are wrapped in helper functions
     * //but you can also access them directly using the ctx property
     * Karma.canvas.someCanvas.ctx.drawImage(someImage, x, y);
     * Karma.canvas.someCanvas.ctx.fillStyle = "#ffffff";
     */
    ctx: undefined,

    //initializes object with values provides by user
    _init: function (config) {
	for (var option in config){
	    if (config.hasOwnProperty(option)){
		switch (option){
		case "name":
		    this.name = config[option];
		    break;
		case "domId":
		    this.domId = config[option];
		    break;
		case "width":
		    if(!this.height){
			throw new Error("If you specify a width you must also" +
					"specify a height");
		    }
		    this.width = config[option];
		    break;
		case "height":
		    if(!this.width){
			throw new Error("If you specify a height you must also" +
					"specify a width");
		    }
		    this.height = parseInt(config.option, 10);
		    break;
		case "fps":
		    this.fps = parseInt(config.option, 10);
		    break;
		}
	    }
	}
	
	if(this.domId && document.getElementById(this.domId)){
	       	this.node = document.getElementById(this.domId);
		this.ctx = this.node.getContext('2d');
	} else {
	    throw new Error('you must specify a valid domId that' +
			    'is in your html page');
	}

	if(!config.height && !config.width){
	    this.width = parseInt(this.node.getAttribute('width'), 10);
	    this.height = parseInt(this.node.getAttribute('height'), 10);
	}

	return this;
    },
    /** Clear area of canvas element specified by parameters, if no
     * parameters supplied, clears entire canvas
     * @param {Number} [x=0] x coordinate, defaults to zero if left blank
     * @param {Number} [y=0] y coordinate, defaults to zero if left blank  
     * @param {Number} [width=0] width of area to be cleared, defaults 
     * entire width of canvas
     * @param {Number} [height=0] height of area to be cleared, defaults 
     * entire height of canvas
     * @returns this
     * @example
     * 
     * k.canvas.ninja.clear();
     * // clears the entire ninja canvas
     * 
     * k.canvas.ninja.clear(0, 10, 20, 30);
     * //clears a specific portion of the ninja canvas
     * 
     */
    clear : function ( x, y, width, height ) {
	var that = this;
	that.ctx.clearRect(
	    x || 0,
	    y || 0, 
	    width  || that.width, 
	    height || that.height
	);
	return that;
    },
  
     /** The globalAlpha attribute gives an alpha value that is applied to shapes 
     * and images before they are composited onto the canvas
     * @param {Number} number in the range from 0.0 to 1.0
     * @returns this
     */
    globalAlpha : function (attribute){
	var name = 'globalAlpha';
	this.ctx[name] = attribute;
	return this;
    },
 
   /** Sets the globalCompositeOperation attribute, which sets how shapes and images 
     * are drawn onto the existing bitmap, once they have had globalAlpha and the 
     * current transformation matrix applied.
     * For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param {String} globalCompositeOperation source-atop, 
     * source-in, source-out, 
     * source-over, destination-atop, destination-in, destination-out, destination-over,
     * lighter
     * @returns this
     */
    globalCompositeOperation: function (attribute){
	var name = ' globalCompositeOperation';
	this.ctx[name] = attribute;
	return this;
    },

    /** Sets the lineWidth attribute which gives the width of lines, in coordinate space 
     * units.
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param {Number} lineWidth
     * @returns this
     */
    lineWidth: function (attribute){
	var name = 'lineWidth';
	this.ctx[name] = attribute;
	return this;
    },
    /** The lineCap attribute defines the type of endings that UAs will place on 
     * the end of lines.  
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param {String} type butt, round, square 
     * @returns this
     */
    lineCap: function (attribute){
	var name = 'lineCap';
	this.ctx[name] = attribute;
	return this;
    },
    /** The lineJoin attribute defines the type of corners that UAs will place 
     * where two lines meet. The three valid values are bevel, round, and miter. 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param {String} type
     * @returns this
     */
    lineJoin: function (attribute){
	var name = 'lineJoin';
	this.ctx[name] = attribute;
	return this;
    },
   
    /** Sets the miter limit 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param {Number} number
     * @returns this
     */
    miterLimit: function (attribute){
	var name = 'miterLimit';
	this.ctx[name] = attribute;
	return this;
    },
    /** Sets the font property and takes the same syntax as setting the font property 
     *  in CSS
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param {String} 
     * @returns this
     */
    font: function (attribute){
	var name = 'font';
	this.ctx[name] = attribute;
	return this;
    },

    /** Changes the text alignment. The possible values are start, end, left, right, 
     * and center. The default is start. Other values are ignored.
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param {string} alignment 
     * @returns this
     */
    textAlign: function (attribute){
	var name = 'textAlign';
	this.ctx[name] = attribute;
	return this;
    },

    /** Changes the baseline alignment. If the value is one of top, hanging, middle, 
     * alphabetic, ideographic, or bottom, then the value must be changed to the new value.
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param {String} alignment
     * @returns this
     */
    textBaseline: function (attribute){
	var name = 'textBaseline';
	this.ctx[name] = attribute;
	return this;
    },
    
    /** Save the current state of the context
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    save : function ( ){
	var name =  'save'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Restore the saved context
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    restore : function ( ){
	var name =  'restore'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
     /** Perform a scale transformation
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    scale : function ( ){
	var name =  'scale'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
   /** Perform a rotation transformation
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    rotate : function ( ){
	var name =  'rotate'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
     /** Performa a translation transformation
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    translate : function ( ){
	var name =  'translate'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    
    /** Transform the identity matrix
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    transform : function ( ){
	var name =  'transform'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Set the transform
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    setTransform : function ( ){
	var name =  'setTransform'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Clear a rectangular area 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    clearRect : function ( ){
	var name =  'clearRect'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Fill a rectangular area 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    fillRect : function ( ){
	var name =  'fillRect'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
      
    /** Draw the outline of the rectangle
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    strokeRect : function ( ){
	var name =  'strokeRect'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Begin a path 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    beginPath : function ( ){
	var name =  'beginPath'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** End a path 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    closePath : function ( ){
	var name =  'closePath'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Move to specified coordinates 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    moveTo : function ( ){
	var name =  'moveTo'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },


    /** Draw a line to the given coordinates 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    lineTo : function ( ){
	var name =  'lineTo'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },

    /** Draw a quadratic curve to given coordinates 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    quadraticCurveTo : function ( ){
	var name =  'quadraticCurveTo'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Draw a bezier curve to given coordinates 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    bezierCurveTo : function ( ){
	var name =  'bezierCurveTo'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Draw an arc to the given points
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    arcTo : function ( ){
	var name =  'arcTo'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Create an arc 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    arc : function ( ){
	var name =  'arc'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },

    /** Create a rectangle 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    rect : function ( ){
	var name =  'rect'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** fill in the current subpaths with the current fillstyle
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    fill : function ( ){
	var name =  'fill'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** Stroke the subpaths
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    stroke : function ( ){
	var name =  'stroke'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    clip : function ( ){
	var name =  'clip'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    fillText : function ( ){
	var name =  'fillText'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    strokeText : function ( ){
	var name =  'strokeText'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    measureText : function ( ){
	var name =  'measureText'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    isPointInPath : function ( ){
	var name =  'isPointInPath'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    
    /** Sets the stroke style 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    strokeStyle: function (attribute){
	var name = 'strokeStyle';
	this.ctx[name] = attribute;
	return this;
    },

    /** Sets the fill style
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    fillStyle: function (attribute){
	var name = 'fillStyle';
	this.ctx[name] = attribute;
	return this;
    },
     /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    createLinearGradient : function ( ){
	var name =  'createLinearGradient'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    createRadialGradient : function ( ){
	var name =  'createRadialGradient'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    createPattern : function ( ){
	var name =  'createPattern'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
     /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    shadowOffsetX: function (attribute){
	var name = 'shadowOffsetX';
	this.ctx[name] = attribute;
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    shadowOffsetY: function (attribute){
	var name = 'shadowOffsetY';
	this.ctx[name] = attribute;
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    shadowBlur: function (attribute){
	var name = 'shadowBlur';
	this.ctx[name] = attribute;
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    shadowColor: function (attribute){
	var name = 'shadowColor';
	this.ctx[name] = attribute;
	return this;
    },
   /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    drawImage : function ( ){
	var name =  'drawImage'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    getImageData : function ( ){
	var name =  'getImageData'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    putImageData : function ( ){
	var name =  'putImageData'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    createImageData : function ( ){
	var name =  'createImageData'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    /** description 
     *  For full details see <a href="http://www.whatwg.org/specs/web-apps/current-work/multipage/the-canvas-element.html#dom-context-2d-globalcompositeoperation">W3C docs</a>
     * @param 
     * @returns this
     */
    drawWindow : function ( ){
	var name =  'drawWindow'; 
	this.ctx[name].apply(this.ctx, arguments);
	return this;
    },
    

 
   
};


/** Prototype object for each svg element submitted to Karma in the
 * Karma() method
 * @throws {Error} if the name and domId for the svg element are not specified
 * @thows {Error} if the supplied domId does not match an element in the DOM
 * @class This object is the prototype for each svg element submitted to Karma in the
 * Karma() method
 */
Karma.kSvg = {
    /** name of instance, used internally 
     * @typeof string
     * @default ""
     */
    name : "",
    /** width of element 
     * @type number
     * @default 0
     */
    width: 0,
    /** height of element 
     * @type number
     * @default 0
     */
    height: 0,
    /** Status of element, either "loaded" or "error"
     * @type string
     * @default ""
     */
    status: "",
    /**  Whether canvas is visible. This value is read-only
     * @type boolean
     * @default true
     */
    visible: true,
    /** Element ID for canvas element in html document. 
     * @type String
     * @default undefined
     */
    domId: undefined,
    /** Reference to the DOM element.
     * @type DOMElement
     * @default undefined
     * @example 
     * //You can access all properties and methods of the underlying DOM element
     * //using the 'node' property
     * Karma.svg.someSvg.node.dispatchEvent;
     * Karma.svg.someSvg.node.addEvenListener(...);
     */
    node: undefined,
    /** Reference to the SVGDocument. You can use the this.doc to manipulate 
     * the SVG document 
     * @type SVGDocument
     * @default undefined
     * @example
     * var myElem = Karma.svg.someSvg.doc.getElementById('foobar');
     * Karma.svg.someSvg.doc.createElement(...);
     * Karma.svg.someSvg.doc.removeChild(someNode);
     *     
     */
    doc: undefined,
    /** Reference to the root element of the SVG Document 
     * @type DocumentElement
     * @default undefined
     * @example
     * // The root element is equivalent to "document" in a regular html document
     * // The root attribute is used frequently with the jQuery SVG plugin for CSS selectors
     * $('#someId', Karma.svg.someSvg.root).css(.. manipulate css attributes ...);
     */
    root: undefined,
    _localized : undefined,
    _init: function (config) {
	Karma._counters.total++;

	for (var option in config){
	    if (config.hasOwnProperty(option)){
		switch (option){
		case "name":
		    this.name = config[option];
		    break;
		case "domId":
		    this.domId = config[option];
		    break;
		case "width":
		    if(!this.height){
			throw new Error("If you specify a width you must also" +
					"specify a height");
		    }
		    this.width = parseInt(config[option], 10);
		    break;
		case "height":
		    if(!this.width){
			throw new Error("If you specify a height you must also" +
					"specify a width");
		    }
		    this.height = config[option];
		    break;
		}
	    }
	}
	
	if(this.domId && document.getElementById(this.domId)){
	       	this.node = document.getElementById(this.domId);
	} else {
	    throw new Error('you must specify a valid domId that' +
			    'is in your html page');
	}

	if(!config.height && !config.width){
	    this.width = parseInt(this.node.getAttribute('width'), 10);
	    this.height = parseInt(this.node.getAttribute('height'), 10);
	}
	
	var that = this;
	that._addEventHandlers();
		   
	return this;
	
	
    },
    _addEventHandlers : function () {
	var that = this;	
	that.doc = that.node.getSVGDocument();	
	that.node.addEventListener(
		"load", 
	    function (e) { 
		that.doc = that.node.getSVGDocument();    
		that.root = that.doc.documentElement;
		Karma._counters.loaded++;
		Karma._updateStatus();
		that.status = "loaded";
	    }, false);

	that.node.addEventListener(
	    "error", 
	    function (e) { 
		Karma._counters.loaded--;
		Karma._counters.errors++;
		that.status = "error";
		var errorMsg = "Error: " + that._type.toUpperCase() +
		    " " + that.name + " cannot be loaded."; 
		Karma._updateStatus(errorMsg);
	    }, 
	    false);
	that.node.addEventListener(
	    "abort", 
	    function (e) { 
		that.status = "aborted";
		var errorMsg = "ABORT: " + that._type.toUpperCase() +
		    " " + that.name + " loading was aborted."; 
		Karma._updateStatus(errorMsg);

	    }, false);

    }
};