
/**** General popup functions **************************************************/

//0 means disabled; 1 means enabled;
var popupStatus = 0;
var nextAction = '';
var nextEval = '';

function getPageSize() {
	var xScroll, yScroll;
	if (window.innerHeight && window.scrollMaxY) {	
		xScroll = window.innerWidth + window.scrollMaxX;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}
	
	var windowWidth, windowHeight;
	
	if (self.innerHeight) {	// all except Explorer
		if(document.documentElement.clientWidth){
			windowWidth = document.documentElement.clientWidth; 
		} else {
			windowWidth = self.innerWidth;
		}
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}	
	
	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else { 
		pageHeight = yScroll;
	}
	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){	
		pageWidth = xScroll;		
	} else {
		pageWidth = windowWidth;
	}
	
	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight);
	return arrayPageSize;
}

function loadBackground() {
	if ($("#backgroundPopup").css('display') != 'block') {
		resizeBackground();
		
		$("#backgroundPopup").css('opacity', '0.7');
		$("#backgroundPopup").fadeIn('normal');
	}
}

function resizeBackground() {
	var psize = getPageSize();
	
	$("#backgroundPopup").css({
		"width": psize[0],
		"height": psize[1],
		"opacity": "0.7"
	});
}


//loading popup with jQuery magic!
function loadPopup(popupId)
{
	loadBackground();
	$('#' + popupId).fadeIn('normal');
	popupStatus = 1;
}

//disabling popup with jQuery magic!
function disablePopup(popupId,fadeBg)
{
    if ($('#' + popupId).css('display') != 'none')
    {
    	$('#onload-img').fadeOut('fast');
    	$("#" + popupId).stop(true, true);
        $("#" + popupId).fadeOut('normal');
        
        if (fadeBg) {
    		$("#backgroundPopup").fadeOut("normal");
    	}
    	
    	popupStatus = 0;
    }
}

function switchPopup(oldPopupId, newPopupId){
	$('#' + oldPopupId).fadeOut('fast', function() {
		$('#' + newPopupId).fadeIn('fast');
		centerPopup(newPopupId, 0);
	});
}


//centering popup
function centerPopup(popupId, animate)
{
	var windowWidth  = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight  = $('#' + popupId).height();
	var popupWidth   = $('#' + popupId).width();
	var scrollTop    = $(document).scrollTop();
	
	var popupTop     = scrollTop+windowHeight / 2 - popupHeight / 2;
	var popupLeft    = windowWidth / 2 - popupWidth / 2;
	
	if (popupTop < 5) popupTop = 5;
	if (popupLeft < 5) popupLeft = 5;
	
	if (animate == undefined || animate == 0) {
    	$('#' + popupId).css({
    		'position' : 'absolute',
    		'top'      : popupTop,
    		'left'     : popupLeft
    	});
	} else {
	    $('#' + popupId).animate({
    		'top'      : popupTop,
    		'left'     : popupLeft
    	}, 200);
	}
}


function centerAllPopups() {
    $('.popup').each(function() {
        if ($(this).css('display') != 'none') {
            centerPopup($(this).attr('id'), 0);
        }
    });
}

/**** End general popup functions **********************************************/




function showLogin(action)
{
	loadSaveNamePopup = false;
	nextAction = action;
    centerPopup('login-p-popup');
    loadPopup('login-p-popup');
}


function loadPreview(img)
{
    switchPreviewSideContents(1);
    
	$('#preview-img').attr('src', img);
	showOnload('preview-img', 'openPreview()');
}


function resetPreviewWidth() 
{
    if ($('#preview-img').width() == 0) {
        setTimeout("resetPreviewWidth()", 100);
    } else {
        var width    = $('#preview-img').attr('width') + 
                    parseInt($('#preview-img').parent().css('padding-left')) + 
                    parseInt($('#preview-img').parent().css('padding-right')) + 2;


        if (width < 265) width = 265;
        
        $('#preview-card-popup').css('width', width);
        centerPopup('preview-card-popup', 1);
    }
}


function openPreview()
{
    resetPreviewWidth();
    
	$('#onload-img').fadeOut('fast');
	centerPopup('preview-card-popup');
    loadPopup('preview-card-popup');
}


function switchPreviewSideContents(side_new)
{
    if (side_new == 1) {
	    var side_old       = 2;
	    var active_side    = 'Buitenzijde';
	    var next_side      = 'binnenzijde';
	} else {
	    var side_old       = 1;
	    var active_side    = 'Binnenzijde';
	    var next_side      = 'buitenzijde';
	}
	
	$('#preview-side').html($('#preview-side').html().replace('switchPreviewSide(' + side_new + ')', 'switchPreviewSide(' + side_old + ')'));
	$('#preview-side .active_side').text(active_side);
	$('#preview-side .next_side').text(next_side);
}


