jQuery.noConflict();

/**
 * Faq
 */

function faqOpen(item) {
	// When this method is called with the item argument, make sure 'this' is not a reference to the window object
	if (jQuery(this).attr('id') != undefined) {
		item = this;
	}
	
	jQuery(item).parent().parent().find(".active").each(function() {
		jQuery(this).parent().children("div").hide("fast");
		jQuery(this).parent().attr("class", "");
	});	
	
	jQuery(item).parent().children("div").show("medium");
	jQuery(item).parent().attr("class", "faq-open");
	jQuery(item).attr("class", "active");
}
function faqClose(item) {
	if (jQuery(this)) {
		item = this
	}
	jQuery(item).parent().children("div").hide("fast");
	jQuery(item).parent().attr("class", "");
	jQuery(item).attr("class", "");
}

/**
 * Navigation
 */

// On Hover Over
function megaHoverOver() {
	var li = jQuery(this);
	var submenu = li.find(".submenu");
	if (jQuery.browser.msie && jQuery.browser.version <= 6) var submenuRow = li.find(".submenu .submenu-inner .categories .row");
	var offset = li.offset().left - jQuery('#container').offset().left;

	if ( (offset - 100) < 0 ) {
		submenu.css({'left': 0});
	}

	if (jQuery.browser.msie && jQuery.browser.version <= 6) submenuRow.css({'width': (submenu.find('div.row:first div.cat').length*200)-58});
	submenu.css({'width': submenu.find('div.row:first div.cat').length*200});

	if(!submenu.find('.h3').length) {
		var width = (li.width() > 245) ? li.width() + 5 : 245;
		submenu.css({'width': width, 'left': 0});
	}

	if (submenu.length) {
		li.find('a[class=mma]').addClass('hover');
	}

	if ( (offset + submenu.width()) > jQuery('#container').width() ) {
		submenu.css({'left': jQuery('#container').width() - (offset + submenu.width())});
	}

	submenu.stop().fadeTo('fast', 1).show(); // Find sub and fade it in

	if (jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#page").css("height",submenu.height());
	else jQuery("#page").css("min-height",submenu.height());
}
// On Hover Out
function megaHoverOut() {
	var li = jQuery(this);
	li.find(".submenu").stop().fadeTo('fast', 0, function() { // Fade to 0 opactiy
		li.find('a.hover').removeClass('hover');
		jQuery(this).hide(); // After fading, hide it
	});
}
// On Focus Out
function megaFocusOut() {
	jQuery("#main-menu li.dropdown").find(".submenu").stop().fadeTo('fast', 0, function() { // Fade to 0 opactiy
		jQuery("#main-menu li.dropdown").find('a.hover').removeClass('hover');
		jQuery(this).hide(); // After fading, hide it
	});
}

// Set custom configurations
var config = {
	sensitivity: 1, // number = sensitivity threshold (must be 1 or higher)
	interval: 0, // number = milliseconds for onMouseOver polling interval
	over: megaHoverOver, // function = onMouseOver callback (REQUIRED)
	timeout: 250, // number = milliseconds delay before onMouseOut
	out: megaHoverOut // function = onMouseOut callback (REQUIRED)
};

jQuery(document).ready(function() {
	if (jQuery.browser.msie && jQuery.browser.version <= 7) jQuery("#footer").css("position", "static");

	jQuery("ul.faq > li > div").hide();
	jQuery("ul.faq > li > span.question").html(function(index, html) {
		return '<a href="#" class="faq">' + html + '</a>';
	});
	jQuery("ul.faq > li > span.question").toggle(faqOpen, faqClose);
	var location = jQuery(document).attr('location').href;
	var anchor = location.split('#');
	if (anchor[1]) {
		var answer_id = '#'+anchor[1];
		if (jQuery(answer_id)) {
			faqOpen(answer_id);
		}
	}

	jQuery("#main-menu li .submenu").css({'opacity':'0'}); // Fade sub nav to 0 opacity on default
	jQuery("#main-menu li.dropdown").hoverIntent(config); // Trigger Hover intent with custom configurations

	var currentLocation=document.location.toString();
	if(currentLocation.match('#')) { currentLocation=currentLocation.split('#')[0]; }
	jQuery("#main-menu,#footer").prepend('<a href="'+currentLocation+'#main" class="skip-link">Direct naar inhoud</a> ').append('<a href="'+currentLocation+'#main" class="skip-link">Direct naar inhoud</a> ');
	jQuery("#main,#footer").prepend('<a href="'+currentLocation+'#main-menu" class="skip-link">Direct naar navigatie</a> ');
	jQuery("#main-menu .skip-link").focusin(function() { megaFocusOut(); });

	jQuery("#main-menu li.dropdown .mma").focusin(function() {
		var mma = jQuery(this);
		jQuery("#main-menu li.dropdown").find(".submenu").stop().fadeTo('fast', 0, function() { // Fade to 0 opactiy
			jQuery("#main-menu li.dropdown").addClass('find-hover');
			mma.parent().removeClass('find-hover');
			jQuery("#main-menu li.find-hover").find('a.hover').removeClass('hover');
			jQuery(this).hide(); // After fading, hide it
		});
		var submenu = mma.parent().find(".submenu");
		if (jQuery.browser.msie && jQuery.browser.version <= 6) var submenuRow = mma.parent().find(".submenu .submenu-inner .categories .row");
		var offset = mma.parent().offset().left - jQuery('#container').offset().left;

		if ( (offset - 100) < 0 ) {
			submenu.css({'left': 0});
		}

		if (jQuery.browser.msie && jQuery.browser.version <= 6) submenuRow.css({'width': (submenu.find('div.row:first div.cat').length*200)-58});
		submenu.css({'width': submenu.find('div.row:first div.cat').length*200});

		if(!submenu.find('.h3').length) {
			var width = (mma.parent().width() > 245) ? mma.parent().width() + 5 : 245;
			submenu.css({'width': width, 'left': 0});
		}

		if (submenu.length) {
			mma.addClass('hover');
		}

		if ( (offset + submenu.width()) > jQuery('#container').width() ) {
			submenu.css({'left': jQuery('#container').width() - (offset + submenu.width())});
		}

		submenu.stop().fadeTo('fast', 1).show(); // Find sub and fade it in

		if (jQuery.browser.msie && jQuery.browser.version <= 6) jQuery("#page").css("height",submenu.height());
		else jQuery("#page").css("min-height",submenu.height());
	});

	jQuery('.btn-print').click(function() {
		window.print();
		return false;
	});

	jQuery('#download-video').next().hide();
	jQuery("#download-video").html(function(index, html) {
		return '<a href="#">' + html + '</a>';
	});
	jQuery('#download-video').toggle(function() {
		jQuery(this).next().slideDown();
	}, function() {
		jQuery(this).next().slideUp();
	});

	jQuery('#show-text').next().hide();
	jQuery("#show-text").html(function(index, html) {
		return '<a href="#">' + html + '</a>';
	});
	jQuery('#show-text').toggle(function() {
		jQuery(this).next().slideDown();
	}, function() {
		jQuery(this).next().slideUp();
	});

	jQuery('.file-info-box').hide();

	jQuery('a.download').click(function() {
		if (!jQuery(this).attr('id'))
			return true;
		var id = jQuery(this).attr('id').split('-')[2];
		if (!id)
			return true;

		var block = jQuery('#file-block-' + id).remove();
		block.css({'position': 'absolute', 'background': 'white', 'left': (jQuery(this).offset().left - jQuery('#container').offset().left) });
		if (jQuery.browser.msie) {
			block.css({'margin-top':25});
		}

		if (!block.find('img').length) {
			block.prepend('<img src="templates/provinciegroningen/assets/images/btn_close.png" alt="x" class="close" />');
		}
		block.find('img[class=close]').click( function() {
			jQuery('#file-block-' + id).hide();
		});

		jQuery(this).after(block);
		block.show();

		return false;
	});

	if (jQuery.browser.msie && jQuery.browser.version <= 7) jQuery("#footer").css("position", "relative");
});

