/**
* @author arjen
*/

var bDebug = false;

/**
 * cross browser stuff
 */	
if ($.browser.mozilla) {
	if (parseFloat($.browser.version) == 1.9)
		$("body").addClass("FF3");
	else if (parseFloat($.browser.version) < 1.9)
			$("body").addClass("FF2");
}	
else if ($.browser.msie && parseFloat($.browser.version) < 7) {
	$("body").addClass("IE6");
	$("acronym[title]").addClass('hasTitle'); /* ie6 doesn't support abbr element */
}

$('body').ImageReplace();

/**
 * mainmenu
 * 
 */
// superfish
$("#hoofdmenu .nav").superfish({speed:0});

// remove borders when active
$('#hoofdmenu li.active').prev().andSelf().find('a').css({backgroundImage: 'none'});


/**
 * anchors (external/popup)
 */
$("a[rel='external']").click(function(){window.open(this.href); return false});
var oPopWin = null;
$("a[rel='popup']").click(function(){
	sHref = $(this).attr('href');
	if (sHref.substr(-1) != '/')
		sHref += '/';
	sHref += '?popup=true';
	if (oPopWin && oPopWin.open && !oPopWin.closed) oPopWin.close();
	oPopWin = window.open(sHref, 'winName', 'width=500, height=500'); return false
});


/**
 * submitHovers
 */
$('input.textSubmit').hover(function(){
	$(this).addClass('textSubmitHover');
},function(){
	$(this).removeClass('textSubmitHover');
});


/**
 * zoekform
 */
if ($('#zoekForm').length) {
	var sZoekFormVal = '';
	$('#zoekForm input.text').addClass('inactive').val('trefwoord');
	$('#zoekForm input.text').focus(function(){
		$(this).val(sZoekFormVal).removeClass('inactive');
	});
	$('#zoekForm input.text').blur(function(){
		sZoekFormVal = $(this).val();
		if (sZoekFormVal == '') 
			$(this).addClass('inactive').val('trefwoord');
	});
}

/**
 * styleswitcher
 */
$(function(){
	var cookie = $.cookie("style");
	var title = cookie ? cookie : StyleSwitcher.getPreferredStyleSheet();
	
	if ($('body').is('#bodySplash')) {
		$('#naarHome').append('<br /><span class="hidden">textgrootte:</span> <span id="textResizeBox"><a href="#" id="textResize-normaal" title="zet tekstgrootte normaal">A</a> <a href="#" id="textResize-groter" title="zet tekstgrootte groter">A</a> <a href="#" id="textResize-grootst" title="zet tekstgrootte grootst">A</a></span>');

		$('#textResize-normaal').click(function(){
			StyleSwitcher.setActiveStyleSheet('tekst normaal');
		});
		$('#textResize-groter').click(function(){
			StyleSwitcher.setActiveStyleSheet('tekst groter');
		});
		$('#textResize-grootst').click(function(){
			StyleSwitcher.setActiveStyleSheet('tekst grootst');
		});
	}
	else {
		var textsizemenu = '<li id="snelmenu-tekst"><ul><li id="snelmenu-tekst-normaal"><a href="#" title="zet tekstgrootte normaal">A</a></li><li id="snelmenu-tekst-groter"><a href="#" title="zet tekstgrootte groter">A</a></li><li id="snelmenu-tekst-grootst"><a href="#" title="zet tekstgrootte grootst">A</a></li></ul></li>';
		if ($('#snelmenu-uitloggen').length)
			$('#snelmenu-uitloggen').before(textsizemenu);
		else
			$('#snelmenu ul').append(textsizemenu);
	
		$('#snelmenu-tekst-normaal a').click(function(){
			StyleSwitcher.setActiveStyleSheet('tekst normaal');
		});
		$('#snelmenu-tekst-groter a').click(function(){
			StyleSwitcher.setActiveStyleSheet('tekst groter');
		});
		$('#snelmenu-tekst-grootst a').click(function(){
			StyleSwitcher.setActiveStyleSheet('tekst grootst');
		});
	}
	StyleSwitcher.setActiveStyleSheet(title);	
});

