var oInstance = null;

function appendObject(obj1, obj2)
{
	if(obj1 && obj2) {
		for (var key in obj2) {
			obj1[key] = obj2[key];
		}
	}
}

var PierMarketing = function()
{
	this.init();
}

PierMarketing.prototype = 
{
		init : function()
		{
			this.oLayout = new LayoutManager();
			
			var oDimensions = this.oLayout.calculateDimensions();
			this.oLayout.resize(oDimensions);
			
			var self = this;
//			$('#slides').slides( {
//					preload : true,
//					preloadImage : '../img/loading.gif',
//					play : 8000,
//					pause : 2500,
//					container: 'slidecontainer',
//					animationComplete: function(current){
//						$('#topstrapline' + current).animate({
//							width: '50%'
//						}, 750);
//						
//						$('#bottomstrapline' + current).animate({
//							width: '45%'
//						}, 850);
//						self.oLastTop = $('#topstrapline' + current);
//						self.oLastBottom = $('#bottomstrapline' + current);
//						setTimeout(function(){
//							self.oLastTop.animate({
//								width: '0%'
//							}, 950);
//							
//							self.oLastBottom.animate({
//								width: '0%'
//							}, 850);
//						}, 7550);
//						self.current = current;
//					},
////					autoHeight: true,
//					effect: 'slide',
//					slideSpeed: 500,
//					generatePagination : false
//			} );
			
			if( $('.pagetext').length > 0 ){
				var iSpeed = 2000;
			}
			else {
				var iSpeed = 8000;
			}
			
			if( $('.homecentre').length > 0 ){
				$('#contentwrapper').css('background', '#000000');
			}
			
			$('.slidecontainer').cycle({
				fx : 'fade',
				after : function(current, next){
					$('#topstrapline' + next.id).animate({
						width: '50%'
					}, 500);
					
					$('#bottomstrapline' + next.id).animate({
						width: '45%'
					}, 600);
					self.oLastTop = $('#topstrapline' + next.id);
					self.oLastBottom = $('#bottomstrapline' + next.id);
					setTimeout( function(){
						self.oLastTop.animate( {
							width: '0%'
						}, 600 );
						
						self.oLastBottom.animate( {
							width: '0%'
						}, 500 );
					}, 6500 );
					self.current = current;
				},
				timeout : iSpeed,
				speed : 1000
			})
			
			if ( $('#topstrapline1') ) {
				$('#topstrapline1').animate({
					width: '50%'
				}, 500);
				self.oLastTop = $('#topstrapline1');
			}
			
			if ( $('#bottomstrapline1') ) {
				$('#bottomstrapline1').animate({
					width: '45%'
				}, 600);
				self.oLastBottom = $('#bottomstrapline1');
			}
			
			setTimeout(function(){
				self.oLastTop.animate({
					width: '0%'
				}, 600);
				
				self.oLastBottom.animate({
					width: '0%'
				}, 500);
			}, 7550);
			
			this.oProject = new Project();
			this.oTeam = new Team();
			this.oTestimonial = new Testimonial();

			this.assignSpies();
		},
		
		assignSpies : function()
		{
			$('#popup1').mouseenter( function(){
				$('#popup1').animate({
					height: '35%'
				}, 500);
				$('#popup1 h3').css('borderBottom', '1px solid #ffffff');
			}).mouseleave( function(){
				$('#popup1').animate({
					height: '7%'
				});
				$('#popup1 h3').css('borderBottom', 'none');
			});
			$('#popup2').mouseenter( function(){
				$('#popup2').animate({
					height: '35%'
				}, 500);
				$('#popup2 h3').css('borderBottom', '1px solid #ffffff');
			}).mouseleave( function(){
				$('#popup2').animate({
					height: '7%'
				});
				$('#popup2 h3').css('borderBottom', 'none');
			});
			$('#popup3').mouseenter( function(){
				$('#popup3').animate({
					height: '35%'
				}, 500);
				$('#popup3 h3').css('borderBottom', '1px solid #ffffff');
			}).mouseleave( function(){
				$('#popup3').animate({
					height: '7%'
				});
				$('#popup3 h3').css('borderBottom', 'none');
			});
		}
}

