var Site = {
	submitActive : false,
	start : function($)
	{
		/* ********* Show labels over input fields ********  */
		$("label.overlabel").overlabel();	
		/* ********* Add empty element for dropshadow over header image ********  */
		$("#headerImg").append("<i class='topShadow'></i>");
		$("#finder").wrapInner("<div class='border'>" + "</div>");
		/* ********* Fade slider ********  */
		//$("#dirNav").addClass("fader"); // put this back on 	
        $("#dirNav").hide();
		var txt = $("a.showMenu");
        if ($("a.open").size() >= 1) {
        	txt.text('Hide menu');
        	$("#dirNav").html('<div style="height:50px"><img style="position:relative;top:20px;left:100px" src="/assets/images/ui/ajax-loader.gif" /></div>');
            $("#dirNav").show();
        	var url = $("a.showMenu").attr("href");
			var u = url.replace(/m=1/,"m=0");
			url = url.replace(/m=0/,"m=1");

			$(this).attr("href",u);

			$.get("/assets/snippets/processor/navajax.class.inc.php", { m: parsePram('m', url), c1: parsePram('c1', url), c2: parsePram('c2', url), c3: parsePram('c3', url), c4: parsePram('c4', url), ajax:true }, function(data) { $("#dirNav").html(data);});

		} else {
			txt.text('Show menu');
		}
		
                //nav effect start
		$("a.showMenu").click(function(event){
			event.preventDefault();
			$(this).toggleClass("open");
            if ($("a.open").size() >= 1) {
				txt.text('Hide menu');
				$("#dirNav").html('<div style="height:50px"><img style="position:relative;top:20px;left:100px" src="/assets/images/ui/ajax-loader.gif" /></div>');
	            $("#dirNav").show();
			} else {
				txt.text('Show menu');
	            $("#dirNav").slideUp();
			}
			var url = $(this).attr("href");
			var u;
			if (parsePram('m', url) == '0') {
				u = url.replace(/m=0/,"m=1");
			} else {
				u = url.replace(/m=1/,"m=0");
			}

			$(this).attr("href",u);
			$.get("/assets/snippets/processor/navajax.class.inc.php", { m: parsePram('m', url), c1: parsePram('c1', url), c2: parsePram('c2', url), c3: parsePram('c3', url), c4: parsePram('c4', url), ajax:true }, function(data) { $("#dirNav").html(data);});
		});
		/* Bigger link */
		$('.thumb').biggerlink({otherstriggermaster:false});
		$('.searchList li').biggerlink({otherstriggermaster:false});
	
		/* Equal Heights Plugin */
		$('.thumbs').each(function() { 
			$('.thumb', this).equalHeights();
		});
		$(".recent .thumb .shadow-inside").equalHeights();

	
		//AJAX EMAIL SIGN-UP - ASSIGNS BOTH CLICK & ENTER BUTTONS TO THE FUNCTION.

		$("form#frmSubcribe input.submit").click(function(e){e.preventDefault();Site.ajaxSignup($);});

		$("form#frmSubcribe input").keypress(function(e)

		{

			if(e.which == 13) //IF ENTER

			{

				//this REFERS TO CONTEXT ELEMENT, BEING THE SUBMIT BUTTON

				e.preventDefault();

				Site.ajaxSignup($);

			}

		});
		
		if( $("input[type=checkbox]#catalogue").is(":checked") ){
			$('div.contactdetextra').show();
		}
		else{
			$('div.contactdetextra').hide();
		}
		
		$("input[type=checkbox]#catalogue").change(function(event){
			if( $("input[type=checkbox]#catalogue").is(":checked") ){
				$('div.contactdetextra').show();
			}
			else{
				$('div.contactdetextra').hide();
			}

		});
	},

	ajaxSignup : function($)

	{

		if(Site.submitActive == false)

		{

			Site.submitActive = true;

		        var email = $("form#frmSubcribe input#email").val();

			jQuery("form#frmSubcribe").fadeOut('4000', function(){
				jQuery("div#newsletter-form").prepend("<p class=\"headline\">Thank you for subscribing to Cotswold's newsletter.</p>").fadeIn('4000');
			});

		        setTimeout(function() {

			jQuery.ajax({ 

			   type: "POST",

			   url: "assets/snippets/mailbuild/mailbuild_subscribe.php",

			   data: "email=" + email,

			   success: function(msg)

				   {


						jQuery("p.headline").remove();

						jQuery("form#frmSubcribe input#email").val('');

						Site.submitActive = false;


						jQuery("p.headline").remove();

						jQuery("form#frmSubcribe").fadeIn('4000');
	
                                               pageTracker._trackPageview('/goal/newsletter/');

				   },

			     error: function(msg)

					{

						 jQuery("form#frmSubcribe").append("<p class=\"headline\">"+m+"</p>").fadeOut(4000, function(){

							jQuery("p.headline").remove();

							Site.submitActive = false;

							jQuery("form#frmSubcribe").fadeIn();

						 });

					}

				});	

			}, 4000);

		}

	},

	effects: function($) 
	{
		function doAlbum() 
		{
			$("#loading-adv").hide();

			//image and vedio gallery
			if ($("#gallery-adv").size() == 1) {
					// Initialize Advanced Galleriffic Gallery
					var galleryAdv = $('#gallery-adv').galleriffic('#thumbImg', {
						delay:                  2000,
						imageContainerSel:      '#slideshow-adv',
						autoStart:              false,
						onTransitionOut:        function(callback) {
							$('#slideshow-adv').fadeOut('fast', callback);
						},
						onTransitionIn:         function() {
							$('#slideshow-adv').fadeIn('fast');
						}
					});
			}
		}
		doAlbum();
	},

	settings: function($)
	{
		// Popup Links
		jQuery("a.popup").click(
			function(event) {
				event.preventDefault();
				window.open($(this).attr("href"));
			}
		);     

		// Email obfuscator
  		$("dd#email").each(function()
   		{
	   		var email = String($("dd#email").html()).replace(/\s*\(.+\)\s*/, "@");
	   		$("dd#email").html(email);
   		});
	}
}

