flashinstalled = 2;
flashversion = 9;

var vcFlash = false;
var vcFlashPage = 0;
var vcContentShown = 1;
var vcPageDetail = 0;
var vcHTML = false;

// init config
document.title = vcTitle;

if ( vcEnviroment == 'web' )
{
	window.moveTo (0, 0);
//	window.resizeTo (vcWidth, vcHeight);
}

var vcBookmarks = new Array();
for ( var i = -1; i < (vcPages + 2); i += 1)
{
	vcBookmarks[i]	=	false;
}

$('content1').setStyle ({left: vcContentMargin + 'px', top: vcContentMargin + 'px', width: vcContentWidth + 'px', height: vcContentHeight + 'px'});
$('content2').setStyle ({display: 'none', left: vcContentMargin + 'px', top: vcContentMargin + 'px', width: vcContentWidth + 'px', height: vcContentHeight + 'px'});
$('menu1').setStyle ({left: '0px', top: vcContentHeight + vcContentMargin + vcContentMargin+ 'px', width: vcMenuWidth + vcContentMargin + vcContentMargin+ 'px', height: vcMenuHeight + 'px'});
$('menu2').setStyle ({display: 'none', left: '0px', top: vcContentHeight + vcContentMargin + vcContentMargin + 'px', width: vcMenuWidth + vcContentMargin + vcContentMargin + 'px', height: vcMenuHeight + 'px'});
$('listings').setStyle ({left: vcContentWidth + vcContentMargin + vcContentMargin + 'px', top: '0px', width: vcListingsWidth + 'px', height: vcListingsHeight + 'px'});

if ( BrowserDetect.browser != 'Explorer' || flashinstalled != 2 || flashversion < 8 )
{
	if ( BrowserDetect.browser == 'Explorer' && vcForceFlash )
	{
		$('content1').innerHTML = vcNoFlashInfo;
	}
	else
	{
		vcHTML = true;
		vcInitHTML();
	}
}
else
{
	vcHTML = false;
	$('content1').innerHTML = '<object OnLoad="initPageListener();" classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://fpdownload.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=7,0,0,0" id="flashobj" width="' + (vcContentWidth - vcContentMargin) + '" height="' + (vcContentHeight - vcContentMargin) + '" align="middle"><param name="allowScriptAccess" value="sameDomain" /><param name="movie" value="vc/vc.swf" /><param name="quality" value="high" /><param name="bgcolor" value="' + vcBgColor + '" /><embed  src="vc/vc.swf"  quality="high" bgcolor="' + vcBgColor + '"  width="' + (vcContentWidth - vcContentMargin) + '"  height="' + (vcContentHeight - vcContentMargin) + '"  swLiveConnect=true  id="flashemb" name="flashemb" align="middle" allowScriptAccess="sameDomain" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" /></object>';
}

// internal functions
function initPageListener ()
{
	if ( $('flashobj') )
	{
		vcGoPageAbsolute(vcPage);
	}
}

function vcInitHTML ()
{
	vcGoPageAbsolute(vcPage);
}

function swfUpdate (page)
{
	page = (typeof(page) == 'undefined') ? vcPages : page;
	page = vcPageLimits (page, false);
	vcFlashPage = page;
	vcGoPageAbsolute (vcFlashPage);
	vcDebug();
}

function vcGoPageRelative (direction)
{
	vcPage += direction;
	
	vcGoPageAbsolute (vcPage);
}

function vcGoPageAbsolute (page)
{
	page = vcPageLimits (page, false);
	vcPage = page;

	if ( vcHTML )
	{
		$('content1').innerHTML = '<div style="float: left; width: ' + (vcContentWidth / 2) + 'px; height: ' + vcContentHeight + 'px;"><a href="javascript:void(0);" OnClick="vcDetails($(\'BookmarkLeft\').value);"><img style="display: none;" OnLoad="this.style.display = \'\';" width="' + (vcContentWidth / 2) + '" src="' + vcPathData + '/details/' + (vcPage - 0) + '.jpg" border="0" /></a></div>';
		$('content1').innerHTML += '<div style="float: left; width: ' + (vcContentWidth / 2) + 'px; height: ' + vcContentHeight + 'px;"><a href="javascript:void(0);" OnClick="vcDetails($(\'BookmarkRight\').value);"><img style="display: none;" OnLoad="this.style.display = \'\';" width="' + (vcContentWidth / 2) + '" src="' + vcPathData + '/details/' + (vcPage + 1) + '.jpg" border="0" /></a></div>';
	}
	else
	{
		$('flashobj').SetVariable ('pageListener', vcPage);
	}

	if ( $('pagejump') )
	{
		$('pagejump').value = vcPage;
	}
	
	vcBookmarkPreselection();
	vcShowContent(1);
	vcDebug();
	vcTrack ('page', page);
}

function vcBookmarkPreselection ()
{
	var leftPage = vcPage - 1;
	var rightPage = vcPage;
	
	$('BookmarkLeft').checked = vcBookmarks[leftPage];
	$('BookmarkLeft').value = leftPage;
	
	$('BookmarkRight').checked = vcBookmarks[rightPage];
	$('BookmarkRight').value = rightPage;
}

function vcBookmarkSet (page)
{
	page = vcPageLimits (page, true);
	vcBookmarks[page] = (vcBookmarks[page]) ? false : true;
	vcBookmarkPreselection ();
	vcDebug();
	vcTrack ('bookmark', page);
}


function vcBookmarksShow ()
{
	var results = new Array ();
	for ( var i in vcBookmarks )
	{
		if ( typeof(vcBookmarks[i]) == 'function' || !vcBookmarks[i] ) continue;
		results.push ( Number(i) + 1 );
	}
	
	vcDisplaySearchResult (results);
}

