﻿
function letteringManager()
{
    this.clearLettering = function(){
        dojo.byId('letteringManager').innerHTML = "";
    };
    this.writeLettering = function(){
          direction = getCurrentDirection();
          dojo.query(".imageContainer").forEach(function(n){
		    if(n.style.display != "none" && globalPropertiesArray[n.id].location == direction && n.id != themeManager.sideThemeElement && n.id != themeManager.hoodThemeElement)
		    {
		        var editLink = document.createElement('span');
		        dojo.byId("letteringManager").appendChild(editLink);        
		        dojo.addClass(editLink, "editLink");
        		editLink.setAttribute('id',"mananger"+n.id);
        		
		        var newLink = document.createElement('a');
		        newLink.setAttribute('href','javascript:letteringMenu(true,\''+n.id+'\');');
		        if(globalPropertiesArray[n.id].type=='text')
		        {
		            newLink.innerHTML = globalPropertiesArray[n.id].text.substring(0,10);
                }
                if(globalPropertiesArray[n.id].type=='image')
                {
                    newLink.innerHTML = "Custom Upload";
                }
                if(globalPropertiesArray[n.id].type=='clipart')
                {
                    newLink.innerHTML = "Custom Clipart";
                }
                editLink.appendChild(newLink);
		        //dataArray[n.id] = globalPropertiesArray[n.id].clone();
		     
		         var newImage = document.createElement('img');
                 newImage.setAttribute('src','images/delete.png');
                 newImage.setAttribute('alt','Delete');
		         
		        var deleteLink = document.createElement('a');
		        deleteLink.setAttribute('href','javascript:deleteObject(\''+n.id+'\')');
		        deleteLink.setAttribute('alt','Delete this item');
		        deleteLink.appendChild(newImage);	         

                editLink.appendChild(deleteLink);         
		        
		    }
	     });    
    };
    
    this.showLettering = function(){
          direction = getCurrentDirection();
          dojo.query(".imageContainer").forEach(function(n){
            if(globalPropertiesArray[n.id].location == direction)
            {
                dojo.byId(n.id).style.visibility = "visible";
            }
            else
            {
                dojo.byId(n.id).style.visibility = "hidden";
            }
            if(n.id == themeManager.hoodThemeElement)
            {
                if(direction == 'side')
                    dojo.byId(n.id).style.visibility = "hidden";
                if(direction == 'hood')
                    dojo.byId(n.id).style.visibility = "visible";
            }            
            if(n.id == themeManager.sideThemeElement)
            {
                if(direction == 'hood')
                    dojo.byId(n.id).style.visibility = "hidden";
                if(direction == 'side')
                    dojo.byId(n.id).style.visibility = "visible";
            }
        });
    };
}

