
if(typeof(site) == 'undefined') {
	var site = {};	
}


site.base ='http://backbeatmemphis.com/dev/';





function fireFeatureVideo(){
	$("#feature-image").hide();
	$("#feature-video").show();
}

var page=1;
var isLoaded=false;
var isLocked=false;
var extra ="";
var isODD ='false';
var isCat ='';

function fireMorePosts(){
	

	if((isLoaded==false) &&(isLocked==false)){
		page++;
		getMorePosts(page)
	}
};



function getMorePosts(page){
		page = page;
		$.get("http://www.backbeatmemphis.com/dev/wp-content/themes/press/_archive_ajax.php",
			{"page":page,"odd":isODD,"cat":isCat},
		//	data: "page="+page+"&odd="+isODD+"&cat="+isCat,
			function(msg,textStatus){
				
				isLocked=false;
				
				if(jQuery.trim(msg)=="done"){
					isLoaded=true;
					$("#moreposts").hide();
				}else{
					if(jQuery.trim(msg)!=""){
						$('#content').append(msg);
						init_overlaysEtc();
					}else{
						//keep trying?
						//getMorePosts(page);
						isLoaded=true;
						$("#moreposts").hide();
					}
				}
		
	    	
	 		},'html');
}	

var debugOnce = false;
function _debug(txt){
	if(debugOnce==false){
		alert(txt);
		debugOnce=true;
	}
}
	
var olds_vid=false;
var olds_img=false;
function init_overlaysEtc(){
		//'.glean-image, .video-preview, #twitter-link'
			
			
		$('.feature-post-image').click(function(){
			fireFeatureVideo();
		});
	
		 $('.feature-post-image').hover(function(){
			$(this).addClass('hover');					  
		 },function() {
			$(this).removeClass('hover');
		 });
		
		
		 $('.image-holder-small').click(function(){
			t = $(this).attr('id');
			eid = t.replace("img_",'');
				if(olds_vid!=false){
					olds_vid.hide();
					olds_img.show();
				}
				olds_vid = $("#vid_"+eid);
				olds_img = $("#img_"+eid);
				$("#img_"+eid).hide();
				$("#vid_"+eid).show();
			
		 });
		
		 $('.image-holder-small').hover(function(){
			$(this).addClass('hover');					  
		 },function() {
			$(this).removeClass('hover');
		 });
}	
/*site.seoSlug = function(string){
	string = string.toLowerCase();
	string = jQuery.trim(string);
	string = string.replace("-"," ");
	var pstring = string.split(" ");
	var fstring = pstring.join("%20");
	
	fstring = fstring.replace("&", "and");
	fstring = fstring.replace("&eacute;", "e");
	
	var res = fstring.replace(/[^a-z0-9]/ig, "-");

	return res;
}

*/

