/**
 *
 * @param {Object} 'a.closeEl'
 * @version 1.0
 * @author jiangsj
 */

var sortable_is_sort = false;

$(document).ready(function(){
	if(init_channel_id != "" && isSystem == "1"){
		$.get("./index.php?r=Layout.get_module_template&rd="+Math.random(),{channel_id:init_channel_id},function(html){
			if(html != ""){
				$("#itemSystem").html(html);
			}
		});
	}

    $("div.groupWrapper").sortable({
        opacity: 0.9,
        tolerance: 'pointer',
        placeholder: 'ui-sortable-placeholder-parent',
        items: 'div.groupItem',
        handle: 'div.itemHeader',
		cancel: '.ui-state-disabled',
        update: function(){
			var me = $(this);
			$.ajax({
				type:'get',
				async:false,
				url:"index.php?r=Layout.sortline&rd="+Math.random(),
				data:me.sortable("serialize"),
				dataType:'json',
				success:function(data){
					if (data.status != 1) {
                        me.sortable('cancel');
                    }else{
						i=1;
						me.children(".groupItem").each(function(){
							$(this).children(".itemHeader").children(".itemTitle").html("第<br>"+i+"<br>行");
							i++;
						});
					}
				}
			});
        },
        stop: function(){
        }
    });
    $("div.groupWrapper").disableSelection();
    
    $("div.itemContent").sortable({
        connectWith: 'div.itemContent',
        opacity: 0.9,
        items: '.groupChildItem',
		tolerance: 'pointer',
		cancel: '.ui-state-disabled',
		placeholder:'ui-sortable-placeholder',
		out:function(event,ui){
			var channelid = $(ui.item).attr("channel");
			var lineid = $(this).parent().attr("line");
			var me = $(this);
			sortable_is_sort = false;
			$.ajax({
				type:'get',
				async:false,
				url:"index.php?r=Layout.sortmodule&rd="+Math.random(),
				data:me.sortable("serialize")+'&channel_id='+channelid+'&line_id='+lineid,
				dataType:'json',
				success:function(data){
					if(data.status == 1){
						sortable_is_sort = true;
						$(ui.item).attr("line",lineid);
					}else{
						alert("不能移动频道！");
						$(ui.sender).sortable('cancel');
					}
				}
			});
			
		},
        update: function(event,ui){
			sortable_is_sort = true;
        },
        stop: function(event,ui){
			var me = $(this);
			if(sortable_is_sort){
				$.ajax({
				type:'get',
				async:false,
				url:"index.php?r=Layout.sortmodule&rd="+Math.random(),
				data:me.sortable("serialize"),
				dataType:'json',
				success:function(data){
					if (data.status == 1) {
                        //me.sortable('cancel');
                    }
				}
			});
			}
			
			sortable_is_sort = false;
        }
    });
    
    $("div.itemContent").disableSelection();
    
    /*操作动作*/
    
    $(".cpanel_tool_add_ubb").live("click", function(){
		var line = $(this).attr("line");
        var channel = $(this).attr("channel");
		
		$.ajax({
			url:"index.php?r=Layout.addubb",
			type:"get",
			cache:false,
			data:{
				line_id: line,
            	channel_id: channel
			},
			success:function(html){
				window.parent.$("#helperBox").empty().html(html);
			}
		});
		/*
		window.parent.createbox("addubb", "新增UBB", 760, "index.php?r=Layout.addubb&rd="+Math.random(), {
            line_id: line,
            channel_id: channel
        });
        */
    });
	$(".cpanel_tool_add_channel").live("click", function(){
		var line = $(this).attr("line");
        var channel = $(this).attr("channel");
		
		$.ajax({
			url:"index.php?r=Layout.addchannel",
			type:"get",
			cache:false,
			data:{
				line_id: line,
				channel_id:channel
			},
			success:function(html){
				window.parent.$("#helperBox").empty().html(html);
			}
		});
		/*
		window.parent.createbox("addchannel", "新增频道", null, "index.php?r=Layout.addchannel&rd="+Math.random(), {
            line_id: line,
            channel_id: channel
        });
        */
    });
    $(".cpanel_tool_add_line").live("click", function(){
        var line = $(this).attr("line");
        var channel = $(this).attr("channel");
		
        window.parent.createbox("addline", "插入新行", 200, "index.php?r=Layout.addline&rd="+Math.random(), {
            line_id: line,
            channel_id: channel
        });
    });
    
    $('.groupChildItem').live("dblclick",function(){
		
	});
	
    
    /*鼠标右键*/
	bindcontextMenu();
	
	$(".groupChildItem").live("dblclick",function(){
		var type = $(this).attr("type");
		var id = $(this).attr("channel");
		if(type == 1 && id !=""){
			window.location.href = "./index.php?r=Layout.cpanel&channel_id="+id+"&rd="+Math.random();
		}
	});
	
	$(".menuItem").live("mouseover",function(){
		$(this).addClass("m-hover");
	});
	$(".menuItem").live("mouseout",function(){
		$(this).removeClass("m-hover");
	});
	$(".menuItem").live("mousedown",function(){
		$(this).removeClass("m-hover");
	});
	
	disableHref();
	
	refreshHeight();
	
});

