function Site7() {
		
    if (typeof(console)=='undefined') console = { log:function() { } };
	if (typeof($)=='undefined') $ = jQuery;
	if (typeof(jQuery)=='undefined') console.log('no jQuery!');
	
	var site = this;
	
	this.baseURL = 'http://' + window.location.hostname;
	
	this.screens = {
		window: $(window),
		document: $(document),
		body: $('body'),
		head: $('head'),
		secondary: $('#secondary'),
		primary: $('#primary')
	}
	
	
	//this.ajaxify();
	
	var pageIs = function(bodyclass){
		return site.screens.body.hasClass(bodyclass);
	}	
	
	this.initShowcase(this);
	
	
	if(pageIs('home')){
		this.easterEgg(this);
	}
		
	if(pageIs('blog') || pageIs('single-post')){
		this.blogInit(this);
	}
	
	if(pageIs('single-sector')){
		this.sectorInit(this);
	}
	
	if(pageIs('single-project')){
		this.projectInit(this);
	}
	
	if(pageIs('page-template-template-howto-php')){
		this.howToInit(this);
	}
	
		
	if(/Apple/.test(navigator.vendor)){
		this.safariFix(this);
	}
		
	this.appStateChecker(this);
	
	this.subMenuEffects(this);
	
	$('.deep-data').hide();
	
}

Site7.prototype.subMenuEffects = function(site){

		
	var submenus = $('nav#access .menu .sub-menu');
	var parents = submenus.parent();
	
	var t = null;
	
	parents.hover(function(){
		clearTimeout(t);
		$(this).find('.sub-menu').slideDown()
	}, function(){
		var $this = $(this);
		clearTimeout(t);
		t = setTimeout(function(){
			$this.find('.sub-menu').slideUp()
		},1000)
	});
	
}

Site7.prototype.appStateChecker = function(site){

	var open = null;
	
	var initial_height = 54;
	
	var app_container = $('#app-7').parent();
	
	var check = function(){
		
		var new_state_open = null;
		
		var height = app_container.height();
		
		new_state_open = (height > initial_height);
		
		if(open != new_state_open){
			if(new_state_open == true){
				//user has opened the app
				//push the state to google analytics
				_gaq.push(['_trackEvent', 'App', 'Open', 'Opens the app']);
			} else {
				//user has closed the app
			}
		} 
		
		open = new_state_open;
	}
	
	var timer = setInterval(check, 1000); 
	
}
	

Site7.prototype.safariFix = function(site){

	var color = site.screens.body.css('color');
	
	$('a').each(function(){
		
		var $this = $(this);
		
		var color = $this.css('color');
		
		var t = setTimeout(function(){
			$this.css('color', '#080808');
		}, 500);
		
		var t = setTimeout(function(){
			$this.css('color', color);
		}, 600);
	})
	
	var t = setTimeout(function(){
		site.screens.body.css('color', '#080808');
	}, 500);
	
	var t = setTimeout(function(){
		site.screens.body.css('color', color);
	}, 600);
}


Site7.prototype.initShowcase = function(site){
	
	var obj = this;
	
	$('.feature-slider a').click(function(e) {
	
		e.preventDefault();
		
		$('.featured-posts section.featured-post').css({
			opacity: 0,
			visibility: 'hidden'
		});
		
		$(this.hash).css({
			opacity: 1,
			visibility: 'visible'
		});
		
		$('.feature-slider a').removeClass('active');
		
		$(this).addClass('active');
		
	});
	
}


Site7.prototype.ajaxify = function(site){

	var obj = this;
	
	$('#menu-s7menu .menu-item a').click(function(event){
		event.preventDefault();
		
		$this = $(this);
		
		var url = $this.attr('href');
		
		var $main = $('#main');
		
		$main
			.css({ opacity: 0.2 })
			.load(url + '  #main', {
				success: function(){
					$main.css({opacity: 1})
				}
			});
		
				
	});
}


Site7.prototype.easterEgg = function(site){
	
	var obj = this;

	this.ee = {
	
		init: function(){
			
			obj.ee.preloadImages();
		
			$('#keeper').css('backgroundImage', 'url(/wp-content/themes/s7_new/images/drag/home_bg.jpg)');
			
			var iphone = $('#iphone').css('backgroundImage', 'url(/wp-content/themes/s7_new/images/drag/iPhone_trans.png)');
			
			var iphone_screen = $('#iphone_screen').css({
				'backgroundImage': 'url(/wp-content/themes/s7_new/images/drag/places.jpg)',
				'backgroundPosition' : '-292px 0px'
			});

			var curPos;

			iphone.draggable({
				axis: 'x',
				containment: 'parent',
				drag: function(event,ui){
					curPos = iphone.css('left').slice(0,-2); 
					
					if(curPos >= 0 && curPos <= 210)
					{
						iphone_screen.css('backgroundPosition','0px 0px');
					}
					else if (curPos >= 211 && curPos <= 402 )
					{
						iphone_screen.css('backgroundPosition','-146px 0px ');
					}
					else if (curPos >= 403&& curPos <= 630 )
					{
						iphone_screen.css('backgroundPosition','-292px 0px');
					}
					else
					{
						iphone_screen.css('backgroundPosition','-438px 0px');				
					}
				}
			});
		},
		
		preloadImages: function(){
			$.each([
				'home_bg.jpg',
				'iPhone_trans.png',
				'places.jpg'
			], function(index, src){
				$('<img/>')
					.attr('src', '/wp-content/themes/s7_new/images/drag/' + src)
					.load(function(){
						$(this)
							.hide()
							.appendTo(site.screens.body)
					})
			});
		}
	}
	
	this.ee.init();
	
}

