var iCreateIFrame = typeof(iCreateIFrame)!='undefined' ? iCreateIFrame : 1;
var sCurrentURLFramed = '';
var iThumbsPerPage = 12;
var sBaseURL = '';

function isHTMLEmpty(sHTML)
{

	return sHTML.replace('<!--TYPO3SEARCH_begin-->', '').replace('<!--TYPO3SEARCH_end-->', '').replace(/ /gi, '').replace(/\n/g, '').replace(/\t/g, '').replace(/\r/g, '')=='' ? true : false;

}

function initGallery()
{
var iCount = 0;
var iThumbsBoxWidth = 0;
var iThumbsBoxHeight = 0;
var iIndex = 0;

	$j('.gallery_thumbs').each(function(i){
		//get box width & height
		iThumbsBoxWidth = $j(this).width();
		iThumbsBoxHeight = $j(this).height();
	
		if ( isNaN(parseInt(iThumbsBoxHeight)) ) return;
	
		//thumbnails wrapping
		$j(this).wrapInner('<div class="content" />');
		$j(this).find('.content').css('width', iThumbsBoxWidth+'px');
	
		// init thumbnails
		$j(this).find('a').each(function(i){
			$j(this).attr('id', 'glry_small_'+(i+iIndex).toString()).click(function(){
				iID = parseInt($j(this).attr('id').replace('glry_small_', ''));
				switchGalleryImage(iID, $j(this).parents('div.columns_holder'));
	
				return false;
			});
			iCount = i;
		});
		$j(this).find('a:eq(0)').addClass('selected');
	
		$oParent = $j(this).parents('div.columns_holder');
		var iIndexTmp = 0;
	
		// init medium images
		$oParent.find('.gallery_large_image img').each(function(i){
			$j(this).attr('id', 'glry_big_'+(i+iIndex).toString()).hide();
			iIndexTmp = i+1;
		});
		$oParent.find('.gallery_large_image img:eq(0)').show();
	
		// increment images index
		iIndex += iIndexTmp;
	
		$oParent.find('.left_column .gallery_buttons_holder > a').click(function(){
			iIDSelectedThumb = parseInt($j(this).parents('div.columns_holder').find('.gallery_thumbs a.selected').attr('id').replace('glry_small_', ''));
			$j(this).parents('div.columns_holder').find('.gallery_large_image img#glry_big_'+iIDSelectedThumb).parents('a:eq(0)').click();
	
			return false;
		});
		$oParent.find('.left_column .gallery_buttons_holder > a').attr('href', $oParent.find('.gallery_thumbs a:eq(0)').attr('href'));
	
		// init pages
		iCountPages = Math.ceil((iCount+1)/iThumbsPerPage);
	
		for (i=0;i<iCountPages;i++)
		{
			$oParent.find('.right_column .gallery_buttons_holder').prepend($j('<a href="#" id="glry_nav_'+i+'">'+(i+1)+'</a>'));
			$oParent.find('.right_column .gallery_buttons_holder a#glry_nav_'+i).click(function(){
				iID = parseInt($j(this).attr('id').replace('glry_nav_', ''));
				switchGalleryPage(iID, iThumbsBoxHeight, $j(this).parents('div.columns_holder'));
	
				return false;
			});
		}
	
//		$oParent.find('.right_column .gallery_buttons_holder a:eq(0)').addClass('selected');
		$oParent.find('.right_column .gallery_buttons_holder a:last').addClass('selected');

		// if there it is just 1 page of images, do not show the page number
		if ( $oParent.find('.right_column .gallery_buttons_holder > a').length<2 )
		{
			$oParent.find('.right_column .gallery_buttons_holder').hide();
		}
	
		// init navigation arrows
		$oParent.find('a.border_and_arrows_next_image').attr('href', '#').click(function(){
			navigateGallery(1, $j(this).parents('div.columns_holder'));
	
			return false;
		});
		$oParent.find('a.border_and_arrows_prev_image').attr('href', '#').click(function(){
			navigateGallery(-1, $j(this).parents('div.columns_holder'));
	
			return false;
		});
	
	});

	if ( typeof(iGalleryModePress)!='undefined' && iGalleryModePress==1 )
	{
		$j('.columns_holder .gallery_large_image > a:gt(1)').click(function(){
			window.open($j(this).attr('href'), '');

			return false;
		});
	}
	else
	{
		$j('.columns_holder .gallery_large_image').addClass('ceebox mixed').ceebox();
	}

}

function navigateGallery(iInc, $oParent)
{

	iMinThumbs = parseInt($oParent.find('.gallery_thumbs a:eq(0)').attr('id').replace('glry_small_', ''));
	iMaxThumbs = iMinThumbs+$oParent.find('.gallery_thumbs a').length;
	iIDSelectedPage = parseInt($oParent.find('.right_column .gallery_buttons_holder a.selected').attr('id').replace('glry_nav_', ''))+1;
	iIDSelectedThumb = parseInt($oParent.find('.gallery_thumbs a.selected').attr('id').replace('glry_small_', ''))+iInc;

	if ( iIDSelectedThumb>=iMaxThumbs )
	{
		iIDSelectedThumb = iMinThumbs;
	}
	else if ( iIDSelectedThumb<iMinThumbs )
	{
		iIDSelectedThumb = iMaxThumbs-1;
	}

	iIDSelectedPage = Math.ceil((iIDSelectedThumb-iMinThumbs+1)/iThumbsPerPage)-1;

	$oParent.find('.right_column .gallery_buttons_holder a#glry_nav_'+iIDSelectedPage).click();
	$oParent.find('.gallery_thumbs a#glry_small_'+iIDSelectedThumb).click();

}