var LayoutManager = function()
{
	this.init();
}

LayoutManager.prototype = 
{
		oTimer : null,
		
		init : function()
		{
			_this = this
			$('.articles').find('>:last-child').css('border-bottom', 'none').css('margin-top', '5%');
			$('.post_body').find('>:first-child').css('float', 'none').css('marginLeft', '220px');
			
			$(window).resize(this.onResize);
		},
		
		onResize : function(e)
		{
			var oDimensions = _this.calculateDimensions();
			clearTimeout(_this.oTimer);
			this.oTimer = setTimeout( function() {
				_this.resize( oDimensions );
			}, 50);
		},
		
		resize : function( oParams )
		{
			if(oParams.height > 1000) {
				$('html').css('height', oParams.height);
			}
			else {
				$('html').css('height', 1000);
			}
			
			var iContentHeight = parseInt($('.pagecontent').css('height')) - 120;
			
			$('.pagecentre').css('height', iContentHeight);
			
			iNewWidth = (oParams.width - 960) / 2;
			iPictureMargin =  iNewWidth - 480;
			
			if(iNewWidth > 0){
				$('.pagecentre').css('marginLeft', iNewWidth);
				$('.headercontent').css('marginLeft', iNewWidth);
				$('.footercontent').css('marginLeft', iNewWidth);
				$('.footerbodycontent').css('marginLeft', iNewWidth);
				$('.footercopycontent').css('marginLeft', iNewWidth);
				$('.pagebannercontent').css('marginLeft', iNewWidth);
				$('.homecentre').css('left', iNewWidth);
				$('.straplinetext').css('marginLeft', iNewWidth);
				$('#homebackground1').css('marginLeft', iPictureMargin);
				$('#homebackground2').css('marginLeft', iPictureMargin);
				$('#homebackground3').css('marginLeft', iPictureMargin);
				$('#topstrapline1').css('right', 0);
				$('#topstrapline2').css('right', 0);
				$('#topstrapline3').css('right', 0);
				$('#bottomstrapline1').css('right', 0);
				$('#bottomstrapline2').css('right', 0);
				$('#bottomstrapline3').css('right', 0);
				$('.topshadow').css('left', iNewWidth);
				$('.bottomshadow').css('left', iNewWidth);
			}
			
			if( oParams.width > 960 ){
				$('html').css('width', oParams.width);
				$('body').css('width', oParams.width);
				$('#pagewrapper').css('width', oParams.width);
				$('#headerwrapper').css('width', oParams.width);
				$('#contentwrapper').css('width', oParams.width);
				$('#footerwrapper').css('width', oParams.width);
			}
			else {
				$('html').css('width', 980);
				$('body').css('width', 980);
				$('#pagewrapper').css('width', 980);
				$('#headerwrapper').css('width', 980);
				$('#contentwrapper').css('width', 980);
				$('#footerwrapper').css('width', 980);
			}
		},
		
		calculateDimensions : function()
		{
			 var iViewPortWidth;
			 var iViewPortHeight;
			 
			 // the more standards compliant browsers (mozilla/netscape/opera/IE7) use window.innerWidth and window.innerHeight
			 
			 if (typeof window.innerWidth != 'undefined')
			 {
				 iViewPortWidth = window.innerWidth,
				 iViewPortHeight = window.innerHeight
			 }
			 
			// IE6 in standards compliant mode (i.e. with a valid doctype as the first line in the document)

			 else if (typeof document.documentElement != 'undefined'
			     && typeof document.documentElement.clientWidth !=
			     'undefined' && document.documentElement.clientWidth != 0)
			 {
				 iViewPortWidth = document.documentElement.clientWidth,
				 iViewPortHeight = document.documentElement.clientHeight
			 }
			 
			 // older versions of IE
			 
			 else
			 {
				 iViewPortWidth = document.getElementsByTagName('body')[0].clientWidth,
				 iViewPortHeight = document.getElementsByTagName('body')[0].clientHeight
			 }
			
			 return {
				 width : iViewPortWidth,
				 height : iViewPortHeight
			 };
		}
}

