$(document).ready(function(){
	
	//global
	//$(document).pngFix({blankgif:'templatePath+'/assets/images/spacer.gif'});
	$('a.email').emailProtection();
	$('a.in-page').scroller();
	$('a.zoom').shadowBox({icon: templatePath+'/assets/images/icon/zoom.gif'});
	
	//home
	if($('#home').length > 0) {
		
		$('#home img').opacityHover();
	}
	
	//heritage
	if($('#heritage').length > 0) {
		
		$('#heritage-year-anchor').remove();
		$('#heritage-year .item').heritageCollapse();
	};
	
	//sign up form
	if($('#sign-up').length > 0) {
		
		$('#sign-up input, #sign-up textarea')
			.focus(function(){ $(this).addClass('focus'); })
			.blur(function(){ $(this).removeClass('focus'); });
		
		$('#sign-up button').hover(function(){ $(this).addClass('hover'); }, function(){ $(this).removeClass('hover'); });
	};
	
	if($('#front-page-grid').length > 0) {
		
		$('.section-image').each(function(){
			$(this).bind('mouseenter mouseleave', function() {
				 //$('.section-des',this).slideToggle('slow');
				if($('.section-des',this).height() == 10){
				 $('.section-des',this).stop().animate({ 
				    height: '50'
				    
				  }, 400 );
				} else {
					 $('.section-des',this).stop().animate({ 
						    height: '10'
						    
						  }, 400 );
				}
			});
		})/*
			
			
			
			 $('.section-des',this).toggle(
					   function()
					   {
					      $('#cdinfo').animate({height: "5"}, 1500);
					   },
					   function()
					   {
					      $('#cdinfo').animate({width: "0"}, 1500);
					   });

		});
		
		*/

	
	};	
		
	//from frome popup swf
	if($('#from-frome-book').length > 0) {
		
		$('#from-frome-book').click(function(){
			
			Shadowbox.open({
				player: 'html',
				title: 'From Frome - A book by Butler Tanner &amp; Dennis',
				content: '',
				width: 950,
				height: 775,
				options: {
					onFinish: function(){
						var flashvars = {
							xmlConfig: templatePath+'/assets/swf/config.xml'
						};
						var params = {
							movie: templatePath+'/assets/swf/flippingBook.swf',
							bgcolor: '#000000',
							scale: 'scale',
							quality: 'high'
						};
						var attributes = {};

						swfobject.embedSWF(templatePath+'/assets/swf/flippingBook.swf', "shadowbox_content", '950', '775', '8',templatePath+'/assets/swf/expressInstall.swf', flashvars, params, attributes);
					}
				}
			});
			
			return false;
		});
	}
	
	//google maps
	if($('#contact').length > 0) {
		
		$('#googleFrome').find('a').googleMap({location:'Somerset',lat:51.224733,lng:-2.312257});
		$('#googleLondon').find('a').googleMap({location:'London',lat:51.512522,lng:-0.138864});
	}
});

