var totalNum = 0;
var currentPage = 0;
var start = 0;

function submitLinkSearch(st, keyword, isOpen)
{
    if (isOpen) {
        window.open("search.php?st=" + st + "&ik=" + encodeURI(keyword));
    }
    else {
        window.location.href = "search.php?st=" + st + "&ik=" + encodeURI(keyword);
    }
}

var g_parent,g_child;
var g_hideTimer = null;
var g_locked = false;
var g_prepareTimer = null;
var g_actived = false;

function $id(id)
{
	return document.getElementById(id);
}

//Get the element's position
function getPos(parentObj)
{
	var parentPos = [];
	parentPos[0] = parentObj.offsetLeft;//x
	parentPos[1] = parentObj.offsetTop;//y
	while (parentObj = parentObj.offsetParent)
	{
		parentPos[0] += parentObj.offsetLeft;
		parentPos[1] += parentObj.offsetTop;
	}
	return parentPos;
}

function prepareMenu(parentObj,childName)
{
	if ( g_prepareTimer != null )
	{
		clearTimeout( g_prepareTimer );
		g_prepareTimer = null;
	}
	if (g_hideTimer != null)
	{
		clearTimeout(g_hideTimer);
		g_hideTimer = null;
		return false;
	}
	if ( g_actived === true )
	{
		return false;
	}

	g_prepareTimer = setTimeout( function(){  showMorecolor(parentObj,childName) }, 10 );


}


function showMorecolor(parentObj,childName)
{

	if (g_hideTimer != null)
	{
		clearTimeout(g_hideTimer);
		g_hideTimer = null;
		return false;
	}
	if ( g_actived === true )
	{
		return false;
	}
	var parentPos = getPos(parentObj);
	$id(childName).style.left = parentPos[0] + "px";
	$id(childName).style.top = parentPos[1] + 26 + "px";
	$id("lockBtn").style.left = "163px";
	parentObj.className = "active";
	$id(childName).style.display = "block";
	g_parent = parentObj;
	g_child = $id(childName);
	g_actived = true;
}

function hideMorecolor ()
{
	if ( g_prepareTimer != null )
	{
		clearTimeout( g_prepareTimer );
		g_prepareTimer = null;
	}
	if ( g_actived === true )
	{
		if (g_locked == true)
		{
			return;
		}
		else
		{
			g_hideTimer = setTimeout(function(){hidingMenu();},1);
		}
	}
}

function hidingMenu()
{
	g_parent.className = "normal";
	g_child.style.display = "none";
	clearTimeout(g_hideTimer);
	g_hideTimer = null;
	g_actived = false;

	if (g_locked == true)
	{
		$id("lockBtn").className = "drawingPin";
		g_locked = false;
	}
}

function lockMenu()
{
	if (g_locked == false)
	{
		$id("lockBtn").className = "drawingPin_2";
		$id("lockBtn").title="解除固定";
		g_locked = true;
	}
	else
	{
		$id("lockBtn").className = "drawingPin";
		$id("lockBtn").title="固定";
		g_locked = false;
	}
}
document.onmousedown = function (event)
{
     var e = event || window.event;
    var elem = e.srcElement||e.target;

    while(elem)
    {
        if(elem.id == "colorlist")
       {
               return;
         }
       elem = elem.parentNode;
   }
     hideMorecolor();
}

function changeLayout()
{
    var pageSize = getPageSize();
    var li = $(".picslist").children();
    var total = $("#total").val();
    var totalPage = Math.ceil(total/pageSize);
    if(window.currentPage == totalPage){
        pageSize = li.length;
    }

    var height = 230;
    if(window.totalNum > 0){
        var screenWidth = document.body.clientWidth;
        //$("#ik").val(screenWidth);
        var line = 0;
        //4*6
        if (screenWidth <= 1186) {
            line = Math.ceil(pageSize/4);
        }
        //5*5
        if (screenWidth > 1186 && screenWidth <= 1366) {
            line = Math.ceil(pageSize/5);
        }
        //4*6
        if (screenWidth >1366 && screenWidth <1680) {
            line = Math.ceil(pageSize/6);
        }
        //4*7
        if (screenWidth >= 1680) {

        }
        $('#pics').css({"height":line*height+"px","overflow":"hidden"});
    }else{
        $('#pics').css({"height":"250px"});
    }
}
function getHeight() {
    var pageSize = getPageSize();
    var lastNum = window.totalNum - (window.currentPage-1)*pageSize;
    var line = Math.ceil();
}
/**
 * 功能：分页
 * totalNum:总记录数
 * start:记录开始数
 * mpurl:链接
 */
function multi(totalNum,start) {
    window.totalNum = totalNum;
    window.start = start;
	mpurl = document.location.href;	//链接

//	if(/.*[\u4e00-\u9fa5]+.*$/.test(mpurl)){
//		mpurl = encodeURI( mpurl );
//	}

    //如果已存在start先去除
    if(mpurl.indexOf('start') != -1){
        mpurl = mpurl.substring(0,mpurl.indexOf('start')-1);
    }
	var pageSize = getPageSize();		//每页显示数量
    var maxpage = 0;					//最多显示页数,0为显示全部
    var page = 10;						//固定显示多少页
	window.currentPage = parseInt(start / pageSize)+1;	//由起始位置和pageSize计算得出
    var multipage = '';
    mpurl += mpurl.indexOf('?')!= -1 ? '&' : '?';
    var realpages = 1;
    if(totalNum > pageSize) {
        offset = 2;
        realpages = parseInt(totalNum / pageSize)+1;
        pages = maxpage && maxpage < realpages ? maxpage : realpages;
        if(page > pages) {
            from = 1;
            to = pages;
        } else {
            from = window.currentPage - offset;
            to = from + page - 1;
            if(from < 1) {
                to = window.currentPage + 1 - from;
                from = 1;
                if(to - from < page) {
                    to = page;
                }
            } else if(to > pages) {
                from = pages - page + 1;
                to = pages;
            }
        }

        multipage = window.currentPage > 1 ? '<a class="pg_prev" href="'+mpurl+'start='+(start-pageSize)+'">上一页</a> ' : '';

        for(var i = from; i <= to; i++) {
            multipage += i == window.currentPage ? '<span class="pg_curr">'+i+'</span>' :
                '<a href="'+mpurl+'start='+((i-1)*pageSize+1)+'">'+i+'</a>';
        }

        multipage += window.currentPage < pages ? '<a class="fy" href="'+mpurl+'start='+(start+pageSize)+'">下一页</a>' : '';
    }
    maxpage = realpages;
	$('#pg').html(multipage);

    //设置显示起始记录数
    var from = (window.currentPage-1)*pageSize+1;
    var to = (window.currentPage*pageSize>window.totalNum)? window.totalNum:window.currentPage*pageSize;
    var str = '<strong>'+from+'-'+to+'</strong>';
    $('#from_to').html(str);

    return multipage;
}
/**
 * 功能：根据屏幕宽度获得每页显示图片数量
 */
function getPageSize(){
	var currentWidth = document.body.clientWidth;
	var pageSize = 28;
	if(currentWidth <= 1186){
		pageSize = 20;
	}else if(currentWidth >=1187 && currentWidth <= 1366){
		pageSize = 25;
	}else if(currentWidth >1366 && currentWidth <= 1440){
		pageSize = 24;
	}else{
		pageSize = 28;
	}
	return pageSize;
}