var obj ;
var Sessions;
var innerObj;
$(document).ready(function(){
	$.ajax({
	   url:"views/ajax?view_name=session&view_display_id=default",
	   dataType:"text",
       error:function(e){},
	   success:function(data){
			obj=$.evalJSON(data);
			Sessions.obj=obj;
			Sessions.sessArr=new Array();
			Sessions.sessNames=new Array();
			output="";
			$.each(obj,function(key,innerObj){
				innerObj.node_title="chk_"+(
					innerObj.node_title.replace(",","").replace("&","and").split(" ")).join("_").toLowerCase();
				innerObj.ismatch=false;
				innerObj.element=false;
				inputs=$("input");
				$.each(inputs,function (key,inpInnerObj){
					if(inpInnerObj.type=="checkbox"){
						var i=0;
						if(inpInnerObj.name == innerObj.node_title){
							innerObj.ismatch=true;
							innerObj.element=inpInnerObj;
							if(Sessions.sessArr[innerObj.node_data_field_session_field_session_value]){
								Sessions.sessArr[innerObj.node_data_field_session_field_session_value].objects.push(innerObj);
							}else{
								Sessions.sessArr[innerObj.node_data_field_session_field_session_value]=new Object();
								Sessions.sessArr[innerObj.node_data_field_session_field_session_value].objects=new Array();
								Sessions.sessArr[innerObj.node_data_field_session_field_session_value].enabled=true;
								Sessions.sessArr[innerObj.node_data_field_session_field_session_value].checked=false;
								Sessions.sessArr[innerObj.node_data_field_session_field_session_value].objects.push(innerObj);
								Sessions.sessNames.push(innerObj.node_data_field_session_field_session_value);
								i++;
							}
						}
					}
				});
			});
			inputs = $("input");
			$.each(inputs,function(key,innerObj){
				if(eval('(typeof(innerObj) != "undefined" );')){
					if(innerObj.type=="checkbox"){
						innerObj.onclick=(function(e){
							e=window.event||e;
							me=this;
							if(me.checked==false){
								$.each(Sessions.obj,function(key,innerObj){
									if(me.name==innerObj.node_title){
										Sessions.sessArr[innerObj.node_data_field_session_field_session_value].checked=false;
										Sessions.EnableSession(innerObj.node_data_field_session_field_session_value);
										Sessions.DisableAdjacentUnchecked();
										Sessions.CheckNonChecked();
									}
								});
							}else{
								$.each(Sessions.obj,function(key,innerObj){
									if(me.name==innerObj.node_title){
										Sessions.sessArr[innerObj.node_data_field_session_field_session_value].checked=true;
										Sessions.DisableSession(innerObj.node_data_field_session_field_session_value);
										Sessions.DisableAdjacentUnchecked();
									}
								});
							}
						});
					}
				}
			});
		}
	});
});
$(window).unload(function(){
	inputs=$("input");
	$.each(inputs,function(key,innerObj){
		innerObj.disabled=true;
	});
});
var Sessions=Sessions||{};
Sessions.DisableEverything=function(){
	inputs=$("input");
	$.each(inputs,function(key,innerObj){
		innerObj.disabled=true;
	});
}
Sessions.EnableEverything=function(){
	inputs=$("input");
	$.each(inputs,function(key,innerObj){
		innerObj.disabled=false;
	});
}
Sessions.SetupHandlers=function(){
	inputs=$("input");
	$.each(inputs,function(key,innerObj){
		innerObj.disabled=true;
	});
}
Sessions.EnableSession=function(sessionName){
	if(Sessions.sessArr[sessionName]){
		$.each(Sessions.sessArr[sessionName].objects,function(key,obj){
			if(obj.element.checked!=true){
				obj.element.disabled=false;
				obj.element.style.background="none";
			}
		});
	}
}
Sessions.DisableSession=function(sessionName){
	if(Sessions.sessArr[sessionName]){
		$.each(Sessions.sessArr[sessionName].objects,function(key,obj){
			if(obj.element.checked!=true){
				obj.element.disabled=true;
			}
		});
	}
}
Sessions.DisableUncheckSession=function(sessionName){
	if(Sessions.sessArr[sessionName]){
		$.each(Sessions.sessArr[sessionName].objects,function(key,obj){
			if(obj.element.checked==true){
				obj.element.checked=false;
			}
			obj.element.disabled=true;
		});
	}
}
Sessions.CheckNonChecked=function(){
	var checkCount=0;
	for(i=0;i<Sessions.sessNames.length;i++){
		if(Sessions.sessArr[Sessions.sessNames[i]].checked==true){
			checkCount++;
		}
	}
	if(checkCount<=0){
		Sessions.EnableEverything();
	}
}
Sessions.DisableAdjacentUnchecked=function(){
	var i=0;
	var low=2147483647;
	var high=-1;
	if(Sessions.sessArr ){
		for(i=0;i<Sessions.sessNames.length;i++){
			if(Sessions.sessArr[Sessions.sessNames[i]].checked==true){
				if(low>i){
					low=i-1;
				}
				if(high<i+1){
					high=i+1;	
				}
			}
		}
		if(low<0){low=0;}
		for(i=0;i<Sessions.sessNames.length;i++){
			if(i<low||i>high){
				Sessions.DisableSession(Sessions.sessNames[i]);
			}else if(Sessions.sessArr[Sessions.sessNames[i]].checked==false){
				Sessions.EnableSession(Sessions.sessNames[i]);
			}
			if (i>low&&i<high&&Sessions.sessArr[Sessions.sessNames[i]].checked==false){
				Sessions.EnableSession(Sessions.sessNames[i]);
				for(j=i+1;i<=high;i++){
					Sessions.sessArr[Sessions.sessNames[j]].checked=false;
					Sessions.DisableUncheckSession(Sessions.sessNames[j]);
				}
				break;
			}
		}
	}
}