svgcanvas.js 249 KB
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 1759 1760 1761 1762 1763 1764 1765 1766 1767 1768 1769 1770 1771 1772 1773 1774 1775 1776 1777 1778 1779 1780 1781 1782 1783 1784 1785 1786 1787 1788 1789 1790 1791 1792 1793 1794 1795 1796 1797 1798 1799 1800 1801 1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1846 1847 1848 1849 1850 1851 1852 1853 1854 1855 1856 1857 1858 1859 1860 1861 1862 1863 1864 1865 1866 1867 1868 1869 1870 1871 1872 1873 1874 1875 1876 1877 1878 1879 1880 1881 1882 1883 1884 1885 1886 1887 1888 1889 1890 1891 1892 1893 1894 1895 1896 1897 1898 1899 1900 1901 1902 1903 1904 1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 1915 1916 1917 1918 1919 1920 1921 1922 1923 1924 1925 1926 1927 1928 1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 1951 1952 1953 1954 1955 1956 1957 1958 1959 1960 1961 1962 1963 1964 1965 1966 1967 1968 1969 1970 1971 1972 1973 1974 1975 1976 1977 1978 1979 1980 1981 1982 1983 1984 1985 1986 1987 1988 1989 1990 1991 1992 1993 1994 1995 1996 1997 1998 1999 2000 2001 2002 2003 2004 2005 2006 2007 2008 2009 2010 2011 2012 2013 2014 2015 2016 2017 2018 2019 2020 2021 2022 2023 2024 2025 2026 2027 2028 2029 2030 2031 2032 2033 2034 2035 2036 2037 2038 2039 2040 2041 2042 2043 2044 2045 2046 2047 2048 2049 2050 2051 2052 2053 2054 2055 2056 2057 2058 2059 2060 2061 2062 2063 2064 2065 2066 2067 2068 2069 2070 2071 2072 2073 2074 2075 2076 2077 2078 2079 2080 2081 2082 2083 2084 2085 2086 2087 2088 2089 2090 2091 2092 2093 2094 2095 2096 2097 2098 2099 2100 2101 2102 2103 2104 2105 2106 2107 2108 2109 2110 2111 2112 2113 2114 2115 2116 2117 2118 2119 2120 2121 2122 2123 2124 2125 2126 2127 2128 2129 2130 2131 2132 2133 2134 2135 2136 2137 2138 2139 2140 2141 2142 2143 2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155 2156 2157 2158 2159 2160 2161 2162 2163 2164 2165 2166 2167 2168 2169 2170 2171 2172 2173 2174 2175 2176 2177 2178 2179 2180 2181 2182 2183 2184 2185 2186 2187 2188 2189 2190 2191 2192 2193 2194 2195 2196 2197 2198 2199 2200 2201 2202 2203 2204 2205 2206 2207 2208 2209 2210 2211 2212 2213 2214 2215 2216 2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231 2232 2233 2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258 2259 2260 2261 2262 2263 2264 2265 2266 2267 2268 2269 2270 2271 2272 2273 2274 2275 2276 2277 2278 2279 2280 2281 2282 2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294 2295 2296 2297 2298 2299 2300 2301 2302 2303 2304 2305 2306 2307 2308 2309 2310 2311 2312 2313 2314 2315 2316 2317 2318 2319 2320 2321 2322 2323 2324 2325 2326 2327 2328 2329 2330 2331 2332 2333 2334 2335 2336 2337 2338 2339 2340 2341 2342 2343 2344 2345 2346 2347 2348 2349 2350 2351 2352 2353 2354 2355 2356 2357 2358 2359 2360 2361 2362 2363 2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476 2477 2478 2479 2480 2481 2482 2483 2484 2485 2486 2487 2488 2489 2490 2491 2492 2493 2494 2495 2496 2497 2498 2499 2500 2501 2502 2503 2504 2505 2506 2507 2508 2509 2510 2511 2512 2513 2514 2515 2516 2517 2518 2519 2520 2521 2522 2523 2524 2525 2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536 2537 2538 2539 2540 2541 2542 2543 2544 2545 2546 2547 2548 2549 2550 2551 2552 2553 2554 2555 2556 2557 2558 2559 2560 2561 2562 2563 2564 2565 2566 2567 2568 2569 2570 2571 2572 2573 2574 2575 2576 2577 2578 2579 2580 2581 2582 2583 2584 2585 2586 2587 2588 2589 2590 2591 2592 2593 2594 2595 2596 2597 2598 2599 2600 2601 2602 2603 2604 2605 2606 2607 2608 2609 2610 2611 2612 2613 2614 2615 2616 2617 2618 2619 2620 2621 2622 2623 2624 2625 2626 2627 2628 2629 2630 2631 2632 2633 2634 2635 2636 2637 2638 2639 2640 2641 2642 2643 2644 2645 2646 2647 2648 2649 2650 2651 2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692 2693 2694 2695 2696 2697 2698 2699 2700 2701 2702 2703 2704 2705 2706 2707 2708 2709 2710 2711 2712 2713 2714 2715 2716 2717 2718 2719 2720 2721 2722 2723 2724 2725 2726 2727 2728 2729 2730 2731 2732 2733 2734 2735 2736 2737 2738 2739 2740 2741 2742 2743 2744 2745 2746 2747 2748 2749 2750 2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761 2762 2763 2764 2765 2766 2767 2768 2769 2770 2771 2772 2773 2774 2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792 2793 2794 2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806 2807 2808 2809 2810 2811 2812 2813 2814 2815 2816 2817 2818 2819 2820 2821 2822 2823 2824 2825 2826 2827 2828 2829 2830 2831 2832 2833 2834 2835 2836 2837 2838 2839 2840 2841 2842 2843 2844 2845 2846 2847 2848 2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860 2861 2862 2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873 2874 2875 2876 2877 2878 2879 2880 2881 2882 2883 2884 2885 2886 2887 2888 2889 2890 2891 2892 2893 2894 2895 2896 2897 2898 2899 2900 2901 2902 2903 2904 2905 2906 2907 2908 2909 2910 2911 2912 2913 2914 2915 2916 2917 2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998 2999 3000 3001 3002 3003 3004 3005 3006 3007 3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090 3091 3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107 3108 3109 3110 3111 3112 3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151 3152 3153 3154 3155 3156 3157 3158 3159 3160 3161 3162 3163 3164 3165 3166 3167 3168 3169 3170 3171 3172 3173 3174 3175 3176 3177 3178 3179 3180 3181 3182 3183 3184 3185 3186 3187 3188 3189 3190 3191 3192 3193 3194 3195 3196 3197 3198 3199 3200 3201 3202 3203 3204 3205 3206 3207 3208 3209 3210 3211 3212 3213 3214 3215 3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248 3249 3250 3251 3252 3253 3254 3255 3256 3257 3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314 3315 3316 3317 3318 3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365 3366 3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386 3387 3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504 3505 3506 3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611 3612 3613 3614 3615 3616 3617 3618 3619 3620 3621 3622 3623 3624 3625 3626 3627 3628 3629 3630 3631 3632 3633 3634 3635 3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658 3659 3660 3661 3662 3663 3664 3665 3666 3667 3668 3669 3670 3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697 3698 3699 3700 3701 3702 3703 3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721 3722 3723 3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750 3751 3752 3753 3754 3755 3756 3757 3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786 3787 3788 3789 3790 3791 3792 3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876 3877 3878 3879 3880 3881 3882 3883 3884 3885 3886 3887 3888 3889 3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914 3915 3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986 3987 3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007 4008 4009 4010 4011 4012 4013 4014 4015 4016 4017 4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047 4048 4049 4050 4051 4052 4053 4054 4055 4056 4057 4058 4059 4060 4061 4062 4063 4064 4065 4066 4067 4068 4069 4070 4071 4072 4073 4074 4075 4076 4077 4078 4079 4080 4081 4082 4083 4084 4085 4086 4087 4088 4089 4090 4091 4092 4093 4094 4095 4096 4097 4098 4099 4100 4101 4102 4103 4104 4105 4106 4107 4108 4109 4110 4111 4112 4113 4114 4115 4116 4117 4118 4119 4120 4121 4122 4123 4124 4125 4126 4127 4128 4129 4130 4131 4132 4133 4134 4135 4136 4137 4138 4139 4140 4141 4142 4143 4144 4145 4146 4147 4148 4149 4150 4151 4152 4153 4154 4155 4156 4157 4158 4159 4160 4161 4162 4163 4164 4165 4166 4167 4168 4169 4170 4171 4172 4173 4174 4175 4176 4177 4178 4179 4180 4181 4182 4183 4184 4185 4186 4187 4188 4189 4190 4191 4192 4193 4194 4195 4196 4197 4198 4199 4200 4201 4202 4203 4204 4205 4206 4207 4208 4209 4210 4211 4212 4213 4214 4215 4216 4217 4218 4219 4220 4221 4222 4223 4224 4225 4226 4227 4228 4229 4230 4231 4232 4233 4234 4235 4236 4237 4238 4239 4240 4241 4242 4243 4244 4245 4246 4247 4248 4249 4250 4251 4252 4253 4254 4255 4256 4257 4258 4259 4260 4261 4262 4263 4264 4265 4266 4267 4268 4269 4270 4271 4272 4273 4274 4275 4276 4277 4278 4279 4280 4281 4282 4283 4284 4285 4286 4287 4288 4289 4290 4291 4292 4293 4294 4295 4296 4297 4298 4299 4300 4301 4302 4303 4304 4305 4306 4307 4308 4309 4310 4311 4312 4313 4314 4315 4316 4317 4318 4319 4320 4321 4322 4323 4324 4325 4326 4327 4328 4329 4330 4331 4332 4333 4334 4335 4336 4337 4338 4339 4340 4341 4342 4343 4344 4345 4346 4347 4348 4349 4350 4351 4352 4353 4354 4355 4356 4357 4358 4359 4360 4361 4362 4363 4364 4365 4366 4367 4368 4369 4370 4371 4372 4373 4374 4375 4376 4377 4378 4379 4380 4381 4382 4383 4384 4385 4386 4387 4388 4389 4390 4391 4392 4393 4394 4395 4396 4397 4398 4399 4400 4401 4402 4403 4404 4405 4406 4407 4408 4409 4410 4411 4412 4413 4414 4415 4416 4417 4418 4419 4420 4421 4422 4423 4424 4425 4426 4427 4428 4429 4430 4431 4432 4433 4434 4435 4436 4437 4438 4439 4440 4441 4442 4443 4444 4445 4446 4447 4448 4449 4450 4451 4452 4453 4454 4455 4456 4457 4458 4459 4460 4461 4462 4463 4464 4465 4466 4467 4468 4469 4470 4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481 4482 4483 4484 4485 4486 4487 4488 4489 4490 4491 4492 4493 4494 4495 4496 4497 4498 4499 4500 4501 4502 4503 4504 4505 4506 4507 4508 4509 4510 4511 4512 4513 4514 4515 4516 4517 4518 4519 4520 4521 4522 4523 4524 4525 4526 4527 4528 4529 4530 4531 4532 4533 4534 4535 4536 4537 4538 4539 4540 4541 4542 4543 4544 4545 4546 4547 4548 4549 4550 4551 4552 4553 4554 4555 4556 4557 4558 4559 4560 4561 4562 4563 4564 4565 4566 4567 4568 4569 4570 4571 4572 4573 4574 4575 4576 4577 4578 4579 4580 4581 4582 4583 4584 4585 4586 4587 4588 4589 4590 4591 4592 4593 4594 4595 4596 4597 4598 4599 4600 4601 4602 4603 4604 4605 4606 4607 4608 4609 4610 4611 4612 4613 4614 4615 4616 4617 4618 4619 4620 4621 4622 4623 4624 4625 4626 4627 4628 4629 4630 4631 4632 4633 4634 4635 4636 4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647 4648 4649 4650 4651 4652 4653 4654 4655 4656 4657 4658 4659 4660 4661 4662 4663 4664 4665 4666 4667 4668 4669 4670 4671 4672 4673 4674 4675 4676 4677 4678 4679 4680 4681 4682 4683 4684 4685 4686 4687 4688 4689 4690 4691 4692 4693 4694 4695 4696 4697 4698 4699 4700 4701 4702 4703 4704 4705 4706 4707 4708 4709 4710 4711 4712 4713 4714 4715 4716 4717 4718 4719 4720 4721 4722 4723 4724 4725 4726 4727 4728 4729 4730 4731 4732 4733 4734 4735 4736 4737 4738 4739 4740 4741 4742 4743 4744 4745 4746 4747 4748 4749 4750 4751 4752 4753 4754 4755 4756 4757 4758 4759 4760 4761 4762 4763 4764 4765 4766 4767 4768 4769 4770 4771 4772 4773 4774 4775 4776 4777 4778 4779 4780 4781 4782 4783 4784 4785 4786 4787 4788 4789 4790 4791 4792 4793 4794 4795 4796 4797 4798 4799 4800 4801 4802 4803 4804 4805 4806 4807 4808 4809 4810 4811 4812 4813 4814 4815 4816 4817 4818 4819 4820 4821 4822 4823 4824 4825 4826 4827 4828 4829 4830 4831 4832 4833 4834 4835 4836 4837 4838 4839 4840 4841 4842 4843 4844 4845 4846 4847 4848 4849 4850 4851 4852 4853 4854 4855 4856 4857 4858 4859 4860 4861 4862 4863 4864 4865 4866 4867 4868 4869 4870 4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888 4889 4890 4891 4892 4893 4894 4895 4896 4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910 4911 4912 4913 4914 4915 4916 4917 4918 4919 4920 4921 4922 4923 4924 4925 4926 4927 4928 4929 4930 4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948 4949 4950 4951 4952 4953 4954 4955 4956 4957 4958 4959 4960 4961 4962 4963 4964 4965 4966 4967 4968 4969 4970 4971 4972 4973 4974 4975 4976 4977 4978 4979 4980 4981 4982 4983 4984 4985 4986 4987 4988 4989 4990 4991 4992 4993 4994 4995 4996 4997 4998 4999 5000 5001 5002 5003 5004 5005 5006 5007 5008 5009 5010 5011 5012 5013 5014 5015 5016 5017 5018 5019 5020 5021 5022 5023 5024 5025 5026 5027 5028 5029 5030 5031 5032 5033 5034 5035 5036 5037 5038 5039 5040 5041 5042 5043 5044 5045 5046 5047 5048 5049 5050 5051 5052 5053 5054 5055 5056 5057 5058 5059 5060 5061 5062 5063 5064 5065 5066 5067 5068 5069 5070 5071 5072 5073 5074 5075 5076 5077 5078 5079 5080 5081 5082 5083 5084 5085 5086 5087 5088 5089 5090 5091 5092 5093 5094 5095 5096 5097 5098 5099 5100 5101 5102 5103 5104 5105 5106 5107 5108 5109 5110 5111 5112 5113 5114 5115 5116 5117 5118 5119 5120 5121 5122 5123 5124 5125 5126 5127 5128 5129 5130 5131 5132 5133 5134 5135 5136 5137 5138 5139 5140 5141 5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152 5153 5154 5155 5156 5157 5158 5159 5160 5161 5162 5163 5164 5165 5166 5167 5168 5169 5170 5171 5172 5173 5174 5175 5176 5177 5178 5179 5180 5181 5182 5183 5184 5185 5186 5187 5188 5189 5190 5191 5192 5193 5194 5195 5196 5197 5198 5199 5200 5201 5202 5203 5204 5205 5206 5207 5208 5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221 5222 5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235 5236 5237 5238 5239 5240 5241 5242 5243 5244 5245 5246 5247 5248 5249 5250 5251 5252 5253 5254 5255 5256 5257 5258 5259 5260 5261 5262 5263 5264 5265 5266 5267 5268 5269 5270 5271 5272 5273 5274 5275 5276 5277 5278 5279 5280 5281 5282 5283 5284 5285 5286 5287 5288 5289 5290 5291 5292 5293 5294 5295 5296 5297 5298 5299 5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312 5313 5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327 5328 5329 5330 5331 5332 5333 5334 5335 5336 5337 5338 5339 5340 5341 5342 5343 5344 5345 5346 5347 5348 5349 5350 5351 5352 5353 5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367 5368 5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381 5382 5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403 5404 5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418 5419 5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435 5436 5437 5438 5439 5440 5441 5442 5443 5444 5445 5446 5447 5448 5449 5450 5451 5452 5453 5454 5455 5456 5457 5458 5459 5460 5461 5462 5463 5464 5465 5466 5467 5468 5469 5470 5471 5472 5473 5474 5475 5476 5477 5478 5479 5480 5481 5482 5483 5484 5485 5486 5487 5488 5489 5490 5491 5492 5493 5494 5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505 5506 5507 5508 5509 5510 5511 5512 5513 5514 5515 5516 5517 5518 5519 5520 5521 5522 5523 5524 5525 5526 5527 5528 5529 5530 5531 5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549 5550 5551 5552 5553 5554 5555 5556 5557 5558 5559 5560 5561 5562 5563 5564 5565 5566 5567 5568 5569 5570 5571 5572 5573 5574 5575 5576 5577 5578 5579 5580 5581 5582 5583 5584 5585 5586 5587 5588 5589 5590 5591 5592 5593 5594 5595 5596 5597 5598 5599 5600 5601 5602 5603 5604 5605 5606 5607 5608 5609 5610 5611 5612 5613 5614 5615 5616 5617 5618 5619 5620 5621 5622 5623 5624 5625 5626 5627 5628 5629 5630 5631 5632 5633 5634 5635 5636 5637 5638 5639 5640 5641 5642 5643 5644 5645 5646 5647 5648 5649 5650 5651 5652 5653 5654 5655 5656 5657 5658 5659 5660 5661 5662 5663 5664 5665 5666 5667 5668 5669 5670 5671 5672 5673 5674 5675 5676 5677 5678 5679 5680 5681 5682 5683 5684 5685 5686 5687 5688 5689 5690 5691 5692 5693 5694 5695 5696 5697 5698 5699 5700 5701 5702 5703 5704 5705 5706 5707 5708 5709 5710 5711 5712 5713 5714 5715 5716 5717 5718 5719 5720 5721 5722 5723 5724 5725 5726 5727 5728 5729 5730 5731 5732 5733 5734 5735 5736 5737 5738 5739 5740 5741 5742 5743 5744 5745 5746 5747 5748 5749 5750 5751 5752 5753 5754 5755 5756 5757 5758 5759 5760 5761 5762 5763 5764 5765 5766 5767 5768 5769 5770 5771 5772 5773 5774 5775 5776 5777 5778 5779 5780 5781 5782 5783 5784 5785 5786 5787 5788 5789 5790 5791 5792 5793 5794 5795 5796 5797 5798 5799 5800 5801 5802 5803 5804 5805 5806 5807 5808 5809 5810 5811 5812 5813 5814 5815 5816 5817 5818 5819 5820 5821 5822 5823 5824 5825 5826 5827 5828 5829 5830 5831 5832 5833 5834 5835 5836 5837 5838 5839 5840 5841 5842 5843 5844 5845 5846 5847 5848 5849 5850 5851 5852 5853 5854 5855 5856 5857 5858 5859 5860 5861 5862 5863 5864 5865 5866 5867 5868 5869 5870 5871 5872 5873 5874 5875 5876 5877 5878 5879 5880 5881 5882 5883 5884 5885 5886 5887 5888 5889 5890 5891 5892 5893 5894 5895 5896 5897 5898 5899 5900 5901 5902 5903 5904 5905 5906 5907 5908 5909 5910 5911 5912 5913 5914 5915 5916 5917 5918 5919 5920 5921 5922 5923 5924 5925 5926 5927 5928 5929 5930 5931 5932 5933 5934 5935 5936 5937 5938 5939 5940 5941 5942 5943 5944 5945 5946 5947 5948 5949 5950 5951 5952 5953 5954 5955 5956 5957 5958 5959 5960 5961 5962 5963 5964 5965 5966 5967 5968 5969 5970 5971 5972 5973 5974 5975 5976 5977 5978 5979 5980 5981 5982 5983 5984 5985 5986 5987 5988 5989 5990 5991 5992 5993 5994 5995 5996 5997 5998 5999 6000 6001 6002 6003 6004 6005 6006 6007 6008 6009 6010 6011 6012 6013 6014 6015 6016 6017 6018 6019 6020 6021 6022 6023 6024 6025 6026 6027 6028 6029 6030 6031 6032 6033 6034 6035 6036 6037 6038 6039 6040 6041 6042 6043 6044 6045 6046 6047 6048 6049 6050 6051 6052 6053 6054 6055 6056 6057 6058 6059 6060 6061 6062 6063 6064 6065 6066 6067 6068 6069 6070 6071 6072 6073 6074 6075 6076 6077 6078 6079 6080 6081 6082 6083 6084 6085 6086 6087 6088 6089 6090 6091 6092 6093 6094 6095 6096 6097 6098 6099 6100 6101 6102 6103 6104 6105 6106 6107 6108 6109 6110 6111 6112 6113 6114 6115 6116 6117 6118 6119 6120 6121 6122 6123 6124 6125 6126 6127 6128 6129 6130 6131 6132 6133 6134 6135 6136 6137 6138 6139 6140 6141 6142 6143 6144 6145 6146 6147 6148 6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161 6162 6163 6164 6165 6166 6167 6168 6169 6170 6171 6172 6173 6174 6175 6176 6177 6178 6179 6180 6181 6182 6183 6184 6185 6186 6187 6188 6189 6190 6191 6192 6193 6194 6195 6196 6197 6198 6199 6200 6201 6202 6203 6204 6205 6206 6207 6208 6209 6210 6211 6212 6213 6214 6215 6216 6217 6218 6219 6220 6221 6222 6223 6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239 6240 6241 6242 6243 6244 6245 6246 6247 6248 6249 6250 6251 6252 6253 6254 6255 6256 6257 6258 6259 6260 6261 6262 6263 6264 6265 6266 6267 6268 6269 6270 6271 6272 6273 6274 6275 6276 6277 6278 6279 6280 6281 6282 6283 6284 6285 6286 6287 6288 6289 6290 6291 6292 6293 6294 6295 6296 6297 6298 6299 6300 6301 6302 6303 6304 6305 6306 6307 6308 6309 6310 6311 6312 6313 6314 6315 6316 6317 6318 6319 6320 6321 6322 6323 6324 6325 6326 6327 6328 6329 6330 6331 6332 6333 6334 6335 6336 6337 6338 6339 6340 6341 6342 6343 6344 6345 6346 6347 6348 6349 6350 6351 6352 6353 6354 6355 6356 6357 6358 6359 6360 6361 6362 6363 6364 6365 6366 6367 6368 6369 6370 6371 6372 6373 6374 6375 6376 6377 6378 6379 6380 6381 6382 6383 6384 6385 6386 6387 6388 6389 6390 6391 6392 6393 6394 6395 6396 6397 6398 6399 6400 6401 6402 6403 6404 6405 6406 6407 6408 6409 6410 6411 6412 6413 6414 6415 6416 6417 6418 6419 6420 6421 6422 6423 6424 6425 6426 6427 6428 6429 6430 6431 6432 6433 6434 6435 6436 6437 6438 6439 6440 6441 6442 6443 6444 6445 6446 6447 6448 6449 6450 6451 6452 6453 6454 6455 6456 6457 6458 6459 6460 6461 6462 6463 6464 6465 6466 6467 6468 6469 6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480 6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493 6494 6495 6496 6497 6498 6499 6500 6501 6502 6503 6504 6505 6506 6507 6508 6509 6510 6511 6512 6513 6514 6515 6516 6517 6518 6519 6520 6521 6522 6523 6524 6525 6526 6527 6528 6529 6530 6531 6532 6533 6534 6535 6536 6537 6538 6539 6540 6541 6542 6543 6544 6545 6546 6547 6548 6549 6550 6551 6552 6553 6554 6555 6556 6557 6558 6559 6560 6561 6562 6563 6564 6565 6566 6567 6568 6569 6570 6571 6572 6573 6574 6575 6576 6577 6578 6579 6580 6581 6582 6583 6584 6585 6586 6587 6588 6589 6590 6591 6592 6593 6594 6595 6596 6597 6598 6599 6600 6601 6602 6603 6604 6605 6606 6607 6608 6609 6610 6611 6612 6613 6614 6615 6616 6617 6618 6619 6620 6621 6622 6623 6624 6625 6626 6627 6628 6629 6630 6631 6632 6633 6634 6635 6636 6637 6638 6639 6640 6641 6642 6643 6644 6645 6646 6647 6648 6649 6650 6651 6652 6653 6654 6655 6656 6657 6658 6659 6660 6661 6662 6663 6664 6665 6666 6667 6668 6669 6670 6671 6672 6673 6674 6675 6676 6677 6678 6679 6680 6681 6682 6683 6684 6685 6686 6687 6688 6689 6690 6691 6692 6693 6694 6695 6696 6697 6698 6699 6700 6701 6702 6703 6704 6705 6706 6707 6708 6709 6710 6711 6712 6713 6714 6715 6716 6717 6718 6719 6720 6721 6722 6723 6724 6725 6726 6727 6728 6729 6730 6731 6732 6733 6734 6735 6736 6737 6738 6739 6740 6741 6742 6743 6744 6745 6746 6747 6748 6749 6750 6751 6752 6753 6754 6755 6756 6757 6758 6759 6760 6761 6762 6763 6764 6765 6766 6767 6768 6769 6770 6771 6772 6773 6774 6775 6776 6777 6778 6779 6780 6781 6782 6783 6784 6785 6786 6787 6788 6789 6790 6791 6792 6793 6794 6795 6796 6797 6798 6799 6800 6801 6802 6803 6804 6805 6806 6807 6808 6809 6810 6811 6812 6813 6814 6815 6816 6817 6818 6819 6820 6821 6822 6823 6824 6825 6826 6827 6828 6829 6830 6831 6832 6833 6834 6835 6836 6837 6838 6839 6840 6841 6842 6843 6844 6845 6846 6847 6848 6849 6850 6851 6852 6853 6854 6855 6856 6857 6858 6859 6860 6861 6862 6863 6864 6865 6866 6867 6868 6869 6870 6871 6872 6873 6874 6875 6876 6877 6878 6879 6880 6881 6882 6883 6884 6885 6886 6887 6888 6889 6890 6891 6892 6893 6894 6895 6896 6897 6898 6899 6900 6901 6902 6903 6904 6905 6906 6907 6908 6909 6910 6911 6912 6913 6914 6915 6916 6917 6918 6919 6920 6921 6922 6923 6924 6925 6926 6927 6928 6929 6930 6931 6932 6933 6934 6935 6936 6937 6938 6939 6940 6941 6942 6943 6944 6945 6946 6947 6948 6949 6950 6951 6952 6953 6954 6955 6956 6957 6958 6959 6960 6961 6962 6963 6964 6965 6966 6967 6968 6969 6970 6971 6972 6973 6974 6975 6976 6977 6978 6979 6980 6981 6982 6983 6984 6985 6986 6987 6988 6989 6990 6991 6992 6993 6994 6995 6996 6997 6998 6999 7000 7001 7002 7003 7004 7005 7006 7007 7008 7009 7010 7011 7012 7013 7014 7015 7016 7017 7018 7019 7020 7021 7022 7023 7024 7025 7026 7027 7028 7029 7030 7031 7032 7033 7034 7035 7036 7037 7038 7039 7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173 7174 7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189 7190 7191 7192 7193 7194 7195 7196 7197 7198 7199 7200 7201 7202 7203 7204 7205 7206 7207 7208 7209 7210 7211 7212 7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230 7231 7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248 7249 7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267 7268 7269 7270 7271 7272 7273 7274 7275 7276 7277 7278 7279 7280 7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302 7303 7304 7305 7306 7307 7308 7309 7310 7311 7312 7313 7314 7315 7316 7317 7318 7319 7320 7321 7322 7323 7324 7325 7326 7327 7328 7329 7330 7331 7332 7333 7334 7335 7336 7337 7338 7339 7340 7341 7342 7343 7344 7345 7346 7347 7348 7349 7350 7351 7352 7353 7354 7355 7356 7357 7358 7359 7360 7361 7362 7363 7364 7365 7366 7367 7368 7369 7370 7371 7372 7373 7374 7375 7376 7377 7378 7379 7380 7381 7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411 7412 7413 7414 7415 7416 7417 7418 7419 7420 7421 7422 7423 7424 7425 7426 7427 7428 7429 7430 7431 7432 7433 7434 7435 7436 7437 7438 7439 7440 7441 7442 7443 7444 7445 7446 7447 7448 7449 7450 7451 7452 7453 7454 7455 7456 7457 7458 7459 7460 7461 7462 7463 7464 7465 7466 7467 7468 7469 7470 7471 7472 7473 7474 7475 7476 7477 7478 7479 7480 7481 7482 7483 7484 7485 7486 7487 7488 7489 7490 7491 7492 7493 7494 7495 7496 7497 7498 7499 7500 7501 7502 7503 7504 7505 7506 7507 7508 7509 7510 7511 7512 7513 7514 7515 7516 7517 7518 7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544 7545 7546 7547 7548 7549 7550 7551 7552 7553 7554 7555 7556 7557 7558 7559 7560 7561 7562 7563 7564 7565 7566 7567 7568 7569 7570 7571 7572 7573 7574 7575 7576 7577 7578 7579 7580 7581 7582 7583 7584 7585 7586 7587 7588 7589 7590 7591 7592 7593 7594 7595 7596 7597 7598 7599 7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614 7615 7616 7617 7618 7619 7620 7621 7622 7623 7624 7625 7626 7627 7628 7629 7630 7631 7632 7633 7634 7635 7636 7637 7638 7639 7640 7641 7642 7643 7644 7645 7646 7647 7648 7649 7650 7651 7652 7653 7654 7655 7656 7657 7658 7659 7660 7661 7662 7663 7664 7665 7666 7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680 7681 7682 7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696 7697 7698 7699 7700 7701 7702 7703 7704 7705 7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717 7718 7719 7720 7721 7722 7723 7724 7725 7726 7727 7728 7729 7730 7731 7732 7733 7734 7735 7736 7737 7738 7739 7740 7741 7742 7743 7744 7745 7746 7747 7748 7749 7750 7751 7752 7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769 7770 7771 7772 7773 7774 7775 7776 7777 7778 7779 7780 7781 7782 7783 7784 7785 7786 7787 7788 7789 7790 7791 7792 7793 7794 7795 7796 7797 7798 7799 7800 7801 7802 7803 7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818 7819 7820 7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832 7833 7834 7835 7836 7837 7838 7839 7840 7841 7842 7843 7844 7845 7846 7847 7848 7849 7850 7851 7852 7853 7854 7855 7856 7857 7858 7859 7860 7861 7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872 7873 7874 7875 7876 7877 7878 7879 7880 7881 7882 7883 7884 7885 7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939 7940 7941 7942 7943 7944 7945 7946 7947 7948 7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961 7962 7963 7964 7965 7966 7967 7968 7969 7970 7971 7972 7973 7974 7975 7976 7977 7978 7979 7980 7981 7982 7983 7984 7985 7986 7987 7988 7989 7990 7991 7992 7993 7994 7995 7996 7997 7998 7999 8000 8001 8002 8003 8004 8005 8006 8007 8008 8009 8010 8011 8012 8013 8014 8015 8016 8017 8018 8019 8020 8021 8022 8023 8024 8025 8026 8027 8028 8029 8030 8031 8032 8033 8034 8035 8036 8037 8038 8039 8040 8041 8042 8043 8044 8045 8046 8047 8048 8049 8050 8051 8052 8053 8054 8055 8056 8057 8058 8059 8060 8061 8062 8063 8064 8065 8066 8067 8068 8069 8070 8071 8072 8073 8074 8075 8076 8077 8078 8079 8080 8081 8082 8083 8084 8085 8086 8087 8088 8089 8090 8091 8092 8093 8094 8095 8096 8097 8098 8099 8100 8101 8102 8103 8104 8105 8106 8107 8108 8109 8110 8111 8112 8113 8114 8115 8116 8117 8118 8119 8120 8121 8122 8123 8124 8125 8126 8127 8128 8129 8130 8131 8132 8133 8134 8135 8136 8137 8138 8139 8140 8141 8142 8143 8144 8145 8146 8147 8148 8149 8150 8151 8152 8153 8154 8155 8156 8157 8158 8159 8160 8161 8162 8163 8164 8165 8166 8167 8168 8169 8170 8171 8172 8173 8174 8175 8176 8177 8178 8179 8180 8181 8182 8183 8184 8185 8186 8187 8188 8189 8190 8191 8192 8193 8194 8195 8196 8197 8198 8199 8200 8201 8202 8203 8204 8205 8206 8207 8208 8209 8210 8211 8212 8213 8214 8215 8216 8217 8218 8219 8220 8221 8222 8223 8224 8225 8226 8227 8228 8229 8230 8231 8232 8233 8234 8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328 8329 8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347 8348 8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369 8370 8371 8372 8373 8374 8375 8376 8377 8378 8379 8380 8381 8382 8383 8384 8385 8386 8387 8388 8389 8390 8391 8392 8393 8394 8395 8396 8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420 8421 8422 8423 8424 8425 8426 8427 8428 8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439 8440 8441 8442 8443 8444 8445 8446 8447 8448 8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488 8489 8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507 8508 8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523 8524 8525 8526 8527 8528 8529 8530 8531 8532 8533 8534 8535 8536 8537 8538 8539 8540 8541 8542 8543 8544 8545 8546 8547 8548 8549 8550 8551 8552 8553 8554 8555 8556 8557 8558 8559 8560 8561 8562 8563 8564 8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575 8576 8577 8578 8579 8580 8581 8582 8583 8584 8585 8586 8587 8588 8589 8590 8591 8592 8593 8594 8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671 8672 8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683 8684 8685 8686 8687 8688 8689 8690 8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709 8710 8711 8712 8713 8714 8715 8716 8717 8718 8719 8720 8721 8722 8723 8724 8725 8726 8727 8728 8729 8730 8731 8732 8733 8734 8735 8736 8737 8738 8739 8740 8741 8742 8743 8744 8745 8746 8747 8748 8749 8750 8751 8752 8753 8754 8755 8756 8757 8758 8759 8760 8761 8762 8763 8764 8765 8766 8767 8768 8769 8770 8771
/*
 * svgcanvas.js
 *
 * Licensed under the Apache License, Version 2
 *
 * Copyright(c) 2010 Alexis Deveria
 * Copyright(c) 2010 Pavol Rusnak
 * Copyright(c) 2010 Jeff Schiller
 *
 */

// Dependencies:
// 1) jQuery
// 2) browser.js
// 3) svgtransformlist.js
// 4) math.js
// 5) units.js
// 6) svgutils.js
// 7) sanitize.js
// 8) history.js
// 9) select.js
// 10) draw.js
// 11) path.js

if(!window.console) {
	window.console = {};
	window.console.log = function(str) {};
	window.console.dir = function(str) {};
}

if(window.opera) {
	window.console.log = function(str) { opera.postError(str); };
	window.console.dir = function(str) {};
}

(function() {

	// This fixes $(...).attr() to work as expected with SVG elements.
	// Does not currently use *AttributeNS() since we rarely need that.
	
	// See http://api.jquery.com/attr/ for basic documentation of .attr()
	
	// Additional functionality: 
	// - When getting attributes, a string that's a number is return as type number.
	// - If an array is supplied as first parameter, multiple values are returned
	// as an object with values for each given attributes

	var proxied = jQuery.fn.attr, svgns = "http://www.w3.org/2000/svg";
	jQuery.fn.attr = function(key, value) {
		var len = this.length;
		if(!len) return proxied.apply(this, arguments);
		for(var i=0; i<len; i++) {
			var elem = this[i];
			// set/get SVG attribute
			if(elem.namespaceURI === svgns) {
				// Setting attribute
				if(value !== undefined) {
					elem.setAttribute(key, value);
				} else if($.isArray(key)) {
					// Getting attributes from array
					var j = key.length, obj = {};

					while(j--) {
						var aname = key[j];
						var attr = elem.getAttribute(aname);
						// This returns a number when appropriate
						if(attr || attr === "0") {
							attr = isNaN(attr)?attr:attr-0;
						}
						obj[aname] = attr;
					}
					return obj;
				
				} else if(typeof key === "object") {
					// Setting attributes form object
					for(var v in key) {
						elem.setAttribute(v, key[v]);
					}
				// Getting attribute
				} else {
					var attr = elem.getAttribute(key);
					if(attr || attr === "0") {
						attr = isNaN(attr)?attr:attr-0;
					}

					return attr;
				}
			} else {
				return proxied.apply(this, arguments);
			}
		}
		return this;
	};
	
}());

