
$(function() {//on doc ready
	CC.init();
});

var CC = new function() {
	this.curr_loc = window.location + '';
	this.curr_loc = this.curr_loc.substring( this.curr_loc.indexOf('.com')+4 );

	this.init = function() {

		$('embed').each( function(){
			$(this).get(0).outerHTML = $(this).get(0).outerHTML; //prevents "Click to activate" in browsers
		});

		$("#menu div").each( function() {
			var page_active = false
			if ( (CC.curr_loc.indexOf( $(this).attr('lnk') ) != -1 && $(this).attr('lnk') != "/") || CC.curr_loc == $(this).attr('lnk') ) {
				page_active = true;
			}
			
			if ( page_active ) {
				$(this).addClass('hover').addClass('active');
				$(this).parents('div[@id!=menu]').addClass('hover').addClass('active');
			}

			$(this).addClass('hand')
			.mouseover(function() {
				if ( $(this).attr("class").indexOf('open') == -1 ) {
					$(this).addClass('hover');
					$(this).addClass('open');
					if ( $(this).attr('control') ) {
						$('.'+$(this).attr('control'), this).fadeIn(160, function(){
							if ( $('div', this).size() > 5 ) {
								$('object').css('visibility', 'hidden');
							}						
						});
					}
				}
			})
			.mouseout(function(e) {
				if ( mouseOutCheck(e, this) ) {
					if ( $(this).attr("class").indexOf('active') == -1 ) {
						$(this).removeClass('hover');
					}
					$(this).removeClass('open');
					if ( $(this).attr('control') ) {
						$('.'+$(this).attr('control'), this).fadeOut(160, function(){
							$('object').css('visibility', 'visible');
						});
					}
				}
			})				
			.click(function(){
				$(this).addClass('hover');
				window.location = $(this).attr('lnk');
				return false;
			})
		} );

		//if no menu selected, we're home
		if ( $('#menu .active').size()==0 && CC.curr_loc.indexOf("cart.pl")==-1 ) {
			setTimeout( "CC.openPop()", 1700 );//even though this is onload, some browsers still need a moment to calculate height of window			
		}	
	}

	this.openPop = function() {

		var img1a = new Image();
		img1a.src = '/images/special.gif';
		var img2a = new Image();
		img2a.src = '/images/close.gif';
		
		var new_elem_str = '<div id="mail_pop" class="no_display absolute">';
			new_elem_str += '<div class="heading"><img class="hand" align="right" alt="click to close" title="click to close" onClick="$(\'#mail_pop\').fadeOut(400, function(){$(\'#mail_pop\').remove(); $(window).unbind() });" src="/images/close.gif" />&#160;&#160;Join Chad\'s Mailing List</div>';
			
			new_elem_str += '<div id="mail_inner">';
				new_elem_str += '<div>Sign-up to receive exclusive bulletins, updates, and offers directly from Chad.  Chad\'s daily journal/blog is made exclusively available to mailing list members, so sign-up today.</div>';
				new_elem_str += '<center><form name="listForm" id="listForm">Name:<br/><input type="textbox" class="formItem" name="name" id="name" /><br/>Email:<br/><input type="textbox" class="formItem" name="email" id="email" /><input type="hidden" name="sendto" id="sendto" value="chadcollyer@yahoo.com"/><br /><br /><center><input type="submit" class="formBtn hand" value=" send "/></center><div class="loading text_center"></div></form></center>';
			new_elem_str += '</div>';
		new_elem_str += '</div">';

		$('body').append(new_elem_str);
		$('#listForm .formBtn').click( CC.checkMailForm );
		 $('#listForm input').keypress(function(event){ 
			if ( event.keyCode == 13 ) {
				return false;
			}
		 });


	    //var dims1 = findWindowCenter( -160, -1 * ( $('#mail_pop').height() / 2 ) );	    			
		var dims1 = findPos( $('#main_table').get(0) )
		var new_left = dims1[0];
		var new_top = dims1[1] + findYScroll();

		jQuery.each(jQuery.browser, function(i) {
		  if($.browser.opera){
				new_top = "120";
		  }
		});

		$('#mail_pop')
			.css('left', (new_left+10) + 'px')
			.css('top', new_top + 'px')
			.fadeIn(1000, function(){
				$(this).addClass('open')
				$(window).resize(function(){
					CC.reCenter( $('#mail_pop'), -160, -1 * ( $('#mail_pop').height() / 2 ) );
				});
				$(window).scroll(function(){
					CC.reCenter( $('#mail_pop'), -160, -1 * ( $('#mail_pop').height() / 2 ) );
				});
			});
	}

	this.checkMailForm = function() {
		var err_msg = '';
		var err_fld = '';
		var form = $(this).parents('form');
		var name1 = $('#name',form).val().trim();
		var email1 = $('#email',form).val().trim();
		var sendto1 = $('#sendto',form).val().trim();
		
		if (name1=='') {
			err_msg = 'Please enter your name.';
			err_fld = 'name';
		}
		else if (email1=='' || !email1.match(/\b(^(\S+@).+((\.com)|(\.net)|(\.edu)|(\.mil)|(\.gov)|(\.org)|(\.info)|(\..{2,2}))$)\b/gi) ) {
			err_msg = 'Please enter your email address.';
			err_fld = 'email';
		}

		if ( err_fld != '' ) {
			$( '.loading',form ).addClass('error').html(err_msg).slideDown(200);
			$( '#'+err_fld,form ).focus();
			return false;
		}
		else {
			$( '.loading',form ).removeClass('error').loading_small_wait().slideDown(200);
			$.post('/mail_signup.php', { name:name1, email:email1, sendto: sendto1 }, function(resp){
				if ( resp == 'ok' ) {
				   $( '.loading',form ).html('Thank you for joining Chad\'s mailing list!');
				}
			})
		}
		return false;
	}

	this.reCenter = function($el, h_correct, v_correct) {
	    //var dims = findWindowCenter(h_correct, v_correct);	    			
		var dims = findPos( $('#main_table').get(0) )
		var new_left = dims[0];
		var new_top = dims[1] + findYScroll();

		$el.css('left', (new_left+10) +'px')
		   .css('top', new_top);			
	}

}