$(document).ready(function(){

		
	if(createDialog ==1){
		
		$("#dialogDiv").dialog({ 
															autoOpen: false ,
															show: 'fold',
															hide: 'fold',
															width: 430,
															height: 350,
															modal:true
													
													});
		
	}

		$("#menu li").removeClass("active");

	
	$("#menuCat_"+menuCat).parent().addClass("active");
	$("#menu_"+menuAct).parent().addClass("active");
	$("#cvM_"+myCv).parent().addClass("active");
	
		
	
	if(typeof menuAdd != "undefined"){	
		$("#menu_"+menuAdd).parent().addClass("active");
	}	
	
		$("#menu_login").click(function(){
			$("#loginForm").toggle("fast");
		})
		
	
    $('.dataTable').dataTable({
     	"aaSorting": [[ 1, "asc" ]],
     	"bPaginate": false,
     	oLanguage:{
     	 "sSearch": "Finn:",
     	 "sInfo": "Viser totalt _TOTAL_ adresser (_START_ til _END_)"
     	}
    });


		restrictCharacters(".restrictLength",175);

		resizeBoxes("div.box3InRow div.infoBox");
		
		resizeBoxes(".nextNews");
		
		resizeBoxes("h4.subtitle");
		
		mouseMoreOver(".restrictLength");
		
	//resizeBoxes(".nextNews");
		imagePreview();

});

	

var moreInfo = new Array();
var lessInfo = new Array();	
var moreOrLess = new Array();
function logout(){
//	alert(+new Date());
	myTs =(+new Date());
	myURL = "/?do=logout&amp;cv=9&amp;lid=0&amp;time="+myTs;
	
	//alert(myURL);
	location.href=myURL;
}
function mouseMoreOver(who){
	$(who).parent().parent().css("height","auto");
	
	$(who).each(function(index){
	//	alert(index);
			thisId = $(this).attr("id"); 
			myP = 	$(this).parent().parent().attr("id");
			//alert(myP);
			
		  $("#"+myP).click(function(){
		  	//alert(thisId + " "+myP);
		  	myPid = $(this).attr("id");
		  	myId = myPid.replace("x_","");
		  	if(moreOrLess[myId]==1){
		  		$("#"+$(this).attr("id")+ " .restrictLength").html(lessInfo[myId]);
		  		moreOrLess[myId] = 0;
		  	}else {
		  		$("#"+$(this).attr("id")+ " .restrictLength").html(moreInfo[myId]);
		  		moreOrLess[myId] = 1;
		  	}
		  	
		  	
		  	
		  })
		 
	
	});
	

}


 function restrictCharacters(who, size){
 	
 	$(who).each(function(index){
 		myString = $(this).text();
 		myId = $(this).attr("id");
 		//alert(index + "\n\n"+myString);
 		
 		if(myString.length>size){
	 		myNewString = myString.slice(0, size)
	 		moreInfo[myId] = $(this).html();
	 		$(this).text(myNewString);
	 		$(this).append("<div class='clear readMore'>Les mer..</div>");
	 		lessInfo[myId] = $(this).html();
 		}else{
 		
 			$(this).parent().parent().find(".readMore").hide();
 		
 		}
 	})
 	
 
 }

var setPw=0;
this.imagePreview = function(){	
	/* CONFIG */

	xOffset = 10;
		yOffset = 30;
		if(setPw==1){
		//	alert("left");
			xOffset = 20;
		yOffset = -350;
		}
		
		
		// these 2 variable determine popup's distance from the cursor
		// you might want to adjust to get the right result
		
	
		
	/* END CONFIG */
	jQuery("a.preview").hover(function(e){
		this.t = this.title;
		this.title = "";	
		var c = (this.t != "") ? "<br/>" + this.t : "";
		jQuery("body").append("<p id='preview'><img src='"+ this.href +"' alt='Image preview' />"+ c +"</p>");								 
		jQuery("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px")
			.fadeIn("fast");						
    },
	function(){
		this.title = this.t;	
		jQuery("#preview").remove();
    });	
	jQuery("a.preview").mousemove(function(e){
		jQuery("#preview")
			.css("top",(e.pageY - xOffset) + "px")
			.css("left",(e.pageX + yOffset) + "px");
	});			
};

var h=0;
function resizeBoxes(cName){

	/*find elements first*/
	var boxes = new Array;
	if(typeof(cName)=="undefined"){
	  return;
	}

	boxes = jQuery(cName);
	
	if (h>0){
		//already set
	}else{
		for(i=0;i<boxes.length;i++){
		  if(cName=="box_content"){
				var myID = "boxId_" +i;
				boxes[i].id=myID;
			}
			
			myboxh = jQuery(boxes[i]).height();
	
			if(myboxh>h){
				h = myboxh;
			}		
		}
		
		for(j=0;j<boxes.length;j++){
	   	if(cName=="box_content"){
				var myID = "boxId_" +j;
				boxes[j].id=myID;
			}
	
			jQuery(boxes[j]).height(h);
		}
	}
	
	h=0;
}

	

