var bgImages = new Array();
var bgIndex = 0;
var currentBG = '';
var BG_FADE_SPEED = 'slow';
var BG_SWITCH_TIME = 8000;
var imageSlides = new Array();
var pageIndex = new Array();
var gallery = null;

$(document).ready(function(){
	
	initBackground();

	setPageImageIndexArray();
	
	initAgenda();
	
	initLoungeClubMenu();
	
	applyPlaceholders();
	
	updateHeights();
	update();
	
	$(window).resize(function(){
		updateHeights();
		update();

		api.options.min_width = $(window).width();
		api.options.min_height = $(window).height();
	});
		
	initGallery('loungeclub-gallery', 'loungeclub-slideshow', 'loungeclub-controls');
	initGallery('hopkamer-gallery', 'hopkamer-slideshow', 'hopkamer-controls');
	// Initially set opacity on thumbs and add
	// additional styling for hover effect on thumbs
	/*var onMouseOutOpacity = 0.67;
	$('#thumbs ul.thumbs li').opacityrollover({
		mouseOutOpacity:   onMouseOutOpacity,
		mouseOverOpacity:  1.0,
		fadeSpeed:         'fast',
		exemptionSelector: '.selected'
	});*/
	
	checkFacebookLikeFrame();

});

function initBackground()
{		
	var r = '';
	
	imageSlides = [			// Slideshow Images
					   	 		/* home */
							{image : r+'images/bg_home.jpg', title : ''},
							/* lounge club */
							{image : r+'images/bg_loungeclub.jpg', title : ''},
							{image : r+'images/bg_loungeclub.jpg', title : ''},
							{image : r+'images/bg_menukaart.jpg', title : ''},
							{image : r+'images/bg_fotogalerij_loungeclub.jpg', title : ''},
							{image : r+'images/bg_livebeelden.jpg', title : ''},
							{image : r+'images/bg_reserveren_loungeclub.jpg', title : ''},
							{image : r+'images/bg_contact_loungeclub.jpg', title : ''},
							/* hopkamer */
							{image : r+'images/bg_hopkamer.jpg', title : ''},
							{image : r+'images/bg_hopkamer.jpg', title : ''},
							{image : r+'images/bg_fotogalerij_hopkamer.jpg', title : ''},
							{image : r+'images/bg_reserveren_hopkamer.jpg', title : ''},
							{image : r+'images/bg_contact_hopkamer.jpg', title : ''},
							/* watch */
							{image : r+'images/bg_watch.jpg', title : ''},
							{image : r+'images/bg_watch.jpg', title : ''},
							{image : r+'images/bg_contact_watch.jpg', title : ''},
							/* party */
							{image : r+'images/bg_party.jpg', title : ''},
							{image : r+'images/bg_party.jpg', title : ''},
							{image : r+'images/bg_contact_party.jpg', title : ''},
							/* overig */
							{image : r+'images/bg_clubcard.jpg', title : ''},
							{image : r+'images/bg_home.jpg', title : ''},
							{image : r+'images/bg_in_de_media.jpg', title : ''},
							{image : r+'images/bg_vacatures.jpg', title : ''},
							{image : r+'images/bg_contact.jpg', title : ''}
					];
	
	$.supersized({
		
		vertical_center:false,
		
		// Functionality
		autoplay:false,
		/*slide_interval          :   3000,		// Length between transitions
		transition              :   1, 			// 0-None, 1-Fade, 2-Slide Top, 3-Slide Right, 4-Slide Bottom, 5-Slide Left, 6-Carousel Right, 7-Carousel Left
		transition_speed		:	700,		// Speed of transition
		*/
												   
		// Components							
		slide_links				:	'blank',	// Individual links for each slide (Options: false, 'number', 'name', 'blank')
		slides 					:  	imageSlides
		
	});
	
	$('#supersized').css({position:'absolute',left:'0px',top:'0px'});
	if ($.browser.msie) $('#website').scroll(function(){updateIEBackground()});
}

