var thumbs=new Array();
var thumb=new Array();

//$(document).ready(function() {
//  $('.hSprite').bind('mouseover',hRotator.start).bind('mouseout',hRotator.stop);
//});

$(document).ready(function() {
  $('.hSprite').bind('mouseover',hRotator.start3);
});

var hRotator = {'timer':0,'cache':{},'id':0,'curr':false, 'stopped':true};

hRotator.start3 = function(el) {
    var t = $(this);
    t.bind('mouseout',hRotator.stop);
    hRotator.start.call(this);
}

hRotator.start2 = function(el) {
    var t = $(el);
    t.bind('mouseout',hRotator.stop);
    hRotator.start.call(el);
}
 
hRotator.start = function(e) {
    var t = $(this);
    hRotator.stop();
    hRotator.curr = t;
    hRotator.id = t.attr('id');
    hRotator.curr.css('background-position','0 0');
    //console.log('start: '+hRotator.id);
    if (!hRotator.cache[hRotator.id]) {
        img = new Image();
        img.loaded = false;
        hRotator.cache[hRotator.id] = img;
        img.vid = hRotator.id;
    } else img = hRotator.cache[hRotator.id];
    hRotator.stopped = false;
    if (!img.loaded) {
        t.parent().append('<span></span>');
        hRotator.loader = $('span',t.parent());
        $(img).bind('load',hRotator.onLoad);
        img.src = t.attr('sprite');
    } else {
        hRotator.begin();
    }
    //console.log('start: done');
}

hRotator.stop = function() {
    //console.log('stop: '+hRotator.id+' stopped('+hRotator.stopped+')');
    hRotator.stopped = true;
    clearTimeout(hRotator.timer);
    if (hRotator.curr) {
        hRotator.curr.css({'background-image':''});
        if (hRotator.loader) {
            hRotator.loader.remove();
            hRotator.loader = false;
        }
    }
    hRotator.id = false; hRotator.curr = false; hRotator.loader = false;
}

hRotator.onTime =  function(num) {
    clearTimeout(hRotator.timer);
    if (hRotator.stopped) {
        hRotator.stop();
        return true;
    }
    num++;
    if (num>9) num=0;
    //console.log('onTime: '+num);
    pos = (num*-160)+'px 0px';
    hRotator.curr.css({'background-position':pos});
    hRotator.timer = setTimeout('hRotator.onTime('+num+')',500);
}

hRotator.onLoad = function(e) {
    //console.log('onload: '+this.src);
    this.loaded = true;
    if (this.vid != hRotator.id) {
        //console.log('onload error: id='+hRotator.id+' img.id='+this.vid);
        return true;
    }
    hRotator.begin();
}

hRotator.begin = function() {
    if (hRotator.loader) {
        hRotator.loader.remove();
        hRotator.loader = false;
    }
    hRotator.curr.css({'background-position': '0 0'});
    hRotator.curr.css({'background-image':'url('+hRotator.curr.attr('sprite')+')'});
    hRotator.onTime(-1);
}






// xv

var OriPicNum = 0;
var CurDocument = 0;
var CurPicNum = 0;
var CurUrl = "";

function preLoadPicture(pic) {
    heavyImage = new Image();
    heavyImage.src = CurUrl + pic + ".jpg";

}

function getNextThumbNum(CurNum) {
    thumbs = new Array(2,5,8,11,14,17,20,23,26,29);
    for (i = 0; i < thumbs.length; i++) {
        if (thumbs[i] > CurNum) {
            return thumbs[i];
        }
    }
    return thumbs[0];
}

function nextThumbnail(idDoc) {
    if (CurDocument != 0 && idDoc == CurDocument) {
        // alert("CurPicNum : " + CurPicNum + " CurDocument : " + CurDocument);
        // alert(document.getElementById("pic_" + CurDocument));
        document.getElementById("pic_" + CurDocument).src = CurUrl + CurPicNum + ".jpg";
        nextthumb = getNextThumbNum(CurPicNum);
        preLoadPicture(nextthumb);
        CurPicNum = nextthumb;
        setTimeout("nextThumbnail("+ CurDocument +")", 1000);
    }
}

function stopThumbSlide() {
    TmpDoc = CurDocument;
    CurDocument = 0;
    document.getElementById("pic_" + TmpDoc).src = CurUrl + OriPicNum + ".jpg";
}

function startThumbSlide(IdDoc, CurNum, Url) {
    if (CurDocument == 0) {
        OriPicNum = CurNum;
        CurDocument = IdDoc;
        CurUrl = Url;
        nextthumb = getNextThumbNum(CurNum);
        preLoadPicture(nextthumb);
        CurPicNum = nextthumb;
        setTimeout("nextThumbnail("+ CurDocument +")", 20);
    }
}

function thumbcastDisplayRandomThumb(content) {
    var thumb = Math.floor((Math.random() * 600)) % 30 + 1;
    content = content.replace(/\.[0-9]+.jpg/, "." + thumb + ".jpg");
    // content = content.replace(/video[0-9]+\/0\//, "/" + thumb + "/");
    content = content.replace(/THUMBNUM/g, thumb);
    document.write(content);

}


