// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// JS FUNCTION TO SLIDE MAGAZINES (HOMEPAGE)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function slideMagazines(direction) {
	
	// SLIDE SETTINGS
	var distance 	= 868;
	var speed		= 1000;
	var magwidth	= 217;
	var totalwidth	= false;
	var position	= 0;
	
	$('.mag-holder').each( function() { totalwidth += magwidth; });
	
	var maxleft		= (0-totalwidth)+distance;
	var maxright	= 0;
	
	$('#mag-holder-next').animate({opacity: 1.0, left: '-=0px'}, 200);
	$('#mag-holder-prev').animate({opacity: 1.0, left: '+=0px'}, 200);
	
	// SLIDE ACTIONS (PREV / NEXT)
	if (direction == "prev") {
		
		// Disable buttons until animation is completed
		$('#mag-holder-prev').unbind('click');
		$('#mag-holder-next').unbind('click');
		
		// Animate container
		$('#container-mag-holder').animate( {left: '+='+distance+'px'}, speed, 'easeOutExpo', function() {
	
			// If container animation reached the end
			if ($('#container-mag-holder').position().left == maxleft)  { 
				$('#mag-holder-next').animate({opacity: 0.0}, 200);
			} else {
				$('#mag-holder-next').click( function() { slideMagazines('next'); $(this).animate({opacity: 1.0}, 200); });
			}
			
			if ($('#container-mag-holder').position().left == maxright) { 
				$('#mag-holder-prev').animate({opacity: 0.0}, 200); 
			} else {
				$('#mag-holder-prev').click( function() { slideMagazines('prev'); $(this).animate({opacity: 1.0}, 200); });
			}
			
		});
				
	} else {
		
		// Disable buttons until animation is completed
		$('#mag-holder-prev').unbind('click');
		$('#mag-holder-next').unbind('click');
		
		// Animate container to the LEFT
		$('#container-mag-holder').animate( {left: '-='+distance+'px'}, speed, 'easeOutExpo', function() { 
																				   
			// If container reached the end, disable NEXT button
			if ($('#container-mag-holder').position().left == maxleft)  { 
				$('#mag-holder-next').animate({opacity: 0.0}, 200);
			} else {
				$('#mag-holder-next').click( function() { slideMagazines('next'); $(this).animate({opacity: 1.0}, 200); });
			}
			
			if ($('#container-mag-holder').position().left == maxright) {
				$('#mag-holder-prev').animate({opacity: 0.0}, 200);
			} else {
				$('#mag-holder-prev').click( function() { slideMagazines('prev'); $(this).animate({opacity: 1.0}, 200); });
			}
			
		});
		
	}
		
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// JS FUNCTION TO LOAD MAGAZINES (HOMEPAGE)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function loadMagazines() {
	
	$('.mag-holder').each( function(i) {
		var id = (i+1);
		setTimeout("$('#mag-'+"+id+").fadeIn('normal');", (250*id));
	});
	
	$('#mag-holder-next').fadeIn("slow");
	$('#mag-holder-prev').fadeIn("fast");
	
	// Disable PREV button onload
	$('#mag-holder-prev').animate({opacity: 0.0}, 100);
	$('#mag-holder-prev').unbind('click');
	
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// JS FUNCTION TO LOAD MAGAZINE (LIBRARY)
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function loadPublication(id,source) {
	
	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
	// IF USER SELECTED A PUBLICATION FROM THE LIBRARY
	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
	if (source == "library") { 
	
		// REMOVE CURRENT PUBLICATION AND RECREATE ISSUU-HOLDER DIV
		$('#container-mag-frame object').remove();
		$('#container-mag-frame').append("<div id='issuu-holder'></div>");
		
		// CALCULATE SCREENHEIGHT
		var screenHeightClosed	= parseFloat($('#container-bibliotheek').height()-185);
		
		var attributes 			= { id: 'issuuViewer' }; 
		
		var params 				= {};
			 params.fullscreen 	= "false";
			 params.menu	   	= "false";
			 params.wmode	   	= "transparent";
		
		var flashvars 			= { jsAPIClientDomain: 'www.jan-eringa.nl', 
									mode: 'embed', 
									layout: 'http://janeringa.jellewielsma.nl/assets/issuu/eringa-plain/layout.xml', 
									showFlipBtn: 'false', 
									documentId: id, 
									docName: 'agent2issue4', 
									username: 'janeringa', 
									loadingInfoText: 'Bezig met laden van publicatie', 
									et: '1251461518896', 
									er: '26' };
	
		swfobject.embedSWF("http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf", 
						   "issuu-holder", "100%", 
						   screenHeightClosed, "9.0.0","swfobject/expressInstall.swf", 
						   flashvars, params, attributes);
		
		$('#libraryToggle').trigger('click');
		
	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 
	// IF PUBLICATION WAS SELECTED THROUGH THE QUERYSTRING (GET)
	// = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = 		
	} else { 
					
		// CALCULATE SCREENHEIGHT
		var screenHeightClosed 	= parseFloat($('#container-bibliotheek').height()-185);
		
		
		var attributes 			= { id: 'issuuViewer' }; 
		
		var params 				= {};
			 params.fullscreen 	= "false";
			 params.menu	   	= "false";
			 params.wmode	   	= "transparent";
		
		var flashvars 			= { jsAPIClientDomain: 'www.jan-eringa.nl', 
									mode: 'embed', 
									layout: 'http://janeringa.jellewielsma.nl/assets/issuu/eringa-plain/layout.xml', 
									showFlipBtn: 'false', 
									documentId: id, 
									docName: 'agent2issue4', 
									username: 'janeringa', 
									loadingInfoText: 'Bezig met laden van publicatie', 
									et: '1251461518896', 
									er: '26' };
	
		swfobject.embedSWF("http://static.issuu.com/webembed/viewers/style1/v1/IssuuViewer.swf", 
						   "issuu-holder", "100%", 
						   screenHeightClosed, "9.0.0","swfobject/expressInstall.swf", 
						   flashvars, params, attributes);

	}	
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// JS FUNCTION TO FIND SPECIFIC OBJECT WITHOUT USING JQUERY
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function findObj(n, d) { //v4.0
	var p,i,x; if(!d) d=document;
	if((p=n.indexOf("?"))>0&&parent.frames.length) {
	d=parent.frames[n.substring(p+1)].document; n=n.substring(0,p);}
	if(!(x=d[n])&&d.all) x=d.all[n]; for (i=0;!x&&i<d.forms.length;i++)
	x=d.forms[i][n];
	for(i=0;!x&&d.layers&&i<d.layers.length;i++)
	x=findObj(n,d.layers[i].document);
	if(!x && document.getElementById) x=document.getElementById(n); return x;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// JS FUNCTION TO GET BROWSER TYPE, PLATFORM AND VERSION
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
function getInternetExplorerVersion() {
  var rv = -1; // Return value assumes failure.
  if (navigator.appName == 'Microsoft Internet Explorer') {
    var ua = navigator.userAgent;
    var re  = new RegExp("MSIE ([0-9]{1,}[\.0-9]{0,})");
    if (re.exec(ua) != null)
      rv = parseFloat( RegExp.$1 );
  }
  return rv;
}


// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
// JS FUNCTIONS FOR FORM VALIDATION AND MAIL PROCESSING
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - //
var attachmentSelected = false;

function attachAttachment() { attachmentSelected = true; }


function sendContactForm() {
	// Send data to mailer script (updater)
	$.post("/api/php/updaters/send_mime_mail.php", $('#formID').serialize(),
	function(data){ 
		if (data == "1") { mailSuccessFunction(data); } else { mailErrorFunction(data); }
	});
	
	return false;
}

function sendSignupForm() {
	// Send data to mailer script (updater)
	$.post("/api/php/updaters/add_member.php", $('#formID').serialize(),
	function(data){ 
		if (data == "1") { signupSuccessFunction(data); } else { mailErrorFunction(data); }
	});
	
	return false;
}

function callSuccessFunction() {
	// Upload file (if found) when form is validated
	if (attachmentSelected == true) {
		$('#attachment').uploadifyUpload();
		return false;
	} else {
		sendMailFunction(false);
		return false;
	}
}

function sendMailFunction(data) {
	// Set data file
	if (data) {
		$('#attachmentHidden').val(data.filePath);
	}
	
	// Send data to mailer script (updater)
	$.post("/api/php/updaters/send_mime_mail.php", $('#formID').serialize(),
	function(data){ 
		if (data == "1") { mailSuccessFunction(data); } else { mailErrorFunction(data); }
	});
	
	return false;
}


function signupSuccessFunction() {
	$.modal("<br/><strong>Hartelijk dank voor uw aanmelding</strong><br/><br/>Uw gegevens zijn succesvol opgeslagen in onze databank. U zult vanaf nu op de hoogte worden gehouden van nieuwe activiteiten, aanbiedingen en andere interessante onderwerpen van Jan Eringa.", { 
	containerCss: { fontSize: 13, height: 150,width: 380,backgroundColor: '#b3b1aa',color: '#000000',border: '7px solid #585753',overflow: 'hidden'}, opacity: 85, close: true 
	});	
}


function mailSuccessFunction() {
	$.modal("<br/><strong>Hartelijk dank voor uw reactie</strong><br/><br/>Uw bericht is succesvol verzonden. Indien nodig zal een van onze medewerkers z.s.m. contact met u opnemen.", { 
	containerCss: { fontSize: 13, height: 150,width: 380,backgroundColor: '#b3b1aa',color: '#000000',border: '7px solid #585753',overflow: 'hidden'}, opacity: 85, close: true 
	});	
}

function mailErrorFunction(data) {
	$.modal("Er is een fout opgetreden bij het versturen van het contactformulier: " + data, { 
		containerCss: {	fontSize: 13, height: 150,width: 380,backgroundColor: '#fff',color: '#333333',border: '7px solid #ccc',overflow: 'hidden'}, opacity: 85, close: true 
	});	
}
