//send forward to burgtheater if not viewed with iphone
var agent=navigator.userAgent.toLowerCase();
var is_iphone = (agent.indexOf('iphone')!=-1);

if (!is_iphone) { 
	// window.location.replace('http://www.burgtheater.at'); 
}


var jQT = $.jQTouch({
	slideSelector:'.right, .forceSlide',
//	icon: 'graphics/appicon.png',
	icon: false,
	addGlossToIcon: false,
	startupScreen:'graphics/splashscreen.jpg',
	statusBar: 'black',
	formSelector: '.searchform',
	preloadImages: [
		'javascript/lib/jqtouch/themes/burg/img/loading.gif',
		'javascript/lib/jqtouch/themes/burg/img/backButton.png',
		'javascript/lib/jqtouch/themes/burg/img/toolButton.png',
		'javascript/lib/jqtouch/themes/burg/img/borderBottom.gif',
		'javascript/lib/jqtouch/themes/burg/img/borderBottomTheatre.gif',
		'javascript/lib/jqtouch/themes/burg/img/borderTop.gif',
		'javascript/lib/jqtouch/themes/burg/img/borderTopTheatre.gif',
		'javascript/lib/jqtouch/themes/burg/img/day.png',
		'javascript/lib/jqtouch/themes/burg/img/day_white.png',
		'javascript/lib/jqtouch/themes/burg/img/chevron_white.png',
		'javascript/lib/jqtouch/themes/burg/img/bgButtonCenter.png',
		'javascript/lib/jqtouch/themes/burg/img/bgButtonLeft.png',
		'javascript/lib/jqtouch/themes/burg/img/bgButtonRight.png'
	]
});

$(function(){
    jQT.addAnimation({
        name: 'slidedown',
        selector: '.slidedown'
    });
    jQT.addAnimation({
    	name: 'none',
    	selector: '.none'
    });
});

$(document.body).bind('turn', function(event, info){
	//scroll to top
//	if( info.orientation == 'landscape' ){
		scrollTo(0,0);	
//	}
	
});

//you will find that jQTouch fails to appropriately identify the initial orientation
//do to the fact that not all components are loaded before firing $.ready
$(window).load(function(){
	$(document.body).trigger('orientationchange');
});