function updateIEBackground()
{
	$('#supersized').css({top:$(window).scrollTop()+'px'});
}

function checkFacebookLikeFrame()
{
	if ($('#home.current').length>0)
	{
		if ($('#fb-like-main .fb_edge_comment_widget').length > 0)
			updateHeights();
	
		setTimeout('checkFacebookLikeFrame()', 500);
	}
}

function setPageImageIndexArray()
{
	pageIndex = new Array();
	var i=1;
	//alert($('#nav li').length);
	$('#nav>li').each(function(){
		if (typeof $(this).attr('page') != 'undefined') pageIndex[$(this).attr('page')] = i;
		else if (typeof $(this).attr('color') != 'undefined') pageIndex[$(this).attr('color')] = i;
		
		i++;
		
		$(this).find('ul li').each(function(){
			if (typeof $(this).attr('page') != 'undefined') pageIndex[$(this).attr('page')] = i;
			else if (typeof $(this).attr('color') != 'undefined') pageIndex[$(this).attr('color')] = i;
			
			i++;
		});
	});
}

function getPageImageIndex(page)
{
	if (typeof pageIndex[page] != 'undefined')
		return pageIndex[page];
	return 0;
}

function rotateBackground()
{
	var activePage = $('#nav>li>a.active').parent().attr('color');
	var sub = $('#nav li ul li.active');
	if (sub.length > 0) {
		activePage = sub.attr('page');
	}
	if ((activePage != 'over_ons') && stringStartsWith(activePage,'over_'))
		activePage = activePage.substr(5);
	
	if (true)
	{
		var index = getPageImageIndex(activePage);
		bgIndex = index;
		if (vars.is_paused==false) api.playToggle();
		
		var image = imageSlides[index-1];
		if (image && image.image)
		{
			$.preload([image.image],
				{loaded_all:function(loaded,total){
					if (bgIndex == index)
						api.goTo(index);
				}});
		}
	}
}

function goToPage(pageName)
{
	// if submenu item -> click on main menu item fist
	$('#nav li li[page='+pageName+']').parent().parent().find('a').first().click();
	// click on actual menu item
	$('#nav li[page='+pageName+'] a').click();
}

function goToSubPage(pageName,subPageName)
{
	menuSubSubName = subPageName;
	goToPage(pageName);
}

function updateHeights()
{
	var menuHeight = $('#menu-content').height();
	
	var currentHeight = 0;
	if ($('.current').length>0) {
		currentHeight = $('.current').height();
		// enlarge if content is made larger
		var contentHeight = $('.current .content-content').height();
		if (!isNaN(contentHeight))
		{
			currentHeight = contentHeight;
			
			var marginTop = $('.current .content-content').css('margin-top').replace('px','');
			var marginBottom = $('.current .content-content').css('margin-bottom').replace('px','');

			currentHeight += parseInt(marginTop);
			currentHeight += parseInt(marginBottom);
		}
	}
	
	var maxHeight = Math.max(menuHeight,currentHeight);
	
	var fbIframe = $('#fb-like-main .fb_edge_comment_widget');
	if (fbIframe.length > 0)
	{
		var offset = fbIframe.offset();
		if (offset)
			maxHeight = Math.max(maxHeight, offset.top + fbIframe.height());
	}
	
	// set to total min-height of 100%
	maxHeight = Math.max($(window).height()-$('#footer').height(),maxHeight);
	
	$('#footer-push').height(maxHeight);
	$('.current').height(maxHeight);
	//maxHeight += $('#footer').height();
	$('#menu').height(maxHeight);
	
	$('#footer').show();
}

function initAgenda()
{	
	$('.agenda-item').click(function(){
		$(this).find('p').slideToggle(function(){
			updateHeights();
			update();
		});
	});
}