Site7.prototype.pagination = function(site, type){
	
	var $paginated_posts = $('.paginated-posts');
	
	console.log('pagination', $paginated_posts);
	
	var center_title = function(){
		$paginated_posts.find('.title').center('vertical');
	}
	
	center_title();

	
	var $article = $('#main article.hentry');
	var post_id = $article.attr('id').split('-')[1];


	$('.paginated-posts a.next, .paginated-posts a.previous')
		.live('click', function(event){
			
			event.preventDefault();
			
			$paginated_posts.css({
				opacity: 0.7
			});
			
			var $this = $(this);
			
			$paginated_posts
				.load(site.baseURL + '/wp-content/themes/s7_new/inc/'+ type +'_pagination.php', {
					ajax: true,
					href: $this.attr('href'),
					post_id: post_id
				}, function(){
					$paginated_posts.css({
						opacity: 1
					});
					center_title();
				});
		});

}

Site7.prototype.blogInit = function(site){

	var obj = this;
		
	this.blog = {
		
		init: function(){		
			
			//obj.blog.thumbFix();
			
			site.pagination(site, 'blog');
			
		},
		
		thumbFix: function(){
			var thumbs = $('#paginated-posts .thumb')
				.error(function(){
					var $this = $(this);
					$this.css({
						backgroundColor: 'red'
					})
					.attr('src', '/wp-content/uploads/placeholder-55x55.jpg');
				});
		}
		
	}
	
	this.blog.init();

}

Site7.prototype.sectorInit = function(site){
	site.pagination(site, 'project');
}

Site7.prototype.projectInit = function(site){
	
	var obj = this;

	
	this.project = {
	
		init: function(){
		
			obj.project.lightBox();
			obj.project.fixEmbedSize();
			obj.project.accentBoxContent();
			
		},
		
		lightBox: function(){
		
			var lightbox_folder = '/wp-content/themes/s7_new/js/common/fancyzoom/';
			
			var loadCSS = function(file){
			
				$('<link/>')
					.attr({
						rel: 'stylesheet',
						type: 'text/css',
						href: file
					})
					.appendTo(site.screens.head);
			}
			
			lightbox_folder = '/wp-content/themes/s7_new/js/common/shadowbox-3.0.3/';
			
			$('#project-images ul li a')
				.attr('rel', 'shadowbox[project]');
			
			$.getScript(lightbox_folder + 'shadowbox.js', function(){
				
				Shadowbox.init({
					animate: true,
					displayNav: true
				});
			
				loadCSS(lightbox_folder + 'shadowbox.css');
				
			});			
			
		},
		
		fixEmbedSize: function(){

			//fix embedded stuff
			var secondary_width = site.screens.secondary.width();
			
			$('#secondary aside iframe').each(function(i, el){
				var $this = $(this);
				var w = $this.width(), h = $this.height();
				
				var scale = w / secondary_width;
				
				$this
					.height(Math.round(h / scale))
					.width('100%');
			});
			
		},
		
		accentBoxContent: function(){
			$('#content article .entry-content p').first()
				.addClass('accent-box');
		}
		
	}
	
	this.project.init();
	
}

Site7.prototype.howToInit = function(site){

	var howToAccordion = function(){
	
		var $panels = $('.panelcontent')
		
		var slideTime = 800;
		
		$('#guide_7sg h2')
			.css({
				cursor: 'pointer'
			})
			.click(function(event){
			
			var $this = $(this);
			var $panel = $this.next();
			
			if($panel.hasClass('panelcontent')){
				
				$parent_panel = $panel.parent().parent();
				
				var is_nested = ($parent_panel.attr('id') == 'nested_info');
				
				if(is_nested){
				
					$sibling_panels = $parent_panel.find('.panelcontent');
					
					$sibling_panels.slideUp(slideTime);
					
				} else {
				
				
					$panels.slideUp(slideTime);

				}
				
				if($panel.is(':hidden')){
					
					
					$panel.slideDown(slideTime, function(){
					
						var scrollTimeout = setTimeout(function(){
							site.screens.body.scrollTo($this, 300);
						}, 300)
					
					});
					
				} else {
					
					$panel.slideUp(slideTime);
					
				}
			}
			
		});
	}
	

	$.getScript('/howto_files/jquery.scrollTo-1.4.2-min.js', howToAccordion);
	
	
}
