var $j = jQuery.noConflict();

function emptySearchString( element )	
{
	element.value = '';
}

document.observe( 'dom:loaded', function () {
	new Sky.Window.PopUp( 'skyPopUpImg', { skyWindowOpts: alpiqOptions, contentGetTag: 'rel', contentType: Sky.Window.PopUp.CONTENTTYPE_CODE } );
} );
	
replaceCufons();

var facebookLikeId = "facebookLike";
var facebookLikeBoxId = "facebookLikeBoxContainer";
var fBUtil = null;

if(isHome)
{
	document.observe( 'dom:loaded', function () {
		var facebookLikeExisting = ( Element.myIsExisting( facebookLikeId ) );
		var facebookLikeBoxContainerExisting = ( Element.myIsExisting( facebookLikeBoxId ) );
		if ( facebookLikeExisting || facebookLikeBoxContainerExisting )
		{
			fBUtil = new Sky.FacebookUtil( fbAppId, {
				likeButtonWidth: 110,
				likeButtonType: Sky.FacebookUtil.LIKE_BUTTON_STYLE_BUTTON_COUNT,
				likeBoxConnections: 8,
				likeBoxShowHeader: false,
				likeBoxShowStream: false,
				likeBoxWidth: 270,
				likeBoxHeight: 275,					
				callbackAfterInit: function() {
					if ( facebookLikeExisting )
					{
						fBUtil.insertLikeButton( $(facebookLikeId) );
					}
					if ( facebookLikeBoxContainerExisting )
					{
						fBUtil.insertLikeBox( $(facebookLikeBoxId), fbPageId );
					}
				}
			} );
		}
	} );
}
else
{
	document.observe( 'dom:loaded', function () {
		var facebookLikeExisting = ( Element.myIsExisting( facebookLikeId ) );
		var facebookLikeBoxContainerExisting = ( Element.myIsExisting( facebookLikeBoxId ) );
		if ( facebookLikeExisting || facebookLikeBoxContainerExisting )
		{
			fBUtil = new Sky.FacebookUtil( fbAppId, {
				likeButtonWidth: 110,
				likeButtonType: Sky.FacebookUtil.LIKE_BUTTON_STYLE_BUTTON_COUNT,
				likeBoxConnections: 6,
				likeBoxShowHeader: false,
				likeBoxShowStream: false,
				likeBoxWidth: 210,
				likeBoxHeight: 280,					
				callbackAfterInit: function() {
					if ( facebookLikeExisting )
					{
						fBUtil.insertLikeButton( $(facebookLikeId) );
					}
					if ( facebookLikeBoxContainerExisting )
					{
						fBUtil.insertLikeBox( $(facebookLikeBoxId), fbPageId );
					}
				}
			} );
		}
	} );
}

function replaceCufons()
{
	Cufon.replace('h1');
	Cufon.replace('span.h1');	
	Cufon.replace('h4');
	Cufon.replace('h6');
	Cufon.replace('.titleSpecial, .titleSpecialBlue');	
	Cufon.replace('#helmuploadTextBoxTitle');
	Cufon.replace('a.footerChannelTitle');
	Cufon.replace('#helmuploadEntryBoxButton');
	Cufon.replace('#helmFormOverlayText');
	Cufon.replace('button#helmuploadUploadFormSubmit');
	Cufon.replace('.helm2012galleryPreview a.message');
	Cufon.replace('#helmuploadTextBoxAddressButton a');
}


/* ###########################
   ####  SPECIAL WINDOWS  ####
   ########################### */

   // AGB
var agbWindow = null;
var agbWindowAdded = false;

var detailviewWindow = null;
var detailviewWindowAdded = false;
var detailviewWindow2 = null;
var detailviewWindow2Added = false;

function openAgbWindow ()
{
	agbWindow.show();
	return false;
}

