// Init custom scrollbar
$(function() {
	// Check if we have a vertical contentblock to scroll
	if($('div.contentBlockCheckPoem').length) {
		// Set scroll pane
		setScrollPane('vertical', $('div.contentBlockCheckPoem'), 24, 235, 235);
	}
});

// Init accordion
$(function() { 
	// Check if we have an accordion
	if($('ul.FAQAccordion').length) {
		// Turn element into an accordion       
		$("ul.FAQAccordion").accordion({ 
			autoHeight: false,
			change: function(event, ui) {
				// Execute after accordion finished
				setBGFAQPage();
				// Set equal heights of content blocks
				setEqualHeights('div.contentBlockLeft', 'div.contentBlockRight');
				// Set background below content
				setBackgroundBelowContent();
			}
		});
	}
});

// Initialize
$(document).ready(function(){
	// Set background heights
	setBGHeight();
	setBackgroundBelowContent();
	// Set equal heights of content blocks
	setEqualHeights('div.contentBlockLeft', 'div.contentBlockRight');
	// Set hover state of menu items
	setMainMenuHover();
	// Set hover state of buttons
	setButtonsHover();
	// Set active FAQ if on FAQ page; default is first
	setActiveFAQ(false);
	// Set hover state of FAQ buttons
	setFAQHover();

	// Set close button fuctionality
	// Check if we have a closebutton
	if($('img.closePopup').length) {
		// Show mouseover state of button when hovered over
		$('img.closePopup').click(function() { 
			// Close popup when close button is clicked
			parent.Shadowbox.close();
		});
	}
	
	// Init autogrow textarea
	// Check if we have a textarea that needs flexible height
	if($('textarea#flexibleHeight').length) {
		$('textarea#flexibleHeight').autoResize({
			// On resize:
			onResize : function() {
				// Get textarea height
				var TAHeight = $('textarea#flexibleHeight').height();

				// Set scroll pane
				if(TAHeight>290) {
					// Set custom scroll
					setScrollPane('vertical', $('div.poemInputArea'), 20, 145, 145);
					// Scroll to end of textarea
					$('div.poemInputArea')[0].scrollTo(TAHeight);
					// Re-focus again after init custom scroll
					$('textarea#flexibleHeight').focus();
				} else {
					// Remove custom scroll
					$('div.poemInputArea').jScrollPaneRemove();
				}
			}
		});
	}

	// Check if we have a form
	if($('form#formPersonalData').length || $('form#formContact').length) {
		// Init pretty checkboxes and radio buttons
		$('form#formPersonalData input[type=checkbox], form#formContact input[type=checkbox]').prettyCheckboxes();
		$('form#formPersonalData input[type=radio], form#formContact input[type=radio]').prettyCheckboxes({
			'display':'inline'}
		);
	}

	// Get all text input fields
	$('input[type=text], textarea').each(function() {
		// Get default value
		$(this).attr("rel",this.value);
		var defaultValue = $(this).attr("rel");

		// Empty field on focus
		$(this).focus(function() {
			if(this.value == defaultValue) {
				this.value = '';
			}
		});

		// When value is not changed, set back default value on blur
		$(this).blur(function() {
			if(this.value == '') {
				this.value = defaultValue;
			}
		});
	});

	// Check if we are on the page terms&conditions 
	if($('div.backgroundTerms').length) {
		// Set background height of terms page
		setBGFAQPage();
	}

	// Check if we have a horizontal contentblock to scroll
	if($('div.holderMenuSub').length) {
		// Set correct actual width of product menu
		var productMenuWidth = getProductMenuWidth();

		// If we have a width
		if(productMenuWidth) {
			// Set width
			$('div.menuSub, div.menuSub ul').width(productMenuWidth);

			// Set scroll pane
			setScrollPane('horizontal', $('div.holderMenuSub'), 17, 140, 170);
		}
	}
});

// On resize
$(window).resize(function() {
	// Reset background heights
	setBGHeight();
	setBackgroundBelowContent();

	// Check if we are on the page terms&conditions 
	if($('div.backgroundTerms').length) {
		// Reset background height of terms page
		setBGFAQPage();
	}
});