// Class: SvgCanvas
// The main SvgCanvas class that manages all SVG-related functions
//
// Parameters:
// container - The container HTML element that should hold the SVG root element
// config - An object that contains configuration data
$.SvgCanvas = function(container, config)
{
// Namespace constants
var svgns = "http://www.w3.org/2000/svg",
	xlinkns = "http://www.w3.org/1999/xlink",
	xmlns = "http://www.w3.org/XML/1998/namespace",
	xmlnsns = "http://www.w3.org/2000/xmlns/", // see http://www.w3.org/TR/REC-xml-names/#xmlReserved
	se_ns = "http://svg-edit.googlecode.com",
	htmlns = "http://www.w3.org/1999/xhtml",
	mathns = "http://www.w3.org/1998/Math/MathML";

// Default configuration options
var curConfig = {
	show_outside_canvas: true,
	selectNew: true,
	dimensions: [640, 480]
};

// Update config with new one if given
if(config) {
	$.extend(curConfig, config);
}

// Array with width/height of canvas
var dimensions = curConfig.dimensions;

var canvas = this;

// "document" element associated with the container (same as window.document using default svg-editor.js)
// NOTE: This is not actually a SVG document, but a HTML document.
var svgdoc = container.ownerDocument;

// This is a container for the document being edited, not the document itself.
var svgroot = svgdoc.importNode(svgedit.utilities.text2xml(
		'<svg id="svgroot" xmlns="' + svgns + '" xlinkns="' + xlinkns + '" ' +
			'width="' + dimensions[0] + '" height="' + dimensions[1] + '" x="' + dimensions[0] + '" y="' + dimensions[1] + '" overflow="visible">' +
			'<defs>' +
				'<filter id="canvashadow" filterUnits="objectBoundingBox">' +
					'<feGaussianBlur in="SourceAlpha" stdDeviation="4" result="blur"/>'+
					'<feOffset in="blur" dx="5" dy="5" result="offsetBlur"/>'+
					'<feMerge>'+
						'<feMergeNode in="offsetBlur"/>'+
						'<feMergeNode in="SourceGraphic"/>'+
					'</feMerge>'+
				'</filter>'+
			'</defs>'+
		'</svg>').documentElement, true);
container.appendChild(svgroot);

// The actual element that represents the final output SVG element
var svgcontent = svgdoc.createElementNS(svgns, "svg");

// This function resets the svgcontent element while keeping it in the DOM.
var clearSvgContentElement = canvas.clearSvgContentElement = function() {
	while (svgcontent.firstChild) { svgcontent.removeChild(svgcontent.firstChild); }

	// TODO: Clear out all other attributes first?
	$(svgcontent).attr({
		id: 'svgcontent',
		width: dimensions[0],
		height: dimensions[1],
		x: dimensions[0],
		y: dimensions[1],
		overflow: curConfig.show_outside_canvas ? 'visible' : 'hidden',
		xmlns: svgns,
		"xmlns:se": se_ns,
		"xmlns:xlink": xlinkns
	}).appendTo(svgroot);

	// TODO: make this string optional and set by the client
	var comment = svgdoc.createComment(" Created with SVG-edit - http://svg-edit.googlecode.com/ ");
	svgcontent.appendChild(comment);
};
clearSvgContentElement();

// Prefix string for element IDs
var idprefix = "svg_";

// Function: setIdPrefix
// Changes the ID prefix to the given value
//
// Parameters: 
// p - String with the new prefix 
canvas.setIdPrefix = function(p) {
	idprefix = p;
};

// Current svgedit.draw.Drawing object
// @type {svgedit.draw.Drawing}
canvas.current_drawing_ = new svgedit.draw.Drawing(svgcontent, idprefix);

// Function: getCurrentDrawing
// Returns the current Drawing.
// @return {svgedit.draw.Drawing}
var getCurrentDrawing = canvas.getCurrentDrawing = function() {
	return canvas.current_drawing_;
};

// Float displaying the current zoom level (1 = 100%, .5 = 50%, etc)
var current_zoom = 1;

// pointer to current group (for in-group editing)
var current_group = null;

// Object containing data for the currently selected styles
var all_properties = {
	shape: {
		fill: (curConfig.initFill.color == 'none' ? '' : '#') + curConfig.initFill.color,
		fill_paint: null,
		fill_opacity: curConfig.initFill.opacity,
		stroke: "#" + curConfig.initStroke.color,
		stroke_paint: null,
		stroke_opacity: curConfig.initStroke.opacity,
		stroke_width: curConfig.initStroke.width,
		stroke_dasharray: 'none',
		stroke_linejoin: 'miter',
		stroke_linecap: 'butt',
		opacity: curConfig.initOpacity
	}
};

all_properties.text = $.extend(true, {}, all_properties.shape);
$.extend(all_properties.text, {
	fill: "#000000",
	stroke_width: 0,
	font_size: 24,
	font_family: 'serif'
});

// Current shape style properties
var cur_shape = all_properties.shape;

// Array with all the currently selected elements
// default size of 1 until it needs to grow bigger
var selectedElements = new Array(1);

// Function: addSvgElementFromJson
// Create a new SVG element based on the given object keys/values and add it to the current layer
// The element will be ran through cleanupElement before being returned 
//
// Parameters:
// data - Object with the following keys/values:
// * element - tag name of the SVG element to create
// * attr - Object with attributes key-values to assign to the new element
// * curStyles - Boolean indicating that current style attributes should be applied first
//
// Returns: The new element
var addSvgElementFromJson = this.addSvgElementFromJson = function(data) {
	var shape = svgedit.utilities.getElem(data.attr.id);
	// if shape is a path but we need to create a rect/ellipse, then remove the path
	var current_layer = getCurrentDrawing().getCurrentLayer();
	if (shape && data.element != shape.tagName) {
		current_layer.removeChild(shape);
		shape = null;
	}
	if (!shape) {
		shape = svgdoc.createElementNS(svgns, data.element);
		if (current_layer) {
			(current_group || current_layer).appendChild(shape);
		}
	}
	if(data.curStyles) {
		svgedit.utilities.assignAttributes(shape, {
			"fill": cur_shape.fill,
			"stroke": cur_shape.stroke,
			"stroke-width": cur_shape.stroke_width,
			"stroke-dasharray": cur_shape.stroke_dasharray,
			"stroke-linejoin": cur_shape.stroke_linejoin,
			"stroke-linecap": cur_shape.stroke_linecap,
			"stroke-opacity": cur_shape.stroke_opacity,
			"fill-opacity": cur_shape.fill_opacity,
			"opacity": cur_shape.opacity / 2,
			"style": "pointer-events:inherit"
		}, 100);
	}
	svgedit.utilities.assignAttributes(shape, data.attr, 100);
	svgedit.utilities.cleanupElement(shape);
	return shape;
};


// import svgtransformlist.js
var getTransformList = canvas.getTransformList = svgedit.transformlist.getTransformList;

// import from math.js.
var transformPoint = svgedit.math.transformPoint;
var matrixMultiply = canvas.matrixMultiply = svgedit.math.matrixMultiply;
var hasMatrixTransform = canvas.hasMatrixTransform = svgedit.math.hasMatrixTransform;
var transformListToTransform = canvas.transformListToTransform = svgedit.math.transformListToTransform;
var snapToAngle = svgedit.math.snapToAngle;
var getMatrix = svgedit.math.getMatrix;

// initialize from units.js
// send in an object implementing the ElementContainer interface (see units.js)
svgedit.units.init({
	getBaseUnit: function() { return curConfig.baseUnit; },
	getElement: svgedit.utilities.getElem,
	getHeight: function() { return svgcontent.getAttribute("height")/current_zoom; },
	getWidth: function() { return svgcontent.getAttribute("width")/current_zoom; },
	getRoundDigits: function() { return save_options.round_digits; }
});
// import from units.js
var convertToNum = canvas.convertToNum = svgedit.units.convertToNum;

// import from svgutils.js
svgedit.utilities.init({
	getDOMDocument: function() { return svgdoc; },
	getDOMContainer: function() { return container; },
	getSVGRoot: function() { return svgroot; },
	// TODO: replace this mostly with a way to get the current drawing.
	getSelectedElements: function() { return selectedElements; },
	getSVGContent: function() { return svgcontent; }
});
var getUrlFromAttr = canvas.getUrlFromAttr = svgedit.utilities.getUrlFromAttr;
var getHref = canvas.getHref = svgedit.utilities.getHref;
var setHref = canvas.setHref = svgedit.utilities.setHref;
var getPathBBox = svgedit.utilities.getPathBBox;
var getBBox = canvas.getBBox = svgedit.utilities.getBBox;
var getRotationAngle = canvas.getRotationAngle = svgedit.utilities.getRotationAngle;
var getElem = canvas.getElem = svgedit.utilities.getElem;
var assignAttributes = canvas.assignAttributes = svgedit.utilities.assignAttributes;
var cleanupElement = this.cleanupElement = svgedit.utilities.cleanupElement;

// import from sanitize.js
var nsMap = svgedit.sanitize.getNSMap();
var sanitizeSvg = canvas.sanitizeSvg = svgedit.sanitize.sanitizeSvg;

// import from history.js
var MoveElementCommand = svgedit.history.MoveElementCommand;
var InsertElementCommand = svgedit.history.InsertElementCommand;
var RemoveElementCommand = svgedit.history.RemoveElementCommand;
var ChangeElementCommand = svgedit.history.ChangeElementCommand;
var BatchCommand = svgedit.history.BatchCommand;
// Implement the svgedit.history.HistoryEventHandler interface.
canvas.undoMgr = new svgedit.history.UndoManager({
	handleHistoryEvent: function(eventType, cmd) {
		var EventTypes = svgedit.history.HistoryEventTypes;
		// TODO: handle setBlurOffsets.
		if (eventType == EventTypes.BEFORE_UNAPPLY || eventType == EventTypes.BEFORE_APPLY) {
			canvas.clearSelection();
		} else if (eventType == EventTypes.AFTER_APPLY || eventType == EventTypes.AFTER_UNAPPLY) {
			var elems = cmd.elements();
			canvas.pathActions.clear();
			call("changed", elems);
			
			var cmdType = cmd.type();
			var isApply = (eventType == EventTypes.AFTER_APPLY);
			if (cmdType == MoveElementCommand.type()) {
				var parent = isApply ? cmd.newParent : cmd.oldParent;
				if (parent == svgcontent) {
					canvas.identifyLayers();
				}
			} else if (cmdType == InsertElementCommand.type() ||
					cmdType == RemoveElementCommand.type()) {
				if (cmd.parent == svgcontent) {
					canvas.identifyLayers();
				}
				if (cmdType == InsertElementCommand.type()) {
					if (isApply) restoreRefElems(cmd.elem);
				} else {
					if (!isApply) restoreRefElems(cmd.elem);
				}
				
				if(cmd.elem.tagName === 'use') {
					setUseData(cmd.elem);
				}
			} else if (cmdType == ChangeElementCommand.type()) {
				// if we are changing layer names, re-identify all layers
				if (cmd.elem.tagName == "title" && cmd.elem.parentNode.parentNode == svgcontent) {
					canvas.identifyLayers();
				}
				var values = isApply ? cmd.newValues : cmd.oldValues;
				// If stdDeviation was changed, update the blur.
				if (values["stdDeviation"]) {
					canvas.setBlurOffsets(cmd.elem.parentNode, values["stdDeviation"]);
				}
				
				// Remove & Re-add hack for Webkit (issue 775) 
				if(cmd.elem.tagName === 'use' && svgedit.browser.isWebkit()) {
					var elem = cmd.elem;
					if(!elem.getAttribute('x') && !elem.getAttribute('y')) {
						var parent = elem.parentNode;
						var sib = elem.nextSibling;
						parent.removeChild(elem);
						parent.insertBefore(elem, sib);
					}
				}
			}
		}
	}
});
var addCommandToHistory = function(cmd) {
	canvas.undoMgr.addCommandToHistory(cmd);
};

// import from select.js
svgedit.select.init(curConfig, {
	createSVGElement: function(jsonMap) { return canvas.addSvgElementFromJson(jsonMap); },
	svgRoot: function() { return svgroot; },
	svgContent: function() { return svgcontent; },
	currentZoom: function() { return current_zoom; },
	// TODO(codedread): Remove when getStrokedBBox() has been put into svgutils.js.
	getStrokedBBox: function(elems) { return canvas.getStrokedBBox([elems]); }
});
// this object manages selectors for us
var selectorManager = this.selectorManager = svgedit.select.getSelectorManager();

// Import from path.js
svgedit.path.init({
	getCurrentZoom: function() { return current_zoom; },
	getSVGRoot: function() { return svgroot; }
});

// Function: snapToGrid
// round value to for snapping
// NOTE: This function did not move to svgutils.js since it depends on curConfig.
svgedit.utilities.snapToGrid = function(value){
	var stepSize = curConfig.snappingStep;
	var unit = curConfig.baseUnit;
	if(unit !== "px") {
	stepSize *= svgedit.units.getTypeMap()[unit];
	}
	value = Math.round(value/stepSize)*stepSize;
	return value;
};
var snapToGrid = svgedit.utilities.snapToGrid;

// Interface strings, usually for title elements
var uiStrings = {
	"exportNoBlur": "Blurred elements will appear as un-blurred",
	"exportNoforeignObject": "foreignObject elements will not appear",
	"exportNoDashArray": "Strokes will appear filled",
	"exportNoText": "Text may not appear as expected"
};

var visElems = 'a,circle,ellipse,foreignObject,g,image,line,path,polygon,polyline,rect,svg,text,tspan,use';
var ref_attrs = ["clip-path", "fill", "filter", "marker-end", "marker-mid", "marker-start", "mask", "stroke"];

var elData = $.data;

// Animation element to change the opacity of any newly created element
var opac_ani = document.createElementNS(svgns, 'animate');
$(opac_ani).attr({
	attributeName: 'opacity',
	begin: 'indefinite',
	dur: 1,
	fill: 'freeze'
}).appendTo(svgroot);

var restoreRefElems = function(elem) {
	// Look for missing reference elements, restore any found
	var attrs = $(elem).attr(ref_attrs);
	for(var o in attrs) {
		var val = attrs[o];
		if (val && val.indexOf('url(') === 0) {
			var id = getUrlFromAttr(val).substr(1);
			var ref = getElem(id);
			if(!ref) {
				findDefs().appendChild(removedElements[id]);
				delete removedElements[id];
			}
		}
	}
	
	var childs = elem.getElementsByTagName('*');
	
	if(childs.length) {
		for(var i = 0, l = childs.length; i < l; i++) {
			restoreRefElems(childs[i]);
		}
	}
};

(function() {
	// TODO For Issue 208: this is a start on a thumbnail
	//	var svgthumb = svgdoc.createElementNS(svgns, "use");
	//	svgthumb.setAttribute('width', '100');
	//	svgthumb.setAttribute('height', '100');
	//	svgedit.utilities.setHref(svgthumb, '#svgcontent');
	//	svgroot.appendChild(svgthumb);

})();

// Object to contain image data for raster images that were found encodable
var encodableImages = {},
	
	// String with image URL of last loadable image
	last_good_img_url = curConfig.imgPath + 'logo.png',
	
	// Array with current disabled elements (for in-group editing)
	disabled_elems = [],
	
	// Object with save options
	save_options = {round_digits: 5},
	
	// Boolean indicating whether or not a draw action has been started
	started = false,
	
	// String with an element's initial transform attribute value
	start_transform = null,
	
	// String indicating the current editor mode
	current_mode = "select",
	
	// String with the current direction in which an element is being resized
	current_resize_mode = "none",
	
	// Object with IDs for imported files, to see if one was already added
	import_ids = {};

// Current text style properties
var cur_text = all_properties.text,
	
	// Current general properties
	cur_properties = cur_shape,
	
	// Array with selected elements' Bounding box object
//	selectedBBoxes = new Array(1),
	
	// The DOM element that was just selected
	justSelected = null,
	
	// DOM element for selection rectangle drawn by the user
	rubberBox = null,
	
	// Array of current BBoxes (still needed?)
	curBBoxes = [],
	
	// Object to contain all included extensions
	extensions = {},
	
	// Canvas point for the most recent right click
	lastClickPoint = null,
	
	// Map of deleted reference elements
	removedElements = {}

// Clipboard for cut, copy&pasted elements
canvas.clipBoard = [];

// Should this return an array by default, so extension results aren't overwritten?
var runExtensions = this.runExtensions = function(action, vars, returnArray) {
	var result = false;
	if(returnArray) result = [];
	$.each(extensions, function(name, opts) {
		if(action in opts) {
			if(returnArray) {
				result.push(opts[action](vars))
			} else {
				result = opts[action](vars);
			}
		}
	});
	return result;
}

// Function: addExtension
// Add an extension to the editor
// 
// Parameters:
// name - String with the ID of the extension
// ext_func - Function supplied by the extension with its data
this.addExtension = function(name, ext_func) {
	if(!(name in extensions)) {
		// Provide private vars/funcs here. Is there a better way to do this?
		
		if($.isFunction(ext_func)) {
		var ext = ext_func($.extend(canvas.getPrivateMethods(), {
			svgroot: svgroot,
			svgcontent: svgcontent,
			nonce: getCurrentDrawing().getNonce(),
			selectorManager: selectorManager
		}));
		} else {
			var ext = ext_func;
		}
		extensions[name] = ext;
		call("extension_added", ext);
	} else {
		console.log('Cannot add extension "' + name + '", an extension by that name already exists"');
	}
};
	
// This method rounds the incoming value to the nearest value based on the current_zoom
var round = this.round = function(val) {
	return parseInt(val*current_zoom)/current_zoom;
};

// This method sends back an array or a NodeList full of elements that
// intersect the multi-select rubber-band-box on the current_layer only.
// 
// Since the only browser that supports the SVG DOM getIntersectionList is Opera, 
// we need to provide an implementation here.  We brute-force it for now.
// 
// Reference:
// Firefox does not implement getIntersectionList(), see https://bugzilla.mozilla.org/show_bug.cgi?id=501421
// Webkit does not implement getIntersectionList(), see https://bugs.webkit.org/show_bug.cgi?id=11274
var getIntersectionList = this.getIntersectionList = function(rect) {
	if (rubberBox == null) { return null; }

	var parent = current_group || getCurrentDrawing().getCurrentLayer();
	
	if(!curBBoxes.length) {
		// Cache all bboxes
		curBBoxes = getVisibleElementsAndBBoxes(parent);
	}
	
	var resultList = null;
	try {
		resultList = parent.getIntersectionList(rect, null);
	} catch(e) { }

	if (resultList == null || typeof(resultList.item) != "function") {
		resultList = [];
		
		if(!rect) {
			var rubberBBox = rubberBox.getBBox();
			var bb = {};
			
			for(var o in rubberBBox) {
				bb[o] = rubberBBox[o] / current_zoom;
			}
			rubberBBox = bb;
			
		} else {
			var rubberBBox = rect;
		}
		var i = curBBoxes.length;
		while (i--) {
			if(!rubberBBox.width || !rubberBBox.width) continue;
			if (svgedit.math.rectsIntersect(rubberBBox, curBBoxes[i].bbox))  {
				resultList.push(curBBoxes[i].elem);
			}
		}
	}
	// addToSelection expects an array, but it's ok to pass a NodeList 
	// because using square-bracket notation is allowed: 
	// http://www.w3.org/TR/DOM-Level-2-Core/ecma-script-binding.html
	return resultList;
};

// TODO(codedread): Migrate this into svgutils.js
// Function: getStrokedBBox
// Get the bounding box for one or more stroked and/or transformed elements
// 
// Parameters:
// elems - Array with DOM elements to check
// 
// Returns:
// A single bounding box object
getStrokedBBox = this.getStrokedBBox = function(elems) {
	if(!elems) elems = getVisibleElements();
	if(!elems.length) return false;
	// Make sure the expected BBox is returned if the element is a group
	var getCheckedBBox = function(elem) {
	
		try {
			// TODO: Fix issue with rotated groups. Currently they work
			// fine in FF, but not in other browsers (same problem mentioned
			// in Issue 339 comment #2).
			
			var bb = svgedit.utilities.getBBox(elem);
			
			var angle = svgedit.utilities.getRotationAngle(elem);
			if ((angle && angle % 90) ||
			    svgedit.math.hasMatrixTransform(svgedit.transformlist.getTransformList(elem))) {
				// Accurate way to get BBox of rotated element in Firefox:
				// Put element in group and get its BBox
				
				var good_bb = false;
				
				// Get the BBox from the raw path for these elements
				var elemNames = ['ellipse','path','line','polyline','polygon'];
				if(elemNames.indexOf(elem.tagName) >= 0) {
					bb = good_bb = canvas.convertToPath(elem, true);
				} else if(elem.tagName == 'rect') {
					// Look for radius
					var rx = elem.getAttribute('rx');
					var ry = elem.getAttribute('ry');
					if(rx || ry) {
						bb = good_bb = canvas.convertToPath(elem, true);
					}
				}
				
				if(!good_bb) {
					// Must use clone else FF freaks out
					var clone = elem.cloneNode(true); 
					var g = document.createElementNS(svgns, "g");
					var parent = elem.parentNode;
					parent.appendChild(g);
					g.appendChild(clone);
					bb = svgedit.utilities.bboxToObj(g.getBBox());
					parent.removeChild(g);
				}
				

				// Old method: Works by giving the rotated BBox,
				// this is (unfortunately) what Opera and Safari do
				// natively when getting the BBox of the parent group
// 						var angle = angle * Math.PI / 180.0;
// 						var rminx = Number.MAX_VALUE, rminy = Number.MAX_VALUE, 
// 							rmaxx = Number.MIN_VALUE, rmaxy = Number.MIN_VALUE;
// 						var cx = round(bb.x + bb.width/2),
// 							cy = round(bb.y + bb.height/2);
// 						var pts = [ [bb.x - cx, bb.y - cy], 
// 									[bb.x + bb.width - cx, bb.y - cy],
// 									[bb.x + bb.width - cx, bb.y + bb.height - cy],
// 									[bb.x - cx, bb.y + bb.height - cy] ];
// 						var j = 4;
// 						while (j--) {
// 							var x = pts[j][0],
// 								y = pts[j][1],
// 								r = Math.sqrt( x*x + y*y );
// 							var theta = Math.atan2(y,x) + angle;
// 							x = round(r * Math.cos(theta) + cx);
// 							y = round(r * Math.sin(theta) + cy);
// 		
// 							// now set the bbox for the shape after it's been rotated
// 							if (x < rminx) rminx = x;
// 							if (y < rminy) rminy = y;
// 							if (x > rmaxx) rmaxx = x;
// 							if (y > rmaxy) rmaxy = y;
// 						}
// 						
// 						bb.x = rminx;
// 						bb.y = rminy;
// 						bb.width = rmaxx - rminx;
// 						bb.height = rmaxy - rminy;
			}
			return bb;
		} catch(e) { 
			console.log(elem, e);
			return null;
		} 
	};

	var full_bb;
	$.each(elems, function() {
		if(full_bb) return;
		if(!this.parentNode) return;
		full_bb = getCheckedBBox(this);
	});
	
	// This shouldn't ever happen...
	if(full_bb == null) return null;
	
	// full_bb doesn't include the stoke, so this does no good!
// 		if(elems.length == 1) return full_bb;
	
	var max_x = full_bb.x + full_bb.width;
	var max_y = full_bb.y + full_bb.height;
	var min_x = full_bb.x;
	var min_y = full_bb.y;
	
	// FIXME: same re-creation problem with this function as getCheckedBBox() above
	var getOffset = function(elem) {
		var sw = elem.getAttribute("stroke-width");
		var offset = 0;
		if (elem.getAttribute("stroke") != "none" && !isNaN(sw)) {
			offset += sw/2;
		}
		return offset;
	}
	var bboxes = [];
	$.each(elems, function(i, elem) {
		var cur_bb = getCheckedBBox(elem);
		if(cur_bb) {
			var offset = getOffset(elem);
			min_x = Math.min(min_x, cur_bb.x - offset);
			min_y = Math.min(min_y, cur_bb.y - offset);
			bboxes.push(cur_bb);
		}
	});
	
	full_bb.x = min_x;
	full_bb.y = min_y;
	
	$.each(elems, function(i, elem) {
		var cur_bb = bboxes[i];
		// ensure that elem is really an element node
		if (cur_bb && elem.nodeType == 1) {
			var offset = getOffset(elem);
			max_x = Math.max(max_x, cur_bb.x + cur_bb.width + offset);
			max_y = Math.max(max_y, cur_bb.y + cur_bb.height + offset);
		}
	});
	
	full_bb.width = max_x - min_x;
	full_bb.height = max_y - min_y;
	return full_bb;
}

// Function: getVisibleElements
// Get all elements that have a BBox (excludes <defs>, <title>, etc).
// Note that 0-opacity, off-screen etc elements are still considered "visible"
// for this function
//
// Parameters:
// parent - The parent DOM element to search within
//
// Returns:
// An array with all "visible" elements.
var getVisibleElements = this.getVisibleElements = function(parent) {
	if(!parent) parent = $(svgcontent).children(); // Prevent layers from being included
	
	var contentElems = [];
	$(parent).children().each(function(i, elem) {
		try {
			if (elem.getBBox()) {
				contentElems.push(elem);
			}
		} catch(e) {}
	});
	return contentElems.reverse();
};

// Function: getVisibleElementsAndBBoxes
// Get all elements that have a BBox (excludes <defs>, <title>, etc).
// Note that 0-opacity, off-screen etc elements are still considered "visible"
// for this function
//
// Parameters:
// parent - The parent DOM element to search within
//
// Returns:
// An array with objects that include:
// * elem - The element
// * bbox - The element's BBox as retrieved from getStrokedBBox
var getVisibleElementsAndBBoxes = this.getVisibleElementsAndBBoxes = function(parent) {
	if(!parent) parent = $(svgcontent).children(); // Prevent layers from being included
	
	var contentElems = [];
	$(parent).children().each(function(i, elem) {
		try {
			if (elem.getBBox()) {
				contentElems.push({'elem':elem, 'bbox':getStrokedBBox([elem])});
			}
		} catch(e) {}
	});
	return contentElems.reverse();
};

// Function: groupSvgElem
// Wrap an SVG element into a group element, mark the group as 'gsvg'
//
// Parameters:
// elem - SVG element to wrap
var groupSvgElem = this.groupSvgElem = function(elem) {
	var g = document.createElementNS(svgns, "g");
	elem.parentNode.replaceChild(g, elem);
	$(g).append(elem).data('gsvg', elem)[0].id = getNextId();
}

// Function: copyElem
// Create a clone of an element, updating its ID and its children's IDs when needed
//
// Parameters:
// el - DOM element to clone
//
// Returns: The cloned element
var copyElem = function(el) {
	// manually create a copy of the element
	var new_el = document.createElementNS(el.namespaceURI, el.nodeName);
	$.each(el.attributes, function(i, attr) {
		if (attr.localName != '-moz-math-font-style') {
			new_el.setAttributeNS(attr.namespaceURI, attr.nodeName, attr.nodeValue);
		}
	});
	// set the copied element's new id
	new_el.removeAttribute("id");
	new_el.id = getNextId();
	
	// Opera's "d" value needs to be reset for Opera/Win/non-EN
	// Also needed for webkit (else does not keep curved segments on clone)
	if(svgedit.browser.isWebkit() && el.nodeName == 'path') {
		var fixed_d = pathActions.convertPath(el);
		new_el.setAttribute('d', fixed_d);
	}

	// now create copies of all children
	$.each(el.childNodes, function(i, child) {
		switch(child.nodeType) {
			case 1: // element node
				new_el.appendChild(copyElem(child));
				break;
			case 3: // text node
				new_el.textContent = child.nodeValue;
				break;
			default:
				break;
		}
	});
	
	if($(el).data('gsvg')) {
		$(new_el).data('gsvg', new_el.firstChild);
	} else if($(el).data('symbol')) {
		var ref = $(el).data('symbol');
		$(new_el).data('ref', ref).data('symbol', ref);
	}
	
	else if(new_el.tagName == 'image') {
		preventClickDefault(new_el);
	}
	return new_el;
};

// Set scope for these functions
var getId, getNextId, call;

(function(c) {

	// Object to contain editor event names and callback functions
	var events = {};

	getId = c.getId = function() { return getCurrentDrawing().getId(); };
	getNextId = c.getNextId = function() { return getCurrentDrawing().getNextId(); };
	
	// Function: call
	// Run the callback function associated with the given event
	//
	// Parameters:
	// event - String with the event name
	// arg - Argument to pass through to the callback function
	call = c.call = function(event, arg) {
		if (events[event]) {
			return events[event](this, arg);
		}
	};
	
	// Function: bind
	// Attaches a callback function to an event
	//
	// Parameters:
	// event - String indicating the name of the event
	// f - The callback function to bind to the event
	// 
	// Return:
	// The previous event
	c.bind = function(event, f) {
	  var old = events[event];
		events[event] = f;
		return old;
	};
	
}(canvas));

// Function: canvas.prepareSvg
// Runs the SVG Document through the sanitizer and then updates its paths.
//
// Parameters:
// newDoc - The SVG DOM document
this.prepareSvg = function(newDoc) {
	this.sanitizeSvg(newDoc.documentElement);

	// convert paths into absolute commands
	var paths = newDoc.getElementsByTagNameNS(svgns, "path");
	for (var i = 0, len = paths.length; i < len; ++i) {
		var path = paths[i];
		path.setAttribute('d', pathActions.convertPath(path));
		pathActions.fixEnd(path);
	}
};

// Function getRefElem
// Get the reference element associated with the given attribute value
//
// Parameters:
// attrVal - The attribute value as a string
var getRefElem = this.getRefElem = function(attrVal) {
	return getElem(getUrlFromAttr(attrVal).substr(1));
}

// Function: ffClone
// Hack for Firefox bugs where text element features aren't updated or get 
// messed up. See issue 136 and issue 137.
// This function clones the element and re-selects it 
// TODO: Test for this bug on load and add it to "support" object instead of 
// browser sniffing
//
// Parameters: 
// elem - The (text) DOM element to clone
var ffClone = function(elem) {
	if(!svgedit.browser.isGecko()) return elem;
	var clone = elem.cloneNode(true)
	elem.parentNode.insertBefore(clone, elem);
	elem.parentNode.removeChild(elem);
	selectorManager.releaseSelector(elem);
	selectedElements[0] = clone;
	selectorManager.requestSelector(clone).showGrips(true);
	return clone;
}


// this.each is deprecated, if any extension used this it can be recreated by doing this:
// $(canvas.getRootElem()).children().each(...)

// this.each = function(cb) {
// 	$(svgroot).children().each(cb);
// };


// Function: setRotationAngle
// Removes any old rotations if present, prepends a new rotation at the
// transformed center
//
// Parameters:
// val - The new rotation angle in degrees
// preventUndo - Boolean indicating whether the action should be undoable or not
this.setRotationAngle = function(val, preventUndo) {
	// ensure val is the proper type
	val = parseFloat(val);
	var elem = selectedElements[0];
	var oldTransform = elem.getAttribute("transform");
	var bbox = svgedit.utilities.getBBox(elem);
	var cx = bbox.x+bbox.width/2, cy = bbox.y+bbox.height/2;
	var tlist = getTransformList(elem);
	
	// only remove the real rotational transform if present (i.e. at index=0)
	if (tlist.numberOfItems > 0) {
		var xform = tlist.getItem(0);
		if (xform.type == 4) {
			tlist.removeItem(0);
		}
	}
	// find R_nc and insert it
	if (val != 0) {
		var center = transformPoint(cx,cy,transformListToTransform(tlist).matrix);
		var R_nc = svgroot.createSVGTransform();
		R_nc.setRotate(val, center.x, center.y);
		if(tlist.numberOfItems) {
			tlist.insertItemBefore(R_nc, 0);
		} else {
			tlist.appendItem(R_nc);
		}
	}
	else if (tlist.numberOfItems == 0) {
		elem.removeAttribute("transform");
	}
	
	if (!preventUndo) {
		// we need to undo it, then redo it so it can be undo-able! :)
		// TODO: figure out how to make changes to transform list undo-able cross-browser?
		var newTransform = elem.getAttribute("transform");
		elem.setAttribute("transform", oldTransform);
		changeSelectedAttribute("transform",newTransform,selectedElements);
		call("changed", selectedElements);
	}
	var pointGripContainer = getElem("pathpointgrip_container");
// 		if(elem.nodeName == "path" && pointGripContainer) {
// 			pathActions.setPointContainerTransform(elem.getAttribute("transform"));
// 		}
	var selector = selectorManager.requestSelector(selectedElements[0]);
	selector.resize();
	selector.updateGripCursors(val);
};

// Function: recalculateAllSelectedDimensions
// Runs recalculateDimensions on the selected elements, 
// adding the changes to a single batch command
var recalculateAllSelectedDimensions = this.recalculateAllSelectedDimensions = function() {
	var text = (current_resize_mode == "none" ? "position" : "size");
	var batchCmd = new BatchCommand(text);

	var i = selectedElements.length;
	while(i--) {
		var elem = selectedElements[i];
// 			if(getRotationAngle(elem) && !hasMatrixTransform(getTransformList(elem))) continue;
		var cmd = recalculateDimensions(elem);
		if (cmd) {
			batchCmd.addSubCommand(cmd);
		}
	}

	if (!batchCmd.isEmpty()) {
		addCommandToHistory(batchCmd);
		call("changed", selectedElements);
	}
};

// this is how we map paths to our preferred relative segment types
var pathMap = [0, 'z', 'M', 'm', 'L', 'l', 'C', 'c', 'Q', 'q', 'A', 'a', 
					'H', 'h', 'V', 'v', 'S', 's', 'T', 't'];
					
// Debug tool to easily see the current matrix in the browser's console
var logMatrix = function(m) {
	console.log([m.a,m.b,m.c,m.d,m.e,m.f]);
};

// Function: remapElement
// Applies coordinate changes to an element based on the given matrix
//
// Parameters:
// selected - DOM element to be changed
// changes - Object with changes to be remapped
// m - Matrix object to use for remapping coordinates
var remapElement = this.remapElement = function(selected,changes,m) {

	var remap = function(x,y) { return transformPoint(x,y,m); },
		scalew = function(w) { return m.a*w; },
		scaleh = function(h) { return m.d*h; },
		doSnapping = curConfig.gridSnapping && selected.parentNode.parentNode.localName === "svg",
		finishUp = function() {
			if(doSnapping) for(var o in changes) changes[o] = snapToGrid(changes[o]);
			assignAttributes(selected, changes, 1000, true);
		}
		box = svgedit.utilities.getBBox(selected);
	
	for(var i = 0; i < 2; i++) {
		var type = i === 0 ? 'fill' : 'stroke';
		var attrVal = selected.getAttribute(type);
		if(attrVal && attrVal.indexOf('url(') === 0) {
			if(m.a < 0 || m.d < 0) {
				var grad = getRefElem(attrVal);
				var newgrad = grad.cloneNode(true);
	
				if(m.a < 0) {
					//flip x
					var x1 = newgrad.getAttribute('x1');
					var x2 = newgrad.getAttribute('x2');
					newgrad.setAttribute('x1', -(x1 - 1));
					newgrad.setAttribute('x2', -(x2 - 1));
				} 
				
				if(m.d < 0) {
					//flip y
					var y1 = newgrad.getAttribute('y1');
					var y2 = newgrad.getAttribute('y2');
					newgrad.setAttribute('y1', -(y1 - 1));
					newgrad.setAttribute('y2', -(y2 - 1));
				}
				newgrad.id = getNextId();
				findDefs().appendChild(newgrad);
				selected.setAttribute(type, 'url(#' + newgrad.id + ')');
			}
			
			// Not really working :(
// 			if(selected.tagName === 'path') {
// 				reorientGrads(selected, m);
// 			}
		}
	}


	var elName = selected.tagName;
	if(elName === "g" || elName === "text" || elName === "use") {
		// if it was a translate, then just update x,y
		if (m.a == 1 && m.b == 0 && m.c == 0 && m.d == 1 && 
			(m.e != 0 || m.f != 0) ) 
		{
			// [T][M] = [M][T']
			// therefore [T'] = [M_inv][T][M]
			var existing = transformListToTransform(selected).matrix,
				t_new = matrixMultiply(existing.inverse(), m, existing);
			changes.x = parseFloat(changes.x) + t_new.e;
			changes.y = parseFloat(changes.y) + t_new.f;
		}
		else {
			// we just absorb all matrices into the element and don't do any remapping
			var chlist = getTransformList(selected);
			var mt = svgroot.createSVGTransform();
			mt.setMatrix(matrixMultiply(transformListToTransform(chlist).matrix,m));
			chlist.clear();
			chlist.appendItem(mt);
		}
	}
	
	// now we have a set of changes and an applied reduced transform list
	// we apply the changes directly to the DOM
	switch (elName)
	{
		case "foreignObject":
		case "rect":
		case "image":
			
			// Allow images to be inverted (give them matrix when flipped)
			if(elName === 'image' && (m.a < 0 || m.d < 0)) {
				// Convert to matrix
				var chlist = getTransformList(selected);
				var mt = svgroot.createSVGTransform();
				mt.setMatrix(matrixMultiply(transformListToTransform(chlist).matrix,m));
				chlist.clear();
				chlist.appendItem(mt);
			} else {
				var pt1 = remap(changes.x,changes.y);
				
				changes.width = scalew(changes.width);
				changes.height = scaleh(changes.height);
				
				changes.x = pt1.x + Math.min(0,changes.width);
				changes.y = pt1.y + Math.min(0,changes.height);
				changes.width = Math.abs(changes.width);
				changes.height = Math.abs(changes.height);
			}
			finishUp();
			break;
		case "ellipse":
			var c = remap(changes.cx,changes.cy);
			changes.cx = c.x;
			changes.cy = c.y;
			changes.rx = scalew(changes.rx);
			changes.ry = scaleh(changes.ry);
		
			changes.rx = Math.abs(changes.rx);
			changes.ry = Math.abs(changes.ry);
			finishUp();
			break;
		case "circle":
			var c = remap(changes.cx,changes.cy);
			changes.cx = c.x;
			changes.cy = c.y;
			// take the minimum of the new selected box's dimensions for the new circle radius
			var tbox = svgedit.math.transformBox(box.x, box.y, box.width, box.height, m);
			var w = tbox.tr.x - tbox.tl.x, h = tbox.bl.y - tbox.tl.y;
			changes.r = Math.min(w/2, h/2);

			if(changes.r) changes.r = Math.abs(changes.r);
			finishUp();
			break;
		case "line":
			var pt1 = remap(changes.x1,changes.y1),
				pt2 = remap(changes.x2,changes.y2);
			changes.x1 = pt1.x;
			changes.y1 = pt1.y;
			changes.x2 = pt2.x;
			changes.y2 = pt2.y;
			
		case "text":
		case "use":
			finishUp();
			break;
		case "g":
			var gsvg = $(selected).data('gsvg');
			if(gsvg) {
				assignAttributes(gsvg, changes, 1000, true);
			}
			break;
		case "polyline":
		case "polygon":
			var len = changes.points.length;
			for (var i = 0; i < len; ++i) {
				var pt = changes.points[i];
				pt = remap(pt.x,pt.y);
				changes.points[i].x = pt.x;
				changes.points[i].y = pt.y;
			}

			var len = changes.points.length;
			var pstr = "";
			for (var i = 0; i < len; ++i) {
				var pt = changes.points[i];
				pstr += pt.x + "," + pt.y + " ";
			}
			selected.setAttribute("points", pstr);
			break;
		case "path":
		
			var segList = selected.pathSegList;
			var len = segList.numberOfItems;
			changes.d = new Array(len);
			for (var i = 0; i < len; ++i) {
				var seg = segList.getItem(i);
				changes.d[i] = {
					type: seg.pathSegType,
					x: seg.x,
					y: seg.y,
					x1: seg.x1,
					y1: seg.y1,
					x2: seg.x2,
					y2: seg.y2,
					r1: seg.r1,
					r2: seg.r2,
					angle: seg.angle,
					largeArcFlag: seg.largeArcFlag,
					sweepFlag: seg.sweepFlag
				};
			}
			
			var len = changes.d.length,
				firstseg = changes.d[0],
				currentpt = remap(firstseg.x,firstseg.y);
			changes.d[0].x = currentpt.x;
			changes.d[0].y = currentpt.y;
			for (var i = 1; i < len; ++i) {
				var seg = changes.d[i];
				var type = seg.type;
				// if absolute or first segment, we want to remap x, y, x1, y1, x2, y2
				// if relative, we want to scalew, scaleh
				if (type % 2 == 0) { // absolute
					var thisx = (seg.x != undefined) ? seg.x : currentpt.x, // for V commands
						thisy = (seg.y != undefined) ? seg.y : currentpt.y, // for H commands
						pt = remap(thisx,thisy),
						pt1 = remap(seg.x1,seg.y1),
						pt2 = remap(seg.x2,seg.y2);
					seg.x = pt.x;
					seg.y = pt.y;
					seg.x1 = pt1.x;
					seg.y1 = pt1.y;
					seg.x2 = pt2.x;
					seg.y2 = pt2.y;
					seg.r1 = scalew(seg.r1),
					seg.r2 = scaleh(seg.r2);
				}
				else { // relative
					seg.x = scalew(seg.x);
					seg.y = scaleh(seg.y);
					seg.x1 = scalew(seg.x1);
					seg.y1 = scaleh(seg.y1);
					seg.x2 = scalew(seg.x2);
					seg.y2 = scaleh(seg.y2);
					seg.r1 = scalew(seg.r1),
					seg.r2 = scaleh(seg.r2);
				}
			} // for each segment
		
			var dstr = "";
			var len = changes.d.length;
			for (var i = 0; i < len; ++i) {
				var seg = changes.d[i];
				var type = seg.type;
				dstr += pathMap[type];
				switch(type) {
					case 13: // relative horizontal line (h)
					case 12: // absolute horizontal line (H)
						dstr += seg.x + " ";
						break;
					case 15: // relative vertical line (v)
					case 14: // absolute vertical line (V)
						dstr += seg.y + " ";
						break;
					case 3: // relative move (m)
					case 5: // relative line (l)
					case 19: // relative smooth quad (t)
					case 2: // absolute move (M)
					case 4: // absolute line (L)
					case 18: // absolute smooth quad (T)
						dstr += seg.x + "," + seg.y + " ";
						break;
					case 7: // relative cubic (c)
					case 6: // absolute cubic (C)
						dstr += seg.x1 + "," + seg.y1 + " " + seg.x2 + "," + seg.y2 + " " +
							 seg.x + "," + seg.y + " ";
						break;
					case 9: // relative quad (q) 
					case 8: // absolute quad (Q)
						dstr += seg.x1 + "," + seg.y1 + " " + seg.x + "," + seg.y + " ";
						break;
					case 11: // relative elliptical arc (a)
					case 10: // absolute elliptical arc (A)
						dstr += seg.r1 + "," + seg.r2 + " " + seg.angle + " " + (+seg.largeArcFlag) +
							" " + (+seg.sweepFlag) + " " + seg.x + "," + seg.y + " ";
						break;
					case 17: // relative smooth cubic (s)
					case 16: // absolute smooth cubic (S)
						dstr += seg.x2 + "," + seg.y2 + " " + seg.x + "," + seg.y + " ";
						break;
				}
			}

			selected.setAttribute("d", dstr);
			break;
	}
};

// Function: updateClipPath
// Updates a <clipPath>s values based on the given translation of an element
//
// Parameters:
// attr - The clip-path attribute value with the clipPath's ID
// tx - The translation's x value
// ty - The translation's y value
var updateClipPath = function(attr, tx, ty) {
	var path = getRefElem(attr).firstChild;
	
	var cp_xform = getTransformList(path);
	
	var newxlate = svgroot.createSVGTransform();
	newxlate.setTranslate(tx, ty);

	cp_xform.appendItem(newxlate);
	
	// Update clipPath's dimensions
	recalculateDimensions(path);
}

// Function: recalculateDimensions
// Decides the course of action based on the element's transform list
//
// Parameters:
// selected - The DOM element to recalculate
//
// Returns: 
// Undo command object with the resulting change
var recalculateDimensions = this.recalculateDimensions = function(selected) {
	if (selected == null) return null;
	
	var tlist = getTransformList(selected);
	
	// remove any unnecessary transforms
	if (tlist && tlist.numberOfItems > 0) {
		var k = tlist.numberOfItems;
		while (k--) {
			var xform = tlist.getItem(k);
			if (xform.type === 0) {
				tlist.removeItem(k);
			}
			// remove identity matrices
			else if (xform.type === 1) {
				if (svgedit.math.isIdentity(xform.matrix)) {
					tlist.removeItem(k);
				}
			}
			// remove zero-degree rotations
			else if (xform.type === 4) {
				if (xform.angle === 0) {
					tlist.removeItem(k);
				}
			}
		}
		// End here if all it has is a rotation
		if(tlist.numberOfItems === 1 && getRotationAngle(selected)) return null;
	}
	
	// if this element had no transforms, we are done
	if (!tlist || tlist.numberOfItems == 0) {
		selected.removeAttribute("transform");
		return null;
	}
	
	// TODO: Make this work for more than 2
	if (tlist) {
		var k = tlist.numberOfItems;
		var mxs = [];
		while (k--) {
			var xform = tlist.getItem(k);
			if (xform.type === 1) {
				mxs.push([xform.matrix, k]);
			} else if(mxs.length) {
				mxs = [];
			}
		}
		if(mxs.length === 2) {
			var m_new = svgroot.createSVGTransformFromMatrix(matrixMultiply(mxs[1][0], mxs[0][0]));
			tlist.removeItem(mxs[0][1]);
			tlist.removeItem(mxs[1][1]);
			tlist.insertItemBefore(m_new, mxs[1][1]);
		}
		
		// combine matrix + translate
		k = tlist.numberOfItems;
		if(k >= 2 && tlist.getItem(k-2).type === 1 && tlist.getItem(k-1).type === 2) {
			var mt = svgroot.createSVGTransform();
			
			var m = matrixMultiply(
				tlist.getItem(k-2).matrix, 
				tlist.getItem(k-1).matrix
			);		
			mt.setMatrix(m);
			tlist.removeItem(k-2);
			tlist.removeItem(k-2);
			tlist.appendItem(mt);
		}
	}
	
	// If it still has a single [M] or [R][M], return null too (prevents BatchCommand from being returned).
	switch ( selected.tagName ) {
		// Ignore these elements, as they can absorb the [M]
		case 'line':
		case 'polyline':
		case 'polygon':
		case 'path':
			break;
		default:
			if(
				(tlist.numberOfItems === 1 && tlist.getItem(0).type === 1)
				||  (tlist.numberOfItems === 2 && tlist.getItem(0).type === 1 && tlist.getItem(0).type === 4)
			) {
				return null;
			}
	}
	
	// Grouped SVG element 
	var gsvg = $(selected).data('gsvg');
	
	// we know we have some transforms, so set up return variable		
	var batchCmd = new BatchCommand("Transform");
	
	// store initial values that will be affected by reducing the transform list
	var changes = {}, initial = null, attrs = [];
	switch (selected.tagName)
	{
		case "line":
			attrs = ["x1", "y1", "x2", "y2"];
			break;
		case "circle":
			attrs = ["cx", "cy", "r"];
			break;
		case "ellipse":
			attrs = ["cx", "cy", "rx", "ry"];
			break;
		case "foreignObject":
		case "rect":
		case "image":
			attrs = ["width", "height", "x", "y"];
			break;
		case "use":
		case "text":
			attrs = ["x", "y"];
			break;
		case "polygon":
		case "polyline":
			initial = {};
			initial["points"] = selected.getAttribute("points");
			var list = selected.points;
			var len = list.numberOfItems;
			changes["points"] = new Array(len);
			for (var i = 0; i < len; ++i) {
				var pt = list.getItem(i);
				changes["points"][i] = {x:pt.x,y:pt.y};
			}
			break;
		case "path":
			initial = {};
			initial["d"] = selected.getAttribute("d");
			changes["d"] = selected.getAttribute("d");
			break;
	} // switch on element type to get initial values
	
	if(attrs.length) {
		changes = $(selected).attr(attrs);
		$.each(changes, function(attr, val) {
			changes[attr] = convertToNum(attr, val);
		});
	} else if(gsvg) {
		// GSVG exception
		changes = {
			x: $(gsvg).attr('x') || 0,
			y: $(gsvg).attr('y') || 0
		};
	}
	
	// if we haven't created an initial array in polygon/polyline/path, then 
	// make a copy of initial values and include the transform
	if (initial == null) {
		initial = $.extend(true, {}, changes);
		$.each(initial, function(attr, val) {
			initial[attr] = convertToNum(attr, val);
		});
	}
	// save the start transform value too
	initial["transform"] = start_transform ? start_transform : "";
	
	// if it's a regular group, we have special processing to flatten transforms
	if ((selected.tagName == "g" && !gsvg) || selected.tagName == "a") {
		var box = svgedit.utilities.getBBox(selected),
			oldcenter = {x: box.x+box.width/2, y: box.y+box.height/2},
			newcenter = transformPoint(box.x+box.width/2, box.y+box.height/2,
							transformListToTransform(tlist).matrix),
			m = svgroot.createSVGMatrix();
		
		
		// temporarily strip off the rotate and save the old center
		var gangle = getRotationAngle(selected);
		if (gangle) {
			var a = gangle * Math.PI / 180;
			if ( Math.abs(a) > (1.0e-10) ) {
				var s = Math.sin(a)/(1 - Math.cos(a));
			} else {
				// FIXME: This blows up if the angle is exactly 0!
				var s = 2/a;
			}
			for (var i = 0; i < tlist.numberOfItems; ++i) {
				var xform = tlist.getItem(i);
				if (xform.type == 4) {
					// extract old center through mystical arts
					var rm = xform.matrix;
					oldcenter.y = (s*rm.e + rm.f)/2;
					oldcenter.x = (rm.e - s*rm.f)/2;
					tlist.removeItem(i);
					break;
				}
			}
		}
		var tx = 0, ty = 0,
			operation = 0,
			N = tlist.numberOfItems;

		if(N) {
			var first_m = tlist.getItem(0).matrix;
		}

		// first, if it was a scale then the second-last transform will be it
		if (N >= 3 && tlist.getItem(N-2).type == 3 && 
			tlist.getItem(N-3).type == 2 && tlist.getItem(N-1).type == 2) 
		{
			operation = 3; // scale
		
			// if the children are unrotated, pass the scale down directly
			// otherwise pass the equivalent matrix() down directly
			var tm = tlist.getItem(N-3).matrix,
				sm = tlist.getItem(N-2).matrix,
				tmn = tlist.getItem(N-1).matrix;
		
			var children = selected.childNodes;
			var c = children.length;
			while (c--) {
				var child = children.item(c);
				tx = 0;
				ty = 0;
				if (child.nodeType == 1) {
					var childTlist = getTransformList(child);

					// some children might not have a transform (<metadata>, <defs>, etc)
					if (!childTlist) continue;

					var m = transformListToTransform(childTlist).matrix;

					// Convert a matrix to a scale if applicable
// 					if(hasMatrixTransform(childTlist) && childTlist.numberOfItems == 1) {
// 						if(m.b==0 && m.c==0 && m.e==0 && m.f==0) {
// 							childTlist.removeItem(0);
// 							var translateOrigin = svgroot.createSVGTransform(),
// 								scale = svgroot.createSVGTransform(),
// 								translateBack = svgroot.createSVGTransform();
// 							translateOrigin.setTranslate(0, 0);
// 							scale.setScale(m.a, m.d);
// 							translateBack.setTranslate(0, 0);
// 							childTlist.appendItem(translateBack);
// 							childTlist.appendItem(scale);
// 							childTlist.appendItem(translateOrigin);
// 						}
// 					}
				
					var angle = getRotationAngle(child);
					var old_start_transform = start_transform;
					var childxforms = [];
					start_transform = child.getAttribute("transform");
					if(angle || hasMatrixTransform(childTlist)) {
						var e2t = svgroot.createSVGTransform();
						e2t.setMatrix(matrixMultiply(tm, sm, tmn, m));
						childTlist.clear();
						childTlist.appendItem(e2t);
						childxforms.push(e2t);
					}
					// if not rotated or skewed, push the [T][S][-T] down to the child
					else {
						// update the transform list with translate,scale,translate
						
						// slide the [T][S][-T] from the front to the back
						// [T][S][-T][M] = [M][T2][S2][-T2]
						
						// (only bringing [-T] to the right of [M])
						// [T][S][-T][M] = [T][S][M][-T2]
						// [-T2] = [M_inv][-T][M]
						var t2n = matrixMultiply(m.inverse(), tmn, m);
						// [T2] is always negative translation of [-T2]
						var t2 = svgroot.createSVGMatrix();
						t2.e = -t2n.e;
						t2.f = -t2n.f;
						
						// [T][S][-T][M] = [M][T2][S2][-T2]
						// [S2] = [T2_inv][M_inv][T][S][-T][M][-T2_inv]
						var s2 = matrixMultiply(t2.inverse(), m.inverse(), tm, sm, tmn, m, t2n.inverse());

						var translateOrigin = svgroot.createSVGTransform(),
							scale = svgroot.createSVGTransform(),
							translateBack = svgroot.createSVGTransform();
						translateOrigin.setTranslate(t2n.e, t2n.f);
						scale.setScale(s2.a, s2.d);
						translateBack.setTranslate(t2.e, t2.f);
						childTlist.appendItem(translateBack);
						childTlist.appendItem(scale);
						childTlist.appendItem(translateOrigin);
						childxforms.push(translateBack);
						childxforms.push(scale);
						childxforms.push(translateOrigin);
// 						logMatrix(translateBack.matrix);
// 						logMatrix(scale.matrix);
					} // not rotated
					batchCmd.addSubCommand( recalculateDimensions(child) );
					// TODO: If any <use> have this group as a parent and are 
					// referencing this child, then we need to impose a reverse 
					// scale on it so that when it won't get double-translated
//						var uses = selected.getElementsByTagNameNS(svgns, "use");
//						var href = "#"+child.id;
//						var u = uses.length;
//						while (u--) {
//							var useElem = uses.item(u);
//							if(href == getHref(useElem)) {
//								var usexlate = svgroot.createSVGTransform();
//								usexlate.setTranslate(-tx,-ty);
//								getTransformList(useElem).insertItemBefore(usexlate,0);
//								batchCmd.addSubCommand( recalculateDimensions(useElem) );
//							}
//						}
					start_transform = old_start_transform;
				} // element
			} // for each child
			// Remove these transforms from group
			tlist.removeItem(N-1);
			tlist.removeItem(N-2);
			tlist.removeItem(N-3);
		}
		else if (N >= 3 && tlist.getItem(N-1).type == 1)
		{
			operation = 3; // scale
			m = transformListToTransform(tlist).matrix;
			var e2t = svgroot.createSVGTransform();
			e2t.setMatrix(m);
			tlist.clear();
			tlist.appendItem(e2t);
		}			
		// next, check if the first transform was a translate 
		// if we had [ T1 ] [ M ] we want to transform this into [ M ] [ T2 ]
		// therefore [ T2 ] = [ M_inv ] [ T1 ] [ M ]
		else if ( (N == 1 || (N > 1 && tlist.getItem(1).type != 3)) && 
			tlist.getItem(0).type == 2) 
		{
			operation = 2; // translate
			var T_M = transformListToTransform(tlist).matrix;
			tlist.removeItem(0);
			var M_inv = transformListToTransform(tlist).matrix.inverse();
			var M2 = matrixMultiply( M_inv, T_M );
			
			tx = M2.e;
			ty = M2.f;

			if (tx != 0 || ty != 0) {
				// we pass the translates down to the individual children
				var children = selected.childNodes;
				var c = children.length;
				
				var clipPaths_done = [];
				
				while (c--) {
					var child = children.item(c);
					if (child.nodeType == 1) {
					
						// Check if child has clip-path
						if(child.getAttribute('clip-path')) {
							// tx, ty
							var attr = child.getAttribute('clip-path');
							if(clipPaths_done.indexOf(attr) === -1) {
								updateClipPath(attr, tx, ty);
								clipPaths_done.push(attr);
							}							
						}

						var old_start_transform = start_transform;
						start_transform = child.getAttribute("transform");
						
						var childTlist = getTransformList(child);
						// some children might not have a transform (<metadata>, <defs>, etc)
						if (childTlist) {
							var newxlate = svgroot.createSVGTransform();
							newxlate.setTranslate(tx,ty);
							if(childTlist.numberOfItems) {
								childTlist.insertItemBefore(newxlate, 0);
							} else {
								childTlist.appendItem(newxlate);
							}
							batchCmd.addSubCommand( recalculateDimensions(child) );
							// If any <use> have this group as a parent and are 
							// referencing this child, then impose a reverse translate on it
							// so that when it won't get double-translated
							var uses = selected.getElementsByTagNameNS(svgns, "use");
							var href = "#"+child.id;
							var u = uses.length;
							while (u--) {
								var useElem = uses.item(u);
								if(href == getHref(useElem)) {
									var usexlate = svgroot.createSVGTransform();
									usexlate.setTranslate(-tx,-ty);
									getTransformList(useElem).insertItemBefore(usexlate,0);
									batchCmd.addSubCommand( recalculateDimensions(useElem) );
								}
							}
							start_transform = old_start_transform;
						}
					}
				}
				
				clipPaths_done = [];
				
				start_transform = old_start_transform;
			}
		}
		// else, a matrix imposition from a parent group
		// keep pushing it down to the children
		else if (N == 1 && tlist.getItem(0).type == 1 && !gangle) {
			operation = 1;
			var m = tlist.getItem(0).matrix,
				children = selected.childNodes,
				c = children.length;
			while (c--) {
				var child = children.item(c);
				if (child.nodeType == 1) {
					var old_start_transform = start_transform;
					start_transform = child.getAttribute("transform");
					var childTlist = getTransformList(child);
					
					if (!childTlist) continue;
					
					var em = matrixMultiply(m, transformListToTransform(childTlist).matrix);
					var e2m = svgroot.createSVGTransform();
					e2m.setMatrix(em);
					childTlist.clear();
					childTlist.appendItem(e2m,0);
					
					batchCmd.addSubCommand( recalculateDimensions(child) );
					start_transform = old_start_transform;
					
					// Convert stroke
					// TODO: Find out if this should actually happen somewhere else
					var sw = child.getAttribute("stroke-width");
					if (child.getAttribute("stroke") !== "none" && !isNaN(sw)) {
						var avg = (Math.abs(em.a) + Math.abs(em.d)) / 2;
						child.setAttribute('stroke-width', sw * avg);
					}

				}
			}
			tlist.clear();
		}
		// else it was just a rotate
		else {
			if (gangle) {
				var newRot = svgroot.createSVGTransform();
				newRot.setRotate(gangle,newcenter.x,newcenter.y);
				if(tlist.numberOfItems) {
					tlist.insertItemBefore(newRot, 0);
				} else {
					tlist.appendItem(newRot);
				}
			}
			if (tlist.numberOfItems == 0) {
				selected.removeAttribute("transform");
			}
			return null;			
		}
		
		// if it was a translate, put back the rotate at the new center
		if (operation == 2) {
			if (gangle) {
				newcenter = {
					x: oldcenter.x + first_m.e,
					y: oldcenter.y + first_m.f
				};
			
				var newRot = svgroot.createSVGTransform();
				newRot.setRotate(gangle,newcenter.x,newcenter.y);
				if(tlist.numberOfItems) {
					tlist.insertItemBefore(newRot, 0);
				} else {
					tlist.appendItem(newRot);
				}
			}
		}
		// if it was a resize
		else if (operation == 3) {
			var m = transformListToTransform(tlist).matrix;
			var roldt = svgroot.createSVGTransform();
			roldt.setRotate(gangle, oldcenter.x, oldcenter.y);
			var rold = roldt.matrix;
			var rnew = svgroot.createSVGTransform();
			rnew.setRotate(gangle, newcenter.x, newcenter.y);
			var rnew_inv = rnew.matrix.inverse(),
				m_inv = m.inverse(),
				extrat = matrixMultiply(m_inv, rnew_inv, rold, m);

			tx = extrat.e;
			ty = extrat.f;

			if (tx != 0 || ty != 0) {
				// now push this transform down to the children
				// we pass the translates down to the individual children
				var children = selected.childNodes;
				var c = children.length;
				while (c--) {
					var child = children.item(c);
					if (child.nodeType == 1) {
						var old_start_transform = start_transform;
						start_transform = child.getAttribute("transform");
						var childTlist = getTransformList(child);
						var newxlate = svgroot.createSVGTransform();
						newxlate.setTranslate(tx,ty);
						if(childTlist.numberOfItems) {
							childTlist.insertItemBefore(newxlate, 0);
						} else {
							childTlist.appendItem(newxlate);
						}

						batchCmd.addSubCommand( recalculateDimensions(child) );
						start_transform = old_start_transform;
					}
				}
			}
			
			if (gangle) {
				if(tlist.numberOfItems) {
					tlist.insertItemBefore(rnew, 0);
				} else {
					tlist.appendItem(rnew);
				}
			}
		}
	}
	// else, it's a non-group
	else {

		// FIXME: box might be null for some elements (<metadata> etc), need to handle this
		var box = svgedit.utilities.getBBox(selected);

		// Paths (and possbly other shapes) will have no BBox while still in <defs>,
		// but we still may need to recalculate them (see issue 595).
		// TODO: Figure out how to get BBox from these elements in case they
		// have a rotation transform
		
		if(!box && selected.tagName != 'path') return null;
		

		var m = svgroot.createSVGMatrix(),
			// temporarily strip off the rotate and save the old center
			angle = getRotationAngle(selected);
		if (angle) {
			var oldcenter = {x: box.x+box.width/2, y: box.y+box.height/2},
			newcenter = transformPoint(box.x+box.width/2, box.y+box.height/2,
							transformListToTransform(tlist).matrix);
		
			var a = angle * Math.PI / 180;
			if ( Math.abs(a) > (1.0e-10) ) {
				var s = Math.sin(a)/(1 - Math.cos(a));
			} else {
				// FIXME: This blows up if the angle is exactly 0!
				var s = 2/a;
			}
			for (var i = 0; i < tlist.numberOfItems; ++i) {
				var xform = tlist.getItem(i);
				if (xform.type == 4) {
					// extract old center through mystical arts
					var rm = xform.matrix;
					oldcenter.y = (s*rm.e + rm.f)/2;
					oldcenter.x = (rm.e - s*rm.f)/2;
					tlist.removeItem(i);
					break;
				}
			}
		}
		
		// 2 = translate, 3 = scale, 4 = rotate, 1 = matrix imposition
		var operation = 0;
		var N = tlist.numberOfItems;
		
		// Check if it has a gradient with userSpaceOnUse, in which case
		// adjust it by recalculating the matrix transform.
		// TODO: Make this work in Webkit using svgedit.transformlist.SVGTransformList
		if(!svgedit.browser.isWebkit()) {
			var fill = selected.getAttribute('fill');
			if(fill && fill.indexOf('url(') === 0) {
				var paint = getRefElem(fill);
				var type = 'pattern';
				if(paint.tagName !== type) type = 'gradient';
				var attrVal = paint.getAttribute(type + 'Units');
				if(attrVal === 'userSpaceOnUse') {
					//Update the userSpaceOnUse element
					m = transformListToTransform(tlist).matrix;
					var gtlist = getTransformList(paint);
					var gmatrix = transformListToTransform(gtlist).matrix;
					m = matrixMultiply(m, gmatrix);
					var m_str = "matrix(" + [m.a,m.b,m.c,m.d,m.e,m.f].join(",") + ")";
					paint.setAttribute(type + 'Transform', m_str);
				}
			}
		}

		// first, if it was a scale of a non-skewed element, then the second-last  
		// transform will be the [S]
		// if we had [M][T][S][T] we want to extract the matrix equivalent of
		// [T][S][T] and push it down to the element
		if (N >= 3 && tlist.getItem(N-2).type == 3 && 
			tlist.getItem(N-3).type == 2 && tlist.getItem(N-1).type == 2) 
			
			// Removed this so a <use> with a given [T][S][T] would convert to a matrix. 
			// Is that bad?
			//  && selected.nodeName != "use"
		{
			operation = 3; // scale
			m = transformListToTransform(tlist,N-3,N-1).matrix;
			tlist.removeItem(N-1);
			tlist.removeItem(N-2);
			tlist.removeItem(N-3);
		} // if we had [T][S][-T][M], then this was a skewed element being resized
		// Thus, we simply combine it all into one matrix
		else if(N == 4 && tlist.getItem(N-1).type == 1) {
			operation = 3; // scale
			m = transformListToTransform(tlist).matrix;
			var e2t = svgroot.createSVGTransform();
			e2t.setMatrix(m);
			tlist.clear();
			tlist.appendItem(e2t);
			// reset the matrix so that the element is not re-mapped
			m = svgroot.createSVGMatrix();
		} // if we had [R][T][S][-T][M], then this was a rotated matrix-element  
		// if we had [T1][M] we want to transform this into [M][T2]
		// therefore [ T2 ] = [ M_inv ] [ T1 ] [ M ] and we can push [T2] 
		// down to the element
		else if ( (N == 1 || (N > 1 && tlist.getItem(1).type != 3)) && 
			tlist.getItem(0).type == 2) 
		{
			operation = 2; // translate
			var oldxlate = tlist.getItem(0).matrix,
				meq = transformListToTransform(tlist,1).matrix,
				meq_inv = meq.inverse();
			m = matrixMultiply( meq_inv, oldxlate, meq );
			tlist.removeItem(0);
		}
		// else if this child now has a matrix imposition (from a parent group)
		// we might be able to simplify
		else if (N == 1 && tlist.getItem(0).type == 1 && !angle) {
			// Remap all point-based elements
			m = transformListToTransform(tlist).matrix;
			switch (selected.tagName) {
				case 'line':
					changes = $(selected).attr(["x1","y1","x2","y2"]);
				case 'polyline':
				case 'polygon':
					changes.points = selected.getAttribute("points");
					if(changes.points) {
						var list = selected.points;
						var len = list.numberOfItems;
						changes.points = new Array(len);
						for (var i = 0; i < len; ++i) {
							var pt = list.getItem(i);
							changes.points[i] = {x:pt.x,y:pt.y};
						}
					}
				case 'path':
					changes.d = selected.getAttribute("d");
					operation = 1;
					tlist.clear();
					break;
				default:
					break;
			}
		}
		// if it was a rotation, put the rotate back and return without a command
		// (this function has zero work to do for a rotate())
		else {
			operation = 4; // rotation
			if (angle) {
				var newRot = svgroot.createSVGTransform();
				newRot.setRotate(angle,newcenter.x,newcenter.y);
				
				if(tlist.numberOfItems) {
					tlist.insertItemBefore(newRot, 0);
				} else {
					tlist.appendItem(newRot);
				}
			}
			if (tlist.numberOfItems == 0) {
				selected.removeAttribute("transform");
			}
			return null;
		}
		
		// if it was a translate or resize, we need to remap the element and absorb the xform
		if (operation == 1 || operation == 2 || operation == 3) {
			remapElement(selected,changes,m);
		} // if we are remapping
		
		// if it was a translate, put back the rotate at the new center
		if (operation == 2) {
			if (angle) {
				if(!hasMatrixTransform(tlist)) {
					newcenter = {
						x: oldcenter.x + m.e,
						y: oldcenter.y + m.f
					};
				}
				var newRot = svgroot.createSVGTransform();
				newRot.setRotate(angle, newcenter.x, newcenter.y);
				if(tlist.numberOfItems) {
					tlist.insertItemBefore(newRot, 0);
				} else {
					tlist.appendItem(newRot);
				}
			}
		}
		// [Rold][M][T][S][-T] became [Rold][M]
		// we want it to be [Rnew][M][Tr] where Tr is the
		// translation required to re-center it
		// Therefore, [Tr] = [M_inv][Rnew_inv][Rold][M]
		else if (operation == 3 && angle) {
			var m = transformListToTransform(tlist).matrix;
			var roldt = svgroot.createSVGTransform();
			roldt.setRotate(angle, oldcenter.x, oldcenter.y);
			var rold = roldt.matrix;
			var rnew = svgroot.createSVGTransform();
			rnew.setRotate(angle, newcenter.x, newcenter.y);
			var rnew_inv = rnew.matrix.inverse();
			var m_inv = m.inverse();
			var extrat = matrixMultiply(m_inv, rnew_inv, rold, m);
		
			remapElement(selected,changes,extrat);
			if (angle) {
				if(tlist.numberOfItems) {
					tlist.insertItemBefore(rnew, 0);
				} else {
					tlist.appendItem(rnew);
				}
			}
		}
	} // a non-group

	// if the transform list has been emptied, remove it
	if (tlist.numberOfItems == 0) {
		selected.removeAttribute("transform");
	}
	
	batchCmd.addSubCommand(new ChangeElementCommand(selected, initial));
	
	return batchCmd;
};

// Root Current Transformation Matrix in user units
var root_sctm = null;

// Group: Selection

// Function: clearSelection
// Clears the selection.  The 'selected' handler is then called.
// Parameters: 
// noCall - Optional boolean that when true does not call the "selected" handler
var clearSelection = this.clearSelection = function(noCall) {
	if (selectedElements[0] != null) {
		var len = selectedElements.length;
		for (var i = 0; i < len; ++i) {
			var elem = selectedElements[i];
			if (elem == null) break;
			selectorManager.releaseSelector(elem);
			selectedElements[i] = null;
		}
//		selectedBBoxes[0] = null;
	}
	if(!noCall) call("selected", selectedElements);
};

// TODO: do we need to worry about selectedBBoxes here?


// Function: addToSelection
// Adds a list of elements to the selection.  The 'selected' handler is then called.
//
// Parameters:
// elemsToAdd - an array of DOM elements to add to the selection
// showGrips - a boolean flag indicating whether the resize grips should be shown
var addToSelection = this.addToSelection = function(elemsToAdd, showGrips) {
	if (elemsToAdd.length == 0) { return; }
	// find the first null in our selectedElements array
	var j = 0;
	
	while (j < selectedElements.length) {
		if (selectedElements[j] == null) { 
			break;
		}
		++j;
	}

	// now add each element consecutively
	var i = elemsToAdd.length;
	while (i--) {
		var elem = elemsToAdd[i];
		if (!elem || !svgedit.utilities.getBBox(elem)) continue;

		if(elem.tagName === 'a' && elem.childNodes.length === 1) {
			// Make "a" element's child be the selected element 
			elem = elem.firstChild;
		}

		// if it's not already there, add it
		if (selectedElements.indexOf(elem) == -1) {

			selectedElements[j] = elem;

			// only the first selectedBBoxes element is ever used in the codebase these days
//			if (j == 0) selectedBBoxes[0] = svgedit.utilities.getBBox(elem);
			j++;
			var sel = selectorManager.requestSelector(elem);
	
			if (selectedElements.length > 1) {
				sel.showGrips(false);
			}
		}
	}
	call("selected", selectedElements);
	
	if (showGrips || selectedElements.length == 1) {
		selectorManager.requestSelector(selectedElements[0]).showGrips(true);
	}
	else {
		selectorManager.requestSelector(selectedElements[0]).showGrips(false);
	}

	// make sure the elements are in the correct order
	// See: http://www.w3.org/TR/DOM-Level-3-Core/core.html#Node3-compareDocumentPosition

	selectedElements.sort(function(a,b) {
		if(a && b && a.compareDocumentPosition) {
			return 3 - (b.compareDocumentPosition(a) & 6);	
		} else if(a == null) {
			return 1;
		}
	});
	
	// Make sure first elements are not null
	while(selectedElements[0] == null) selectedElements.shift(0);
};

// Function: selectOnly()
// Selects only the given elements, shortcut for clearSelection(); addToSelection()
//
// Parameters:
// elems - an array of DOM elements to be selected
var selectOnly = this.selectOnly = function(elems, showGrips) {
	clearSelection(true);
	addToSelection(elems, showGrips);
}

// TODO: could use slice here to make this faster?
// TODO: should the 'selected' handler

// Function: removeFromSelection
// Removes elements from the selection.
//
// Parameters:
// elemsToRemove - an array of elements to remove from selection
var removeFromSelection = this.removeFromSelection = function(elemsToRemove) {
	if (selectedElements[0] == null) { return; }
	if (elemsToRemove.length == 0) { return; }

	// find every element and remove it from our array copy
	var newSelectedItems = new Array(selectedElements.length);
		j = 0,
		len = selectedElements.length;
	for (var i = 0; i < len; ++i) {
		var elem = selectedElements[i];
		if (elem) {
			// keep the item
			if (elemsToRemove.indexOf(elem) == -1) {
				newSelectedItems[j] = elem;
				j++;
			}
			else { // remove the item and its selector
				selectorManager.releaseSelector(elem);
			}
		}
	}
	// the copy becomes the master now
	selectedElements = newSelectedItems;
};

// Function: selectAllInCurrentLayer
// Clears the selection, then adds all elements in the current layer to the selection.
this.selectAllInCurrentLayer = function() {
	var current_layer = getCurrentDrawing().getCurrentLayer();
	if (current_layer) {
		current_mode = "select";
		selectOnly($(current_group || current_layer).children());
	}
};

// Function: getMouseTarget
// Gets the desired element from a mouse event
// 
// Parameters:
// evt - Event object from the mouse event
// 
// Returns:
// DOM element we want
var getMouseTarget = this.getMouseTarget = function(evt) {
	if (evt == null) {
		return null;
	}
	var mouse_target = evt.target;
	
	// if it was a <use>, Opera and WebKit return the SVGElementInstance
	if (mouse_target.correspondingUseElement) mouse_target = mouse_target.correspondingUseElement;
	
	// for foreign content, go up until we find the foreignObject
	// WebKit browsers set the mouse target to the svgcanvas div 
	if ([mathns, htmlns].indexOf(mouse_target.namespaceURI) >= 0 && 
		mouse_target.id != "svgcanvas") 
	{
		while (mouse_target.nodeName != "foreignObject") {
			mouse_target = mouse_target.parentNode;
			if(!mouse_target) return svgroot;
		}
	}
	
	// Get the desired mouse_target with jQuery selector-fu
	// If it's root-like, select the root
	var current_layer = getCurrentDrawing().getCurrentLayer();
	if([svgroot, container, svgcontent, current_layer].indexOf(mouse_target) >= 0) {
		return svgroot;
	}
	
	var $target = $(mouse_target);

	// If it's a selection grip, return the grip parent
	if($target.closest('#selectorParentGroup').length) {
		// While we could instead have just returned mouse_target, 
		// this makes it easier to indentify as being a selector grip
		return selectorManager.selectorParentGroup;
	}

	while (mouse_target.parentNode !== (current_group || current_layer)) {
		mouse_target = mouse_target.parentNode;
	}
	
// 	
// 	// go up until we hit a child of a layer
// 	while (mouse_target.parentNode.parentNode.tagName == 'g') {
// 		mouse_target = mouse_target.parentNode;
// 	}
	// Webkit bubbles the mouse event all the way up to the div, so we
	// set the mouse_target to the svgroot like the other browsers
// 	if (mouse_target.nodeName.toLowerCase() == "div") {
// 		mouse_target = svgroot;
// 	}
	
	return mouse_target;
};

// Mouse events
(function() {
	var d_attr = null,
		start_x = null,
		start_y = null,
		r_start_x = null,
		r_start_y = null,
		init_bbox = {},
		freehand = {
			minx: null,
			miny: null,
			maxx: null,
			maxy: null
		};
	
	// - when we are in a create mode, the element is added to the canvas
	//   but the action is not recorded until mousing up
	// - when we are in select mode, select the element, remember the position
	//   and do nothing else
	var mouseDown = function(evt)
	{
		if(canvas.spaceKey || evt.button === 1) return;
		
		var right_click = evt.button === 2;
	
		if(evt.altKey) { // duplicate when  dragging
			svgCanvas.cloneSelectedElements(0,0);
		}
	
		root_sctm = svgcontent.getScreenCTM().inverse();
		
		var pt = transformPoint( evt.pageX, evt.pageY, root_sctm ),
			mouse_x = pt.x * current_zoom,
			mouse_y = pt.y * current_zoom;
			
		evt.preventDefault();

		if(right_click) {
			current_mode = "select";
			lastClickPoint = pt;
		}
		
		// This would seem to be unnecessary...
// 		if(['select', 'resize'].indexOf(current_mode) == -1) {
// 			setGradient();
// 		}
		
		var x = mouse_x / current_zoom,
			y = mouse_y / current_zoom,
			mouse_target = getMouseTarget(evt);
		
		if(mouse_target.tagName === 'a' && mouse_target.childNodes.length === 1) {
			mouse_target = mouse_target.firstChild;
		}
		
		// real_x/y ignores grid-snap value
		var real_x = r_start_x = start_x = x;
		var real_y = r_start_y = start_y = y;

		if(curConfig.gridSnapping){
			x = snapToGrid(x);
			y = snapToGrid(y);
			start_x = snapToGrid(start_x);
			start_y = snapToGrid(start_y);
		}

		// if it is a selector grip, then it must be a single element selected, 
		// set the mouse_target to that and update the mode to rotate/resize
		
		if (mouse_target == selectorManager.selectorParentGroup && selectedElements[0] != null) {
			var grip = evt.target;
			var griptype = elData(grip, "type");
			// rotating
			if (griptype == "rotate") {
				current_mode = "rotate";
			}
			// resizing
			else if(griptype == "resize") {
				current_mode = "resize";
				current_resize_mode = elData(grip, "dir");
			}
			mouse_target = selectedElements[0];
		}
		
		start_transform = mouse_target.getAttribute("transform");
		var tlist = getTransformList(mouse_target);
		switch (current_mode) {
			case "select":
				started = true;
				current_resize_mode = "none";
				if(right_click) started = false;
				
				if (mouse_target != svgroot) {
					// if this element is not yet selected, clear selection and select it
					if (selectedElements.indexOf(mouse_target) == -1) {
						// only clear selection if shift is not pressed (otherwise, add 
						// element to selection)
						if (!evt.shiftKey) {
							// No need to do the call here as it will be done on addToSelection
							clearSelection(true);
						}
						addToSelection([mouse_target]);
						justSelected = mouse_target;
						pathActions.clear();
					}
					// else if it's a path, go into pathedit mode in mouseup
					
					if(!right_click) {
						// insert a dummy transform so if the element(s) are moved it will have
						// a transform to use for its translate
						for (var i = 0; i < selectedElements.length; ++i) {
							if(selectedElements[i] == null) continue;
							var slist = getTransformList(selectedElements[i]);
							if(slist.numberOfItems) {
								slist.insertItemBefore(svgroot.createSVGTransform(), 0);
							} else {
								slist.appendItem(svgroot.createSVGTransform());
							}
						}
					}
				}
				else if(!right_click){
					clearSelection();
					current_mode = "multiselect";
					if (rubberBox == null) {
						rubberBox = selectorManager.getRubberBandBox();
					}
					r_start_x *= current_zoom;
					r_start_y *= current_zoom;
// 					console.log('p',[evt.pageX, evt.pageY]);					
// 					console.log('c',[evt.clientX, evt.clientY]);	
// 					console.log('o',[evt.offsetX, evt.offsetY]);	
// 					console.log('s',[start_x, start_y]);
					
					assignAttributes(rubberBox, {
						'x': r_start_x,
						'y': r_start_y,
						'width': 0,
						'height': 0,
						'display': 'inline'
					}, 100);
				}
				break;
			case "zoom": 
				started = true;
				if (rubberBox == null) {
					rubberBox = selectorManager.getRubberBandBox();
				}
				assignAttributes(rubberBox, {
						'x': real_x * current_zoom,
						'y': real_x * current_zoom,
						'width': 0,
						'height': 0,
						'display': 'inline'
				}, 100);
				break;
			case "resize":
				started = true;
				start_x = x;
				start_y = y;
				
				// Getting the BBox from the selection box, since we know we
				// want to orient around it
				init_bbox = svgedit.utilities.getBBox($('#selectedBox0')[0]);
				var bb = {};
				$.each(init_bbox, function(key, val) {
					bb[key] = val/current_zoom;
				});
				init_bbox = bb;
				
				// append three dummy transforms to the tlist so that
				// we can translate,scale,translate in mousemove
				var pos = getRotationAngle(mouse_target)?1:0;
				
				if(hasMatrixTransform(tlist)) {
					tlist.insertItemBefore(svgroot.createSVGTransform(), pos);
					tlist.insertItemBefore(svgroot.createSVGTransform(), pos);
					tlist.insertItemBefore(svgroot.createSVGTransform(), pos);
				} else {
					tlist.appendItem(svgroot.createSVGTransform());
					tlist.appendItem(svgroot.createSVGTransform());
					tlist.appendItem(svgroot.createSVGTransform());
					
					if(svgedit.browser.supportsNonScalingStroke()) {
						//Handle crash for newer Chrome: https://code.google.com/p/svg-edit/issues/detail?id=904
						//Chromium issue: https://code.google.com/p/chromium/issues/detail?id=114625
						// TODO: Remove this workaround (all isChrome blocks) once vendor fixes the issue
						var isChrome = svgedit.browser.isChrome();
						if(isChrome) {
							var delayedStroke = function(ele) {
								var _stroke = ele.getAttributeNS(null, 'stroke');
								ele.removeAttributeNS(null, 'stroke');
								//Re-apply stroke after delay. Anything higher than 1 seems to cause flicker
								setTimeout(function() { ele.setAttributeNS(null, 'stroke', _stroke) }, 1);
							}
						}
						mouse_target.style.vectorEffect = 'non-scaling-stroke';
						if(isChrome) delayedStroke(mouse_target);

						var all = mouse_target.getElementsByTagName('*'),
						    len = all.length;
						for(var i = 0; i < len; i++) {
							all[i].style.vectorEffect = 'non-scaling-stroke';
							if(isChrome) delayedStroke(all[i]);
						}
					}
				}
				break;
			case "fhellipse":
			case "fhrect":
			case "fhpath":
				started = true;
				d_attr = real_x + "," + real_y + " ";
				var stroke_w = cur_shape.stroke_width == 0?1:cur_shape.stroke_width;
				addSvgElementFromJson({
					"element": "polyline",
					"curStyles": true,
					"attr": {
						"points": d_attr,
						"id": getNextId(),
						"fill": "none",
						"opacity": cur_shape.opacity / 2,
						"stroke-linecap": "round",
						"style": "pointer-events:none"
					}
				});
				freehand.minx = real_x;
				freehand.maxx = real_x;
				freehand.miny = real_y;
				freehand.maxy = real_y;
				break;
			case "image":
				started = true;
				var newImage = addSvgElementFromJson({
					"element": "image",
					"attr": {
						"x": x,
						"y": y,
						"width": 0,
						"height": 0,
						"id": getNextId(),
						"opacity": cur_shape.opacity / 2,
						"style": "pointer-events:inherit"
					}
				});
				setHref(newImage, last_good_img_url);
				preventClickDefault(newImage);
				break;
			case "square":
				// FIXME: once we create the rect, we lose information that this was a square
				// (for resizing purposes this could be important)
			case "rect":
				started = true;
				start_x = x;
				start_y = y;
				addSvgElementFromJson({
					"element": "rect",
					"curStyles": true,
					"attr": {
						"x": x,
						"y": y,
						"width": 0,
						"height": 0,
						"id": getNextId(),
						"opacity": cur_shape.opacity / 2
					}
				});
				break;
			case "line":
				started = true;
				var stroke_w = cur_shape.stroke_width == 0?1:cur_shape.stroke_width;
				addSvgElementFromJson({
					"element": "line",
					"curStyles": true,
					"attr": {
						"x1": x,
						"y1": y,
						"x2": x,
						"y2": y,
						"id": getNextId(),
						"stroke": cur_shape.stroke,
						"stroke-width": stroke_w,
						"stroke-dasharray": cur_shape.stroke_dasharray,
						"stroke-linejoin": cur_shape.stroke_linejoin,
						"stroke-linecap": cur_shape.stroke_linecap,
						"stroke-opacity": cur_shape.stroke_opacity,
						"fill": "none",
						"opacity": cur_shape.opacity / 2,
						"style": "pointer-events:none"
					}
				});
				break;
			case "circle":
				started = true;
				addSvgElementFromJson({
					"element": "circle",
					"curStyles": true,
					"attr": {
						"cx": x,
						"cy": y,
						"r": 0,
						"id": getNextId(),
						"opacity": cur_shape.opacity / 2
					}
				});
				break;
			case "ellipse":
				started = true;
				addSvgElementFromJson({
					"element": "ellipse",
					"curStyles": true,
					"attr": {
						"cx": x,
						"cy": y,
						"rx": 0,
						"ry": 0,
						"id": getNextId(),
						"opacity": cur_shape.opacity / 2
					}
				});
				break;
			case "text":
				started = true;
				var newText = addSvgElementFromJson({
					"element": "text",
					"curStyles": true,
					"attr": {
						"x": x,
						"y": y,
						"id": getNextId(),
						"fill": cur_text.fill,
						"stroke-width": cur_text.stroke_width,
						"font-size": cur_text.font_size,
						"font-family": cur_text.font_family,
						"text-anchor": "middle",
						"xml:space": "preserve",
						"opacity": cur_shape.opacity
					}
				});
// 					newText.textContent = "text";
				break;
			case "path":
				// Fall through
			case "pathedit":
				start_x *= current_zoom;
				start_y *= current_zoom;
				pathActions.mouseDown(evt, mouse_target, start_x, start_y);
				started = true;
				break;
			case "textedit":
				start_x *= current_zoom;
				start_y *= current_zoom;
				textActions.mouseDown(evt, mouse_target, start_x, start_y);
				started = true;
				break;
			case "rotate":
				started = true;
				// we are starting an undoable change (a drag-rotation)
				canvas.undoMgr.beginUndoableChange("transform", selectedElements);
				break;
			default:
				// This could occur in an extension
				break;
		}
		
		var ext_result = runExtensions("mouseDown", {
			event: evt,
			start_x: start_x,
			start_y: start_y,
			selectedElements: selectedElements
		}, true);
		
		$.each(ext_result, function(i, r) {
			if(r && r.started) {
				started = true;
			}
		});
	};
	
	// in this function we do not record any state changes yet (but we do update
	// any elements that are still being created, moved or resized on the canvas)
	var mouseMove = function(evt)
	{
		if (!started) return;
		if(evt.button === 1 || canvas.spaceKey) return;

		var selected = selectedElements[0],
			pt = transformPoint( evt.pageX, evt.pageY, root_sctm ),
			mouse_x = pt.x * current_zoom,
			mouse_y = pt.y * current_zoom,
			shape = getElem(getId());

		var real_x = x = mouse_x / current_zoom;
		var real_y = y = mouse_y / current_zoom;

		if(curConfig.gridSnapping){
			x = snapToGrid(x);
			y = snapToGrid(y);
		}

		evt.preventDefault();
		
		switch (current_mode)
		{
			case "select":
				// we temporarily use a translate on the element(s) being dragged
				// this transform is removed upon mousing up and the element is 
				// relocated to the new location
				if (selectedElements[0] !== null) {
					var dx = x - start_x;
					var dy = y - start_y;
					
					if(curConfig.gridSnapping){
						dx = snapToGrid(dx);
						dy = snapToGrid(dy);
					}

					if(evt.shiftKey) { var xya = snapToAngle(start_x,start_y,x,y); x=xya.x; y=xya.y; }

					if (dx != 0 || dy != 0) {
						var len = selectedElements.length;
						for (var i = 0; i < len; ++i) {
							var selected = selectedElements[i];
							if (selected == null) break;
//							if (i==0) {
//								var box = svgedit.utilities.getBBox(selected);
// 									selectedBBoxes[i].x = box.x + dx;
// 									selectedBBoxes[i].y = box.y + dy;
//							}

							// update the dummy transform in our transform list
							// to be a translate
							var xform = svgroot.createSVGTransform();
							var tlist = getTransformList(selected);
							// Note that if Webkit and there's no ID for this
							// element, the dummy transform may have gotten lost.
							// This results in unexpected behaviour
							
							xform.setTranslate(dx,dy);
							if(tlist.numberOfItems) {
								tlist.replaceItem(xform, 0);
							} else {
								tlist.appendItem(xform);
							}
							
							// update our internal bbox that we're tracking while dragging
							selectorManager.requestSelector(selected).resize();
						}
						
						call("transition", selectedElements);
					}
				}
				break;
			case "multiselect":
				real_x *= current_zoom;
				real_y *= current_zoom;
				assignAttributes(rubberBox, {
					'x': Math.min(r_start_x, real_x),
					'y': Math.min(r_start_y, real_y),
					'width': Math.abs(real_x - r_start_x),
					'height': Math.abs(real_y - r_start_y)
				},100);

				// for each selected:
				// - if newList contains selected, do nothing
				// - if newList doesn't contain selected, remove it from selected
				// - for any newList that was not in selectedElements, add it to selected
				var elemsToRemove = [], elemsToAdd = [],
					newList = getIntersectionList(),
					len = selectedElements.length;
				
				for (var i = 0; i < len; ++i) {
					var ind = newList.indexOf(selectedElements[i]);
					if (ind == -1) {
						elemsToRemove.push(selectedElements[i]);
					}
					else {
						newList[ind] = null;
					}
				}
				
				len = newList.length;
				for (i = 0; i < len; ++i) { if (newList[i]) elemsToAdd.push(newList[i]); }
				
				if (elemsToRemove.length > 0) 
					canvas.removeFromSelection(elemsToRemove);
				
				if (elemsToAdd.length > 0) 
					addToSelection(elemsToAdd);
					
				break;
			case "resize":
				// we track the resize bounding box and translate/scale the selected element
				// while the mouse is down, when mouse goes up, we use this to recalculate
				// the shape's coordinates
				var tlist = getTransformList(selected),
					hasMatrix = hasMatrixTransform(tlist),
					box = hasMatrix ? init_bbox : svgedit.utilities.getBBox(selected), 
					left=box.x, top=box.y, width=box.width,
					height=box.height, dx=(x-start_x), dy=(y-start_y);
				
				if(curConfig.gridSnapping){
					dx = snapToGrid(dx);
					dy = snapToGrid(dy);
					height = snapToGrid(height);
					width = snapToGrid(width);
				}

				// if rotated, adjust the dx,dy values
				var angle = getRotationAngle(selected);
				if (angle) {
					var r = Math.sqrt( dx*dx + dy*dy ),
						theta = Math.atan2(dy,dx) - angle * Math.PI / 180.0;
					dx = r * Math.cos(theta);
					dy = r * Math.sin(theta);
				}

				// if not stretching in y direction, set dy to 0
				// if not stretching in x direction, set dx to 0
				if(current_resize_mode.indexOf("n")==-1 && current_resize_mode.indexOf("s")==-1) {
					dy = 0;
				}
				if(current_resize_mode.indexOf("e")==-1 && current_resize_mode.indexOf("w")==-1) {
					dx = 0;
				}				
				
				var ts = null,
					tx = 0, ty = 0,
					sy = height ? (height+dy)/height : 1, 
					sx = width ? (width+dx)/width : 1;
				// if we are dragging on the north side, then adjust the scale factor and ty
				if(current_resize_mode.indexOf("n") >= 0) {
					sy = height ? (height-dy)/height : 1;
					ty = height;
				}
				
				// if we dragging on the east side, then adjust the scale factor and tx
				if(current_resize_mode.indexOf("w") >= 0) {
					sx = width ? (width-dx)/width : 1;
					tx = width;
				}
				
				// update the transform list with translate,scale,translate
				var translateOrigin = svgroot.createSVGTransform(),
					scale = svgroot.createSVGTransform(),
					translateBack = svgroot.createSVGTransform();

				if(curConfig.gridSnapping){
					left = snapToGrid(left);
					tx = snapToGrid(tx);
					top = snapToGrid(top);
					ty = snapToGrid(ty);
				}

				translateOrigin.setTranslate(-(left+tx),-(top+ty));
				if(evt.shiftKey) {
					if(sx == 1) sx = sy
					else sy = sx;
				}
				scale.setScale(sx,sy);
				
				translateBack.setTranslate(left+tx,top+ty);
				if(hasMatrix) {
					var diff = angle?1:0;
					tlist.replaceItem(translateOrigin, 2+diff);
					tlist.replaceItem(scale, 1+diff);
					tlist.replaceItem(translateBack, 0+diff);
				} else {
					var N = tlist.numberOfItems;
					tlist.replaceItem(translateBack, N-3);
					tlist.replaceItem(scale, N-2);
					tlist.replaceItem(translateOrigin, N-1);
				}

				selectorManager.requestSelector(selected).resize();
				
				call("transition", selectedElements);
				
				break;
			case "zoom":
				real_x *= current_zoom;
				real_y *= current_zoom;
				assignAttributes(rubberBox, {
					'x': Math.min(r_start_x*current_zoom, real_x),
					'y': Math.min(r_start_y*current_zoom, real_y),
					'width': Math.abs(real_x - r_start_x*current_zoom),
					'height': Math.abs(real_y - r_start_y*current_zoom)
				},100);			
				break;
			case "text":
				assignAttributes(shape,{
					'x': x,
					'y': y
				},1000);
				break;
			case "line":
				// Opera has a problem with suspendRedraw() apparently
				var handle = null;
				if (!window.opera) svgroot.suspendRedraw(1000);

				if(curConfig.gridSnapping){
					x = snapToGrid(x);
					y = snapToGrid(y);
				}

				var x2 = x;
				var y2 = y;					

				if(evt.shiftKey) { var xya = snapToAngle(start_x,start_y,x2,y2); x2=xya.x; y2=xya.y; }
				
				shape.setAttributeNS(null, "x2", x2);
				shape.setAttributeNS(null, "y2", y2);
				if (!window.opera) svgroot.unsuspendRedraw(handle);
				break;
			case "foreignObject":
				// fall through
			case "square":
				// fall through
			case "rect":
				// fall through
			case "image":
				var square = (current_mode == 'square') || evt.shiftKey,
					w = Math.abs(x - start_x),
					h = Math.abs(y - start_y),
					new_x, new_y;
				if(square) {
					w = h = Math.max(w, h);
					new_x = start_x < x ? start_x : start_x - w;
					new_y = start_y < y ? start_y : start_y - h;
				} else {
					new_x = Math.min(start_x,x);
					new_y = Math.min(start_y,y);
				}
	
				if(curConfig.gridSnapping){
					w = snapToGrid(w);
					h = snapToGrid(h);
					new_x = snapToGrid(new_x);
					new_y = snapToGrid(new_y);
				}

				assignAttributes(shape,{
					'width': w,
					'height': h,
					'x': new_x,
					'y': new_y
				},1000);
				
				break;
			case "circle":
				var c = $(shape).attr(["cx", "cy"]);
				var cx = c.cx, cy = c.cy,
					rad = Math.sqrt( (x-cx)*(x-cx) + (y-cy)*(y-cy) );
				if(curConfig.gridSnapping){
					rad = snapToGrid(rad);
				}
				shape.setAttributeNS(null, "r", rad);
				break;
			case "ellipse":
				var c = $(shape).attr(["cx", "cy"]);
				var cx = c.cx, cy = c.cy;
				// Opera has a problem with suspendRedraw() apparently
					handle = null;
				if (!window.opera) svgroot.suspendRedraw(1000);
				if(curConfig.gridSnapping){
					x = snapToGrid(x);
					cx = snapToGrid(cx);
					y = snapToGrid(y);
					cy = snapToGrid(cy);
				}
				shape.setAttributeNS(null, "rx", Math.abs(x - cx) );
				var ry = Math.abs(evt.shiftKey?(x - cx):(y - cy));
				shape.setAttributeNS(null, "ry", ry );
				if (!window.opera) svgroot.unsuspendRedraw(handle);
				break;
			case "fhellipse":
			case "fhrect":
				freehand.minx = Math.min(real_x, freehand.minx);
				freehand.maxx = Math.max(real_x, freehand.maxx);
				freehand.miny = Math.min(real_y, freehand.miny);
				freehand.maxy = Math.max(real_y, freehand.maxy);
			// break; missing on purpose
			case "fhpath":
				d_attr += + real_x + "," + real_y + " ";
				shape.setAttributeNS(null, "points", d_attr);
				break;
			// update path stretch line coordinates
			case "path":
				// fall through
			case "pathedit":
				x *= current_zoom;
				y *= current_zoom;
				
				if(curConfig.gridSnapping){
					x = snapToGrid(x);
					y = snapToGrid(y);
					start_x = snapToGrid(start_x);
					start_y = snapToGrid(start_y);
				}
				if(evt.shiftKey) {
					var path = svgedit.path.path;
					if(path) {
						var x1 = path.dragging?path.dragging[0]:start_x;
						var y1 = path.dragging?path.dragging[1]:start_y;
					} else {
						var x1 = start_x;
						var y1 = start_y;
					}
					var xya = snapToAngle(x1,y1,x,y);
					x=xya.x; y=xya.y;
				}
				
				if(rubberBox && rubberBox.getAttribute('display') !== 'none') {
					real_x *= current_zoom;
					real_y *= current_zoom;
					assignAttributes(rubberBox, {
						'x': Math.min(r_start_x*current_zoom, real_x),
						'y': Math.min(r_start_y*current_zoom, real_y),
						'width': Math.abs(real_x - r_start_x*current_zoom),
						'height': Math.abs(real_y - r_start_y*current_zoom)
					},100);	
				}
				pathActions.mouseMove(x, y);
				
				break;
			case "textedit":
				x *= current_zoom;
				y *= current_zoom;
// 					if(rubberBox && rubberBox.getAttribute('display') != 'none') {
// 						assignAttributes(rubberBox, {
// 							'x': Math.min(start_x,x),
// 							'y': Math.min(start_y,y),
// 							'width': Math.abs(x-start_x),
// 							'height': Math.abs(y-start_y)
// 						},100);
// 					}
				
				textActions.mouseMove(mouse_x, mouse_y);
				
				break;
			case "rotate":
				var box = svgedit.utilities.getBBox(selected),
					cx = box.x + box.width/2, 
					cy = box.y + box.height/2,
					m = getMatrix(selected),
					center = transformPoint(cx,cy,m);
				cx = center.x;
				cy = center.y;
				var angle = ((Math.atan2(cy-y,cx-x)  * (180/Math.PI))-90) % 360;
				if(curConfig.gridSnapping){
					angle = snapToGrid(angle);
				}
				if(evt.shiftKey) { // restrict rotations to nice angles (WRS)
					var snap = 45;
					angle= Math.round(angle/snap)*snap;
				}

				canvas.setRotationAngle(angle<-180?(360+angle):angle, true);
				call("transition", selectedElements);
				break;
			default:
				break;
		}
		
		runExtensions("mouseMove", {
			event: evt,
			mouse_x: mouse_x,
			mouse_y: mouse_y,
			selected: selected
		});

	}; // mouseMove()
	
	// - in create mode, the element's opacity is set properly, we create an InsertElementCommand
	//   and store it on the Undo stack
	// - in move/resize mode, the element's attributes which were affected by the move/resize are
	//   identified, a ChangeElementCommand is created and stored on the stack for those attrs
	//   this is done in when we recalculate the selected dimensions()
	var mouseUp = function(evt)
	{
		if(evt.button === 2) return;
		var tempJustSelected = justSelected;
		justSelected = null;
		if (!started) return;
		var pt = transformPoint( evt.pageX, evt.pageY, root_sctm ),
			mouse_x = pt.x * current_zoom,
			mouse_y = pt.y * current_zoom,
			x = mouse_x / current_zoom,
			y = mouse_y / current_zoom,
			element = getElem(getId()),
			keep = false;

		var real_x = x;
		var real_y = y;

		// TODO: Make true when in multi-unit mode
		var useUnit = false; // (curConfig.baseUnit !== 'px');
		started = false;
		switch (current_mode)
		{
			// intentionally fall-through to select here
			case "resize":
			case "multiselect":
				if (rubberBox != null) {
					rubberBox.setAttribute("display", "none");
					curBBoxes = [];
				}
				current_mode = "select";
			case "select":
				if (selectedElements[0] != null) {
					// if we only have one selected element
					if (selectedElements[1] == null) {
						// set our current stroke/fill properties to the element's
						var selected = selectedElements[0];
						switch ( selected.tagName ) {
							case "g":
							case "use":
							case "image":
							case "foreignObject":
								break;
							default:
								cur_properties.fill = selected.getAttribute("fill");
								cur_properties.fill_opacity = selected.getAttribute("fill-opacity");
								cur_properties.stroke = selected.getAttribute("stroke");
								cur_properties.stroke_opacity = selected.getAttribute("stroke-opacity");
								cur_properties.stroke_width = selected.getAttribute("stroke-width");
								cur_properties.stroke_dasharray = selected.getAttribute("stroke-dasharray");
								cur_properties.stroke_linejoin = selected.getAttribute("stroke-linejoin");
								cur_properties.stroke_linecap = selected.getAttribute("stroke-linecap");
						}

						if (selected.tagName == "text") {
							cur_text.font_size = selected.getAttribute("font-size");
							cur_text.font_family = selected.getAttribute("font-family");
						}
						selectorManager.requestSelector(selected).showGrips(true);
						
						// This shouldn't be necessary as it was done on mouseDown...
// 							call("selected", [selected]);
					}
					// always recalculate dimensions to strip off stray identity transforms
					recalculateAllSelectedDimensions();
					// if it was being dragged/resized
					if (real_x != r_start_x || real_y != r_start_y) {
						var len = selectedElements.length;
						for	(var i = 0; i < len; ++i) {
							if (selectedElements[i] == null) break;
							if(!selectedElements[i].firstChild) {
								// Not needed for groups (incorrectly resizes elems), possibly not needed at all?
								selectorManager.requestSelector(selectedElements[i]).resize();
							}
						}
					}
					// no change in position/size, so maybe we should move to pathedit
					else {
						var t = evt.target;
						if (selectedElements[0].nodeName === "path" && selectedElements[1] == null) {
							pathActions.select(selectedElements[0]);
						} // if it was a path
						// else, if it was selected and this is a shift-click, remove it from selection
						else if (evt.shiftKey) {
							if(tempJustSelected != t) {
								canvas.removeFromSelection([t]);
							}
						}
					} // no change in mouse position
					
					// Remove non-scaling stroke
					if(svgedit.browser.supportsNonScalingStroke()) {
						var elem = selectedElements[0];
						if (elem) {
							elem.removeAttribute('style');
							svgedit.utilities.walkTree(elem, function(elem) {
								elem.removeAttribute('style');
							});
						}
					}

				}
				return;
				break;
			case "zoom":
				if (rubberBox != null) {
					rubberBox.setAttribute("display", "none");
				}
				var factor = evt.shiftKey?.5:2;
				call("zoomed", {
					'x': Math.min(r_start_x, real_x),
					'y': Math.min(r_start_y, real_y),
					'width': Math.abs(real_x - r_start_x),
					'height': Math.abs(real_y - r_start_y),
					'factor': factor
				});
				return;
			case "fhpath":
				// Check that the path contains at least 2 points; a degenerate one-point path
				// causes problems.
				// Webkit ignores how we set the points attribute with commas and uses space
				// to separate all coordinates, see https://bugs.webkit.org/show_bug.cgi?id=29870
				var coords = element.getAttribute('points');
				var commaIndex = coords.indexOf(',');
				if (commaIndex >= 0) {
					keep = coords.indexOf(',', commaIndex+1) >= 0;
				} else {
					keep = coords.indexOf(' ', coords.indexOf(' ')+1) >= 0;
				}
				if (keep) {
					element = pathActions.smoothPolylineIntoPath(element);
				}
				break;
			case "line":
				var attrs = $(element).attr(["x1", "x2", "y1", "y2"]);
				keep = (attrs.x1 != attrs.x2 || attrs.y1 != attrs.y2);
				break;
			case "foreignObject":
			case "square":
			case "rect":
			case "image":
				var attrs = $(element).attr(["width", "height"]);
				// Image should be kept regardless of size (use inherit dimensions later)
				keep = (attrs.width != 0 || attrs.height != 0) || current_mode === "image";
				break;
			case "circle":
				keep = (element.getAttribute('r') != 0);
				break;
			case "ellipse":
				var attrs = $(element).attr(["rx", "ry"]);
				keep = (attrs.rx != null || attrs.ry != null);
				break;
			case "fhellipse":
				if ((freehand.maxx - freehand.minx) > 0 &&
					(freehand.maxy - freehand.miny) > 0) {
					element = addSvgElementFromJson({
						"element": "ellipse",
						"curStyles": true,
						"attr": {
							"cx": (freehand.minx + freehand.maxx) / 2,
							"cy": (freehand.miny + freehand.maxy) / 2,
							"rx": (freehand.maxx - freehand.minx) / 2,
							"ry": (freehand.maxy - freehand.miny) / 2,
							"id": getId()
						}
					});
					call("changed",[element]);
					keep = true;
				}
				break;
			case "fhrect":
				if ((freehand.maxx - freehand.minx) > 0 &&
					(freehand.maxy - freehand.miny) > 0) {
					element = addSvgElementFromJson({
						"element": "rect",
						"curStyles": true,
						"attr": {
							"x": freehand.minx,
							"y": freehand.miny,
							"width": (freehand.maxx - freehand.minx),
							"height": (freehand.maxy - freehand.miny),
							"id": getId()
						}
					});
					call("changed",[element]);
					keep = true;
				}
				break;
			case "text":
				keep = true;
				selectOnly([element]);
				textActions.start(element);
				break;
			case "path":
				// set element to null here so that it is not removed nor finalized
				element = null;
				// continue to be set to true so that mouseMove happens
				started = true;
				
				var res = pathActions.mouseUp(evt, element, mouse_x, mouse_y);
				element = res.element
				keep = res.keep;
				break;
			case "pathedit":
				keep = true;
				element = null;
				pathActions.mouseUp(evt);
				break;
			case "textedit":
				keep = false;
				element = null;
				textActions.mouseUp(evt, mouse_x, mouse_y);
				break;
			case "rotate":
				keep = true;
				element = null;
				current_mode = "select";
				var batchCmd = canvas.undoMgr.finishUndoableChange();
				if (!batchCmd.isEmpty()) { 
					addCommandToHistory(batchCmd);
				}
				// perform recalculation to weed out any stray identity transforms that might get stuck
				recalculateAllSelectedDimensions();
				call("changed", selectedElements);
				break;
			default:
				// This could occur in an extension
				break;
		}
		
		var ext_result = runExtensions("mouseUp", {
			event: evt,
			mouse_x: mouse_x,
			mouse_y: mouse_y
		}, true);
		
		$.each(ext_result, function(i, r) {
			if(r) {
				keep = r.keep || keep;
				element = r.element;
				started = r.started || started;
			}
		});
		
		if (!keep && element != null) {
			getCurrentDrawing().releaseId(getId());
			element.parentNode.removeChild(element);
			element = null;
			
			var t = evt.target;
			
			// if this element is in a group, go up until we reach the top-level group 
			// just below the layer groups
			// TODO: once we implement links, we also would have to check for <a> elements
			while (t.parentNode.parentNode.tagName == "g") {
				t = t.parentNode;
			}
			// if we are not in the middle of creating a path, and we've clicked on some shape, 
			// then go to Select mode.
			// WebKit returns <div> when the canvas is clicked, Firefox/Opera return <svg>
			if ( (current_mode != "path" || !drawn_path) &&
				t.parentNode.id != "selectorParentGroup" &&
				t.id != "svgcanvas" && t.id != "svgroot") 
			{
				// switch into "select" mode if we've clicked on an element
				canvas.setMode("select");
				selectOnly([t], true);
			}
			
		} else if (element != null) {
			canvas.addedNew = true;
			
			if(useUnit) svgedit.units.convertAttrs(element);
			
			var ani_dur = .2, c_ani;
			if(opac_ani.beginElement && element.getAttribute('opacity') != cur_shape.opacity) {
				c_ani = $(opac_ani).clone().attr({
					to: cur_shape.opacity,
					dur: ani_dur
				}).appendTo(element);
				try {
					// Fails in FF4 on foreignObject
					c_ani[0].beginElement();
				} catch(e){}
			} else {
				ani_dur = 0;
			}
			
			// Ideally this would be done on the endEvent of the animation,
			// but that doesn't seem to be supported in Webkit
			setTimeout(function() {
				if(c_ani) c_ani.remove();
				element.setAttribute("opacity", cur_shape.opacity);
				element.setAttribute("style", "pointer-events:inherit");
				cleanupElement(element);
				if(current_mode === "path") {
					pathActions.toEditMode(element);
				} else {
					if(curConfig.selectNew) {
						selectOnly([element], true);
					}
				}
				// we create the insert command that is stored on the stack
				// undo means to call cmd.unapply(), redo means to call cmd.apply()
				addCommandToHistory(new InsertElementCommand(element));
				
				call("changed",[element]);
			}, ani_dur * 1000);
		}
		
		start_transform = null;
	};
	
	var dblClick = function(evt) {
		var evt_target = evt.target;
		var parent = evt_target.parentNode;
		
		// Do nothing if already in current group
		if(parent === current_group) return;
		
		var mouse_target = getMouseTarget(evt);
		var tagName = mouse_target.tagName;
		
		if(tagName === 'text' && current_mode !== 'textedit') {
			var pt = transformPoint( evt.pageX, evt.pageY, root_sctm );
			textActions.select(mouse_target, pt.x, pt.y);
		}
		
		if((tagName === "g" || tagName === "a") && getRotationAngle(mouse_target)) {
			// TODO: Allow method of in-group editing without having to do 
			// this (similar to editing rotated paths)
		
			// Ungroup and regroup
			pushGroupProperties(mouse_target);
			mouse_target = selectedElements[0];
			clearSelection(true);
		}
		// Reset context
		if(current_group) {
			leaveContext();
		}
		
		if((parent.tagName !== 'g' && parent.tagName !== 'a') ||
			parent === getCurrentDrawing().getCurrentLayer() ||
			mouse_target === selectorManager.selectorParentGroup)
		{
			// Escape from in-group edit
			return;
		}
		setContext(mouse_target);
	}

	// prevent links from being followed in the canvas
	var handleLinkInCanvas = function(e) {
		e.preventDefault();
		return false;
	};
	
	// Added mouseup to the container here.
	// TODO(codedread): Figure out why after the Closure compiler, the window mouseup is ignored.
	$(container).mousedown(mouseDown).mousemove(mouseMove).click(handleLinkInCanvas).dblclick(dblClick).mouseup(mouseUp);
//	$(window).mouseup(mouseUp);
	
	$(container).bind("mousewheel DOMMouseScroll", function(e){
		if(!e.shiftKey) return;
		e.preventDefault();

		root_sctm = svgcontent.getScreenCTM().inverse();
		var pt = transformPoint( e.pageX, e.pageY, root_sctm );
		var bbox = {
			'x': pt.x,
			'y': pt.y,
			'width': 0,
			'height': 0
		};

		// Respond to mouse wheel in IE/Webkit/Opera.
		// (It returns up/dn motion in multiples of 120)
		if(e.wheelDelta) {
			if (e.wheelDelta >= 120) {
				bbox.factor = 2;
			} else if (e.wheelDelta <= -120) {
				bbox.factor = .5;
			}
		} else if(e.detail) {
			if (e.detail > 0) {
				bbox.factor = .5;
			} else if (e.detail < 0) {
				bbox.factor = 2;			
			}				
		}
		
		if(!bbox.factor) return;
		call("zoomed", bbox);
	});
	
}());

// Function: preventClickDefault
// Prevents default browser click behaviour on the given element
//
// Parameters:
// img - The DOM element to prevent the cilck on
var preventClickDefault = function(img) {
	$(img).click(function(e){e.preventDefault()});
}

// Group: Text edit functions
// Functions relating to editing text elements
var textActions = canvas.textActions = function() {
	var curtext;
	var textinput;
	var cursor;
	var selblock;
	var blinker;
	var chardata = [];
	var textbb, transbb;
	var matrix;
	var last_x, last_y;
	var allow_dbl;
	
	function setCursor(index) {
		var empty = (textinput.value === "");
		$(textinput).focus();
	
		if(!arguments.length) {
			if(empty) {
				index = 0;
			} else {
				if(textinput.selectionEnd !== textinput.selectionStart) return;
				index = textinput.selectionEnd;
			}
		}
		
		var charbb;
		charbb = chardata[index];
		if(!empty) {
			textinput.setSelectionRange(index, index);
		}
		cursor = getElem("text_cursor");
		if (!cursor) {
			cursor = document.createElementNS(svgns, "line");
			assignAttributes(cursor, {
				'id': "text_cursor",
				'stroke': "#333",
				'stroke-width': 1
			});
			cursor = getElem("selectorParentGroup").appendChild(cursor);
		}
		
		if(!blinker) {
			blinker = setInterval(function() {
				var show = (cursor.getAttribute('display') === 'none');
				cursor.setAttribute('display', show?'inline':'none');
			}, 600);

		}
		
		
		var start_pt = ptToScreen(charbb.x, textbb.y);
		var end_pt = ptToScreen(charbb.x, (textbb.y + textbb.height));
		
		assignAttributes(cursor, {
			x1: start_pt.x,
			y1: start_pt.y,
			x2: end_pt.x,
			y2: end_pt.y,
			visibility: 'visible',
			display: 'inline'
		});
		
		if(selblock) selblock.setAttribute('d', '');
	}
	
	function setSelection(start, end, skipInput) {
		if(start === end) {
			setCursor(end);
			return;
		}
	
		if(!skipInput) {
			textinput.setSelectionRange(start, end);
		}
		
		selblock = getElem("text_selectblock");
		if (!selblock) {

			selblock = document.createElementNS(svgns, "path");
			assignAttributes(selblock, {
				'id': "text_selectblock",
				'fill': "green",
				'opacity': .5,
				'style': "pointer-events:none"
			});
			getElem("selectorParentGroup").appendChild(selblock);
		}

		
		var startbb = chardata[start];
		
		var endbb = chardata[end];
		
		cursor.setAttribute('visibility', 'hidden');
		
		var tl = ptToScreen(startbb.x, textbb.y),
			tr = ptToScreen(startbb.x + (endbb.x - startbb.x), textbb.y),
			bl = ptToScreen(startbb.x, textbb.y + textbb.height),
			br = ptToScreen(startbb.x + (endbb.x - startbb.x), textbb.y + textbb.height);
		
		
		var dstr = "M" + tl.x + "," + tl.y
					+ " L" + tr.x + "," + tr.y
					+ " " + br.x + "," + br.y
					+ " " + bl.x + "," + bl.y + "z";
		
		assignAttributes(selblock, {
			d: dstr,
			'display': 'inline'
		});
	}
	
	function getIndexFromPoint(mouse_x, mouse_y) {
		// Position cursor here
		var pt = svgroot.createSVGPoint();
		pt.x = mouse_x;
		pt.y = mouse_y;

		// No content, so return 0
		if(chardata.length == 1) return 0;
		// Determine if cursor should be on left or right of character
		var charpos = curtext.getCharNumAtPosition(pt);
		if(charpos < 0) {
			// Out of text range, look at mouse coords
			charpos = chardata.length - 2;
			if(mouse_x <= chardata[0].x) {
				charpos = 0;
			}
		} else if(charpos >= chardata.length - 2) {
			charpos = chardata.length - 2;
		}
		var charbb = chardata[charpos];
		var mid = charbb.x + (charbb.width/2);
		if(mouse_x > mid) {
			charpos++;
		}
		return charpos;
	}
	
	function setCursorFromPoint(mouse_x, mouse_y) {
		setCursor(getIndexFromPoint(mouse_x, mouse_y));
	}
	
	function setEndSelectionFromPoint(x, y, apply) {
		var i1 = textinput.selectionStart;
		var i2 = getIndexFromPoint(x, y);
		
		var start = Math.min(i1, i2);
		var end = Math.max(i1, i2);
		setSelection(start, end, !apply);
	}
		
	function screenToPt(x_in, y_in) {
		var out = {
			x: x_in,
			y: y_in
		}
		
		out.x /= current_zoom;
		out.y /= current_zoom;			

		if(matrix) {
			var pt = transformPoint(out.x, out.y, matrix.inverse());
			out.x = pt.x;
			out.y = pt.y;
		}
		
		return out;
	}	
	
	function ptToScreen(x_in, y_in) {
		var out = {
			x: x_in,
			y: y_in
		}
		
		if(matrix) {
			var pt = transformPoint(out.x, out.y, matrix);
			out.x = pt.x;
			out.y = pt.y;
		}
		
		out.x *= current_zoom;
		out.y *= current_zoom;
		
		return out;
	}
	
	function hideCursor() {
		if(cursor) {
			cursor.setAttribute('visibility', 'hidden');
		}
	}
	
	function selectAll(evt) {
		setSelection(0, curtext.textContent.length);
		$(this).unbind(evt);
	}

	function selectWord(evt) {
		if(!allow_dbl || !curtext) return;
	
		var ept = transformPoint( evt.pageX, evt.pageY, root_sctm ),
			mouse_x = ept.x * current_zoom,
			mouse_y = ept.y * current_zoom;
		var pt = screenToPt(mouse_x, mouse_y);
		
		var index = getIndexFromPoint(pt.x, pt.y);
		var str = curtext.textContent;
		var first = str.substr(0, index).replace(/[a-z0-9]+$/i, '').length;
		var m = str.substr(index).match(/^[a-z0-9]+/i);
		var last = (m?m[0].length:0) + index;
		setSelection(first, last);
		
		// Set tripleclick
		$(evt.target).click(selectAll);
		setTimeout(function() {
			$(evt.target).unbind('click', selectAll);
		}, 300);
		
	}

	return {
		select: function(target, x, y) {
			curtext = target;
			textActions.toEditMode(x, y);
		},
		start: function(elem) {
			curtext = elem;
			textActions.toEditMode();
		},
		mouseDown: function(evt, mouse_target, start_x, start_y) {
			var pt = screenToPt(start_x, start_y);
		
			textinput.focus();
			setCursorFromPoint(pt.x, pt.y);
			last_x = start_x;
			last_y = start_y;
			
			// TODO: Find way to block native selection
		},
		mouseMove: function(mouse_x, mouse_y) {
			var pt = screenToPt(mouse_x, mouse_y);
			setEndSelectionFromPoint(pt.x, pt.y);
		},			
		mouseUp: function(evt, mouse_x, mouse_y) {
			var pt = screenToPt(mouse_x, mouse_y);
			
			setEndSelectionFromPoint(pt.x, pt.y, true);
			
			// TODO: Find a way to make this work: Use transformed BBox instead of evt.target 
// 				if(last_x === mouse_x && last_y === mouse_y
// 					&& !svgedit.math.rectsIntersect(transbb, {x: pt.x, y: pt.y, width:0, height:0})) {
// 					textActions.toSelectMode(true);				
// 				}

			if(
				evt.target !== curtext
				&&	mouse_x < last_x + 2
				&& mouse_x > last_x - 2
				&&	mouse_y < last_y + 2
				&& mouse_y > last_y - 2) {

				textActions.toSelectMode(true);
			}

		},
		setCursor: setCursor,
		toEditMode: function(x, y) {
			allow_dbl = false;
			current_mode = "textedit";
			selectorManager.requestSelector(curtext).showGrips(false);
			// Make selector group accept clicks
			var sel = selectorManager.requestSelector(curtext).selectorRect;
			
			textActions.init();

			$(curtext).css('cursor', 'text');
			
// 				if(svgedit.browser.supportsEditableText()) {
// 					curtext.setAttribute('editable', 'simple');
// 					return;
// 				}
			
			if(!arguments.length) {
				setCursor();
			} else {
				var pt = screenToPt(x, y);
				setCursorFromPoint(pt.x, pt.y);
			}
			
			setTimeout(function() {
				allow_dbl = true;
			}, 300);
		},
		toSelectMode: function(selectElem) {
			current_mode = "select";
			clearInterval(blinker);
			blinker = null;
			if(selblock) $(selblock).attr('display','none');
			if(cursor) $(cursor).attr('visibility','hidden');
			$(curtext).css('cursor', 'move');
			
			if(selectElem) {
				clearSelection();
				$(curtext).css('cursor', 'move');
				
				call("selected", [curtext]);
				addToSelection([curtext], true);
			}
			if(curtext && !curtext.textContent.length) {
				// No content, so delete
				canvas.deleteSelectedElements();
			}
			
			$(textinput).blur();
			
			curtext = false;
			
// 				if(svgedit.browser.supportsEditableText()) {
// 					curtext.removeAttribute('editable');
// 				}
		},
		setInputElem: function(elem) {
			textinput = elem;
// 			$(textinput).blur(hideCursor);
		},
		clear: function() {
			if(current_mode == "textedit") {
				textActions.toSelectMode();
			}
		},
		init: function(inputElem) {
			if(!curtext) return;

// 				if(svgedit.browser.supportsEditableText()) {
// 					curtext.select();
// 					return;
// 				}
		
			if(!curtext.parentNode) {
				// Result of the ffClone, need to get correct element
				curtext = selectedElements[0];
				selectorManager.requestSelector(curtext).showGrips(false);
			}
			
			var str = curtext.textContent;
			var len = str.length;
			
			var xform = curtext.getAttribute('transform');

			textbb = svgedit.utilities.getBBox(curtext);
			
			matrix = xform?getMatrix(curtext):null;

			chardata = Array(len);
			textinput.focus();
			
			$(curtext).unbind('dblclick', selectWord).dblclick(selectWord);
			
			if(!len) {
				var end = {x: textbb.x + (textbb.width/2), width: 0};
			}
			
			for(var i=0; i<len; i++) {
				var start = curtext.getStartPositionOfChar(i);
				var end = curtext.getEndPositionOfChar(i);
				
				if(!svgedit.browser.supportsGoodTextCharPos()) {
					var offset = canvas.contentW * current_zoom;
					start.x -= offset;
					end.x -= offset;
					
					start.x /= current_zoom;
					end.x /= current_zoom;
				}
				
				// Get a "bbox" equivalent for each character. Uses the
				// bbox data of the actual text for y, height purposes
				
				// TODO: Decide if y, width and height are actually necessary
				chardata[i] = {
					x: start.x,
					y: textbb.y, // start.y?
					width: end.x - start.x,
					height: textbb.height
				};
			}
			
			// Add a last bbox for cursor at end of text
			chardata.push({
				x: end.x,
				width: 0
			});
			setSelection(textinput.selectionStart, textinput.selectionEnd, true);
		}
	}
}();

// TODO: Migrate all of this code into path.js
// Group: Path edit functions
// Functions relating to editing path elements
var pathActions = canvas.pathActions = function() {
	
	var subpath = false;
	var current_path;
	var newPoint, firstCtrl;
	
	function resetD(p) {
		p.setAttribute("d", pathActions.convertPath(p));
	}

	// TODO: Move into path.js
		svgedit.path.Path.prototype.endChanges = function(text) {
			if(svgedit.browser.isWebkit()) resetD(this.elem);
			var cmd = new ChangeElementCommand(this.elem, {d: this.last_d}, text);
			addCommandToHistory(cmd);
			call("changed", [this.elem]);
		}

		svgedit.path.Path.prototype.addPtsToSelection = function(indexes) {
			if(!$.isArray(indexes)) indexes = [indexes];
			for(var i=0; i< indexes.length; i++) {
				var index = indexes[i];
				var seg = this.segs[index];
				if(seg.ptgrip) {
					if(this.selected_pts.indexOf(index) == -1 && index >= 0) {
						this.selected_pts.push(index);
					}
				}
			};
			this.selected_pts.sort();
			var i = this.selected_pts.length,
				grips = new Array(i);
			// Loop through points to be selected and highlight each
			while(i--) {
				var pt = this.selected_pts[i];
				var seg = this.segs[pt];
				seg.select(true);
				grips[i] = seg.ptgrip;
			}
			// TODO: Correct this:
			pathActions.canDeleteNodes = true;
			
			pathActions.closed_subpath = this.subpathIsClosed(this.selected_pts[0]);
			
			call("selected", grips);
		}

	var current_path = null,
		drawn_path = null,
		hasMoved = false;
	
	// This function converts a polyline (created by the fh_path tool) into
	// a path element and coverts every three line segments into a single bezier
	// curve in an attempt to smooth out the free-hand
	var smoothPolylineIntoPath = function(element) {
		var points = element.points;
		var N = points.numberOfItems;
		if (N >= 4) {
			// loop through every 3 points and convert to a cubic bezier curve segment
			// 
			// NOTE: this is cheating, it means that every 3 points has the potential to 
			// be a corner instead of treating each point in an equal manner.  In general,
			// this technique does not look that good.
			// 
			// I am open to better ideas!
			// 
			// Reading:
			// - http://www.efg2.com/Lab/Graphics/Jean-YvesQueinecBezierCurves.htm
			// - http://www.codeproject.com/KB/graphics/BezierSpline.aspx?msg=2956963
			// - http://www.ian-ko.com/ET_GeoWizards/UserGuide/smooth.htm
			// - http://www.cs.mtu.edu/~shene/COURSES/cs3621/NOTES/spline/Bezier/bezier-der.html
			var curpos = points.getItem(0), prevCtlPt = null;
			var d = [];
			d.push(["M",curpos.x,",",curpos.y," C"].join(""));
			for (var i = 1; i <= (N-4); i += 3) {
				var ct1 = points.getItem(i);
				var ct2 = points.getItem(i+1);
				var end = points.getItem(i+2);
				
				// if the previous segment had a control point, we want to smooth out
				// the control points on both sides
				if (prevCtlPt) {
					var newpts = svgedit.path.smoothControlPoints( prevCtlPt, ct1, curpos );
					if (newpts && newpts.length == 2) {
						var prevArr = d[d.length-1].split(',');
						prevArr[2] = newpts[0].x;
						prevArr[3] = newpts[0].y;
						d[d.length-1] = prevArr.join(',');
						ct1 = newpts[1];
					}
				}
				
				d.push([ct1.x,ct1.y,ct2.x,ct2.y,end.x,end.y].join(','));
				
				curpos = end;
				prevCtlPt = ct2;
			}
			// handle remaining line segments
			d.push("L");
			for(;i < N;++i) {
				var pt = points.getItem(i);
				d.push([pt.x,pt.y].join(","));
			}
			d = d.join(" ");

			// create new path element
			element = addSvgElementFromJson({
				"element": "path",
				"curStyles": true,
				"attr": {
					"id": getId(),
					"d": d,
					"fill": "none"
				}
			});
			// No need to call "changed", as this is already done under mouseUp
		}
		return element;
	};

	return {
		mouseDown: function(evt, mouse_target, start_x, start_y) {
			if(current_mode === "path") {
				mouse_x = start_x;
				mouse_y = start_y;
				
				var x = mouse_x/current_zoom,
					y = mouse_y/current_zoom,
					stretchy = getElem("path_stretch_line");
				newPoint = [x, y];	
				
				if(curConfig.gridSnapping){
					x = snapToGrid(x);
					y = snapToGrid(y);
					mouse_x = snapToGrid(mouse_x);
					mouse_y = snapToGrid(mouse_y);
				}

				if (!stretchy) {
					stretchy = document.createElementNS(svgns, "path");
					assignAttributes(stretchy, {
						'id': "path_stretch_line",
						'stroke': "#22C",
						'stroke-width': "0.5",
						'fill': 'none'
					});
					stretchy = getElem("selectorParentGroup").appendChild(stretchy);
				}
				stretchy.setAttribute("display", "inline");
				
				var keep = null;
				
				// if pts array is empty, create path element with M at current point
				if (!drawn_path) {
					d_attr = "M" + x + "," + y + " ";
					drawn_path = addSvgElementFromJson({
						"element": "path",
						"curStyles": true,
						"attr": {
							"d": d_attr,
							"id": getNextId(),
							"opacity": cur_shape.opacity / 2
						}
					});
					// set stretchy line to first point
					stretchy.setAttribute('d', ['M', mouse_x, mouse_y, mouse_x, mouse_y].join(' '));
					var index = subpath ? svgedit.path.path.segs.length : 0;
					svgedit.path.addPointGrip(index, mouse_x, mouse_y);
				}
				else {
					// determine if we clicked on an existing point
					var seglist = drawn_path.pathSegList;
					var i = seglist.numberOfItems;
					var FUZZ = 6/current_zoom;
					var clickOnPoint = false;
					while(i) {
						i --;
						var item = seglist.getItem(i);
						var px = item.x, py = item.y;
						// found a matching point
						if ( x >= (px-FUZZ) && x <= (px+FUZZ) && y >= (py-FUZZ) && y <= (py+FUZZ) ) {
							clickOnPoint = true;
							break;
						}
					}
					
					// get path element that we are in the process of creating
					var id = getId();
				
					// Remove previous path object if previously created
					svgedit.path.removePath_(id);
					
					var newpath = getElem(id);
					
					var len = seglist.numberOfItems;
					// if we clicked on an existing point, then we are done this path, commit it
					// (i,i+1) are the x,y that were clicked on
					if (clickOnPoint) {
						// if clicked on any other point but the first OR
						// the first point was clicked on and there are less than 3 points
						// then leave the path open
						// otherwise, close the path
						if (i <= 1 && len >= 2) {
							// Create end segment
							var abs_x = seglist.getItem(0).x;
							var abs_y = seglist.getItem(0).y;
							

							var s_seg = stretchy.pathSegList.getItem(1);
							if(s_seg.pathSegType === 4) {
								var newseg = drawn_path.createSVGPathSegLinetoAbs(abs_x, abs_y);
							} else {
								var newseg = drawn_path.createSVGPathSegCurvetoCubicAbs(
									abs_x,
									abs_y,
									s_seg.x1 / current_zoom,
									s_seg.y1 / current_zoom,
									abs_x,
									abs_y
								);
							}
							
							var endseg = drawn_path.createSVGPathSegClosePath();
							seglist.appendItem(newseg);
							seglist.appendItem(endseg);
						} else if(len < 3) {
							keep = false;
							return keep;
						}
						$(stretchy).remove();
						
						// this will signal to commit the path
						element = newpath;
						drawn_path = null;
						started = false;
						
						if(subpath) {
							if(svgedit.path.path.matrix) {
								remapElement(newpath, {}, svgedit.path.path.matrix.inverse());
							}
						
							var new_d = newpath.getAttribute("d");
							var orig_d = $(svgedit.path.path.elem).attr("d");
							$(svgedit.path.path.elem).attr("d", orig_d + new_d);
							$(newpath).remove();
							if(svgedit.path.path.matrix) {
								svgedit.path.recalcRotatedPath();
							}
							svgedit.path.path.init();
							pathActions.toEditMode(svgedit.path.path.elem);
							svgedit.path.path.selectPt();
							return false;
						}
					}
					// else, create a new point, update path element
					else {
						// Checks if current target or parents are #svgcontent
						if(!$.contains(container, getMouseTarget(evt))) {
							// Clicked outside canvas, so don't make point
							console.log("Clicked outside canvas");
							return false;
						}

						var num = drawn_path.pathSegList.numberOfItems;
						var last = drawn_path.pathSegList.getItem(num -1);
						var lastx = last.x, lasty = last.y;

						if(evt.shiftKey) { var xya = snapToAngle(lastx,lasty,x,y); x=xya.x; y=xya.y; }
						
						// Use the segment defined by stretchy
						var s_seg = stretchy.pathSegList.getItem(1);
						if(s_seg.pathSegType === 4) {
							var newseg = drawn_path.createSVGPathSegLinetoAbs(round(x), round(y));
						} else {
							var newseg = drawn_path.createSVGPathSegCurvetoCubicAbs(
								round(x),
								round(y),
								s_seg.x1 / current_zoom,
								s_seg.y1 / current_zoom,
								s_seg.x2 / current_zoom,
								s_seg.y2 / current_zoom
							);
						}
						
						drawn_path.pathSegList.appendItem(newseg);
						
						x *= current_zoom;
						y *= current_zoom;
						
						// set stretchy line to latest point
						stretchy.setAttribute('d', ['M', x, y, x, y].join(' '));
						var index = num;
						if(subpath) index += svgedit.path.path.segs.length;
						svgedit.path.addPointGrip(index, x, y);
					}
// 					keep = true;
				}
				
				return;
			}
			
			// TODO: Make sure current_path isn't null at this point
			if(!svgedit.path.path) return;
			
			svgedit.path.path.storeD();
			
			var id = evt.target.id;
			if (id.substr(0,14) == "pathpointgrip_") {
				// Select this point
				var cur_pt = svgedit.path.path.cur_pt = parseInt(id.substr(14));
				svgedit.path.path.dragging = [start_x, start_y];
				var seg = svgedit.path.path.segs[cur_pt];
				
				// only clear selection if shift is not pressed (otherwise, add 
				// node to selection)
				if (!evt.shiftKey) {
					if(svgedit.path.path.selected_pts.length <= 1 || !seg.selected) {
						svgedit.path.path.clearSelection();
					}
					svgedit.path.path.addPtsToSelection(cur_pt);
				} else if(seg.selected) {
					svgedit.path.path.removePtFromSelection(cur_pt);
				} else {
					svgedit.path.path.addPtsToSelection(cur_pt);
				}
			} else if(id.indexOf("ctrlpointgrip_") == 0) {
				svgedit.path.path.dragging = [start_x, start_y];
				
				var parts = id.split('_')[1].split('c');
				var cur_pt = parts[0]-0;
				var ctrl_num = parts[1]-0;
				svgedit.path.path.selectPt(cur_pt, ctrl_num);
			}

			// Start selection box
			if(!svgedit.path.path.dragging) {
				if (rubberBox == null) {
					rubberBox = selectorManager.getRubberBandBox();
				}
				assignAttributes(rubberBox, {
						'x': start_x * current_zoom,
						'y': start_y * current_zoom,
						'width': 0,
						'height': 0,
						'display': 'inline'
				}, 100);
			}
		},
		mouseMove: function(mouse_x, mouse_y) {
			hasMoved = true;
			if(current_mode === "path") {
				if(!drawn_path) return;
				var seglist = drawn_path.pathSegList;
				var index = seglist.numberOfItems - 1;

				if(newPoint) {
					// First point
// 					if(!index) return;

					// Set control points
					var pointGrip1 = svgedit.path.addCtrlGrip('1c1');
					var pointGrip2 = svgedit.path.addCtrlGrip('0c2');
					
					// dragging pointGrip1
					pointGrip1.setAttribute('cx', mouse_x);
					pointGrip1.setAttribute('cy', mouse_y);
					pointGrip1.setAttribute('display', 'inline');

					var pt_x = newPoint[0];
					var pt_y = newPoint[1];
					
					// set curve
					var seg = seglist.getItem(index);
					var cur_x = mouse_x / current_zoom;
					var cur_y = mouse_y / current_zoom;
					var alt_x = (pt_x + (pt_x - cur_x));
					var alt_y = (pt_y + (pt_y - cur_y));
					
					pointGrip2.setAttribute('cx', alt_x * current_zoom);
					pointGrip2.setAttribute('cy', alt_y * current_zoom);
					pointGrip2.setAttribute('display', 'inline');
					
					var ctrlLine = svgedit.path.getCtrlLine(1);
					assignAttributes(ctrlLine, {
						x1: mouse_x,
						y1: mouse_y,
						x2: alt_x * current_zoom,
						y2: alt_y * current_zoom,
						display: 'inline'
					});

					if(index === 0) {
						firstCtrl = [mouse_x, mouse_y];
					} else {
						var last_x, last_y;
						
						var last = seglist.getItem(index - 1);
						var last_x = last.x;
						var last_y = last.y
	
						if(last.pathSegType === 6) {
							last_x += (last_x - last.x2);
							last_y += (last_y - last.y2);
						} else if(firstCtrl) {
							last_x = firstCtrl[0]/current_zoom;
							last_y = firstCtrl[1]/current_zoom;
						}
						svgedit.path.replacePathSeg(6, index, [pt_x, pt_y, last_x, last_y, alt_x, alt_y], drawn_path);
					}
				} else {
					var stretchy = getElem("path_stretch_line");
					if (stretchy) {
						var prev = seglist.getItem(index);
						if(prev.pathSegType === 6) {
							var prev_x = prev.x + (prev.x - prev.x2);
							var prev_y = prev.y + (prev.y - prev.y2);
							svgedit.path.replacePathSeg(6, 1, [mouse_x, mouse_y, prev_x * current_zoom, prev_y * current_zoom, mouse_x, mouse_y], stretchy);							
						} else if(firstCtrl) {
							svgedit.path.replacePathSeg(6, 1, [mouse_x, mouse_y, firstCtrl[0], firstCtrl[1], mouse_x, mouse_y], stretchy);
						} else {
							svgedit.path.replacePathSeg(4, 1, [mouse_x, mouse_y], stretchy);
						}
					}
				}
				return;
			}
			// if we are dragging a point, let's move it
			if (svgedit.path.path.dragging) {
				var pt = svgedit.path.getPointFromGrip({
					x: svgedit.path.path.dragging[0],
					y: svgedit.path.path.dragging[1]
				}, svgedit.path.path);
				var mpt = svgedit.path.getPointFromGrip({
					x: mouse_x,
					y: mouse_y
				}, svgedit.path.path);
				var diff_x = mpt.x - pt.x;
				var diff_y = mpt.y - pt.y;
				svgedit.path.path.dragging = [mouse_x, mouse_y];
				
				if(svgedit.path.path.dragctrl) {
					svgedit.path.path.moveCtrl(diff_x, diff_y);
				} else {
					svgedit.path.path.movePts(diff_x, diff_y);
				}
			} else {
				svgedit.path.path.selected_pts = [];
				svgedit.path.path.eachSeg(function(i) {
					var seg = this;
					if(!seg.next && !seg.prev) return;
						
					var item = seg.item;
					var rbb = rubberBox.getBBox();
					
					var pt = svgedit.path.getGripPt(seg);
					var pt_bb = {
						x: pt.x,
						y: pt.y,
						width: 0,
						height: 0
					};
				
					var sel = svgedit.math.rectsIntersect(rbb, pt_bb);

					this.select(sel);
					//Note that addPtsToSelection is not being run
					if(sel) svgedit.path.path.selected_pts.push(seg.index);
				});

			}
		}, 
		mouseUp: function(evt, element, mouse_x, mouse_y) {
			
			// Create mode
			if(current_mode === "path") {
				newPoint = null;
				if(!drawn_path) {
					element = getElem(getId());
					started = false;
					firstCtrl = null;
				}

				return {
					keep: true,
					element: element
				}
			}
			
			// Edit mode
			
			if (svgedit.path.path.dragging) {
				var last_pt = svgedit.path.path.cur_pt;

				svgedit.path.path.dragging = false;
				svgedit.path.path.dragctrl = false;
				svgedit.path.path.update();
				
			
				if(hasMoved) {
					svgedit.path.path.endChanges("Move path point(s)");
				} 
				
				if(!evt.shiftKey && !hasMoved) {
					svgedit.path.path.selectPt(last_pt);
				} 
			}
			else if(rubberBox && rubberBox.getAttribute('display') != 'none') {
				// Done with multi-node-select
				rubberBox.setAttribute("display", "none");
				
				if(rubberBox.getAttribute('width') <= 2 && rubberBox.getAttribute('height') <= 2) {
					pathActions.toSelectMode(evt.target);
				}
				
			// else, move back to select mode	
			} else {
				pathActions.toSelectMode(evt.target);
			}
			hasMoved = false;
		},
		toEditMode: function(element) {
			svgedit.path.path = svgedit.path.getPath_(element);
			current_mode = "pathedit";
			clearSelection();
			svgedit.path.path.show(true).update();
			svgedit.path.path.oldbbox = svgedit.utilities.getBBox(svgedit.path.path.elem);
			subpath = false;
		},
		toSelectMode: function(elem) {
			var selPath = (elem == svgedit.path.path.elem);
			current_mode = "select";
			svgedit.path.path.show(false);
			current_path = false;
			clearSelection();
			
			if(svgedit.path.path.matrix) {
				// Rotated, so may need to re-calculate the center
				svgedit.path.recalcRotatedPath();
			}
			
			if(selPath) {
				call("selected", [elem]);
				addToSelection([elem], true);
			}
		},
		addSubPath: function(on) {
			if(on) {
				// Internally we go into "path" mode, but in the UI it will
				// still appear as if in "pathedit" mode.
				current_mode = "path";
				subpath = true;
			} else {
				pathActions.clear(true);
				pathActions.toEditMode(svgedit.path.path.elem);
			}
		},
		select: function(target) {
			if (current_path === target) {
				pathActions.toEditMode(target);
				current_mode = "pathedit";
			} // going into pathedit mode
			else {
				current_path = target;
			}	
		},
		reorient: function() {
			var elem = selectedElements[0];
			if(!elem) return;
			var angle = getRotationAngle(elem);
			if(angle == 0) return;
			
			var batchCmd = new BatchCommand("Reorient path");
			var changes = {
				d: elem.getAttribute('d'),
				transform: elem.getAttribute('transform')
			};
			batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
			clearSelection();
			this.resetOrientation(elem);
			
			addCommandToHistory(batchCmd);

			// Set matrix to null
			svgedit.path.getPath_(elem).show(false).matrix = null; 

			this.clear();
	
			addToSelection([elem], true);
			call("changed", selectedElements);
		},
		
		clear: function(remove) {
			current_path = null;
			if (drawn_path) {
				var elem = getElem(getId());
				$(getElem("path_stretch_line")).remove();
				$(elem).remove();
				$(getElem("pathpointgrip_container")).find('*').attr('display', 'none');
				drawn_path = firstCtrl = null;
				started = false;
			} else if (current_mode == "pathedit") {
				this.toSelectMode();
			}
			if(svgedit.path.path) svgedit.path.path.init().show(false);
		},
		resetOrientation: function(path) {
			if(path == null || path.nodeName != 'path') return false;
			var tlist = getTransformList(path);
			var m = transformListToTransform(tlist).matrix;
			tlist.clear();
			path.removeAttribute("transform");
			var segList = path.pathSegList;
			
			// Opera/win/non-EN throws an error here.
			// TODO: Find out why!
			// Presumed fixed in Opera 10.5, so commented out for now
			
// 			try {
				var len = segList.numberOfItems;
// 			} catch(err) {
// 				var fixed_d = pathActions.convertPath(path);
// 				path.setAttribute('d', fixed_d);
// 				segList = path.pathSegList;
// 				var len = segList.numberOfItems;
// 			}
			var last_x, last_y;


			for (var i = 0; i < len; ++i) {
				var seg = segList.getItem(i);
				var type = seg.pathSegType;
				if(type == 1) continue;
				var pts = [];
				$.each(['',1,2], function(j, n) {
					var x = seg['x'+n], y = seg['y'+n];
					if(x !== undefined && y !== undefined) {
						var pt = transformPoint(x, y, m);
						pts.splice(pts.length, 0, pt.x, pt.y);
					}
				});
				svgedit.path.replacePathSeg(type, i, pts, path);
			}
			
			reorientGrads(path, m);


		},
		zoomChange: function() {
			if(current_mode == "pathedit") {
				svgedit.path.path.update();
			}
		},
		getNodePoint: function() {
			var sel_pt = svgedit.path.path.selected_pts.length ? svgedit.path.path.selected_pts[0] : 1;

			var seg = svgedit.path.path.segs[sel_pt];
			return {
				x: seg.item.x,
				y: seg.item.y,
				type: seg.type
			};
		}, 
		linkControlPoints: function(linkPoints) {
			svgedit.path.setLinkControlPoints(linkPoints);
		},
		clonePathNode: function() {
			svgedit.path.path.storeD();
			
			var sel_pts = svgedit.path.path.selected_pts;
			var segs = svgedit.path.path.segs;
			
			var i = sel_pts.length;
			var nums = [];

			while(i--) {
				var pt = sel_pts[i];
				svgedit.path.path.addSeg(pt);
				
				nums.push(pt + i);
				nums.push(pt + i + 1);
			}
			svgedit.path.path.init().addPtsToSelection(nums);

			svgedit.path.path.endChanges("Clone path node(s)");
		},
		opencloseSubPath: function() {
			var sel_pts = svgedit.path.path.selected_pts;
			// Only allow one selected node for now
			if(sel_pts.length !== 1) return;
			
			var elem = svgedit.path.path.elem;
			var list = elem.pathSegList;

			var len = list.numberOfItems;

			var index = sel_pts[0];
			
			var open_pt = null;
			var start_item = null;

			// Check if subpath is already open
			svgedit.path.path.eachSeg(function(i) {
				if(this.type === 2 && i <= index) {
					start_item = this.item;
				}
				if(i <= index) return true;
				if(this.type === 2) {
					// Found M first, so open
					open_pt = i;
					return false;
				} else if(this.type === 1) {
					// Found Z first, so closed
					open_pt = false;
					return false;
				}
			});
			
			if(open_pt == null) {
				// Single path, so close last seg
				open_pt = svgedit.path.path.segs.length - 1;
			}

			if(open_pt !== false) {
				// Close this path
				
				// Create a line going to the previous "M"
				var newseg = elem.createSVGPathSegLinetoAbs(start_item.x, start_item.y);
			
				var closer = elem.createSVGPathSegClosePath();
				if(open_pt == svgedit.path.path.segs.length - 1) {
					list.appendItem(newseg);
					list.appendItem(closer);
				} else {
					svgedit.path.insertItemBefore(elem, closer, open_pt);
					svgedit.path.insertItemBefore(elem, newseg, open_pt);
				}
				
				svgedit.path.path.init().selectPt(open_pt+1);
				return;
			}
			
			

			// M 1,1 L 2,2 L 3,3 L 1,1 z // open at 2,2
			// M 2,2 L 3,3 L 1,1
			
			// M 1,1 L 2,2 L 1,1 z M 4,4 L 5,5 L6,6 L 5,5 z 
			// M 1,1 L 2,2 L 1,1 z [M 4,4] L 5,5 L(M)6,6 L 5,5 z 
			
			var seg = svgedit.path.path.segs[index];
			
			if(seg.mate) {
				list.removeItem(index); // Removes last "L"
				list.removeItem(index); // Removes the "Z"
				svgedit.path.path.init().selectPt(index - 1);
				return;
			}
			
			var last_m, z_seg;
			
			// Find this sub-path's closing point and remove
			for(var i=0; i<list.numberOfItems; i++) {
				var item = list.getItem(i);

				if(item.pathSegType === 2) {
					// Find the preceding M
					last_m = i;
				} else if(i === index) {
					// Remove it
					list.removeItem(last_m);
// 						index--;
				} else if(item.pathSegType === 1 && index < i) {
					// Remove the closing seg of this subpath
					z_seg = i-1;
					list.removeItem(i);
					break;
				}
			}
			
			var num = (index - last_m) - 1;
			
			while(num--) {
				svgedit.path.insertItemBefore(elem, list.getItem(last_m), z_seg);
			}
			
			var pt = list.getItem(last_m);
			
			// Make this point the new "M"
			svgedit.path.replacePathSeg(2, last_m, [pt.x, pt.y]);
			
			var i = index
			
			svgedit.path.path.init().selectPt(0);
		},
		deletePathNode: function() {
			if(!pathActions.canDeleteNodes) return;
			svgedit.path.path.storeD();
			
			var sel_pts = svgedit.path.path.selected_pts;
			var i = sel_pts.length;

			while(i--) {
				var pt = sel_pts[i];
				svgedit.path.path.deleteSeg(pt);
			}
			
			// Cleanup
			var cleanup = function() {
				var segList = svgedit.path.path.elem.pathSegList;
				var len = segList.numberOfItems;
				
				var remItems = function(pos, count) {
					while(count--) {
						segList.removeItem(pos);
					}
				}

				if(len <= 1) return true;
				
				while(len--) {
					var item = segList.getItem(len);
					if(item.pathSegType === 1) {
						var prev = segList.getItem(len-1);
						var nprev = segList.getItem(len-2);
						if(prev.pathSegType === 2) {
							remItems(len-1, 2);
							cleanup();
							break;
						} else if(nprev.pathSegType === 2) {
							remItems(len-2, 3);
							cleanup();
							break;
						}

					} else if(item.pathSegType === 2) {
						if(len > 0) {
							var prev_type = segList.getItem(len-1).pathSegType;
							// Path has M M  
							if(prev_type === 2) {
								remItems(len-1, 1);
								cleanup();
								break;
							// Entire path ends with Z M 
							} else if(prev_type === 1 && segList.numberOfItems-1 === len) {
								remItems(len, 1);
								cleanup();
								break;
							}
						}
					}
				}	
				return false;
			}
			
			cleanup();
			
			// Completely delete a path with 1 or 0 segments
			if(svgedit.path.path.elem.pathSegList.numberOfItems <= 1) {
				pathActions.toSelectMode(svgedit.path.path.elem);
				canvas.deleteSelectedElements();
				return;
			}
			
			svgedit.path.path.init();
			
			svgedit.path.path.clearSelection();
			
			// TODO: Find right way to select point now
			// path.selectPt(sel_pt);
			if(window.opera) { // Opera repaints incorrectly
				var cp = $(svgedit.path.path.elem); cp.attr('d',cp.attr('d'));
			}
			svgedit.path.path.endChanges("Delete path node(s)");
		},
		smoothPolylineIntoPath: smoothPolylineIntoPath,
		setSegType: function(v) {
			svgedit.path.path.setSegType(v);
		},
		moveNode: function(attr, newValue) {
			var sel_pts = svgedit.path.path.selected_pts;
			if(!sel_pts.length) return;
			
			svgedit.path.path.storeD();
			
			// Get first selected point
			var seg = svgedit.path.path.segs[sel_pts[0]];
			var diff = {x:0, y:0};
			diff[attr] = newValue - seg.item[attr];
			
			seg.move(diff.x, diff.y);
			svgedit.path.path.endChanges("Move path point");
		},
		fixEnd: function(elem) {
			// Adds an extra segment if the last seg before a Z doesn't end
			// at its M point
			// M0,0 L0,100 L100,100 z
			var segList = elem.pathSegList;
			var len = segList.numberOfItems;
			var last_m;
			for (var i = 0; i < len; ++i) {
				var item = segList.getItem(i);
				if(item.pathSegType === 2) {
					last_m = item;
				}
				
				if(item.pathSegType === 1) {
					var prev = segList.getItem(i-1);
					if(prev.x != last_m.x || prev.y != last_m.y) {
						// Add an L segment here
						var newseg = elem.createSVGPathSegLinetoAbs(last_m.x, last_m.y);
						svgedit.path.insertItemBefore(elem, newseg, i);
						// Can this be done better?
						pathActions.fixEnd(elem);
						break;
					}
					
				}
			}
			if(svgedit.browser.isWebkit()) resetD(elem);
		},
		// Convert a path to one with only absolute or relative values
		convertPath: function(path, toRel) {
			var segList = path.pathSegList;
			var len = segList.numberOfItems;
			var curx = 0, cury = 0;
			var d = "";
			var last_m = null;
			
			for (var i = 0; i < len; ++i) {
				var seg = segList.getItem(i);
				// if these properties are not in the segment, set them to zero
				var x = seg.x || 0,
					y = seg.y || 0,
					x1 = seg.x1 || 0,
					y1 = seg.y1 || 0,
					x2 = seg.x2 || 0,
					y2 = seg.y2 || 0;
	
				var type = seg.pathSegType;
				var letter = pathMap[type]['to'+(toRel?'Lower':'Upper')+'Case']();
				
				var addToD = function(pnts, more, last) {
					var str = '';
					var more = more?' '+more.join(' '):'';
					var last = last?' '+svgedit.units.shortFloat(last):'';
					$.each(pnts, function(i, pnt) {
						pnts[i] = svgedit.units.shortFloat(pnt);
					});
					d += letter + pnts.join(' ') + more + last;
				}
				
				switch (type) {
					case 1: // z,Z closepath (Z/z)
						d += "z";
						break;
					case 12: // absolute horizontal line (H)
						x -= curx;
					case 13: // relative horizontal line (h)
						if(toRel) {
							curx += x;
							letter = 'l';
						} else {
							x += curx;
							curx = x;
							letter = 'L';
						}
						// Convert to "line" for easier editing
						addToD([[x, cury]]);
						break;
					case 14: // absolute vertical line (V)
						y -= cury;
					case 15: // relative vertical line (v)
						if(toRel) {
							cury += y;
							letter = 'l';
						} else {
							y += cury;
							cury = y;
							letter = 'L';
						}
						// Convert to "line" for easier editing
						addToD([[curx, y]]);
						break;
					case 2: // absolute move (M)
					case 4: // absolute line (L)
					case 18: // absolute smooth quad (T)
						x -= curx;
						y -= cury;
					case 5: // relative line (l)
					case 3: // relative move (m)
						// If the last segment was a "z", this must be relative to 
						if(last_m && segList.getItem(i-1).pathSegType === 1 && !toRel) {
							curx = last_m[0];
							cury = last_m[1];
						}
					
					case 19: // relative smooth quad (t)
						if(toRel) {
							curx += x;
							cury += y;
						} else {
							x += curx;
							y += cury;
							curx = x;
							cury = y;
						}
						if(type === 3) last_m = [curx, cury];
						
						addToD([[x,y]]);
						break;
					case 6: // absolute cubic (C)
						x -= curx; x1 -= curx; x2 -= curx;
						y -= cury; y1 -= cury; y2 -= cury;
					case 7: // relative cubic (c)
						if(toRel) {
							curx += x;
							cury += y;
						} else {
							x += curx; x1 += curx; x2 += curx;
							y += cury; y1 += cury; y2 += cury;
							curx = x;
							cury = y;
						}
						addToD([[x1,y1],[x2,y2],[x,y]]);
						break;
					case 8: // absolute quad (Q)
						x -= curx; x1 -= curx;
						y -= cury; y1 -= cury;
					case 9: // relative quad (q) 
						if(toRel) {
							curx += x;
							cury += y;
						} else {
							x += curx; x1 += curx;
							y += cury; y1 += cury;
							curx = x;
							cury = y;
						}
						addToD([[x1,y1],[x,y]]);
						break;
					case 10: // absolute elliptical arc (A)
						x -= curx;
						y -= cury;
					case 11: // relative elliptical arc (a)
						if(toRel) {
							curx += x;
							cury += y;
						} else {
							x += curx;
							y += cury;
							curx = x;
							cury = y;
						}
						addToD([[seg.r1,seg.r2]], [
								seg.angle,
								(seg.largeArcFlag ? 1 : 0),
								(seg.sweepFlag ? 1 : 0)
							],[x,y]
						);
						break;
					case 16: // absolute smooth cubic (S)
						x -= curx; x2 -= curx;
						y -= cury; y2 -= cury;
					case 17: // relative smooth cubic (s)
						if(toRel) {
							curx += x;
							cury += y;
						} else {
							x += curx; x2 += curx;
							y += cury; y2 += cury;
							curx = x;
							cury = y;
						}
						addToD([[x2,y2],[x,y]]);
						break;
				} // switch on path segment type
			} // for each segment
			return d;
		}
	}
}();
// end pathActions

// Group: Serialization

// Function: removeUnusedDefElems
// Looks at DOM elements inside the <defs> to see if they are referred to,
// removes them from the DOM if they are not.
// 
// Returns:
// The amount of elements that were removed
var removeUnusedDefElems = this.removeUnusedDefElems = function() {
	var defs = svgcontent.getElementsByTagNameNS(svgns, "defs");
	if(!defs || !defs.length) return 0;
	
// 	if(!defs.firstChild) return;
	
	var defelem_uses = [],
		numRemoved = 0;
	var attrs = ['fill', 'stroke', 'filter', 'marker-start', 'marker-mid', 'marker-end'];
	var alen = attrs.length;
	
	var all_els = svgcontent.getElementsByTagNameNS(svgns, '*');
	var all_len = all_els.length;
	
	for(var i=0; i<all_len; i++) {
		var el = all_els[i];
		for(var j = 0; j < alen; j++) {
			var ref = getUrlFromAttr(el.getAttribute(attrs[j]));
			if(ref) {
				defelem_uses.push(ref.substr(1));
			}
		}
		
		// gradients can refer to other gradients
		var href = getHref(el);
		if (href && href.indexOf('#') === 0) {
			defelem_uses.push(href.substr(1));
		}
	};
	
	var defelems = $(defs).find("linearGradient, radialGradient, filter, marker, svg, symbol");
		defelem_ids = [],
		i = defelems.length;
	while (i--) {
		var defelem = defelems[i];
		var id = defelem.id;
		if(defelem_uses.indexOf(id) < 0) {
			// Not found, so remove (but remember)
			removedElements[id] = defelem;
			defelem.parentNode.removeChild(defelem);
			numRemoved++;
		}
	}

	return numRemoved;
}

// Function: svgCanvasToString
// Main function to set up the SVG content for output 
//
// Returns: 
// String containing the SVG image for output
this.svgCanvasToString = function() {
	// keep calling it until there are none to remove
	while (removeUnusedDefElems() > 0) {};
	
	pathActions.clear(true);
	
	// Keep SVG-Edit comment on top
	$.each(svgcontent.childNodes, function(i, node) {
		if(i && node.nodeType === 8 && node.data.indexOf('Created with') >= 0) {
			svgcontent.insertBefore(node, svgcontent.firstChild);
		}
	});
	
	// Move out of in-group editing mode
	if(current_group) {
		leaveContext();
		selectOnly([current_group]);
	}
	
	var naked_svgs = [];
	
	// Unwrap gsvg if it has no special attributes (only id and style)
	$(svgcontent).find('g:data(gsvg)').each(function() {
		var attrs = this.attributes;
		var len = attrs.length;
		for(var i=0; i<len; i++) {
			if(attrs[i].nodeName == 'id' || attrs[i].nodeName == 'style') {
				len--;
			}
		}
		// No significant attributes, so ungroup
		if(len <= 0) {
			var svg = this.firstChild;
			naked_svgs.push(svg);
			$(this).replaceWith(svg);
		}
	});
	var output = this.svgToString(svgcontent, 0);
	
	// Rewrap gsvg
	if(naked_svgs.length) {
		$(naked_svgs).each(function() {
			groupSvgElem(this);
		});
	}
	
	return output;
};

// Function: svgToString
// Sub function ran on each SVG element to convert it to a string as desired
// 
// Parameters: 
// elem - The SVG element to convert
// indent - Integer with the amount of spaces to indent this tag
//
// Returns: 
// String with the given element as an SVG tag
this.svgToString = function(elem, indent) {
	var out = new Array(), toXml = svgedit.utilities.toXml;
	var unit = curConfig.baseUnit;
	var unit_re = new RegExp('^-?[\\d\\.]+' + unit + '$');

	if (elem) {
		cleanupElement(elem);
		var attrs = elem.attributes,
			attr,
			i,
			childs = elem.childNodes;
		
		for (var i=0; i<indent; i++) out.push(" ");
		out.push("<"); out.push(elem.nodeName);
		if(elem.id === 'svgcontent') {
			// Process root element separately
			var res = getResolution();
			
			var vb = "";
			// TODO: Allow this by dividing all values by current baseVal
			// Note that this also means we should properly deal with this on import
// 			if(curConfig.baseUnit !== "px") {
// 				var unit = curConfig.baseUnit;
// 				var unit_m = svgedit.units.getTypeMap()[unit];
// 				res.w = svgedit.units.shortFloat(res.w / unit_m)
// 				res.h = svgedit.units.shortFloat(res.h / unit_m)
// 				vb = ' viewBox="' + [0, 0, res.w, res.h].join(' ') + '"';				
// 				res.w += unit;
// 				res.h += unit;
// 			}
			
			if(unit !== "px") {
				res.w = svgedit.units.convertUnit(res.w, unit) + unit;
				res.h = svgedit.units.convertUnit(res.h, unit) + unit;
			}
			
			out.push(' width="' + res.w + '" height="' + res.h + '"' + vb + ' xmlns="'+svgns+'"');
			
			var nsuris = {};
			
			// Check elements for namespaces, add if found
			$(elem).find('*').andSelf().each(function() {
				var el = this;
				$.each(this.attributes, function(i, attr) {
					var uri = attr.namespaceURI;
					if(uri && !nsuris[uri] && nsMap[uri] !== 'xmlns' && nsMap[uri] !== 'xml' ) {
						nsuris[uri] = true;
						out.push(" xmlns:" + nsMap[uri] + '="' + uri +'"');
					}
				});
			});
			
			var i = attrs.length;
			var attr_names = ['width','height','xmlns','x','y','viewBox','id','overflow'];
			while (i--) {
				attr = attrs.item(i);
				var attrVal = toXml(attr.nodeValue);
				
				// Namespaces have already been dealt with, so skip
				if(attr.nodeName.indexOf('xmlns:') === 0) continue;

				// only serialize attributes we don't use internally
				if (attrVal != "" && attr_names.indexOf(attr.localName) == -1) 
				{

					if(!attr.namespaceURI || nsMap[attr.namespaceURI]) {
						out.push(' '); 
						out.push(attr.nodeName); out.push("=\"");
						out.push(attrVal); out.push("\"");
					}
				}
			}
		} else {
			// Skip empty defs
			if(elem.nodeName === 'defs' && !elem.firstChild) return;
		
			var moz_attrs = ['-moz-math-font-style', '_moz-math-font-style'];
			for (var i=attrs.length-1; i>=0; i--) {
				attr = attrs.item(i);
				var attrVal = toXml(attr.nodeValue);
				//remove bogus attributes added by Gecko
				if (moz_attrs.indexOf(attr.localName) >= 0) continue;
				if (attrVal != "") {
					if(attrVal.indexOf('pointer-events') === 0) continue;
					if(attr.localName === "class" && attrVal.indexOf('se_') === 0) continue;
					out.push(" "); 
					if(attr.localName === 'd') attrVal = pathActions.convertPath(elem, true);
					if(!isNaN(attrVal)) {
						attrVal = svgedit.units.shortFloat(attrVal);
					} else if(unit_re.test(attrVal)) {
						attrVal = svgedit.units.shortFloat(attrVal) + unit;
					}
					
					// Embed images when saving 
					if(save_options.apply
						&& elem.nodeName === 'image' 
						&& attr.localName === 'href'
						&& save_options.images
						&& save_options.images === 'embed') 
					{
						var img = encodableImages[attrVal];
						if(img) attrVal = img;
					}
					
					// map various namespaces to our fixed namespace prefixes
					// (the default xmlns attribute itself does not get a prefix)
					if(!attr.namespaceURI || attr.namespaceURI == svgns || nsMap[attr.namespaceURI]) {
						out.push(attr.nodeName); out.push("=\"");
						out.push(attrVal); out.push("\"");
					}
				}
			}
		}

		if (elem.hasChildNodes()) {
			out.push(">");
			indent++;
			var bOneLine = false;
			
			for (var i=0; i<childs.length; i++)
			{
				var child = childs.item(i);
				switch(child.nodeType) {
				case 1: // element node
					out.push("\n");
					out.push(this.svgToString(childs.item(i), indent));
					break;
				case 3: // text node
					var str = child.nodeValue.replace(/^\s+|\s+$/g, "");
					if (str != "") {
						bOneLine = true;
						out.push(toXml(str) + "");
					}
					break;
				case 4: // cdata node
					out.push("\n");
					out.push(new Array(indent+1).join(" "));
					out.push("<![CDATA[");
					out.push(child.nodeValue);
					out.push("]]>");
					break;
				case 8: // comment
					out.push("\n");
					out.push(new Array(indent+1).join(" "));
					out.push("<!--");
					out.push(child.data);
					out.push("-->");
					break;
				} // switch on node type
			}
			indent--;
			if (!bOneLine) {
				out.push("\n");
				for (var i=0; i<indent; i++) out.push(" ");
			}
			out.push("</"); out.push(elem.nodeName); out.push(">");
		} else {
			out.push("/>");
		}
	}
	return out.join('');
}; // end svgToString()

// Function: embedImage
// Converts a given image file to a data URL when possible, then runs a given callback
//
// Parameters: 
// val - String with the path/URL of the image
// callback - Optional function to run when image data is found, supplies the
// result (data URL or false) as first parameter.
this.embedImage = function(val, callback) {

	// load in the image and once it's loaded, get the dimensions
	$(new Image()).load(function() {
		// create a canvas the same size as the raster image
		var canvas = document.createElement("canvas");
		canvas.width = this.width;
		canvas.height = this.height;
		// load the raster image into the canvas
		canvas.getContext("2d").drawImage(this,0,0);
		// retrieve the data: URL
		try {
			var urldata = ';svgedit_url=' + encodeURIComponent(val);
			urldata = canvas.toDataURL().replace(';base64',urldata+';base64');
			encodableImages[val] = urldata;
		} catch(e) {
			encodableImages[val] = false;
		}
		last_good_img_url = val;
		if(callback) callback(encodableImages[val]);
	}).attr('src',val);
}

// Function: setGoodImage
// Sets a given URL to be a "last good image" URL
this.setGoodImage = function(val) {
	last_good_img_url = val;
}

this.open = function() {
	// Nothing by default, handled by optional widget/extension
};

// Function: save
// Serializes the current drawing into SVG XML text and returns it to the 'saved' handler.
// This function also includes the XML prolog.  Clients of the SvgCanvas bind their save
// function to the 'saved' event.
//
// Returns: 
// Nothing
this.save = function(opts) {
	// remove the selected outline before serializing
	clearSelection();
	// Update save options if provided
	if(opts) $.extend(save_options, opts);
	save_options.apply = true;
	
	// no need for doctype, see http://jwatt.org/svg/authoring/#doctype-declaration
	var str = this.svgCanvasToString();
	call("saved", str);
};

// Function: rasterExport
// Generates a PNG Data URL based on the current image, then calls "exported" 
// with an object including the string and any issues found
this.rasterExport = function() {
	// remove the selected outline before serializing
	clearSelection();
	
	// Check for known CanVG issues 
	var issues = [];
	
	// Selector and notice
	var issue_list = {
		'feGaussianBlur': uiStrings.exportNoBlur,
		'foreignObject': uiStrings.exportNoforeignObject,
		'[stroke-dasharray]': uiStrings.exportNoDashArray
	};
	var content = $(svgcontent);
	
	// Add font/text check if Canvas Text API is not implemented
	if(!("font" in $('<canvas>')[0].getContext('2d'))) {
		issue_list['text'] = uiStrings.exportNoText;
	}
	
	$.each(issue_list, function(sel, descr) {
		if(content.find(sel).length) {
			issues.push(descr);
		}
	});

	var str = this.svgCanvasToString();
	call("exported", {svg: str, issues: issues});
};

// Function: getSvgString
// Returns the current drawing as raw SVG XML text.
//
// Returns:
// The current drawing as raw SVG XML text.
this.getSvgString = function() {
	save_options.apply = false;
	return this.svgCanvasToString();
};

// Function: randomizeIds
// This function determines whether to use a nonce in the prefix, when
// generating IDs for future documents in SVG-Edit.
// 
//  Parameters:
//   an opional boolean, which, if true, adds a nonce to the prefix. Thus
//     svgCanvas.randomizeIds()  <==> svgCanvas.randomizeIds(true)
//
// if you're controlling SVG-Edit externally, and want randomized IDs, call
// this BEFORE calling svgCanvas.setSvgString
//
this.randomizeIds = function() {
	if (arguments.length > 0 && arguments[0] == false) {
		svgedit.draw.randomizeIds(false, getCurrentDrawing());
	} else {
		svgedit.draw.randomizeIds(true, getCurrentDrawing());
	}
};

// Function: uniquifyElems
// Ensure each element has a unique ID
//
// Parameters:
// g - The parent element of the tree to give unique IDs
var uniquifyElems = this.uniquifyElems = function(g) {
	var ids = {};
	// TODO: Handle markers and connectors.  These are not yet re-identified properly
	// as their referring elements do not get remapped.
	//
	// <marker id='se_marker_end_svg_7'/>
	// <polyline id='svg_7' se:connector='svg_1 svg_6' marker-end='url(#se_marker_end_svg_7)'/>
	// 
	// Problem #1: if svg_1 gets renamed, we do not update the polyline's se:connector attribute
	// Problem #2: if the polyline svg_7 gets renamed, we do not update the marker id nor the polyline's marker-end attribute
	var ref_elems = ["filter", "linearGradient", "pattern",	"radialGradient", "symbol", "textPath", "use"];
	
	svgedit.utilities.walkTree(g, function(n) {
		// if it's an element node
		if (n.nodeType == 1) {
			// and the element has an ID
			if (n.id) {
				// and we haven't tracked this ID yet
				if (!(n.id in ids)) {
					// add this id to our map
					ids[n.id] = {elem:null, attrs:[], hrefs:[]};
				}
				ids[n.id]["elem"] = n;
			}
			
			// now search for all attributes on this element that might refer
			// to other elements
			$.each(ref_attrs,function(i,attr) {
				var attrnode = n.getAttributeNode(attr);
				if (attrnode) {
					// the incoming file has been sanitized, so we should be able to safely just strip off the leading #
					var url = svgedit.utilities.getUrlFromAttr(attrnode.value),
						refid = url ? url.substr(1) : null;
					if (refid) {
						if (!(refid in ids)) {
							// add this id to our map
							ids[refid] = {elem:null, attrs:[], hrefs:[]};
						}
						ids[refid]["attrs"].push(attrnode);
					}
				}
			});
			
			// check xlink:href now
			var href = svgedit.utilities.getHref(n);
			// TODO: what if an <image> or <a> element refers to an element internally?
			if(href && ref_elems.indexOf(n.nodeName) >= 0)
			{
				var refid = href.substr(1);
				if (refid) {
					if (!(refid in ids)) {
						// add this id to our map
						ids[refid] = {elem:null, attrs:[], hrefs:[]};
					}
					ids[refid]["hrefs"].push(n);
				}
			}						
		}
	});
	
	// in ids, we now have a map of ids, elements and attributes, let's re-identify
	for (var oldid in ids) {
		if (!oldid) continue;
		var elem = ids[oldid]["elem"];
		if (elem) {
			var newid = getNextId();
			
			// assign element its new id
			elem.id = newid;
			
			// remap all url() attributes
			var attrs = ids[oldid]["attrs"];
			var j = attrs.length;
			while (j--) {
				var attr = attrs[j];
				attr.ownerElement.setAttribute(attr.name, "url(#" + newid + ")");
			}
			
			// remap all href attributes
			var hreffers = ids[oldid]["hrefs"];
			var k = hreffers.length;
			while (k--) {
				var hreffer = hreffers[k];
				svgedit.utilities.setHref(hreffer, "#"+newid);
			}
		}
	}
}

// Function setUseData
// Assigns reference data for each use element
var setUseData = this.setUseData = function(parent) {
	var elems = $(parent);
	
	if(parent.tagName !== 'use') {
		elems = elems.find('use');
	}
	
	elems.each(function() {
		var id = getHref(this).substr(1);
		var ref_elem = getElem(id);
		if(!ref_elem) return;
		$(this).data('ref', ref_elem);
		if(ref_elem.tagName == 'symbol' || ref_elem.tagName == 'svg') {
			$(this).data('symbol', ref_elem).data('ref', ref_elem);
		}
	});
}

// Function convertGradients
// Converts gradients from userSpaceOnUse to objectBoundingBox
var convertGradients = this.convertGradients = function(elem) {
	var elems = $(elem).find('linearGradient, radialGradient');
	if(!elems.length && svgedit.browser.isWebkit()) {
		// Bug in webkit prevents regular *Gradient selector search
		elems = $(elem).find('*').filter(function() {
			return (this.tagName.indexOf('Gradient') >= 0);
		});
	}
	
	elems.each(function() {
		var grad = this;
		if($(grad).attr('gradientUnits') === 'userSpaceOnUse') {
			// TODO: Support more than one element with this ref by duplicating parent grad
			var elems = $(svgcontent).find('[fill=url(#' + grad.id + ')],[stroke=url(#' + grad.id + ')]');
			if(!elems.length) return;
			
			// get object's bounding box
			var bb = svgedit.utilities.getBBox(elems[0]);
			
			// This will occur if the element is inside a <defs> or a <symbol>,
			// in which we shouldn't need to convert anyway.
			if(!bb) return;
			
			if(grad.tagName === 'linearGradient') {
				var g_coords = $(grad).attr(['x1', 'y1', 'x2', 'y2']);
				
				// If has transform, convert
				var tlist = grad.gradientTransform.baseVal;
				if(tlist && tlist.numberOfItems > 0) {
					var m = transformListToTransform(tlist).matrix;
					var pt1 = transformPoint(g_coords.x1, g_coords.y1, m);
					var pt2 = transformPoint(g_coords.x2, g_coords.y2, m);
					
					g_coords.x1 = pt1.x;
					g_coords.y1 = pt1.y;
					g_coords.x2 = pt2.x;
					g_coords.y2 = pt2.y;
					grad.removeAttribute('gradientTransform');
				}
				
				$(grad).attr({
					x1: (g_coords.x1 - bb.x) / bb.width,
					y1: (g_coords.y1 - bb.y) / bb.height,
					x2: (g_coords.x2 - bb.x) / bb.width,
					y2: (g_coords.y2 - bb.y) / bb.height
				});
				grad.removeAttribute('gradientUnits');
			} else {
				// Note: radialGradient elements cannot be easily converted 
				// because userSpaceOnUse will keep circular gradients, while
				// objectBoundingBox will x/y scale the gradient according to
				// its bbox. 
				
				// For now we'll do nothing, though we should probably have
				// the gradient be updated as the element is moved, as 
				// inkscape/illustrator do.
			
//         				var g_coords = $(grad).attr(['cx', 'cy', 'r']);
//         				
// 						$(grad).attr({
// 							cx: (g_coords.cx - bb.x) / bb.width,
// 							cy: (g_coords.cy - bb.y) / bb.height,
// 							r: g_coords.r
// 						});
// 						
// 	        			grad.removeAttribute('gradientUnits');
			}
			

		}
	});
}

// Function: convertToGroup
// Converts selected/given <use> or child SVG element to a group
var convertToGroup = this.convertToGroup = function(elem) {
	if(!elem) {
		elem = selectedElements[0];
	}
	var $elem = $(elem);
	
	var batchCmd = new BatchCommand();
	
	var ts;
	
	if($elem.data('gsvg')) {
		// Use the gsvg as the new group
		var svg = elem.firstChild;
		var pt = $(svg).attr(['x', 'y']);
		
		$(elem.firstChild.firstChild).unwrap();
		$(elem).removeData('gsvg');
		
		var tlist = getTransformList(elem);
		var xform = svgroot.createSVGTransform();
		xform.setTranslate(pt.x, pt.y);
		tlist.appendItem(xform);
		recalculateDimensions(elem);
		call("selected", [elem]);
	} else if($elem.data('symbol')) {
		elem = $elem.data('symbol');
		
		ts = $elem.attr('transform');
		var pos = $elem.attr(['x','y']);

		var vb = elem.getAttribute('viewBox');
		
		if(vb) {
			var nums = vb.split(' ');
			pos.x -= +nums[0];
			pos.y -= +nums[1];
		}
		
		// Not ideal, but works
		ts += " translate(" + (pos.x || 0) + "," + (pos.y || 0) + ")";
		
		var prev = $elem.prev();
		
		// Remove <use> element
		batchCmd.addSubCommand(new RemoveElementCommand($elem[0], $elem[0].nextSibling, $elem[0].parentNode));
		$elem.remove();
		
		// See if other elements reference this symbol
		var has_more = $(svgcontent).find('use:data(symbol)').length;
			
		var g = svgdoc.createElementNS(svgns, "g");
		var childs = elem.childNodes;
		
		for(var i = 0; i < childs.length; i++) {
			g.appendChild(childs[i].cloneNode(true));
		}
		
		// Duplicate the gradients for Gecko, since they weren't included in the <symbol>
		if(svgedit.browser.isGecko()) {
			var dupeGrads = $(findDefs()).children('linearGradient,radialGradient,pattern').clone();
			$(g).append(dupeGrads);
		}
		
		if (ts) {
			g.setAttribute("transform", ts);
		}
		
		var parent = elem.parentNode;
		
		uniquifyElems(g);
		
		// Put the dupe gradients back into <defs> (after uniquifying them)
		if(svgedit.browser.isGecko()) {
			$(findDefs()).append( $(g).find('linearGradient,radialGradient,pattern') );
		}
	
		// now give the g itself a new id
		g.id = getNextId();
		
		prev.after(g);
		
		if(parent) {
			if(!has_more) {
				// remove symbol/svg element
				var nextSibling = elem.nextSibling;
				parent.removeChild(elem);
				batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
			}
			batchCmd.addSubCommand(new InsertElementCommand(g));
		}
		
		setUseData(g);
		
		if(svgedit.browser.isGecko()) {
			convertGradients(findDefs());
		} else {
			convertGradients(g);
		}
		
		// recalculate dimensions on the top-level children so that unnecessary transforms
		// are removed
		svgedit.utilities.walkTreePost(g, function(n){try{recalculateDimensions(n)}catch(e){console.log(e)}});
		
		// Give ID for any visible element missing one
		$(g).find(visElems).each(function() {
			if(!this.id) this.id = getNextId();
		});
		
		selectOnly([g]);
		
		var cm = pushGroupProperties(g, true);
		if(cm) {
			batchCmd.addSubCommand(cm);
		}

		addCommandToHistory(batchCmd);
		
	} else {
		console.log('Unexpected element to ungroup:', elem);
	}
}

//   
// Function: setSvgString
// This function sets the current drawing as the input SVG XML.
//
// Parameters:
// xmlString - The SVG as XML text.
//
// Returns:
// This function returns false if the set was unsuccessful, true otherwise.
this.setSvgString = function(xmlString) {
	try {
		// convert string into XML document
		var newDoc = svgedit.utilities.text2xml(xmlString);

		this.prepareSvg(newDoc);

		var batchCmd = new BatchCommand("Change Source");

		// remove old svg document
		var nextSibling = svgcontent.nextSibling;
		var oldzoom = svgroot.removeChild(svgcontent);
		batchCmd.addSubCommand(new RemoveElementCommand(oldzoom, nextSibling, svgroot));
	
		// set new svg document
		// If DOM3 adoptNode() available, use it. Otherwise fall back to DOM2 importNode()
		if(svgdoc.adoptNode) {
			svgcontent = svgdoc.adoptNode(newDoc.documentElement);
		}
		else {
			svgcontent = svgdoc.importNode(newDoc.documentElement, true);
		}
		
		svgroot.appendChild(svgcontent);
		var content = $(svgcontent);
		
		canvas.current_drawing_ = new svgedit.draw.Drawing(svgcontent, idprefix);
		
		// retrieve or set the nonce
		var nonce = getCurrentDrawing().getNonce();
		if (nonce) {
			call("setnonce", nonce);
		} else {
			call("unsetnonce");
		}
		
		// change image href vals if possible
		content.find('image').each(function() {
			var image = this;
			preventClickDefault(image);
			var val = getHref(this);
			if(val.indexOf('data:') === 0) {
				// Check if an SVG-edit data URI
				var m = val.match(/svgedit_url=(.*?);/);
				if(m) {
					var url = decodeURIComponent(m[1]);
					$(new Image()).load(function() {
						image.setAttributeNS(xlinkns,'xlink:href',url);
					}).attr('src',url);
				}
			}
			// Add to encodableImages if it loads
			canvas.embedImage(val);
		});
	
		// Wrap child SVGs in group elements
		content.find('svg').each(function() {
			// Skip if it's in a <defs>
			if($(this).closest('defs').length) return;
		
			uniquifyElems(this);
		
			// Check if it already has a gsvg group
			var pa = this.parentNode;
			if(pa.childNodes.length === 1 && pa.nodeName === 'g') {
				$(pa).data('gsvg', this);
				pa.id = pa.id || getNextId();
			} else {
				groupSvgElem(this);
			}
		});
		
		// For Firefox: Put all paint elems in defs
		if(svgedit.browser.isGecko()) {
			content.find('linearGradient, radialGradient, pattern').appendTo(findDefs());
		}

		
		// Set ref element for <use> elements
		
		// TODO: This should also be done if the object is re-added through "redo"
		setUseData(content);
		
		convertGradients(content[0]);
		
		// recalculate dimensions on the top-level children so that unnecessary transforms
		// are removed
		svgedit.utilities.walkTreePost(svgcontent, function(n){try{recalculateDimensions(n)}catch(e){console.log(e)}});
		
		var attrs = {
			id: 'svgcontent',
			overflow: curConfig.show_outside_canvas?'visible':'hidden'
		};
		
		var percs = false;
		
		// determine proper size
		if (content.attr("viewBox")) {
			var vb = content.attr("viewBox").split(' ');
			attrs.width = vb[2];
			attrs.height = vb[3];
		}
		// handle content that doesn't have a viewBox
		else {
			$.each(['width', 'height'], function(i, dim) {
				// Set to 100 if not given
				var val = content.attr(dim);
				
				if(!val) val = '100%';
				
				if((val+'').substr(-1) === "%") {
					// Use user units if percentage given
					percs = true;
				} else {
					attrs[dim] = convertToNum(dim, val);
				}
			});
		}
		
		// identify layers
		identifyLayers();
		
		// Give ID for any visible layer children missing one
		content.children().find(visElems).each(function() {
			if(!this.id) this.id = getNextId();
		});
		
		// Percentage width/height, so let's base it on visible elements
		if(percs) {
			var bb = getStrokedBBox();
			attrs.width = bb.width + bb.x;
			attrs.height = bb.height + bb.y;
		}
		
		// Just in case negative numbers are given or 
		// result from the percs calculation
		if(attrs.width <= 0) attrs.width = 100;
		if(attrs.height <= 0) attrs.height = 100;
		
		content.attr(attrs);
		this.contentW = attrs['width'];
		this.contentH = attrs['height'];
		
		batchCmd.addSubCommand(new InsertElementCommand(svgcontent));
		// update root to the correct size
		var changes = content.attr(["width", "height"]);
		batchCmd.addSubCommand(new ChangeElementCommand(svgroot, changes));
		
		// reset zoom
		current_zoom = 1;
		
		// reset transform lists
		svgedit.transformlist.resetListMap();
		clearSelection();
		svgedit.path.clearData();
		svgroot.appendChild(selectorManager.selectorParentGroup);
		
		addCommandToHistory(batchCmd);
		call("changed", [svgcontent]);
	} catch(e) {
		console.log(e);
		return false;
	}

	return true;
};

// Function: importSvgString
// This function imports the input SVG XML as a <symbol> in the <defs>, then adds a
// <use> to the current layer.
//
// Parameters:
// xmlString - The SVG as XML text.
//
// Returns:
// This function returns false if the import was unsuccessful, true otherwise.
// TODO: 
// * properly handle if namespace is introduced by imported content (must add to svgcontent
// and update all prefixes in the imported node)
// * properly handle recalculating dimensions, recalculateDimensions() doesn't handle
// arbitrary transform lists, but makes some assumptions about how the transform list 
// was obtained
// * import should happen in top-left of current zoomed viewport	
this.importSvgString = function(xmlString) {

	try {
		// Get unique ID
		var uid = svgedit.utilities.encode64(xmlString.length + xmlString).substr(0,32);
		
		var useExisting = false;

		// Look for symbol and make sure symbol exists in image
		if(import_ids[uid]) {
			if( $(import_ids[uid].symbol).parents('#svgroot').length ) {
				useExisting = true;
			}
		}
		
		var batchCmd = new BatchCommand("Import SVG");
	
		if(useExisting) {
			var symbol = import_ids[uid].symbol;
			var ts = import_ids[uid].xform;
		} else {
			// convert string into XML document
			var newDoc = svgedit.utilities.text2xml(xmlString);
	
			this.prepareSvg(newDoc);
	
			// import new svg document into our document
			var svg;
			// If DOM3 adoptNode() available, use it. Otherwise fall back to DOM2 importNode()
			if(svgdoc.adoptNode) {
				svg = svgdoc.adoptNode(newDoc.documentElement);
			}
			else {
				svg = svgdoc.importNode(newDoc.documentElement, true);
			}
			
			uniquifyElems(svg);
			
			var innerw = convertToNum('width', svg.getAttribute("width")),
				innerh = convertToNum('height', svg.getAttribute("height")),
				innervb = svg.getAttribute("viewBox"),
				// if no explicit viewbox, create one out of the width and height
				vb = innervb ? innervb.split(" ") : [0,0,innerw,innerh];
			for (var j = 0; j < 4; ++j)
				vb[j] = +(vb[j]);
	
			// TODO: properly handle preserveAspectRatio
			var canvasw = +svgcontent.getAttribute("width"),
				canvash = +svgcontent.getAttribute("height");
			// imported content should be 1/3 of the canvas on its largest dimension
			
			if (innerh > innerw) {
				var ts = "scale(" + (canvash/3)/vb[3] + ")";
			}
			else {
				var ts = "scale(" + (canvash/3)/vb[2] + ")";
			}
			
			// Hack to make recalculateDimensions understand how to scale
			ts = "translate(0) " + ts + " translate(0)";
			
			var symbol = svgdoc.createElementNS(svgns, "symbol");
			var defs = findDefs();
			
			if(svgedit.browser.isGecko()) {
				// Move all gradients into root for Firefox, workaround for this bug:
				// https://bugzilla.mozilla.org/show_bug.cgi?id=353575
				// TODO: Make this properly undo-able.
				$(svg).find('linearGradient, radialGradient, pattern').appendTo(defs);
			}
	
			while (svg.firstChild) {
				var first = svg.firstChild;
				symbol.appendChild(first);
			}
			var attrs = svg.attributes;
			for(var i=0; i < attrs.length; i++) {
				var attr = attrs[i];
				symbol.setAttribute(attr.nodeName, attr.nodeValue);
			}
			symbol.id = getNextId();
			
			// Store data
			import_ids[uid] = {
				symbol: symbol,
				xform: ts
			}
			
			findDefs().appendChild(symbol);
			batchCmd.addSubCommand(new InsertElementCommand(symbol));
		}
		
		
		var use_el = svgdoc.createElementNS(svgns, "use");
		use_el.id = getNextId();
		setHref(use_el, "#" + symbol.id);
		
		(current_group || getCurrentDrawing().getCurrentLayer()).appendChild(use_el);
		batchCmd.addSubCommand(new InsertElementCommand(use_el));
		clearSelection();
		
		use_el.setAttribute("transform", ts);
		recalculateDimensions(use_el);
		$(use_el).data('symbol', symbol).data('ref', symbol);
		addToSelection([use_el]);
		
		// TODO: Find way to add this in a recalculateDimensions-parsable way
// 				if (vb[0] != 0 || vb[1] != 0)
// 					ts = "translate(" + (-vb[0]) + "," + (-vb[1]) + ") " + ts;
		addCommandToHistory(batchCmd);
		call("changed", [svgcontent]);

	} catch(e) {
		console.log(e);
		return false;
	}

	return true;
};

// TODO(codedread): Move all layer/context functions in draw.js
// Layer API Functions

// Group: Layers

// Function: identifyLayers
// Updates layer system
var identifyLayers = canvas.identifyLayers = function() {
	leaveContext();
	getCurrentDrawing().identifyLayers();
};

// Function: createLayer
// Creates a new top-level layer in the drawing with the given name, sets the current layer 
// to it, and then clears the selection  This function then calls the 'changed' handler.
// This is an undoable action.
//
// Parameters:
// name - The given name
this.createLayer = function(name) {
	var batchCmd = new BatchCommand("Create Layer");
	var new_layer = getCurrentDrawing().createLayer(name);
	batchCmd.addSubCommand(new InsertElementCommand(new_layer));
	addCommandToHistory(batchCmd);
	clearSelection();
	call("changed", [new_layer]);
};

// Function: cloneLayer
// Creates a new top-level layer in the drawing with the given name, copies all the current layer's contents
// to it, and then clears the selection  This function then calls the 'changed' handler.
// This is an undoable action.
//
// Parameters:
// name - The given name
this.cloneLayer = function(name) {
	var batchCmd = new BatchCommand("Duplicate Layer");
	var new_layer = svgdoc.createElementNS(svgns, "g");
	var layer_title = svgdoc.createElementNS(svgns, "title");
	layer_title.textContent = name;
	new_layer.appendChild(layer_title);
	var current_layer = getCurrentDrawing().getCurrentLayer();
	$(current_layer).after(new_layer);
	var childs = current_layer.childNodes;
	for(var i = 0; i < childs.length; i++) {
		var ch = childs[i];
		if(ch.localName == 'title') continue;
		new_layer.appendChild(copyElem(ch));
	}
	
	clearSelection();
	identifyLayers();

	batchCmd.addSubCommand(new InsertElementCommand(new_layer));
	addCommandToHistory(batchCmd);
	canvas.setCurrentLayer(name);
	call("changed", [new_layer]);
};

// Function: deleteCurrentLayer
// Deletes the current layer from the drawing and then clears the selection. This function 
// then calls the 'changed' handler.  This is an undoable action.
this.deleteCurrentLayer = function() {
	var current_layer = getCurrentDrawing().getCurrentLayer();
	var nextSibling = current_layer.nextSibling;
	var parent = current_layer.parentNode;
	current_layer = getCurrentDrawing().deleteCurrentLayer();
	if (current_layer) {
		var batchCmd = new BatchCommand("Delete Layer");
		// store in our Undo History
		batchCmd.addSubCommand(new RemoveElementCommand(current_layer, nextSibling, parent));
		addCommandToHistory(batchCmd);
		clearSelection();
		call("changed", [parent]);
		return true;
	}
	return false;
};

// Function: setCurrentLayer
// Sets the current layer. If the name is not a valid layer name, then this function returns
// false. Otherwise it returns true. This is not an undo-able action.
//
// Parameters:
// name - the name of the layer you want to switch to.
//
// Returns:
// true if the current layer was switched, otherwise false
this.setCurrentLayer = function(name) {
	var result = getCurrentDrawing().setCurrentLayer(svgedit.utilities.toXml(name));
	if (result) {
		clearSelection();
	}
	return result;
};

// Function: renameCurrentLayer
// Renames the current layer. If the layer name is not valid (i.e. unique), then this function 
// does nothing and returns false, otherwise it returns true. This is an undo-able action.
// 
// Parameters:
// newname - the new name you want to give the current layer.  This name must be unique 
// among all layer names.
//
// Returns:
// true if the rename succeeded, false otherwise.
this.renameCurrentLayer = function(newname) {
	var drawing = getCurrentDrawing();
	if (drawing.current_layer) {
		var oldLayer = drawing.current_layer;
		// setCurrentLayer will return false if the name doesn't already exist
		// this means we are free to rename our oldLayer
		if (!canvas.setCurrentLayer(newname)) {
			var batchCmd = new BatchCommand("Rename Layer");
			// find the index of the layer
			for (var i = 0; i < drawing.getNumLayers(); ++i) {
				if (drawing.all_layers[i][1] == oldLayer) break;
			}
			var oldname = drawing.getLayerName(i);
			drawing.all_layers[i][0] = svgedit.utilities.toXml(newname);
		
			// now change the underlying title element contents
			var len = oldLayer.childNodes.length;
			for (var i = 0; i < len; ++i) {
				var child = oldLayer.childNodes.item(i);
				// found the <title> element, now append all the
				if (child && child.tagName == "title") {
					// wipe out old name 
					while (child.firstChild) { child.removeChild(child.firstChild); }
					child.textContent = newname;

					batchCmd.addSubCommand(new ChangeElementCommand(child, {"#text":oldname}));
					addCommandToHistory(batchCmd);
					call("changed", [oldLayer]);
					return true;
				}
			}
		}
		drawing.current_layer = oldLayer;
	}
	return false;
};

// Function: setCurrentLayerPosition
// Changes the position of the current layer to the new value. If the new index is not valid, 
// this function does nothing and returns false, otherwise it returns true. This is an
// undo-able action.
//
// Parameters:
// newpos - The zero-based index of the new position of the layer.  This should be between
// 0 and (number of layers - 1)
// 
// Returns:
// true if the current layer position was changed, false otherwise.
this.setCurrentLayerPosition = function(newpos) {
	var drawing = getCurrentDrawing();
	if (drawing.current_layer && newpos >= 0 && newpos < drawing.getNumLayers()) {
		for (var oldpos = 0; oldpos < drawing.getNumLayers(); ++oldpos) {
			if (drawing.all_layers[oldpos][1] == drawing.current_layer) break;
		}
		// some unknown error condition (current_layer not in all_layers)
		if (oldpos == drawing.getNumLayers()) { return false; }
		
		if (oldpos != newpos) {
			// if our new position is below us, we need to insert before the node after newpos
			var refLayer = null;
			var oldNextSibling = drawing.current_layer.nextSibling;
			if (newpos > oldpos ) {
				if (newpos < drawing.getNumLayers()-1) {
					refLayer = drawing.all_layers[newpos+1][1];
				}
			}
			// if our new position is above us, we need to insert before the node at newpos
			else {
				refLayer = drawing.all_layers[newpos][1];
			}
			svgcontent.insertBefore(drawing.current_layer, refLayer);
			addCommandToHistory(new MoveElementCommand(drawing.current_layer, oldNextSibling, svgcontent));
			
			identifyLayers();
			canvas.setCurrentLayer(drawing.getLayerName(newpos));
			
			return true;
		}
	}
	
	return false;
};

// Function: setLayerVisibility
// Sets the visibility of the layer. If the layer name is not valid, this function return 
// false, otherwise it returns true. This is an undo-able action.
//
// Parameters:
// layername - the name of the layer to change the visibility
// bVisible - true/false, whether the layer should be visible
//
// Returns:
// true if the layer's visibility was set, false otherwise
this.setLayerVisibility = function(layername, bVisible) {
	var drawing = getCurrentDrawing();
	var prevVisibility = drawing.getLayerVisibility(layername);
	var layer = drawing.setLayerVisibility(layername, bVisible);
	if (layer) {
		var oldDisplay = prevVisibility ? 'inline' : 'none';
		addCommandToHistory(new ChangeElementCommand(layer, {'display':oldDisplay}, 'Layer Visibility'));
	} else {
		return false;
	}
	
	if (layer == drawing.getCurrentLayer()) {
		clearSelection();
		pathActions.clear();
	}
//		call("changed", [selected]);	
	return true;
};

// Function: moveSelectedToLayer
// Moves the selected elements to layername. If the name is not a valid layer name, then false 
// is returned.  Otherwise it returns true. This is an undo-able action.
//
// Parameters:
// layername - the name of the layer you want to which you want to move the selected elements
//
// Returns:
// true if the selected elements were moved to the layer, false otherwise.
this.moveSelectedToLayer = function(layername) {
	// find the layer
	var layer = null;
	var drawing = getCurrentDrawing();
	for (var i = 0; i < drawing.getNumLayers(); ++i) {
		if (drawing.getLayerName(i) == layername) {
			layer = drawing.all_layers[i][1];
			break;
		}
	}
	if (!layer) return false;
	
	var batchCmd = new BatchCommand("Move Elements to Layer");
	
	// loop for each selected element and move it
	var selElems = selectedElements;
	var i = selElems.length;
	while (i--) {
		var elem = selElems[i];
		if (!elem) continue;
		var oldNextSibling = elem.nextSibling;
		// TODO: this is pretty brittle!
		var oldLayer = elem.parentNode;
		layer.appendChild(elem);
		batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldLayer));
	}
	
	addCommandToHistory(batchCmd);
	
	return true;
};

