var maxFadeOutItems = 0;
var currentFadeOutItem = 0;
var maxCarouselItems = 0;
var currentCarouselItem = 1;
var googleRouteText = "Bereken uw route vanaf";
var url = document.location.toString();

$(document).ready(function() {
	$('.readmore .pijl').attr('align', 'absmiddle');

	initSubMenuHeight();
	initFadeImages();
	initEditPanel();
	initPhotoGallery();
	initPortfolio();
	initCarousel();
	initNewsLinks();
	initNewsletterForm();

	//	var webcamproject = $(".removeProjectJquery").parent().html();
	//	$(".removeProjectJquery").parent().parent().parent().parent().parent().remove();
	//	$(".webcamlist li img.addProjectJquery").parent().append(webcamproject);

	var webcamproject = $(".removeProjectJquery").parent().parent().html();
	$(".removeProjectJquery").parent().remove();
	$(".webcamlist li img.addProjectJquery").parent().parent().append(webcamproject);

	$('#navParent li').each(function() {
		if ($(this).attr('onmouseover') != "") {
			$(this).removeAttr('onmouseover');
			$(this).removeAttr('onmouseout');
		}
	});
});

$(window).resize(function() {
	initNewsletterForm();
});

function showGoogleRoute() {
	if (url.indexOf("en-GB") > 0)
		googleRouteText = "Show route from"

	document.write('<input type="text" id="addressfield" size="30" value="' + googleRouteText + '" />');
	document.write('<div class="button"><a id="googleGoButton" href="javascript:void(0);" class="googleGo">Route</a></div>');

	$('#addressfield').bind('focus', function() {
		var val = $(this).val();
		if (val == googleRouteText) {
			$(this).attr("value", "");
		}
	});

	$('#addressfield').bind('blur', function() {
		var val = $(this).val();
		if (val == "") {
			$(this).attr("value", googleRouteText);
		}
	});

	$('#googleGoButton').bind('click', function() {
		var val = $('#addressfield').val();
		if (val != googleRouteText && val != "") {
			window.open('http://maps.google.com/?f=d&source=s_d&daddr=Maasboulevard 7, 3114 HB Schiedam, Netherlands (DHVG Property Solutions)&saddr=' + val + '&hl=nl&geocode=');
		}
		else {
			if (url.indexOf("en-GB") > 0)
				alert('Please fill in an address');
			else
				alert('Vul alstublieft een adres in');
		}
	});
}

function initNewsletterForm() {
	$('#newsletterModalOverlay').height($(window).height());
	$('#newsletterModalOverlay').width($(window).width());
	$('#newsletterModalOverlay').bind("click", function() {
		CloseNewsletterModal();
	});
}

function OpenNewsletterForm() {
	$('#newsletterModalOverlay').fadeTo('medium', 0.8, function() {
		$('#newsletterPositioner').fadeIn();
	});
}

function CloseNewsletterModal() {
	$('#newsletterPositioner').fadeOut('medium', function() {
		$('#newsletterModalOverlay').fadeOut('medium');
		var link = $('.newsletterReset').attr('href');
		if (link != undefined && link != "") {
			location.href = link;
		}
	});
}

function initNewsLinks() {
    $('.DNN_ANN_Description').each(function() {
        var url = $(this).children('span').children('a').attr('href');
        if (url != undefined) {
            var loc = 'location.href=\'' + url + '\';';
            $(this).parent().children('.SRTItemHeader').bind('click', function() { eval(loc) }).css('cursor', 'pointer');
            $(this).parent().parent().children('.newsimagealt').children('img').bind('click', function() { eval(loc) }).css('cursor', 'pointer');
            $(this).parent().parent().children('.newsimage').children('img').bind('click', function() { eval(loc) }).css('cursor', 'pointer');
        }
    });
}

function initSubMenuHeight() {
	$('#nav ul li ul').each(function() {
		var contentHeight = $("#contentContainer").height();

		if (contentHeight != "" && contentHeight != undefined) {
			if ($.browser.msie) {
			$(this).height(contentHeight+100);
			}
			else
			{
			$(this).height(contentHeight);
			}
		}
    });
    
   $('#nav ul li ul').hide();

   $('#nav ul li').hover(function() {
   		$(this).addClass("hoverMenu").children("ul").show();
   	//$(this).children("ul").fadeTo("slow",0.85);
   }, function() {
   	//$(this).children("ul").fadeTo("slow", 0, function() {
   	//	$(this).parent().removeClass("hoverMenu");
   	//});
		$(this).removeClass("hoverMenu").children("ul").hide();
   });
}

function initCarousel() {
	$("#carousel ul li").each(function() {
	$(this).attr("fadeNr", maxCarouselItems);
		maxCarouselItems++;
	});

	if (maxCarouselItems > 0) {
		setTimeout("FadeOutCarousel();", 2000);
	}
}

