// JavaScript Document
var g_Channels = new Array("好笑","新奇","恐怖","驚險","甜美","性感","美味","時尚","感人","可愛", "其他", "八卦") ;

function wawabar_writewidget(vid, idreg, w, h_title, ch, play, extend, bgcolor, bdcolor, zoom, logo)
{
    var extend_src,extend_h;
    extend_src = '';
    extend_h = '';
    if( parseInt(w) < 160 )
        w = 160;
    frame_w = parseInt(w)+4;
    flash_height = parseInt(w) * 0.75;
    others_height = 10; //padding,margin
    if( extend == "")
        extend = 0;

    if( logo==0 )
        next_w = parseInt(w)-34;
    else
        next_w = parseInt(w)-34-45;  //34=next的圖寬, 45=logo的圖寬

    if( bdcolor == "")
    {
        if( bgcolor == "" )
            bdcolor = '#7cbf40';
        else
            bdcolor = bgcolor;
    }

    textColor = chTextColor(bgcolor);

    var hostName='www.wawaqu.com' ;
    var hostNamePrefix='http://'+hostName+'/widget/' ;
    if( extend == 0 )
    {
        extend_src = hostNamePrefix+'Rand_ad.php';
        extend_h = 130
    }
    else if( extend == 2 )
    {
        extend_src = hostNamePrefix+'Rand5.php?&w='+w+'&ch='+ch+'&bgcolor='+encodeURIComponent(bgcolor)+'&tcolor='+encodeURIComponent(textColor)+'&bdcolor='+encodeURIComponent(bdcolor);
        extend_h = 88;
    }
    rand_id = 0 + Math.floor(Math.random() * (9999 - 0 +1) );
    next_src = hostNamePrefix+'wwb_widget.php?vid='+vid+'&idreg='+idreg+'&w='+w+'&title='+h_title+'&ch='+ch+'&play=1&extend='+extend+'&bgcolor='+encodeURIComponent(bgcolor)+'&tcolor='+encodeURIComponent(textColor)+'&play_next=1' ;
    openZoom_src = hostNamePrefix+'wwb_widget.php?vid='+vid+'&idreg='+idreg+'&w='+w+'&title='+h_title+'&ch='+ch+'&play=0&extend='+extend+'&bgcolor='+encodeURIComponent(bgcolor)+'&tcolor='+encodeURIComponent(textColor)+'&play_next=0' ;

    document.write('<link href="'+hostNamePrefix+'css/widget.css" rel="stylesheet" type="text/css">');
    document.write('<div style="width:'+(parseInt(w)+4)+'px;border: 1px solid '+bdcolor+'">');

    var widget_div_next ='';
    widget_div_next+='<div class="easego" style="width:'+(parseInt(w)+2)+'px;background-color:'+bgcolor+'">';
    widget_div_next+='<div class="container" style="background-color:'+bgcolor+';border: 1px solid ';
    widget_div_next+= (bgcolor=='') ? '#bedfa0':bgcolor;
    widget_div_next+=';border-bottom:none;">';
    widget_div_next+='<div class="header">';
    widget_div_next+='<div align="center">';
    widget_div_next+='<table cellpadding="0" cellspacing="0" border="0" width="100%">';
    widget_div_next+='<tr>';
    if( logo==1 )
        widget_div_next+='<td height="27" align="left"><a href="http:\/\/wawaqu.pixnet.net\/blog" target="_blank" style="padding:0;border:0;background:transparent none repeat scroll 0% 0%;"><img src="'+hostNamePrefix+'images/easego_guy-trans.png" border="0" align="absmiddle" style="border: 0pt none ; vertical-align: middle;" \/><\/a><\/td>';

    widget_div_next+='<td height="27" nowrap="nowrap" align="center"><div id="easego_title" align="center" style="width:'+next_w+'px; overflow:hidden; margin-top: 2px;font-size: 9pt;color:'+textColor+'">';
    if( h_title == 1 )
        widget_div_next+= '趣挖寶'+g_Channels[ch]+'影片';
    widget_div_next+='<\/div><\/td>';
    widget_div_next+='<td valign="middle" align="right">';
    widget_div_next+='<div class="next"><a href="#" onclick="getNext(\''+next_src+'\',\''+extend_src+'\',\''+ rand_id +'\'); return false;" style="padding:0;border:0;background:transparent none repeat scroll 0% 0%;"><img src="'+hostNamePrefix+'images/next_btn.gif" border="0" align="absmiddle" style="border: 0pt none ; vertical-align: middle;" \/><\/a><\/div>';
    widget_div_next+='<\/td><\/tr><\/table><\/div><\/div><\/div><\/div>';
    document.write(widget_div_next);
    total_height = flash_height+others_height;
    var iframe_player;
    iframe_player = '<iframe id="easego_player_'+rand_id+'" frameborder="0" width="'+frame_w+'" height="'+total_height+
                    '" src="'+hostNamePrefix+'wwb_widget.php?vid='+vid+'&idreg='+idreg+'&w='+w+'&title='+h_title+
                    '&ch='+ch+'&play='+play+'&extend='+extend+'&bgcolor='+encodeURIComponent(bgcolor)+
                    '&tcolor='+encodeURIComponent(textColor)+'&play_next=0&zoom='+zoom+'"><\/iframe>';
    document.write(iframe_player);

	var quote='';
	quote+='<div class="easego" style="width:'+(parseInt(w)+2)+'px;background-color:'+bgcolor+'">';
	quote+='<div class="container" style="border:0px">';
	quote+='<div class="enlarge" style="background-color:'+bgcolor+'">';
	quote+='<table cellpadding="0" cellspacing="0" border="0" width="'+(parseInt(w)-5)+'">';
	quote+='<tr><td align="left">';
	quote+='<a id="easego_quoteLink" href="http://wawaqu.pixnet.net/blog/post/27068234" target="_blank" style="text-decoration:none;padding:0;border:0;background:transparent none repeat scroll 0% 0%;"><img src="'+hostNamePrefix+'images/widget_add.png" border="0" style="border: 0pt none;vertical-align: middle;" \/><span id="easego_quoteText" style="color:'+textColor+'">引用小工具<\/span><\/a>';
	quote+='<\/td>';
	if( zoom == 1 )
		quote+='<td align="right"><a id="easego_zoomLink" onClick="return openZoom(\''+openZoom_src+'\',\''+rand_id+'\')" href="'+
		       'http://'+hostName+'/?c='+ch+'&play_last=1" target="_blank" style="text-decoration:none;padding:0;border:0;background:transparent none repeat scroll 0% 0%;"><img src="'+
		       hostNamePrefix+'images/widget_enlarge.png" border="0" style="border: 0pt none;vertical-align:middle;" \/><span id="easego_zoomText" style="color:'+
		       textColor+'">放大欣賞<\/span><\/a><\/td>';

	quote+='<\/tr><\/table><\/div><\/div><\/div>';
	document.write(quote);

    if( extend !=1 )
    {
        var iframe_extend='';
        iframe_extend+='<iframe id="easego_extend_'+rand_id+'" name="easego_extend" scrolling="no" frameborder="0" width="'+frame_w+'" height="'+extend_h+'" src="'+extend_src+'"><\/iframe>';
        document.write(iframe_extend);
    }
    document.write('<\/div>');
    document.write('<div style="display:none"><form name="zoom_form" method="post" target="_blank" action="http://'+
                   hostName + '/index.php"><input name="c" type="hidden" value=0><input name="rid" type="hidden" />'+
                   '<input name="test" type="hidden" /></form></div>') ;
}