$(document).ready(function(){

    // Online und Offline-Status des iPhones erkennen
   if( !navigator.onLine ){
	   $('.content').hide();
	   $('.search').hide();
	   $('.toolbar a').hide();
	   
	   var offline_msg = '<div id="offline">Diese Applikation benötigt eine<br /><b>Verbindung zum Internet!</b></div>';
	   $('#overview').append( offline_msg );
   }
	
	
	if( $('body').hasClass('fullscreen') ){
		window.localStorage.setItem('install', 'hidden');
		$("#install").hide();
	}
	
	// show install guide
	if( window.localStorage.getItem('install') != "hidden" ){
		$("#install").show();
	}
	
	// hide install guide
	$('#install a.close').click(function() {
		window.localStorage.setItem('install', 'hidden');
		$("#install").hide();
	});
	
	// tap on button in footer
	// prevent all further events (error in page history)
	$('a.loadMonth').bind('tap', function() {
		loadMonth($(this));
		return false;
	});	

	// click on button in footer
	// prevent all further events (error in page history)
	$('a.loadMonth').click( function(e) {
		loadMonth($(this));
		return false;
	});	

	//load "homepage", current month, all theatres
	loadMonth( 'current-all' );
	
	//define html of preloader
	var preloader = '<div class="loading">Inhalt wird geladen</div>';
	
	handleExternalLinks();
	
	//act pageAnimationStart
	$('#gallery').bind('pageAnimationStart', function(e, info){
		
		//set opacity to 0
		$('#gallery > .toolbar').css('opacity',0);
		
		var id = $(this).data('referrer').attr('rel');
		
		//calculate startposition
		//also edited extension slideHorizontally() !!
		var width = 320;
		if( $('body').hasClass('landscape') ){
			width = 480;
		}
		var position = parseInt($(this).data('referrer').attr('number')) * width * -1;
		
		//cause of asynchr. loading, set slide manually
		$('#gallery-'+id+ ' > table').slideHorizontally({acceleration:500,pos:position});
		
	});	 
	
	
	//act pageAnimationStart
	$('#act').bind('pageAnimationStart', function(e, info){
		
//		console.log("act; pageAnimationStart; " + info.direction);
		
		var referrer = $(this).data('referrer');
		var id = referrer.attr('rel'); 
		
		//hide all active acts, if referrer is from overview,
		//to prevent "kurzes flackern"
		if( referrer.hasClass('right') ){
			
			$('.acts').hide();
			
			//show on start if already loaded
			if ( $('#act-'+id).data('loaded') ) {
				$('#act-'+ id).show();
			}
			
		}	
		
	});	 
	
	//act pageAnimationEnd
	$('#act').bind('pageAnimationEnd', function(e, info){
    	
//    	console.log("act; pageAnimationEnd; " + info.direction);
    	
    	//referrer (link)
    	var referrer = $(this).data('referrer');
    	
    	//if event is triggered by an list item
    	if( info.direction == 'in' && referrer.hasClass('right')  ){
    		
	    	//get id of act, stored in attribute rel
	       	var id = referrer.attr('rel'); 
	       	
	    	if (!$('#act-'+id).data('loaded')) {
	    		
	    		//create wrapper for act
	    		$(this).
	    			children('.content').
	    			append('<div id="act-'+id+'" class="acts"></div>');
	    		
	    		var container = $('#act-'+id); 
	    		
	    		//load content into wrapper
	    		container
					.html(preloader)
					.load('/content/ajax/act.ajax.php?id='+id, function() { 
						$('#act-'+id).data('loaded', true);
						
						//when loaded show info button in toolbar
						var link = $('#act').children('.top').children('a.hide');
						link.attr('rel',id);
						link.show();
						
						//set subpart intro as loaded
						$('#act-'+id).find('.intro').data('loaded', true);
						
					});
	            
	        }
	    	
    	}
    	    	
    });

	//overview pageAnimationStart
	$('#overview').bind('pageAnimationStart', function(e, info){
		
//		console.log("overview; pageAnimationStart; " + info.direction);
		
		if( info.direction == 'in' ){
			var referrer = $(this).data('referrer');
			var id = referrer.attr('rel'); 
			var month = referrer.attr('title'); 
			
			//hide all active acts, if referrer is from overview,
			//to prevent "kurzes flackern"
			if( referrer.hasClass('right') ){
				
				$('.theatre > div').hide();
//				console.log($('#'+id).children('.'+month));
				
				//show on start if already loaded
				if ( $('#'+id).children('.'+month).data('loaded') ) {
					$('#'+id).children('.'+month).show();
				}
				
			}
		}
	});			
	
	//overview pageAnimationEnd
    $('#overview').bind('pageAnimationEnd', function(e, info){
    	
//    	console.log("overview; pageAnimationEnd; " + info.direction);
    	
		if( info.direction == 'in' ){
			
			//if event is triggered by button in bottom toolbar
			if( $(this).data('referrer').parent().parent().parent().hasClass('bottom') ) {
				
				//add class current to container
				if( !$(this).hasClass('current') ){
					$(this).addClass('current');
				}
			}
			
			// id of place (eg, all, burg, ...)
			var id = $(this).data('referrer').attr('rel');
			
			// month (possible is nextMonth or currentMonth)
			var month = $(this).data('referrer').attr('title');
			
			// if no data referrer set, load current, all
			if( id == undefined ){
				id = 'all';
			}
			if( month == undefined ){
				month = 'currentMonth';
			}
			
			//container, where content is added
			var container = $('#'+id).children('.'+month);
			
			//hide all overviews and show current
			$('.theatre > div').hide();
			container.show();
			
			
			//if not loaded before, send ajax request and load data
	    	if ( !container.data('loaded') ) {
	    		
	        	container
					.html(preloader)
					.load('/content/ajax/overview.ajax.php?id='+id+'&month='+month, function() { 
						container.data('loaded', true);
					});	
	        	
	    	}
	    	
		}
    	
    });

});

//load current or next month
function loadMonth(el){
	
	//define html of preloader
	var preloader = '<div class="loading">Inhalt wird geladen</div>';	
	
	//called initially on startup or by button in bottom toolbar
	if( el == 'current-all' ){

		var id = 'all';
		var month = 'currentMonth';
		
	}else{
		
		// id of place (eg, all, burg, ...)
		var id = el.attr('rel');
		
		// month (possible is nextMonth or currentMonth)
		var month = el.attr('title');
		
		$('a.loadMonth').removeClass('pressed');
		$('a[title='+month+']').addClass('pressed');

		//define data referrer
		$('#overview').data('referrer', el); 
	}
	
	//container, where content is added
	var container = $('#'+id).children('.'+month);
	
	//hide all overviews and show current
	$('.theatre > div').hide();
	container.show();
	
	//if not loaded before, send ajax request and load data
	if ( !container.data('loaded') ) {
		
    	container
			.html(preloader)
			.load('/content/ajax/overview.ajax.php?id='+id+'&month='+month, function() { 
				container.data('loaded', true);
				
				if( el == 'current-all' ){
					//define 1st data referrer for page back
					$('#overview').data('referrer', $('#currentMonth-currentMonth-all'));
				}
				
			});	
    	
	}
	
}


//handle external links
function handleExternalLinks(prefix){
	
	if( prefix ==  undefined ){
		prefix = '';
	}
	
	$(prefix+'a[rel="external"]').click(function(e) { 
		$(this).removeClass('active'); 
	});
	
	$(prefix+'a[target="_blank"]').click(function(e) {
		$(this).removeClass('active');
		return confirm('Dieser Link öffnet in einem neuen Fenster.');
	});
}