function FadeOutCarousel() {
	$("#carousel ul li[fadeNr=" + currentCarouselItem + "]").fadeIn(1000);
	$("#carousel ul li[fadeNr!=" + currentCarouselItem + "]").fadeOut(1000);

	currentCarouselItem++;

	if (currentCarouselItem >= maxCarouselItems) {
		currentCarouselItem = 0;
	}

	setTimeout("FadeOutCarousel();", 5000);
}

function initFadeImages() {
	$(".fadeOutImage img").hover(function() {
		$(this).fadeTo("fast", 1);
	}, function() {
		$(this).fadeTo("fast", 0.4);
	});

	$(".fadeOutImage  img").each(function() {
		$(this).attr("fadeNr", maxFadeOutItems);
		maxFadeOutItems++;
	});

	if (maxFadeOutItems > 0) {
		setTimeout("FadeOutImages();", 2000);
	}

	
}

function FadeOutImages() {
	$(".fadeOutImage  img[fadeNr=" + currentFadeOutItem + "]").fadeTo(2000, 1);
	$(".fadeOutImage  img[fadeNr!=" + currentFadeOutItem + "]").fadeTo(2000, 0.4);

	currentFadeOutItem++;

	if (currentFadeOutItem >= maxFadeOutItems) {
		currentFadeOutItem = 0;
	}

	setTimeout("FadeOutImages();", 3000);

}

function initEditPanel() {
	$('.editPanel').hover(function() {
		$("#" + this.id + "_popup").show();
		$("#" + this.id + "_popup").bind("mouseleave", function() {
			$(this).hide();
		});
	}, void (0));
}

function initPhotoGallery() {
	if ($('#photogallery a').Size > 0) $('#photogallery a').lightBox({ fixedNavigation: true });

	$('a.galleryNext').bind("click", function() {
		$('#photogallery').scrollTo({ top: '+=471px' }, 1500);
	});
	$('a.galleryPrevious').bind("click", function() {
		$('#photogallery').scrollTo({ top: '-=471px' }, 1500);
	});
	$("#photogallery li").hover(function() {
		$(this).addClass("galHover");
	}, function() {
		$(this).removeClass("galHover");
	});

	$('#photogalleryThumbs a.galLink').lightBox({ fixedNavigation: true });
	$('a.galleryNextThumbs').bind("click", function() {
		$('#photogalleryThumbs').scrollTo({ top: '+=471px' }, 1500);
	});
	$('a.galleryPreviousThumbs').bind("click", function() {
		$('#photogalleryThumbs').scrollTo({ top: '-=471px' }, 1500);
	});
	$("#photogalleryThumbs li").hover(function() {
		$(this).addClass("galHover");
	}, function() {
		$(this).removeClass("galHover");
	});

	if ($('#photogallery li').size() < 13) {
		$('a.galleryNext').hide();
		$('a.galleryPrevious').hide();
	}

	if ($('#photogalleryThumbs li').size() < 13) {
		$('a.galleryNextThumbs').hide();
		$('a.galleryPreviousThumbs').hide();
		$('a.galleryTerugThumbs').css("margin", "0 250px 0 0");
	}
}

function initPortfolio() {
	$('#dnn_ctr630_ContentPane').parent().hide();
	var xml = $('#dnn_ctr762_ViewOnroerendGoed_HiddenXmlCheck').attr("value");
	var edit = $('#dnn_ctr762_ViewOnroerendGoed_HiddenEditCheck').attr("value");
	if (xml == "empty" && edit == "view") {
		$('#placeHolderSales').hide();
		$('#dnn_ctr630_ContentPane').parent().show();
		$('#dnn_ctr630_ContentPane').css("height", "auto"); //400px");
		$('#dnn_ctr783_ContentPane').parent().hide();
		$('#dnn_ctr570_ContentPane').parent().hide();
		$('#dnn_ctr762_ContentPane').parent().hide();
	}
	if (xml == "filled" && edit == "view") {
		$('#placeHolderSales').hide();
		$('#dnn_ctr630_ContentPane').parent().hide();
		$('#dnn_ctr783_ContentPane').parent().show();
		$('#dnn_ctr570_ContentPane').parent().show();
		$('#dnn_ctr762_ContentPane').parent().show();
	}
	else if (edit == "edit") {
		$('#placeHolderSales').hide();
		$('#dnn_ctr630_ContentPane').parent().show();
		$('#dnn_ctr783_ContentPane').parent().show();
		$('#dnn_ctr570_ContentPane').parent().show();
		$('#dnn_ctr762_ContentPane').parent().show();
	}
}

