﻿function isUndefined(variable) {
	return typeof variable == 'undefined' ? true : false;
}
var jsmenu = new Array();
var ctrlobjclassName;
jsmenu['active'] = new Array();
jsmenu['timer'] = new Array();
jsmenu['iframe'] = new Array();

function initCtrl(ctrlobj, click, duration, timeout, layer) {
	if(ctrlobj && !ctrlobj.initialized) {
		ctrlobj.initialized = true;
		ctrlobj.unselectable = true;

		ctrlobj.outfunc = typeof ctrlobj.onmouseout == 'function' ? ctrlobj.onmouseout : null;
		ctrlobj.onmouseout = function() {
			if(this.outfunc) this.outfunc();
			if(duration < 3) jsmenu['timer'][ctrlobj.id] = setTimeout('hideMenu(' + layer + ')', timeout);
		}

		if(click && duration) {
			ctrlobj.clickfunc = typeof ctrlobj.onclick == 'function' ? ctrlobj.onclick : null;
			ctrlobj.onclick = function (e) {
				doane(e);
				if(jsmenu['active'][layer] == null || jsmenu['active'][layer].ctrlkey != this.id) {
					if(this.clickfunc) this.clickfunc();
					else showMenu(this.id, true);
				} else {
					hideMenu(layer);
				}
			}
		}

		ctrlobj.overfunc = typeof ctrlobj.onmouseover == 'function' ? ctrlobj.onmouseover : null;
		ctrlobj.onmouseover = function(e) {
			doane(e);
			if(this.overfunc) this.overfunc();
			if(click) {
				clearTimeout(jsmenu['timer'][this.id]);
			} else {
				for(var id = 0; id < jsmenu['timer'].length; id++) {
					if(jsmenu['timer'][id]) clearTimeout(jsmenu['timer'][id]);
				}
			}
		}
	}
}

function initMenu(ctrlid, menuobj, duration, timeout, layer) {
	if(menuobj && !menuobj.initialized) {
		menuobj.initialized = true;
		menuobj.ctrlkey = ctrlid;
		menuobj.onclick = ebygum;
		menuobj.style.position = 'absolute';
		if(duration < 3) {
			if(duration > 1) {
				menuobj.onmouseover = function() {
					clearTimeout(jsmenu['timer'][ctrlid]);
				}
			}
			if(duration != 1) {
				menuobj.onmouseout = function() {
					jsmenu['timer'][ctrlid] = setTimeout('hideMenu(' + layer + ')', timeout);
				}
			}
		}
		menuobj.style.zIndex = 50;
		if(is_ie) {
			menuobj.style.filter += "progid:DXImageTransform.Microsoft.shadow(direction=135,color=#CCCCCC,strength=2)";
		}
	}
}

function ebygum(eventobj) {
	if(!eventobj || is_ie) {
		window.event.cancelBubble = true;
		return window.event;
	} else {
		if(eventobj.target.type == 'submit') {
			eventobj.target.form.submit();
		}
		eventobj.stopPropagation();
		return eventobj;
	}
}

function setMenuPosition(showid, offset, containerid) {
	var showobj = $(showid);
	var menuobj = $(showid + '_menu');
	var container = $(containerid);
	if(isUndefined(offset)) offset = 0;
	if(showobj) {
		showobj.pos = fetchOffset(showobj);
		if (container)
		{
			showobj.X = showobj.pos['left'] - container.offsetLeft;
			showobj.Y = 0;
		}
		else
		{
			showobj.X = showobj.pos['left'];
			showobj.Y = showobj.pos['top'];
		}		
		showobj.w = showobj.offsetWidth;
		showobj.h = showobj.offsetHeight;
		menuobj.w = menuobj.offsetWidth;
		menuobj.h = menuobj.offsetHeight;
		menuobj.style.left = (showobj.X + menuobj.w > document.body.clientWidth) && (showobj.X + showobj.w - menuobj.w >= 0) ? showobj.X + showobj.w - menuobj.w + 'px' : showobj.X + 'px';
		menuobj.style.top = offset == 1 ? showobj.Y + 'px' : (offset == 2 || ((showobj.Y + showobj.h + menuobj.h > document.documentElement.scrollTop + document.documentElement.clientHeight) && (showobj.Y - menuobj.h >= 0)) ? (showobj.Y - menuobj.h) + 'px' : showobj.Y + showobj.h + 'px');
		if(menuobj.style.clip && !is_opera) {
			menuobj.style.clip = 'rect(auto, auto, auto, auto)';
		}
	}
}

