/**
 * A&M ImpacT Internetdiensten BV
 * Foto player
 */
function am_player()
{
	// vars met instellingen van de player
	this.site = '';
	this.logo = '';
	this.title = '';

	this.reageer_url = '';
	this.reageer_onclick = '';
	
	this.reacties_url = '';
	this.reacties_aantal = 0;

	this.doorsturen_url = '';
	this.doorsturen_onclick = '';
	
	this.stuur_uw_foto_url = '';
	this.stuur_uw_foto_click = '';
	
	this.max_width = 620;
	this.max_height = 465;

	// referentie naar this
	var self = this;
	
	// als jQuery in noconflict draait of er al een library gebruikt maakt van $ deze even intern omzetten
	var $ = window.jQuery;
	
	// data collectie
	var data = {
		photo		: [],
		advertorial	: [],
		related		: []
	};
	
	// huidige foto
	var currentPhoto 	= 0;
	
		
	/**
	 * buildHTML
	 * @return	void
	 */
	this.buildHTML = function()
	{
		html = '<div id="foto_bg" style="display:none;"></div>';
		html += '<div id="foto_container" style="display:none;"><div id="fotogalerie">';
		html += '<div id="topbalk"><h1>TITEL</h1><img class="logo" src="" height="33" alt="Logo" /><a class="sluit" href="#" title="Sluit pagina">Sluit</a></div>';
		html += '<div id="slideshow">';
		html += '<div id="groot"><div><table cellpadding="0" cellspacing="0" border="0"><tr><td align="center" valign="middle"><img src="" alt="" /></td></tr></table>';
		html += '<a class="prev" href="#" title="Vorige">Vorige</a>';
		html += '<a class="next" href="#" title="Volgende">Volgende</a></div></div>';
		html += '<p class="aantal"><a class="first" href="#" title="Eerste afbeelding">&laquo;</a> <a class="prev" href="#" title="Vorige afbeelding">vorige</a>';
		html += ' <span>0 van de 0</span> ';
		html += '<a class="next" href="#" title="Volgende afbeelding">volgende</a> <a class="last" href="#" title="Laatste afbeelding">&raquo;</a></p>';
		html += '<p class="omschrijving"></p></div>';
		html += '<div id="rechterkolom"><ul id="advertorials"></ul>';
		html += '<ul id="buttons">';
		html += '<li><a class="reageren" href="#" title="Reageren">Reageren</a></li>';
		html += '<li><a class="doorsturen" href="#" title="Doorsturen">Doorsturen</a></li>';
		html += '<li><a class="reacties" href="#" title="Lees reacties (288)">Reacties (288)</a></li>';
		html += '<li><a class="stuur-foto" href="#" title="Stuur ons uw foto">Stuur uw foto</a></li></ul>';
		html += '<h2>Gerelateerd</h2><div id="gerelateerd"><ul></ul></div></div></div></div>';
		
		$("body").append(html);
	}

	/**
	 * buildBackground
	 * @return	void
	 */
	this.buildBackground = function()
	{
		if($.browser.msie && $.browser.version == 6)	{
			$(function(){
				$("#foto_bg").height( $("body").height() );
			});
		}
	}

	
	// het spul klaar zetten
	if(document.getElementById("fotogalerie") === null)	{
		this.buildHTML();
		this.buildBackground();
	}
	
	
	/**
	 * add
	 * @param	string	type	photo, advertorial, related
	 * @param	mixed	a	
	 * @param	mixed	b	
	 * @param	mixed	c	
	 * @return	void
	 */
	this.add = function( type, a, b, c )
	{
		switch(type)
		{
			case 'related':
				data[type].push({
					url		: a,
					text	: b
				});
				break;
				
			case 'photo':
				data[type].push({
					url		: a,
					text	: b,
					icon	: c
				});
				break;
			
			case 'advertorial':
				data[type].push({
					element	: a,
					width	: parseInt(b),
					height	: parseInt(c)
				});
				break;
		}
	}	
	
	
	/**
	 * showPhoto
	 * @param	int		index
	 * @return	void
	 */
	this.showPhoto = function( index )
	{
		var preloader = new Image();
		preloader.onload = function()	
		{
			if(preloader.width > self.max_width || preloader.height > self.max_height)
			{
				// Als foto breder is dan max breedte, maar hoogte minder is dan max hoogte
				if(preloader.width > self.max_width && preloader.height <= self.max_height)
				{
					width_ratio = self.max_width / preloader.width;
					height_ratio = width_ratio;
				}
				// Als foto hoger is dan max hoogte, maar breedte minder is dan max breedte
				else if(preloader.height > self.max_height && preloader.width <= self.max_width)
				{
					height_ratio =  self.max_height / preloader.height;
					width_ratio = height_ratio;
				}
				// alle andere gevallen
				else
				{
					width_ratio = self.max_width / preloader.width;
					height_ratio =  self.max_height / preloader.height;
					
					// Als verhoudingverschil breedte hoger is dan verhouding hoogte dan breedteverhouding aanhouden voor beide dimensies.
					if(width_ratio > height_ratio)
					{
						width_ratio = height_ratio;	
					}
					// Als verhoudingverschil hoogte hoger is dan verhouding breedte dan hoogteverhouding aanhouden voor beide dimensies.
					else if(height_ratio > width_ratio)
					{
						height_ratio = width_ratio;	
					}
					else
					{
						//niks
					}
				}
				
				preloader.width *= width_ratio;
				preloader.height *= height_ratio;
				
			} 
			
			
			$("#slideshow #groot img").attr({ src: preloader.src, width: preloader.width, height: preloader.height });
			
			try 
			{
				dcsMultiTrack('DCS.dcsuri', data.photo[index].url, 'WT.ti', data.photo[index].url);
			} 
			catch(e)
			{ }
		}
		preloader.src = data.photo[index].url;

		$("#slideshow p.omschrijving").html( data.photo[index].text );
		//$("#slideshow p.omschrijving a").attr("target", "_blank");
		
		if(data.photo[index].icon)	{
			$("#slideshow p.omschrijving").append(" <img src=\""+ data.photo[index].icon +"\" />");
		}
		
		$("#slideshow p.aantal span").html("Afbeelding "+ (index+1) +" van "+ data.photo.length);
	}
	
	
	/**
	 * navigatePhoto
	 * @param	string	nav		next / prev / first / last
	 * @return	void
	 */
	this.navigatePhoto = function( nav )
	{
		// vorige
		switch(nav)
		{
			case 'prev':
				if(currentPhoto > 0) 
					currentPhoto--; 
				break;
			
			case 'first':
				currentPhoto = 0;
				break;
			
			case 'next':
				if(currentPhoto + 1 < data.photo.length) 
					currentPhoto++; 
				break;
			
			case 'last':
				currentPhoto = data.photo.length - 1; 
				break;
		}
		
		// vorige volgende links toggle
		self.toggleNavigation();
		
		// foto tonen
		self.showPhoto( currentPhoto );
	}
	
	
	/**
	 * toggleNavigation
	 */
	this.toggleNavigation = function()
	{
		// vorige en volgende links bijwerken
		// volgende links verbergen	
		if(currentPhoto >= data.photo.length - 1) 
			$("#slideshow a.next, #slideshow a.last").css("visibility", "hidden");
		else
			$("#slideshow a.next, #slideshow a.last").css("visibility", "visible");
			
		// vorige links verbergen
		if(currentPhoto <= 0) 
			$("#slideshow a.prev, #slideshow a.first").css("visibility", "hidden");
		else
			$("#slideshow a.prev, #slideshow a.first").css("visibility", "visible");
			
		// vorige en volgende links tonen
		//if((currentPhoto < data.photo.length - 1) && (currentPhoto > 0)) 
		//	$("#slideshow a.prev, #slideshow a.first, #slideshow a.next, #slideshow a.last").css("visibility", "visible");	
	}
		
	
	/**
	 * hide
	 * @return	void
	 */
	this.hide = function()
	{		
		// voor ie6 selectboxen weer tonen
		if($.browser.msie && $.browser.version == 6)	{
			$("select").css("visibility", "visible");
		}

		$("#foto_container").fadeOut(300);
		$("#foto_bg").fadeOut(300);
	}
	
	
	/**
	 * show
	 * @return	void
	 */
	this.show = function()
	{
		// voor ie6 selectboxen verbergen
		if($.browser.msie && $.browser.version == 6)	{
			$("select").css("visibility", "hidden");
		}
		
		// overlay verbergen
		$("#foto_container").hide();

		// foto navigatie en sluiten instellen		
		self.toggleNavigation();

		$("#slideshow a.next").unbind("click").click(function() 	{ self.navigatePhoto('next'); 	return false; });
		$("#slideshow a.prev").unbind("click").click(function() 	{ self.navigatePhoto('prev'); 	return false; });
		$("#slideshow a.first").unbind("click").click(function() 	{ self.navigatePhoto('first'); 	return false; });
		$("#slideshow a.last").unbind("click").click(function() 	{ self.navigatePhoto('last'); 	return false; });
	
		// toetsenbord navigatie
		$("body").unbind("keydown").keydown(function(e) { 			
			switch(e.keyCode)
			{
				case 37:	// pijl links
					self.navigatePhoto('prev');
					break;
				
				case 39:	// pijl rechts
					self.navigatePhoto('next');
					break;
					
				case 27:	// escape
					self.hide();
					break;
			}
		});

	
		$("#fotogalerie a.sluit").unbind("click").click(function() { 
			self.hide();
			return false; 
		});
		
		$("#fotogalerie").unbind("click").click(function(e) { e.stopPropagation(); });
		
		// specifieke inhoud voor deze player instellen
		$("#fotogalerie").attr("class", self.site);
		$("#fotogalerie img.logo").attr("src", self.logo);

		// if both are empty: don't show the button, ie: $("#fotogalerie a.reageren").attr("style", "visibility: hidden;");
		$("#fotogalerie a.reageren").attr("href", self.reageer_url);
		$("#fotogalerie a.reageren").attr("onClick", self.reageer_onclick);
		
		$("#fotogalerie a.reacties").attr({ href: self.reacties_url, title: "Reacties ("+ self.reacties_aantal +")" }).text("Reacties ("+ self.reacties_aantal +")");

		$("#fotogalerie a.doorsturen").attr("href", self.doorsturen_url);
		$("#fotogalerie a.doorsturen").attr("onClick", self.doorsturen_onclick);

		$("#fotogalerie a.stuur-foto").attr("href", self.stuur_uw_foto_url);
		$("#fotogalerie a.stuur-foto").attr("onClick", self.stuur_uw_foto_onclick);
	
		$("#fotogalerie h1:first").text(self.title);

		// advertorials vullen
		$("#fotogalerie #advertorials").empty();
		$(data.advertorial).each(function(index)	
		{
			try	{
				// ads invoegen dmv een iframe ivm document.write in de banner codes...
				var iframe = document.createElement("iframe");
				iframe.setAttribute("name", "iframe_"+ Math.random() * Math.random());
				iframe.setAttribute("frameborder", 0);
				iframe.setAttribute("scrolling", 'no');
				iframe.setAttribute("width", 	this.width);
				// iframe.setAttribute("height", 	this.height);
				iframe.setAttribute("height", 	200);
	
				var li = document.createElement("li");
				li.appendChild(iframe);
				
				$("#fotogalerie #advertorials")[0].appendChild(li);
				
				var doc = null;  
				if(iframe.contentDocument)  // Firefox, Opera  
					doc = iframe.contentDocument;  
				else if(iframe.contentWindow)  // Internet Explorer  
					doc = iframe.contentWindow.document;  
				else if(iframe.document)  // andere?  
					doc = iframe.document;  
				
				doc.open();
				doc.write("<html><head><style>img,body{margin:0;border:none;}</style></head><body><div>"+ $(this.element).html() +"</div></body></html>");
				doc.close();
			}
			catch(e) { }
		});
		
		
		// related links vullen
		$("#fotogalerie #gerelateerd ul").empty();
		$(data.related).each(function()	{
			$("#fotogalerie #gerelateerd ul").append('<li><a href="'+ this.url +'">'+ this.text +'</a></li>');
		});
	
	
		// eerste/huidige foto tonen
		self.showPhoto( currentPhoto );
	
		// alles is klaar, overlay tonen
		jQuery.each(jQuery.browser, function(i, val) {
			if($.browser.msie && $.browser.version == 6)
			{
				windowHeight = document.documentElement.clientHeight;
				$("#foto_bg").css({ opacity: 0, display: 'block', position: 'absolute', height: windowHeight }).fadeTo(300, 0.7);
				
				$(window).scroll(function () { 
				  	yScroll = document.documentElement.scrollTop;
					$("#foto_bg").css({ top: yScroll });
				});
			}
			else
			{
				$("#foto_bg").css({ opacity: 0, display: 'block' }).fadeTo(300, 0.7);
			}
		});	
		$("#foto_container").css("top", $(window).scrollTop()+10 ).fadeIn(300);
		
		
	}
	
	
};

$(function()
{
	
});