function switchPreviewSide(side_new)
{
    switchPreviewSideContents(side_new);
    
    if (side_new == 1) {
	    var side_old       = 2;
	} else {
	    var side_old       = 1;
	}
    
	$('#preview-img').attr('src', $('#preview-img').attr('src').replace('_0' + side_old + '.jpg', '_0' + side_new + '.jpg'));
    $('#side-loader').fadeIn('fast');
}


function showOnload(img_id, callback)
{
	var windowWidth  = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight  = $("#onload-img").height();
	var popupWidth   = $("#onload-img").width();
	var scrollTop    = $(document).scrollTop();
	
	//centering
	$("#onload-img").css({
		"position": "absolute",
		"top": scrollTop+windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});
	
	loadBackground();
	$('#onload-img').fadeIn('slow');
	$('#' + img_id).load(function() {
		loadBackground();
		eval(callback);
	});
}

function loadTmpPreview(path) {
	var windowWidth  = document.documentElement.clientWidth;
	var windowHeight = document.documentElement.clientHeight;
	var popupHeight  = $("#onload-img").height();
	var popupWidth   = $("#onload-img").width();
	var scrollTop    = $(document).scrollTop();
	
	//centering
	$("#onload-img").css({
		"position": "absolute",
		"top": scrollTop+windowHeight/2-popupHeight/2,
		"left": windowWidth/2-popupWidth/2
	});

	loadBackground();
	$('#onload-img').fadeIn('slow');
	
	$.ajax({
		type: "GET",
		url: "ajax/create_tmp_preview.php",
		success: function(msg) {
			$('#preview-img').attr('src', path + '/page_small_01.jpg?nocache=' + Math.round( Math.random() * 100 ));
			switchPreviewSideContents(1);
//			$('#preview-side').html('<strong>Buitenzijde</strong> - bekijk de <a href="#" onclick="switchPreviewSide(2); return false;">binnenzijde</a>');
		}
	});
}

function openTmpPreview() {
    // in IE is deze bij eerste keer laden 0!!
    if($('#preview-img').attr('width') != 0) {
	   //$('#preview-tmp-card-popup').css('width', $('#preview-img').attr('width'));
	   $('#side-loader').fadeOut('fast');
    }
	centerPopup('preview-tmp-card-popup');
	loadPopup('preview-tmp-card-popup');
}


$(document).ready(function() {
		
    $(window).scroll(function() {
        centerAllPopups();
    });
    
    $(window).resize(function() {
        centerAllPopups();
    });
    
	//LOADING POPUP
	//Click the button event!
		$("#eigen-varianten, #product-selectie, #card-name").click(function() {
			centerPopup($(this).attr('id') + '-popup');
			loadPopup($(this).attr('id') + '-popup');
		});
		
		
//		$("#product-selectie").click(function() {
//			centerPopup('product-selectie-popup');
//			loadPopup('product-selectie-popup');
//		});

//		$("#card-name").click(function() {
//			centerPopup('card-name-popup');
//			loadPopup('card-name-popup');
//		});
		
		$(".product-preview").click(function() {
			productId = this.id.substring(13);
			$('#product-preview-img').attr('src', '/pdf_assets/pdf/products/'+productId+'/pdf_3.png');
			
			centerPopup('product-preview-popup');
			loadPopup('product-preview-popup');
		});
		
		$(".product-preview-selectie").click(function() {
			productId = this.id.substring(13);
			$('#product-preview-selectie-img').attr('src', '/pdf_assets/pdf/products/'+productId+'/pdf_3.png');
			
			switchPopup('product-selectie-popup', 'product-preview-selectie-popup');			
		});
		
		$("#login-p-save").click(function() {
		    if ($('#login').text().indexOf('ingelogd als') > -1) {
    			centerPopup("card-name-popup");
    			loadPopup("card-name-popup");
		    } else {
    			centerPopup('login-p-popup');
    			loadPopup('login-p-popup');
    			    			
    			loadSaveNamePopup = true;
		    }
		});
		
		$("#login-p-specials-save").click(function() {
			nextEval = "$('#card_approve_form').submit();";
			
			centerPopup('login-p-popup');
			loadPopup('login-p-popup');
		});
		
		$(".special").click(function() {
			centerPopup('kaart-type-popup');
			loadPopup('kaart-type-popup');
		});
		
	
    	//CLOSING POPUP
    	//Click the x event!
    	
        $('.popup-closebar img[@src$=close-button.gif]').click(function() {
            var popup_id = $(this).parents('.popup').attr('id');
            
            switch(popup_id)
            {
                default:
                    disablePopup(popup_id, 1);
                    break;
                
                case 'product-preview-selectie-popup':
                    switchPopup(popup_id, 'product-selectie-popup');
                    break;
            }
        });

	
	//Click out event!
		$("#backgroundPopup").click(function() {
		    $('.popup').each(function() {
                disablePopup($(this).attr('id'), 1);
		    });
		});

});





