$(function() {
	$(".hoverimg").hover(function() {
		$(this).css({'z-index' : '10'});
		$('#'+$(this).attr('id').replace('h','s')).css('visibility','hidden');
		//$(this).css({'position' : 'absolute'});
		
		//$(this).addClass("hover").stop() /* Add class of "hover", then stop animation queue buildup*/
		$(this).animate({
				marginTop: '-23px', /* The next 4 lines will vertically align this image */ 
				marginLeft: '-60px',
				top: '50%',
				left: '50%',
				width: '180px', /* Set new width */
				height: '135px' /* Set new height */
			}, 200); /* this value of "200" is the speed of how fast/slow this hover animates */
			
			switchThumb(this,true);

		} , function() {
		$(this).css({'z-index' : '0'}); /* Set z-index back to 0 */
		//$(this).css({'position' : 'relative'});
		$(this).animate({
				marginTop: '0', /* Set alignment back to default */
				marginLeft: '0',
				top: '0',
				left: '0',
				width: '120px', /* Set width back to default */
				height: '90px' /* Set height back to default */
			}, 400, '', function() {
					$('#'+$(this).attr('id').replace('h','s')).css('visibility','visible');
				}
		);
		resetThumb(this,false);
		
	});
});


var ads = [],ct = 0,cur = 0,t, src, id, dom = 'http://' + document.domain;
var big = false;
function switchThumb(o,biggy) {
	big = biggy;
    t = 0;
    if (o) {
        id = o.id;
        if (typeof(himg) != 'undefined') {
            id = id.replace('h', '');
            src = himg[id];
            t = 3
        }
        if (t != 0) {
            cur++;
            BNexecLoop(o, cur, 1)
        }
    }
}

function BNexecLoop(o, b, a) {
    var n;
    if (o && cur == b) {
        try {
			if(himg[id].length==1) return;
            n = (ct + 1) % src.length;
            if (ads[n] && (ads[n].complete || ads[n].complete == null)) {
                o.src = ads[ct = n].src
            }
            ads[n = (ct + 1) % src.length] = new Image;
            ads[n].onerror = function () {
                resetThumb(o)
            };
			
			if(!big){
				ads[n].src = dom + '/tube/images/' + id + '/' + src[n] + '.jpg';
			}else{
				ads[n].src = dom + '/tube/images/' + id + '/' + src[n] + '_big.jpg';
			}
            if (a == 1) {
                BNexecLoop(o, b);
            } else {
                setTimeout(function () { BNexecLoop(o, b) },500)
            }
        } catch(e) {}
    }
}

function resetThumb(o,biggy) {
	if(himg[id].length==1) return;
	big = biggy;
    try {
		if(!big){
			o.src = dom + '/tube/images/' + id + '/' + himg[id][0] + '.jpg'
		}else{
			o.src = dom + '/tube/images/' + id + '/' + himg[id][0] + '_big.jpg'
		}
    } catch(e) {}
    ct = 0;
    ads = [],prev = '',cur++
}
