function isIPAD()
{
	return (navigator.userAgent.match(/iPad/i) != null) ? true : false;
}
function isIPHONE()
{
	return (navigator.userAgent.match(/iPhone/i) != null || navigator.userAgent.match(/iPod/i) != null) ? true : false;
}

if (navigator.userAgent.match(/safari/i) != null && $.cookie('safari') == null)
{
  $.cookie('safari', 'Y', { expires: 7 });
}

function requestFunc(ts)
{
	// Get monitor URL from HTML and check response
	var monitorUrl = $('#monitorUrl').val();
	var urlSession = $('#urlSession').val();
	var LID = $('#LID').val();
	ts_cached = $('.contentitem').attr('requestfunc');
	ts_cached = parseInt(ts_cached);
	if(ts_cached == ts)
	{
		$.getJSON(monitorUrl, function(data) {
			
			if (data.status == 'paid') {
				
				newpage('?page=payment-completed&urlSession='+urlSession+'&LID='+LID);
				
			} else if (data.status == 'canceled') {
				
				newpage('?page=payment-canceled');
				
			} else if (data.status == 'pending-banktransfer') {
				
				newpage('?page=pending-banktransfer&urlSession='+urlSession+'&LID='+LID);
				
			} else if (data.status == 'error') {
				
				alert('Error');
				
			}
			    
		});
		
	}
}

function timestamp()
{
  return Math.round(new Date().getTime() / 1000) + 1;
}