$(window).unload(function(){
	var title = StyleSwitcher.getActiveStyleSheet();
	$.cookie("style", title);
});

// overlay inactive items
$('body#bodyReserveren #content .voorstelling').not('.voorstellingActive').find('img').after('<span class="overlay"></span>');


/**
 * suggesties/winkelmandjes pops
 */
var iPopTimeoutId, oHoverdItem;

$('#suggesties li.voorstelling, #winkelmandje li.voorstelling').hover(function(){
	window.clearTimeout(iPopTimeoutId);	
	if (bDebug) $.log('in1');
	
	artiest = $(this).find('.voorstellingInfo .artiest').text();
	titel = $(this).find('.voorstellingInfo .titel').text();
	
	// need to create voorstellingPopup?
	if (!$('#voorstellingPop').length) {
		$('body').append('<div id="voorstellingPop"></div>');		
	}
	$('#voorstellingPop').addClass('voorstellingPopHover');
	
	// set popup contents
	//$('#voorstellingPop').html('<a href="#" class="voorstellingDetailLink"><span class="voorstellingKop"><span class="artiest">' + artiest + '</span><span class="titel">' + titel + '</span></span></a>');
	oWrap = $(this).html();
	$('#voorstellingPop').html(oWrap);
	$('#voorstellingPop').find('img, .voorstellingDatum').remove();
	$('#voorstellingPop').show();
	
	// calculate popup offset & set it
	oOffset = $(this).offset();	
	iOffsetY = $(this).find('.voorstellingDatum').height();
	iOffsetX = $(this).find('.voorstellingDatum').width();
	
	iVoorstellingPopWidth = $('#voorstellingPop').width() + parseInt($('#voorstellingPop').css('paddingLeft')) + parseInt($('#voorstellingPop').css('paddingRight'));
	iVoorstellingPopHeight = $('#voorstellingPop').height();
	if (bDebug) $.log(iVoorstellingPopWidth);
	iPopLeft = oOffset.left-iVoorstellingPopWidth+iOffsetX;
	if (oOffset.left < 260)
		iPopLeft = oOffset.left;
	
	iPopTop = oOffset.top+iOffsetY;	
	
	$('#voorstellingPop').css({ left: iPopLeft, top: iPopTop });
	
	// popup hover events
	$(oHoveredItem).removeClass('voorstellingHover');
	
	oThisItem = $(this);
	oHoveredItem = oThisItem;
	oThisItem.addClass('voorstellingHover');
	
	$('#voorstellingPop').hover(function() {
		if (bDebug) $.log('in2');
		$(oHoveredItem).removeClass('voorstellingHover');
		oHoveredItem = oThisItem;
		oThisItem.addClass('voorstellingHover');
		$('#voorstellingPop').addClass('voorstellingPopHover');		
		window.clearTimeout(iPopTimeoutId);			
	}, function() {
		if (bDebug) $.log('out2');
		
		window.clearTimeout(iPopTimeoutId);	
			
		iPopTimeoutId = window.setTimeout(function(){
			if (bDebug) $.log('remove2');
			$('#voorstellingPop').removeClass('voorstellingPopHover');
			oThisItem.removeClass('voorstellingHover');
			$('#voorstellingPop').remove();
			oHoveredItem = null;
		}, 300);
	});
	
	$('#voorstellingPop').click(function(){
		window.location.href = $(this).find('a.voorstellingDetailLink').attr('href');
	});
	
}, function(){
	if (bDebug) $.log('out1');
	iPopTimeoutId = window.setTimeout(function(){
		if (bDebug) $.log('remove1');
		$('#voorstellingPop').remove();
		$('#voorstellingPop').removeClass('voorstellingPopHover');
		$(oHoveredItem).removeClass('voorstellingHover');
		oHoveredItem = null;
	}, 300);
});