this.mergeLayer = function(skipHistory) {
	var batchCmd = new BatchCommand("Merge Layer");
	var drawing = getCurrentDrawing();
	var prev = $(drawing.current_layer).prev()[0];
	if(!prev) return;
	var childs = drawing.current_layer.childNodes;
	var len = childs.length;
	var layerNextSibling = drawing.current_layer.nextSibling;
	batchCmd.addSubCommand(new RemoveElementCommand(drawing.current_layer, layerNextSibling, svgcontent));

	while(drawing.current_layer.firstChild) {
		var ch = drawing.current_layer.firstChild;
		if(ch.localName == 'title') {
			var chNextSibling = ch.nextSibling;
			batchCmd.addSubCommand(new RemoveElementCommand(ch, chNextSibling, drawing.current_layer));
			drawing.current_layer.removeChild(ch);
			continue;
		}
		var oldNextSibling = ch.nextSibling;
		prev.appendChild(ch);
		batchCmd.addSubCommand(new MoveElementCommand(ch, oldNextSibling, drawing.current_layer));
	}
	
	// Remove current layer
	svgcontent.removeChild(drawing.current_layer);
	
	if(!skipHistory) {
		clearSelection();
		identifyLayers();

		call("changed", [svgcontent]);
		
		addCommandToHistory(batchCmd);
	}
	
	drawing.current_layer = prev;
	return batchCmd;
}

