﻿/*
*descrition:幻灯片图片方法：包括小图导航、大图切换效果等
*author:cici
*date：2009-09-17
*/
function imageonmousemove(evnt)
{
	var photopos = getpos($("bigImage"));

	if (evnt)
	{
		//nx = (parseInt(evnt.clientX)-photopos2.left) / $("bigImage").width;
		nx = (parseInt(evnt.clientX)-photopos.left) / $("bigImage").width;
		if((nx>0.5 && $("nextLink")) ||($("preLink")==null && $("nextLink")))
		{
			$("bigImage").style.cursor = "url("+"images/right.cur"+"),auto";
			$("bigImage").title = "点击浏览下一张>>";
		}
		
		if ( (nx<=0.5 && $("preLink")) || ($("nextLink")==null && $("preLink")))
		{
			$("bigImage").style.cursor = "url("+"images/left.cur"+"),auto";
			$("bigImage").title = "<<点击浏览上一张";
		}
	}
}
function imageonclick(evnt)
{
	var photopos = getpos($("bigImage"));

	if (evnt)
	{
		nx = (parseInt(evnt.clientX)-photopos.left) / $("bigImage").width;
	}

	if((nx>0.5 && $("nextLink")) ||($("preLink")==null && $("nextLink")))
	{
		var href= $("nextLink").href;
		window.location.href = href;
	}
	
	if ( (nx<=0.5 && $("preLink")) || ($("nextLink")==null && $("preLink")))
	{
		var href= $("preLink").href;
		window.location.href = href;
	}
}
/*
切换图片时图片幻灯展示的效果
*/
function ChangeImg(){
	var transition = 12;//图片切换时渐变方式 1－－23
	var tranSpeed = 0.5;//图片切换渐变速度。 值越小速度越快
	
	var oImg = $("bigImage");
	if(!oImg) return;
	if (document.all){ 
		oImg.filters.revealTrans.apply();
	}
	if (document.all)
	{
		if(!this.tranRandom) oImg.filters.revealTrans.transition = transition;
		oImg.style.visibility = "";
		oImg.style.display ="block";
		oImg.filters.revealTrans.play(tranSpeed);
	}
	else
	{
		oImg.style.visibility = "";
	}
}
function fixImage(i,w,h){
  var ow = i.width;
    var oh = i.height;
    var rw = w/ow;
    var rh = h/oh;
    var r = Math.min(rw,rh);
    if (w ==0 && h == 0){
        r = 1;
    }else if (w == 0){
        r = rh<1?rh:1;
    }else if (h == 0){
        r = rw<1?rw:1;
    }
    if (ow!=0 && oh!=0){
    i.width = ow * r;
      i.height = oh * r;
    }else{
      var __method = this, args = $A(arguments);
        window.setTimeout(function() {
          fixImage.apply(__method, args);
        }, 200);
    }
    i.onload = function(){}
}
//获取元素的位置
function getpos(element)
{
        if ( arguments.length != 1 || element == null )
        {
               return null;
        }
        var elmt = element;
        var offsetTop = elmt.offsetTop;
        var offsetLeft = elmt.offsetLeft;
        var offsetWidth = elmt.offsetWidth;
        var offsetHeight = elmt.offsetHeight;
        while( elmt = elmt.offsetParent )
        {
                // add this judge
                if ( elmt.style.position == 'absolute'
//              || elmt.style.position == 'relative'
                || ( elmt.style.overflow != 'visible' && elmt.style.overflow != '' ) )
                {
                        break;
                }
                offsetTop += elmt.offsetTop;
                offsetLeft += elmt.offsetLeft;
        }
        return {top:offsetTop, left:offsetLeft, right:offsetWidth+offsetLeft, bottom:offsetHeight+offsetTop };
}
//关于图片的方法 结束

//页面的方法 开始
var THUMBIMAGECOUNT = 3;//每屏显示的小图个数
var defaultSmallPic = "http://img.ifeng.com/tres/TemplateRes/pic70.jpg";
var curPage = Math.floor(currentSmallImgIndex/THUMBIMAGECOUNT);

	//////////////////////////图片方法////////////////////////////////////
function nextPic2()
{
	if(curPage * THUMBIMAGECOUNT >=imgs.picCount() || curPage<0)
	{
		return;
	}
	curPage++;
	ResetSmallImage();
}
function prePic2()
{
	if(curPage * THUMBIMAGECOUNT >=imgs.picCount() || curPage<0)
	{
		return;
	}
	curPage--;
	ResetSmallImage();
}
function ResetSmallImage()
{
	if(curPage * THUMBIMAGECOUNT >=imgs.picCount() || curPage<0)
	{
		return;
	}
	
	var fromIndex = curPage * THUMBIMAGECOUNT +1;
	var toIndex = (curPage+1) * THUMBIMAGECOUNT;
	toIndex=toIndex<imgs.picCount()?toIndex:imgs.picCount();
	
	$("picIndexFrom").innerHTML = fromIndex;
	$("picIndexTo").innerHTML =  toIndex;


	$("photoList1").src = imgs.get(fromIndex-1)==null?defaultSmallPic:imgs.get(fromIndex-1).smallpic;
	$("photoList2").src = imgs.get(fromIndex)==null?defaultSmallPic:imgs.get(fromIndex).smallpic;
	$("photoList3").src = imgs.get(fromIndex+1)==null?defaultSmallPic:imgs.get(fromIndex+1).smallpic;
	
	$("photoList1").parentNode.href =  imgs.get(fromIndex-1)==null?"javascript:void(0);": imgs.get(fromIndex-1).url;
	$("photoList2").parentNode.href =  imgs.get(fromIndex)==null?"javascript:void(0);": imgs.get(fromIndex).url;
	$("photoList3").parentNode.href =  imgs.get(fromIndex+1)==null?"javascript:void(0);": imgs.get(fromIndex+1).url;
	
	for(var i=1;i<=THUMBIMAGECOUNT;i++)
	{
		if(currentSmallImgIndex+1 == fromIndex + i-1)
			$("photoList" + i).parentNode.parentNode.className="current";
		else
			$("photoList" + i).parentNode.parentNode.className="";
	}
	
	if(fromIndex==1)
		$("preli2").style.visibility = "hidden";
	else
		$("preli2").style.visibility = "visible";
	
	if(toIndex>=imgs.picCount())
		$("nextli2").style.visibility = "hidden";
	else
		$("nextli2").style.visibility = "visible";
}


function ImgItem() {
	this.title="";
	this.url="";
	this.pic="";
	this.smallpic="";
	this.memo="";
	this.width=0;
	this.height=0;	
}
ImgItem.prototype = {           
	toImgHTML:function()
	{
		//todo...
	},
	toAHTML:function()
	{
		//todo...
	}
}
function ImgItems(){
	
	this.Imgs = new Array();
	
	this.add = addImg;
	this.get = getImg;
	this.picCount = picCount;
	this.toLiHtml = toLiHtml;
	this.doDivHtml = doDivHtml;
	
	function addImg(item){
		this.Imgs[this.Imgs.length] = item;
	}
	
	function getImg(i){
		if(i<this.Imgs.length) return this.Imgs[i];
		else return null;
	}
	function picCount()
	{
		return this.Imgs.length;
	}
		   
	function toLiHtml(){
		//todo...
	}
	
	function doDivHtml(){
		//todo...
	}
}
function $(id)
{
	return document.getElementById(id);
}