(function($){
	
	$.fn.googleMap = function(options){
	
		var options = $.extend({location:'',lat:'',lng:''}, options);
		
		return this.each(function(){
			
			$(this).click(function(){

				var address = (options.location == 'London') ? "9-11 Kingly Street<br />London<br />W1B 5PN" : "Caxton Road Frome<br />Somerset<br />BA11 1NF";
				
				Shadowbox.open({
				
					player: 'html',
					content: '',
					height: 300,
					width: 500,
					options: {
						onFinish: function(item){
							if(GBrowserIsCompatible()){
		
								var map = new GMap2(document.getElementById('shadowbox_content'));
								map.addControl(new GSmallMapControl());
								map.addControl(new GMapTypeControl());
								
								var point = new GLatLng(options.lat,options.lng);
								map.setCenter(point, 15);
								
								var marker = new GMarker(point);
								map.addOverlay(marker);
								marker.openInfoWindowHtml("<div id=\"googleAddress\">Butler Tanner &amp; Dennis<br />"+address+"</div>");
							}
						},
						onClose: GUnload
					}
				});
				
				return false;
			});
		});
	};
	
	/**
	* --------------------------------------------------------------------
	* jQuery-Plugin "hoverClass"
	*
	* Quick replacement for hovering elements.
	* --------------------------------------------------------------------
	* @package Core
	* @author Atif Qureshi <atif@opx.co.uk>
	* --------------------------------------------------------------------
	*/
	$.fn.hoverClass = function(options){
	
		var options = $.extend({classname:'hover'}, options);
		
		return this.each(function(){
		
			$(this).hover(function(){ $(this).addClass(options.classname); }, function(){ $(this).removeClass(options.classname); });
		});
	};
		  
	/**
	* --------------------------------------------------------------------
	* jQuery-Plugin "scroller"
	*
	* Scrolls to the matching element given through by link hash.
	* --------------------------------------------------------------------
	* @package Core
	* @author Atif Qureshi <atif@opx.co.uk>
	* --------------------------------------------------------------------
	*/
	$.fn.scroller = function(){
		
		return this.each(function(){

			var hash = $(this).attr('href');
		
			if(hash.substring(0, 1) == '#' && $(hash).length > 0){
				
				$(this).click(function(){
									   
					$.scrollTo(hash, 800);
					return false;
				});
			}
		});
	};
	
	
	/**
	* --------------------------------------------------------------------
	* jQuery-Plugin "shadowBox"
	*
	* Works independently for Shadowbox utility creating a rollover state.
	* --------------------------------------------------------------------
	* @package Core
	* @author Atif Qureshi <atif@opx.co.uk>
	*
	* @param string separator
	* --------------------------------------------------------------------
	*/
	$.fn.shadowBox = function(options){
	
		var options = $.extend({icon:'',classname:'zoom-icon'}, options);
		
		return this.each(function(){
			
			var $img = $(this).find('img');
			
			$(this)
				.hover(function(){
				
					var zoom = $('<img>').attr('src', options.icon).addClass(options.classname).css({left:$img.width() + 'px'}).hide();
					
					$img.after(zoom);
					$img.stop().fadeTo('fast', 0.5).next().fadeIn('fast');
				
				},function(){
					
					$img.stop().fadeTo('normal', 1.0);
					$img.next().stop().fadeOut('fast', function(){ $(this).remove(); });
			});
				
		});
	};
	
	
	/**
	* --------------------------------------------------------------------
	* jQuery-Plugin "emailProtection"
	*
	* Replaces domain separator with custom character(s) for protection
	* against spam robots.
	* --------------------------------------------------------------------
	* @package Core
	* @author Atif Qureshi <atif@opx.co.uk>
	*
	* @param string separator
	* --------------------------------------------------------------------
	*/
	$.fn.emailProtection = function(options){
	
		var options = $.extend({separator:'[at]'}, options);
		
		return this.each(function(){
		
			var e = this.href.replace(options.separator, '@');
			var t = this.href.replace('mailto:', '');
			
			if(t == $(this).text()) {
			
				$(this).text(e.replace('mailto:', ''));
			};
			
			this.href = e;
		});
	};
	
	
	/**
	* --------------------------------------------------------------------
	* jQuery-Plugin "opactiyHover"
	*
	* Applies opacity animation on elements mainly images.
	* --------------------------------------------------------------------
	* @package Core
	* @author Atif Qureshi <atif@opx.co.uk>
	*
	* @param int start
	* @param int end
	* @param int startLength
	* @param int endLength
	* --------------------------------------------------------------------
	*/
	$.fn.opacityHover = function(options){
	
		opacityHover = {
		
			defaults : {
				fade_to: 0.5,
				fade_back: 1.0,
				speed: 'normal'
			},
			
			doMouseOver : function(){
			
				$(this).stop().fadeTo(options.speed, options.fade_to);
			},
			
			doMouseOut : function(){
			
				$(this).stop().fadeTo(options.speed, options.fade_back);
			}
		};
		
		var options = $.extend({}, opacityHover.defaults, options);
		
		return this.each(function(){
		
			$(this).hover(opacityHover.doMouseOver, opacityHover.doMouseOut);
		});
	};
	

	/**
	 * --------------------------------------------------------------------
	 * jQuery-Plugin "heritageCollapse"
	 *
	 * Toggles collapsable elements with a scroll effect.
	 * --------------------------------------------------------------------
	 * @package Core
	 * @author Atif Qureshi <atif@opx.co.uk>
	 *
	 * @param string iterator
	 * @param string trigger
	 * @param string collapsable
	 * --------------------------------------------------------------------
	 */
	$.fn.heritageCollapse = function(options){
		
		heritageCollapse = {
			
			defaults : {
				iterator: '.item',
				trigger: 'h3',
				collapsable: '.collapsable'
			},
			
			toggleDetail : function(el){
				
				var $collapsable = el.find(options.collapsable);
				var $container = el.parent().find(options.iterator);
					
				$container.each(function(){
					
					var $loopItem = $(this);
					
					if($loopItem.attr('id') == el.attr('id')){
						
						if($collapsable.css('display') == 'none') {
							
							if(!el.hasClass('active')) el.addClass('active');
							
							$collapsable.slideDown('normal', function(){
								
								$.scrollTo('#'+el.attr('id'), 800);
							});
						} else {
							
							if(el.hasClass('active')) el.removeClass('active');
							
							$collapsable.slideUp();
						}
					} else {
						
						$loopItem.removeClass('active').find(options.collapsable).slideUp();	
					}
				});
			},
			
			doMouseEnter : function(){
				
				$(this).addClass('hover');
			},
			
			doMouseLeave : function(){
				
				$(this).removeClass('hover');
			}
		};
		
		var options = $.extend({}, heritageCollapse.defaults, options);
		
		return this.each(function(){
			
			var $container2 = $(this);
			
			$(this)
				.find(options.trigger)
					.hover(heritageCollapse.doMouseEnter, heritageCollapse.doMouseLeave)
					.click(function(){ heritageCollapse.toggleDetail($container2); }).end()
				.find(options.collapsable).hide();
		});
	};
	
	
	/**
	 * --------------------------------------------------------------------
	 * jQuery-Plugin "pngFix"
	 * Version: 1.1, 11.09.2007
	 * by Andreas Eberhard, andreas.eberhard@gmail.com
	 *                      http://jquery.andreaseberhard.de/
	 *
	 * Copyright (c) 2007 Andreas Eberhard
	 * Licensed under GPL (http://www.opensource.org/licenses/gpl-license.php)
	 * --------------------------------------------------------------------
	 * @example $(function(){$(document).pngFix();});
	 * @desc Fixes all PNG's in the document on document.ready
	 *
	 * jQuery(function(){jQuery(document).pngFix();});
	 * @desc Fixes all PNG's in the document on document.ready when using noConflict
	 *
	 * @example $(function(){$('div.examples').pngFix();});
	 * @desc Fixes all PNG's within div with class examples
	 *
	 * @example $(function(){$('div.examples').pngFix( { blankgif:'ext.gif' } );});
	 * @desc Fixes all PNG's within div with class examples, provides blank gif for input with png
	 * --------------------------------------------------------------------
	 */
	$.fn.pngFix = function(settings) {

		// Settings
		settings = $.extend({blankgif: 'blank.gif'}, settings);
	
		var ie55 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 5.5") != -1);
		var ie6 = (navigator.appName == "Microsoft Internet Explorer" && parseInt(navigator.appVersion) == 4 && navigator.appVersion.indexOf("MSIE 6.0") != -1);
	
		if ($.browser.msie && (ie55 || ie6)) {
	
			//fix images with png-source
			$(this).find("img[@src$=.png]").each(function() {
	
				$(this).attr('width',$(this).width());
				$(this).attr('height',$(this).height());
	
				var prevStyle = '';
				var strNewHTML = '';
				var imgId = ($(this).attr('id')) ? 'id="' + $(this).attr('id') + '" ' : '';
				var imgClass = ($(this).attr('class')) ? 'class="' + $(this).attr('class') + '" ' : '';
				var imgTitle = ($(this).attr('title')) ? 'title="' + $(this).attr('title') + '" ' : '';
				var imgAlt = ($(this).attr('alt')) ? 'alt="' + $(this).attr('alt') + '" ' : '';
				var imgAlign = ($(this).attr('align')) ? 'float:' + $(this).attr('align') + ';' : '';
				var imgHand = ($(this).parent().attr('href')) ? 'cursor:hand;' : '';
				if (this.style.border) {
					prevStyle += 'border:'+this.style.border+';';
					this.style.border = '';
				};
				if (this.style.padding) {
					prevStyle += 'padding:'+this.style.padding+';';
					this.style.padding = '';
				};
				if (this.style.margin) {
					prevStyle += 'margin:'+this.style.margin+';';
					this.style.margin = '';
				};
				var imgStyle = (this.style.cssText);
	
				strNewHTML += '<span '+imgId+imgClass+imgTitle+imgAlt;
				strNewHTML += 'style="position:relative;white-space:pre-line;display:inline-block;background:transparent;'+imgAlign+imgHand;
				strNewHTML += 'width:' + $(this).width() + 'px;' + 'height:' + $(this).height() + 'px;';
				strNewHTML += 'filter:progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + $(this).attr('src') + '\', sizingMethod=\'scale\');';
				strNewHTML += imgStyle+'"></span>';
				if (prevStyle != ''){
					strNewHTML = '<span style="position:relative;display:inline-block;'+prevStyle+imgHand+'width:' + $(this).width() + 'px;' + 'height:' + $(this).height() + 'px;'+'">' + strNewHTML + '</span>';
				};
	
				$(this).hide();
				$(this).after(strNewHTML);
	
			});
	
			// fix css background pngs
			$(this).find("*").each(function(){
				var bgIMG = $(this).css('background-image');
				if(bgIMG.indexOf(".png")!=-1){
					var iebg = bgIMG.split('url("')[1].split('")')[0];
					$(this).css('background-image', 'none');
					$(this).get(0).runtimeStyle.filter = "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='" + iebg + "',sizingMethod='scale')";
				}
			});
			
			//fix input with png-source
			$(this).find("input[@src$=.png]").each(function() {
				var bgIMG = $(this).attr('src');
				$(this).get(0).runtimeStyle.filter = 'progid:DXImageTransform.Microsoft.AlphaImageLoader' + '(src=\'' + bgIMG + '\', sizingMethod=\'scale\');';
				$(this).attr('src', settings.blankgif);
			});
		
		}
	};
	
})(jQuery);