this.mergeAllLayers = function() {
	var batchCmd = new BatchCommand("Merge all Layers");
	var drawing = getCurrentDrawing();
	drawing.current_layer = drawing.all_layers[drawing.getNumLayers()-1][1];
	while($(svgcontent).children('g').length > 1) {
		batchCmd.addSubCommand(canvas.mergeLayer(true));
	}
	
	clearSelection();
	identifyLayers();
	call("changed", [svgcontent]);
	addCommandToHistory(batchCmd);
}

// Function: leaveContext
// Return from a group context to the regular kind, make any previously
// disabled elements enabled again
var leaveContext = this.leaveContext = function() {
	var len = disabled_elems.length;
	if(len) {
		for(var i = 0; i < len; i++) {
			var elem = disabled_elems[i];
			
			var orig = elData(elem, 'orig_opac');
			if(orig !== 1) {
				elem.setAttribute('opacity', orig);
			} else {
				elem.removeAttribute('opacity');
			}
			elem.setAttribute('style', 'pointer-events: inherit');
		}
		disabled_elems = [];
		clearSelection(true);
		call("contextset", null);
	}
	current_group = null;
}

// Function: setContext
// Set the current context (for in-group editing)
var setContext = this.setContext = function(elem) {
	leaveContext();
	if(typeof elem === 'string') {
		elem = getElem(elem);
	}

	// Edit inside this group
	current_group = elem;
	
	// Disable other elements
	$(elem).parentsUntil('#svgcontent').andSelf().siblings().each(function() {
		var opac = this.getAttribute('opacity') || 1;
		// Store the original's opacity
		elData(this, 'orig_opac', opac);
		this.setAttribute('opacity', opac * .33);
		this.setAttribute('style', 'pointer-events: none');
		disabled_elems.push(this);
	});

	clearSelection();
	call("contextset", current_group);
}

