$(document).ready(function() {
	
	// Nivo Slider
	$('#slides').nivoSlider({
		effect:'random',
		directionNav: false,
		animSpeed:500,
		pauseTime:4000,
		captionOpacity: 1
	});
	
	// Media Slideshow
	$("#media_slideshow").tabs({ fx:{ opacity: "toggle" } }).tabs("rotate", 5000, true);
	$("#media_slideshow").hover(function() {
		$("#media_slideshow").tabs("rotate",0,true);
	},function() {
		$("#media_slideshow").tabs("rotate",5000,true);
	});
	
	// Drop down menus
	$("div#header #nav li ul").each(function() {
		$(this).prepend('<li class="arrow"></li>');
		$(this).css({
			'left' : -($(this).width() / 2 - ($(this).parent().width() / 2) + 10)
		});
	});
	$("div#header #nav li").hover(function() {
		if($(this).find("ul").size != 0) {
			$(this).find("ul").stop(true, true).fadeIn("fast");
		}
	}, function() {
		$(this).find("ul").stop(true, true).fadeOut("fast");
	});
	
	$("div#header ul#nav li").each(function() {
		$("ul li:last a", this).css({ 'border' : 'none' });
	});
	
	// Hours Drop Down
	$(".hours").css({ 'top' : "-"+($(".hours").outerHeight())+"px" });
	$("a.hours_toggle").click(function() {
		if($(this).hasClass("active")) {
			$(".hours").animate({ 'top' : "-"+($(".hours").outerHeight())+"px" }, 300);
			$(this).animate({ 'top' : '-1px' }, 300)
						 .removeClass("active")
						 .find("span")
						 .addClass("down").removeClass("up");
		} else {
			$(".hours").animate({ 'top' : 0 }, 300);
			$(this).animate({ 'top' : $(".hours").height() + $(this).outerHeight() - 6 }, 300)
						 .addClass("active")
						 .find("span")
						 .removeClass("down").addClass("up");
		}
	});
	
	// Our story reviews slideshow
	$("#reviews").tabs({ fx:{ opacity: "toggle" } }).tabs("rotate", 3000, true);
	
	// Gallery Slideshow 
	$("#slideshow").nivoSlider({
			effect:'fade',
	    controlNavThumbs:true,
			controlNavThumbsReplace: '.jpg',
			directionNav: false,
			captionOpacity: 1,
			afterLoad: function(){
				$(".nivo-controlNav a:nth-child(7n) img").css({ 'margin-right' : 0 });
			}
	});
	
	// Gallery hover
	$("div#gallery .three_column li, div#gallery .four_column li").each(function() {
		$("a", this).append('<div class="hover"></div>');
	});
	$("div#gallery .three_column li, div#gallery .four_column li").hover(function() {
		$("a", this).find(".hover").stop(true, true).fadeIn(400);
	}, function() {
		$("a", this).find(".hover").stop(true, true).fadeOut(400);
	});
	
	// Gallery Fancyboxes
	$("a.gallery_image").fancybox({
		'transitionIn'	:	'elastic',
		'transitionOut'	:	'elastic',
		'speedIn'		:	400, 
		'speedOut'		:	400, 
		'overlayShow'	:	false
	});
	$("#gallery ul.three_column li:nth-child(3n)").addClass("last");
	$("#gallery ul.four_column li:nth-child(4n)").addClass("last");
	
	// Small Sidebar
	$("div#flickr ul li").hover(function() {
		$(this).animate({ 'opacity' : '0.6' }, 300);
	}, function() {
		$(this).stop(true,true).animate({ 'opacity' : '1' }, 300)
	});
	
	// Tabs, toggles and accordions
	$(".tabs").tabs();
	$(".accordion").accordion({ autoHeight: false, header: '.heading' });
	$(".small_accordion").accordion({ autoHeight: false });
	$(".list_accordion").accordion({ autoHeight: false, active: false });
	
	// Button hover state
	$(".button").stop(true,true).hover(function() {
		$(this).animate({ 'opacity' : '0.8' }, 300);
	}, function() {
		$(this).stop(true,true).animate({ 'opacity' : '1' }, 300)
	});
	
	// Pricing table hover
	$(".pricing_box").hover(function() {
		$(this).stop(true,true).animate({ 'opacity' : '1' }, 300).addClass("active");
	}, function() {
		$(this).stop(true,true).animate({ 'opacity' : '0.8' }, 300).removeClass("active");
	});
	
	// Default text field values
	$(".text_field").focus(function(srcc)
  {
      if ($(this).val() == $(this)[0].title) {
          $(this).addClass("text_field_active");
          $(this).val("");
      }
  });
  $(".text_field").blur(function() {
      if ($(this).val() == "") {
          $(this).removeClass("text_field_active");
          $(this).val($(this)[0].title);
      }
  });
  $(".text_field").blur();
	
	// Fix table borders
	$("table").each(function() {
		$("tr th:last", this).addClass("no_right_border");
		$("tr:last td", this).addClass("no_bottom_border");
		$("tr", this).each(function() {
			$("td:last", this).addClass("no_right_border");
		});
	});	
	
	// Twitter Arrows
	//$("div#feedback div.tweets ul li").append('<em class="arrow"></em>');
	
	// UL borders
	$(".press ul li:last, .specials ul li:last").css({ 'border-bottom' : 'none' });
	
	// Hours & location hover states
	$(".directions a").css({ 'opacity' : 0.6 }).append("<span></span>");
	$(".directions a").hover(function() {
		$(this).stop(true, true).animate({ 'opacity' : 1 }, 200);
	}, function() {
		$(this).stop(true, true).animate({ 'opacity' : 0.6 }, 200);
	});
	
	// Ajax contact form
	$('#contact_form').submit(function() {
       
		var this_form = $(this);
  	$.ajax({
  		type: 'post',
  		data: this_form.serialize(),
  		url: 'scripts/send_email.php',
  		success: function(res) {
  			if(res == "true") {
					$(this_form)[0].reset();
					$(".notice").removeClass("error").text("Thank you for contacting us!").addClass("success").fadeIn("fast");
  			} else {
  				$(".notice").text("Please check all fields and try again.").addClass("error").fadeIn("fast");
  			}
  		}
  	});
		
   });
	
});
// Twitter integration
jQuery.fn.reverse=Array.prototype.reverse;String.prototype.linkify=function()
{return this.replace(/[A-Za-z]+:\/\/[A-Za-z0-9-_]+\.[A-Za-z0-9-_:%&\?\/.=]+/g,function(m)
{return m.link(m);});};String.prototype.linkuser=function()
{return this.replace(/[@]+[A-Za-z0-9-_]+/g,function(u)
{var username=u.replace("@","")
return u.link("http://twitter.com/"+username);});};String.prototype.linktag=function()
{return this.replace(/[#]+[A-Za-z0-9-_]+/,function(t)
{var tag=t.replace("#","%23")
return t.link("http://search.twitter.com/search?q="+tag);});};function fetch_tweets(elem)
{elem=$(elem);input=elem.attr('title');lang=elem.attr('lang');if(input!=window.monitter['text-'+input])
{window.monitter['last_id'+input]=0;window.monitter['text-'+input]=input;window.monitter['count-'+input]=12;;}
if(window.monitter['count-'+input]>10)
{elem.prepend('<div class="tweet"><img src="http://monitter.com/widget/favicon.gif" align="absmiddle" />real time twitter by: <a href="http://monitter.com" target="_blank">monitter.com</a></div>');window.monitter['count-'+input]=0;}
var url="http://search.twitter.com/search.json?q="+input+"&lang="+lang+"&rpp="+rrp+"&since_id="+window.monitter['last_id'+input]+"&callback=?";$.getJSON(url,function(json)
{$('div.tweet:gt('+window.monitter['limit']+')',elem).each(function(){$(this).fadeOut('slow')});$(json.results).reverse().each(function()
{if($('#tw'+this.id,elem).length==0)
{window.monitter['count-'+input]++;var thedate=new Date(Date.parse(this.created_at));var thedatestr=thedate.getHours()+':'+thedate.getMinutes();var divstr='<div id="tw'+this.id+'" class="tweet"><img width="48" height="48" src="'+this.profile_image_url+'" ><p class="text">'+this.text.linkify().linkuser().linktag()+'<br />&nbsp;<b><a href="http://twitter.com/#!/prestolatin" target="_blank">'+this.from_user+'</a></b> &nbsp;-&nbsp;<b>'+thedatestr+'</b></p></div>';window.monitter['last_id'+input]=this.id;elem.prepend(divstr);$('#tw'+this.id,elem).hide();$('#tw'+this.id+' img',elem).hide();$('#tw'+this.id+' img',elem).fadeIn(4000);$('#tw'+this.id,elem).fadeIn('slow');}});input=escape(input);rrp=1;setTimeout(function(){fetch_tweets(elem)},2000);});return(false);}
$(document).ready(function(){window.monitter={};$('.monitter').each(function(e){rrp=6;fetch_tweets(this);});});
