// Top Videos
var AOLVS = null;
var theHTML =null;
var query = null;
var tab =0;
var tabtopic = null;
var numbertab = null;
var currentSlide = 0;
var vidArray = new Array();
var maxChar = null;

function doFMClick(oLink,image,headline){return false}
function showPop(e,obj,str){return false}
function hidePop(){return false}

function wDate()
{
	var currentDay = new Date();
	var monthArray =  new Array('01', '02', '03', '04', '05', '06', '07', '08', '09', '10', '11', '12');
	var dayArray = new Array('日', '一', '二', '三', '四', '五', '六');
	var year = currentDay.getFullYear();
	var month = currentDay.getMonth();
	var day = currentDay.getDay();
	var date = currentDay.getDate();
	if (date < 10){date = '0' + date} document.write('今天'+year +'年' + monthArray[month] + '月' + date +  '日星期' + dayArray[day] )
}
function shareOperation(iOperation, strUrl, strTitle)
{
    var staf_obj = new staf_SE();
    staf_obj.setLang("zh-CN");
    if (strUrl != "")
    {
        staf_obj.setURL(strUrl);
    }
    if (strTitle != "")
    {
        staf_obj.setTitle(strTitle);        
    }
    if (iOperation == 1) // IM This
    {
        staf_obj.sendIM();
    }
    else
    {
        staf_obj.sendEmail();
    }
}

function runLightboxGallery(swfTag)
{
	var w = 821;
	var h = 494;
	var x = (window.screen.availWidth - w) / 2;
	var y = (window.screen.availHeight - h) / 3;
	if(window.galleryWindow == null || window.galleryWindow.closed) 
	{
		var attr = "location=no,menubar=no,resizable=no,scrollbars=no,status=no,";
		attr += "titlebar=no,toolbar=no,directories=no,location=no,top=" + y + ",left=" + x + ",width=" + w;
		attr += ",height=" + h;
		window.galleryWindow = window.open('http://cdn.channel.aol.com/_media/kx_newsmakers/newsmakers.html', "galleryWindow", attr);
		if(window.galleryWindow.opener == null) window.galleryWindow.opener = this;
	} 
	else 
	{
		window.galleryWindow.focus()
	}
}

function loadVideo()
{
    handleVSLoad(arguments[0],arguments[1]);
    initvideo = null;
    currentSlide=0;
    return initvideo;
}
 
function handleVSLoad() 
{
    displayMessage();
    tabtopic = arguments[1];
    param = arguments[0];	
    AOLVS.getVideos(arguments[0]);  
    activeTab(arguments[1]);
    return tabtopic;
}
 
function activeTab()
{
    for(var v=0; v<numbertab.length; v++){
        if (arguments[0] != v) {
            document.getElementById("tb."+v).className = '';
        }
    }
    document.getElementById("tb."+arguments[0]).className = 'selected';
}

function displayMessage()
{
    theHTML = '<div style="text-align:center;clear:both;height:85px;width:308px;background-color:#f3f3f3;border-top:45px solid #f3f3f3;"><img src="http://myfeeds.aolcdn.com/vis/progress_animation.gif"> Loading...</div>';
    document.getElementById('video_results').innerHTML = theHTML;
}

function handlePageLoad()
{
    numbertab = document.getElementById("videotab").getElementsByTagName('li');
    AOLVS = new AOLVideoSearch('sskxzi7esi3tjgqvl');

    AOLVS.results = arguments[1];
	query = arguments[0];
	maxChar = arguments[2];
    AOLVS.attachEvent('onload', 'handleVSLoad(query, 0);');
    AOLVS.attachEvent('onupdate', 'handleUpdate();');
    AOLVS.initialize();
}