// Group: Document functions

// Function: clear
// Clears the current document.  This is not an undoable action.
this.clear = function() {
	pathActions.clear();

	clearSelection();

	// clear the svgcontent node
	canvas.clearSvgContentElement();

	// create new document
	canvas.current_drawing_ = new svgedit.draw.Drawing(svgcontent);

	// create empty first layer
	canvas.createLayer("Layer 1");
	
	// clear the undo stack
	canvas.undoMgr.resetUndoStack();

	// reset the selector manager
	selectorManager.initGroup();

	// reset the rubber band box
	rubberBox = selectorManager.getRubberBandBox();

	call("cleared");
};

// Function: linkControlPoints
// Alias function
this.linkControlPoints = pathActions.linkControlPoints;

// Function: getContentElem
// Returns the content DOM element
this.getContentElem = function() { return svgcontent; };

// Function: getRootElem
// Returns the root DOM element
this.getRootElem = function() { return svgroot; };

// Function: getSelectedElems
// Returns the array with selected DOM elements
this.getSelectedElems = function() { return selectedElements; };

// Function: getResolution
// Returns the current dimensions and zoom level in an object
var getResolution = this.getResolution = function() {
// 		var vb = svgcontent.getAttribute("viewBox").split(' ');
// 		return {'w':vb[2], 'h':vb[3], 'zoom': current_zoom};
	
	var width = svgcontent.getAttribute("width")/current_zoom;
	var height = svgcontent.getAttribute("height")/current_zoom;
	
	return {
		'w': width,
		'h': height,
		'zoom': current_zoom
	};
};

