
(function($) {
	$.fn.containsHTML = function()
	{
	    var regexp = /<("[^"]*"|'[^']*'|[^'">])*>/gi;
		var hasHTML = false;
        this.each(function() {
			if ($(this).val().match(regexp))
			{
				hasHTML = true;
			}
        });
        return hasHTML;
	}
	
	function num(elem, prop) {
		return elem[0] && parseInt( jQuery.curCSS(elem[0], prop, true), 10 ) || 0;
	}
	
	$.fn.position_wSafariAuto = function() {
		// this is a copy of the jquery position function, except it ignores the left margin,
		// thus preventing an erroneous result in Safari when left margin is auto
		var left = 0, top = 0, results;

		if ( this[0] ) {
			// Get *real* offsetParent
			var offsetParent = this.offsetParent(),

			// Get correct offsets
			offset       = this.offset(),
			parentOffset = /^body|html$/i.test(offsetParent[0].tagName) ? { top: 0, left: 0 } : offsetParent.offset();

			// Subtract element margins
			// note: when an element has margin: auto the offsetLeft and marginLeft 
			// are the same in Safari causing offset.left to incorrectly be 0
			offset.top  -= num( this, 'marginTop'  );
			if (!$.browser.safari) { 
				offset.left -= num( this, 'marginLeft' );
			}
			// Add offsetParent borders
			parentOffset.top  += num( offsetParent, 'borderTopWidth'  );
			parentOffset.left += num( offsetParent, 'borderLeftWidth' );

			// Subtract the two offsets
			results = {
				top:  offset.top  - parentOffset.top,
				left: offset.left - parentOffset.left
			};
			
			results = {
				top:  this.offset().top,
				left: offset.left - parentOffset.left
			};
		}

		return results;
	}

})(jQuery);