function themeClass()
{
    this.sideThemeElement = "";
    this.hoodThemeElement = "";
    this.sideThemeSizes = new dojox.collections.ArrayList();
    this.hoodThemeSizes = new dojox.collections.ArrayList();
    
    this.syncMenus = function(){
        if(this.sideThemeElement != "")
        {
            var objList = dojo.byId("themeList");
            for(i=0;i< objList.options.length;i++)
            {
                if(objList.options[i].value==globalPropertiesArray[this.sideThemeElement].filename)
                    objList.selectedIndex = i;
            }
            updateSizeDisplay(this.sideThemeElement);
        }
        else
        {
            dojo.byId("themeList").selectedIndex = 0;
        }
        if(this.hoodThemeElement != "")
        {
            var objList = dojo.byId("hoodThemeList");
            for(i=0;i< objList.options.length;i++)
            {
                if(objList.options[i].value==globalPropertiesArray[this.hoodThemeElement].filename)
                    objList.selectedIndex = i;
            }               
        }  
        else
        {
            dojo.byId("hoodThemeList").selectedIndex = 0;
        }  
    };
    
    this.removeTheme = function(){
       if(globalCurrentStep == 2)
       {
             if(this.sideThemeElement != "")
             {
                deleteObject(this.sideThemeElement);
                this.sideThemeElement = "";
             }
       }
       else
       {
            if(this.hoodThemeElement != "")
            {
                deleteObject(this.hoodThemeElement);
                this.hoodThemeElement = "";
            }
       }
    };
    
    this.enableTheme = function(theme){
       if(globalCurrentStep == 2)
       {
            if(this.sideThemeElement != "")
            {
                var value = this.getSizes(theme);
                globalPropertiesArray[this.sideThemeElement].filename = value.filename;
                /*globalPropertiesArray[this.sideThemeElement].t = value.t;
                globalPropertiesArray[this.sideThemeElement].l = value.l;
                dojo.byId(this.sideThemeElement).style.left = value.l;
                dojo.byId(this.sideThemeElement).style.top = value.t;
                globalPropertiesArray[this.sideThemeElement].w = value.w;
                globalPropertiesArray[this.sideThemeElement].h = value.h;*/
                globalSelectedElement = this.sideThemeElement;
                generatePreview();
                this.setZ();
                clipImage(true);
            }
            else
            {     
                newClipart(theme,false,true);
                this.sideThemeElement = globalSelectedElement;
                this.setZ();
                clipImage(true);
            }
       }
       else
       {
            if(this.hoodThemeElement != "")
            {
                var value = this.getSizes(theme);
                globalPropertiesArray[this.hoodThemeElement].filename = value.filename;
                globalPropertiesArray[this.hoodThemeElement].t = value.t;
                globalPropertiesArray[this.hoodThemeElement].l = value.l;
                dojo.byId(this.hoodThemeElement).style.left = value.l;
                dojo.byId(this.hoodThemeElement).style.top = value.t;
                globalPropertiesArray[this.hoodThemeElement].width = value.w;
                globalPropertiesArray[this.hoodThemeElement].height = value.h;
                globalSelectedElement = this.hoodThemeElement;
                generatePreview();
                this.setZ();
                clipImage(true);
            }
            else
            {
                newClipart(theme,false,true);
                this.hoodThemeElement = globalSelectedElement;
                this.setZ();
                clipImage(true);
            }
       }
    };  
    
    this.setZ = function(){
       if(globalCurrentStep == 2)
       {
            if(this.sideThemeElement != "")
            {
                var targetNode = dojo.byId(this.sideThemeElement);
		        targetNode.style.zIndex = 1;
		    }
       }
       else
       {
            if(this.hoodThemeElement != "")
            {
                var targetNode = dojo.byId(this.hoodThemeElement);
		        targetNode.style.zIndex = 1;
            }
       }

    };
    
    this.getSizes = function(filename){
       if(globalCurrentStep == 2)
       {
            var returnValue;
            var i = 0;
            this.sideThemeSizes.forEach(function(n){
                if(n.filename == filename)
                {                    
                    returnValue =  n;
                }
                i++;
             });  
            return returnValue;
       }
       else
       {
            var returnValue;
            var i = 0;
            this.hoodThemeSizes.forEach(function(n){
                if(n.filename == filename)
                {                    
                    returnValue =  n;
                }
                i++;
             });  
            return returnValue;
       }
    };  
    
    this.edit = function(){
        if(globalCurrentStep == 2)
       {
            if(this.sideThemeElement != "")
            {
                var targetNode = dojo.byId(this.sideThemeElement);
		        targetNode.style.zIndex = 999;
		        dojo.query(".dojoxResizeHandle*").style("zIndex","1000");
		    }
       }
       else
       {
            if(this.hoodThemeElement != "")
            {
                var targetNode = dojo.byId(this.hoodThemeElement);
		        targetNode.style.zIndex = 999;
		        dojo.query(".dojoxResizeHandle*").style("zIndex","1000");
		    }
       }        
    };
}


/* define class to store object information */

//when this is added to, update paste() section also
function globalProperties() {
    this.colorId = 1;
    this.colorValue = "000000";
    this.colorType = 'standard';
    this.text = "";
    this.height = "";
    this.width = "";
    this.font = "Arial";
    this.scale = 0;
    this.ratio = 0;
    this.bold = false;
    this.italic = false;
    this.shadow = false;
    this.shadowColorValue = "000000";
    this.shadowColorId = 1;
    this.shadowColorType = 'standard';
    this.outline = false;
    this.outlineColorValue = "000000";
    this.outlineColorId = 1;
    this.outlineColorType = 'standard';
    this.outlineTwo = false;
    this.outlineTwoColorValue = "000000";
    this.outlineTwoColorId = 1;
    this.outlineTwoColorType = 'standard';
    this.textAlign = "center";
    this.type = "text";
    this.filename = "";
    this.active = true;
    this.l = 0;
    this.t = 0;
    this.flipVertical = false;
    this.flipHorizontal = false;
    this.allowColorChange = false;
    this.location = "side"; //top, hood, side
    this.measuredHeight = 0;
    this.measuredWidth = 0;
    this.useCustomSize = false;
    this.price = 0;
    this.gradient = false;
    this.gradientTopColorValue = "000000";
    this.gradientTopColorId = 1;
    this.gradientBottomColorValue = "FFFFFF";
    this.gradientBottomColorId = 2;
}  