// Function: getZoom
// Returns the current zoom level
this.getZoom = function(){return current_zoom;};

// Function: getVersion
// Returns a string which describes the revision number of SvgCanvas.
this.getVersion = function() {
	return "svgcanvas.js ($Rev: 2070 $)";
};

// Function: setUiStrings
// Update interface strings with given values
//
// Parameters:
// strs - Object with strings (see uiStrings for examples)
this.setUiStrings = function(strs) {
	$.extend(uiStrings, strs.notification);
}

// Function: setConfig
// Update configuration options with given values
//
// Parameters:
// opts - Object with options (see curConfig for examples)
this.setConfig = function(opts) {
	$.extend(curConfig, opts);
}

// Function: getTitle
// Returns the current group/SVG's title contents
this.getTitle = function(elem) {
	elem = elem || selectedElements[0];
	if(!elem) return;
	elem = $(elem).data('gsvg') || $(elem).data('symbol') || elem;
	var childs = elem.childNodes;
	for (var i=0; i<childs.length; i++) {
		if(childs[i].nodeName == 'title') {
			return childs[i].textContent;
		}
	}
	return '';
}

// Function: setGroupTitle
// Sets the group/SVG's title content
// TODO: Combine this with setDocumentTitle
this.setGroupTitle = function(val) {
	var elem = selectedElements[0];
	elem = $(elem).data('gsvg') || elem;
	
	var ts = $(elem).children('title');
	
	var batchCmd = new BatchCommand("Set Label");
	
	if(!val.length) {
		// Remove title element
		var tsNextSibling = ts.nextSibling;
		batchCmd.addSubCommand(new RemoveElementCommand(ts[0], tsNextSibling, elem));
		ts.remove();
	} else if(ts.length) {
		// Change title contents
		var title = ts[0];
		batchCmd.addSubCommand(new ChangeElementCommand(title, {'#text': title.textContent}));
		title.textContent = val;
	} else {
		// Add title element
		title = svgdoc.createElementNS(svgns, "title");
		title.textContent = val;
		$(elem).prepend(title);
		batchCmd.addSubCommand(new InsertElementCommand(title));
	}

	addCommandToHistory(batchCmd);
}