$(document).ready(function() {

	function Centre_V()
	{
		//reposition the top & bottom backgronds, since they contain everything else
		var overall_height = $('div.top_background').height() + $('div.bottom_background').height();
		var win_height = $(window).height();
		
		if (overall_height < win_height)
		{
			$('div.top_background').css('margin-top', ((win_height - overall_height)/2) + 'px');
		}
	}	
	
	Centre_V();
	
	$(window).resize(function(){
		//close open menus
		$('#sn_styles').hide();
		$('#sn_jots').hide();
		$('#sn_press').hide();
		$('#sn_cards').hide();
		$('#sn_about').hide();
		
		//close open catalogue preview
		$('#catalogue_download_shadow').remove();
		$('#catalogue_download_note').remove();
		
		Centre_V();
		
		//reposition any open note type thing...
		var parent_top = $('div.bottom_background').position_wSafariAuto().top -1;
		var parent_left = $('div.bottom_background').position_wSafariAuto().left + 230;
		var parent_top_cards = $('div.bottom_background').position_wSafariAuto().top + 200;
		var parent_left_cards = $('div.bottom_background').position_wSafariAuto().left + 630;
		
		$('#eCard_work_box').css('top', parent_top_cards + 'px');
		$('#eCard_work_box').css('left', parent_left_cards + 'px');
		$('#top_tab_form_underlay').css('top', parent_top + 'px');
		$('#top_tab_form_underlay').css('left', parent_left + 'px');
		$('#email_promise_form_underlay').css('top', parent_top + 'px');
		$('#email_promise_form_underlay').css('left', parent_left + 'px');
		$('#captcha_form_underlay').css('top', parent_top + 'px');
		$('#captcha_form_underlay').css('left', parent_left + 'px');
	});
	
	$('#bottom_catalogue_link a').click(function(e){
		e.preventDefault();
		$('#catalogue_download_shadow').remove();
		$('#catalogue_download_note').remove();
		$('.bottom_background').append('<div id="catalogue_download_shadow"></div>');
		var parent_top = $('div.bottom_background').position_wSafariAuto().top + 420;
		var parent_left = $('div.bottom_background').position_wSafariAuto().left + 30;
		$('.bottom_background').append('<div id="catalogue_download_note"><p>The wholesale catalogue is a large file and may take a few minutes to download, depending on your internet connection. Please be patient.</p><p><a href="./downloads/ecojot_catalogue.pdf">Get the catalogue</a> or <a id="close_catalogue_note" href="">cancel</a>.</p></div>');
		
		$('#catalogue_download_shadow').css('top', parent_top + 'px');
		$('#catalogue_download_shadow').css('left', parent_left + 'px');
		$('#catalogue_download_note').css('top', parent_top + 2 + 'px');
		$('#catalogue_download_note').css('left', parent_left + 2 + 'px');
		
		$('#close_catalogue_note').click(function(e){
			e.preventDefault();
			$('#catalogue_download_shadow').remove();
			$('#catalogue_download_note').remove();
		});
		
	});
	
	function isEmailAddress(CheckValue) {
		if (CheckValue)
		{
			var emailReg = '^[\\w-_\.+]*[\\w-_\.]\@([\\w]+\\.)+[\\w]+[\\w]$';
			var regexp = new RegExp(emailReg);
			if (CheckValue.match(regexp)) return true;
		}	
        return false;
    }

	function OpenNoteForm()
	{
		$('#top_tab_form_underlay').remove();
		$('#top_tab_form').remove();
		$('#content').append('<div id="top_tab_form_underlay"><div id="top_tab_form"></div></div>');
		var parent_top = $('div.bottom_background').position_wSafariAuto().top;
		var parent_left = $('div.bottom_background').position_wSafariAuto().left + 230;
		
		//alert('bottom_background is located at (' + $('div.bottom_background').position().left + ', ' + $('div.bottom_background').position().top + ').');
		
		$('#top_tab_form_underlay').css('top', parent_top + 'px');
		$('#top_tab_form_underlay').css('left', parent_left + 'px');
	}
	
	function CloseNoteForm()
	{
		$('#top_tab_form_underlay').remove();
		$('#top_tab_form').remove();
	}
	
	$('#bottom_news').click(function (e){
		e.preventDefault();
		window.open('http://visitor.constantcontact.com/d.jsp?m=1102372201541&p=oi');
		
	});
	
	$('#bottom_send').click(function (e){
		e.preventDefault();
		OpenNoteForm();
		$.post('a_friend_form.aspx', function(STFForm) {
			if(STFForm){
				$('#top_tab_form').append(STFForm);
				$('#stf_cancel').click(function(e){
					e.preventDefault();
					CloseNoteForm();
				});
				
				$('#stf_your_name').click(function(e){
					$(this).val('');
					$(this).unbind('click');
				});
				
				$('#stf_your_name').change(function(e){
					$(this).unbind('click');
				});
				
				$('#stf_friend_name').click(function(e){
					$(this).val('');
					$(this).unbind('click');
				});
				
				$('#stf_friend_name').change(function(e){
					$(this).unbind('click');
				});
				
				$('#stf_friend_email').click(function(e){
					$(this).val('');
					$(this).unbind('click');
				});
				
				$('#stf_friend_email').change(function(e){
					$(this).unbind('click');
				});
				
				$('#friend_form input').blur(function(e){

					$('#friend_form_notice_shadow').remove();
					$('#friend_form_notice').remove();
				});
				
				$('#email_promise').click(function(e){
					e.preventDefault();
					OpenPromise();
				});
				
				$('#stf_send').click(function(e){
					e.preventDefault();
					SendToFriend();
				});
			}
		});
	});
	
	
	$('#bottom_feedback').click(function (e){
		e.preventDefault();
		OpenNoteForm();
		$.post('a_feedback_form.aspx', function(eFBForm) {
			if(eFBForm){
				$('#top_tab_form').append(eFBForm);
				$('#fb_cancel').click(function(e){
					e.preventDefault();
					CloseNoteForm();
				});
				
				$('#fb_name').click(function(e){
					$(this).val('');
					$(this).unbind('click');
				});
				
				$('#fb_name').change(function(e){
					$(this).unbind('click');
				});
				
				$('#fb_email').click(function(e){
					$(this).val('');
					$(this).unbind('click');
				});
				
				$('#fb_email').change(function(e){
					$(this).unbind('click');
				});
				
				$('#fb_rate').click(function(e){
					$(this).val('');
					$(this).unbind('click');
				});
				
				$('#fb_rate').change(function(e){
					$(this).unbind('click');
				});
				
				$('#email_promise').click(function(e){
					e.preventDefault();
					OpenPromise();
				});
				
				$('#fb_send').click(function(e){
					e.preventDefault();
					SendFeedback();
				});
			}
		});
	});
	
	
	
	function SendToFriend()
	{
		$('#friend_form_notice_shadow').remove();
		$('#friend_form_notice').remove();
		//check for bad stuff
		if($('#friend_form input').containsHTML())
		{
			$('#friend_form').append('<div id="friend_form_notice_shadow"></div>');
			$('#friend_form').append('<div id="friend_form_notice"><p>Please do not include HTML or HTML-like tags in your feedback.</p></div>');
			return;
		}
		//wipe  pre-set data
		
		
		if(($('#stf_your_name').val().length == 0) || ($('#stf_your_name').val() =='your name'))
		{
			$('#friend_form').append('<div id="friend_form_notice_shadow"></div>');
			$('#friend_form').append('<div id="friend_form_notice"><p>You should fill in your name</p></div>');
			return;
		}

		if(($('#stf_friend_name').val().length == 0) || ($('#stf_friend_name').val() =='friend\'s name'))
		{
			$('#friend_form').append('<div id="friend_form_notice_shadow"></div>');
			$('#friend_form').append('<div id="friend_form_notice"><p>You should fill in your friend\'s name</p></div>');
			return;
		}
		
		if(($('#stf_friend_email').val().length == 0) || ($('#stf_friend_email').val() =='friend\'s email address') || (!isEmailAddress($('#stf_friend_email').val())))
		{
			$('#friend_form').append('<div id="friend_form_notice_shadow"></div>');
			$('#friend_form').append('<div id="friend_form_notice"><p>The email address you provided appears to be incorrect.</p></div>');
			return;
		}
		
		$('#friend_form').append('<div id="friend_form_notice_shadow"></div>');
		$('#friend_form').append('<div id="friend_form_notice"><p>We are sending your email...</p></div>');
		$.post("a_friend_send.aspx", {sendername: $('#stf_your_name').val(), friendname: $('#stf_friend_name').val(), friendemail: $('#stf_friend_email').val()}, function(e){
			CloseNoteForm();
		});
		
	}
	
	
	
	function SendFeedback()
	{
		
		//check for bad stuff
		if($('#feedback_form input').containsHTML())
		{
			alert('Please do not include HTML or HTML-like tags in your feedback.');
			return;
		}

		if($('#feedback_form textarea').containsHTML())
		{
			alert('Please do not include HTML or HTML-like tags in your feedback.');
			return;
		}
		
		//resolve radio boxes
		var HardToFind = '';
		var Recommend = '';
		
		if ($('#fb_hard').get(0).checked) HardToFind='Yes';
		else if ($('#fb_easy').get(0).checked) HardToFind='No';
		
		if ($('#fb_friend').get(0).checked) Recommend='Yes';
		if ($('#fb_nofriend').get(0).checked) Recommend='No';
		
		//wipe  pre-set data
		
		//$('#fb_name').trigger('click');
		var s_name = '';
		if($('#fb_name').val() != 'name') s_name=$('#fb_name').val();
		
		var s_email = '';
		if($('#fb_email').val() != 'email address (optional)') s_email=$('#fb_email').val();
		
		var s_rate = '';
		if($('#fb_rate').val() != 'insert adjective') s_rate=$('#fb_rate').val();
		
		$.post("a_feedback_send.aspx", {name: s_name, email: s_email, where: $('#fb_where').val(), hardtofind: HardToFind, rate: s_rate, recommendtofriend: Recommend, styles: $('#fb_styles').val(), products: $('#fb_products').val(), additional: $('#fb_additional').val(), location: $('#fb_location').val()}, function(e){
		});
		CloseNoteForm();
	}


	function OpenPromise(){
		$('#email_promise_form_underlay').remove();
		$('#email_promise_form').remove();
		$('#content').append('<div id="email_promise_form_underlay"><div id="email_promise_form"></div></div>');
		
		var parent_top = $('div.bottom_background').position_wSafariAuto().top;
		var parent_left = $('div.bottom_background').position_wSafariAuto().left + 230;
		
		$('#email_promise_form_underlay').css('top', parent_top + 'px');
		$('#email_promise_form_underlay').css('left', parent_left + 'px');
		
		var epform = $('#email_promise_form');
		epform.append('<h2>ecojot email promise</h2>');
		epform.append('<ul><li>We will not use your email address to send you information about any other products and services.</li><li>We will not give your email address to any other companies.</li><li>To delete your email address from our address book, just email <a href="mailto:remove@ecojot.com">remove@ecojot.com</a> and we will delete it for you.</li></ul>');
		epform.append('<p>[ <a href="" class="popup_form_button" id="close_promise">close</a> ]</p>');
		$('#close_promise').click(function(e){
			e.preventDefault();
			$('#email_promise_form_underlay').remove();
			$('#email_promise_form').remove();
		});
	}

// ============================ nav buttons ======================= //

	$('#mn_give').click(function(e){
		window.location = './give_campaign.aspx';
	});

	$('#mn_home').click(function(e){
		window.location = './';
	});
	
	$('#mn_shop').click(function(e){
		window.location = './wheretoshop.aspx';
	});

	$('#flare7_button').click(function(e){
		window.location = './give_campaign.aspx';
	});

	$('#flare7_button').mouseenter(function(e){
		$('#sn_styles').hide('slow');
		$('#sn_jots').hide('slow');
		$('#sn_press').hide('slow');
		$('#sn_cards').hide('slow');
		$('#sn_about').hide('slow');
		$(this).css('background', 'url(\'./img/flare7_highlight.gif\') 0 0 no-repeat');
	});

	$('#flare7_button').mouseleave(function(e){
		$(this).css('background', '');
	});
	
	$('#mn_give').mouseenter(function(e){
		$('#sn_styles').hide('slow');
		$('#sn_jots').hide('slow');
		$('#sn_press').hide('slow');
		$('#sn_cards').hide('slow');
		$('#sn_about').hide('slow');
		$(this).css('background', 'url(\'./img/give_highlight.gif\') 0 0 no-repeat');
	});

	$('#mn_give').mouseleave(function(e){
		$(this).css('background', '');
	});
	
	
	$('#mn_home').mouseenter(function(e){
		$('#sn_styles').hide('slow');
		$('#sn_jots').hide('slow');
		$('#sn_press').hide('slow');
		$('#sn_cards').hide('slow');
		$('#sn_about').hide('slow');
		$(this).css('background', 'url(\'./img/lit_home.gif\') 0 0 no-repeat');
	});

	$('#mn_home').mouseleave(function(e){
		$(this).css('background', '');
	});	
	
	$('.bottom_background').mouseenter(function(e){
		$('#sn_styles').hide('slow');
		$('#sn_jots').hide('slow');
		$('#sn_press').hide('slow');
		$('#sn_cards').hide('slow');
		$('#sn_about').hide('slow');
	});



	$('#mn_shop').mouseenter(function(e){
		$('#sn_styles').hide('slow');
		$('#sn_jots').hide('slow');
		$('#sn_press').hide('slow');
		$('#sn_cards').hide('slow');
		$('#sn_about').hide('slow');
		$(this).css('background', 'url(\'./img/lit_shop.gif\') 0 0 no-repeat');
	});

	$('#mn_shop').mouseleave(function(e){
		$(this).css('background', '');
	});

	
	$('#mn_about').mouseenter(function(e){
		$('#sn_styles').hide('slow');
		$('#sn_jots').hide('slow');
		$('#sn_press').hide('slow');
		$('#sn_cards').hide('slow');
		var about_top = $('div.top_background').position_wSafariAuto().top + 120;
		var about_left = $('div.top_background').position_wSafariAuto().left + 445;
		$('#sn_about').css('top', about_top + 'px');
		$('#sn_about').css('left', about_left + 'px');
		$('#sn_about').show('slow');
	});

	$('#sn_about').mouseleave(function(e){
		$('#sn_about').hide('slow');
	});
	
	$('#mn_styles').mouseenter(function(e){
		$('#sn_about').hide('slow');
		$('#sn_jots').hide('slow');
		$('#sn_press').hide('slow');
		$('#sn_cards').hide('slow');
		var style_top = $('div.top_background').position_wSafariAuto().top + 130;
		var style_left = $('div.top_background').position_wSafariAuto().left + 540;
		$('#sn_styles').css('top', style_top + 'px');
		$('#sn_styles').css('left', style_left + 'px');
		$('#sn_styles').show('slow');
	});

	$('#sn_styles').mouseleave(function(e){
		$('#sn_styles').hide('slow');
	});

	$('#mn_jots').mouseenter(function(e){
		$('#sn_about').hide('slow');
		$('#sn_styles').hide('slow');
		$('#sn_press').hide('slow');
		$('#sn_cards').hide('slow');
		var jots_top = $('div.top_background').position_wSafariAuto().top + 125;
		var jots_left = $('div.top_background').position_wSafariAuto().left + 735;
		$('#sn_jots').css('top', jots_top + 'px');
		$('#sn_jots').css('left', jots_left + 'px');
		$('#sn_jots').show('slow');
	});

	$('#sn_jots').mouseleave(function(e){
		$('#sn_jots').hide('slow');
	});
	
	$('#mn_press').mouseenter(function(e){
		$('#sn_about').hide('slow');
		$('#sn_styles').hide('slow');
		$('#sn_jots').hide('slow');
		$('#sn_cards').hide('slow');
		var press_top = $('div.top_background').position_wSafariAuto().top + 105;
		var press_left = $('div.top_background').position_wSafariAuto().left + 790;
		$('#sn_press').css('top', press_top + 'px');
		$('#sn_press').css('left', press_left + 'px');
		$('#sn_press').show('slow');
	});

	$('#sn_press').mouseleave(function(e){
		$('#sn_press').hide('slow');
	});
	
	$('#mn_cards').mouseenter(function(e){
		$('#sn_about').hide('slow');
		$('#sn_styles').hide('slow');
		$('#sn_jots').hide('slow');
		$('#sn_press').hide('slow');
		var cards_top = $('div.top_background').position_wSafariAuto().top + 135;
		var cards_left = $('div.top_background').position_wSafariAuto().left + 850;
		$('#sn_cards').css('top', cards_top + 'px');
		$('#sn_cards').css('left', cards_left + 'px');
		$('#sn_cards').show('slow');
	});

	$('#sn_cards').mouseleave(function(e){
		$('#sn_cards').hide('slow');
	});

 });