function openAgbWindowOpened ()
{
	$('agbcontent').hide();
	
	if( !agbWindowAdded )
	{
		agbWindowAdded = true;
		$('agbcontent').setStyle( {
			zIndex: ( ( 10000 + ( agbWindow.instanceId * 10 ) ) + 10 )
		} );
	}
	specialAGBWindowSetPosition();
	$('agbcontent').setStyle( {
		top: '-600px'
	} );
	$('agbcontent').show();
	new Effect.Move( $('agbcontent'), {
		x: 0,
		y: 600,
		mode: 'relative',
		duration: .5
	} );
}

function closeAgbWindow ()
{
	new Effect.Move( $('agbcontent'), {
		x: 0,
		y: -600,
		mode: 'relative',
		duration: .5,
		afterFinish: function() {
			$('agbcontent').hide();
			agbWindow.hide();
		}
	} );

	return false;
}

// FAIRNESS
var fairnessWindow = null;
var fairnessWindowAdded = false;

function openFairnessWindow ()
{
	if ( Element.myIsExisting( 'detailviewGallery' ) )
	{
		$('detailviewGallery').setStyle( { zIndex: ( 10020 ) } );
		$('skyOverlay3').setStyle( { zIndex: ( 10025 ) } );
	}
	fairnessWindow.show();
	return false;
}

function openFairnessWindowOpened ()
{
	$('fairnesscontent').hide();
	
	if( !fairnessWindowAdded )
	{
		fairnessWindowAdded = true;
		$('fairnesscontent').setStyle( {
			zIndex: ( 10030 )
		} );
	}
	specialFairnessWindowSetPosition();
	$('fairnesscontent').setStyle( {
		top: '-600px'
	} );
	$('fairnesscontent').show();
	new Effect.Move( $('fairnesscontent'), {
		x: 0,
		y: 600,
		mode: 'relative',
		duration: .5
	} );
}

function closeFairnessWindow ()
{
	new Effect.Move( $('fairnesscontent'), {
		x: 0,
		y: -600,
		mode: 'relative',
		duration: .5,
		afterFinish: function() {
			$('fairnesscontent').hide();
			fairnessWindow.hide();
		}
	} );
	
	if ( Element.myIsExisting( 'detailviewGallery' ) )
	{
		$('detailviewGallery').setStyle( { zIndex: ( 10030 ) } );
		$('skyOverlay3').setStyle( { zIndex: ( 10020 ) } );
	}

	return false;
}


	// DETAIL VIEW
function openDetailviewWindow ()
{
	$('gallerychooser').style.visibility = "hidden";
	detailviewWindow.show();
	return false;
}

function openDetailviewWindow2 ()
{
	$('gallerychooser').style.visibility = "hidden";
	detailviewWindow2.show();
	return false;
}

function openDetailviewWindowOpened ()
{
	$('detailviewGallery').hide();
	
	if( !detailviewWindowAdded )
	{
		detailviewWindowAdded = true;
		$('detailviewGallery').setStyle( {
			zIndex: ( ( 10000 + ( detailviewWindow.instanceId * 10 ) ) + 10 )
		} );
	}
	specialDetailWindowSetPosition();
	$('detailviewGallery').setStyle( {
		top: '-600px'
	} );
	$('detailviewGallery').show();
	new Effect.Move( $('detailviewGallery'), {
		x: 0,
		y: 600,
		mode: 'relative',
		duration: .5
	} );
}

function openDetailviewWindow2Opened ()
{
	$('detailviewGallery').hide();
	
	if( !detailviewWindow2Added )
	{
		detailviewWindow2Added = true;
		$('detailviewGallery').setStyle( {
			zIndex: ( ( 10000 + ( detailviewWindow2.instanceId * 10 ) ) + 10 )
		} );
	}
	specialDetailWindowSetPosition();
	$('detailviewGallery').show();
	//detailviewWindow2.show();
	/*$('detailviewGallery').setStyle( {
		top: '-600px'
	} );
	$('detailviewGallery').show();
	new Effect.Move( $('detailviewGallery'), {
		x: 0,
		y: 600,
		mode: 'relative',
		duration: .5
	} );*/
}

function closeDetailviewWindow ()
{
	$('gallerychooser').style.visibility = "visible";
	new Effect.Move( $('detailviewGallery'), {
		x: 0,
		y: -600,
		mode: 'relative',
		duration: .5,
		afterFinish: function() {
			$('detailviewGallery').hide();
			detailviewWindow.hide();
		}
	} );

	return false;
}