function handleUpdate()
{
    theHTML = '';
    initHTML ='';
    viewAllHTML = '';

    switch(tabtopic){	
		case 0:
            viewAllHTML += '<a href="http://video.aol.com/video-search/query/category%3A%22News%22%20channel%3A%22AOL%20News%22%20sort%3AmostRecent%20distributor%3A%22AP%22/familyFilter/0" target="_blank">See all AP Videos</a>';
            break;
        case 1:
            viewAllHTML += '<a href="http://video.aol.com/video-search/query/category%3A%22News%22%20channel%3A%22AOL%20News%22%20sort%3AmostRecent%20distributor%3A%22CNN%22/familyFilter/0" target="_blank">See all CNN Videos</a>';
            break;
        case 2:
            viewAllHTML += '<a href="http://video.aol.com/video-search/query/category%3A%22News%22%20channel%3A%22AOL%20News%22%20sort%3AmostRecent%20distributor%3A%22CBS%22/familyFilter/0" target="_blank">See all CBS Videos</a>';
            break;
        case 3:
            viewAllHTML += '<a href="http://video.aol.com/video-search/query/category%3A%22News%22%20channel%3A%22AOL%20News%22%20sort%3AmostRecent%20distributor%3A%22Reuters%22/familyFilter/0" target="_blank">See all Reuters Videos</a>';
            break;
    }

    theHTML +='<div id="video">';

    if ( AOLVS.VideoSet.totalResultsReturned < AOLVS.results ) { 	
        AOLVS.results = AOLVS.VideoSet.totalResultsReturned;
	}

    if ( AOLVS.VideoSet.totalResultsReturned > 0 ) {
        for (var i=0; i < AOLVS.VideoSet.totalResultsReturned; i++) {   
            var caption = AOLVS.VideoSet.Video[i].description;
            var charCount = new description(caption, maxChar);
            vidArray[i] ='<div id="vid.'+i+'" class="tab"><div class="video_content"><p class="news_title">'+ AOLVS.VideoSet.Video[i].title+'</p><div class="news_image"><a href="'+AOLVS.VideoSet.Video[i].videoUrl +'" target="_blank" class="img"><img src="'+ AOLVS.VideoSet.Video[i].thumbnailUrl +'" width="116" height="82"></a></div><p class="news_content">'+ charCount.characherCount() +'<span class="details">'+ viewAllHTML+'</span></p><div class="clear"></div></div></div>';         
        }
        theHTML += '</div>';
        theHTML += '<div class="top_ftr">';
        theHTML += '<span class="prev"><a href="javascript:void(0);" onClick="vidSlide();return false;">< Previous</a></span>';
        theHTML += '<span id="tally" class="count">'+'      1 of '+ '     '+AOLVS.results+'   </span>'; 
        theHTML += '<span class="nxt"><a href="javascript:void(0);" onClick="vidSlide(1);return false;">Next ></a></span>';
        theHTML += '</div>';
    
        document.getElementById('video_results').innerHTML = theHTML;
        intializeSeq();
    }
}

function intializeSeq()
{
    initVidHTML = vidArray[0];  
    document.getElementById('video').innerHTML = initVidHTML;
}

function countChar()
{
    if (this.desclength > this.descmax){this.desc = this.desc.substring(0, this.descmax)+'...'}
	else{this.desc = this.desc}
    return this.desc;
}

function description(caption, descriptionmax)
{
    this.desc = caption;
    this.desclength = caption.length;
    this.descmax = descriptionmax;
    this.characherCount = countChar;
}

function searchTarget(url, newWindow, newTarget)
{
	// Set the target for the form
    document.bb_topform.action = url;
    if (newWindow) {document.bb_topform.target = '_blank'} else {document.bb_topform.target = ''}
    // Update the tabs classes to allow highlighting of the selected
    var arrElements = getElementsByClassName ('searchCatBg', 'li');
    for (var i=0; i<arrElements.length; i++) {arrElements[i].className = ''}
    document.getElementById(newTarget).className = 'searchCatBg';
    return false;
}

function getElementsByClassName(strClass, strTag, objContElm)
{
	strTag = strTag || "*";
	objContElm = objContElm || document;

	var objColl = objContElm.getElementsByTagName(strTag);
	if (!objColl.length &&  strTag == "*" &&  objContElm.all) objColl = objContElm.all;
	var arr = new Array();
	var delim = strClass.indexOf('|') != -1  ? '|' : ' ';
	var arrClass = strClass.split(delim);
	for (var i = 0, j = objColl.length; i < j; i++) 
	{
		var arrObjClass = objColl[i].className.split(' ');

		if (delim == ' ' && arrClass.length > arrObjClass.length) continue;
		var c = 0;
		comparisonLoop:
		for (var k = 0, l = arrObjClass.length; k < l; k++) 
		{
			for (var m = 0, n = arrClass.length; m < n; m++)
			{
				if (arrClass[m] == arrObjClass[k]) c++;
				if (( delim == '|' && c == 1) || (delim == ' ' && c == arrClass.length)) 
				{
					arr[arr.length] = objColl[i];
					//arr.push(objColl[i]);
					 break comparisonLoop;
				}
			}
		}
	}
	return arr;
}

function mouseoverup()
{
    document.getElementById("thumbsup").className="thumbsup_on";
}

function mouseoutup()
{
    document.getElementById("thumbsup").className="thumbsup";
} 

function mouseoverdown()
{
    document.getElementById("thumbsdown").className="thumbsdown_on";
}

function mouseoutdown()
{
    document.getElementById("thumbsdown").className="thumbsdown";
}

// End: SHARE THIS WIDGET JS

// Start: News Article - User Comments JS 
// rating
var xmlHttpRating;

