/**
 * @section   : Unobtrusive JavaScript events and function calls, triggered on DOMContentLoaded.
 * @project   : BKV dynamic banner
 * @author    : Wietse Ouwerkerk <wietse@e-sites.nl>
 * @version   : 1.0
 */

var activeBanner;
var horizontalID;
var verticalID;

$(document).ready(function() {
	//add last banner optimalisation in ie6.
	if(!window.XMLHttpRequest) checkLastClass();

	$('.banners li a.structure').bind('mouseover', function(){
		if(horizontalID >= 0){
			clearTimeout(horizontalID);
		}
		horizontalID = setTimeout("bannerClick(" + $('.banners li a.structure').index(this) + ");", 300);
	}).bind('mouseout', function(){
		if(horizontalID >= 0){
			clearTimeout(horizontalID);
		}
	});
	$('.banners li a.first').mouseover();

	$('.vBanner a.structure').bind('mouseover', function(){
		if(vBannerClick >= 0){
			clearTimeout(verticalID);
		}
		verticalID = setTimeout("vBannerClick(" + $('.vBanner a.structure').index(this) + ");", 300);
	}).bind('mouseout', function(){
		if(verticalID >= 0){
			clearTimeout(verticalID);
		}
	});
});

/**
 * handles a banner click when clicked on.
 * @return false to stop the click action
 */

function bannerClick(index){
	var self = $('.banners li a.structure').get(index);
	var parent = $(self).parent('li');
	if(activeBanner !== index){
	//open the new banner
	parent.find('div').slideDown('fast');
	parent.addClass('active');

	//fake a click to close the open banner.
	if(activeBanner != null){
		$($('.banners li a.structure')[activeBanner]).parent('li').removeClass('active');
		$($('.banners li a.structure')[activeBanner]).parent('li').find('div').slideUp('fast');
	}

	activeBanner = index;

	} else {
		$($('.banners li a.structure')[activeBanner]).parent('li').removeClass('active');
		$($('.banners li a.structure')[activeBanner]).parent('li').find('div').slideUp('fast');
		activeBanner = null;
	}
	return false;
}

function vBannerClick(index){
	var self = $('.vBanner a.structure').get(index);
	var sDiv = '.vBannerContent .' + $(self).attr('rel');
	$(self).parent('li').addClass('active').siblings().removeClass('active');
	$(sDiv).siblings().each(function(){
		if(!$(this).hasClass('hide')){
			$(this).fadeOut('fast', function(){
				$(sDiv).fadeIn('fast', function(){
					$(this).removeClass('hide')
				})
			}).addClass('hide');
		}
	});

	return false;
}

/**
 * checks if its ie6. if it is, it will adjust the last banner in the list.
 */
function checkLastClass(){
	if(!window.XMLHttpRequest){
		var last = $('#lastbanner');
		if(last.length > 0){
			//if the parent of the last banner has the active class add the active image.
			if(last.parent('li').hasClass('active')){
				if(last.hasClass('odd')){
					last.css('background-position', '-245px -182px');
				} else {
					last.css('background-position', '-245px -234px');
				}
			} else {
				//else set the mouseover and mouseout events
				var over = 0;
				var out = 0;

				if(last.hasClass('odd')){
					over = -182;
					out = -156;
				} else {
					over = -234;
					out = -208;
				}

				last.css('background-position', '-245px '+out+'px').bind("mouseover", function(){
					$(this).css('background-position', '-245px '+over+'px');
				}).bind("mouseout", function(){
					$(this).css('background-position', '-245px '+out+'px');
				});
			}
		}
	}
}