//ph
var changing_thumbs = new Array();
function changeThumb(index, i, num_thumbs, path, premium_flag)
{
if (isNaN(premium_flag)) premium_flag = 0;
if (premium_flag == 0) {
if(path.indexOf('cdn1.image.keezmovies.phncdn.com')>-1 || path.indexOf('cdn1.videothumbs.xtube.com')>-1)
{
imgBase = '';
}
else
{
imgBase = 'small';
}
}
else
imgBase = '0';
if (changing_thumbs[index])
{
if( path.indexOf('{i}') > 0 )
{
$('#'+index).attr('src', path.replace('{i}',i) );
}
else if( path.indexOf('{index}') > 0 )
{
$('#'+index).attr('src', path.replace('{index}',i) );
}
else
{
if(endsWith(path, ".jpg")) {
//alert(path)
lastIndexOfDash = path.lastIndexOf("/");
path = path.substring(0,lastIndexOfDash+1);
$('#'+index).attr('src', path + i + ".jpg" );
} else {
$('#'+index).attr('src', path + imgBase + i + ".jpg" );
}
}
i = i % num_thumbs;
i++;
changing_thumbs[index] = setTimeout("changeThumb('" + index + "'," + i + ", " + num_thumbs + ", '" + path + "'," + premium_flag + ")", 600);
}
}
function endsWith(str, suffix) {
return str.indexOf(suffix, str.length - suffix.length) !== -1;
}
function startThumbChange(index, num_thumbs, path, premium_flag)
{
if (isNaN(premium_flag)) premium_flag = 0;
changing_thumbs[index] = true;
changeThumb(index, 1, num_thumbs, path, premium_flag);
}
function endThumbChange(index, path, premium_flag, xtube_flag)
{
clearTimeout(changing_thumbs[index]);
if (isNaN(premium_flag)) {
premium_flag = 0;
}
if (isNaN(xtube_flag)) {
xtube_flag = 0;
}
if (premium_flag == 0) {
if(path.indexOf('cdn1.image.keezmovies.phncdn.com')>-1 || path.indexOf('cdn1.videothumbs.xtube.com')>-1)
{
imgBase = '1';
}
else
{
imgBase = 'small';
}
}
else
imgBase = '01';
if(xtube_flag == 0) {
if(endWith(path,".jpg"))
{
document.getElementById(index).src = path;
}else{
document.getElementById(index).src = path + imgBase + ".jpg";
}
} else {
document.getElementById(index).src = path;
}
}





// t8
var cats = '';
		var changing_thumbs8 = new Object();
		var first_thumbs = new Object();

		function changeThumb8(index, i, path)
		{
			if (changing_thumbs8[index])
			{
				var src = path + i + '.jpg';

				if (path.indexOf("{index}") != -1)
				{
					src = path.replace("{index}", i);
				}

				$('#'+index).attr('src', src);

				var maxcnt = 16;

				if($('#' + index).attr('maxnum'))
				{
					maxcnt = parseInt($('#' + index).attr('maxnum'));
				}

				if (++i > maxcnt) i=1;
				changing_thumbs8[index] = setTimeout("changeThumb8('" + index + "'," + i + ",'" + path + "')", 600);
			}
		}

		function startThumbChange8(index, path)
		{
			changing_thumbs8[index] = true;
			changeThumb8(index, 2, path);
		}

		function endThumbChange8(index)
		{
			clearTimeout(changing_thumbs8[index]);
			$('#'+index).attr('src', first_thumbs[index]);
			changing_thumbs8[index] = false;
		}

		$(document).ready(function(){
			$('.t8Thumbs').live('mouseover', function() {
				var index = $(this).attr('id');

				first_thumbs[index] = $(this).attr('src');
				startThumbChange8(index, $(this).attr('thumb_src'));
			});
			$('.t8Thumbs').live('mouseout', function(){
				endThumbChange8($(this).attr('id'));
			});
		});


//rt
var pics=new Array();
var stat=new Array();
var pic=new Array();
function changepic(i,code, prefix)
{ var loop=0;
if (stat[code])
{
while ((pics[code][i]==0 || i>=16) && loop<100)
{ if (i>=16) { i=0; }
else
{ i++;
}
loop++;
}
if (pic[code][i].complete)
{
if(prefix != null){
document.getElementById(prefix).src=pic[code][i].src;
setTimeout("changepic("+(i+1)+",'"+code+"', '"+prefix+"')",500);
}else{
document.getElementById(code).src=pic[code][i].src;
setTimeout("changepic("+(i+1)+",'"+code+"')",500);
}
}
else
{
if(prefix != null) setTimeout("changepic("+i+",'"+code+"', '"+prefix+"')",20);
else setTimeout("changepic("+i+",'"+code+"')",20);
}
}
}
function loadpic(url,code,j)
{ if (stat[code]) { pic[code][j].src=url; }
}
function startm(code,ta,te,prefix)
{
stat[code]=1;
var jj, jjj;
var first=1;
for(var j=0;j<16;j++)
{ 
if ('undefined'==typeof(pics[code])) {
    pics[code]=new Array();
    pic[code]=new Array();
}
    if (typeof(pics[code][j])=='undefined')

{ pic[code][j]=new Image();
jj=j+1;
if (jj>=100) { jjj=""+jj; }
if (jj<100 && jj>=10) { jjj="0"+jj; }
if (jj<10) { jjj="00"+jj; }
if (first) {
first=0; loadpic(ta+jjj+te,code,j);
if(prefix != null){
document.getElementById(prefix).title="";
document.getElementById(prefix).parentNode.title="";
}else{
document.getElementById(code).title="";
document.getElementById(code).parentNode.title="";
}
}
else { setTimeout("loadpic('"+ta+jjj+te+"','"+code+"',"+j+")",j*50); }
}
}
if(prefix != null) changepic(0,code,prefix);
else changepic(0,code);
}
function endm(code)
{ stat[code]=0;
}
//
//console.log(typeof(pics['aa']));
//stat['0065162'] = 0;
//pic['0065162'] = new Array();
//pics['0065162'] = new Array(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
//
//
//		stat['0070035']	= 0;
//		pic['0070035']	= new Array();
//		pics['0070035']	= new Array(1, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1);