function fetchOffset(obj) {
	var left_offset = obj.offsetLeft;
	var top_offset = obj.offsetTop;
	while((obj = obj.offsetParent) != null) {
		left_offset += obj.offsetLeft;
		top_offset += obj.offsetTop;
	}
	return { 'left' : left_offset, 'top' : top_offset };
}

function doane(event) {
	e = event ? event : window.event;
	if(is_ie) {
		e.returnValue = false;
		e.cancelBubble = true;
	} else if(e) {
		e.stopPropagation();
		e.preventDefault();
	}
}

function showMenu(ctrlid, click, offset, duration, timeout, layer, showid, maxh, containerid) {
	e = window.event ? window.event : showMenu.caller.arguments[0];
	var ctrlobj = $(ctrlid);
	if(!ctrlobj) return;
	if(isUndefined(click)) click = false;
	if(isUndefined(offset)) offset = 0;
	if(isUndefined(duration)) duration = 2;
	if(isUndefined(timeout)) timeout = 500;
	if(isUndefined(layer)) layer = 0;
	if(isUndefined(showid)) showid = ctrlid;
	var showobj = $(showid);
	var menuobj = $(showid + '_menu');
	if(!showobj|| !menuobj) return;
	if(isUndefined(maxh)) maxh = 400;

	hideMenu(layer);

	for(var id = 0; id < jsmenu['timer'].length; id++) {
		if(jsmenu['timer'][id]) clearTimeout(jsmenu['timer'][id]);
	}

	initCtrl(ctrlobj, click, duration, timeout, layer);
	ctrlobjclassName = ctrlobj.className;
	ctrlobj.className += ' hover';
	initMenu(ctrlid, menuobj, duration, timeout, layer);

	menuobj.style.display = '';
	if(!is_opera) {
		menuobj.style.clip = 'rect(auto, auto, auto, auto)';
	}

	setMenuPosition(showid, offset, containerid);

	if(is_ie && is_ie < 7) {
		if(!jsmenu['iframe'][layer]) {
			var iframe = document.createElement('iframe');
			iframe.style.display = 'none';
			iframe.style.position = 'absolute';
			iframe.style.filter = 'progid:DXImageTransform.Microsoft.Alpha(style=0,opacity=0)';
			try {
			    $('append_parent').appendChild(iframe);
			}
			catch(e) {
			//for Discuz!NT
			}
			jsmenu['iframe'][layer] = iframe;
		}
		jsmenu['iframe'][layer].style.top = menuobj.style.top;
		jsmenu['iframe'][layer].style.left = menuobj.style.left;
		jsmenu['iframe'][layer].style.width = menuobj.w;
		jsmenu['iframe'][layer].style.height = menuobj.h;
		jsmenu['iframe'][layer].style.display = 'block';
	}

	if(maxh && menuobj.scrollHeight > maxh) {
		menuobj.style.height = maxh + 'px';
		if(is_opera) {
			menuobj.style.overflow = 'auto';
		} else {
			menuobj.style.overflowY = 'auto';
		}
	}

	if(!duration) {
		setTimeout('hideMenu(' + layer + ')', timeout);
	}

	jsmenu['active'][layer] = menuobj;
}

function hideMenu(layer) {
	if(isUndefined(layer)) layer = 0;
	if(jsmenu['active'][layer]) {
		try {
			$(jsmenu['active'][layer].ctrlkey).className = ctrlobjclassName;
		} catch(e) {}
		clearTimeout(jsmenu['timer'][jsmenu['active'][layer].ctrlkey]);
		jsmenu['active'][layer].style.display = 'none';
		if(is_ie && is_ie < 7 && jsmenu['iframe'][layer]) {
			jsmenu['iframe'][layer].style.display = 'none';
		}
		jsmenu['active'][layer] = null;
	}
}



function ChangeSortList(id,textvalue,salue)
{
    $("sorttop").style.display = "none";
    $("itemlist_"+id).style.display = "";
    $("checkid").value=id;
    $("sortidbox").value=textvalue;
    $("sortid").value=salue;
}
function reviewsort()
{
    $("sorttop").style.display = "";
    var cid = $("checkid").value;
    if($("itemlist_"+cid))
    { 
    $("itemlist_"+cid).style.display = "none";
    }
    $("checkid").value="";
    $("sortidbox").value="";
}

function ChangeBtypeInput(bt,layer,tvalue)
{
    $("btype").value = bt;
    $("btspan").innerHTML=tvalue;
    hideMenu(0) ;
}