function submitNewsletter()
{
	var data = {
			naam:valueWithPlaceholder('newsletter-naam'),
			email:valueWithPlaceholder('newsletter-email'),
			geboortedatum:valueWithPlaceholder('newsletter-geboortedatum')
	};
	var contactName = 'newsletter';
	
	$.post('newsletter.php', data, function(data){

		var ok = false;
		var split = data.split(':');
		if (split.length >= 2)
		{
			var type = split[0];
			if ((type=='error') && (split.length >= 3))
			{
				var msg = $('<div></div>').addClass('contact-message');
				var elemId = split[1];
				var message = split[2];
				for(var i=3; i<split.length; i++)
				{ message += ':' + split[i]; }
				
				ok = true;
				$('#'+elemId).nextMsg({msg:message, CSSClass:'nextMsg-DarkTheme'});
			}
			else if (type=='message')
			{
				var message = split[1];
				for(var i=2; i<split.length; i++)
				{ message += ':' + split[i]; }
				ok = true;
				$('#'+contactName+'-result').hide();
				$('#'+contactName+'-text').fadeOut(function(){
					clearForm('#'+contactName+'-form');
					$('#'+contactName+'-result').html(message).fadeIn();
				});
			}
		}
		
		if (ok==false)
		{
			var message = '<p><b>Fout bij versturen gegevens</b></p><p>Er is een fout opgetreden bij het versturen van de gegevens. Probeer het later nog eens.</p>';
			//alert('MESSAGE: ' + message);
			$('#'+contactName+'-result').hide();
			$('#'+contactName+'-text').fadeOut(function(){
				$('#'+contactName+'-result').html(message).fadeIn();
			});
		}
	});
}

function backToContact(contactName)
{
	$('#'+contactName+'-result').fadeOut(function(){
		if (contactName == 'clubcard')
			$('#clubcard-top').fadeIn();
		$('#'+contactName+'-text').fadeIn();
	});
}

