/* ***************************************
 *
 * JAVASCRIPT FOR WEZEL @ THE MOVIES 2011
 * DEVELOPED BY BRAM VERDYCK (@TROTI13)
 *
*************************************** */

var WAM = (function(window, document, $, google, undefined) {
	var settings, private, public;
	
	settings = {
		tickets: {
			$container:	$('#navigation .nav'),
			$el:		$('.ticket-container'),
			className:	'ticket'
		},
		polaroids: {
			$container: $('#pictures'),
			$el:		$('.polaroid-front'),
			className:	'polaroid-wrapper'
		},
		fancybox: {
			padding:			0,
			margin:				0,
			centerOnScroll:		true,
			hideOnOverlayClick:	false,
			overlayColor:		'#262626',
			overlayOpacity:		0.6,
			titlePosition:		'over'
		},
		vote: {
			id:					'vote',
			voteContainer:		'vote-container',
			movieContainer:		'movie-container',
			infoContainer:		'info-container',
			pollContainer:		'poll-container',
			top:				'vote-top',
			bottom:				'vote-bottom',
			button:				'vote-button',
			result:				'movie_result',
			width:				640,
			height:				450
		},
		charts: {
			id:					'vote-result',
			width:				500,
			height:				400
		},
		animationSpeed:			500
	};
	private = {};
	public = {};
	
	private.vars = {};
	
	private.init = function() {
		
		private.vars.token = $('body').attr('rel');
		$('body').attr('rel', '');
		
		// bind events
		private.bindEvents();
	};
	
	private.bindEvents = function() {
		$.each(settings.tickets.$el, function() {
			$(this).bind({
				click:			private.onTicketClicked,
				mouseenter:		private.onTicketOver,
				mouseleave:		private.onTicketOut
			});
		});
		
		$.each(settings.polaroids.$el, function() {
			$(this).bind({
				//click:			private.onPolaroidClicked,
				mouseenter:		private.onPolaroidOver,
				mouseleave:		private.onPolaroidOut
			});
		});
		
		var polaroidBox = settings.fancybox;
		
		$.extend(polaroidBox, {
			title:			'<a href="#" id="download-pic">Download deze foto</a>',
			onComplete:		function() {
				$('#fancybox-title-over').find('a').bind('click', function(e) {
					e.preventDefault();
					var href = $('#fancybox-img').attr('src');
					href = href.replace('medium', 'big');
					window.open(href);
				});
			}
		});
		
		settings.polaroids.$el.fancybox(polaroidBox);
	};
	
	private.onTicketClicked = function(e) {
		e.preventDefault();
		var data = {
			movie: [
				{
					name: 'Film1',
					votes: 0
				},
				{
					name: 'Film2',
					votes: 1
				},
				{
					name: 'Film3',
					votes: 2
				},
				{
					name: 'Film4',
					votes: 3
				},
				{
					name: 'Film5',
					votes: 4
				}
			]
		};
		
		var ticketsFancybox = settings.fancybox;
		$.extend(ticketsFancybox, { 
			href:				$(this).attr('href'),
			type: 				'ajax',
			autoDimensions:		false,
			height:				500,
			width:				640,
			scroll:				false,
			titleShow:			false,
			onComplete:			private.onTicketLoaded
		});
		// set plugins
		$.fancybox(ticketsFancybox);
	};
	
	private.onTicketLoaded = function(resp) {		
		$('#fancybox-content').html('');
		
		$.get(resp[0].href, function(data) {
			
		  if(data.success) {
			  var r = data.response;
			  
			  if(r.answers) {
			  
				  var post_data = {
					  question:		r.question,
					  a1:			r.answers[0].answer,
					  a2:			r.answers[1].answer,
					  a3:			r.answers[2].answer,
					  a4:			r.answers[3].answer,
					  a5:			r.answers[4].answer,
					  key1:			r.answers[0].key,
					  key2:			r.answers[1].key,
					  key3:			r.answers[2].key,
					  key4:			r.answers[3].key,
					  key5:			r.answers[4].key,
					  id1:			r.answers[0].id,
					  id2:			r.answers[1].id,
					  id3:			r.answers[2].id,
					  id4:			r.answers[3].id,
					  id5:			r.answers[4].id
				  };
				  
				  $.post('poll.php', post_data, function(response) {
					  var $container, $top, $bottom, $poll, $info, $movie;
					  
					  $('#fancybox-content').html(response);
					  
					  $container = $('#fancybox-content #vote-container');
	  
					  $poll = $('#fancybox-content #poll-container');
					  $info = $('#fancybox-content #info-container');
					  $movie = $('#fancybox-content #movie-container');
					  
					  $container.scrollTo($poll, settings.animationSpeed, { 
							offset:-40,
							onAfter: function() {
								$movie.empty();
								$info.empty();
							}
						});
					  
					  $('#' + settings.vote.button).bind('click', function(e) {
						  e.preventDefault();
						  
						  var href, data, $this, $form;
						  
						  $this = $(this);

						  poll_data = {
							  token: 		private.vars.token,
							  poll_id: 		r.id,
							  answer_id:	$poll.find(':radio:checked').attr('rel')
						  };
						  
						  if(poll_data.answer_id) {
								$.post('services/insertVote.php', poll_data, function(result) {
									
									if(result.success) {
										private.showResult(result);
									}
						  		},'json');
						  }
					  });
					  
					  $('#' + settings.vote.id + ' .movie_more').bind('click', function(e) { 
						  e.preventDefault(); 
						  
						  var p = {
							i: $(this).attr('rel')
						  };
						  
						  $.post($(this).attr('href'), p, function(response) {
							  $info.html(response);
							  
							  $container.scrollTo($info, settings.animationSpeed, { offset:-40 });
							  
							  $info.find('#back-to-vote').bind('click', function(e) { 
								  e.preventDefault(); 
								  $container.scrollTo($poll, settings.animationSpeed, { 
									  offset:-40,
									  onAfter: function() {
										  $movie.empty();
										  $info.empty();
									  }
								  });
							  });
							  
							  $info.find('#show-trailer').bind('click', function(e) {
								  e.preventDefault();
								  
								  var p = { m: $(this).attr('rel') };
								  
								  $.post($(this).attr('href'), p, function(response) {
									  $movie.html(response);
									  $container.scrollTo($movie, settings.animationSpeed, { offset:40 });
									  
									  $movie.find('#back-to-info').bind('click', function(e) {
										  e.preventDefault();
										  $container.scrollTo($info, settings.animationSpeed, { offset:-40 });
									  });
									  $movie.find('#back-to-vote-movie').bind('click', function(e) {
										  e.preventDefault();
										  
										  $container.scrollTo($poll, settings.animationSpeed * 2, { 
											  offset:-40,
											  onAfter: function() {
												  $movie.empty();
												  $info.empty();
											  }
										  });
									  });
									  
								  });
							  });
						  });
					  });
				  });
			  } else {
				  private.showResult(data);
			  }
		  }
		}, "json");
		
	};
	
	private.showResult = function(data, question) {
		var res = data.response.result;
		
		var result_data = {
			result: true,
			q: 	res.question,
			p1: ''+res.answers[0].procent,
			p2: ''+res.answers[1].procent,
			p3: ''+res.answers[2].procent,
			p4: ''+res.answers[3].procent,
			p5: ''+res.answers[4].procent,
			f1: res.answers[0].answer,
			f2: res.answers[1].answer,
			f3: res.answers[2].answer,
			f4: res.answers[3].answer,
			f5: res.answers[4].answer,
			k1:	res.answers[0].key,
			k2:	res.answers[1].key,
			k3:	res.answers[2].key,
			k4:	res.answers[3].key,
			k5:	res.answers[4].key
		};
	
		$.post('movie_info.php', result_data, function(html) {
			$('#fancybox-content').html(html);
			
			var $container, $top, $bottom, $poll, $info, $movie;
					  
			$container = $('#fancybox-content #vote-container');
	  
			$poll = $('#fancybox-content #movie-result');
			$info = $('#fancybox-content #info-container');
			$movie = $('#fancybox-content #movie-container');
			
			$('#' + settings.vote.id + ' .movie_more').bind('click', function(e) { 
				e.preventDefault(); 
				
				var post_data = {
					i: $(this).attr('rel')
				};
				
				$.post($(this).attr('href'), post_data, function(response) {
					$info.html(response);
					
					$container.scrollTo($info, settings.animationSpeed, { offset:-40 });
					
					$info.find('#back-to-vote').bind('click', function(e) { 
						e.preventDefault(); 
						$container.scrollTo($poll, settings.animationSpeed, { 
							offset:-40,
							onAfter: function() {
								$movie.empty();
								$info.empty();
							}
						});
					});
					
					$info.find('#show-trailer').bind('click', function(e) {
						e.preventDefault();
						
						var p = { m: $(this).attr('rel') };
						
						$.post($(this).attr('href'), p, function(response) {
							$movie.html(response);
							$container.scrollTo($movie, settings.animationSpeed, { offset:40 });
							
							$movie.find('#back-to-info').bind('click', function(e) {
								e.preventDefault();
								$container.scrollTo($info, settings.animationSpeed, { offset:-40 });
							});
							$movie.find('#back-to-vote-movie').bind('click', function(e) {
								e.preventDefault();
								
								$container.scrollTo($poll, settings.animationSpeed * 2, { 
									offset:-40,
									onAfter: function() {
										$movie.empty();
										$info.empty();
									}
								});
							});
							
						});
					});
				});
			});
		});
	};
	
	private.onTicketOver = function() { $(this).parents('.' + settings.tickets.className).addClass('hover'); };
	private.onTicketOut = function() { $(this).parents('.' + settings.tickets.className).removeClass('hover'); };
	
	private.onPolaroidOver = function() { $(this).parents('.' + settings.polaroids.className).addClass('hover'); };
	private.onPolaroidOut = function() { $(this).parents('.' + settings.polaroids.className).removeClass('hover'); };
	
	public.init = private.init;
	return public;
	
}(window, window.document, jQuery, google));

$(document).ready(WAM.init);
