var mindviewMiddle = Titanium.UI.createView({// »õ·Î¿î window¿¡ view¼³Á¤

	backgroundColor : '#cacaca', // view ¹è°æ»ö±ò
	top : 0, // view ¹è°æÀ§
	width : 'auto', // view ³Êºñ
	backgroundImage : 'nPMMobile_Image/mindmap_Background.png' // view¹è°æ ÀÌ¹ÌÁö
});


var saveContent = "";
// ¼­ºí¸´ ÁÖ¼Ò
var servletAddr = 'http://solar4.ssu.ac.kr:8080/mobileServlet';


var mindMapInit = function() {

	// ¼­¹ö·ÎºÎÅÍ data¸¦ °¡Á®¿À±â À§ÇÑ ÀÛ¾÷
	var param = "action=init";
	var xhr = Ti.Network.createHTTPClient();

	xhr.open("POST", servletAddr);
	xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded;charset=UTF-8");
	xhr.setRequestHeader("Cache-Control", "no-cache, must-revalidate");
	xhr.setRequestHeader("Pragma", "no-cache");
	xhr.send(param);
	xhr.onload = function() {
		var doc = this.responseXML.documentElement;
		// ¼­¹ö°¡ sml ¹®ÀÚ ¹ÞÀ½
		var id = doc.getElementsByTagName('id');

		// »õ·Î¿î node »ý¼º½Ã ³ªÅ¸³¯ À§Ä¡ ÀúÀå º¯¼ö
		var height = 220;
		// var check_side=68;
		var check_side = 10;
		// var check_height=135;
		var check_height = 100;
		// var plus_side=225;
		var plus_side = 20;
		var node_Count = 0;
		var height_Count = 0;
		var upgrade_count = 0;
		
		var circle = [];   // circleÀ» ÀúÀåÇÒ ¹è¿­ ¼ºÁ¤
		var circle_r = [];   // circleÀ» ÀúÀåÇÒ ¹è¿­ ¼ºÁ¤
		var circleSize = 130; // parnetNode¸¦ ÀúÀåÇÏ´Â ¿øÀÇ Å©±â
		var circleX = 120;    // ¿øÀÇ xÁÂÇ¥
		var circleY = 0;      // ¿øÀÇ yÁÂÇ¥
		
		// idÀÇ °³¼ö¿¡ µû¶ó ÆÄ½Ì ½ÃÀÛ
		for (var i = 0; i < id.length; i++) {
			var currentItem = id.item(i);
			var mytext = currentItem.getElementsByTagName('mytext').item(0).text;
			var mynumber = currentItem.getElementsByTagName('number').item(0).text;

			num = i;
			num2 = i + 1;

			
		if(i==0){
			
			// ¹Ù±ù¿ø ¸ð¾çÀÇ view »ý¼º
			var circle[i]= Titanium.UI.createView({
				height : circleSize,  // ¿øÀÇ ³ôÀÌ ¼³Á¤
				width : circleSize,   // ¿øÀÇ Å©±â ¼³Á¤
				anchorPoint : {   // ¿øÀÇ Áß½ÉÀ» ±âÁØÀ¸·Î ÇÑ Å©±â ¼³Á¤
					x : (circleSize / 2),
					y : (circleSize / 2)
				},
				borderRadius : (circleSize / 2), // ¿øÀÇ µÑ·¹ ¼³Á¤
				backgroundColor : '#336699',   // ¿øÀÇ ¹è°æ»ö ¼³Á¤
				opacity : 0.3,
				left : check_side + node_Count * plus_side,  // ¿øÀÇ ½ÃÀÛ ¿ÞÂÊ À§Ä¡
				top : check_height + height  // ¿øÀÇ ½ÃÀÛ À§ÂÏ À§Ä¡
			});
			
			// ¾ÈÂÊ¿ø ¸ð¾çÀÇ view »ý¼º
			var circle_r[i]=Titanium.UI.createView({
				height : circleSize,   // ¿øÀÇ ³ôÀÌ ¼³Á¤
				width : circleSize,    // ¿øÀÇ Å©±â ¼³Á¤
				anchorPoint : {        // ¿øÀÇ Áß½ÉÀ» ±âÁØÀ¸·Î ÇÑ Å©±â ¼³Á¤
					x : (circleSize / 2),  
					y : (circleSize / 2)
				},
				borderRadius : (circleSize / 2),  // ¿øÀÇ µÑ·¹ ¼³Á¤
				backgroundColor : '#e6e6fa',      // ¿øÀÇ ¹è°æ»ö ¼³Á¤
				top : check_height + height,      // ¿øÀÇ ½ÃÀÛ À§ÂÊ À§Ä¡
				left : check_side + node_Count * plus_side  // ¿øÀÇ ½ÃÀÛ ¿ÞÂÊ À§Ä¡
			});
							
			// ¾ÈÂÊ ¿ø¿¡ ´ëÇÑ ÀÌº¥Æ® ÇÚµé·¯ ¼³Á¤
			circle_r[i].addEventListener('touchmove', function(e)  // ¾ÈÂÊ ¿øÀ» Å¬¸¯ÇØ¼­ ¿òÁ÷ÀÎ´Ù¸é
			{
				circle[i].setTop(e.y + circleY - (circleSize/2));  // ¹Ù±ùÂÊ ¿øÀÇ top°ªÀ» ÀÌµ¿
				circle[i]1.setLeft(e.x + circleX - (circleSize/2));  // ¹Ù±ùÂÊ ¿øÀÇ left°ªÀ» ÀÌµ¿
            });
                                
			// ¾ÈÂÊ À¢¿¡ ´ëÇÑ ÀÌº¥Æ® ÇÚµé·¯
			circle_r[i].addEventListener('touchend', function(e)  // ¿øÀÇ ¿òÁ÷ÀÓÀ» ³¡³½´Ù¸é
            {
                   	circleY = circle[i].getTop();  // ÇöÀç ¾ÈÂÊ¿øÀÇ x°ªÀ» °íÁ¤
                   	circleX = circle[i].getLeft(); // ÇöÀç ¾ÈÂÊ¿øÀÇ y°ªÀ» °íÁ¤
                    	
                   	circle_r[i].setTop(circle1.getTop()); 
                   	circle_r[i].setLeft(circle1.getLeft());
             });
                    
			// ¹Ù±ùÂÊ ¿ø¿¡ ´ëÇÑ ÀÌº¥Æ® ÇÚµé·¯ ¼³Á¤
			circle[i].addEventListener('touchmove', function(e)  // ¹Ù±ùÂÊ ¿øÀ» ¿òÁ÷ÀÎ´Ù¸é
			{
				label.setLeft(e.x);  
				label.setTop(e.y);
			});
 

		// ¹öÆ° »ý¼º ¹× ¹öÆ° ¼Ó¼º ÃÊ±âÈ­
		var parentNode_Button = Ti.UI.createButton({

		color: '#000',
		top:check_height + height,
		objName:mynumber,
		title:mytext,
		width:185,
		height:50,
		left:check_side + node_Count*plus_side
		});

		mindviewMiddle.add(parentNode_Button);

		// mind ¹öÆ° Å¬¸¯ÇÏ¿´À» ¶§ ÀÌº¥Æ® ÇÚµé·¯ È£Ãâ
		parentNode_Button.addEventListener('click', function(e) {

		saveContent = this.title + "  -->  "  // ÇöÀç Å¬¸¯ÇÑ ³ëµåÀÇ ÀÌ¸§ ÀúÀå
		mindmapAdd(this.objName, this.title); // mindmapAdd ÇÔ¼ö È£Ãâ
		});
		}
		*/
		// ÇÑÁÙ¿¡ 3°³¾¿ ³ëµå¸¦ Ç¥ÇöÇÏ±â À§ÇØ 3°³°¡ ¿Ï¼ºµÇ¸é ±× ´ÙÀ½ÁÙ·Î °³Çà
		
		else if(i%3==0){
			
			var circle[i]= Titanium.UI.createView({
				height : circleSize,
				width : circleSize,
				anchorPoint : {
					x : (circleSize / 2),
					y : (circleSize / 2)
				},
				borderRadius : (circleSize / 2),
				backgroundColor : '#336699',
				opacity : 0.3,
				left : check_side + node_Count * plus_side,
				top : check_height + height
			});
				
			var circle_r[i]=Titanium.UI.createView({
				height : circleSize,
				width : circleSize,
				anchorPoint : {
					x : (circleSize / 2),
					y : (circleSize / 2)
				},
				borderRadius : (circleSize / 2),
				backgroundColor : '#e6e6fa',
				top : check_height + height,
				left : check_side + node_Count * plus_side
			});
							
			circle_r[i].addEventListener('touchmove', function(e)
			{
				circle[i].setTop(e.y + circleY - (circleSize/2));
				circle[i]1.setLeft(e.x + circleX - (circleSize/2));
            });
                                 
			circle_r[i].addEventListener('touchend', function(e)
            {
                   	circleY = circle[i].getTop();
                   	circleX = circle[i].getLeft();
                    	
                   	circle_r[i].setTop(circle1.getTop());
                   	circle_r[i].setLeft(circle1.getLeft());
             });
                    
			circle[i].addEventListener('touchmove', function(e)
			{
				label.setLeft(e.x);
				label.setTop(e.y);
			});	

				
		height_Count++;
		node_Count = 0;

		var parentNode_Button = Ti.UI.createButton({

		color: '#000',
		 top:check_height + height_Count*check_height + height,
		objName:mynumber,
		top:40,
		title:mytext,
		width:120,
		height:50,
		 left:check_side + node_Count*plus_side
		});

	    mindviewMiddle.add(parentNode_Button);
		parentNode_Button.addEventListener('click', function(e) {

		saveContent = this.title + "  -->  "
		mindmapAdd(this.objName, this.title);
		});
		}
		// Ã³À½ ³ëµå »ý¼ºÀÌ ¾Æ´Ï°í 2¹øÂ° ÁÙÀÇ 2¹øÂ° ³ëµåºÎÅÍ »ý¼ºÇÒ ¶§
		else if(i%3!=0 & node_Count!=0){
			
			var circle[i]= Titanium.UI.createView({
				height : circleSize,
				width : circleSize,
				anchorPoint : {
					x : (circleSize / 2),
					y : (circleSize / 2)
				},
				borderRadius : (circleSize / 2),
				backgroundColor : '#336699',
				opacity : 0.3,
				left : check_side + node_Count * plus_side,
				top : check_height + height
			});
				
			var circle_r[i]=Titanium.UI.createView({
				height : circleSize,
				width : circleSize,
				anchorPoint : {
					x : (circleSize / 2),
					y : (circleSize / 2)
				},
				borderRadius : (circleSize / 2),
				backgroundColor : '#e6e6fa',
				top : check_height + height,
				left : check_side + node_Count * plus_side
			});
							
			circle_r[i].addEventListener('touchmove', function(e)
			{
				circle[i].setTop(e.y + circleY - (circleSize/2));
				circle[i]1.setLeft(e.x + circleX - (circleSize/2));
            });
                                 
			circle_r[i].addEventListener('touchend', function(e)
            {
                   	circleY = circle[i].getTop();
                   	circleX = circle[i].getLeft();
                    	
                   	circle_r[i].setTop(circle1.getTop());
                   	circle_r[i].setLeft(circle1.getLeft());
             });
                    
			circle[i].addEventListener('touchmove', function(e)
			{
				label.setLeft(e.x);
				label.setTop(e.y);
			});	

		var parentNode_Button = Ti.UI.createButton({

		color: '#000',
		top:check_height + height_Count*check_height +  height,
		objName:mynumber,
		title:mytext,
		width:185,
		height:50,
		left:check_side + node_Count*plus_side
		});

		mindviewMiddle.add(parentNode_Button);

		parentNode_Button.addEventListener('click', function(e) {

		saveContent = this.title + "  -->  "
		mindmapAdd(this.objName, this.title);
		});

		}
		// À§ÀÇ 3°¡Áö °æ¿ì°¡ ¾Æ´Ò ¶§
		else{
			
			var circle[i]= Titanium.UI.createView({
				height : circleSize,
				width : circleSize,
				anchorPoint : {
					x : (circleSize / 2),
					y : (circleSize / 2)
				},
				borderRadius : (circleSize / 2),
				backgroundColor : '#336699',
				opacity : 0.3,
				left : check_side + node_Count * plus_side,
				top : check_height + height
			});
				
			var circle_r[i]=Titanium.UI.createView({
				height : circleSize,
				width : circleSize,
				anchorPoint : {
					x : (circleSize / 2),
					y : (circleSize / 2)
				},
				borderRadius : (circleSize / 2),
				backgroundColor : '#e6e6fa',
				top : check_height + height,
				left : check_side + node_Count * plus_side
			});
							
			circle_r[i].addEventListener('touchmove', function(e)
			{
				circle[i].setTop(e.y + circleY - (circleSize/2));
				circle[i]1.setLeft(e.x + circleX - (circleSize/2));
            });
                                 
			circle_r[i].addEventListener('touchend', function(e)
            {
                   	circleY = circle[i].getTop();
                   	circleX = circle[i].getLeft();
                    	
                   	circle_r[i].setTop(circle1.getTop());
                   	circle_r[i].setLeft(circle1.getLeft());
             });
                    
			circle[i].addEventListener('touchmove', function(e)
			{
				label.setLeft(e.x);
				label.setTop(e.y);
			});	
		node_Count = upgrade_count;

		var parentNode_Button = Ti.UI.createButton({

		color: '#000',
		top:check_height  + height,
		objName:mynumber,
		title:mytext,
		width:185,
		height:50,
		left:check_side + node_Count*plus_side
		});

		 mindviewMiddle.add(parentNode_Button);

		parentNode_Button.addEventListener('click', function(e) {

		saveContent = this.title + "  -->  "
		mindmapAdd(this.objName, this.title);
		});
		}

		node_Count++;
		upgrade_count++;

		}
	
		// ¹öÆ° »ý¼º ¹× ¼Ó¼º ÃÊ±âÈ­
		var useMind = Ti.UI.createButton({
			top : 47,
			title : '¼³¸í',
			width : 40,
			height : 'auto',
			right : 45
		});

		// ¹öÆ° »ý¼º ¹× ¼Ó¼º ÃÊ±âÈ­
		var btnBack = Titanium.UI.createButton({

			title : 'µÚ·Î',
			width : 40,
			height : 'auto',
			right : 2,
			top : 47

		});

		// ¼³¸í ¹öÆ°¿¡ ÀÌº¥Æ® ÇÚµé·¯ ºÎ¿©
		useMind.addEventListener('click', function(e) {
			alert('¿øÇÏ´Â ³ëµå¸¦ Å¬¸¯ÇÏ¼¼¿ä!');
		});

		// µÚ¿À ¹öÆ°¿¡ ÀÌº¥Æ® ÇÚµé·¯ ºÎ¿©
		btnBack.addEventListener('click', function(e) {
			mindmap_Win.close();
			main_Win.open();
			
			// º¸ÀÌ´Â ¸ðµç ¿øÀ» ¾ø¾Ø´Ù.
			for ( i = 0; i < num; i++) {
				circle[i].hide();
				circle_r[i].hide();
			}
		});
		// ÇöÀç À©µµ¿ì¿¡ ¼Ó¼º Ãß°¡ÇÔ
		 mindviewMiddle.add(btnBack);
		 mindviewMiddle.add(useMind);
	
	}
	 mindmap_Win.add(mindviewMiddle);
};