function vcDetails (page)
{
	page = vcPageLimits (page, true);
	vcPageDetail = page + 1;
	if ( vcDetailsExtension == 'htm' )
	{
		$('content2').src = vcPathData + '/details/' + vcPageDetail + '.' + vcDetailsExtension;
	}
	else
	{
		$('content2').src = 'vc/details.htm?' + vcPathData + '/details/' + vcPageDetail + '.' + vcDetailsExtension;
	}
	vcShowContent(2);
	vcTrack ('detail', page);
}

function vcPrintDetails ()
{
	$('content2').src = 'vc/print.htm?' + vcPathData + '/details/' + vcPageDetail + '.' + vcDetailsExtension;
	vcTrack ('print', vcPageDetail);
}

function vcShowPDF ()
{
	vcTrack ('pdf', vcPageDetail);
	window.open (vcPathData + '/pdf/' + vcPageDetail + '.pdf');
}

function vcSearch (sstring)
{
	var results = new Array ();
	if ( sstring && typeof(vcPageContents) != 'undefined' )
	{
		sstring = sstring.toLowerCase();

		for ( var i = 0; i < vcPages; i += 1)
		{
			if ( typeof(vcPageContents[i]) != 'undefined' && vcPageContents[i].indexOf(sstring) >= 0 )
			{
				results.push ( i + 1 );
			}
		}		
	}
	else
	{
		for ( var i = 0; i < vcPages; i += 1)
		{
			results.push ( i + 1 );
		}
	}

	vcDisplaySearchResult (results);
	vcTrack ('search', sstring);
}

function vcDisplaySearchResult (results)
{
	var string_results = '';
	$('listings').innerHTML = '';

	for ( var i in results )
	{
		if ( typeof(results[i]) == 'function' || results[i] <= 0 ) continue;

		var listingItem = vcListingItem;
		while ( listingItem.indexOf('%page%') > 0 )
		{
			listingItem = listingItem.replace ('%page%', results[i]);
		}

		if ( listingItem.indexOf('%pageimage%') > 0 )
		{
			listingItem = listingItem.replace ('%pageimage%', vcNumber2Image( (results[i] + vcPageNumOffset) ));
		}
		
		$('listings').innerHTML += listingItem;

		string_results += results[i] + ',';
	}
	
	if ( $('listings').innerHTML && vcEnviroment == 'web' )
	{
		//$('listings').innerHTML += '<form method="get" action="download.php"><input type="hidden" name="pages" value="' + string_results + '" /><button class="buttonDownloadListing" type="submit">Download</button></form>';
	}
}

function vcNumber2Image (num)
{
	if ( num <= 0 )
	{
		return '';
	}
	
	num = String(num);
	var numImage = '';
	for ( var i = 0; i < num.length; i += 1 )
	{
		numImage += '<img src="vc/images/numbers/' + num.substr(i,1) + '.gif" border="0" />';
	}
	
	return numImage;
}

function vcShowContent (content)
{
	if ( vcContentShown == content )
	{
		return false;
	}
	
	switch (content)
	{
		default:
		case 1:
			$('content2').setStyle ({display: 'none'});
			$('menu2').setStyle ({display: 'none'});

			$('content1').setStyle ({display: ''});
			$('menu1').setStyle ({display: ''});
			vcContentShown = 1;
			break;

		case 2:
			$('content1').setStyle ({display: 'none'});
			$('menu1').setStyle ({display: 'none'});

			$('content2').setStyle ({display: ''});
			$('menu2').setStyle ({display: ''});
			vcContentShown = 2;
			break;
	}
}

// if uneven isset, allow uneven numbers
function vcPageLimits (page, uneven)
{
	page = Number(page);
	
	// limits
	page = (page < 0) ? 0 : page;
	page = (page > vcPages) ? vcPages : page;
	
	// even numbers
	if ( !uneven )
	{
		page -= (page % 2) ? 1 : 0;
	}
	
	return page;
}

function vcExitCD ()
{
	document.title = 'exit';
}

function vcDebug ()
{
	if ( !vcDebugging )
	{
		return false;
	}
	
	$('debug').innerHTML = 'vcPage: ' + vcPage + ' - vcFlashPage: ' + vcFlashPage;
	$('debug').innerHTML += ' - Bookmark Values: ' + $('BookmarkLeft').value + '/' + $('BookmarkRight').value;
	
	$('debug').innerHTML += ' - Bookmarks: ';
	for ( var i = -1; i < (vcPages + 2); i += 1)
	{
		if ( vcBookmarks[i]	)
		{
			$('debug').innerHTML += i + ',';
		}
	}
}

function vcTrack (type, data) {
	if ( vcStatsId ) {
		if ( !$('tracking') ) {
			var trackingDiv = document.createElement('DIV');
			trackingDiv.style.width = '1px';
			trackingDiv.style.height = '1px';
			trackingDiv.style.overflow = 'hidden';
			trackingDiv.border = 0;
			trackingDiv.id = 'tracking';
			$('body').appendChild (trackingDiv);
		}

		var trackingImage = document.createElement('IMG');
		trackingImage.style.width = '1px';
		trackingImage.style.height = '1px';
		trackingImage.style.overflow = 'hidden';
		trackingImage.border = 0;
		trackingImage.id = 'tracking';
		trackingImage.src = 'http://www.virtual-publisher.de/stats.php?id=' + vcStatsId + '&type=' + type + '&data=' + data;
		$('tracking').appendChild (trackingImage);
	}
}