site.seoSlug = function(string){
	string = string.toLowerCase();
	string = jQuery.trim(string);
	string = string.replace("-"," ");
	var pstring = string.split(" ");
	var fstring = pstring.join("%20");
	
	fstring = fstring.replace("&", "and");
	fstring = fstring.replace("&eacute;", "e");
	
	var res = fstring.replace(/[^a-z0-9]/ig, "-");

	return string;
}




	
$('document').ready(function()
	{
		
			
			init_overlaysEtc();
			
			/*
				$("#moreposts-button").click(function(){			
					fireMorePosts();
				});
			*/
		
			var $search = $('#search input[type=text]');
			var $searchForm = $('#search form');

			$searchForm.submit(function() {		
				window.location.assign(site.base + 'search/'+site.seoSlug($search.val()));
				return false;
			});
			
			
			$search
				.preserveDefaultText({
					defaultValue: 'SEARCH'
				})
				.focus(function() {
					$(this).addClass('focus');
				})
				.blur(function() {
					$(this).removeClass('focus');
				}
			);
			
		
		
		/*
			endless scrolling
		*/
			//endless scrolling
			$('#content').endlessScroll({

					fireOnce: true,
					fireDelay: 1,
					bottomPixels:150,
					insertAfter: "#content li:last",
					callback: function(p){
							if((isLoaded==false) &&(isLocked==false)){
								page++;
								isLocked=true;
								getMorePosts(page);
							}
					}
			});
	
			
		/********************/
		/* Ajax Comments */
		
	
		
		$("#commentform").submit(function(){return false;});
		
		$("#comments-link").click(function(){			
			$("html").animate({scrollTop: $("#comments").offset().top}, 1000);
			return false;
		});
		$("#comment_submit").live("click", function(){
			// Compile the request location
			$post_page = $("#template-directory").html()+"/functions/ocmx_comment_post.php";
			// Compile all the request details
			$author = $("#author").attr("value");
			$email = $("#email").attr("value");
			$url = $("#url").attr("value");
			$comment = $("#comment").attr("value");
			$twitter = $("#twitter").attr("value");
			$email_subscribe = $("#email_subscribe").attr("checked");
			$post_id = $("#comment_post_id").attr("value");
			$comment_parent_id = $("#comment_parent_id").attr("value");
	
			// Set which area the new comment will end up in
			if($comment_parent_id !== "0" && $comment_parent_id !== "")
				{$new_comments_id = "#new-reply-"+$comment_parent_id;}
			else
				{$new_comments_id = "#new_comments";}
			
			// Fade out the new comment div so that we can fade it in after posting our new comment
			//$($new_comments_id).fadeOut("fast");
			$("#commment-post-alert").fadeIn("slow");
			// Perform the "Magic" which is just a bit of Ajax
			$.post($post_page, { author: $author, email: $email, url: $url, twitter: $twitter, email_subscribe: $email_subscribe, comment: $comment, comment_post_id: $post_id, comment_parent: $comment_parent_id}, 
				function(data) {
					if($.browser.msie)
						{location.reload();}
					else
						{$($new_comments_id).html($($new_comments_id).html()+" "+data).fadeIn("slow");}
					$("#commment-post-alert").fadeOut("fast");
					$("#comment").attr("value", "");
			});
			return false;
		});
		
		$("a[id^='reply-']").live("click", function(){
			// Create the Comment Id and apply it to the comment form
			$comment_id = $(this).attr("id").replace("reply-", "");
			
			// Set which href we're dealing with

			if($.href_id)
				{
					$oldhref = $.href_id;
					$($oldhref).html("Reply");
				}
			$.href_id = "#reply-"+$comment_id;
			
			//Set where exactly the comment form will end up
			$new_location_id = "#form-placement-"+$comment_id;
			
			//Create the Id for the new placement of the comment Form and put it there
			if($($new_location_id).html().toString().indexOf("Leave") == -1)
				{
					$("#comment_form_container").remove().appendTo($new_location_id);
					$($new_location_id).fadeIn("slow");
					$("#comment_parent_id").attr("value", $comment_id);
					// Change href to Cancel
					$($.href_id).html("Cancel Reply");
				}
			else
				{
					$($new_location_id).fadeOut("fast");
					$("#comment_form_container").remove().appendTo("#original_comment_location");
					$("#comment_parent_id").attr("value", "0");
					// Change href back to Reply
					$($.href_id).html("Reply");
				}
			setTimeout(function(){$("html").animate({scrollTop: $(".comment-form-content").offset().top}, 1000);}, 500);
			return false;
		});
		$("#contact_form").submit(function(){
			$err = "";	
			var theForm = document.getElementById("contact_form");
			var e_value = $("#contact_email").attr("value");
			
			if ($("#contact_name").attr("value") == "" || $("#contact_name").attr("value") == "Name")
				{$err = $err + "\n - Enter your name.";}
			if(e_value !== "Email Address" && e_value !== "" && e_value.indexOf("@") !== -1 && e_value.indexOf("@.") == -1 && e_value.indexOf("@@") == -1 && ( e_value.indexOf(",") == -1  && e_value.indexOf("/") == -1 && e_value.indexOf("'") == -1 && e_value.indexOf("&") == -1 && e_value.indexOf("%") == -1 ))
				{}
			else
				{$err = $err + "\n - Enter a valid e-mail address.";}		
			if ($("#contact_subject").attr("value") == "" || $("#contact_subject").attr("value") == "Subject")
				{$err = $err + "\n - Enter a subject title for your message.";}
			if ($("#contact_message").attr("value") == "" || $("#contact_message").attr("value") == "Your Message")
				{$err = $err + "\n - Enter a message.";}
	
			if($err !== "")
				{
					$err = "Please correct the following: \n" + $err;
					alert($err);
					return false
				}			
			else
				{return true;}		  	
		});
		/**********************/
		/* Search Form Clearer */
		$search_criteria_id = "search_criteria";
		$("#"+$search_criteria_id).focus(function(){
			if($("#"+$search_criteria_id).attr("value") == "Search...")
				{$("#"+$search_criteria_id).attr("value", "");}
		});
		
		$("#"+$search_criteria_id).blur(function(){
			if($("#"+$search_criteria_id).attr("value") == "")
				{$("#"+$search_criteria_id).attr("value", "Search...");}
		});
		
		/************************/
		/* Contact Form Clearer */
		$contact_name_id = "contact_name";
		$("#"+$contact_name_id).focus(function(){
			if($("#"+$contact_name_id).attr("value") == "Name")
				{$("#"+$contact_name_id).attr("value", "");}
		});
		
		$("#"+$contact_name_id).blur(function(){
			if($("#"+$contact_name_id).attr("value") == "")
				{$("#"+$contact_name_id).attr("value", "Name");}
		});
					
		$contact_email_id = "contact_email";
		$("#"+$contact_email_id).focus(function(){
			if($("#"+$contact_email_id).attr("value") == "Email Address")
				{$("#"+$contact_email_id).attr("value", "");}
		});
		
		$("#"+$contact_email_id).blur(function(){
			if($("#"+$contact_email_id).attr("value") == "")
				{$("#"+$contact_email_id).attr("value", "Email Address");}
		});
			
		$contact_subject_id = "contact_subject";
		$("#"+$contact_subject_id).focus(function(){
			if($("#"+$contact_subject_id).attr("value") == "Subject")
				{$("#"+$contact_subject_id).attr("value", "");}
		});
		
		$("#"+$contact_subject_id).blur(function(){
			if($("#"+$contact_subject_id).attr("value") == "")
				{$("#"+$contact_subject_id).attr("value", "Subject");}
		});
		$contact_message_id = "contact_message";
		$("#"+$contact_message_id).focus(function(){
			if($("#"+$contact_message_id).attr("value") == "Your Message")
				{$("#"+$contact_message_id).attr("value", "");}
		});
		
		$("#"+$contact_message_id).blur(function(){
			if($("#"+$contact_message_id).attr("value") == "")
				{$("#"+$contact_message_id).attr("value", "Your Message");}
		});
		
		/*************************/
		/* Comments Form Clearer */
		$search_id = "s";	
		$("#"+$search_id).focus(function(){
			if($("#"+$search_id).attr("value") == "Search...")
				{$("#"+$search_id).attr("value", "");}
		});
		
		$("#"+$search_id).blur(function(){
			if($("#"+$search_id).attr("value") == "")
				{$("#"+$search_id).attr("value", "Search...");}
		});
		
		/*************************/
		/* Comments Form Clearer */
		$author_id = "author";	
		$("#"+$author_id).focus(function(){
			if($("#"+$author_id).attr("value") == "Name")
				{$("#"+$author_id).attr("value", "");}
		});
		
		$("#"+$author_id).blur(function(){
			if($("#"+$author_id).attr("value") == "")
				{$("#"+$author_id).attr("value", "Name");}
		});
		
		$email_id = "email";	
		$("#"+$email_id).focus(function(){
			if($("#"+$email_id).attr("value") == "Email Address")
				{$("#"+$email_id).attr("value", "");}
		});
		
		$("#"+$email_id).blur(function(){
			if($("#"+$email_id).attr("value") == "")
				{$("#"+$email_id).attr("value", "Email Address");}
		});
		
		$url_id = "url";		
		$("#"+$url_id).focus(function(){
			if($("#"+$url_id).attr("value") == "Website URL")
				{$("#"+$url_id).attr("value", "");}
		});
		$("#"+$url_id).blur(function(){
			if($("#"+$url_id).attr("value") == "")
				{$("#"+$url_id).attr("value", "Website URL");}
		});
		
		$twitter_id = "twitter";		
		$("#"+$twitter_id).focus(function(){
			if($("#"+$twitter_id).attr("value") == "Twitter Name")
				{$("#"+$twitter_id).attr("value", "");}
		});
		$("#"+$twitter_id).live("blur", function(){
			if($("#"+$twitter_id).attr("value") == "")
				{$("#"+$twitter_id).attr("value", "Twitter Name");}
		});
	});
