var application = {
	initialize: function(){
		var toggle = new Toggle($('.navigation > li > a'), { callback: this._update._bind(this) });
		
		this.bg_image = $('<img id="bg_image" src="http://spworks.dyndns.org/wp-content/themes/scoom/images/main/background.jpg" alt="Background" width="1300" height="730" />');
		
		$('#content').after($('#pager'));
		
		$('#inner_content').children('ul').children('li').each(function(index, element){
			if($(element).children('img').hasClass('text_background')){
				$('#background_image').append($(element).children('img').hide());
			}else{
				$('#background_image').append(this.bg_image.hide());
			}
		}._bind(this));
		
		$('#inner_content > ul').remove('img.text_background').find('li:empty').remove();
		
		var cycle_options = this._cycle_options('home');
		
		$('#inner_content > ul').cycle($.extend(cycle_options, {
			pagerAnchorBuilder: function(idx, slide) { return '<a href="#"></a>'; }
		}));
		$('#background_image').cycle($.extend(cycle_options, {
			pagerAnchorBuilder: function(idx, slide) { return $('#pager a:eq(' + idx + ')'); }
		}));
	},
	
	_hash: function(event){
		window.location.hash = (window.location.hash.length > 0) ? window.location.hash.replace(/[a-z0-9_-]+/i, event.currentTarget.title) : window.location.hash.concat(event.currentTarget.title);
	},
	
	_controls: function(title){
		$('#content').mouseenter(function(){
			$('.control_buttons').fadeIn(750, 'swing');
			if(title == 'scoom' || title == 'home'){ $('#inner_content > ul, #background_image').cycle('pause'); } });
		$('#content').mouseleave(function(){
			$('.control_buttons').fadeOut(750, 'swing');
			if(title == 'scoom' || title == 'home'){ $('#inner_content > ul, #background_image').cycle('resume'); }
		});
	},
	
	_resize: function(event){
		this.resize_image(event.data.image);
		this.center_image(event.data.image);
	},
	
	resize_image: function(image){
		var aspect_ratio =  image.height() / image.width();
		if($(window).width() > $(window).height()){
			var _height = Math.round($(window).width() * aspect_ratio);
			
			if(_height < $(window).height()){
				newHeight = $(window).height();
				newWidth = Math.round(newHeight / aspect_ratio); 
			}else{
				newWidth = $(window).width();
				newHeight = _height;
			}
		}
		
		if($(window).width() < $(window).height() || $(window).width() == $(window).height()){
			newHeight = $(window).height();
			newWidth = Math.round($(window).height() / aspect_ratio);
		}
		
		image.width(newWidth).height(newHeight);
	},
	
	center_image: function(image){
		var top = ($(window).height() - image.height()) / 2;
		var left = ($(window).width() - image.width()) / 2;
		image.css({ 'top': top, 'left': left });
	},
	
	_cycle_options: function(title){		
		var cycle_options = {
			before: function(currSlideElement, nextSlideElement, options, forwardFlag){
				if($(nextSlideElement).is('img')){
					var image = $(nextSlideElement);
					this.resize_image(image);
					this.center_image(image);
					$(window).bind('resize', { image: image }, this._resize._bind(this));
				}
			}._bind(this),
			onPagerEvent: function(zeroBasedSlideIndex, slideElement){
				if($(slideElement).is('img')){
					var image = $(slideElement);
					this.resize_image(image);
					this.center_image(image);
					$(window).bind('resize', { image: image }, this._resize._bind(this));
				}
			}._bind(this),
			slideResize:   0,
			pager: '#pager'
		}
		
		if($('#inner_content > ul > li').length > 1){
			cycle_options.fx = 'fade';
			cycle_options.speed = 1000;
			cycle_options.timeout = (title == 'scoom' || title == 'home') ? 5000 : 0;
			cycle_options.next = '#skip_next';
			cycle_options.prev = '#skip_previous';
			
			this._controls(title);
		}
		
		return cycle_options;
	},
	
	location: function(event){
		var coords = event.currentTarget.rel.split(',');
		var latlng = new google.maps.LatLng(parseFloat(coords[0]),parseFloat(coords[1]));
		this.googlemap.map.setCenter(latlng);
		if(this.googlemap.marker){ this.googlemap.marker.setPosition(latlng); }
	},
	
	_update: function(event){
		event.preventDefault();
		this._hash(event);
		
		$.post('wp-admin/admin-ajax.php', { action: 'ajax_action', title: event.currentTarget.title }, function(data){			
			var title = event.currentTarget.title.toLowerCase();
			
			if(title == 'scoom' || title == 'home'){ $('#inner_content > ul, #background_image').cycle('pause'); }
			else{ $('#inner_content > ul, #background_image').cycle('stop'); }
			
			$('#pager').remove();
			$('#inner_content').html(data);
			$('#content').after($('#pager'));
			
			$('#map').hide();
			
			$('#content').removeClass('image');
			
			$('#background_image').children().remove();
			
			$('#inner_content > ul > li').each(function(index, element){
				if($(element).children('img').hasClass('text_background')){
					$('#background_image').append($(element).children('img').hide());
				}else{
					$('#background_image').append(this.bg_image.hide());
				}
			}._bind(this));
			
			$('#inner_content > ul').remove('img.text_background').find('li:empty').remove();
			
			var cycle_options = this._cycle_options(event.currentTarget.title.toLowerCase());
			
			switch(title){
				case 'standorte':
					this.googlemap = new GoogleMap('#map');
					$('.location').click(this.location._bind(this));
					$('.locations').cycle(cycle_options);
					break;
				default:
					$('#inner_content > ul').cycle($.extend(cycle_options, {
						pagerAnchorBuilder: function(idx, slide) { return '<a href="#"></a>'; }
					}));
					$('#background_image').cycle($.extend(cycle_options, {
						pagerAnchorBuilder: function(idx, slide) { return $('#pager a:eq(' + idx + ')'); }
					}));
					if(title == 'scoom' || title == 'home'){ $('#inner_content > ul, #background_image').cycle('resume'); }
					break;
			}
		}._bind(this));
	}
}

$(document).ready(function(){ application.initialize(); });
