$(function() {
	
	
	
	if ($.browser.msie && $.browser.version <= 7) {
	$('form button').click(function() {
		$(this).parents('form').submit();
	});
	}
	
	if ($('div#gallery').length) {
		$('div#gallery a.next').click(function() {
			$('ul#gallery-slide li:last-child').prependTo($('ul#gallery-slide'));
			$('ul#gallery-slide').css({marginLeft:'-329px'}).stop().animate(
				{marginLeft:'-110px'},
				400,
				'easeOutExpo');
			return false;
			});
		$('div#gallery a.prev').click(function() {
			$('ul#gallery-slide').stop().animate(
				{marginLeft:'-329px'},
				400,
				'easeOutExpo',
				function() {
					$('ul#gallery-slide li:first-child').appendTo($('ul#gallery-slide'));
					$('ul#gallery-slide').css({marginLeft:'-110px'})
					}
				);
			return false;
			});
		}
	
	
	
	var nets = {
		'tw':'TWITTER',
		'ff':'FRIENDFEED',
		'fb':'FACEBOOK',
		'yt':'YOUTUBE',
		'fr':'FLICKR',
		'dl':'DELICIOUS'
		}
	$('div#footer div.soc-net div.pane > ul.net:eq(0)').addClass('shown');
	$('div#footer div.soc-net div.pane > ul.net:not(:eq(0))').addClass('hidden');
	$('div#footer div.soc-net ul.tabs li.tab a').click(function() {
		$('div#footer div.soc-net ul.tabs li').removeClass('on');
		$(this).parent('li').addClass('on');
		var c = this.className;
		var $curD = $('div#footer div.soc-net div.pane > ul.net:not(.hidden)');
		if ($curD.is(':not(.'+c+')')) {
			var $newD = $('div#footer div.soc-net div.pane > ul.'+c);
			$newD.removeClass('hidden').addClass('shown');
			var newH = $newD.outerHeight(true);
			$('div#footer div.soc-net div.pane').height($curD.outerHeight(true));
			$curD.animate({
				opacity:0
				},400,function(){
					$(this).addClass('hidden').removeClass('shown');
				});
			$('div#footer div.soc-net div.pane').animate({
				height:$newD.outerHeight(true)
				},400,function() {
					$newD.animate({
						opacity:1
						},400);
					$('div#footer div.soc-net ul.tabs li.title').text(nets[c]+' LIVE STREAM');
				});
		}
		return false;
	});
	$('div#footer div.soc-net ul.net.fr li').each(function() {
		$('p:has(img)',this).show();
		$('a',this).attr('target','_blank');
	});
	$('div#footer div.soc-net ul.net.yt li').each(function() {
		$('a',this).attr('target','_blank');
	});
	$('div#footer div.soc-net ul.net.fr li p a img').hover(function(){$(this).addClass('hov')},function(){$(this).removeClass('hov')});
	$('div#footer div.soc-net ul.tabs li a').append('<span class="hover"></span>');
	$('div#footer div.soc-net ul.tabs li a').hover(function() {
		$('.hover',this).stop().animate({
			'opacity':1
			},800,'easeOutExpo');
	},function() {
		$('.hover',this).stop().animate({
			'opacity':0
			},800,'easeOutExpo');
	});
	
	
	
	/*
	$('form:not(#comment_form) :text').each(function() {
		var pre = $(this).siblings('span.title').text();
		if ($(this).val()=='') $(this).val(pre).addClass('pre');
		$(this).focus(function() {
			if ($(this).val()==pre) $(this).val('').removeClass('pre');
		});
		$(this).blur(function() {
			if ($(this).val()=='') $(this).val(pre).addClass('pre');
		});
	});
	*/
	$('div#page form#rainmail-login p input').focus(function(){$(this).addClass('focus')});
	$('div#page form#rainmail-login p input').blur(function(){
		if ($(this).val() == '') $(this).removeClass('focus')
	});
	
	
	
	$('div#page form.email-signup input#em-signup-sms').click(function() {
		if ($(this).is(':checked')) {
			$('div#page form.email-signup ul li.sms').slideDown(400,function() {
				$('div#page form.email-signup ul li.sms input').focus();
			});
		} else {
			$('div#page form.email-signup ul li.sms').slideUp(200,function() {
				$('div#page form.email-signup ul li.sms input').val('');
			});
		}
	});
	
	
	
	if ($('div#page form.email-signup').length) {
		var emSubVal = {
			debug:true,
			rules: {
				name:'required',
				email: {
					required:true,
					email:true
				},
				mobile: {
					required: {
						depends: function(e) {
							return $('div#page form.email-signup input#em-signup-sms:checked').length;
						}
					}
				}
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formShade(f);
				$.ajax({
					type:'POST',
					url:'/cr/php/processor.php',
					dataType:'json',
					data:{
						action:'em-sub',
						jssub:'1',
						name:$('input#em-signup-name',f).val(),
						email:$('input#em-signup-email',f).val(),
						cell:$('input#em-signup-mobile',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<div class="alert"><h1>Thanks for your interest.</h1><p>'+json.msg+'</p></div>';
						} else {
							m = '<div class="alert"><h1>There was a problem subscribing.</h1><p>'+json.msg+'</p></div>';
						}
						formReveal(f,m);
					},
					error:function(x,t,e) {
						formReveal(f,'<div class="alert"><h1>There was a problem subscribing.</h1></div>');
					}
				});
			},
			invalidHandler: function(f,v) {
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		}
		$('div#page form.email-signup input#em-signup-mobile').mask("999-999-9999",{placeholder:" "});
		$('div#page form.email-signup').validate(emSubVal);
	}
	
	
	
	if ($('div#page form#contact').length) {
		var conVal = {
			debug:true,
			rules: {
				fname:'required',
				lname:'required',
				email: {
					required:true,
					email:true
				},
				comments:'required'
			},
			errorClass:'invalid',
			highlight:function(e,ec) {
				$(e).addClass(ec);
			},
			unhighlight:function(e,ec) {
				$(e).removeClass(ec);
				$(e).siblings('span.error').remove();
			},
			submitHandler:function(f) {
				formShade(f);
				$.ajax({
					type:'POST',
					url:'/cr/php/processor.php',
					dataType:'json',
					data:{
						action:'contact',
						jssub:'1',
						fname:$('input#contact-fname',f).val(),
						first_name:$('input#contact-first_name',f).val(),
						lname:$('input#contact-lname',f).val(),
						email:$('input#contact-email',f).val(),
						phone:$('input#contact-phone',f).val(),
						addr:$('input#contact-addr',f).val(),
						city:$('input#contact-city',f).val(),
						state:$('select#contact-state',f).val(),
						zip:$('input#contact-zip',f).val(),
						how:$('select#contact-how',f).val(),
						comments:$('textarea#contact-comments',f).val()
					},
					success:function(json) {
						var m = '';
						if (json.success) {
							m = '<div class="alert"><h1>Thanks for contacting us.</h1><p>Now we\'ve finally got something to read.</p></div>';
						} else {
							m = '<div class="alert"><h1>Huh? That didn\'t work right.</h1><p>You\'re gonna have to <a href="/contact/">try that again</a>.</p></div>';
						}
						formReveal(f,m);
					},
					error:function(x,t,e) {
						formReveal(f,'<div class="alert"><h1>Huh. That didn\'t work right.</h1><p>You\'re gonna have to <a href="/contact/">try that again</a>.</p></div>');
					}
				});
			},
			invalidHandler: function(f,v) {
				//
			},
			errorPlacement: function(err,el) {
				var fn = $(el).siblings('span.title').text();
				$(el).siblings('span.error').remove();
				if(!$(el).siblings('span.error').length) {
					$(el).after('<span class="error">Please provide your <strong>'+fn+'</strong>.</span>');
				}
			}
		}
		$('div#page form#contact').validate(conVal);
	}
	
	
	
	$('.hoverable').hover(function(){$(this).addClass('hov');},function(){$(this).removeClass('hov');});
	
	
	
	if ($('div.screen-gallery img.secondary').length) {
		$('div.screen-gallery').append('<ul class="ctrl"></ul>');
		$('div.screen-gallery').cycle({
			timeout:6000,
			speed:2000,
			slideExpr:'img',
			pager:'div.screen-gallery ul.ctrl',
			pagerAnchorBuilder:function(i,e) {
				var ni = i+1;
				return '<li><a href="#">'+ni+'</a></li>';
			}
		});
	}
	
	
	
	if($('div.gallery').length) {
		var c = '';
		var ci = '';
		var g = $('div.gallery');
		var l = $('ul',g);
		var llen = $('li',l).length;
		var dims = {
			s:{mt:'16px',w:'143px',h:'119px'},
			m:{mt:'12px',w:'162px',h:'134px'},
			l:{mt:'8px',w:'187px',h:'155px'}
			}
		if ($.browser.msie && $.browser.version <= 7) {
			$(g).css('paddingTop','12px');
			$(l).css({marginLeft:'0',width:'845px',paddingLeft:'35px'});
		}
		//console.log(dims.s.mt);
		// if ($('li',g).length<6) {
		var lc = $('li',g).clone();
		$(lc).appendTo('div.gallery ul');
		$(lc).clone().prependTo('div.gallery ul');
		// }
		$('li',g).each(function(a) {
			if(a<5+llen&&a>=llen) {
				if (($.browser.msie && $.browser.version >= 8) || !$.browser.msie)  {
					if(a==0+llen||a==4+llen) $(this).addClass('s');
					if(a==1+llen||a==3+llen) $(this).addClass('m');
					if(a==2+llen) $(this).addClass('l');
					$(this).fadeIn(800);
				} else {
					$('li a img',g).css({width:'187px',height:'155px'});
					$(this).show();
				}
			}
		});
		$('a.ctrl-prev',g).click(function() {
			prevSlide();
		});
		$('a.ctrl-next',g).click(function() {
			nextSlide();
		});
		function nextSlide() {
			$('a.ctrl-next',g).unbind('click');
			$(l).css({width:'1040px'});
			if ($.browser.msie && $.browser.version <= 7) {
				$(l).animate({marginLeft:'-202px'},800,function() {
					$(l).css({marginLeft:'0',width:'845px',paddingLeft:'35px'});
				});
			} else {
				$(l).animate({marginLeft:'-159px'},800,function() {
					$(l).css({marginLeft:'0',width:'880px'});
				});
			}
			$('li:eq('+llen+')',l).fadeOut(800,function() {
				$('li:first-child',l).appendTo(l);
			});
			if ($.browser.msie && $.browser.version <= 7) {
				$('li:eq('+(4+llen)+')',l).fadeIn(800);
			} else {
				$('li:eq('+(5+llen)+')',l).fadeIn(800);
			}
			$('li a',g).each(function(a) {
				if(a==1+llen||a==5+llen) {
					c = {marginTop:'16px'};
					ci = {height:'119px',width:'143px'};
				} else if(a==2+llen||a==4+llen) {
					c = {marginTop:'12px'};
					ci = {height:'134px',width:'162px'};
				} else if(a==3+llen) {
					c = {marginTop:'8px'};
					ci = {height:'155px',width:'187px'};
				} else {
					c = {marginTop:'8px'};
					ci = {height:'119px',width:'143px'};
				}	
				if ($.browser.msie && $.browser.version <= 7) {
					c = {};
					ci = {height:'155px',width:'187px'};
				}
				$(this).animate(c,800);
				$('img:not(.ref)',this).animate(ci,800,function() {
					$('a.ctrl-next',g).click(function() {
						nextSlide();
					});
				});
			});
			return false;
		}
		function prevSlide() {
			$('a.ctrl-prev',g).unbind('click');
			if ($.browser.msie && $.browser.version <= 7) {
				$(l).css({marginLeft:'-202px',width:'1040px'});
				$(l).animate({marginLeft:0},800,function() {
					$(l).css({width:'845px',paddingLeft:'35px'});
				});
			} else {
				$(l).css({marginLeft:'-159px',width:'1040px'});
				$(l).animate({marginLeft:0},800,function() {
					$(l).css({width:'880px'});
				});
			}
			$('li:last-child',l).prependTo(l);
			$('li:eq('+llen+')',l).fadeIn(800);
			if ($.browser.msie && $.browser.version <= 7) {
				$('li:eq('+(4+llen)+')',l).fadeOut(800);
				$('li:eq('+(5+llen)+')',l).hide();
			} else {
				$('li:eq('+(5+llen)+')',l).fadeOut(800);
			}
			$('li a',l).each(function(a) {
				if(a==llen||a==4+llen) {
					c = {marginTop:'16px'};
					ci = {height:'119px',width:'143px'};
				} else if(a==1+llen||a==3+llen) {
					c = {marginTop:'12px'};
					ci = {height:'134px',width:'162px'};
				} else if(a==2+llen) {
					c = {marginTop:'8px'};
					ci = {height:'155px',width:'187px'};
				} else {
					c = {marginTop:'16px'};
					ci = {height:'119px',width:'143px'};
				}	
				if ($.browser.msie && $.browser.version <= 7) {
					c = {};
					ci = {height:'155px',width:'187px'};
				}
				$(this).animate(c,800);
				$('img',this).animate(ci,800,function() {
					$('a.ctrl-prev',g).click(function() {
						prevSlide();
					});
				});
			});
			return false;
		}
	}
	/*
	if($('div.gallery').length) {
		var c = '';
		var ci = '';
		var g = $('div.gallery');
		var l = $('ul',g);
		var llen = $('li',l).length;
		var dims = {
			s:{mt:'16px',w:'143px',h:'119px'},
			m:{mt:'12px',w:'162px',h:'134px'},
			l:{mt:'8px',w:'187px',h:'155px'}
			}
		//console.log(dims.s.mt);
		// if ($('li',g).length<6) {
		var lc = $('li',g).clone();
		$(lc).appendTo('div.gallery ul');
		$(lc).clone().prependTo('div.gallery ul');
		// }
		$('li',g).each(function(a) {
			if(a<5+llen&&a>=llen) {
				if(a==0+llen||a==4+llen) $(this).addClass('s');
				if(a==1+llen||a==3+llen) $(this).addClass('m');
				if(a==2+llen) $(this).addClass('l');
				$(this).fadeIn(800);
			}
		});
		$('a.ctrl-prev',g).click(function() {
			prevSlide();
		});
		$('a.ctrl-next',g).click(function() {
			nextSlide();
		});
		function nextSlide() {
			$('a.ctrl-next',g).unbind('click');
			$(l).css({width:'1040px'});
			$(l).animate({marginLeft:'-159px'},800,function() {
				$(l).css({marginLeft:'0',width:'880px'});
			});
			$('li:eq('+llen+')',l).fadeOut(800,function() {
				$('li:first-child',l).appendTo(l);
			});
			$('li:eq('+(5+llen)+')',l).fadeIn(800);
			$('li a',g).each(function(a) {
				if(a==1+llen||a==5+llen) {
					//c = {display:'block',marginTop:'16px'};
					c = {marginTop:'16px'};
					ci = {height:'119px',width:'143px'};
				} else if(a==2+llen||a==4+llen) {
					//c = {display:'block',marginTop:'12px'};
					c = {marginTop:'12px'};
					ci = {height:'134px',width:'162px'};
				} else if(a==3+llen) {
					//c = {display:'block',marginTop:'8px'};
					c = {marginTop:'8px'};
					ci = {height:'155px',width:'187px'};
				} else {
					//c = {display:'none',marginTop:'16px'};
					c = {marginTop:'8px'};
					ci = {height:'119px',width:'143px'};
				}
				$(this).animate(c,800);
				$('img:not(.ref)',this).animate(ci,800,function() {
					$('a.ctrl-next',g).click(function() {
						nextSlide();
					});
				});
			});
			return false;
		}
		function prevSlide() {
			$('a.ctrl-prev',g).unbind('click');
			$(l).css({marginLeft:'-159px',width:'1040px'});
			$(l).animate({marginLeft:0},800,function() {
				$(l).css({width:'880px'});
			});
			$('li:last-child',l).prependTo(l);
			$('li:eq('+llen+')',l).fadeIn(800);
			$('li:eq('+(5+llen)+')',l).fadeOut(800);
			$('li a',l).each(function(a) {
				if(a==llen||a==4+llen) {
					//c = {display:'block',marginTop:'16px'};
					c = {marginTop:'16px'};
					ci = {height:'119px',width:'143px'};
				} else if(a==1+llen||a==3+llen) {
					//c = {display:'block',marginTop:'12px'};
					c = {marginTop:'12px'};
					ci = {height:'134px',width:'162px'};
				} else if(a==2+llen) {
					//c = {display:'block',marginTop:'8px'};
					c = {marginTop:'8px'};
					ci = {height:'155px',width:'187px'};
				} else {
					//c = {display:'none',marginTop:'16px'};
					c = {marginTop:'16px'};
					ci = {height:'119px',width:'143px'};
				}
				$(this).animate(c,800);
				$('img',this).animate(ci,800,function() {
					$('a.ctrl-prev',g).click(function() {
						prevSlide();
					});
				});
			});
			return false;
		}
	}
	*/
	
	if ($('ul#twitter_update_list').length) {
		$('ul#twitter_update_list li').each(function() {
			var twId = $('>a',this).attr('href').split('/');
			twId = twId.pop();
			var replyUrl = 'http://twitter.com/?status=@clickrain%20&in_reply_to_status_id='+twId+'&in_reply_to=clickrain';
			$('>a',this).wrap(document.createElement("div"));
			$('div',this).prepend('<a href="'+replyUrl+'" style="font-size:85%;">reply</a> &nbsp; ');
			//http://api.twitter.com/1/statuses/retweet/6153992355.xml
			//http://twitter.com/douglaswils/status/6153992355
		});
	}
	
});

function formShade(el) {
	$(el).fadeOut(400);
}
function formReveal(el,str) {
	$(el)
		.fadeOut(200,function() {
			$(this).html(str);
		})
		.fadeIn(400);
}
