var thisItem, thisItem1, thisItem2, thisItem3;

// cookies
function _create_cookie (name, value, days) {
	var expires;
	var date = new Date();
	date.setTime(date.getTime()+(days*24*60*60*1000));
	expires = "; expires="+date.toGMTString();

	document.cookie = name+"="+value+expires+"; path=/";
}

function _read_cookie (name) {
	var nameEQ = name + "=";
	var ca = document.cookie.split(';');
	for(var i=0;i < ca.length;i++) {
		var c = ca[i];
		while (c.charAt(0)==' ') c = c.substring(1,c.length);
		if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
	}
	return null;
}

function _erase_cookie (name) {
	_create_cookie(name,"",-1);
	
}

// preview video
function _preview_video (id, show, video, type, is_small, days) {
	var count = parseInt(_read_cookie('cb_video_preview'));
	var countMax = 7;
	var timer = null;
	days = 1;
	
	thisItem = myGet('preview_video_' + id);
	thisItem1 = myGet('preview_' + id);
	thisItem2 = myGet('play_' + id);
	thisItem3 = myGet('player_' + id);
	
	if (count >= countMax) {
		document.location = '/joinnow.phtml?lang=en';
	} else {
		_erase_cookie('cb_video_preview');
		_create_cookie('cb_video_preview', (count + 1), days);
	}

	if (thisItem && thisItem1 && thisItem2 && thisItem3) {
		thisItem.className = show ? 'preview_video hidden' : 'preview_video visible';
		thisItem1.className = show ? 'preview_box hidden' : 'preview_box visible';
		thisItem2.className = show ? 'play_box visible' : 'play_box hidden';
		thisItem3.innerHTML = show ? _preview_video_player(video, type, is_small) : '';
	}
	
	clearTimeout(timer);
	timer = setTimeout("_preview_video(" + id + ", false, '', '', false)", 45 * 1000);
}

function _preview_video_player (video, type, is_small) {
	var mov, swf;
	var width = 320;
	var height = 240;
	
	if (!is_small) {
		if (type == "mov") {
			width = 480;
			height = 320;
		} else {
			width = 533;
			height = 400;
		}
	}
	
	mov = '<div class="mov' + (is_small ? ' small' : '') + '">';
	mov += '	<object classid="clsid:02BF25D5-8C17-4B23-BC80-D3488ABDDC6B" width="' + width + '" height="' + height + '" codebase="http://www.apple.com/qtactivex/qtplugin.cab">';
	mov += '		<param name="SRC" value="/members/stream.php?file=previews/' + video + '&video=' + type + '&preview=1&nonmember=1">';
	mov += '		<param name="AUTOPLAY" value="true">';
	mov += '		<param name="CONTROLLER" value="true">';
	mov += '		<param name="KIOSKMODE" value="true">';
	mov += '		<param name="CACHE" value="true">';
	mov += '		<param name="SCALE" value="tofit">';
	mov += '		<embed src="/members/stream.php?file=previews/' + video + '&video=' + type + '&preview=1&nonmember=1" width="' + width + '" height="' + height + '" qtsrcdontusebrowser="true" pluginspage="http://www.apple.com/quicktime/download/" kioskmode="true" scale="tofit" cache="true"></embed>';
	mov += '	</object>';
	mov += '</div>';
	
	swf = '<div class="swf' + (is_small ? ' small' : '') + '">';
	swf += '	<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,0,0" width="' + width + '" height="' + height + '">';
	swf += '		<param name="src" value="/members/stream.php?file=previews/' + video + '&video=' + type + '&preview=1&nonmember=1">';
	swf += '		<param name="allowScriptAccess" value="sameDomain" />';
	swf += '		<param name="quality" value="high" />';
	swf += '		<param name="loop" value="false" />';
	swf += '		<param name="flashvars" value="loop=false" />';
	swf += '		<embed src="/members/stream.php?file=previews/' + video + '&video=' + type + '&preview=1&nonmember=1" width="' + width + '" height="' + height + '" loop="false" quality="high" name="flashvideo" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer"></embed>';
	swf += '	</object>';
	swf += '</div>';
	
	return type == "mov" ? mov : swf;
}

// new window
function _new_window (thisItem) {
	window.open(thisItem.href);

	return false;
}

// element
function myGet (name) {
	if (document.getElementById)
		return document.getElementById(name);
	else if (document.all)
		return document.all(name);
	else
		return null;
}