function switchGalleryImage(iID, $oParent)
{

	if ( $oParent.find('.gallery_thumbs a#glry_small_'+iID).attr('class')=='selected' ) return;

	iIDSelected = parseInt($oParent.find('.gallery_thumbs a.selected').attr('id').replace('glry_small_', ''));
	//set thumbnail class
	$oParent.find('.gallery_thumbs a#glry_small_'+iIDSelected).removeClass('selected');
	$oParent.find('.gallery_thumbs a#glry_small_'+iID).addClass('selected');

	// display correct image
	$oParent.find('.gallery_large_image img#glry_big_'+iIDSelected).hide();
	$oParent.find('.gallery_large_image img#glry_big_'+iID).fadeIn();

	// set large href
	$oParent.find('.left_column .gallery_buttons_holder > a').attr('href', $oParent.find('.gallery_thumbs a#glry_small_'+iID).attr('href'));

}

function switchGalleryPage(iID, iThumbsBoxHeight, $oParent)
{

	if ( $oParent.find('.right_column .gallery_buttons_holder a#glry_nav_'+iID).attr('class')=='selected' ) return;

	iIDSelected = parseInt($oParent.find('.right_column .gallery_buttons_holder a.selected').attr('id').replace('glry_nav_', ''));
	//set navigation class
	$oParent.find('.right_column .gallery_buttons_holder a#glry_nav_'+iIDSelected).removeClass('selected');
	$oParent.find('.right_column .gallery_buttons_holder a#glry_nav_'+iID).addClass('selected');

	//show the correct thumbnails
	$oParent.find('.gallery_thumbs .content').css('margin-top', (-1*iID*iThumbsBoxHeight-iID*3)+'px').fadeIn();
/*
	if ( $j('.right_column .gallery_buttons_holder a#glry_nav_'+iID).attr('class')=='selected' ) return;

	iIDSelected = parseInt($j('.right_column .gallery_buttons_holder a.selected').attr('id').replace('glry_nav_', ''));
	//set navigation class
	$j('.right_column .gallery_buttons_holder a#glry_nav_'+iIDSelected).removeClass('selected');
	$j('.right_column .gallery_buttons_holder a#glry_nav_'+iID).addClass('selected');

	//show the correct thumbnails
	$j('.gallery_thumbs .content').css('margin-top', -1*iID*iThumbsBoxHeight+'px').fadeIn();
*/

}

function openURL(sURL)
{

	if ( typeof(aHomeURLs[sURL])!='undefined' && aHomeURLs[sURL]==1 )
	{
		$j("div#content_box").show();
		$j("div#top_margin").hide();
		$j("div#bottom_margin").hide();
		$j("div#content_area").addClass('special-home').html(aHomeImgContent).show();

		return;
	}
	else
	{
		$j("div#top_margin").show();
		$j("div#bottom_margin").show();
		$j("div#content_area").removeClass('special-home').html('');
	}

	var sURLSection = sURL.indexOf('#');
	sURLSection = sURLSection>0 ? sURL.substr(sURLSection+1) : '';

	$j.ajax({
		url: sURL,
		dataType: 'html',
		beforeSend: function(){
			$j("div#content_box").show();
			$j("div#content_area").html('<div class="loading"></div>');
//			$j("div#content_box").hide();
		},
		success: function(sHTML){
			$j("div#content_area").html(sHTML);

			if ( typeof(isHTMLEmpty)!='undefined' && !isHTMLEmpty(sHTML) )
			{
				$j("div#content_box").show().find('a[href*="'+sBaseURL+'"]').each(function(){					
					if ( $j(this).attr('href').indexOf('.html')>0 && $j(this).attr('href').indexOf('#')<0 )
					{
						$j(this).click(function(){
							SWFAddress.setValue($j(this).attr('href').replace(sBaseURL, '/'));
	
							return false;
						});
					}
				});
				initGallery();
				$j('a.special_link').ceebox();

				if ( sURLSection!='' && $j('#'+sURLSection).length==1 )
				{
					setTimeout(function(){ $j(document).scrollTop($j('#'+sURLSection).position().top+$j('#slider_box').height());}, 200);
				}
			}
			else
			{
				$j("div#content_box").hide();
			}
		}
	});

	sCurrentURLFramed = sURL;

}

function resize(param) {
	document.getElementById("flashcontent").style.height = param + "px";
	document.getElementById("slider_box").style.height = param + "px";
}

function doInit()
{

	// hide content area if empty
	if ( isHTMLEmpty($j("div#content_area").html()) || (typeof(iNotShowContent)!='undefined' && iNotShowContent==1) )
	{
		$j("div#content_box").hide();
	}

	rewriteURL();

	$j('a.special_link').ceebox();

}

function rewriteURL()
{
	sURL = window.location.toString();
/*
	sBaseURL = sURL.substr(0, sURL.indexOf('/', 7)+1);
	sBaseURL += 'dev/';
*/
	sBaseURL = $j('base').attr('href');

	if ( sURL.indexOf('#') == -1 )
	{
		sNewURL = sBaseURL+'#/'+sURL.substr(sBaseURL.length);
/*
		if ( ( iIndex = sNewURL.indexOf('.htm') ) > -1 )
		{
			sNewURL = sNewURL.substr(0, iIndex);
		}
*/
		window.location = sNewURL;
	}

}

$j = jQuery;
$j(document).ready(doInit);
