//Document Ready Function
$(document).ready(function(){
	//What to do once the document is loaded.
	//getNowPlaying();
	
	getRecentTweets();
	
	//getRecentTexts();
		
   	applyBubblePopup();
   	
   	applyGoogleCustomSearch();
   	
   	getRecentPhotos();

	$("#SMS_Join").click(function(){
		
		
		if((/^[0-9]{3}$/.test($("#AreaCode").val())) && (/^[0-9]{3}$/.test($("#Exchange").val())) && (/^[0-9]{4}$/.test($("#Subscriber").val()))){
			smsWindow = window.open('http://www.lookingtotheuttereast.com/WayEarlyShow/mozes.php?AreaCode='+ $('#AreaCode').val() + '&Exchange='+ $('#Exchange').val() + '&Subscriber='+ $('#Subscriber').val() + '', 'sms_signup', 'width=630,height=540,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,status=no,left=50,top=50');
			
			smsWindow.focus();
		}else{
			alert("This is Not A Valid Phone Number");

		}
	});


});

//System 30 Second Timer
$.timer(30000, function (timer) {
	//getNowPlaying();
});




//Page Functions

function StartPlayer(url, width, height) {
	win = window.open(url, 'popup_player', 'width='+width+',height='+height+',location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no,status=no,left=50,top=50');

	win.focus(); // brings the window to the front
} 


function showSearch(){
	
	$("#searchResultsContainer").css('display', 'block');
}

function hideSearch(){
	$("#searchResultsContainer").css('display', 'none');
	$("#siteSearch").attr("value","");

}

function applyGoogleCustomSearch(){

  
 	 var customSearchControl = new google.search.CustomSearchControl('009293939731017235813:6khlgvc_3km');
 	 var drawOptions = new google.search.DrawOptions();
		 drawOptions.setInput(document.getElementById('siteSearch'));


		customSearchControl.setSearchCompleteCallback(this, showSearch);	
		// Draw the control in content div
		customSearchControl.draw('searchResults', drawOptions);
		

}

function applyBubblePopup(){
	$('.bubbleInfo').each(function () {
            var distance = 10;
            var time = 250;
            var hideDelay = 500;

            var hideDelayTimer = null;

            var beingShown = false;
            var shown = false;
            var trigger = $('.trigger', this);
            var info = $('.description', this).css('opacity', 0);


            $([trigger.get(0), info.get(0)]).mouseover(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                if (beingShown || shown) {
                    // don't trigger the animation again
                    return;
                } else {
                    // reset position of info box
                    beingShown = true;

                    info.css({
                        top: -76,
                        left: -132,
                        display: 'block'
                    }).animate({
                        top: '-=' + distance + 'px',
                        opacity: 1
                    }, time, 'swing', function() {
                        beingShown = false;
                        shown = true;
                    });
                }

                return false;
            }).mouseout(function () {
                if (hideDelayTimer) clearTimeout(hideDelayTimer);
                hideDelayTimer = setTimeout(function () {
                    hideDelayTimer = null;
                    info.animate({
                        top: '-=' + distance + 'px',
                        opacity: 0
                    }, time, 'swing', function () {
                        shown = false;
                        info.css('display', 'none');
                    });

                }, hideDelay);

                return false;
            });
        });
}


function applyScrollBars(){
	$(".scroll-pane").jScrollPane(
        {
        	scrollbarWidth: 5,
			scrollbarMargin: 0
        }
      );
}


function getRecentTexts(){
$.jgooglevoice(function(data){
		$listStart = "<ul class='sms'>";
		$listEnd = "</ul>";
		$list = $listStart;
		
		
		$.each(data, function(i,item){
			$list = $list+"<li><span class='message'>"+item.message+"</span><span class='time'>"+item.date+"</span></li>";
		});
		$list = $list+$listEnd;
		
		//console.log($list);
		$(".mozes").append($list);
		var li = $(".sms");
		li.children('li:first').addClass('sms_first');
        li.children('li:odd').addClass('sms_even');
        li.children('li:even').addClass('sms_odd');
		
		
		applyScrollBars(); 
	});
}


function getRecentPhotos(){
		$.getJSON("http://graph.facebook.com/465803077520/photos?callback=?", function(data){
			$('#facebookPix').html('');
			$.each(data.data, function(i, item){
				if(i<20){
					$('#facebookPix').append("<a href='http://www.facebook.com/album.php?aid=253274&id=23368892520' target='_new'><img src='" + item.images[3].source + "' width='100px' style='margin: 3px;' /></a>");
				}
			})
			applyScrollBars();
		});
}



function getNowPlaying(){
	$.jTwitter('wayj_nowplaying',1, function(data){
		//Callback function with the user data as shown above
		$(".nowPlaying").tweetparse({
					input: data,
            		join_text: ":",
            		avatar_size: 48,
            		intro_text: "Now Playing",
            		auto_join_text_default: "Jeff said,",
            		auto_join_text_ed: "Jeff",
            		auto_join_text_ing: "Jeff was",
            		auto_join_text_reply: "Jeff replied to",
            		auto_join_text_url: "Jeff was checking out",
            		loading_text: "loading what's playing..."
            	});		
	});
}







            
function getRecentTweets(){
				$(".tweet").tweet({
					username: "wayearlyshow",
            		join_text: "auto",
            		avatar_size: 32,
            		count: 16,
            		toreturn: 16,
            		replies: true,
            		auto_join_text_default: "We said,",
            		auto_join_text_ed: "We",
            		auto_join_text_ing: "We were",
            		auto_join_text_reply: "We replied to",
            		auto_join_text_url: "We were checking out",
            		loading_text: "loading tweets..."
            	});
            	
            	$(".steveTweet").tweet({
					username: "steveshore",
            		join_text: "auto",
            		avatar_size: 48,
            		count: 5,
            		toreturn: 1,
            		replies: false,
            		auto_join_text_default: "Steve said,",
            		auto_join_text_ed: "Steve",
            		auto_join_text_ing: "Steve was",
            		auto_join_text_reply: "Steve replied to",
            		auto_join_text_url: "Steve was checking out",
            		loading_text: "loading Steve's tweets..."
            	});
            	$(".jeffTweet").tweet({
					username: "waytaylor",
            		join_text: "auto",
            		avatar_size: 48,
            		count: 5,
            		toreturn: 1,
            		replies: false,
            		auto_join_text_default: "Jeff said,",
            		auto_join_text_ed: "Jeff",
            		auto_join_text_ing: "Jeff was",
            		auto_join_text_reply: "Jeff replied to",
            		auto_join_text_url: "Jeff was checking out",
            		loading_text: "loading Jeff's tweets..."
            	});
};
