// reference local blank image
Ext.BLANK_IMAGE_URL = '../extjs/resources/images/default/s.gif';

Ext.namespace('Appt', 'Appt.application');
 
Appt.application.MyDragZone = function(el, config) {
    config = config || {};
    Ext.apply(config, {
        ddel: document.createElement('div')
    });
    Appt.application.MyDragZone.superclass.constructor.call(this, el, config);

};
 
Ext.extend(Appt.application.MyDragZone, Ext.dd.DragZone, {
    getDragData: function(e) {
        var target = Ext.get(e.getTarget());
        if(target.hasClass('dd-ct')) {
            return false;
        }
        return {ddel:this.ddel, item:target};
    },
    onInitDrag: function(e) {
        this.ddel.innerHTML = this.dragData.item.dom.innerHTML;
        this.ddel.className = this.dragData.item.dom.className;
        this.ddel.style.width = this.dragData.item.getWidth() + "px";
        this.proxy.update(this.ddel);
    },
    getRepairXY: function(e, data) {
        data.item.highlight('#e8edff');
        return data.item.getXY();
    }
});
 
Appt.application.MyDropTarget = function(el, config) {
	Appt.application.MyDropTarget.superclass.constructor.call(this, el, config);
};

Ext.extend(Appt.application.MyDropTarget, Ext.dd.DropTarget, {
    notifyDrop: function(dd, e, data) {
        this.el.removeClass(this.overClass);
        this.el.appendChild(data.item);    
        changeTime();
        sortNodes(document.getElementById(this.el.id)); 
        dataChanged('diskImage');
        return true;
    }
});

function sortNodes(container){
	var nodeList = container.childNodes;
	var array = new Array(nodeList.length);
	for (var i=0;i<nodeList.length;i++){
		array[i] = nodeList[i];
	}
	array.sort(alphabetical);
	for (var j=0;j<array.length;j++){
		container.appendChild(array[j]);
	}
}

function alphabetical(a, b)
{
	var A = "";
	var B = "";
	if (a.childNodes[0]!==null && a.childNodes[0]!==undefined){
		A = a.childNodes[0].nodeValue.toLowerCase();
	}
	if (b.childNodes[0]!==null && b.childNodes[0]!==undefined){
    	B = b.childNodes[0].nodeValue.toLowerCase();
	}
	if (A.indexOf("promotions -")!=-1 && B.indexOf("promotions -")==-1){
		return -1;
	}
	else if(A.indexOf("promotions -")==-1 && B.indexOf("promotions -")!=-1){
		return 1;
	}
	else if(A.indexOf("promotions -")!=-1 && B.indexOf("promotions -")!=-1){
		return 0;
	}
	else if (A < B){
       return -1;
    }else if (A > B){
      return  1;
    }else{
      return 0;
    }
}

function moveServices(source,target){
	var services = document.getElementById(source).childNodes;
	var dropZone = document.getElementById(target);
	for (var i=0;i<services.length;i++){		
		if (services[i].className == "dd-item-highlighted"){
			services[i].className = "dd-item";
			dropZone.appendChild(services[i]);
			i--;
		}				
	}	
	changeTime();
	sortNodes(dropZone);	
	dataChanged('diskImage');
}

function moveAllServices(source,target){
	var services = document.getElementById(source).childNodes;
	var dropZone = document.getElementById(target);
	for (var i=0;i<services.length;i++){	
		services[i].className = "dd-item";
		dropZone.appendChild(services[i]);		
		i--;
	}	
	changeTime();
	sortNodes(dropZone);	
	dataChanged('diskImage');
}

function selectDivToHighlight(evt){
	if (evt.id===undefined){
		var e_out;
		var ie_var = "srcElement";
		var moz_var = "target";	
		evt[moz_var] ? e_out = evt[moz_var]["id"] : e_out = evt[ie_var]["id"];			
	
		Appt.application.highlightDiv = e_out;
	}else{
		Appt.application.highlightDiv = evt.id;
	}	
}

