/*!
 * @name Force9
 * @version 1/09/2011
 * @author Binary Vein Digital Media [www.binaryvein.com]
 */
/** 
 * @plugin cyclelite
 * @url http://jquery.malsup.com/cycle/lite/
 */
(function(a){
	function d(a,b,d){var e=a[0].parentNode,f=e.cycleTimeout;if(f){clearTimeout(f);e.cycleTimeout=0}b.nextSlide=b.currSlide+d;if(b.nextSlide<0){b.nextSlide=a.length-1}else if(b.nextSlide>=a.length){b.nextSlide=0}c(a,b,1,d>=0);return false}function c(b,d,e,f){if(d.busy)return;var g=b[0].parentNode,h=b[d.currSlide],i=b[d.nextSlide];if(g.cycleTimeout===0&&!e)return;if(e||!g.cyclePause){if(d.before.length)a.each(d.before,function(a,b){b.apply(i,[h,i,d,f])});var j=function(){if(a.browser.msie)this.style.removeAttribute("filter");a.each(d.after,function(a,b){b.apply(i,[h,i,d,f])})};if(d.nextSlide!=d.currSlide){d.busy=1;a.fn.cycle.custom(h,i,d,j)}var k=d.nextSlide+1==b.length;d.nextSlide=k?0:d.nextSlide+1;d.currSlide=k?b.length-1:d.nextSlide-1}if(d.timeout)g.cycleTimeout=setTimeout(function(){c(b,d,0,!d.rev)},d.timeout)}var b="Lite-1.3";a.fn.cycle=function(b){return this.each(function(){b=b||{};if(this.cycleTimeout)clearTimeout(this.cycleTimeout);this.cycleTimeout=0;this.cyclePause=0;var e=a(this);var f=b.slideExpr?a(b.slideExpr,this):e.children();var g=f.get();if(g.length<2){window.console&&console.log("terminating; too few slides: "+g.length);return}var h=a.extend({},a.fn.cycle.defaults,b||{},a.metadata?e.metadata():a.meta?e.data():{});var i=a.isFunction(e.data)?e.data(h.metaAttr):null;if(i)h=a.extend(h,i);h.before=h.before?[h.before]:[];h.after=h.after?[h.after]:[];h.after.unshift(function(){h.busy=0});var j=this.className;h.width=parseInt((j.match(/w:(\d+)/)||[])[1])||h.width;h.height=parseInt((j.match(/h:(\d+)/)||[])[1])||h.height;h.timeout=parseInt((j.match(/t:(\d+)/)||[])[1])||h.timeout;if(e.css("position")=="static")e.css("position","relative");if(h.width)e.width(h.width);if(h.height&&h.height!="auto")e.height(h.height);var k=0;f.css({position:"absolute",top:0,left:0}).each(function(b){a(this).css("z-index",g.length-b)});a(g[k]).css("opacity",1).show();if(a.browser.msie)g[k].style.removeAttribute("filter");if(h.fit&&h.width)f.width(h.width);if(h.fit&&h.height&&h.height!="auto")f.height(h.height);if(h.pause)e.hover(function(){this.cyclePause=1},function(){this.cyclePause=0});var l=a.fn.cycle.transitions[h.fx];l&&l(e,f,h);f.each(function(){var b=a(this);this.cycleH=h.fit&&h.height?h.height:b.height();this.cycleW=h.fit&&h.width?h.width:b.width()});if(h.cssFirst)a(f[k]).css(h.cssFirst);if(h.timeout){if(h.speed.constructor==String)h.speed={slow:600,fast:200}[h.speed]||400;if(!h.sync)h.speed=h.speed/2;while(h.timeout-h.speed<250)h.timeout+=h.speed}h.speedIn=h.speed;h.speedOut=h.speed;h.slideCount=g.length;h.currSlide=k;h.nextSlide=1;var m=f[k];if(h.before.length)h.before[0].apply(m,[m,m,h,true]);if(h.after.length>1)h.after[1].apply(m,[m,m,h,true]);if(h.click&&!h.next)h.next=h.click;if(h.next)a(h.next).bind("click",function(){return d(g,h,h.rev?-1:1)});if(h.prev)a(h.prev).bind("click",function(){return d(g,h,h.rev?1:-1)});if(h.timeout)this.cycleTimeout=setTimeout(function(){c(g,h,0,!h.rev)},h.timeout+(h.delay||0))})};a.fn.cycle.custom=function(b,c,d,e){var f=a(b),g=a(c);g.css(d.cssBefore);var h=function(){g.animate(d.animIn,d.speedIn,d.easeIn,e)};f.animate(d.animOut,d.speedOut,d.easeOut,function(){f.css(d.cssAfter);if(!d.sync)h()});if(d.sync)h()};a.fn.cycle.transitions={fade:function(a,b,c){b.not(":eq(0)").hide();c.cssBefore={opacity:0,display:"block"};c.cssAfter={display:"none"};c.animOut={opacity:0};c.animIn={opacity:1}},fadeout:function(b,c,d){d.before.push(function(b,c,d,e){a(b).css("zIndex",d.slideCount+(e===true?1:0));a(c).css("zIndex",d.slideCount+(e===true?0:1))});c.not(":eq(0)").hide();d.cssBefore={opacity:1,display:"block",zIndex:1};d.cssAfter={display:"none",zIndex:0};d.animOut={opacity:0}}};a.fn.cycle.ver=function(){return b};a.fn.cycle.defaults={animIn:{},animOut:{},fx:"fade",after:null,before:null,cssBefore:{},cssAfter:{},delay:0,fit:0,height:"auto",metaAttr:"cycle",next:null,pause:0,prev:null,speed:1e3,slideExpr:null,sync:1,timeout:4e3}
})(jQuery);

