loginIntervalID = 0;
 
function CheckPopupClose() {
	var currentURL = new Array();
	currentURL = window.location.href.split('#');
 
	if ( window.location.hash == "#close-pp-delay" ) {
		window.location.href = currentURL[0] + "#";
		clearInterval( loginIntervalID );
		setTimeout( '0prettyPhoto.close()', 6000);
 
	} else if ( window.location.hash == "#close-pp" ) {
		window.location.href = currentURL[0] + "#";
		$.prettyPhoto.close();
		clearInterval( loginIntervalID );
 
	} else if ( window.location.hash == "#open-reg" ) {
		$.prettyPhoto.close();
		clearInterval( loginIntervalID );
	}
 
	return false;
}

// function shareSuccess(res){
// 	$('#ajax-comment').html(res);
// }
// 
// function populateMessages(xhr){
// 	$('#ajax-comment').html(xhr.responseText);
// }

function customSubmit(){
	
	if($('#comment-text').val().length > 1000){
		alert('Your post is too long!');
		return false;
	} else {
		$('#comments-form input').attr('disabled','disabled');
		$('#ajax-comment').html('\
			<img src="/_images/ajax-loader.gif" alt="loading..." /> \
		')
	}
}

function toggler(elem){
	$(elem).toggleClass('active');			
	$.cookies.set("help_message", $(elem).hasClass('active'), { expires: 14 });
}

$(document).ready(function(){
	// DD_roundies.addRule('.rounded', '5px');
	/*
	* example: '10px 5px 3px 0' sets:
	* top-left to 10px, 
	* top-right to 5px, 
	* bottom-right to 3px, 
	* the bottom-left to 0px
	*/
	
	 $('#slider').slider({
     value: 50,
     stop: function(event, ui) {
       if(event.originalEvent) {
         var newVal = Math.round(ui.value/25);
         $( '#coverflow' ).jcoverflip( 'current', newVal );
         $('#slider').slider('value', newVal*25);
       }
     }
   });

	$('#coverflow').jcoverflip({
     current: 2,
     beforeCss: function( el, container, offset ){
       return [
         $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 210 - 110*offset + 20*offset )+'px', bottom: '20px' }, { } ),
         $.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,100-20*offset*offset) + 'px' }, {} )
       ];
     },
     afterCss: function( el, container, offset ){
       return [
         $.jcoverflip.animationElement( el, { left: ( container.width( )/2 + 110 + 110*offset )+'px', bottom: '20px' }, { } ),
         $.jcoverflip.animationElement( el.find( 'img' ), { width: Math.max(10,100-20*offset*offset) + 'px' }, {} )
       ];
     },
     currentCss: function( el, container ){
       return [
         $.jcoverflip.animationElement( el, { left: ( container.width( )/2 - 80 )+'px', bottom: 0 }, { } ),
         $.jcoverflip.animationElement( el.find( 'img' ), { width: '154px' }, { } )
       ];
     },
     change: function(event, ui){
       $('#slider').slider('value', ui.to*25);
     }
   });

	$('#cf_wrapper').css({
			visibility:'visible'
		});



  
	
	
	$('#comment-text, #entry-body').bind('keyup paste', function(){
		var num = $(this).val().length;
		$('#chars').html(num);
		if(num > 1000){
			$('#chars').css('color','#f00');
		} else {
			$('#chars').css('color','#000');
		}
	});
	
	$('#entry-submit').click(function(){
		if($('#entry-body').val().length > 1000){
			alert('Your post is too long! Please limit to 1000 characters (including spaces)');
			return false;
		}
	});

	$('#catalog_cycle').cycle({
		fx:     'scrollHorz', 
    timeout: 0, 
    next:   '#cat_next', 
    prev:   '#cat_prev',
		easing: 'easeInOutCubic'
	});
	
	
	$('#expert_slider').cycle({
		fx:     'scrollHorz', 
    timeout: 0, 
    next:   '#cat_next2', 
    prev:   '#cat_prev2',
		easing: 'easeInOutCubic'
	});
	
	
	if($.cookies.get("help_message") != false){
		$('.message, .toggler').addClass('active');
		$('.toggler').text('Hide help');
	}
	
	$('.toggler').click(function(){
		$('.message').slideToggle({
			duration: 300,
			easing: 'easeInOutCubic',
			complete: toggler(this)
		});
		setTimeout(function(){$('.toggler').text($('.toggler').text() == 'Show help' ? 'Hide help' : 'Show help')}, 500);
	});
	
	$('#comments-form').submit( function(){
			 $(this).ajaxSubmit({
				 target: '#ajax-comment',
				 beforeSubmit: customSubmit,
				success: showResponse
			 });
			return false;
		}).find('input[name="armor"]').val('830a50d758601e3856255384687aa8e6c9bd3c93');
	      
	function showResponse(responseText, statusText, xhr, $form)  { 
			
			if(responseText.match(/<!DOCTYPE html>/)){
				location.reload();
			} else if (responseText.match(/.error/) || responseText.match(/Comment Preview:/)){
				$('#comments-form input').removeAttr('disabled');
			}
		}
	
	$('.email_form').live('submit', function(){
			$(this).ajaxSubmit({
				beforeSubmit: hideSuggest,
				success: showSuggest,
				target: '#suggest'
				
			});
			return false;
		});
		
	$('#gimme_form').live('click', function(){
			$('#suggest').fadeTo('fast',0,function(){
				$(this).load('/redesign/_library/emailer/suggest.php', function(){
					$('#suggest').fadeTo('fast',100);
				});
			});
			return false;
		});
		
		$('#gimme_email_form').live('click', function(){
				$('#email_signup').fadeTo('fast',0,function(){
					$(this).load('/redesign/_library/emailer/newsletter_signup.php', function(){
						$('#email_signup').fadeTo('fast',100);
					});
				});
				return false;
			});
	
	
		$("a[rel^='prettyPhoto']").mouseup(function () {
			clearInterval( loginIntervalID );
			var currentURL = new Array();
			currentURL = window.location.href.split('#');
			window.location.href = currentURL[0] + "#";
			loginIntervalID = setInterval ("CheckPopupClose()", 500);
		});
	
		if($("a[rel^='reportComments']").length > 0){
			$("a[rel^='reportComments']").prettyPhoto(
			{
				showTitle: true,
				callback: function()
				{
					clearInterval( loginIntervalID );
					return false;
				}
			});
		}
	
		
		 $('a.email').colorbox();


			$('.email_form').live('submit', function(){
				$(this).ajaxSubmit({
					success: shareSuccess,
					error: populateMessages
				});
				return false;
			});





		});

		function shareSuccess(res){
			$('#messages').html(res);
			$('.email_form, .useful_form').hide();
		}

		function populateMessages(xhr){
			$('#messages').html(xhr.responseText);
		}

 function signInToPost(entry_id, blog_id){
		if (!(mtGetUser())){
			$('#ajax-comment').html('<a href="http://www.goodinbed.com/cgi-bin/mt5/mt-cp.cgi?__mode=login&entry_id=' + entry_id + '&blog_id=' + blog_id + '&static=1">Sign in' + '</a>' + ' to reply to this topic.');
		}
	}
	
	function showSuggest(){
		$('#suggest').fadeTo(100,100);
	}

	function hideSuggest(){
		$('#suggest').fadeTo(100,0);
	}
	

