jQuery(function() {
	if (window.self != window.top)
		window.top.location = window.self.location;

	if (jQuery('ul#menuList').length > 0) {
		jQuery('ul#menuList')
		.superfish({ animation: {height:'show'},delay:500,autoArrows:false,dropShadows:true })
		.find('ul')
		.bgIframe({opacity:false});
	}

	if (jQuery('a.fancyIt').length > 0) {
		fancyIt();
	}

	if (jQuery('a.filmpjesPopup').length > 0) {
		jQuery('a.filmpjesPopup').click(function() {
			jQuery.fancybox({
				'padding' : 20,
				'autoScale' : true,
				'transitionIn' : 'elastic',
				'transitionOut' : 'elastic',
				'title' : this.title,
				'width' : 300,
				'height' : 225,
				'href' : this.href,
				'type' : 'swf',
				'swf' : {
					'wmode' : 'transparent',
					'allowfullscreen' : 'true'
				}
			});

			return false;
		});
	}

	/* Woningen foto / video scroller */
	if (jQuery('#woningMedia #aanbodContent li').length > 0) {
		jQuery('#woningMedia #aanbodContent').jcarousel({
			auto: 0,
			wrap: null,
			animation: 2000,
			initCallback: mycarousel_initCallback
		});
	}
	/* Homepage aanbod scroller */
	if (jQuery('#aanbodScroller #aanbodContent li').length > 0) {
		jQuery('#aanbodScroller #aanbodContent').jcarousel({
			auto: 4,
			wrap: 'circular',
			animation: 2000,
			initCallback: mycarousel_initCallback
		});
	}

	jQuery('fieldset#fieldsetAanbodFilter input[type=text]').blur(function() {
		if (jQuery(this).attr('id') == 'prijsVan' || jQuery(this).attr('id') == 'prijsTot' || jQuery(this).attr('id') == 'oppervlak') {
			if (isNaN(jQuery(this).val()))
				jQuery(this).val('');
		}
		jQuery('#p_ID').val(1);
	});

	jQuery('a.playIcon').hover(function() {
		$(this).parent().find('a:first').addClass('sfHover');
	}, function() {
		$(this).parent().find('a:first').removeClass('sfHover')
	});
});

function fancyIt() {
	jQuery('a.fancyIt').fancybox({'transitionIn':'elastic','transitionOut':'elastic','speedIn':600,'speedOut':200,'overlayShow':true,
		'titlePosition':'outside'
	});
}

var lastType = '';
function setMediaItem(num, mov1, mov2, youtube) {
	var mediaType = '';
	var cont = jQuery('#mediaContainer');

	if (!isNaN(num)) {
		if (jQuery('#woningMedia #aanbodContent li div.aanbodItem a img.foto_'+num).length) {
			mediaType = 'foto';
			var thumb = jQuery('#woningMedia #aanbodContent li div.aanbodItem a img.foto_'+num);
			jQuery('#woningMedia #aanbodContent li div.aanbodItem a').removeClass('sfHover');
			jQuery(thumb).parent().addClass('sfHover');
			setMediaItemFoto(num, cont, thumb);
			lastType = mediaType;
		} else if (jQuery('#woningMedia #aanbodContent li div.aanbodItem a img.film_'+num).length) {
			if (youtube.toString() != "") {
				mediaType = 'youtube';
			} else {
				mediaType = 'film';
			}
			var thumb = jQuery('#woningMedia #aanbodContent li div.aanbodItem a img.film_'+num);
			jQuery('#woningMedia #aanbodContent li div.aanbodItem a').removeClass('sfHover');
			jQuery(thumb).parent().addClass('sfHover');
			setMediaItemMovie(num, cont, thumb, mov1, mov2, youtube);
			lastType = mediaType;
		}
	}
}

function setMediaItemFoto(num, cont, thumb) {
	var orgSrcThumb = jQuery(thumb).attr('longdesc');//.replace('/images/thumb/', '/images/original/');
	var orgSrcFull  = jQuery(thumb).attr('fulldesc'); //.replace('/images/thumb/', '/images/original/');

	/* Reset media container html */
	if (lastType != 'foto') {
		jQuery(cont)
		.html($('#imgDummy').html());
	}
	jQuery('.aanbodItem a[ind='+num+']').attr('rel', 'overzichtMediaDisabled');

	jQuery(cont).find('img:first').attr('src', orgSrcThumb);
	jQuery(cont).find('a')
	.attr('href', orgSrcFull)
	.attr('rel', 'overzichtMedia')
	.fancybox({
		'transitionIn':'elastic','transitionOut':'elastic','speedIn':600,'speedOut':200,'overlayShow':true,
		'titlePosition':'outside',
		'onClosed': function() {
			jQuery('.aanbodItem a[ind='+num+']').attr('rel', 'overzichtMedia');
		}
	});
}

var flashMovie = null;
function setMediaItemMovie(num, cont, thumb, mov1, mov2, youtube) {
	flashMovie = null;

	if (youtube.toString() != "") {
		var dummy = $('#youtubeDummy').html().toString();
		dummy = dummy.replace(/myYoutubeLink/g, youtube.toString());
		jQuery(cont).html(dummy);
	} else if (mov2.toString() != "nomovie.swf") {
		if (lastType != 'film')
			jQuery(cont).html($('#movieDummy').html());

		flashMovie = $('.innerContent #flashcontent');
		flashMovie.flash({
			swf: '/combine/combine0.swf',
			width: 300,
			height: 225,
			play: false,
			flashvars: {
				movie1:"/movies/"+mov1,
				movie2:"/movies/"+mov2
			}
		});
	} else {
		if (lastType != 'film')
			jQuery(cont).html($('#movieDummy').html());

		flashMovie = $('.innerContent #flashcontent');
		flashMovie.flash({
			swf: '/combine/combine0.swf',
			width: 300,
			height: 225,
			play: false,
			flashvars: {
				movie1:"/movies/"+mov1
			}
		});
	}
}


function mycarousel_initCallback(carousel) {
	// Disable autoscrolling if the user clicks the prev or next button.
	carousel.buttonNext.bind('click', function() {
		carousel.startAuto(0);
	});

	carousel.buttonPrev.bind('click', function() {
		carousel.startAuto(0);
	});

	// Pause autoscrolling if the user moves with the cursor over the clip.
	carousel.clip.hover(function() {
		carousel.stopAuto();
	}, function() {
		carousel.startAuto();
	});
};

function setSort(sortValue) {
	jQuery('#sorton').val(sortValue);
	jQuery('#frmZoeken input[type=submit]').click();

	return false;
}

function setPage(pageNum) {
	if (isNaN(pageNum))
		pageNum = 1;

	jQuery('#p_ID').val(pageNum);
	jQuery('#frmZoeken input[type=submit]').click();

	return false;
}