var Force9Global = {
		map : {},
		windfarms : {
			current : {
				blackwood : {
						lat : 55.1745775209,
						long : -3.6894257651,
						name : 'Blackwood',
						status : 'In Scoping. Met Mast Application made',
						url : '/projects/current/blackwood/',
						zoom : 6
					},
					deuchries : {
						lat : 57.5791092421,
						long : -2.6036397707,
						name : 'Deuchries',
						status : 'In Scoping. Met Mast Application consented',
						url : '/projects/current/deuchries/',
						zoom : 6
					},
					dorcas : {
						lat : 51.9482017782,
						long : -0.7355938799,
						name : 'Dorcas Lane',
						status : 'In Scoping. Met Mast Application made',
						url : '/projects/current/dorcas-lane/',
						zoom : 6
					},
					andershaw : {
						lat : 55.5095640252,
						long : -3.8304384806,
						name : 'Andershaw',
						status : 'Planning application currently being considered',
						url : '/projects/current/andershaw/',
						zoom : 6
					},
					mull : {
						lat : 56.4309790105,
						long : -3.8096149848,
						name : 'Mull Hill',
						status : 'In Scoping. Met Mast Application made',
						url : '/projects/current/mull-hill/',
						zoom : 6
					},
					berryburn : {
						lat : 57.4858629880,
						long : -3.5361944214,
						name : 'Berryburn',
						status : 'To enter construction 2012',
						url : '/projects/current/berryburn/',
						zoom : 6
					},
					barmoor : {
						lat : 55.6355599678,
						long : -2.0174461506,
						name : 'Barmoor',
						status : 'To enter construction 2012',
						url : '/projects/current/barmoor/',
						zoom : 6
					}
			},
			completed : {
				alltwalis : {
					lat : 51.9787009039,
					long : -4.2363818410,
					name : 'Alltwalis',
					status : 'Operational',
					url : '/projects/completed/alltwalis/',
					zoom : 6
				},
				markhill : {
					lat : 55.1193974129,
					long : -4.7456940611,
					name : 'Markhill',
					status : 'Operational',
					url : '/projects/completed/markhill/',
					zoom : 6
				}
			}
		},
		facts : ["Wind power is completely renewable. Using renewable energy makes our current lifestyle more sustainable and helps to preserve a high standard of living for generations to come.", 
		"Wind power produces no harmful waste such as carbon dioxide and offsets the emissions of generating electricity from other energy sources.", 
		"Wind energy is considered to be a reliable, cost-effective, pollution free and secure source of energy.", "Wind energy is compatible with other land uses and can serve as a boost for rural economic development.", "Wind energy costs are low and stable for consumers especially at a time when oil and gas prices are at an all time high."],
		loadMap : function (el, centerLat, centerLong, zoom) {
			zoom = typeof zoom != 'undefined' ? zoom : 6;
			var myMapOptions = {
				zoom: zoom,
				center: new google.maps.LatLng(centerLat,centerLong),
				streetViewControl: false,
				scaleControl: false,
				mapTypeId: google.maps.MapTypeId.TERRAIN,
					mapTypeControl: true,
				mapTypeControlOptions: {
				  style: google.maps.MapTypeControlStyle.DROPDOWN_MENU
				 }
			};
			Force9Global.map = new google.maps.Map(document.getElementById(el),myMapOptions);
		},
		markMap : function (windfarm){
			var url = location.href;
			var image = new google.maps.MarkerImage(
			  '/assets/img/ui/marker.png',
			  new google.maps.Size(37,38),
			  new google.maps.Point(0,0),
			  new google.maps.Point(19,38)
			);
			
			var shadow = new google.maps.MarkerImage(
			  '/assets/img/ui/shadow.png',
			  new google.maps.Size(59,38),
			  new google.maps.Point(0,0),
			  new google.maps.Point(19,38)
			);
		
			var shape = {
			  coord: [36,0,36,1,36,2,36,3,36,4,36,5,36,6,36,7,36,8,36,9,36,10,36,11,36,12,36,13,36,14,36,15,36,16,36,17,36,18,36,19,36,20,36,21,36,22,36,23,24,24,24,25,23,26,23,27,23,28,22,29,22,30,21,31,21,32,21,33,20,34,20,35,19,36,19,37,17,37,17,36,16,35,16,34,15,33,15,32,15,31,14,30,14,29,13,28,13,27,13,26,12,25,12,24,0,23,0,22,0,21,0,20,0,19,0,18,0,17,0,16,0,15,0,14,0,13,0,12,0,11,0,10,0,9,0,8,0,7,0,6,0,5,0,4,0,3,0,2,0,1,0,0,36,0],
			  type: 'poly'
			};

			var marker = new google.maps.Marker({
				draggable: false,
				raiseOnDrag: false,
				icon: image,
				shadow: shadow,
				shape: shape,
				map: Force9Global.map,
				position: new google.maps.LatLng(windfarm.lat, windfarm.long)
			});

			var myInfoWindowOptions = {
				content: '<div class="info-window-content"><h4>' + windfarm.name + '</h4><p>' + windfarm.status + '</p><p><a href="'+ windfarm.url + '">view project</a></p></div>',
				maxWidth: 275
			};

			var infoWindow = new google.maps.InfoWindow(myInfoWindowOptions);

			google.maps.event.addListener(marker, 'click', function() {
				infoWindow.open(Force9Global.map,this);
			});

			google.maps.event.addListener(marker, 'dragstart', function(){
				infoWindow.close();
			});
		},
		prepareCarousel : function(){		
			$('#carousel').cycle({
				slideExpr: 'li',  
				fx: 'custom', 
			  cssBefore: { left: 940 }, 
			  animIn:  { left: 0 }, 
			  animOut: { left: -940 },
				delay:  500,
				speed:  1500
			}); 
		},
		scrollMe : function () {
			$('.top').click(function(e){
				e.preventDefault();
	    	$('html,body').animate({scrollTop: $("#container").offset().top},'slow');
	  	});
		},
		getFact : function () {
			var rand = Math.floor(Math.random()*4);
			$($('.fact p')).append(this.facts[rand]);
		},
		init: function () {
			document.documentElement.className = document.documentElement.className.replace(/\bno-js\b/,'js');
			if($('#carousel').length){
				this.prepareCarousel();
			}
			if($('.fact').length){
				this.getFact();
			}
			if($('#map').length){
				var windfarms = $('.current').length ? this.windfarms.current : this.windfarms.completed;
				this.loadMap("map",54.54658,-2.39502,6);
				for(var i in windfarms){
					this.markMap(windfarms[i]);
				}
				
			}
			if($('.map-holder').length){
				var mapId = $('#small-map').data('map-name'),
				    windfarms = $('.current').length ? this.windfarms.current : this.windfarms.completed,
				    lat = windfarms[mapId].lat, 
				    long = windfarms[mapId].long;
					zoom = windfarms[mapId].zoom;
				this.loadMap("small-map",lat,long,zoom);
				this.markMap(windfarms[mapId]);
			}
			if($('.top').length){
				this.scrollMe();
			}
		}
};

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