// Function: getDocumentTitle
// Returns the current document title or an empty string if not found
this.getDocumentTitle = function() {
	return canvas.getTitle(svgcontent);
}

// Function: setDocumentTitle
// Adds/updates a title element for the document with the given name.
// This is an undoable action
//
// Parameters:
// newtitle - String with the new title
this.setDocumentTitle = function(newtitle) {
	var childs = svgcontent.childNodes, doc_title = false, old_title = '';
	
	var batchCmd = new BatchCommand("Change Image Title");
	
	for (var i=0; i<childs.length; i++) {
		if(childs[i].nodeName == 'title') {
			doc_title = childs[i];
			old_title = doc_title.textContent;
			break;
		}
	}
	if(!doc_title) {
		doc_title = svgdoc.createElementNS(svgns, "title");
		svgcontent.insertBefore(doc_title, svgcontent.firstChild);
	} 
	
	if(newtitle.length) {
		doc_title.textContent = newtitle;
	} else {
		// No title given, so element is not necessary
		doc_title.parentNode.removeChild(doc_title);
	}
	batchCmd.addSubCommand(new ChangeElementCommand(doc_title, {'#text': old_title}));
	addCommandToHistory(batchCmd);
}

// Function: getEditorNS
// Returns the editor's namespace URL, optionally adds it to root element
//
// Parameters:
// add - Boolean to indicate whether or not to add the namespace value
this.getEditorNS = function(add) {
	if(add) {
		svgcontent.setAttribute('xmlns:se', se_ns);
	}
	return se_ns;
}

// Function: setResolution
// Changes the document's dimensions to the given size
//
// Parameters: 
// x - Number with the width of the new dimensions in user units. 
// Can also be the string "fit" to indicate "fit to content"
// y - Number with the height of the new dimensions in user units. 
//
// Returns:
// Boolean to indicate if resolution change was succesful. 
// It will fail on "fit to content" option with no content to fit to.
this.setResolution = function(x, y) {
	var res = getResolution();
	var w = res.w, h = res.h;
	var batchCmd;

	if(x == 'fit') {
		// Get bounding box
		var bbox = getStrokedBBox();
		
		if(bbox) {
			batchCmd = new BatchCommand("Fit Canvas to Content");
			var visEls = getVisibleElements();
			addToSelection(visEls);
			var dx = [], dy = [];
			$.each(visEls, function(i, item) {
				dx.push(bbox.x*-1);
				dy.push(bbox.y*-1);
			});
			
			var cmd = canvas.moveSelectedElements(dx, dy, true);
			batchCmd.addSubCommand(cmd);
			clearSelection();
			
			x = Math.round(bbox.width);
			y = Math.round(bbox.height);
		} else {
			return false;
		}
	}
	if (x != w || y != h) {
		var handle = svgroot.suspendRedraw(1000);
		if(!batchCmd) {
			batchCmd = new BatchCommand("Change Image Dimensions");
		}

		x = convertToNum('width', x);
		y = convertToNum('height', y);
		
		svgcontent.setAttribute('width', x);
		svgcontent.setAttribute('height', y);
		
		this.contentW = x;
		this.contentH = y;
		batchCmd.addSubCommand(new ChangeElementCommand(svgcontent, {"width":w, "height":h}));

		svgcontent.setAttribute("viewBox", [0, 0, x/current_zoom, y/current_zoom].join(' '));
		batchCmd.addSubCommand(new ChangeElementCommand(svgcontent, {"viewBox": ["0 0", w, h].join(' ')}));
	
		addCommandToHistory(batchCmd);
		svgroot.unsuspendRedraw(handle);
		call("changed", [svgcontent]);
	}
	return true;
};

// Function: getOffset
// Returns an object with x, y values indicating the svgcontent element's
// position in the editor's canvas.
this.getOffset = function() {
	return $(svgcontent).attr(['x', 'y']);
}

// Function: setBBoxZoom
// Sets the zoom level on the canvas-side based on the given value
// 
// Parameters:
// val - Bounding box object to zoom to or string indicating zoom option 
// editor_w - Integer with the editor's workarea box's width
// editor_h - Integer with the editor's workarea box's height
this.setBBoxZoom = function(val, editor_w, editor_h) {
	var spacer = .85;
	var bb;
	var calcZoom = function(bb) {
		if(!bb) return false;
		var w_zoom = Math.round((editor_w / bb.width)*100 * spacer)/100;
		var h_zoom = Math.round((editor_h / bb.height)*100 * spacer)/100;	
		var zoomlevel = Math.min(w_zoom,h_zoom);
		canvas.setZoom(zoomlevel);
		return {'zoom': zoomlevel, 'bbox': bb};
	}
	
	if(typeof val == 'object') {
		bb = val;
		if(bb.width == 0 || bb.height == 0) {
			var newzoom = bb.zoom?bb.zoom:current_zoom * bb.factor;
			canvas.setZoom(newzoom);
			return {'zoom': current_zoom, 'bbox': bb};
		}
		return calcZoom(bb);
	}

	switch (val) {
		case 'selection':
			if(!selectedElements[0]) return;
			var sel_elems = $.map(selectedElements, function(n){ if(n) return n; });
			bb = getStrokedBBox(sel_elems);
			break;
		case 'canvas':
			var res = getResolution();
			spacer = .95;
			bb = {width:res.w, height:res.h ,x:0, y:0};
			break;
		case 'content':
			bb = getStrokedBBox();
			break;
		case 'layer':
			bb = getStrokedBBox(getVisibleElements(getCurrentDrawing().getCurrentLayer()));
			break;
		default:
			return;
	}
	return calcZoom(bb);
}

// Function: setZoom
// Sets the zoom to the given level
//
// Parameters:
// zoomlevel - Float indicating the zoom level to change to
this.setZoom = function(zoomlevel) {
	var res = getResolution();
	svgcontent.setAttribute("viewBox", "0 0 " + res.w/zoomlevel + " " + res.h/zoomlevel);
	current_zoom = zoomlevel;
	$.each(selectedElements, function(i, elem) {
		if(!elem) return;
		selectorManager.requestSelector(elem).resize();
	});
	pathActions.zoomChange();
	runExtensions("zoomChanged", zoomlevel);
}

// Function: getMode
// Returns the current editor mode string
this.getMode = function() {
	return current_mode;
};

// Function: setMode
// Sets the editor's mode to the given string
//
// Parameters:
// name - String with the new mode to change to
this.setMode = function(name) {
	pathActions.clear(true);
	textActions.clear();
	cur_properties = (selectedElements[0] && selectedElements[0].nodeName == 'text') ? cur_text : cur_shape;
	current_mode = name;
};

// Group: Element Styling

// Function: getColor
// Returns the current fill/stroke option
this.getColor = function(type) {
	return cur_properties[type];
};

// Function: setColor
// Change the current stroke/fill color/gradient value
// 
// Parameters:
// type - String indicating fill or stroke
// val - The value to set the stroke attribute to
// preventUndo - Boolean indicating whether or not this should be and undoable option
this.setColor = function(type, val, preventUndo) {
	cur_shape[type] = val;
	cur_properties[type + '_paint'] = {type:"solidColor"};
	var elems = [];
	var i = selectedElements.length;
	while (i--) {
		var elem = selectedElements[i];
		if (elem) {
			if (elem.tagName == "g")
				svgedit.utilities.walkTree(elem, function(e){if(e.nodeName!="g") elems.push(e);});
			else {
				if(type == 'fill') {
					if(elem.tagName != "polyline" && elem.tagName != "line") {
						elems.push(elem);
					}
				} else {
					elems.push(elem);
				}
			}
		}
	}
	if (elems.length > 0) {
		if (!preventUndo) {
			changeSelectedAttribute(type, val, elems);
			call("changed", elems);
		} else 
			changeSelectedAttributeNoUndo(type, val, elems);
	}
}


// Function: findDefs
// Return the document's <defs> element, create it first if necessary
var findDefs = function() {
	var defs = svgcontent.getElementsByTagNameNS(svgns, "defs");
	if (defs.length > 0) {
		defs = defs[0];
	}
	else {
		defs = svgdoc.createElementNS(svgns, "defs" );
		if(svgcontent.firstChild) {
			// first child is a comment, so call nextSibling
			svgcontent.insertBefore( defs, svgcontent.firstChild.nextSibling);
		} else {
			svgcontent.appendChild(defs);
		}
	}
	return defs;
};

// Function: setGradient
// Apply the current gradient to selected element's fill or stroke
//
// Parameters
// type - String indicating "fill" or "stroke" to apply to an element
var setGradient = this.setGradient = function(type) {
	if(!cur_properties[type + '_paint'] || cur_properties[type + '_paint'].type == "solidColor") return;
	var grad = canvas[type + 'Grad'];
	// find out if there is a duplicate gradient already in the defs
	var duplicate_grad = findDuplicateGradient(grad);
	var defs = findDefs();
	// no duplicate found, so import gradient into defs
	if (!duplicate_grad) {
		var orig_grad = grad;
		grad = defs.appendChild( svgdoc.importNode(grad, true) );
		// get next id and set it on the grad
		grad.id = getNextId();
	}
	else { // use existing gradient
		grad = duplicate_grad;
	}
	canvas.setColor(type, "url(#" + grad.id + ")");
}

// Function: findDuplicateGradient
// Check if exact gradient already exists
//
// Parameters:
// grad - The gradient DOM element to compare to others
//
// Returns:
// The existing gradient if found, null if not
var findDuplicateGradient = function(grad) {
	var defs = findDefs();
	var existing_grads = $(defs).find("linearGradient, radialGradient");
	var i = existing_grads.length;
	var rad_attrs = ['r','cx','cy','fx','fy'];
	while (i--) {
		var og = existing_grads[i];
		if(grad.tagName == "linearGradient") {
			if (grad.getAttribute('x1') != og.getAttribute('x1') ||
				grad.getAttribute('y1') != og.getAttribute('y1') ||
				grad.getAttribute('x2') != og.getAttribute('x2') ||
				grad.getAttribute('y2') != og.getAttribute('y2')) 
			{
				continue;
			}
		} else {
			var grad_attrs = $(grad).attr(rad_attrs);
			var og_attrs = $(og).attr(rad_attrs);
			
			var diff = false;
			$.each(rad_attrs, function(i, attr) {
				if(grad_attrs[attr] != og_attrs[attr]) diff = true;
			});
			
			if(diff) continue;
		}
		
		// else could be a duplicate, iterate through stops
		var stops = grad.getElementsByTagNameNS(svgns, "stop");
		var ostops = og.getElementsByTagNameNS(svgns, "stop");

		if (stops.length != ostops.length) {
			continue;
		}

		var j = stops.length;
		while(j--) {
			var stop = stops[j];
			var ostop = ostops[j];

			if (stop.getAttribute('offset') != ostop.getAttribute('offset') ||
				stop.getAttribute('stop-opacity') != ostop.getAttribute('stop-opacity') ||
				stop.getAttribute('stop-color') != ostop.getAttribute('stop-color')) 
			{
				break;
			}
		}

		if (j == -1) {
			return og;
		}
	} // for each gradient in defs

	return null;
};

function reorientGrads(elem, m) {
	var bb = svgedit.utilities.getBBox(elem);
	for(var i = 0; i < 2; i++) {
		var type = i === 0 ? 'fill' : 'stroke';
		var attrVal = elem.getAttribute(type);
		if(attrVal && attrVal.indexOf('url(') === 0) {
			var grad = getRefElem(attrVal);
			if(grad.tagName === 'linearGradient') {
				var x1 = grad.getAttribute('x1') || 0;
				var y1 = grad.getAttribute('y1') || 0;
				var x2 = grad.getAttribute('x2') || 1;
				var y2 = grad.getAttribute('y2') || 0;
				
				// Convert to USOU points
				x1 = (bb.width * x1) + bb.x;
				y1 = (bb.height * y1) + bb.y;
				x2 = (bb.width * x2) + bb.x;
				y2 = (bb.height * y2) + bb.y;
			
				// Transform those points
				var pt1 = transformPoint(x1, y1, m);
				var pt2 = transformPoint(x2, y2, m);
				
				// Convert back to BB points
				var g_coords = {};
				
				g_coords.x1 = (pt1.x - bb.x) / bb.width;
				g_coords.y1 = (pt1.y - bb.y) / bb.height;
				g_coords.x2 = (pt2.x - bb.x) / bb.width;
				g_coords.y2 = (pt2.y - bb.y) / bb.height;
		
				var newgrad = grad.cloneNode(true);
				$(newgrad).attr(g_coords);
	
				newgrad.id = getNextId();
				findDefs().appendChild(newgrad);
				elem.setAttribute(type, 'url(#' + newgrad.id + ')');
			}
		}
	}
}

// Function: setPaint
// Set a color/gradient to a fill/stroke
//
// Parameters: 
// type - String with "fill" or "stroke"
// paint - The jGraduate paint object to apply
this.setPaint = function(type, paint) {
	// make a copy
	var p = new $.jGraduate.Paint(paint);
	this.setPaintOpacity(type, p.alpha/100, true);

	// now set the current paint object
	cur_properties[type + '_paint'] = p;
	switch ( p.type ) {
		case "solidColor":
			this.setColor(type, p.solidColor != "none" ? "#"+p.solidColor : "none");;
			break;
		case "linearGradient":
		case "radialGradient":
			canvas[type + 'Grad'] = p[p.type];
			setGradient(type);
			break;
		default:
//			console.log("none!");
	}
};


// this.setStrokePaint = function(p) {
// 	// make a copy
// 	var p = new $.jGraduate.Paint(p);
// 	this.setStrokeOpacity(p.alpha/100);
// 
// 	// now set the current paint object
// 	cur_properties.stroke_paint = p;
// 	switch ( p.type ) {
// 		case "solidColor":
// 			this.setColor('stroke', p.solidColor != "none" ? "#"+p.solidColor : "none");;
// 			break;
// 		case "linearGradient"
// 		case "radialGradient"
// 			canvas.strokeGrad = p[p.type];
// 			setGradient(type); 
// 		default:
// //			console.log("none!");
// 	}
// };
// 
// this.setFillPaint = function(p, addGrad) {
// 	// make a copy
// 	var p = new $.jGraduate.Paint(p);
// 	this.setFillOpacity(p.alpha/100, true);
// 
// 	// now set the current paint object
// 	cur_properties.fill_paint = p;
// 	if (p.type == "solidColor") {
// 		this.setColor('fill', p.solidColor != "none" ? "#"+p.solidColor : "none");
// 	}
// 	else if(p.type == "linearGradient") {
// 		canvas.fillGrad = p.linearGradient;
// 		if(addGrad) setGradient(); 
// 	}
// 	else if(p.type == "radialGradient") {
// 		canvas.fillGrad = p.radialGradient;
// 		if(addGrad) setGradient(); 
// 	}
// 	else {
// //			console.log("none!");
// 	}
// };

// Function: getStrokeWidth
// Returns the current stroke-width value
this.getStrokeWidth = function() {
	return cur_properties.stroke_width;
};

// Function: setStrokeWidth
// Sets the stroke width for the current selected elements
// When attempting to set a line's width to 0, this changes it to 1 instead
//
// Parameters:
// val - A Float indicating the new stroke width value
this.setStrokeWidth = function(val) {
	if(val == 0 && ['line', 'path'].indexOf(current_mode) >= 0) {
		canvas.setStrokeWidth(1);
		return;
	}
	cur_properties.stroke_width = val;
	
	var elems = [];
	var i = selectedElements.length;
	while (i--) {
		var elem = selectedElements[i];
		if (elem) {
			if (elem.tagName == "g")
				svgedit.utilities.walkTree(elem, function(e){if(e.nodeName!="g") elems.push(e);});
			else 
				elems.push(elem);
		}
	}		
	if (elems.length > 0) {
		changeSelectedAttribute("stroke-width", val, elems);
		call("changed", selectedElements);
	}
};

// Function: setStrokeAttr
// Set the given stroke-related attribute the given value for selected elements
//
// Parameters:
// attr - String with the attribute name
// val - String or number with the attribute value
this.setStrokeAttr = function(attr, val) {
	cur_shape[attr.replace('-','_')] = val;
	var elems = [];
	var i = selectedElements.length;
	while (i--) {
		var elem = selectedElements[i];
		if (elem) {
			if (elem.tagName == "g")
				svgedit.utilities.walkTree(elem, function(e){if(e.nodeName!="g") elems.push(e);});
			else 
				elems.push(elem);
		}
	}		
	if (elems.length > 0) {
		changeSelectedAttribute(attr, val, elems);
		call("changed", selectedElements);
	}
};

// Function: getStyle
// Returns current style options
this.getStyle = function() {
	return cur_shape;
}

// Function: getOpacity
// Returns the current opacity
this.getOpacity = function() {
	return cur_shape.opacity;
};

// Function: setOpacity
// Sets the given opacity to the current selected elements
this.setOpacity = function(val) {
	cur_shape.opacity = val;
	changeSelectedAttribute("opacity", val);
};

// Function: getOpacity
// Returns the current fill opacity
this.getFillOpacity = function() {
	return cur_shape.fill_opacity;
};

// Function: getStrokeOpacity
// Returns the current stroke opacity
this.getStrokeOpacity = function() {
	return cur_shape.stroke_opacity;
};

// Function: setPaintOpacity
// Sets the current fill/stroke opacity
//
// Parameters:
// type - String with "fill" or "stroke"
// val - Float with the new opacity value
// preventUndo - Boolean indicating whether or not this should be an undoable action
this.setPaintOpacity = function(type, val, preventUndo) {
	cur_shape[type + '_opacity'] = val;
	if (!preventUndo)
		changeSelectedAttribute(type + "-opacity", val);
	else
		changeSelectedAttributeNoUndo(type + "-opacity", val);
};

// Function: getBlur
// Gets the stdDeviation blur value of the given element
//
// Parameters:
// elem - The element to check the blur value for
this.getBlur = function(elem) {
	var val = 0;
// 		var elem = selectedElements[0];
	
	if(elem) {
		var filter_url = elem.getAttribute('filter');
		if(filter_url) {
			var blur = getElem(elem.id + '_blur');
			if(blur) {
				val = blur.firstChild.getAttribute('stdDeviation');
			}
		}
	}
	return val;
};

(function() {
	var cur_command = null;
	var filter = null;
	var filterHidden = false;
	
	// Function: setBlurNoUndo
	// Sets the stdDeviation blur value on the selected element without being undoable
	//
	// Parameters:
	// val - The new stdDeviation value
	canvas.setBlurNoUndo = function(val) {
		if(!filter) {
			canvas.setBlur(val);
			return;
		}
		if(val === 0) {
			// Don't change the StdDev, as that will hide the element.
			// Instead, just remove the value for "filter"
			changeSelectedAttributeNoUndo("filter", "");
			filterHidden = true;
		} else {
			var elem = selectedElements[0];
			if(filterHidden) {
				changeSelectedAttributeNoUndo("filter", 'url(#' + elem.id + '_blur)');
			}
			if(svgedit.browser.isWebkit()) {
				console.log('e', elem);
				elem.removeAttribute('filter');
				elem.setAttribute('filter', 'url(#' + elem.id + '_blur)');
			}
			changeSelectedAttributeNoUndo("stdDeviation", val, [filter.firstChild]);
			canvas.setBlurOffsets(filter, val);
		}
	}
	
	function finishChange() {
		var bCmd = canvas.undoMgr.finishUndoableChange();
		cur_command.addSubCommand(bCmd);
		addCommandToHistory(cur_command);
		cur_command = null;	
		filter = null;
	}

	// Function: setBlurOffsets
	// Sets the x, y, with, height values of the filter element in order to
	// make the blur not be clipped. Removes them if not neeeded
	//
	// Parameters:
	// filter - The filter DOM element to update
	// stdDev - The standard deviation value on which to base the offset size
	canvas.setBlurOffsets = function(filter, stdDev) {
		if(stdDev > 3) {
			// TODO: Create algorithm here where size is based on expected blur
			assignAttributes(filter, {
				x: '-50%',
				y: '-50%',
				width: '200%',
				height: '200%'
			}, 100);
		} else {
			// Removing these attributes hides text in Chrome (see Issue 579)
			if(!svgedit.browser.isWebkit()) {
				filter.removeAttribute('x');
				filter.removeAttribute('y');
				filter.removeAttribute('width');
				filter.removeAttribute('height');
			}
		}
	}

	// Function: setBlur 
	// Adds/updates the blur filter to the selected element
	//
	// Parameters:
	// val - Float with the new stdDeviation blur value
	// complete - Boolean indicating whether or not the action should be completed (to add to the undo manager)
	canvas.setBlur = function(val, complete) {
		if(cur_command) {
			finishChange();
			return;
		}
	
		// Looks for associated blur, creates one if not found
		var elem = selectedElements[0];
		var elem_id = elem.id;
		filter = getElem(elem_id + '_blur');
		
		val -= 0;
		
		var batchCmd = new BatchCommand();
		
		// Blur found!
		if(filter) {
			if(val === 0) {
				filter = null;
			}
		} else {
			// Not found, so create
			var newblur = addSvgElementFromJson({ "element": "feGaussianBlur",
				"attr": {
					"in": 'SourceGraphic',
					"stdDeviation": val
				}
			});
			
			filter = addSvgElementFromJson({ "element": "filter",
				"attr": {
					"id": elem_id + '_blur'
				}
			});
			
			filter.appendChild(newblur);
			findDefs().appendChild(filter);
			
			batchCmd.addSubCommand(new InsertElementCommand(filter));
		}

		var changes = {filter: elem.getAttribute('filter')};
		
		if(val === 0) {
			elem.removeAttribute("filter");
			batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
			return;
		} else {
			changeSelectedAttribute("filter", 'url(#' + elem_id + '_blur)');
			
			batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
			
			canvas.setBlurOffsets(filter, val);
		}
		
		cur_command = batchCmd;
		canvas.undoMgr.beginUndoableChange("stdDeviation", [filter?filter.firstChild:null]);
		if(complete) {
			canvas.setBlurNoUndo(val);
			finishChange();
		}
	};
}());

// Function: getBold
// Check whether selected element is bold or not
//
// Returns:
// Boolean indicating whether or not element is bold
this.getBold = function() {
	// should only have one element selected
	var selected = selectedElements[0];
	if (selected != null && selected.tagName  == "text" &&
		selectedElements[1] == null) 
	{
		return (selected.getAttribute("font-weight") == "bold");
	}
	return false;
};

// Function: setBold
// Make the selected element bold or normal
//
// Parameters:
// b - Boolean indicating bold (true) or normal (false)
this.setBold = function(b) {
	var selected = selectedElements[0];
	if (selected != null && selected.tagName  == "text" &&
		selectedElements[1] == null) 
	{
		changeSelectedAttribute("font-weight", b ? "bold" : "normal");
	}
	if(!selectedElements[0].textContent) {
		textActions.setCursor();
	}
};

// Function: getItalic
// Check whether selected element is italic or not
//
// Returns:
// Boolean indicating whether or not element is italic
this.getItalic = function() {
	var selected = selectedElements[0];
	if (selected != null && selected.tagName  == "text" &&
		selectedElements[1] == null) 
	{
		return (selected.getAttribute("font-style") == "italic");
	}
	return false;
};

// Function: setItalic
// Make the selected element italic or normal
//
// Parameters:
// b - Boolean indicating italic (true) or normal (false)
this.setItalic = function(i) {
	var selected = selectedElements[0];
	if (selected != null && selected.tagName  == "text" &&
		selectedElements[1] == null) 
	{
		changeSelectedAttribute("font-style", i ? "italic" : "normal");
	}
	if(!selectedElements[0].textContent) {
		textActions.setCursor();
	}
};

// Function: getFontFamily
// Returns the current font family
this.getFontFamily = function() {
	return cur_text.font_family;
};

// Function: setFontFamily
// Set the new font family
//
// Parameters:
// val - String with the new font family
this.setFontFamily = function(val) {
	cur_text.font_family = val;
	changeSelectedAttribute("font-family", val);
	if(selectedElements[0] && !selectedElements[0].textContent) {
		textActions.setCursor();
	}
};


// Function: setFontColor
// Set the new font color
//
// Parameters:
// val - String with the new font color
this.setFontColor = function(val) {
	cur_text.fill = val;
	changeSelectedAttribute("fill", val);
};

// Function: getFontColor
// Returns the current font color
this.getFontSize = function() {
	return cur_text.fill;
};

// Function: getFontSize
// Returns the current font size
this.getFontSize = function() {
	return cur_text.font_size;
};

// Function: setFontSize
// Applies the given font size to the selected element
//
// Parameters:
// val - Float with the new font size
this.setFontSize = function(val) {
	cur_text.font_size = val;
	changeSelectedAttribute("font-size", val);
	if(!selectedElements[0].textContent) {
		textActions.setCursor();
	}
};

// Function: getText
// Returns the current text (textContent) of the selected element
this.getText = function() {
	var selected = selectedElements[0];
	if (selected == null) { return ""; }
	return selected.textContent;
};

// Function: setTextContent
// Updates the text element with the given string
//
// Parameters:
// val - String with the new text
this.setTextContent = function(val) {
	changeSelectedAttribute("#text", val);
	textActions.init(val);
	textActions.setCursor();
};

// Function: setImageURL
// Sets the new image URL for the selected image element. Updates its size if
// a new URL is given
// 
// Parameters:
// val - String with the image URL/path
this.setImageURL = function(val) {
	var elem = selectedElements[0];
	if(!elem) return;
	
	var attrs = $(elem).attr(['width', 'height']);
	var setsize = (!attrs.width || !attrs.height);

	var cur_href = getHref(elem);
	
	// Do nothing if no URL change or size change
	if(cur_href !== val) {
		setsize = true;
	} else if(!setsize) return;

	var batchCmd = new BatchCommand("Change Image URL");

	setHref(elem, val);
	batchCmd.addSubCommand(new ChangeElementCommand(elem, {
		"#href": cur_href
	}));

	if(setsize) {
		$(new Image()).load(function() {
			var changes = $(elem).attr(['width', 'height']);
		
			$(elem).attr({
				width: this.width,
				height: this.height
			});
			
			selectorManager.requestSelector(elem).resize();
			
			batchCmd.addSubCommand(new ChangeElementCommand(elem, changes));
			addCommandToHistory(batchCmd);
			call("changed", [elem]);
		}).attr('src',val);
	} else {
		addCommandToHistory(batchCmd);
	}
};

// Function: setLinkURL
// Sets the new link URL for the selected anchor element.
// 
// Parameters:
// val - String with the link URL/path
this.setLinkURL = function(val) {
	var elem = selectedElements[0];
	if(!elem) return;
	if(elem.tagName !== 'a') {
		// See if parent is an anchor
		var parents_a = $(elem).parents('a');
		if(parents_a.length) {
			elem = parents_a[0];
		} else {
			return;
		}
	}
	
	var cur_href = getHref(elem);
	
	if(cur_href === val) return;
	
	var batchCmd = new BatchCommand("Change Link URL");

	setHref(elem, val);
	batchCmd.addSubCommand(new ChangeElementCommand(elem, {
		"#href": cur_href
	}));

	addCommandToHistory(batchCmd);
};


// Function: setRectRadius
// Sets the rx & ry values to the selected rect element to change its corner radius
// 
// Parameters:
// val - The new radius
this.setRectRadius = function(val) {
	var selected = selectedElements[0];
	if (selected != null && selected.tagName == "rect") {
		var r = selected.getAttribute("rx");
		if (r != val) {
			selected.setAttribute("rx", val);
			selected.setAttribute("ry", val);
			addCommandToHistory(new ChangeElementCommand(selected, {"rx":r, "ry":r}, "Radius"));
			call("changed", [selected]);
		}
	}
};

// Function: makeHyperlink
// Wraps the selected element(s) in an anchor element or converts group to one
this.makeHyperlink = function(url) {
	canvas.groupSelectedElements('a', url);
	
	// TODO: If element is a single "g", convert to "a"
	//	if(selectedElements.length > 1 && selectedElements[1]) {

}

// Function: removeHyperlink
this.removeHyperlink = function() {
	canvas.ungroupSelectedElement();
}

// Group: Element manipulation

// Function: setSegType
// Sets the new segment type to the selected segment(s). 
//
// Parameters:
// new_type - Integer with the new segment type
// See http://www.w3.org/TR/SVG/paths.html#InterfaceSVGPathSeg for list
this.setSegType = function(new_type) {
	pathActions.setSegType(new_type);
}

