/*jslint vars: true, plusplus: true, devel: true, nomen: true, regexp: true, indent: 4, maxerr: 50 */
/*global $, window, location, CSInterface, SystemPath, themeManager*/

(function () {
    'use strict';

////////////////////////////////////////////////////////////////////////////////
// AD PLASTILINE LAB GLOBALS
////////////////////////////////////////////////////////////////////////////////

    var csInterface = new CSInterface();
	var ExtensionId = "ad.plastilinelab";
	
	var lyrsfx = window.document.getElementById("dropdwn1").value;

	var dup01 = window.document.getElementById('newLYR');
	var dup02 = window.document.getElementById('dupFX');

	var b01 = window.document.getElementById('btn01');
	var b02 = window.document.getElementById('btn02');
	var b03 = window.document.getElementById('btn03');
	var b04 = window.document.getElementById('btn04');
	var b05 = window.document.getElementById('btn05');
	var b06 = window.document.getElementById('btn06');
	var b07 = window.document.getElementById('btn07');
	var b08 = window.document.getElementById('btn08');
	var b09 = window.document.getElementById('btn09');
	
	var b21 = window.document.getElementById('btn21');
	var b22 = window.document.getElementById('btn22');
	var b23 = window.document.getElementById('btn23');
	var b24 = window.document.getElementById('btn24');
	var b25 = window.document.getElementById('btn25');
	var b26 = window.document.getElementById('btn26');
	var b27 = window.document.getElementById('btn27');
	var b28 = window.document.getElementById('btn28');
	var b29 = window.document.getElementById('btn29');

	var b41 = window.document.getElementById('btn41');
	var b42 = window.document.getElementById('btn42');
	var b43 = window.document.getElementById('btn43');
	var b44 = window.document.getElementById('btn44');
	var b45 = window.document.getElementById('btn45');
	var b46 = window.document.getElementById('btn46');
	var b47 = window.document.getElementById('btn47');
	var b48 = window.document.getElementById('btn48');
	var b49 = window.document.getElementById('btn49');

	var f01 = window.document.getElementById('func01');
	var f02 = window.document.getElementById('func02');
	var f03 = window.document.getElementById('func03');
	var f04 = window.document.getElementById('func04');
	var f05 = window.document.getElementById('func05');
	var f06 = window.document.getElementById('func06');
	var f07 = window.document.getElementById('func07');
	var f08 = window.document.getElementById('func08');
	var f09 = window.document.getElementById('func09');
	var f10 = window.document.getElementById('func10');
	var f11 = window.document.getElementById('func11');
	var f12 = window.document.getElementById('func12');
	
	var br01 = window.document.getElementById('brush01');
	var br02 = window.document.getElementById('brush02');
	var br03 = window.document.getElementById('brush03');
	var br04 = window.document.getElementById('brush04');
	var br05 = window.document.getElementById('brush05');
	var br06 = window.document.getElementById('brush06');

    function init() {
        themeManager.init();
		csInterface.evalScript("loadStyles()");
		csInterface.evalScript("loadPatterns()");
		
////////////////////////////////////////////////////////////////////////////////
// AD PLASTILINELAB BUTTONS FUNCTIONS
//////////////////////////////////////////////////////////////////////////////// 

		$("#btnweb").click(function () { csInterface.openURLInDefaultBrowser("http://alexdukal.com/store/plastilinelab/"); LoseFocus();});
		
		$('#dropdwn1').on('change', function() { csInterface.evalScript(""+this.value+"()"); $('select').prop('selectedIndex',0);});
		
        $("#dupFX").click(function () { csInterface.evalScript('DuplicateFX()'); });
        $("#newLYR").click(function () { csInterface.evalScript('NewLayer()'); });

        $("#btn01").click(function () { csInterface.evalScript('flat01()'); });
        $("#btn02").click(function () { csInterface.evalScript('flat02()'); });
        $("#btn03").click(function () { csInterface.evalScript('flat03()'); });
        $("#btn04").click(function () { csInterface.evalScript('flat04()'); });
        $("#btn05").click(function () { csInterface.evalScript('flat05()'); });
        $("#btn06").click(function () { csInterface.evalScript('flat06()'); });
        $("#btn07").click(function () { csInterface.evalScript('flat07()'); });
        $("#btn08").click(function () { csInterface.evalScript('flat08()'); });
        $("#btn09").click(function () { csInterface.evalScript('flat09()'); });

        $("#btn21").click(function () { csInterface.evalScript('med21()'); });
        $("#btn22").click(function () { csInterface.evalScript('med22()'); });
        $("#btn23").click(function () { csInterface.evalScript('med23()'); });
        $("#btn24").click(function () { csInterface.evalScript('med24()'); });
        $("#btn25").click(function () { csInterface.evalScript('med25()'); });
        $("#btn26").click(function () { csInterface.evalScript('med26()'); });
        $("#btn27").click(function () { csInterface.evalScript('med27()'); });
        $("#btn28").click(function () { csInterface.evalScript('med28()'); });
        $("#btn29").click(function () { csInterface.evalScript('med29()'); });

        $("#btn41").click(function () { csInterface.evalScript('thick01()'); });
        $("#btn42").click(function () { csInterface.evalScript('thick02()'); });
        $("#btn43").click(function () { csInterface.evalScript('thick03()'); });
        $("#btn44").click(function () { csInterface.evalScript('thick04()'); });
        $("#btn45").click(function () { csInterface.evalScript('thick05()'); });
        $("#btn46").click(function () { csInterface.evalScript('thick06()'); });
        $("#btn47").click(function () { csInterface.evalScript('thick07()'); });
        $("#btn48").click(function () { csInterface.evalScript('thick08()'); });
        $("#btn49").click(function () { csInterface.evalScript('thick09()'); });

        $("#func01").click(function () { csInterface.evalScript('strips()'); });
        $("#func02").click(function () { csInterface.evalScript('thinstrips()'); });
        $("#func03").click(function () { csInterface.evalScript('smallballs()'); });
        $("#func04").click(function () { csInterface.evalScript('scarbe()'); });
        $("#func05").click(function () { csInterface.evalScript('pushsoft()'); });
        $("#func06").click(function () { csInterface.evalScript('pushheavy()'); });
        $("#func07").click(function () { csInterface.evalScript('flattensoft()'); });
        $("#func08").click(function () { csInterface.evalScript('flattenheavy()'); });
        $("#func09").click(function () { csInterface.evalScript('fingerprints()'); });
        $("#func10").click(function () { csInterface.evalScript('colorize()'); });
        $("#func11").click(function () { csInterface.evalScript('brights()'); });
        $("#func12").click(function () { csInterface.evalScript('grain()'); });

        $("#brush01").click(function () { csInterface.evalScript('regularbrush()'); });
        $("#brush02").click(function () { csInterface.evalScript('strip()'); });
        $("#brush03").click(function () { csInterface.evalScript('thinstrip()'); });
        $("#brush04").click(function () { csInterface.evalScript('slice()'); });
        $("#brush05").click(function () { csInterface.evalScript('expander()'); });
        $("#brush06").click(function () { csInterface.evalScript('eraser()'); });

////////////////////////////////////////////////////////////////////////////////
// AD PLASTILINE LAB FLYOUT MENU
////////////////////////////////////////////////////////////////////////////////
				
		// Flyout menu XML string 
		var flyoutXML = '<Menu> \
							<MenuItem Id="AboutItem" Label="About AD Plastiline Lab"/> \
							<MenuItem Id="QuickHelp" Label="Quick Help"/> \
							<MenuItem Id="OpenWebsite" Label="AD Plastiline Lab Online Help"/> \
							\
							<MenuItem Label="---" /> \
							\
						</Menu>';

		// Uses the XML string to build the menu
		csInterface.setPanelFlyoutMenu(flyoutXML);

		// Flyout Menu Click Callback
		function flyoutMenuClickedHandler (event) {

			// The Data (Object)
			console.dir(event); 
			switch (event.data.menuId) {
				case "AboutItem":
					csInterface.evalScript("alert('AD Plastiline Lab v1.0\\n©2016 Alex Dukal\\nalexdukal.com/store', 'About Plastiline Lab');");
					break;
				case "QuickHelp":
					csInterface.evalScript("alert('1. Create a New 300Dpi Photoshop Document.\\n2. From the first menu select a Background Texture (Optional).\\n3. Create a new Layer.\\n4. Apply a Plastiline Style (Flat, Medium or Thick) and start painting.\\n5. Use the Duplicate button to add more layers with the same style or create a new one with a different style.\\n6. Use the tools inside the Tools tab to add details.\\n7. For best results put each shape in an individual layer.', 'Impastoshop Quick Help');");
					break;					
				case "OpenWebsite":
					csInterface.openURLInDefaultBrowser("http://alexdukal.com/store/plastilinelab/"); LoseFocus();
					break;
				default: 
					console.log(event.data.menuName + " clicked!");
			}
		}

		// Listen for the Flyout menu clicks
		csInterface.addEventListener("com.adobe.csxs.events.flyoutMenuClicked", flyoutMenuClickedHandler);

    }
    init();  
}());