function highlightDiv(evt){
	var id;
	if (evt.id===undefined){		
		var ie_var = "srcElement";
		var moz_var = "target";	
		evt[moz_var] ? id = evt[moz_var]["id"] : id = evt[ie_var]["id"];
	}else{
		id = evt.id;
	}

	if (id == Appt.application.highlightDiv){
		var div = document.getElementById(id);
		if (div.className == "dd-item"){
			div.className = "dd-item-highlighted"
			return
		}
		div.className = "dd-item";
	}
}

function changeTime(){
	while (document.getElementById("draghelp") != null){
		var dragHelp = document.getElementById("draghelp");
		dragHelp.parentNode.removeChild(dragHelp);		
	}
	
	// Very bad hack so I can use Drag drop elsewhere and not break main appts
	if (document.getElementById("startHour")!=null){
	
		var container = document.getElementById("dd2-ct");
		var label;		
		var hours = 0;
		var mins = 0;		
		
		for (i=0;i<container.childNodes.length;i++){		
			label = container.childNodes[i].id;
			hours += Number(label.substring(label.indexOf("|")+1,label.lastIndexOf("|"))); 
		    mins  += Number(label.substring(label.lastIndexOf("|")+1));			
		}
		
		hours += Math.floor(mins/60);
		mins = mins%60;
	  
	    startHour = Number(document.getElementById("startHour").value) + hours + Math.floor((Number(document.getElementById("startMin").value) + mins)/60);
	    startMin = (Number(document.getElementById("startMin").value) +  mins)%60;
	    startHour = ""+startHour;
	    startMin = ""+startMin;
	    if (startHour.length == 1){
	        startHour = "0" + startHour;
	    }
	    if (startMin.length == 1){
	        startMin = "0" + startMin;
	    }    
	    document.getElementById("endHour").value = startHour;
	    document.getElementById("endMin").value = startMin;
	    document.getElementById("updatedEndTimeHour").value = startHour;
	    document.getElementById("updatedEndTimeMin").value = startMin;    
	    
	        
	    var header = document.getElementById("availableTime");
	    var hoursLeft = Number(document.getElementById("availableTimeHour").value) - hours;    
	    var minsLeft = Number(document.getElementById("availableTimeMin").value);
		if (minsLeft < mins){		
			hoursLeft = (hoursLeft-1);
			mins = mins-minsLeft;
			if (mins==0){
				minsLeft=0;
			}
			else{
				minsLeft = 60-mins;
			}
		}
		else{
			minsLeft = (minsLeft-mins);
		}
		
		if (hoursLeft < 0){
			if (minsLeft > 0){
				hoursLeft = Number(hoursLeft)+1;
				minsLeft = 60 - Number(minsLeft);
			}		
			hoursLeft = Math.abs(hoursLeft);
			header.innerHTML="<font color='red'>You have exceeded the time available before the next appointment or closing time by " + hoursLeft +" hour(s) and " + minsLeft +" minutes, please remove a service that will bring you back withn the allotted time. Please call us to discuss if this selection is necessary as we may be able to help.</font>"; 
		}
		else{
			header.innerHTML="You have <font color='red'>"+hoursLeft+"</font> hrs and <font color='red'>"+minsLeft+"</font> mins available before the next appointment or closing time";
		}
	}
} 

function addSelectedService(select){
	var id = select.options[select.selectedIndex].value;
	if (id!="none"){
		var div = document.getElementById(id.substring(3));
		if (div.className == "dd-item"){
			div.className = "dd-item-highlighted"			
		}
		else{
			div.className = "dd-item";
		}
		if (div.parentNode.id == "dd1-ct"){
			moveServices("dd1-ct","dd2-ct");
		}
		else{
			moveServices("dd2-ct","dd1-ct");
		}
	}	
	
}
