/**
 * Picture player functions
 * 
 * Author: Ziming <ziming@staff.sina.com.cn,jorygong@gmail.com>
 * Last modified: 2007-9-26
 */
PicApp = function(page, book_id, chapter_id)
{
    PicApp.book_id = book_id;
	PicApp.chapter_id = chapter_id;
	PicApp.AjaxCall(page, book_id, chapter_id);
}

PicApp.AjaxCall = function(page, book_id, chapter_id)
{
	var Ajax = new class_ajax();
	Ajax.init();
	Ajax.set_method('GET');
	Ajax.set_event(callBack);
	Ajax.set_url("/iframe/picture_server.php");
	Ajax.add_param("action", "getpage");
	Ajax.add_param("page", page);
	Ajax.add_param("book_id", book_id);
	Ajax.add_param("chapter_id", chapter_id);
	Ajax.submit();
	
	function callBack()
	{
		if(Ajax.isSending())
		{
			PicApp.StartLoading();
		}
		if(Ajax.success())
		{
			 var response  = Ajax.get_content('text');
			 if (response==false)
			 {
				alert('图片数据获取错误'); 
			 }
			 else
			 {
			 	PicApp.SendParam(eval('('+response+')'));
			 }
		}
	}
}

PicApp.TurnPage = function(num)
{
	page = parseInt(page) + parseInt(num);
    if (page < 1)
    {
        page = 1;
        alert("已经到达第一张");
        return ;
    }
    if (page > totalpage)
    {
        page = 1;
        num = 1-totalpage
    }
	PicApp.AjaxCall(page, PicApp.book_id, PicApp.chapter_id);
	
}

PicApp.CountPV=function(wpid)
{
	//window.location.hash=pic_info[wpid].photoid;
	//SetPagetitle(pic_info[wpid].title);
	document.getElementById("cartoon_pv").src = "http://vip.book.sina.com.cn/cartoon_pv.html?guid="+ Math.round((Math.random())*100000);
}

PicApp.StartLoading = function(){}
PicApp.SendParam = function(ret)
{
    var ua = navigator.userAgent.toLowerCase();
    var os = new Object();
    os.isFirefox = ua.indexOf("gecko") !=  - 1;
    os.isOpera = ua.indexOf("opera") !=  - 1;
    os.isIE = !os.isOpera && ua.indexOf("msie") !=  - 1;
    var initid = 0;
    var ids = "", pics = "", links = "", titles = "", thumbs = "", intros = "", comms = "";
    chapter_id = ret.chapter_id;
    book_id = ret.book_id;
    page = ret.page;
    totalpage = ret.totalpage;
    pic_info = ret.photo;
    for (var i = 0; i < ret.photo.length; i++)
    {
        ids += "|" + ret.photo[i].chapter_id;
        pics += "|" + ret.photo[i].img;
        thumbs += "|" + ret.photo[i].img_small;
        links += "|" + ret.photo[i].comments_url;
        comms += "|" + ret.photo[i].comments_url;
        titles += "|" + ret.photo[i].title;
        intros += "|" + ret.photo[i].info;
        if (ret.photo[i].chapter_id == chapter_id)
            initid = i;
    }
    ids = ids.substring(1);
    pics = pics.substring(1);
    thumbs = thumbs.substring(1);
    links = links.substring(1);
    comms = comms.substring(1);
    titles = titles.substring(1);
    intros = intros.substring(1);
    if (os.isFirefox || os.isOpera)
    {
        var o = document.embeds['FlashPic'];
    }
    else
    {
        var o = document.getElementById('FlashPic');
    }
    o.SetVariable("ids", ids);
    o.SetVariable("pics", pics);
    o.SetVariable("thumbs", thumbs);
    o.SetVariable("links", links);
    o.SetVariable("comms", comms);
    o.SetVariable("titles", titles);
    o.SetVariable("intros", intros);
    o.SetVariable("initid", initid);
    o.SetVariable("page", ret.page);
    o.SetVariable("totalcount", ret.totalpage);
    o.SetVariable("_root.trigger.fArg", "trigger");
    document.getElementById("box_volume_title").innerHTML = ret.volume_title;
	document.getElementById("box_chapter_title").innerHTML = ret.chapter_title;
}

PicApp.ShowFlash = function()
{
    var bgcolor = "#FFFFFF";
    var FlashPic = new sinaFlash("images/pic.swf", "FlashPic", "100%", "100%", "7", bgcolor, false, "High");
    FlashPic.addParam("allowScriptAccess", "sameDomain");
    FlashPic.addParam("menu", "false");
    FlashPic.addParam("wmode", "transparent");
    FlashPic.addParam("scale", "noscale");
    FlashPic.write("picViewer");
}