jQuery(document).ready(function($){
/*   $(window).load( function() {
	  var targetOffset = $('#content').offset().top;
          $('html,body').animate({scrollTop: targetOffset}, 500);
   });*/

	Site.start($);
	Site.settings($);
	Site.effects($);
});

function parsePram(name, url) {
	var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = "[\\?&]"+name+"=([^&#]*)";
	var regex = new RegExp( regexS );
	var results = regex.exec( url);
	if( results == null )
		return "";
	else
		return results[1];
}

function landing(name, url) {
	var name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
	var regexS = name;
	var regex = new RegExp( regexS );
	var results = regex.exec( url);
	if( results == null )
		return false;
	else
		return true;
}


/* ****************** sIFR 3 ******************  */



var baskervilleItalic = { src: 'assets/fonts/new_baskerville_italic_bt-sIFR3.swf' };
var resPublica = { src: 'assets/fonts/res_publica-sIFR3.swf' };


sIFR.fixHover = false;

sIFR.activate(baskervilleItalic);
sIFR.activate(resPublica);


/*
 sIFR replaces elements in the order of `sIFR.replace()` calls. Therefore, make sure to replace the most specific selectors first, before replacing generic selectors. A CSS selector may match multiple elements, sIFR replaces all of them.
*/

sIFR.replace(baskervilleItalic, {
	selector: 	'.specialHeading',
	css: [
		'.sIFR-root { color: #666666; letter-spacing: 0.12; }',
		'.sIFR-root a { color: #666666; letter-spacing: 0.12; text-decoration: none; }',
		'.sIFR-root a:hover { color: #a57c52; letter-spacing: 0.12;  }',
	],	
	tuneHeight: -5,
	offsetTop:	-0,
	wmode: 'transparent'
});

sIFR.replace(baskervilleItalic, {
	selector: 	'.boxHeading',
	css: [
		'.sIFR-root { color: #666666; letter-spacing: 0.08; }',
		'.sIFR-root a { color: #666666; letter-spacing: 0.08; }',
		'.sIFR-root a:hover { color: #a57c52; letter-spacing: 0.08; }',
		'.bl-hover .sIFR-root a { color: #a57c52; letter-spacing: 0.08; }',
	],	
	tuneHeight: -0,
	offsetTop:	-0,
	wmode: 'transparent'
});


sIFR.replace(resPublica, {
	selector: 	'.thumb h3 .boxFeatured',
	css: [
	//	'.sIFR-root { color: #666666; }',
		'.sIFR-root { color: #666666; text-decoration: none; cursor: pointer; }',
		'.sIFR-root:hover { color: #a57c52;  text-decoration: none;  cursor: pointer; }',
		'.bl-hover .sIFR-root { color: #a57c52;  text-decoration: none;  cursor: pointer; }',
	],
	
	tuneHeight: -0,
	offsetTop:	-0,
	wmode: 'transparent'
});


sIFR.replace(resPublica, {
	selector: 	'.pageHeader',
	css: [
		'.sIFR-root { color: #666666; }',
	],
	
	tuneHeight: -5,
	offsetTop:	-0,
	wmode: 'transparent'
});

sIFR.replace(resPublica, {
	selector: 	'.dirNav',
	css: [
		'.sIFR-root { color: #666666; }',
	],
	
	tuneHeight: -5,
	offsetTop:	-0,
	wmode: 'transparent'
});

sIFR.replace(resPublica, {
	selector: 	'.product .boxFeatured',
	css: [
		'.sIFR-root { color: #666666; }',
	],
	
	tuneHeight: -0,
	offsetTop:	-0,
	wmode: 'transparent'
});




/**
 * Equal Heights Plugin
 * Equalize the heights of elements. Great for columns or any elements
 * that need to be the same size (floats, etc).
 * 
 * Version 1.0
 * Updated 12/10/2008
 *
 * Copyright (c) 2008 Rob Glazebrook (cssnewbie.com) 
 *
 * Usage: $(object).equalHeights([minHeight], [maxHeight]);
 * 
 * Example 1: $(".cols").equalHeights(); Sets all columns to the same height.
 * Example 2: $(".cols").equalHeights(400); Sets all cols to at least 400px tall.
 * Example 3: $(".cols").equalHeights(100,300); Cols are at least 100 but no more
 * than 300 pixels tall. Elements with too much content will gain a scrollbar.
 * 
 */

(function($) {
	$.fn.equalHeights = function(minHeight, maxHeight) {
		tallest = (minHeight) ? minHeight : 0;
		this.each(function() {
			if($(this).height() > tallest) {
				tallest = $(this).height();
			}
		});
		if((maxHeight) && tallest > maxHeight) tallest = maxHeight;
		return this.each(function() {
			$(this).height(tallest) /*.css("overflow","auto")*/;
		});
	}
})(jQuery);