var Project = function()
{
	this.init();
}

Project.prototype =
{
	aProjects : null,
	
	init : function()
	{
//		$('#first').css('marginLeft', '4000px');
		
		$('#next').click(function(e){
			var oElement = $('.projects').find(">:first-child");
			var oNewFirst = $('.projects').find(">:nth-child(2)");
			var sMargin = oElement.css('marginLeft');
			
			if ( !oElement.is(':animated') ) {
				oElement.animate({
					"marginLeft": "-=960px"
				}, 500, 'swing', function(){
					$('.projects').append(oElement);
					oNewFirst.css('marginLeft', sMargin);
					oElement.css('marginLeft', '0px');
				});
			}
		});
		
		$('#prev').click(function(e){
			var oElement = $('.projects').find(">:first-child");
			var sMargin = oElement.css('marginLeft');
			var oLastChild = $('.projects').find(">:last-child");
			
			if ( !oElement.is(':animated') ) {
				oLastChild.css('marginLeft', parseInt(oElement.css('marginLeft')) - 960);
				$('.projects').prepend(oLastChild);
				oElement.css('marginLeft', '0px');
				
				oLastChild.animate({
					"marginLeft": "+=960px"
				}, 500, 'swing');
			}
		});
	}
}

var Team = function()
{
	this.init();
}

Team.prototype =
{
	init: function(){
		$('#rightarrow').click(function(e){
			var oElement = $('.team').find(">:first-child");
			var oNewFirst = $('.team').find(">:nth-child(2)");
			var sMargin = oElement.css('marginLeft');
			
			if ( !oElement.is(':animated') ) {
				oElement.animate({
					"marginLeft": "-=300px"
				}, 500, 'swing', function(){
					$('.team').append(oElement);
					oNewFirst.css('marginLeft', sMargin);
					oElement.css('marginLeft', '60px');
				});
			}
		});
		
		
		$('#leftarrow').click(function(e){
			var oElement = $('.team').find(">:first-child");
			var sMargin = oElement.css('marginLeft');
			var oLastChild = $('.team').find(">:last-child");
			
			if ( !oElement.is(':animated') ) {
				oLastChild.css('marginLeft', parseInt(oElement.css('marginLeft')) - 300);
				$('.team').prepend(oLastChild);
				oElement.css('marginLeft', '60px');
				
				oLastChild.animate({
					"marginLeft": "+=300px"
				}, 500, 'swing');
			}
		});
	}
}

var News = function()
{
	this.init();
}

News.prototype =
{
	init : function()
	{
		$('#newslinkone').click(function(){
			$('#articleone').animate({
				height : '300'
			}, 500);
			$('#articletwo').animate({
				height : '0'
			}, 500);
		});
		
		$('#newslinktwo').click(function(){
			$('#articletwo').animate({
				height : '300'
			}, 500);
			$('#articleone').animate({
				height : '0'
			}, 500);
		});
	}
}

var Testimonial = function()
{
	this.init();
}

Testimonial.prototype =
{
	iCount : 1,
	
	init : function()
	{
		var iTestimonial = $('.testimonialtext').children().length;
		var self = this;
		var sTestimonialID = '#testimonial' + this.iCount;
		
		$(sTestimonialID).fadeIn(700, function(){
			var oFadeOutTimer = setTimeout(function(){
				$(sTestimonialID).fadeOut(500, function(){
					$('.testimonialtext').append($(sTestimonialID))
					self.iCount++;
					if(self.iCount > iTestimonial){
						self.iCount = 1;
					}
					self.init();
					
				});
			}, 16000);
		});
	}
}

$(document).ready( function() {
	oInstance = new PierMarketing();
} );