function intializepage(slideLeft, slideRight, fadeIn)
{
	var scroll_timeout = 0;
	
	// Create title
	$('#bar').html($('.contentitem').attr('title'));
	
	// Create some ajax links
	$('[rel=ajax]').each(function()
	{
		$(this).attr({'loadpage' : $(this).attr('href')}).removeAttr('href');
	});
	
	// slideLeft effect
	if($('.contentitem').is('.slideleft') || slideLeft == true)
	{
		$('.contentitem').css({'position' : 'absolute', 'left' : '340px', 'top' : 0});
		$('.contentitem').animate({
		left: 0
		}, 550, function()
		{
			$('.contentitem').css({'position' : 'relative'});
		});
	}
	
	if($('.contentitem').is('.requestfunc'))
	{
		var current_ts = timestamp();
		$('.contentitem').attr({'requestfunc' : current_ts});
		setInterval('requestFunc(' + current_ts + ')', 1000 );	
	}
	
	// Hideitem
	$('.hideitem').removeClass('hideitem');
	
	// Tickets scroll
	if($('.buytickets .title.scrollme').is(':visible'))
	{
		$('.buytickets .title.scrollme').each(function()
		{
			$(this).attr({'title' : $(this).text()});
		});
		$('.buytickets .title.scrollme').bind('mouseenter',function()
		{
			$(this).html('<marquee>' + $(this).attr('title') + '</marquee>');
		}).bind('mouseleave',function()
		{
			$(this).html($(this).attr('title'));
		});
	}
	
	// Tickets scroll
	if($('.buytickets .count').is(':visible'))
	{
		var ticketshtml = '';
		$('.buytickets .count').each(function(i)
		{
			var lis = '';
			$(this).children('select').children('option').each(function()
			{
				lis += '<li rel="' + i + '">' + $(this).val() + '</li>';
			});
			ticketshtml = $('.tickets.hidden').html();
			ticketshtml = ticketshtml.replace('hidden', '');
			ticketshtml = '<div class="tickets">' + ticketshtml + '</div>';
			$('<div></div>').html('' + ticketshtml + '<div class="ticketsbar closed">' + $(this).closest('.buytickets').html() + '</div><ul class="list">' + lis + '</ul>').addClass('choosecount').addClass('hide').attr({'id' : 'choosecount' + i + '', 'rel' : i}).appendTo('#body .frame');
			$(this).addClass('withjs').html('<div class="value">' + $(this).children('select').children('option:selected').val() + '</div>' + $(this).html() + '');
			$(this).children('select').attr({'id' : 'counting' + i + '', 'rel' : i}).hide();
			$(this).attr({'id' : 'countoption' + i + ''});
			$('#countoption' + i + '').click(function()
			{
				$('#choosecount' + i + '').show().animate({
				left: 0
				}, 550, function()
				{

				});
			});
			$('#choosecount' + i + '').children('ul').children('li').click(function()
			{
				$('#choosecount' + i + '').show().animate({
				left: '340px'
				}, 550, function()
				{
					$('#choosecount' + i + '').hide();
				});
				$('#countoption' + i + ' .value').html($(this).text());
				$('#countoption' + i + ' select option:selected').val($(this).text());
			});
		});
		$('.ticketsbar .price, .ticketsbar .count').remove();
	}
	
	// slideRight effect
	if($('.contentitem').is('.slideright') || slideRight == true)
	{
		$('.contentitem').css({'position' : 'absolute', 'left' : '-340px', 'top' : 0});
		$('.contentitem').animate({
		left: 0
		}, 550, function()
		{
			$('.contentitem').css({'position' : 'relative'});
		});
	}
	
	// fadeIn effect
	if($('.contentitem').is('.fadein') || fadeIn == true)
	{
		$('.contentitem').hide().fadeIn(500);
	}
	
	// Tickets effect
	if($('.tickets.witheffect').is(':hidden') || $('.tickets.witheffect').is(':visible'))
	{
		scroll_timeout = 3400;
		var count_tickets = $('.tickets ul li').length;
		$('.tickets').children('ul').children('li').children('.holder').children('a').hide();
		$('.tickets ul li .holder a').each(function(i)
		{
			$(this).delay(i * 850).fadeIn(600);
		});
	}
	
	if(!isIPHONE() && !isIPAD())
	{
		// Scrollable page
		setTimeout(function()
		{ 
			makeScrollable('div.scrollme', 'div.content', false);
		}, scroll_timeout);
	}
	
	// Map
	$('form.createpopup').submit(function()
	{
		window.open('http://www.easyticket.nl/mapPod.php?val=' + $('.takepostalcode').val() + '', 'dummyname', 'height=400,width=500', false);
		return false;
	});
	
	// Ajax post
	$('form[rel=ajax]').submit(function()
	{
		var replaced = false;
		var request_url = 'httprequest.php' + $(this).attr('action') + '';
		var slideLeft = false;
		var slideRight = false;
		var fadeIn = false;
		var postdate = $(this).serialize();
		if($(this).is('.slideleft'))
		{
			var slideLeft = true;
		} else if($(this).is('.slideright'))
		{
			var slideRight = true;
		}
		if($(this).is('.fadein'))
		{
			var fadeIn = true;
		}
		var newpage_timeout = 0;
		if($(this).is('.fadeout'))
		{
			newpage_timeout = 500;
			$('#holder .frame .contentitem').fadeOut(500);
		} else if($(this).is('.slideleftout'))
		{
			newpage_timeout = 550;
			$('.contentitem').css({'position' : 'absolute', 'left' : 0, 'top' : 0});
			$('.contentitem').animate({
			left: '-340px'
			}, 550, function()
			{
				$('.contentitem').css({'position' : 'relative'});
			});
		} else if($(this).is('.sliderightout'))
		{
			newpage_timeout = 550;
			$('.contentitem').css({'position' : 'absolute', 'left' : 0, 'top' : 0});
			$('.contentitem').animate({
			left: '340px'
			}, 550, function()
			{
				$('.contentitem').css({'position' : 'relative'});
			});
		}
		setTimeout(function()
		{
			$.post(request_url, postdate + '',function(data)
			{
				$('#holder .frame').html(data);
							
				// Re-intialize the page
				intializepage(true, slideRight, fadeIn);
			});
		}, newpage_timeout);
		return false;
	});
	
	// Httprequest
	$('[loadpage], .fromhiddenurl').click(function()
	{
		var replaced = false;
		var request_url = 'httprequest.php' + $(this).attr('loadpage') + '';
		var slideLeft = false;
		var slideRight = false;
		var fadeIn = false;
		if($(this).is('.fromhiddenurl'))
		{
			request_url = 'httprequest.php' + $('#hiddenurl').val() + '';
			slideLeft = true;
		}
		if($(this).is('.slideleft'))
		{
			var slideLeft = true;
		} else if($(this).is('.slideright'))
		{
			var slideRight = true;
		}
		if($(this).is('.fadein'))
		{
			var fadeIn = true;
		}
		var newpage_timeout = 0;
		if($(this).is('.fadeout'))
		{
			newpage_timeout = 500;
			$('#holder .frame .contentitem').fadeOut(500);
		} else if($(this).is('.slideleftout'))
		{
			newpage_timeout = 550;
			$('.contentitem').css({'position' : 'absolute', 'left' : 0, 'top' : 0});
			$('.contentitem').animate({
			left: '-340px'
			}, 550, function()
			{
				$('.contentitem').css({'position' : 'relative'});
			});
		} else if($(this).is('.sliderightout'))
		{
			newpage_timeout = 550;
			$('.contentitem').css({'position' : 'absolute', 'left' : 0, 'top' : 0});
			$('.contentitem').animate({
			left: '340px'
			}, 550, function()
			{
				$('.contentitem').css({'position' : 'relative'});
			});
		}
		setTimeout(function()
		{
			$.ajax({
				url: request_url, cache: false, success: function(html)
				{
					$('#holder .frame').html(html);
								
					// Re-intialize the page
					intializepage(slideLeft, slideRight, fadeIn);
				}
			});
		}, newpage_timeout);
	});
}

function newpage(request_url)
{
	request_url = 'httprequest.php' + request_url + '';
	$.ajax({
		url: request_url, cache: false, success: function(html)
		{
			$('#holder .frame').html(html);
						
			// Re-intialize the page
			intializepage(true, false, false);
		}
	});
}

// jQuery document ready
$(document).ready(function()
{
	// Hide & fadeIn
	$('#ticketpod').hide().fadeIn(500).addClass('ticketpod-js');
	
	if(isIPHONE() || isIPAD())
	{
		jQuery('body').addClass('ipadiphone');
	}
	
	setTimeout(function()
	{
		// Intialize page
		intializepage(false, false, false);
	}, 1000);
});
