// JavaScript Document
$(function()
{
	//magazine growth function
	function growMags()
	{
		$('#hg').posOne();
		$('#hg').posTwo();
		$('#hg').posThree();
		$('#hg').posFour();
		$('#hg').posFive();
		$('#hg').posSix();
		$('#hg').posSeven();
		
		$('#lif').posTwo();
		$('#lif').posThree();
		$('#lif').posFour();
		$('#lif').posFive();
		$('#lif').posSix();
		$('#lif').posSeven();
		$('#lif').posOne();
		
		$('#ci').posThree();
		$('#ci').posFour();
		$('#ci').posFive();
		$('#ci').posSix();
		$('#ci').posSeven();
		$('#ci').posOne();
		$('#ci').posTwo();
		
		$('#fd').posFour();
		$('#fd').posFive();
		$('#fd').posSix();
		$('#fd').posSeven();
		$('#fd').posOne();
		$('#fd').posTwo();
		$('#fd').posThree();
		
		$('#pw').posFive();
		$('#pw').posSix();
		$('#pw').posSeven();
		$('#pw').posOne();
		$('#pw').posTwo();
		$('#pw').posThree();
		$('#pw').posFour();
		
		$('#ad').posSix();
		$('#ad').posSeven();
		$('#ad').posOne();
		$('#ad').posTwo();
		$('#ad').posThree();
		$('#ad').posFour();
		$('#ad').posFive();
		
		$('#nel').posSeven();
		$('#nel').posOne();
		$('#nel').posTwo();
		$('#nel').posThree();
		$('#nel').posFour();
		$('#nel').posFive();
		$('#nel').posSix();
		
		return false;
	}
	
	//function for position one
	$.fn.posOne = function(){
		$(this).animate({'height':'300px','width':'210px','top':'0px','left':'345px'},1500, 'linear',
						function(){
							$(this).css({'z-index':'70'});
							var mag = $(this).attr('id');
							if(mag == 'lif')
							{
								growMags();
							}
							return false;
						});
		return false;
	}
	
	//function for position two
	$.fn.posTwo = function(){
		$(this).animate({'height':'251px','width':'175px','top':'24px','left':'169px'},1500, 'linear',
						function(){
							$(this).css({'z-index':'60'});
							return false;
						});		
		return false;
	}	
	
	//function for position three
	$.fn.posThree = function(){
		$(this).animate({'height':'241px','width':'168px','top':'30px','left':'0px'},1500, 'linear',
						function(){
							$(this).css({'z-index':'50'});
							return false;
						});		
		return false;
	}
	
	//function for position four
	$.fn.posFour = function(){
		$(this).animate({'height':'231px','width':'160px','top':'35px','left':'-161px'},1500, 'linear',
						function(){
							$(this).css({'z-index':'30'});
							return false;
						});		
		return false;
	}
	
	//function for position five
	$.fn.posFive = function(){
		$(this).animate({'height':'231px','width':'160px','top':'35px','left':'901px'},1500, 'linear',
						function(){
							$(this).css({'z-index':'40'});
							return false;
						});		
		return false;
	}
	
	//function for position six
	$.fn.posSix = function(){
		$(this).animate({'height':'241px','width':'168px','top':'30px','left':'732px'},1500, 'linear',
						function(){
							$(this).css({'z-index':'50'});
							return false;
						});		
		return false;
	}
	
	//function for position seven
	$.fn.posSeven = function(){
		$(this).animate({'height':'251px','width':'175px','top':'24px','left':'556px'},1500, 'linear',
						function(){
							$(this).css({'z-index':'60'});
							return false;
						});		
		return false;
	}
		
	//pre load the magazine covers
	var lif = new Image();
	var ci = new Image();
	var fd = new Image();
	var pw = new Image();
	var ad = new Image();
	var nel = new Image();
	var hg = new Image();
	
	$(lif).load(function(){
			$(this).hide();
			$('#lif').append(this);
			$(this).fadeIn();
			return false;
		}).attr('src','images/lif.jpg');
			
	$(ci).load(function(){
			$(this).hide();
			$('#ci').append(this);
			$(this).fadeIn();
			return false;
		}).attr('src','images/ci.jpg');
			
	$(fd).load(function(){
			$(this).hide();
			$('#fd').append(this);
			$(this).fadeIn();
			return false;
		}).attr('src','images/fd.jpg');
			
	$(pw).load(function(){
			$(this).hide();
			$('#pw').append(this);
			$(this).fadeIn();
			return false;
		}).attr('src','images/pw.jpg');
	
	$(ad).load(function(){
			$(this).hide();
			$('#ad').append(this);
			$(this).fadeIn();
			return false;
		}).attr('src','images/ad.jpg');
	
	$(nel).load(function(){
			$(this).hide();
			$('#nel').append(this);
			$(this).fadeIn();
			return false;
		}).attr('src','images/nel.jpg');

	$(hg).load(function(){
			$(this).hide();
			$('#hg').append(this);
			$(this).fadeIn();
			growMags();
			return false;;
		}).attr('src','images/hg.jpg');
	//function to load top images
	function loadTops()
	{
		var graphic = new Image();
		$(graphic).load(function(){
				$(this).hide();
				$('#graphicCont').append(this).removeClass('loader');
				$(this).fadeIn();
				return false;
			}).attr('src','images/graphicsTopImage.png');
		
		var tca = new Image();
		$(tca).load(function(){
				$(this).hide();
				$('#tcaCont').append(this).removeClass('loader');
				$(this).fadeIn();
				return false;
			}).attr('src','images/tcaTopImage.png');
		
		var eMark = new Image();
		$(eMark).load(function(){
				$(this).hide();
				$('#emarkCont').append(this).removeClass('loader');
				$(this).fadeIn();
				return false;
			}).attr('src','images/emarkTopImage.png');
		
		var pr = new Image();
		$(pr).load(function(){
				$(this).hide();
				$('#prCont').append(this).removeClass('loader');
				$(this).fadeIn();
				return false;
			}).attr('src','images/prTopImage.png');
		
		var wmw = new Image();
		$(wmw).load(function(){
				$(this).hide();
				$('#wmwCont').append(this).removeClass('loader');
				$(this).fadeIn();
				return false;
			}).attr('src','images/wmwTopImage.png');
		
		var test = new Image();
		$(test).load(function(){
				$(this).hide();
				$('#testCont').append(this).removeClass('loader');
				$(this).fadeIn();
				return false;
			}).attr('src','images/testimonialsTopImage.png');
		
		var subIm = new Image();
		$(subIm).load(function(){
				$(this).hide();
				$('#subCont').append(this).removeClass('loader');
				$(this).fadeIn();
				return false;
			}).attr('src','images/subscribeTopImage.png');
		
		var jobs = new Image();
		$(jobs).load(function(){
				$(this).hide();
				$('#jobsCont').append(this).removeClass('loader');
				$(this).fadeIn();
				return false;
			}).attr('src','images/jobsTopImage.png');
		
		var cont = new Image();
		$(cont).load(function(){
				$(this).hide();
				$('#contactsCont').append(this).removeClass('loader');
				$(this).fadeIn();
				return false;
			}).attr('src','images/contactTopImage.png');
		
		return false;
	}
	//fire the preloaders	
	loadTops();
	//attach functions to move top window on navigation button press.
	$('#graphicPress').click(function(){
						$('#topSectionScroll').animate({'left':'-900px'});
						$('#textSectionScroll').animate({'left':'-900px'});
						return false;
						});
	$('#header, #home').click(function(){
						$('#topSectionScroll').animate({'left':'0px'});
						$('#textSectionScroll').animate({'left':'0px'});
						return false;
						});
	$('#tca').click(function(){
						$('#topSectionScroll').animate({'left':'-1800px'});
						$('#textSectionScroll').animate({'left':'-1800px'});
						return false;
						});
	$('#emarket').click(function(){
						$('#topSectionScroll').animate({'left':'-2700px'});
						$('#textSectionScroll').animate({'left':'-2700px'});
						return false;
						});
	$('#pr').click(function(){
						$('#topSectionScroll').animate({'left':'-3600px'});
						$('#textSectionScroll').animate({'left':'-3600px'});
						return false;
						});
	$('#wmw').click(function(){
						$('#topSectionScroll').animate({'left':'-4500px'});
						$('#textSectionScroll').animate({'left':'-4500px'});
						return false;
						});
	$('#testimo').click(function(){
						$('#topSectionScroll').animate({'left':'-5400px'});
						$('#textSectionScroll').animate({'left':'-5400px'});
						return false;
						});
	$('#subs').click(function(){
						$('#topSectionScroll').animate({'left':'-6300px'});
						$('#textSectionScroll').animate({'left':'-6300px'});
						return false;
						});
	$('#jobs').click(function(){
						$('#topSectionScroll').animate({'left':'-7200px'});
						$('#textSectionScroll').animate({'left':'-7200px'});
						return false;
						});
	$('#contact').click(function(){
						$('#topSectionScroll').animate({'left':'-8100px'});
						$('#textSectionScroll').animate({'left':'-8100px'});
						return false;
						});
	//function to show click magazine box
	$('#caroCont').hover(
		function(){
			$('#ins').css({'opacity':'0.8'});
			$('#ins').fadeIn();
			return false;
		},
		function(){
			$('#ins').fadeOut();
			return false;
		}
	);
	//functions to link clicking covers to websites
	$('#lif').click(function(){
		window.open("http://www.lincolnshireinfocus.co.uk");
		return false;
	});
	$('#ci').click(function(){
		window.open("http://www.commerceandindustryonline.co.uk");
		return false;
	});
	$('#fd').click(function(){
		window.open("http://foodanddrinknetworkukonline.co.uk");
		return false;
	});
	$('#pw').click(function(){
		window.open("http://www.theperfectweddingonline.co.uk");
		return false;
	});
	$('#ad').click(function(){
		window.open("http://www.theadvertisernel.com");
		return false;
	});
	$('#hg').click(function(){
		window.open("http://www.thehumberestuaryguideonline.co.uk");
		return false;
	});
	$('#nel').click(function(){
		window.open("http://www.northeastlincscountyguide.co.uk");
		return false;
	});
	//send subscription form to PHP
	$('#subButton').click(function(){
		//set some variables
		var name = $('#nameInp').attr('value');
		var company = $('#companyInp').attr('value');
		var address = $('#addressInp').attr('value');
		var postcode = $('#postInp').attr('value');
		var tel = $('#telInp').attr('value');
		var whichMag = "Selected: ";
		if($('#ciTick').attr('checked') == "checked")
		{
			whichMag = whichMag+"Commerce and Industry, ";
		}
		if($('#fdTick').attr('checked') == "checked")
		{
			whichMag = whichMag+"Food and Drink, ";
		}
		if($('#lifTick').attr('checked') == "checked")
		{
			whichMag = whichMag+"Lincs in Focus, ";
		}
		var errors = 0;
		var errorType = "errors: ";
		//make sure all elements are filled in
		if(name == "")
		{
			errors ++;
			errorType = errorType +"Please enter a Name, ";
		}
		if(company == "")
		{
			errors ++;
			errorType = errorType +"Please enter a Company, ";
		}
		if(address == "")
		{
			errors ++;
			errorType = errorType +"Please enter an Address, ";
		}
		if(postcode == "")
		{
			errors ++;
			errorType = errorType +"Please enter a Postcode, ";
		}
		if(tel == "")
		{
			errors ++;
			errorType = errorType +"Please enter a Telephone Number, ";
		}
		if(whichMag == "Selected: ")
		{
			errors ++;
			errorType = errorType +"Please select which magazines you wish to subscribe to, ";
		}
		if(errors > 0)
		{
			alert(errorType);
		}
		else
		{
			var message = "A NEW SUBSCRIPTION\r\n\r\nName: "+name+"\r\nCompany name: "+company+"\r\nAddress: "+address+"\r\nPostcode: "+postcode+"\r\nTelephone: "+tel+"\r\nI wish to subscribe to "+whichMag;
			$.ajax({
				type: "POST",
				url: "sendEnquiry.php",
				data: "message="+message,
				success: function(data){
					alert(data);
				}
			})			
		}
		return false;					   
	});
});