function getRating(objectURI, category, type, score, appId)
{
    var procName = "dms.trri.addRatingAndGet"
    var parameters = objectURI + ',' + category + ',' + type  + ',' + score + ',' + appId;

    if (window.ActiveXObject) {xmlHttpRating = new ActiveXObject("Microsoft.XMLHTTP")}
    else if (window.XMLHttpRequest) {xmlHttpRating = new XMLHttpRequest()}
   
    xmlHttpRating.onreadystatechange = handleStateChange;
   
    //var ourl = document.URL;
	var url ="http://sports.channel.office.aol.com/httprequest.adp"
    //url="http://sports.channel.office.aol.com/httprequest.adp"
    //var ind = ourl.indexOf('aol.com/');
    //var url = ourl.substring(0, ind+8);
    //url += 'httprequest.adp';

    url = url + "?_pn=" + procName;
    if (parameters!=""){url = url + "&_pa=" + parameters}

    url += '&date=' + new Date().getTime();

    xmlHttpRating.open("GET", url, true);
    xmlHttpRating.setRequestHeader("Accept", "text/xml");
    xmlHttpRating.send(null);
}

function handleStateChange()
{
    if(xmlHttpRating.readyState == 4)
	{
        if(xmlHttpRating.status == 200)
		{
            if(xmlHttpRating.responseText.length)
			{
                var result = xmlHttpRating.responseText.substring(1, xmlHttpRating.responseText.length - 1);
                var results = result.split(" ");
                document.getElementById('label').innerHTML = "投票成功！";
                document.getElementById('thumbsup').innerHTML='<img src="http://www.aolcdn.com/ch_news/news-icon-thumbup" title="好" class="images" />'+results[0]+' %';
                document.getElementById('thumbsdown').innerHTML='<img src="http://www.aolcdn.com/ch_news/news-icon-thumbdown" title="差" class="images" />'+results[1]+' %';
                document.getElementById('total').innerHTML = '共'+results[2]+'人投票';
                mouseoutup();
                mouseoutdown();
            }
        }
    }
}
/***
function AsnsSignIn()
{
 	pSNS = getEl("snsMiniUI");
	pSNS.innerHTML = "";
	pSNS.innerHTML = _sns_var_;
	pSNS.style.display = 'block';
}
***/



// Login Functions
function AsnsSignIn(obj,leftOffSet,topOffSet) 
{
	
 	if(!leftOffSet) leftOffSet = 0;
 	if(!topOffSet) topOffSet = 0;
 	var pSNS = getEl("snsMiniUI");
 	pSNS.innerHTML = "";
 	pSNS.innerHTML += _sns_var_;
 	if(document.all)
	{
 		var pos=findPos(obj);
 		pSNS.style.position='absolute';
 		pSNS.style.left=(pos[0]+leftOffSet-138)+'px';
		pSNS.style.top=(pos[1]+topOffSet+12)+'px';
 	} 
	else 
	{
		obj.appendChild(pSNS);
		pSNS.style.left=leftOffSet+'px';
		pSNS.style.top=topOffSet+'px';
		pSNS.style.position='relative';
	}
	pSNS.onclick = closeSNS;
	var close = document.createElement("span");
	var a = document.createElement("a");
	a.href = "javascript:AsnsClose();";
	a.title = "close";
	var img = document.createElement("img")
	img.src = "http://my.screenname.aol.com/images/10x10_x.gif";
	img.style.border = "0px"
	a.appendChild(img);
	close.appendChild(a);
	spans = pSNS.getElementsByTagName( "span" );
	spans[1].style.cssFloat = "right";
	spans[1].style.styleFloat = "right";
	spans[1].style.margin = "1px 0px 0px 10px";
	close.style.cssFloat = "right";
	close.style.styleFloat = "right";
	close.style.margin = "2px 2px 0px 0px";
	spanPar = spans[1].parentNode;
	spanPar.insertBefore(close,spans[1]);
	pSNS.style.zIndex = 5000;
 	pSNS.style.display = "block";
}
function closeSNS(){getEl("snsMiniUI").innerHTML = ''}

function AsnsClose() 
{
 	pSNS = getEl("snsMiniUI");
 	pSNS.style.display = "none";
}

function getSelectedValue(selObj){return selObj.options[selObj.selectedIndex].value}

function getEl(id){return document.getElementById(id)}

var sharePopFlag = true;                             //variable for onclick popup
  
function showShareThisPop(topDivId, botDivId)        //function to call the popup onclick
{
    if (sharePopFlag)
    {
        document.getElementById('shareLeftIDBB').style.borderBottom = "1px solid #000";
        document.getElementById(botDivId).style.display = "block";
        document.getElementById(topDivId).className = "share_right_on";
    }
    else 
    {
        document.getElementById('shareLeftIDBB').style.borderBottom = "none";
        document.getElementById(botDivId).style.display = "none";
        document.getElementById(topDivId).className = "share_right";
    }
    sharePopFlag = !sharePopFlag;
}

function shareOperation(iOperation, strUrl, strTitle)
{
    var staf_obj = new staf_SE();
    staf_obj.setLang("en-US");
    if (strUrl != "")
    {
        staf_obj.setURL(strUrl);
    }
    if (strTitle != "")
    {
        staf_obj.setTitle(strTitle);        
    }
    if (iOperation == 1) // IM This
    {
        staf_obj.sendIM();
    }
    else
    {
        staf_obj.sendEmail();
    }
}