function getNext(next_src, extend_src, rand_id)
{
    document.getElementById('easego_player_'+rand_id).src = next_src ;
    try
    {
        document.getElementById('easego_extend_'+rand_id).src = extend_src ;
    }
    catch(err){};
}

function openZoom(openZoom_src, rand_id)
{
    var oFrame=document.getElementById('easego_player_'+rand_id) ;
    if( oFrame )
        oFrame.src = openZoom_src ;

    return true ;
}

var base_hexa = "0123456789ABCDEF";

function dec2Hexa(number)
{
    return base_hexa.charAt(Math.floor(number / 16)) + base_hexa.charAt(number % 16);
}

function RGB2Hexa(TR,TG,TB)
{
    return "#" + dec2Hexa(TR) + dec2Hexa(TG) + dec2Hexa(TB);
}

function chTextColor(strBGColor)
{
    var r,g,b;
    var nr,ng,nb;
    var re1 = /^#\w{6}/;
    var re2 = /[^A-Fa-f0-9#]/;

    if(strBGColor.length!=7 || !re1.test(strBGColor) || re2.test(strBGColor))
        return '#404040';

    r = 255-parseInt(strBGColor.substr(1,2),16); //取補數
    g = 255-parseInt(strBGColor.substr(3,2),16); //取補數
    b = 255-parseInt(strBGColor.substr(5,2),16); //取補數

    return RGB2Hexa(r,g,b); //顯示變更後的文字顏色
}