function submitContact(contactName)
{
	$('.contact-message').remove();
	
	var backToContact = '<p>Klik <a href="#" onclick="backToContact(\''+contactName+'\');return false">hier</a> om terug te gaan.</p>';
	
	var url = 'contact.php';
	var data = {contact:contactName};
	
	if (contactName == 'contact')
	{
		data.naam = valueWithPlaceholder('contact-naam');
		data.email = valueWithPlaceholder('contact-email');
		data.extra = valueWithPlaceholder('contact-extra');
		data.bericht = valueWithPlaceholder('contact-bericht');
		//data = $.extend(data,{naam:naam, email:email, extra:extra, bericht:bericht});
	}
	if (contactName == 'contact_watch')
	{
		data.voornaam = valueWithPlaceholder('contact_watch-voornaam');
		data.achternaam = valueWithPlaceholder('contact_watch-achternaam');
		data.bedrijf = valueWithPlaceholder('contact_watch-bedrijf');
		data.woonplaats = valueWithPlaceholder('contact_watch-woonplaats');
		data.telefoonnummer = valueWithPlaceholder('contact_watch-telefoonnummer');
		data.email = valueWithPlaceholder('contact_watch-email');
		data.extra = valueWithPlaceholder('contact_watch-extra');
		data.opmerkingen = valueWithPlaceholder('contact_watch-opmerkingen');
	}
	if (contactName == 'contact_party')
	{
		data.voornaam = valueWithPlaceholder('contact_party-voornaam');
		data.achternaam = valueWithPlaceholder('contact_party-achternaam');
		data.bedrijf = valueWithPlaceholder('contact_party-bedrijf');
		data.woonplaats = valueWithPlaceholder('contact_party-woonplaats');
		data.telefoonnummer = valueWithPlaceholder('contact_party-telefoonnummer');
		data.email = valueWithPlaceholder('contact_party-email');
		data.extra = valueWithPlaceholder('contact_party-extra');
		data.opmerkingen = valueWithPlaceholder('contact_party-opmerkingen');
	}
	if (contactName == 'reserveren_loungeclub')
	{
		data.voornaam = valueWithPlaceholder('reserveren_loungeclub-voornaam');
		data.achternaam = valueWithPlaceholder('reserveren_loungeclub-achternaam');
		data.bedrijf = valueWithPlaceholder('reserveren_loungeclub-bedrijf');
		data.aantal_gasten = valueWithPlaceholder('reserveren_loungeclub-aantal_gasten');
		data.datum = valueWithPlaceholder('reserveren_loungeclub-datum');
		data.tijd = valueWithPlaceholder('reserveren_loungeclub-tijd');
		data.woonplaats = valueWithPlaceholder('reserveren_loungeclub-woonplaats');
		data.telefoonnummer = valueWithPlaceholder('reserveren_loungeclub-telefoonnummer');
		data.email = valueWithPlaceholder('reserveren_loungeclub-email');
		data.extra = valueWithPlaceholder('reserveren_loungeclub-extra');
		data.opmerkingen = valueWithPlaceholder('reserveren_loungeclub-opmerkingen');

	}
	if (contactName == 'reserveren_hopkamer')
	{
		data.voornaam = valueWithPlaceholder('reserveren_hopkamer-voornaam');
		data.achternaam = valueWithPlaceholder('reserveren_hopkamer-achternaam');
		data.bedrijf = valueWithPlaceholder('reserveren_hopkamer-bedrijf');
		data.woonplaats = valueWithPlaceholder('reserveren_hopkamer-woonplaats');
		data.email = valueWithPlaceholder('reserveren_hopkamer-email');
		data.extra = valueWithPlaceholder('reserveren_hopkamer-extra');
		data.opmerkingen = valueWithPlaceholder('reserveren_hopkamer-opmerkingen');

	}
	if (contactName == 'clubcard')
	{
		data.voornaam = valueWithPlaceholder('clubcard-voornaam');
		data.achternaam = valueWithPlaceholder('clubcard-achternaam');
		data.bedrijf = valueWithPlaceholder('clubcard-bedrijf');
		data.woonplaats = valueWithPlaceholder('clubcard-woonplaats');
		data.email = valueWithPlaceholder('clubcard-email');
		data.telefoonnummer = valueWithPlaceholder('clubcard-telefoonnummer');
		data.extra = valueWithPlaceholder('clubcard-extra');
		data.opmerkingen = valueWithPlaceholder('clubcard-opmerkingen');
	}
	
	$.post(url, data, function(data){

		var ok = false;
		var split = data.split(':');
		if (split.length >= 2)
		{
			var type = split[0];
			if ((type=='error') && (split.length >= 3))
			{
				var msg = $('<div></div>').addClass('contact-message');
				var elemId = split[1];
				var message = split[2];
				for(var i=3; i<split.length; i++)
				{ message += ':' + split[i]; }
				
				ok = true;
				$('#'+elemId).nextMsg({msg:message, CSSClass:'nextMsg-DarkTheme'});
			}
			else if (type=='message')
			{
				var message = split[1];
				for(var i=2; i<split.length; i++)
				{ message += ':' + split[i]; }
				message += backToContact;
				ok = true;
				$('#'+contactName+'-result').hide();
				$('#'+contactName+'-text').fadeOut(function(){
					clearForm('#'+contactName+'-form');
					if (contactName == 'clubcard')
						$('#clubcard-top').hide();
					$('#'+contactName+'-result').html(message).fadeIn();
				});
			}
		}
		
		if (ok==false)
		{
			var message = '<p><b>Fout bij versturen gegevens</b></p><p>Er is een fout opgetreden bij het versturen van de gegevens. Probeer het later nog eens.</p>' +
							backToContact;

			$('#'+contactName+'-result').hide();
			$('#'+contactName+'-text').fadeOut(function(){
				$('#'+contactName+'-result').html(message).fadeIn();
			});
		}
	});
}

function clearForm(formId) {

    $(formId).find(':input').each(function() {
        switch(this.type) {
            case 'password':
            case 'select-multiple':
            case 'select-one':
            case 'text':
            case 'textarea':
                $(this).val('');
                break;
            case 'checkbox':
            case 'radio':
                this.checked = false;
        }
    });
    
    applyPlaceholders();

}

