/**
 * lh.js
 * @author Lewis Howles (lh)
 *
 * Default niceties and other Voodoo.
 */

var lh = {
	init : function() {
		lh.externalLinks();
		lh.highlighter();
		lh.isiDevice() && $('html').addClass('iDevice');

		if ($('#design-main-link').length) {
			$('#design-main-link, #design-prices-link, #logo-design-link, #artwork-checking-link, #file-supply-guides-link').each(function() {
				$(this).removeAttr('href');
			});

			$('#column-left').delegate('#design-main-link, #design-prices-link, #logo-design-link, #artwork-checking-link, #file-supply-guides-link', 'click', function(e) {
				$('.toggle-section').hide();
				$('#'+this.id.replace('-link', '')).fadeIn();
				$('#column-left, #column-centre, #column-right').height('auto');
				$('#content').children().equalHeights();
			});
		}

		$('#column-right-portfolio').length && lh.portfolio();

		var columnRightPortfolio = $('#column-right-portfolio');

		if (columnRightPortfolio.length) {
			columnRightPortfolio.find('li').filter(':nth-child(3n)').addClass('third');
		}

		$('#map-one-canvas').length && lh.maps();
		$('#map-two-canvas').length && lh.maps2();

		$('#content').children().equalHeights();

		$('#testimonials').innerfade({
			speed: 1e3,
			timeout: 12e3,
			containerheight: '300px'
		});
	},

	/*
	 * Set target blank on external links
	 */
	externalLinks : function() {
		$('a[rel~="external"]')
			.attr('target', '_blank');
	},

	highlighter : function() {
		$('.fancy')
			.delegate('input, select, textarea', 'mouseover focusin mouseout focusout', function(e) {
				var isIn = (e.type === 'mouseover' || e.type === 'focusin');

				lh.toggleFocus($(this), isIn);
			});
	},

	toggleFocus : function($this, modifier) {
		$this.parent('li').not('.submit').toggleClass('focus', modifier);
	},

	portfolio : function() {
		var portfolioBox = $('#column-right-portfolio');

		portfolioBox.find('a').colorbox({
			maxHeight : '90%'
		});

		$('#portfolio-nav').find('a').click(function() {
			var $this = $(this);

			portfolioBox.find('ul').filter(':visible').fadeOut(function(){
				portfolioBox.height('auto').find($this.attr('href')).fadeIn();
				$('#content').children().equalHeights();
			});
		});
	},

	maps : function() {
		// Set a new map and set the lat and long
		var latlng = new google.maps.LatLng(53.69000, -2.32822);
			// Set map options
			var myOptions = {
			zoom: 15,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		var map = new google.maps.Map(document.getElementById("map-one-canvas"), myOptions);

		// Create a new map marker
		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title: "2 The Parade, Broadway, Helmshore, Rossendale, Lancashire, BB4 4HD"
		});

		// Creating an InfoWindow object
		var infowindow = new google.maps.InfoWindow({
			maxWidth: 500,
			content: "2 The Parade, Broadway, Helmshore, Rossendale, Lancashire, BB4 4HD"
		});

		// Open the infowindow
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map, marker);
		});
	},

	maps2 : function() {
		// Set a new map and set the lat and long
		var latlng = new google.maps.LatLng(53.75346, -2.36751);
			// Set map options
			var myOptions = {
			zoom: 15,
			center: latlng,
			mapTypeId: google.maps.MapTypeId.ROADMAP
		};

		var map = new google.maps.Map(document.getElementById("map-two-canvas"), myOptions);

		// Create a new map marker
		var marker = new google.maps.Marker({
			position: latlng,
			map: map,
			title: "113a Blackburn Road, Accrington, Lancashire, BB5 1JJ"
		});

		// Creating an InfoWindow object
		var infowindow = new google.maps.InfoWindow({
			maxWidth: 500,
			content: "113a Blackburn Road, Accrington, Lancashire, BB5 1JJ"
		});

		// Open the infowindow
		google.maps.event.addListener(marker, 'click', function() {
			infowindow.open(map, marker);
		});
	},

	isiPhone : function() {
		var agent = navigator.userAgent.toLowerCase();
		return agent.match(/iPhone/i);
	},

	isiPad : function() {
		var agent = navigator.userAgent.toLowerCase();
		return agent.match(/iPad/i);
	},

	isiDevice : function() {
		return (lh.isiPhone() || lh.isiPad());
	}
}

$(function(){
	lh.init();
});