function closeDetailviewWindow2 ()
{
	$('gallerychooser').style.visibility = "visible";
	$('detailviewGallery').hide();
	detailviewWindow2.hide();

	return false;
}
				
				
		
document.observe( 'dom:loaded', function() {

	if ( Element.myIsExisting( 'agbcontent' ) )
	{
		$('agbcontent').hide();

		
		agbWindow = new Sky.Overlay( {
			overlayCloseFunction: closeAgbWindow,
			overlayOpacity: .8,
			overlayClose: false
		} );
		agbWindow.getOverlayElement().observe( Sky.Overlay.EVENT_OVERLAY_OPENED, function() {
			openAgbWindowOpened();
		} );
	}
	
	if ( Element.myIsExisting( 'fairnesscontent' ) )
	{
		$('fairnesscontent').hide();

		
		fairnessWindow = new Sky.Overlay( {
			overlayCloseFunction: closeFairnessWindow,
			overlayOpacity: .8,
			overlayClose: false
		} );
		fairnessWindow.getOverlayElement().observe( Sky.Overlay.EVENT_OVERLAY_OPENED, function() {
			openFairnessWindowOpened();
		} );
	}
	
	if ( Element.myIsExisting( 'detailviewGallery' ) )
	{
		$('detailviewGallery').hide();

		detailviewWindow = new Sky.Overlay( {
			overlayCloseFunction: closeDetailviewWindow,
			overlayOpacity: .8,
			overlayClose: false
		} );
		detailviewWindow.getOverlayElement().observe( Sky.Overlay.EVENT_OVERLAY_OPENED, function() {
			openDetailviewWindowOpened();
		} );
		
		detailviewWindow2 = new Sky.Overlay( {
			overlayCloseFunction: closeDetailviewWindow2,
			overlayOpacity: .8,
			overlayClose: false
		} );
		detailviewWindow2.getOverlayElement().observe( Sky.Overlay.EVENT_OVERLAY_OPENED, function() {
			openDetailviewWindow2Opened();
		} );
	}
} );
		
function specialAGBWindowSetPosition ()
{
	_specialWindowSetPosition( 'agbcontent' );
}

function specialFairnessWindowSetPosition()
{
	_specialWindowSetPosition( 'fairnesscontent' );
}

function specialDetailWindowSetPosition ()
{
	_specialWindowSetPosition( 'detailviewGallery' );
}
				
function _specialWindowSetPosition ( id )
{
	var documentHeight = (window.innerHeight || document.documentElement.clientHeight || document.body.clientHeight || 0);
	var documentWidth = (window.innerWidth || document.documentElement.clientWidth || document.body.clientWidth || 0);

	var top = 0;
	var left = ( ( documentWidth / 2 ) - ( $(id).getWidth() / 2 ) );

	if( Prototype.Browser.IE && !Prototype.Browser.IE7up )
	{
		var dOffsets = document.viewport.getScrollOffsets();
		top += dOffsets.top;
		left += dOffsets.left;
	}

	if ( left < 0 )
	{
		left = 0;
	}

	$(id).setStyle( {
		top: top + 'px',
		left: left + 'px'
	} );

	if ( documentWidth >= $(id).getWidth() && documentHeight >= $(id).getHeight() )
	{
		$(id).addClassName( 'positionFixed' );
	}
	else
	{
		$(id).removeClassName( 'positionFixed' );
	}
}

function getNewCaptcha ()
{
	var now = new Date()
	$('captcha').src = webroot + "myinterfaces/cms/captcha/" + now.getTime() + ".png";
}

function activateTwitterInvite()
{
	//if ( $('twitter-custom-share-button') )
	//{
		var skyTwitter = new Sky.TwitterUtil( { locale: '$!{locale}', tweetText: '' } );
		$$('.twitter-custom-share-button').each(
		function(obj)
		{
				skyTwitter.insertCustomShareButton( obj );
		});
		
	//}
}