function applyPlaceholders()
{
	$("* [placeholder]").focus(function(){
		var a=$(this);
		if(a.val()==a.attr("placeholder")){
			a.val("");
			a.removeClass("placeholder");
		}
	}).blur(function(){
		var a=$(this);
		if(a.val()==""){
			a.addClass("placeholder");
			a.val(a.attr("placeholder"));
		}
	}).blur().parents("form").submit(function(){
		$(this).find("[placeholder]").each(function(){
			var a=$(this);
			if(a.val()==a.attr("placeholder")){
				a.val("");
			}
		})
	});
}

function valueWithPlaceholder(elemId)
{
	var elem = $('#'+elemId);
	if (elem.length>0)
	{
		if (elem.val()==elem.attr("placeholder")){
			return '';
		}
		return elem.val();
	}
	return '';
}

function selectMenu(link,name)
{
	$('.top-menu a').removeClass('selected');
	$(link).addClass('selected');
	
	$('.menu-item').removeClass('selected');
	$('#menu-' + name.toLowerCase()).addClass('selected');
	
	updateHeights();
}

function selectAlbum(link, albumId)
{
	// stop all gallery slideshows
	$('.play-stop[status=play]').click();
	
	// select menu item
	$('.top-menu a').removeClass('selected');
	$(link).addClass('selected');
	
	// show thumbs	
	$.gallery.albumId = albumId;
	
	// set title & description
	$('.album-info').html($('.album-info-text[album='+albumId+']').html());
		
	$('.thumbs-pages a').hide();
	$('.thumbs-pages a[album='+albumId+']').show();
	
	// select first page
	selectAlbumPage(albumId, 0);
	
	if ($('.thumbs-pages a[album='+albumId+']').length==0)
	{
		$('.controls,.slideshow,.image-options,.thumbs-container').hide();
		$('.album-info').html('Foto&#39;s zijn binnenkort beschikbaar.');
	}
	else
	{
		$('.controls,.slideshow,.image-options,.thumbs-container').show();
	}
}

function selectAlbumPage(albumId, pageNr)
{
	$('.thumbs-pages a').removeClass('selected');
	$('.thumbs-pages a[album='+albumId+'][page='+pageNr+']').addClass('selected');
	
	//$('.thumbs li[album!='+albumId+']').hide();
	$('.thumbs li').hide();
	$('.thumbs li[album='+albumId+'][page='+pageNr+']').show()
		.find('a.thumb').first().click();
}

function initGallery(id, slideshowId, controlsId)
{
	// We only want these styles applied when javascript is enabled
	$('div.navigation').css({'width' : '300px', 'float' : 'left'});
	$('div.content-gallery').css('display', 'block');
	
	gallery = $('#'+id).gallery({
		albumTitle: 'Opening',
		albumDescription: 'Opening van de Forum Lounge Club...'
	});
}

function showGallery(album, targetId)
{
	$.get('gallery.php?album='+album, function(data){
		// set thumbs
		$('#'+targetId).html(data);
		// init gallery
		initGallery('thumbs','slideshow','controls');
	});
}

function initLoungeClubMenu()
{
	$('.menu-item ul').addClass('menulist');
	
	$('.menulist li').after('<li class="menu-item-seperator">&middot;</li>');
	// remove last dot
	$('.menulist').each(function(){$(this).children().last().hide()})
}

function setLogo(page)
{
	if ((page!='loungeclub') && (page!='hopkamer') && (page!='watch') && (page!='party'))
		page = '';
	
	if (page != '')
		page = '_' + page;
	else
		page = '_home';
		
	$('#logo-sub').attr('src','images/logo'+page+'.png');
}

function stringStartsWith(str, prefix)
{
	if ((typeof str == 'string') && 
		(typeof prefix == 'string'))
		return str.substring(0,prefix.length) == prefix;
	
	return false;
}