globalProperties.prototype.clone = function() {
  var newObj = (this instanceof Array) ? [] : {};
  for (i in this) {
    if (i == 'clone') continue;
    if (this[i] && typeof this[i] == "object") {
      newObj[i] = this[i].clone();
    } else newObj[i] = this[i]
  } return newObj;
};

globalFirstSelect = true;
globalPreviousLines = 0;
globalExtendBorder = false;
var globalElementUpdate;
var initDND = function(){
    
	//create moveable objects
	dojo.query(".imageContainer").forEach(function(n){
		new dojo.dnd.Moveable(n.id);
		globalPropertiesArray[n.id] = new globalProperties(); //initialize properties class
	 });	
	
	dojo.query(".dojoxResizeHandle*").style("display","none"); //clear drag handles on load
	
	dojo.subscribe("/dnd/move/start", function(mover)
									{ 	
										//console.debug("Start move", mover);
										
										if(mover.node.id.indexOf("imagebox") > -1) //don't want this firing for anything other than image boxes
										{	
									   			
									        if(globalSelectedElement != mover.node.id || globalFirstSelect)
										        changeControlPanel(mover.node.id);
										        
										    globalFirstSelect = false; 									    
										    globalSelectedElement = mover.node.id;	
										    									
										    coords = dojo.coords(dojo.byId(globalSelectedElement),true);
										    globalPropertiesArray[globalSelectedElement].t = Math.round(coords.t);
										    globalPropertiesArray[globalSelectedElement].l = Math.round(coords.l);
										    globalEventStack.push({operation: "move", element: globalSelectedElement, t: coords.t, l: coords.l});
										    dojo.query(".dojoxResizeHandle*").style("display","none");
										   
										}
									}); 
	dojo.subscribe("/dnd/move/stop", function(mover)
									{ 
									    if(mover.node.id.indexOf("imagebox") > -1) //don't want this firing for anything other than image boxes
										{										   											    
										    globalSelectedElement = mover.node.id;										
										    coords = dojo.coords(dojo.byId(globalSelectedElement),true);
										    globalPropertiesArray[globalSelectedElement].t = Math.round(coords.t);
										    globalPropertiesArray[globalSelectedElement].l = Math.round(coords.l);									   
										}
										 									
									}); 

	dojo.query(".imageContainer img").forEach(function(n){
			dojo.connect(dojo.byId(n.id), "onmouseover", function(mover){showHover(mover);});
			dojo.connect(dojo.byId(n.id), "onmouseout", function(mover){hideHover(mover); });
		 });

	dojo.subscribe("/layout/ResizeHandle/OnResize",function(e)
									{ 

										globalEventStack.push({operation: "resize", element: globalSelectedElement, w: coords.w, h: coords.h, t: coords.t, l: coords.l, x: coords.x, y: coords.y}); 
										
									});		
									
	dojo.subscribe("/layout/ResizeHandle/OnResizeEnd",function(e)
									{ 				
									    coords = dojo.coords(dojo.byId(globalSelectedElement),true);
									    globalPropertiesArray[globalSelectedElement].t = Math.round(coords.t);
										globalPropertiesArray[globalSelectedElement].l = Math.round(coords.l);					    
                                        generatePreview();
									});	
	
	//initialize menus
	dojo.query(".imageContainer").forEach(function(n){
			dijit.byId('submenu1').bindDomNode(n);  // bind menu to all imagebox elements
		 });	

	dijit.byId('submenu2').bindDomNode("contentholder");
    
    
    globalTypeList = new dojox.collections.ArrayList();
    globalElementUpdate = new dojox.collections.ArrayList();
    
	//use an array list to manage our Zindex
	//this simplifies the process of moving around objects
	globalZList = new dojox.collections.ArrayList();
	dojo.query(".imageContainer").forEach(function(n){
	 currentZ = parseInt(dojo.style(n,"zIndex"));
	 globalZList.insert(currentZ,n.id);
 	});
	 
	 
	 currentLettering = new letteringManager();
	 currentLettering.clearLettering();
	 
	 themeManager = new themeClass();
	 
	 globalEventStack = new dojox.collections.Stack();
	 globalRedoStack = new dojox.collections.Stack();	 


}; 
dojo.addOnLoad(initDND); 