function disableHref()
{
	$(".menuItem").find("a").each(function(){
		$(this).click(function(){
			return false;
		});
	});
}

function bindcontextMenu(){
    /*鼠标右键*/
    $('.menuItem').contextMenu('myMenu', {
    	onShowMenu:function(e, menu){
			var show = $(e.target).attr("show");
			var type = $(e.target).attr("type");
			if(show == undefined){
				show = $(e.target).parent().attr("show");
				if (show == undefined) {
					show = $(e.target).parent().parent().attr("show");
					if(show == undefined){
						show = $(e.target).parent().parent().parent().attr("show");
					}
				}
			}
			if(type == undefined){
				type = $(e.target).parent().attr("type");
				if (type == undefined) {
					type = $(e.target).parent().parent().attr("type");
					if(type == undefined){
						type = $(e.target).parent().parent().parent().attr("type");
					}
				}
			}
			
			var typetext = '';
			if(type == 1)
			{
				typetext = '频道';
			}else{
				typetext = 'UBB';
			}
			
			$("#myMenu_delete",menu).text("删除"+typetext);
			
			if(show == "0"){
				$("#myMenu_hide",menu).text("显示"+typetext);
			}else{
				$("#myMenu_hide",menu).text("隐藏"+typetext);
			}
			return menu;
		},
        bindings: {
        
            'myMenu_attribute': function(t){
            	var id = $(t).attr("channel");
				var type = $(t).attr("type");
				if (type == 1 && id != "") {
					
					$.ajax({
						url:"index.php?r=Layout.edit_channel",
						type:"get",
						cache:false,
						data:{
							id: id
						},
						success:function(html){
							window.parent.$("#helperBox").empty().html(html);
						}
					});
					/*
					window.parent.createbox("editchannel", "修改频道", null, "./index.php?r=Layout.edit_channel&rd="+Math.random(), {
            			id: id
        			});
        			*/
				}else if(type == 2 && id != ""){
					
					
					$.ajax({
						url:"index.php?r=Layout.edit_ubb",
						type:"get",
						cache:false,
						data:{
							id: id
						},
						success:function(html){
							window.parent.$("#helperBox").empty().html(html);
						}
					});
					/*
					window.parent.createbox("editubb", "修改UBB", 760, "./index.php?r=Layout.edit_ubb&rd="+Math.random(), {
            			id: id
        			});
        			*/
				}
            },
            
            'myMenu_channel': function(t){
				var line = $(t).attr("line");
        		var channel = $(t).parent().parent().attr("channel");
				var module_id = $(t).attr("channel");
				
				$.ajax({
					url:"index.php?r=Layout.addchannel",
					type:"get",
					cache:false,
					data:{
						line_id: line,
						channel_id:channel,
						module_id:module_id
					},
					success:function(html){
						window.parent.$("#helperBox").empty().html(html);
					}
				});
				/*
        		window.parent.createbox("addchannel", "新增频道", null, "index.php?r=Layout.addchannel&rd="+Math.random(), {
            		line_id: line,
            		channel_id: channel,
					module_id:module_id
        		});
        		*/
            },
            
            'myMenu_ubb': function(t){
				var line = $(t).attr("line");
        		var channel = $(t).parent().parent().parent().attr("channel");
				var module_id = $(t).attr("channel");
				
				$.ajax({
					url:"index.php?r=Layout.addubb",
					type:"get",
					cache:false,
					data:{
						line_id: line,
            			channel_id: channel,
						module_id:module_id
					},
					success:function(html){
						window.parent.$("#helperBox").empty().html(html);
					}
				});
				/*
				window.parent.createbox("addubb", "新增UBB", 760, "index.php?r=Layout.addubb&rd="+Math.random(), {
            		line_id: line,
            		channel_id: channel,
					module_id:module_id
        		});
        		*/
            },
			
			'myMenu_up': function(t){
				var line_id = $(t).attr("line");
				var channel_id = $(t).attr("channel");
				if(line_id  != "" && channel_id != ""){
					$.get("index.php?r=layout.ChannelSortById&rd="+Math.random(),{line_id:line_id,channel_id:channel_id,sort:"up"},function(data){
						if(data.status == 1){
							window.location.reload();
						}
					},"json");
				}
            },
			
			'myMenu_down': function(t){
				var line_id = $(t).attr("line");
				var channel_id = $(t).attr("channel");
				if(line_id  != "" && channel_id != ""){
					$.get("index.php?r=layout.ChannelSortById&rd="+Math.random(),{line_id:line_id,channel_id:channel_id,sort:"down"},function(data){
						if(data.status == 1){
							window.location.reload();
						}
					},"json");
				}
            },
			
			'myMenu_hide': function(t){ 
				var line_id = $(t).attr("line");
				var channel_id = $(t).attr("channel");
				var show = $(t).attr("show");
				if(show != ""){
					$.get("index.php?r=layout.change_channel_status&rd="+Math.random(),{line_id:line_id,channel_id:channel_id,status:show},function(data){
						if(data.status == 1){
							if(show == "0"){
								$(t).removeClass("disable");
								$(t).attr("show","1");
							}else{
								$(t).addClass("disable");
								$(t).attr("show","0");
							}
						}else{
							alert("无法更改显示或隐藏！");
						}
					},"json");
				}
            },
            
            'myMenu_delete': function(t){
				var type = $(t).attr("type");
				var cfstr = "";
				if(type == 2){
					cfstr = "你确定要删除该UBB吗？";
				}else{
					cfstr = "你确定要删除该频道吗？这样将会删除该频道下的所有频道。";
				}
				if(confirm(cfstr)){
					var line_id = $(t).attr("line");
					var channel_id = $(t).attr("channel");
					
					if(channel_id !=""){
						$.get("index.php?r=layout.deleteChannel&rd="+Math.random(),{line_id:line_id,channel_id:channel_id},function(data){
							if(data.status == 1){
								$(t).remove();
								window.parent.frames['leftLayout'].location.reload();
							}else{
								if (type == 2) {
									alert("删除失败");
								}else{
									alert("不能删除,频道下还有其它内容");
								}
								
							}
						},"json");
					}
				}
            }
        }
    });
    $('.itemContent').contextMenu('myMenu', {
    	onShowMenu:function(e, menu){
			var show = $(e.target).parent().attr("show");
			if(show == "0"){
				$("#myMenu_hide",menu).text("显示行");
			}else{
				$("#myMenu_hide",menu).text("隐藏行");
			}
			
			$("#myMenu_delete",menu).text("删除行");
			
			$('#myMenu_attribute', menu).remove();
			
			return menu;
		},
        bindings: {
        
            'myMenu_attribute': function(t){
            },
            
            'myMenu_channel': function(t){ 
				var line = $(t).parent().attr("line");
        		var channel = $(t).parent().attr("channel");
				
				$.ajax({
					url:"index.php?r=Layout.addchannel",
					type:"get",
					cache:false,
					data:{
						line_id: line,
						channel_id:channel
					},
					success:function(html){
						window.parent.$("#helperBox").empty().html(html);
					}
				});
				/*
        		window.parent.createbox("addchannel", "新增频道", null, "index.php?r=Layout.addchannel&rd="+Math.random(), {
            		line_id: line,
            		channel_id: channel
        		});
        		*/
            },
            
            'myMenu_ubb': function(t){
				var line = $(t).parent().attr("line");
        		var channel = $(t).parent().attr("channel");
				
				$.ajax({
					url:"index.php?r=Layout.addubb",
					type:"get",
					cache:false,
					data:{
						line_id: line,
            			channel_id: channel
					},
					success:function(html){
						window.parent.$("#helperBox").empty().html(html);
					}
				});
				/*
				window.parent.createbox("addubb", "新增UBB", 760, "index.php?r=Layout.addubb&rd="+Math.random(), {
            		line_id: line,
            		channel_id: channel
        		});
        		*/
            },
			
			'myMenu_up': function(t){
				var line_id = $(t).parent().attr("line");
				var channel_id = $(t).parent().attr("channel");
				if(line_id  != "" && channel_id != ""){
					$.get("index.php?r=layout.LineSortById&rd="+Math.random(),{line_id:line_id,channel_id:channel_id,sort:"up"},function(data){
						if(data.status == 1){
							window.location.reload();
						}
					},'json');
				}
            },
			
			'myMenu_down': function(t){
				var line_id = $(t).parent().attr("line");
				var channel_id = $(t).parent().attr("channel");
				if(line_id  != "" && channel_id != ""){
					$.get("index.php?r=layout.LineSortById&rd="+Math.random(),{line_id:line_id,channel_id:channel_id,sort:"down"},function(data){
						if(data.status == 1){
							window.location.reload();
						}
					},'json');
				}
            },
			
			'myMenu_hide': function(t){ 
				var line_id = $(t).parent().attr("line");
				var channel_id = $(t).parent().attr("channel");
				var show = $(t).parent().attr("show");
				if(show != ""){
					$.get("index.php?r=layout.change_line_status&rd="+Math.random(),{line_id:line_id,channel_id:channel_id,status:show},function(data){
						if(data.status == 1){
							if(show == "0"){
								$(t).parent().removeClass("line_disable");
								$(t).parent().attr("show","1");
							}else{
								$(t).parent().addClass("line_disable");
								$(t).parent().attr("show","0");
							}
						}else{
							alert("无法更改显示或隐藏！");
						}
					},"json");
				}
            },
            
            'myMenu_delete': function(t){
				if(confirm("你确定要删除该行吗？这样将会删除该行下的所有频道。")){
					var line_id = $(t).parent().attr("line");
					var channel_id = $(t).parent().attr("channel");
					var i = 0;
					$(t).children(".groupChildItem").each(function(){
						if($(this).attr("id") != ""){
							i++;
						}
					});
					if(line_id !="" && i==0){
						$.get("index.php?r=layout.deleteLine&rd="+Math.random(),{line_id:line_id,channel_id:channel_id},function(data){
							if(data.status == 1){
								$(t).parent().remove();
							}else{
								alert("不能删除,该行下还有其它内容");
							}
							
						},"json");
					}else if(i>0){
						alert("该行下还有其它频道，请先删除频道。");
					}
				}
            }
        }
    });
    
}

var toggleContent = function(e){
    var targetContent = $('div.itemContent', this.parentNode.parentNode);
    if (targetContent.css('display') == 'none') {
        targetContent.slideDown(300);
        $(this).html('[-]');
    }
    else {
        targetContent.slideUp(300);
        $(this).html('[+]');
    }
    return false;
};

function refreshHeight()
{
	$(".itemHeader").each(function(){
		//alert($(this).parent().outerHeight());
		$(this).css("height",$(this).parent().outerHeight());
	});
}