function openCatInfo(catID,cvId,pos,det,catTitle){
		if(det<1){
			//alert("Det er ingenting i denne mappen, prøv en annen mappe");
			return;
		}
		var contentPos = $("#innerBody").offset();
		
		//alert("cpos: "+contentPos.left);
		
		myPos = [(contentPos.left+420),250];
		if(pos=="left"){
			myPos = [(contentPos.left+25),250];
		}
		
		$("#dialogDiv").dialog("close");
		$("#dialogDiv").dialog( "option" , "title", catTitle )
		$("#dialogDiv").dialog( "option" , "position", myPos )
				
		
		$.ajax({
  		 type: "POST", url: "/?cv="+cvId+"&do=catcont&category=" + catID+"&use_layout=layout_blank.html&template_file=catcont_medlem.html",
  		 success: function(html){$("#dialogDiv").html(html).dialog('open');}
		});




}
 treeUp=0;
 
function buildTreeList(catId){
	//console.log("----------");
	//console.log("BUILDING " +catId);
//myCatList_<TMPL_VAR int_id>"  class="filetree"></ul>
var startDepth=2;
myCatList = "#myCatList_"+catId;
myTreeList = "#buildTreeList_"+catId ;
//<div style="display:block" id="buildTreeList_
	
	//console.log(jQuery(myCatList).position());
	myPos = jQuery(myCatList).position();
	myLeft = myPos.left;
	myDialogPos = "right";
	if(myLeft>300){
			myDialogPos = "left";		
	}

 	jQuery(myTreeList +" div.catDepth_"+startDepth).each(function(index){
 		
 		
 		//console.log("build "+index);
 		mycatid = jQuery(this).attr('id');
 		mycatid = mycatid.replace("cat","");
 		myAcc = jQuery(this).hasClass("access_1");
 		myGrey = jQuery(this).hasClass("status_0");
 		
 		
 		myCompVar = jQuery("#"+jQuery(this).attr('id')).find("span.compvar").text();
 		myDetails = jQuery("#"+jQuery(this).attr('id')).find("span.numdetails").text();
 		
 	//	//console.log("COMPVAR: "+myCompVar);
 		
 		mySpanClass = "folder-closed";
 		if(myAcc){
// 			mySpanClass = "folder-closed-red";
 		}
 		if(myGrey){
 		//	mySpanClass = "folder-closed-grey";
 		}
 		
 		if(jQuery("div.parent_"+mycatid).length>0){
 			mySpanClass="folder";
 			if(myAcc){
 	//			mySpanClass = "folder-red";
 			}
 			if(myGrey){
 		//		mySpanClass = "folder-grey";
 			}
		}
		
		
	

 			myLI = "<li id='menuID_" + jQuery(this).attr('id')+"'><span class='" + mySpanClass + "'>"
 						+"\<a style='text-decoration:none;' href='javascript:openCatInfo(\"" 
 						+ mycatid + "\",\""+ myCompVar+ "\",\"" +myDialogPos + "\",\"" + myDetails + "\",\""
 						+ jQuery("#"+jQuery(this).attr('id') +" span.menuTitle").html() +"\")'>"
 						+ jQuery("#"+jQuery(this).attr('id') +" span.menuTitle").html()
 						+"\<\/a>\<\/span><\/li>";
 		
 		
 		jQuery(myCatList).append(myLI);
 		
 	});
	
	var message="";
	startDepth++;
	var idDone = new Array();
 	for (i=startDepth;i<10;i++){
 	
 		var j=0;
 		
		jQuery(myTreeList +" div.catDepth_"+i).each(function(index){
			//alert(i);
			//console.log(idDone[jQuery(this).attr("id")] + " " +jQuery(this).attr("id"));
			
			if(idDone[jQuery(this).attr("id")] !=1){
				
				idDone[jQuery(this).attr("id")] =1;
				
				myId = jQuery(this).attr("id");
				myAcc = jQuery(this).hasClass("access_1");
				myGrey = jQuery(this).hasClass("status_0");
				
				myCompVar = jQuery("#"+jQuery(this).attr('id')).find("span.compvar").text();
				myDetails = jQuery("#"+jQuery(this).attr('id')).find("span.numdetails").text();
				
				parentId = "menuID_cat" +  jQuery("#"+myId +" span.menuParent").html();
				subParentId = "submenuID_cat" +  jQuery("#"+myId +" span.menuParent").html();
				if(i==1){
 					message += i+" "+ parentId +  " - "+ subParentId+"\n";
 				}

				if(jQuery("ul#"+subParentId).length!=0){
  		
  			}else{
 					jQuery("#"+parentId).append("<ul id='sub"+parentId+"'><\/ul>");
 				}
 			
 				mycatid = jQuery(this).attr('id');
 				mycatid = mycatid.replace("cat","");
	
	 			mySpanClass="folder";
 			
 				if(myAcc){
 				//	mySpanClass = "folder-red";
 				}
 				if(myGrey){
 				//	mySpanClass = "folder-grey";
 				}
 			
	 			myLI = "\<li id='menuID_"+jQuery(this).attr("id")+"'>\<span class='"+mySpanClass+"'>\<a style='text-decoration:none;' href='javascript:openCatInfo(\"" + mycatid + "\",\""+ myCompVar+ "\",\""+myDialogPos+ "\",\""+myDetails+ "\",\""+  jQuery("#"+jQuery(this).attr('id') +" span.menuTitle").html() 
 			+ "\")'>"+jQuery("#"+jQuery(this).attr("id") +" span.menuTitle").html()+"\<\/a>\<\/span>\<\/li>";
 				
 				jQuery("#"+subParentId).append(myLI);
 			}
 		});
 	
 	}
 	
 

 	 	treeUp=1;	
 	}