// TODO(codedread): Remove the getBBox argument and split this function into two.
// Function: convertToPath
// Convert selected element to a path, or get the BBox of an element-as-path
//
// Parameters: 
// elem - The DOM element to be converted
// getBBox - Boolean on whether or not to only return the path's BBox
//
// Returns:
// If the getBBox flag is true, the resulting path's bounding box object.
// Otherwise the resulting path element is returned.
this.convertToPath = function(elem, getBBox) {
	if(elem == null) {
		var elems = selectedElements;
		$.each(selectedElements, function(i, elem) {
			if(elem) canvas.convertToPath(elem);
		});
		return;
	}
	
	if(!getBBox) {
		var batchCmd = new BatchCommand("Convert element to Path");
	}
	
	var attrs = getBBox?{}:{
		"fill": cur_shape.fill,
		"fill-opacity": cur_shape.fill_opacity,
		"stroke": cur_shape.stroke,
		"stroke-width": cur_shape.stroke_width,
		"stroke-dasharray": cur_shape.stroke_dasharray,
		"stroke-linejoin": cur_shape.stroke_linejoin,
		"stroke-linecap": cur_shape.stroke_linecap,
		"stroke-opacity": cur_shape.stroke_opacity,
		"opacity": cur_shape.opacity,
		"visibility":"hidden"
	};
	
	// any attribute on the element not covered by the above
	// TODO: make this list global so that we can properly maintain it
	// TODO: what about @transform, @clip-rule, @fill-rule, etc?
	$.each(['marker-start', 'marker-end', 'marker-mid', 'filter', 'clip-path'], function() {
		if (elem.getAttribute(this)) {
			attrs[this] = elem.getAttribute(this);
		}
	});
	
	var path = addSvgElementFromJson({
		"element": "path",
		"attr": attrs
	});
	
	var eltrans = elem.getAttribute("transform");
	if(eltrans) {
		path.setAttribute("transform",eltrans);
	}
	
	var id = elem.id;
	var parent = elem.parentNode;
	if(elem.nextSibling) {
		parent.insertBefore(path, elem);
	} else {
		parent.appendChild(path);
	}
	
	var d = '';
	
	var joinSegs = function(segs) {
		$.each(segs, function(j, seg) {
			var l = seg[0], pts = seg[1];
			d += l;
			for(var i=0; i < pts.length; i+=2) {
				d += (pts[i] +','+pts[i+1]) + ' ';
			}
		});
	}

	// Possibly the cubed root of 6, but 1.81 works best
	var num = 1.81;

	switch (elem.tagName) {
	case 'ellipse':
	case 'circle':
		var a = $(elem).attr(['rx', 'ry', 'cx', 'cy']);
		var cx = a.cx, cy = a.cy, rx = a.rx, ry = a.ry;
		if(elem.tagName == 'circle') {
			rx = ry = $(elem).attr('r');
		}
	
		joinSegs([
			['M',[(cx-rx),(cy)]],
			['C',[(cx-rx),(cy-ry/num), (cx-rx/num),(cy-ry), (cx),(cy-ry)]],
			['C',[(cx+rx/num),(cy-ry), (cx+rx),(cy-ry/num), (cx+rx),(cy)]],
			['C',[(cx+rx),(cy+ry/num), (cx+rx/num),(cy+ry), (cx),(cy+ry)]],
			['C',[(cx-rx/num),(cy+ry), (cx-rx),(cy+ry/num), (cx-rx),(cy)]],
			['Z',[]]
		]);
		break;
	case 'path':
		d = elem.getAttribute('d');
		break;
	case 'line':
		var a = $(elem).attr(["x1", "y1", "x2", "y2"]);
		d = "M"+a.x1+","+a.y1+"L"+a.x2+","+a.y2;
		break;
	case 'polyline':
	case 'polygon':
		d = "M" + elem.getAttribute('points');
		break;
	case 'rect':
		var r = $(elem).attr(['rx', 'ry']);
		var rx = r.rx, ry = r.ry;
		var b = elem.getBBox();
		var x = b.x, y = b.y, w = b.width, h = b.height;
		var num = 4-num; // Why? Because!
		
		if(!rx && !ry) {
			// Regular rect
			joinSegs([
				['M',[x, y]],
				['L',[x+w, y]],
				['L',[x+w, y+h]],
				['L',[x, y+h]],
				['L',[x, y]],
				['Z',[]]
			]);
		} else {
			joinSegs([
				['M',[x, y+ry]],
				['C',[x,y+ry/num, x+rx/num,y, x+rx,y]],
				['L',[x+w-rx, y]],
				['C',[x+w-rx/num,y, x+w,y+ry/num, x+w,y+ry]],
				['L',[x+w, y+h-ry]],
				['C',[x+w, y+h-ry/num, x+w-rx/num,y+h, x+w-rx,y+h]],
				['L',[x+rx, y+h]],
				['C',[x+rx/num, y+h, x,y+h-ry/num, x,y+h-ry]],
				['L',[x, y+ry]],
				['Z',[]]
			]);
		}
		break;
	default:
		path.parentNode.removeChild(path);
		break;
	}
	
	if(d) {
		path.setAttribute('d',d);
	}
	
	if(!getBBox) {
		// Replace the current element with the converted one
		
		// Reorient if it has a matrix
		if(eltrans) {
			var tlist = getTransformList(path);
			if(hasMatrixTransform(tlist)) {
				pathActions.resetOrientation(path);
			}
		}
		
		var nextSibling = elem.nextSibling;
		batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
		batchCmd.addSubCommand(new InsertElementCommand(path));

		clearSelection();
		elem.parentNode.removeChild(elem)
		path.setAttribute('id', id);
		path.removeAttribute("visibility");
		addToSelection([path], true);
		
		addCommandToHistory(batchCmd);
		
	} else {
		// Get the correct BBox of the new path, then discard it
		pathActions.resetOrientation(path);
		var bb = false;
		try {
			bb = path.getBBox();
		} catch(e) {
			// Firefox fails
		}
		path.parentNode.removeChild(path);
		return bb;
	}
};


// Function: changeSelectedAttributeNoUndo
// This function makes the changes to the elements. It does not add the change
// to the history stack. 
// 
// Parameters:
// attr - String with the attribute name
// newValue - String or number with the new attribute value
// elems - The DOM elements to apply the change to
var changeSelectedAttributeNoUndo = function(attr, newValue, elems) {
	var handle = svgroot.suspendRedraw(1000);
	if(current_mode == 'pathedit') {
		// Editing node
		pathActions.moveNode(attr, newValue);
	}
	var elems = elems || selectedElements;
	var i = elems.length;
	var no_xy_elems = ['g', 'polyline', 'path'];
	var good_g_attrs = ['transform', 'opacity', 'filter'];
	
	while (i--) {
		var elem = elems[i];
		if (elem == null) continue;
		
		// Go into "select" mode for text changes
		if(current_mode === "textedit" && attr !== "#text" && elem.textContent.length) {
			textActions.toSelectMode(elem);
		}
		
		// Set x,y vals on elements that don't have them
		if((attr === 'x' || attr === 'y') && no_xy_elems.indexOf(elem.tagName) >= 0) {
			var bbox = getStrokedBBox([elem]);
			var diff_x = attr === 'x' ? newValue - bbox.x : 0;
			var diff_y = attr === 'y' ? newValue - bbox.y : 0;
			canvas.moveSelectedElements(diff_x*current_zoom, diff_y*current_zoom, true);
			continue;
		}
		
		// only allow the transform/opacity/filter attribute to change on <g> elements, slightly hacky
		// TODO: FIXME: This doesn't seem right.  Where's the body of this if statement?
		if (elem.tagName === "g" && good_g_attrs.indexOf(attr) >= 0);
		var oldval = attr === "#text" ? elem.textContent : elem.getAttribute(attr);
		if (oldval == null)  oldval = "";
		if (oldval !== String(newValue)) {
			if (attr == "#text") {
				var old_w = svgedit.utilities.getBBox(elem).width;
				elem.textContent = newValue;
				
				// FF bug occurs on on rotated elements
				if(/rotate/.test(elem.getAttribute('transform'))) {
					elem = ffClone(elem);
				}
				
				// Hoped to solve the issue of moving text with text-anchor="start",
				// but this doesn't actually fix it. Hopefully on the right track, though. -Fyrd
				
// 					var box=getBBox(elem), left=box.x, top=box.y, width=box.width,
// 						height=box.height, dx = width - old_w, dy=0;
// 					var angle = getRotationAngle(elem, true);
// 					if (angle) {
// 						var r = Math.sqrt( dx*dx + dy*dy );
// 						var theta = Math.atan2(dy,dx) - angle;
// 						dx = r * Math.cos(theta);
// 						dy = r * Math.sin(theta);
// 						
// 						elem.setAttribute('x', elem.getAttribute('x')-dx);
// 						elem.setAttribute('y', elem.getAttribute('y')-dy);
// 					}
				
			} else if (attr == "#href") {
				setHref(elem, newValue);
			}
			else elem.setAttribute(attr, newValue);
//			if (i==0)
//				selectedBBoxes[0] = svgedit.utilities.getBBox(elem);
			// Use the Firefox ffClone hack for text elements with gradients or
			// where other text attributes are changed. 
			if(svgedit.browser.isGecko() && elem.nodeName === 'text' && /rotate/.test(elem.getAttribute('transform'))) {
				if((newValue+'').indexOf('url') === 0 || ['font-size','font-family','x','y'].indexOf(attr) >= 0 && elem.textContent) {
					elem = ffClone(elem);
				}
			}
			// Timeout needed for Opera & Firefox
			// codedread: it is now possible for this function to be called with elements
			// that are not in the selectedElements array, we need to only request a
			// selector if the element is in that array
			if (selectedElements.indexOf(elem) >= 0) {
				setTimeout(function() {
					// Due to element replacement, this element may no longer
					// be part of the DOM
					if(!elem.parentNode) return;
					selectorManager.requestSelector(elem).resize();
				},0);
			}
			// if this element was rotated, and we changed the position of this element
			// we need to update the rotational transform attribute 
			var angle = getRotationAngle(elem);
			if (angle != 0 && attr != "transform") {
				var tlist = getTransformList(elem);
				var n = tlist.numberOfItems;
				while (n--) {
					var xform = tlist.getItem(n);
					if (xform.type == 4) {
						// remove old rotate
						tlist.removeItem(n);
						
						var box = svgedit.utilities.getBBox(elem);
						var center = transformPoint(box.x+box.width/2, box.y+box.height/2, transformListToTransform(tlist).matrix);
						var cx = center.x,
							cy = center.y;
						var newrot = svgroot.createSVGTransform();
						newrot.setRotate(angle, cx, cy);
						tlist.insertItemBefore(newrot, n);
						break;
					}
				}
			}
		} // if oldValue != newValue
	} // for each elem
	svgroot.unsuspendRedraw(handle);	
};

// Function: changeSelectedAttribute
// Change the given/selected element and add the original value to the history stack
// If you want to change all selectedElements, ignore the elems argument.
// If you want to change only a subset of selectedElements, then send the
// subset to this function in the elems argument.
// 
// Parameters:
// attr - String with the attribute name
// newValue - String or number with the new attribute value
// elems - The DOM elements to apply the change to
var changeSelectedAttribute = this.changeSelectedAttribute = function(attr, val, elems) {
	var elems = elems || selectedElements;
	canvas.undoMgr.beginUndoableChange(attr, elems);
	var i = elems.length;

	changeSelectedAttributeNoUndo(attr, val, elems);

	var batchCmd = canvas.undoMgr.finishUndoableChange();
	if (!batchCmd.isEmpty()) { 
		addCommandToHistory(batchCmd);
	}
};

// Function: deleteSelectedElements
// Removes all selected elements from the DOM and adds the change to the 
// history stack
this.deleteSelectedElements = function() {
	var batchCmd = new BatchCommand("Delete Elements");
	var len = selectedElements.length;
	var selectedCopy = []; //selectedElements is being deleted
	for (var i = 0; i < len; ++i) {
		var selected = selectedElements[i];
		if (selected == null) break;

		var parent = selected.parentNode;
		var t = selected;
		
		// this will unselect the element and remove the selectedOutline
		selectorManager.releaseSelector(t);
		
		// Remove the path if present.
		svgedit.path.removePath_(t.id);
		
		// Get the parent if it's a single-child anchor
		if(parent.tagName === 'a' && parent.childNodes.length === 1) {
			t = parent;
			parent = parent.parentNode;
		}
		
		var nextSibling = t.nextSibling;
		var elem = parent.removeChild(t);
		selectedCopy.push(selected); //for the copy
		selectedElements[i] = null;
		batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
	}
	if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
	call("changed", selectedCopy);
	clearSelection();
};

// Function: cutSelectedElements
// Removes all selected elements from the DOM and adds the change to the 
// history stack. Remembers removed elements on the clipboard

// TODO: Combine similar code with deleteSelectedElements
this.cutSelectedElements = function() {
	var batchCmd = new BatchCommand("Cut Elements");
	var len = selectedElements.length;
	var selectedCopy = []; //selectedElements is being deleted
	for (var i = 0; i < len; ++i) {
		var selected = selectedElements[i];
		if (selected == null) break;

		var parent = selected.parentNode;
		var t = selected;

		// this will unselect the element and remove the selectedOutline
		selectorManager.releaseSelector(t);

		// Remove the path if present.
		svgedit.path.removePath_(t.id);

		var nextSibling = t.nextSibling;
		var elem = parent.removeChild(t);
		selectedCopy.push(selected); //for the copy
		selectedElements[i] = null;
		batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, parent));
	}
	if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
	call("changed", selectedCopy);
	clearSelection();
	
	canvas.clipBoard = selectedCopy;
};

// Function: copySelectedElements
// Remembers the current selected elements on the clipboard
this.copySelectedElements = function() {
	canvas.clipBoard = $.merge([], selectedElements);
};

this.pasteElements = function(type, x, y) {
	var cb = canvas.clipBoard;
	var len = cb.length;
	if(!len) return;
	
	var pasted = [];
	var batchCmd = new BatchCommand('Paste elements');
	
	// Move elements to lastClickPoint

	while (len--) {
		var elem = cb[len];
		if(!elem) continue;
		var copy = copyElem(elem);

		// See if elem with elem ID is in the DOM already
		if(!getElem(elem.id)) copy.id = elem.id;
		
		pasted.push(copy);
		(current_group || getCurrentDrawing().getCurrentLayer()).appendChild(copy);
		batchCmd.addSubCommand(new InsertElementCommand(copy));
	}
	
	selectOnly(pasted);
	
	if(type !== 'in_place') {
		
		var ctr_x, ctr_y;
		
		if(!type) {
			ctr_x = lastClickPoint.x;
			ctr_y = lastClickPoint.y;
		} else if(type === 'point') {
			ctr_x = x;
			ctr_y = y;
		} 
		
		var bbox = getStrokedBBox(pasted);
		var cx = ctr_x - (bbox.x + bbox.width/2),
			cy = ctr_y - (bbox.y + bbox.height/2),
			dx = [],
			dy = [];
	
		$.each(pasted, function(i, item) {
			dx.push(cx);
			dy.push(cy);
		});
		
		var cmd = canvas.moveSelectedElements(dx, dy, false);
		batchCmd.addSubCommand(cmd);
	}
	

	
	addCommandToHistory(batchCmd);
	call("changed", pasted);
}

// Function: groupSelectedElements
// Wraps all the selected elements in a group (g) element

// Parameters: 
// type - type of element to group into, defaults to <g>
this.groupSelectedElements = function(type) {
	if(!type) type = 'g';
	var cmd_str = '';
	
	switch ( type ) {
		case "a":
			cmd_str = "Make hyperlink";
			var url = '';
			if(arguments.length > 1) {
				url = arguments[1];
			}
			break;
		default:
			type = 'g';
			cmd_str = "Group Elements";
			break;
	}
	
	var batchCmd = new BatchCommand(cmd_str);
	
	// create and insert the group element
	var g = addSvgElementFromJson({
							"element": type,
							"attr": {
								"id": getNextId()
							}
						});
	if(type === 'a') {
		setHref(g, url);
	}
	batchCmd.addSubCommand(new InsertElementCommand(g));
	
	// now move all children into the group
	var i = selectedElements.length;
	while (i--) {
		var elem = selectedElements[i];
		if (elem == null) continue;
		
		if (elem.parentNode.tagName === 'a' && elem.parentNode.childNodes.length === 1) {
			elem = elem.parentNode;
		}
		
		var oldNextSibling = elem.nextSibling;
		var oldParent = elem.parentNode;
		g.appendChild(elem);
		batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));			
	}
	if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
	
	// update selection
	selectOnly([g], true);
};


// Function: pushGroupProperties
// Pushes all appropriate parent group properties down to its children, then
// removes them from the group
var pushGroupProperties = this.pushGroupProperties = function(g, undoable) {

	var children = g.childNodes;
	var len = children.length;
	var xform = g.getAttribute("transform");

	var glist = getTransformList(g);
	var m = transformListToTransform(glist).matrix;
	
	var batchCmd = new BatchCommand("Push group properties");

	// TODO: get all fill/stroke properties from the group that we are about to destroy
	// "fill", "fill-opacity", "fill-rule", "stroke", "stroke-dasharray", "stroke-dashoffset", 
	// "stroke-linecap", "stroke-linejoin", "stroke-miterlimit", "stroke-opacity", 
	// "stroke-width"
	// and then for each child, if they do not have the attribute (or the value is 'inherit')
	// then set the child's attribute
	
	var i = 0;
	var gangle = getRotationAngle(g);
	
	var gattrs = $(g).attr(['filter', 'opacity']);
	var gfilter, gblur;
	
	for(var i = 0; i < len; i++) {
		var elem = children[i];
		
		if(elem.nodeType !== 1) continue;
		
		if(gattrs.opacity !== null && gattrs.opacity !== 1) {
			var c_opac = elem.getAttribute('opacity') || 1;
			var new_opac = Math.round((elem.getAttribute('opacity') || 1) * gattrs.opacity * 100)/100;
			changeSelectedAttribute('opacity', new_opac, [elem]);
		}

		if(gattrs.filter) {
			var cblur = this.getBlur(elem);
			var orig_cblur = cblur;
			if(!gblur) gblur = this.getBlur(g);
			if(cblur) {
				// Is this formula correct?
				cblur = (gblur-0) + (cblur-0);
			} else if(cblur === 0) {
				cblur = gblur;
			}
			
			// If child has no current filter, get group's filter or clone it.
			if(!orig_cblur) {
				// Set group's filter to use first child's ID
				if(!gfilter) {
					gfilter = getRefElem(gattrs.filter);
				} else {
					// Clone the group's filter
					gfilter = copyElem(gfilter);
					findDefs().appendChild(gfilter);
				}
			} else {
				gfilter = getRefElem(elem.getAttribute('filter'));
			}

			// Change this in future for different filters
			var suffix = (gfilter.firstChild.tagName === 'feGaussianBlur')?'blur':'filter'; 
			gfilter.id = elem.id + '_' + suffix;
			changeSelectedAttribute('filter', 'url(#' + gfilter.id + ')', [elem]);
			
			// Update blur value 
			if(cblur) {
				changeSelectedAttribute('stdDeviation', cblur, [gfilter.firstChild]);
				canvas.setBlurOffsets(gfilter, cblur);
			}
		}
		
		var chtlist = getTransformList(elem);

		// Don't process gradient transforms
		if(~elem.tagName.indexOf('Gradient')) chtlist = null;
		
		// Hopefully not a problem to add this. Necessary for elements like <desc/>
		if(!chtlist) continue;
		
		// Apparently <defs> can get get a transformlist, but we don't want it to have one!
		if(elem.tagName === 'defs') continue;
		
		if (glist.numberOfItems) {
			// TODO: if the group's transform is just a rotate, we can always transfer the
			// rotate() down to the children (collapsing consecutive rotates and factoring
			// out any translates)
			if (gangle && glist.numberOfItems == 1) {
				// [Rg] [Rc] [Mc]
				// we want [Tr] [Rc2] [Mc] where:
				// 	- [Rc2] is at the child's current center but has the 
				//	  sum of the group and child's rotation angles
				// 	- [Tr] is the equivalent translation that this child 
				// 	  undergoes if the group wasn't there
				
				// [Tr] = [Rg] [Rc] [Rc2_inv]
				
				// get group's rotation matrix (Rg)
				var rgm = glist.getItem(0).matrix;
				
				// get child's rotation matrix (Rc)
				var rcm = svgroot.createSVGMatrix();
				var cangle = getRotationAngle(elem);
				if (cangle) {
					rcm = chtlist.getItem(0).matrix;
				}
				
				// get child's old center of rotation
				var cbox = svgedit.utilities.getBBox(elem);
				var ceqm = transformListToTransform(chtlist).matrix;
				var coldc = transformPoint(cbox.x+cbox.width/2, cbox.y+cbox.height/2,ceqm);
				
				// sum group and child's angles
				var sangle = gangle + cangle;
				
				// get child's rotation at the old center (Rc2_inv)
				var r2 = svgroot.createSVGTransform();
				r2.setRotate(sangle, coldc.x, coldc.y);
				
				// calculate equivalent translate
				var trm = matrixMultiply(rgm, rcm, r2.matrix.inverse());
				
				// set up tlist
				if (cangle) {
					chtlist.removeItem(0);
				}
				
				if (sangle) {
					if(chtlist.numberOfItems) {
						chtlist.insertItemBefore(r2, 0);
					} else {
						chtlist.appendItem(r2);
					}
				}

				if (trm.e || trm.f) {
					var tr = svgroot.createSVGTransform();
					tr.setTranslate(trm.e, trm.f);
					if(chtlist.numberOfItems) {
						chtlist.insertItemBefore(tr, 0);
					} else {
						chtlist.appendItem(tr);
					}
				}
			}
			else { // more complicated than just a rotate
			
				// transfer the group's transform down to each child and then
				// call recalculateDimensions()				
				var oldxform = elem.getAttribute("transform");
				var changes = {};
				changes["transform"] = oldxform ? oldxform : "";

				var newxform = svgroot.createSVGTransform();

				// [ gm ] [ chm ] = [ chm ] [ gm' ]
				// [ gm' ] = [ chm_inv ] [ gm ] [ chm ]
				var chm = transformListToTransform(chtlist).matrix,
					chm_inv = chm.inverse();
				var gm = matrixMultiply( chm_inv, m, chm );
				newxform.setMatrix(gm);
				chtlist.appendItem(newxform);
			}
			var cmd = recalculateDimensions(elem);
			if(cmd)	batchCmd.addSubCommand(cmd);
		}
	}

	
	// remove transform and make it undo-able
	if (xform) {
		var changes = {};
		changes["transform"] = xform;
		g.setAttribute("transform", "");
		g.removeAttribute("transform");				
		batchCmd.addSubCommand(new ChangeElementCommand(g, changes));
	}
	
	if (undoable && !batchCmd.isEmpty()) {
		return batchCmd;
	}
}


// Function: ungroupSelectedElement
// Unwraps all the elements in a selected group (g) element. This requires
// significant recalculations to apply group's transforms, etc to its children
this.ungroupSelectedElement = function() {
	var g = selectedElements[0];
	if($(g).data('gsvg') || $(g).data('symbol')) {
		// Is svg, so actually convert to group

		convertToGroup(g);
		return;
	} else if(g.tagName === 'use') {
		// Somehow doesn't have data set, so retrieve
		var symbol = getElem(getHref(g).substr(1));
		$(g).data('symbol', symbol).data('ref', symbol);
		convertToGroup(g);
		return;
	}
	var parents_a = $(g).parents('a');
	if(parents_a.length) {
		g = parents_a[0];
	}
	
	// Look for parent "a"
	if (g.tagName === "g" || g.tagName === "a") {
		
		var batchCmd = new BatchCommand("Ungroup Elements");
		var cmd = pushGroupProperties(g, true);
		if(cmd) batchCmd.addSubCommand(cmd);
		
		var parent = g.parentNode;
		var anchor = g.nextSibling;
		var children = new Array(g.childNodes.length);
		
		var i = 0;
		
		while (g.firstChild) {
			var elem = g.firstChild;
			var oldNextSibling = elem.nextSibling;
			var oldParent = elem.parentNode;
			
			// Remove child title elements
			if(elem.tagName === 'title') {
				var nextSibling = elem.nextSibling;
				batchCmd.addSubCommand(new RemoveElementCommand(elem, nextSibling, oldParent));
				oldParent.removeChild(elem);
				continue;
			}
			
			children[i++] = elem = parent.insertBefore(elem, anchor);
			batchCmd.addSubCommand(new MoveElementCommand(elem, oldNextSibling, oldParent));
		}

		// remove the group from the selection			
		clearSelection();
		
		// delete the group element (but make undo-able)
		var gNextSibling = g.nextSibling;
		g = parent.removeChild(g);
		batchCmd.addSubCommand(new RemoveElementCommand(g, gNextSibling, parent));

		if (!batchCmd.isEmpty()) addCommandToHistory(batchCmd);
		
		// update selection
		addToSelection(children);
	}
};

// Function: moveToTopSelectedElement
// Repositions the selected element to the bottom in the DOM to appear on top of
// other elements
this.moveToTopSelectedElement = function() {
	var selected = selectedElements[0];
	if (selected != null) {
		var t = selected;
		var oldParent = t.parentNode;
		var oldNextSibling = t.nextSibling;
		t = t.parentNode.appendChild(t);
		// If the element actually moved position, add the command and fire the changed
		// event handler.
		if (oldNextSibling != t.nextSibling) {
			addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "top"));
			call("changed", [t]);
		}
	}
};

// Function: moveToBottomSelectedElement
// Repositions the selected element to the top in the DOM to appear under 
// other elements
this.moveToBottomSelectedElement = function() {
	var selected = selectedElements[0];
	if (selected != null) {
		var t = selected;
		var oldParent = t.parentNode;
		var oldNextSibling = t.nextSibling;
		var firstChild = t.parentNode.firstChild;
		if (firstChild.tagName == 'title') {
			firstChild = firstChild.nextSibling;
		}
		// This can probably be removed, as the defs should not ever apppear
		// inside a layer group
		if (firstChild.tagName == 'defs') {
			firstChild = firstChild.nextSibling;
		}
		t = t.parentNode.insertBefore(t, firstChild);
		// If the element actually moved position, add the command and fire the changed
		// event handler.
		if (oldNextSibling != t.nextSibling) {
			addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "bottom"));
			call("changed", [t]);
		}
	}
};

// Function: moveUpDownSelected
// Moves the select element up or down the stack, based on the visibly
// intersecting elements
//
// Parameters: 
// dir - String that's either 'Up' or 'Down'
this.moveUpDownSelected = function(dir) {
	var selected = selectedElements[0];
	if (!selected) return;
	
	curBBoxes = [];
	var closest, found_cur;
	// jQuery sorts this list
	var list = $(getIntersectionList(getStrokedBBox([selected]))).toArray();
	if(dir == 'Down') list.reverse();

	$.each(list, function() {
		if(!found_cur) {
			if(this == selected) {
				found_cur = true;
			}
			return;
		}
		closest = this;
		return false;
	});
	if(!closest) return;
	
	var t = selected;
	var oldParent = t.parentNode;
	var oldNextSibling = t.nextSibling;
	$(closest)[dir == 'Down'?'before':'after'](t);
	// If the element actually moved position, add the command and fire the changed
	// event handler.
	if (oldNextSibling != t.nextSibling) {
		addCommandToHistory(new MoveElementCommand(t, oldNextSibling, oldParent, "Move " + dir));
		call("changed", [t]);
	}
};

// Function: moveSelectedElements
// Moves selected elements on the X/Y axis 
//
// Parameters:
// dx - Float with the distance to move on the x-axis
// dy - Float with the distance to move on the y-axis
// undoable - Boolean indicating whether or not the action should be undoable
//
// Returns:
// Batch command for the move
this.moveSelectedElements = function(dx, dy, undoable) {
	// if undoable is not sent, default to true
	// if single values, scale them to the zoom
	if (dx.constructor != Array) {
		dx /= current_zoom;
		dy /= current_zoom;
	}
	var undoable = undoable || true;
	var batchCmd = new BatchCommand("position");
	var i = selectedElements.length;
	while (i--) {
		var selected = selectedElements[i];
		if (selected != null) {
//			if (i==0)
//				selectedBBoxes[0] = svgedit.utilities.getBBox(selected);
			
//			var b = {};
//			for(var j in selectedBBoxes[i]) b[j] = selectedBBoxes[i][j];
//			selectedBBoxes[i] = b;
			
			var xform = svgroot.createSVGTransform();
			var tlist = getTransformList(selected);
			
			// dx and dy could be arrays
			if (dx.constructor == Array) {
//				if (i==0) {
//					selectedBBoxes[0].x += dx[0];
//					selectedBBoxes[0].y += dy[0];
//				}
				xform.setTranslate(dx[i],dy[i]);
			} else {
//				if (i==0) {
//					selectedBBoxes[0].x += dx;
//					selectedBBoxes[0].y += dy;
//				}
				xform.setTranslate(dx,dy);
			}

			if(tlist.numberOfItems) {
				tlist.insertItemBefore(xform, 0);
			} else {
				tlist.appendItem(xform);
			}
			
			var cmd = recalculateDimensions(selected);
			if (cmd) {
				batchCmd.addSubCommand(cmd);
			}
			
			selectorManager.requestSelector(selected).resize();
		}
	}
	if (!batchCmd.isEmpty()) {
		if (undoable)
			addCommandToHistory(batchCmd);
		call("changed", selectedElements);
		return batchCmd;
	}
};

// Function: cloneSelectedElements
// Create deep DOM copies (clones) of all selected elements and move them slightly 
// from their originals
this.cloneSelectedElements = function(x,y) {
	var batchCmd = new BatchCommand("Clone Elements");
	// find all the elements selected (stop at first null)
	var len = selectedElements.length;
	for (var i = 0; i < len; ++i) {
		var elem = selectedElements[i];
		if (elem == null) break;
	}
	// use slice to quickly get the subset of elements we need
	var copiedElements = selectedElements.slice(0,i);
	this.clearSelection(true);
	// note that we loop in the reverse way because of the way elements are added
	// to the selectedElements array (top-first)
	var i = copiedElements.length;
	while (i--) {
		// clone each element and replace it within copiedElements
		var elem = copiedElements[i] = copyElem(copiedElements[i]);
		(current_group || getCurrentDrawing().getCurrentLayer()).appendChild(elem);
		batchCmd.addSubCommand(new InsertElementCommand(elem));
	}
	
	if (!batchCmd.isEmpty()) {
		addToSelection(copiedElements.reverse()); // Need to reverse for correct selection-adding
		this.moveSelectedElements(x,y,false);
		addCommandToHistory(batchCmd);
	}
};

// Function: alignSelectedElements
// Aligns selected elements
//
// Parameters:
// type - String with single character indicating the alignment type
// relative_to - String that must be one of the following: 
// "selected", "largest", "smallest", "page"
this.alignSelectedElements = function(type, relative_to) {
	var bboxes = [], angles = [];
	var minx = Number.MAX_VALUE, maxx = Number.MIN_VALUE, miny = Number.MAX_VALUE, maxy = Number.MIN_VALUE;
	var curwidth = Number.MIN_VALUE, curheight = Number.MIN_VALUE;
	var len = selectedElements.length;
	if (!len) return;
	for (var i = 0; i < len; ++i) {
		if (selectedElements[i] == null) break;
		var elem = selectedElements[i];
		bboxes[i] = getStrokedBBox([elem]);
		
		// now bbox is axis-aligned and handles rotation
		switch (relative_to) {
			case 'smallest':
				if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth > bboxes[i].width) ||
					 (type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight > bboxes[i].height) ) {
					minx = bboxes[i].x;
					miny = bboxes[i].y;
					maxx = bboxes[i].x + bboxes[i].width;
					maxy = bboxes[i].y + bboxes[i].height;
					curwidth = bboxes[i].width;
					curheight = bboxes[i].height;
				}
				break;
			case 'largest':
				if ( (type == 'l' || type == 'c' || type == 'r') && (curwidth == Number.MIN_VALUE || curwidth < bboxes[i].width) ||
					 (type == 't' || type == 'm' || type == 'b') && (curheight == Number.MIN_VALUE || curheight < bboxes[i].height) ) {
					minx = bboxes[i].x;
					miny = bboxes[i].y;
					maxx = bboxes[i].x + bboxes[i].width;
					maxy = bboxes[i].y + bboxes[i].height;
					curwidth = bboxes[i].width;
					curheight = bboxes[i].height;
				}
				break;
			default: // 'selected'
				if (bboxes[i].x < minx) minx = bboxes[i].x;
				if (bboxes[i].y < miny) miny = bboxes[i].y;
				if (bboxes[i].x + bboxes[i].width > maxx) maxx = bboxes[i].x + bboxes[i].width;
				if (bboxes[i].y + bboxes[i].height > maxy) maxy = bboxes[i].y + bboxes[i].height;
				break;
		}
	} // loop for each element to find the bbox and adjust min/max

	if (relative_to == 'page') {
		minx = 0;
		miny = 0;
		maxx = canvas.contentW;
		maxy = canvas.contentH;
	}

	var dx = new Array(len);
	var dy = new Array(len);
	for (var i = 0; i < len; ++i) {
		if (selectedElements[i] == null) break;
		var elem = selectedElements[i];
		var bbox = bboxes[i];
		dx[i] = 0;
		dy[i] = 0;
		switch (type) {
			case 'l': // left (horizontal)
				dx[i] = minx - bbox.x;
				break;
			case 'c': // center (horizontal)
				dx[i] = (minx+maxx)/2 - (bbox.x + bbox.width/2);
				break;
			case 'r': // right (horizontal)
				dx[i] = maxx - (bbox.x + bbox.width);
				break;
			case 't': // top (vertical)
				dy[i] = miny - bbox.y;
				break;
			case 'm': // middle (vertical)
				dy[i] = (miny+maxy)/2 - (bbox.y + bbox.height/2);
				break;
			case 'b': // bottom (vertical)
				dy[i] = maxy - (bbox.y + bbox.height);
				break;
		}
	}
	this.moveSelectedElements(dx,dy);
};

// Group: Additional editor tools

this.contentW = getResolution().w;
this.contentH = getResolution().h;

// Function: updateCanvas
// Updates the editor canvas width/height/position after a zoom has occurred 
//
// Parameters:
// w - Float with the new width
// h - Float with the new height
//
// Returns: 
// Object with the following values:
// * x - The canvas' new x coordinate
// * y - The canvas' new y coordinate
// * old_x - The canvas' old x coordinate
// * old_y - The canvas' old y coordinate
// * d_x - The x position difference
// * d_y - The y position difference
this.updateCanvas = function(w, h) {
	svgroot.setAttribute("width", w);
	svgroot.setAttribute("height", h);
	var bg = $('#canvasBackground')[0];
	var old_x = svgcontent.getAttribute('x');
	var old_y = svgcontent.getAttribute('y');
	var x = (w/2 - this.contentW*current_zoom/2);
	var y = (h/2 - this.contentH*current_zoom/2);

	assignAttributes(svgcontent, {
		width: this.contentW*current_zoom,
		height: this.contentH*current_zoom,
		'x': x,
		'y': y,
		"viewBox" : "0 0 " + this.contentW + " " + this.contentH
	});
	
	assignAttributes(bg, {
		width: svgcontent.getAttribute('width'),
		height: svgcontent.getAttribute('height'),
		x: x,
		y: y
	});

	var bg_img = getElem('background_image');
	if (bg_img) {
		assignAttributes(bg_img, {
			'width': '100%',
			'height': '100%'
		});
	}
	
	selectorManager.selectorParentGroup.setAttribute("transform","translate(" + x + "," + y + ")");
	
	return {x:x, y:y, old_x:old_x, old_y:old_y, d_x:x - old_x, d_y:y - old_y};
}

// Function: setBackground
// Set the background of the editor (NOT the actual document)
//
// Parameters:
// color - String with fill color to apply
// url - URL or path to image to use
this.setBackground = function(color, url) {
	var bg =  getElem('canvasBackground');
	var border = $(bg).find('rect')[0];
	var bg_img = getElem('background_image');
	border.setAttribute('fill',color);
	if(url) {
		if(!bg_img) {
			bg_img = svgdoc.createElementNS(svgns, "image");
			assignAttributes(bg_img, {
				'id': 'background_image',
				'width': '100%',
				'height': '100%',
				'preserveAspectRatio': 'xMinYMin',
				'style':'pointer-events:none'
			});
		}
		setHref(bg_img, url);
		bg.appendChild(bg_img);
	} else if(bg_img) {
		bg_img.parentNode.removeChild(bg_img);
	}
}

// Function: cycleElement
// Select the next/previous element within the current layer
//
// Parameters:
// next - Boolean where true = next and false = previous element
this.cycleElement = function(next) {
	var cur_elem = selectedElements[0];
	var elem = false;
	var all_elems = getVisibleElements(current_group || getCurrentDrawing().getCurrentLayer());
	if(!all_elems.length) return;
	if (cur_elem == null) {
		var num = next?all_elems.length-1:0;
		elem = all_elems[num];
	} else {
		var i = all_elems.length;
		while(i--) {
			if(all_elems[i] == cur_elem) {
				var num = next?i-1:i+1;
				if(num >= all_elems.length) {
					num = 0;
				} else if(num < 0) {
					num = all_elems.length-1;
				} 
				elem = all_elems[num];
				break;
			} 
		}
	}		
	selectOnly([elem], true);
	call("selected", selectedElements);
}

this.clear();


// DEPRECATED: getPrivateMethods 
// Since all methods are/should be public somehow, this function should be removed

// Being able to access private methods publicly seems wrong somehow,
// but currently appears to be the best way to allow testing and provide
// access to them to plugins.
this.getPrivateMethods = function() {
	var obj = {
		addCommandToHistory: addCommandToHistory,
		setGradient: setGradient,
		addSvgElementFromJson: addSvgElementFromJson,
		assignAttributes: assignAttributes,
		BatchCommand: BatchCommand,
		call: call,
		ChangeElementCommand: ChangeElementCommand,
		copyElem: copyElem,
		ffClone: ffClone,
		findDefs: findDefs,
		findDuplicateGradient: findDuplicateGradient,
		getElem: getElem,
		getId: getId,
		getIntersectionList: getIntersectionList,
		getMouseTarget: getMouseTarget,
		getNextId: getNextId,
		getPathBBox: getPathBBox,
		getUrlFromAttr: getUrlFromAttr,
		hasMatrixTransform: hasMatrixTransform,
		identifyLayers: identifyLayers,
		InsertElementCommand: InsertElementCommand,
		isIdentity: svgedit.math.isIdentity,
		logMatrix: logMatrix,
		matrixMultiply: matrixMultiply,
		MoveElementCommand: MoveElementCommand,
		preventClickDefault: preventClickDefault,
		recalculateAllSelectedDimensions: recalculateAllSelectedDimensions,
		recalculateDimensions: recalculateDimensions,
		remapElement: remapElement,
		RemoveElementCommand: RemoveElementCommand,
		removeUnusedDefElems: removeUnusedDefElems,
		round: round,
		runExtensions: runExtensions,
		sanitizeSvg: sanitizeSvg,
		SVGEditTransformList: svgedit.transformlist.SVGTransformList,
		toString: toString,
		transformBox: svgedit.math.transformBox,
		transformListToTransform: transformListToTransform,
		transformPoint: transformPoint,
		walkTree: svgedit.utilities.walkTree
	}
	return obj;
};

}