/*
   COPYRIGHT TNAFLIX 2011
   ALL RIGHTS RESERVED
   www.tnaflix.com

   Tested in: Opera, Safari, IE6-8, Firefox, Chrome
*/
var isFF = (navigator.userAgent.indexOf('Firefox') > -1 ? true : false);
var isIE = (navigator.userAgent.indexOf('MSIE') > -1 ? true : false);
var curThumbURL = '';

//document.body.setAttribute('onbeforeunload', 'firefix = false;');

function goToThumb() {
	if (curThumbURL != '') document.location = curThumbURL;
}


function getFlashMovie(movieName) {
	var wind = window.document[movieName];
	var doc = document[movieName];
	var embeds = document.embeds[movieName];
	if (wind != false) return wind;
	if (doc != false) return doc;
	if (embeds != false) return embeds;
	return false;
}

function IsIE8Browser() {
	var rv = -1;
	var ua = navigator.userAgent;
	var re = new RegExp("Trident\/([0-9]{1,}[\.0-9]{0,})");
	if (re.exec(ua) != null) {
		rv = parseFloat(RegExp.$1);
	}
	return (rv == 4);
}

function findPos(obj) {
	var posX = obj.offsetLeft;
	var posY = obj.offsetTop;
	while (obj.offsetParent) {
		if (obj == document.getElementsByTagName('body')[0]) {
			break
		} else {
			posX = posX + obj.offsetParent.offsetLeft;
			posY = posY + obj.offsetParent.offsetTop;
			obj = obj.offsetParent;
		}
	}
	//get window size
	if (!window.innerWidth) {
		if (!(document.documentElement.clientWidth == 0)) {
			w = document.documentElement.clientWidth;
		} else {
			w = document.body.clientWidth;
		}
	} else {
		w = window.innerWidth;
	}
	if (IsIE8Browser() && (w % 2)) {
		posX = posX - 1;
	}
	var posArray = [posX, posY]
	return posArray;
}

function fireFoxFix() {
	if (isFF) {
		var flash = document.getElementById('thumb');
		var pos = findPos(flash);
		if (pos[0] > 0 && pos[1] > 0) {
			flash.style.left = '-900px';
			flash.style.top = '-900px';
		}
	}
}

/*
This function will be called when flash is loaded if in video thumb player has:
flashvars.startJS = "flashLodedHandler()";
*/
var flashThumbPlayerIsLoaded = false;
function flashLodedHandler() {
	flashThumbPlayerIsLoaded = true;
}


function movieStart(thumb, movie, file, target, vid, sceneid) {
	var flash = document.getElementById('thumb');
	var image = findPos(document.getElementById(thumb));
	/*
	var icon = document.getElementById('hdmovieRelIELink');
	if (sceneid) {
		var isHD = (document.getElementById('hdIcon' + vid + '_' + sceneid).className == 'hdIcon');

		if (isHD) {
			icon.className = "dvdIconThumb hdIcon";
		} else {
			icon.className = "dvdIconThumb dvdIcon";
		}

		icon.style.display = 'block';
	} else {
		icon.style.display = 'none';
	}
	*/
	//$(flash).show();
	var obj = swfobject.getObjectById("videoThumb");
	if (obj == null) {
		return;
	}
	if (isFF == true) {
		obj.style.position = 'relative';
		obj.style.position = '';
	}
	curThumbURL = target;
	if (typeof obj.startMovie == 'function') {
		obj.startMovie(thumb, movie, file, target);
	}
	
	//console.log("> "+flashThumbPlayerIsLoaded+ "> "+(typeof obj.startMovie));
	if (flashThumbPlayerIsLoaded) {
		if (image[0] > 0 && image[1] > 0) {
			flash.style.display = 'block';
			flash.style.left = (image[0] + 1) + 'px';
			flash.style.top = (image[1] + 1) + 'px';
		}
	}
}

function smovieStart(thumb, movie, file, target) {
	var flash = document.getElementById('thumb');
	var image = findPos(document.getElementById(thumb));

	if (image[0] > 0 && image[1] > 0) {
		flash.style.left = (image[0] + 1) + 'px';
		flash.style.top = (image[1] + 1) + 'px';
	}
	var obj = swfobject.getObjectById("videoThumb");
	if (obj == null) {
		return;
	}
	if (isFF == true) {
		obj.style.position = 'relative';
		obj.style.position = '';
	}
	curThumbURL = target;
	obj.startMovie(thumb, movie, file, target);
}

function hideJpeg(thumb, movie) {
	var movieItem = document.getElementById(movie);
	var image = document.getElementById(thumb);
	//image.className = 'hidden';
	movieItem.className = '';
}
var last_thumb = "";

function showJpeg(thumb) {
	if (thumb == "" && last_thumb != "") {
		thumb = last_thumb;
	} else {
		last_thumb = thumb;
	}
	var image = document.getElementById(thumb);
	if (image) {
		image.className = '';
	}
}

function killVideoThumbs() {
	var flash = document.getElementById('thumb');
	flash.style.left = -900;
	flash.style.top = -900;
}

// IE...
if (window.attachEvent) window.attachEvent("onresize", function () {
	killVideoThumbs();
});
// FF etc.
else window.addEventListener("resize", killVideoThumbs, true);
