var intervalID = 0;
var intervalID2 = 0;
var popup = false;
var fadeActive = new Array();

$(function() {
	/** menu fix om zelfde hoogte in IE te hebben als de rest **/
	if($.browser.msie) {
		$('#menu').css('bottom', '-5px');
		if($.browser.version <= 7) {
			var contentHeight = (($('.left').outerHeight() > $('.right').outerHeight()) ? $('.left').outerHeight() : $('.right').outerHeight()) + 'px';
			$('content').css('height', contentHeight);
		}
	}

	/** product page **/
	$('.divProducts .product .image').hover(function () {
		$(this).children('div.text').animate({'bottom':'0px'}, 'fast');
	}, function () {
		$(this).children('div.text').animate({'bottom':'-150px'}, 'fast');
	});
	
	/** popup **/
	$("#showPopup").click(function(){
		centerPopup();
		loadPopup('/login.php');
	});
	$("#closePopup").click(function(){  
		disablePopup();  
	});	
	$("#backgroundPopup").click(function(){  
		disablePopup();  
	});	
	$(document).keypress(function(e){
		if(e.keyCode == 27 & popupStatus == 1){
			disablePopup();
		}
	});
		
	intervalID  = setInterval( "slideRight('divscroll')", 3000);
	slideRight('divscroll');
	fadeActive['divscroll'] = 0;
	intervalID2 = setInterval( "slideRight('divscroll2')", 3000);
	slideRight('divscroll2');
	fadeActive['divscroll2'] = 0;
	/**
	$('#divscroll').hover(function(){
		clearInterval(intervalID);
	},function(){
		intervalID  = setInterval( "slideRight('divscroll')", 3000);
	});
	
	$('#divscroll #next').click(function(){
		slideRight('divscroll');
	});
	$('#divscroll #prev').click(function(){
		slideLeft('divscroll');
	});
	
	$('#divscroll2').hover(function(){
		clearInterval(intervalID2);
	},function(){
		intervalID2 = setInterval( "slideRight('divscroll2')", 3000);
	});
	
	$('#divscroll2 #next').click(function(){
		slideRight('divscroll2');
	});
	$('#divscroll2 #prev').click(function(){
		slideLeft('divscroll2');
	});
	**/
	if(popup == true) {
		centerPopup();
		loadPopup('/login.php');
	}

	$('#custom_made').click(function (){
		$("#popup").css('height', '600px').css('width', '1000px').css('background', '#FFF');
		centerPopup(600);
		loadPopup('/popupCatalog.php');
		return false;
	});
	
	$('#routeform').submit(function(e) {
		$('#gmap').animate({width:'352px',height:'500px'}, 500, function () {
		if (GBrowserIsCompatible()) {
				$(window).unload(GUnload);
				var map = new GMap2($('#gmap')[0]);
				map.addControl(new GLargeMapControl3D());
				map.addControl(new GMapTypeControl());
				map.enableScrollWheelZoom();
				var directions = new GDirections(map, $('#gdirections')[0]);
				directions.load('from: ' 
					+ e.target.routefrom.value.replace(/(\d{4})\W+([A-Za-z]{2})/, "$1$2")
					+ ' to: PROMACTION@52.50880,4.78439', {locale: 'nl_NL'});
				GEvent.addListener(directions, 'addoverlay', function() {
					var last = directions.getMarker(directions.getNumRoutes());
				});
				$('#gmap').css('border', '1px solid silver').delay(2000).css('background', '#EEE');
			}
		});
		return false;
	});
});
function slideRight(id) {
	if(fadeActive[id] == 0) {
		fadeActive[id] = 1;
		var $active = $('#'+id+' #divbg div.active');

		if ( $active.length == 0 ) $active = $('#'+id+' #divbg div:last');

		// use this to pull the divs in the order they appear in the markup
		var $next =  $active.next().length ? $active.next()
			: $('#'+id+' #divbg div:first');

		$active.addClass('last-active');

		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 500, function() {
				$active.delay(100).removeClass('active last-active');
			});
	}
	fadeActive[id] = 0;
}
/**
function slideLeft(id) {
	if(fadeActive[id] == 0) {
		fadeActive[id] = 1;
		var $active = $('#'+id+' #divbg div.active');

		if ( $active.length == 0 ) $active = $('#'+id+' #divbg div:last');

		// use this to pull the divs in the order they appear in the markup
		var $next = $active.prev().length ? $active.prev()
			: $('#'+id+' #divbg div:last');

		$active.addClass('last-active');

		$next.css({opacity: 0.0})
			.addClass('active')
			.animate({opacity: 1.0}, 500, function() {
				$active.delay(100).removeClass('active last-active');
			});
	}
	fadeActive[id] = 0;
}
**/

/** popup **/
var popupStatus = 0; 

function centerPopup(height){
	if(height == undefined) {
		height = 450;
	}
	var windowWidth = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupWidth = $("#popup").outerWidth();
	$("#popup").css({
		"position": "fixed",
		"top": windowHeight/2-(height)/2,
		"left": windowWidth/2-popupWidth/2
	});
}
function loadPopup(url){
	if(url != undefined) {
		$.ajax({
			url: url,
			success: function(data) {
				$('#popupInner').html(data);
				if(popupStatus==0){
					$("#backgroundPopup").css({
						"opacity": "0.7"
					});
					$("#backgroundPopup").fadeIn("fast");
					$("#popup").fadeIn("fast");
					popupStatus = 1;
				}
			}
		});
		return false;
	} else {
		if(popupStatus==0){
			$("#backgroundPopup").css({"opacity": "0.75"});
			$("#backgroundPopup").fadeIn("fast");
			$("#popup").fadeIn("fast");
			popupStatus = 1;
		}
	}
}
function disablePopup(){
	if(popupStatus==1){  
		$("#backgroundPopup").css("display", 'none');
		$("#popup").css("display", 'none');
		popupStatus = 0;  
	}  
}  