//打开模式对话框（图片选择）
function OpenDialog(ObjID){
    if (navigator.appVersion.indexOf("MSIE") == -1){
        this.returnAction=function(strResult){
            if(strResult!=null && document.getElementById(ObjID)){
                document.getElementById(ObjID).value = strResult;
                if(document.getElementById(ObjID + "View")){
                    document.getElementById(ObjID + "View").src = "../" + strResult;
                    document.getElementById(ObjID + "View").style.display = "block";
                }
            }
        }
        window.open('/admin/Picture_Picture_Manage_Dialog.aspx?d=' + Date(),'newWin','modal=yes,width=620,height=620,top=200,left=300,resizable=no,scrollbars=yes');
        return;
    }else{
        var GetValue = showModalDialog('/admin/Picture_Picture_Manage_Dialog.aspx?d=' + Date(),'','dialogWidth:620px;dialogHeight:620px;')
        if (GetValue != null && document.getElementById(ObjID)) {
            document.getElementById(ObjID).value = GetValue;
            if(document.getElementById(ObjID + "View")){
                document.getElementById(ObjID + "View").src = "../" + GetValue;
                document.getElementById(ObjID + "View").style.display = "block";
            }
        } 
    }
}

//将选中的图片放入父窗体的控件中
function InitPicture(obj){
    if (navigator.appVersion.indexOf("MSIE") == -1){
        window.opener.returnAction(obj.title.replace("双击选择该图片，图片地址是",""));
        window.close();
    }else{
        window.returnValue = obj.title.replace("双击选择该图片，图片地址是","");
        window.close();
    }
}

function OpenDialogSort(ObjID)
{
var CheckStr = $(ObjID).value;


    if (navigator.appVersion.indexOf("MSIE") == -1){
        this.returnAction=function(strResult){
            if(strResult!=null && document.getElementById(ObjID)){
                document.getElementById(ObjID).value = strResult;
            }
        }
        window.open('/itemset_dialog/?d=' + Date()+'&CheckStr='+CheckStr+'','newWin','modal=yes,width=620,height=620,top=200,left=300,resizable=no,scrollbars=yes');
        return;
    }
    else{
        var GetValue = showModalDialog('/itemset_dialog/?d=' + Date()+'&CheckStr='+CheckStr+'','','dialogWidth:620px;dialogHeight:620px;')
        if (GetValue != null && document.getElementById(ObjID)) {
            document.getElementById(ObjID).value = GetValue;
        } 
    }
    //alert(CheckStr);
}
function InitSortStr(ObjID)
{
    var checkedstr=$("tcheck").value;

    if(checkedstr=="")
    {
        return false;
    }
    if (navigator.appVersion.indexOf("MSIE") == -1){
        window.opener.returnAction(checkedstr);
        window.close();
    }else{
        window.returnValue = checkedstr;
        window.close();
    }

}
function delcheck(id)
{
    $("li"+id).style.display="none";
    var svalue = $("tcheck").value;
    svalue=svalue.replace(","+id+",",",");
    $("tcheck").value = svalue;
    var allCheckbox=document.getElementsByName("item");
    for(var j = 0 ; j < allCheckbox.length ; j++){
        if(allCheckbox[j].type == "checkbox")
        {
            if(id==allCheckbox[j].value)
            {
                if(allCheckbox[j].checked)
                {
                    allCheckbox[j].checked=false;
                }
            }
        }
    }
}
function itemcheck(id)
{
    if(!checknum())return false;
    var svalue = $("tcheck").value;
    var allCheckbox=document.getElementsByName("item");
    for(var j = 0 ; j < allCheckbox.length ; j++){
        if(allCheckbox[j].type == "checkbox")
        {
            if(id==allCheckbox[j].value){
                if(allCheckbox[j].checked)
                {
                    svalue+=","+id+",";
                    $("tcheck").value = svalue;
                    $("li"+id).style.display="";
                }
                else
                {
                    delcheck(id);
                }
            }
            
        }
    }
}
function checknum()
{
    var svalue = $("tcheck").value;
    var arr = svalue.split(',');
    var acount=0;
    for(var t=0;t<arr.length;t++)
    {
        if(arr[t].length>0)
        {
            acount++;
        }
    }
    if(acount>4)
    {
    alert("最多只能选择5项");
    return false;
    }
    return true;
}
  function redirect(strURL){
   //logic
   
   var a=document.createElement("a");
   a.style.display = "none";
   document.body.appendChild(a);   
   a.href=strURL;
   a.click();
  }
//改变图片大小
function resizepic(thispic)
{
if(thispic.width>700) thispic.width=700;
}
//无级缩放图片大小
function bbimg(o)
{
  var zoom=parseInt(o.style.zoom, 10)||100;
  zoom+=event.wheelDelta/12;
  if (zoom>0) o.style.zoom=zoom+'%';
  return false;
}



