/* Version: 4r
*/
    var WinHeight = screen.height-80;
    var contentwinfeat = 'toolbar=0,scrollbars=1,status=1,resizable=1,menubar=0,location=0,' +
                        'top=28,left=20,width=850,height=' + (WinHeight - 40);
    var listmenu = new Array();

    function CM_openBrWindow(theURL,winName,features) {
     window.open(theURL,winName,features);
    }
    
    function CM_highlight(obj, color) {
      $(obj).attr('style', 'background-color:'+color);
    }
    
    function CM_lmin(obj, idx) {
      listmenu[idx] = 1;
      $(obj).attr('style', 'background-color:#BBB');
    }
    
    function CM_lmout(obj, idx, menuid) {
      $(obj).attr('style', 'background-color:#EEE');
      CM_closelm(idx, menuid);
      //setTimeout("CM_closelm("+idx+", '"+menuid+"')", 500);
    }
    
    function CM_closelm(idx, menuid) {
      listmenu[idx] = 0;
      setTimeout("if (listmenu["+idx+"] == 0) $('#"+menuid+"').hide('slow');", 500);
    }
    
    function CM_lmclick(cmd, listID, contentID) {
      $("#CMListMenu_"+listID).hide("slow");
      callCommand(cmd, contentID, listID);
    }
    
	function callSaveOrderCommand(contentID, listID, order) {
        nocache = Math.random(); //stupid IE cashing issue
		//$("#" + contentID).attr("style", "background-color:#CCCCCC");
		$("#" + contentID).addClass("inProgress");
        $.get('/CM/CMCommands.php',
            { cmd: 'SaveItemSortOrder', contentID: contentID, listID: listID, order: order, nocache: nocache }, 
            function(data){
				$("#" + contentID).removeClass("inProgress");
                //alert(data);
            },
            'text');		
		
	}
	
	function callCommand(cmd, contentID, listID) {
      //alert("cmd= " + cmd + ", contentID= " + contentID + ", listID= " + listID);

      if (cmd == "DeleteListItem") {
        r=confirm("Do you really want to permanently delete this item?");
        if (r!=true){
          return false;
        }
      }
      
      nocache = Math.random(); //stupid IE cashing issue
      $.get('/CM/CMCommands.php',
            { cmd: cmd, contentID: contentID, listID: listID, nocache: nocache }, 
            function(data){
                if (cmd=="DeleteListItem") {
                    //document.location.reload();
                    
                    /* This code works as well: removing elements without refreshing
                    document.getElementById(id).style.display = 'none';
                    document.getElementById(id + "1").style.display = 'none';
                    document.getElementById(id + "2").style.display = 'none';
                    document.getElementById(id + "3").style.display = 'none';
                    document.getElementById(id + "4").style.display = 'none';
                    document.getElementById(id + "Edit").style.display = 'none';
                    document.getElementById(id + "Menu").style.display = 'none';
                    moveCMAreas();
                    */
                }
                //alert("cmd= " + cmd + ", contentID= " + contentID + ", listID= " + listID + ", siteid= " + siteID + ", data: " + data);
                if (cmd=="AddListItem") {
                    //document.location.reload();
                    //alert("ADD - " + data);
                }
                if (cmd=="GetContent") {
                    return (data);
                }
                document.location.reload();
            },
            'text');
   }
   
   $(function() {
//		$("#sortable").sortable();
		$(".CMList").sortable({
			 stop: function(event, ui) {
				        var newOrder = -1;
		                var myListID = this.id; 
						var myContentID = ui.item.attr('id');
						var items = $(".CMListItem");
        				for(var x=0; x<items.length; x++) {
							if (items[x].id == myContentID) {
								newOrder = x + 1;
								break;
							}
        				}
						//alert(myListID + " - " + myContentID + " - " + newOrder);
						callSaveOrderCommand(myContentID, myListID, newOrder);
					}
        });
		$("#sortable").disableSelection();		
	});
