/**
 * @author John Botica
 */
var scrollTos = {
	init: function(){
		if($('#latest_posts').length){
			$('#latest_posts').click(function(e){
				e.preventDefault();
				$.scrollTo('#content',500,{offset:{top:-20}});
				return false;
			});
		}
		if($('a.postComment').length){
			$('a.postComment').click(function(e){
				e.preventDefault();
				$.scrollTo('#leave_comment',500,{offset:{top:-20}});
				return false;
			});
		}
		if($('a.readComments').length){
			$('a.readComments').click(function(e){
				e.preventDefault();
				$.scrollTo('#post_comments',500,{offset:{top:-20}});
				return false;
			});
		}
	}
};
var fullList = {
	toggleHTML: '<div id="see_all" class="container"><a href="#hidden_list" id="see_full_list">See Full Mix List</a></div>',
	closeList: function(l){
		$('#hidden_list').slideUp('normal',function(){
			$('#hidden_list').removeClass('open').addClass('closed');
			l.html('See Full Mix List');
		});
	},
	openList: function(l){
		$('#hidden_list').slideDown('normal',function(){
			$('#hidden_list').removeClass('closed').addClass('open');
			l.html('Minimize List');
		});
	},
	insertToggle: function(){
		if($('#hidden_list').length){
			$('#hidden_list').after(fullList.toggleHTML);
			$('#see_full_list').click(function(e){
				e.preventDefault();
				if($('#hidden_list').hasClass('closed')){
					fullList.openList($(this));
				}else if($('#hidden_list').hasClass('open')){
					fullList.closeList($(this));
				}
			});
		}		
	},
	init: function(){
		if($('#hidden_list').length){
			fullList.insertToggle();
		}
	}
};
var featuredCycle = {
	init: function(){
		if ($('#featured_posts_images').length) {
			// Initiate cycler
			$('#featured_posts_images').cycle({
				speed: 1000,
				timeout: 7000,
				pager: '#featured_posts_nav'
			});
			$('#featured_posts_images').mouseenter(function(){
				$('#featured_posts_images').cycle('pause');
			});
			$('#featured_posts_images').mouseleave(function(){
				$('#featured_posts_images').cycle('resume');
			});
		}
	}	
};
var audioPopOut = {
	init: function(){
		if($('a.rbmar').length){
			$('a.rbmar').click(function(e){
				e.preventDefault();
				window.open(this.href,"rbmarPlayer","resizable=0,scrollbars=0,height=280,width=440,status=1,menubar=0");
				return false;
			});
		}
		if($('a.btn_mix_window').length){
			$('a.btn_mix_window').click(function(e){
				e.preventDefault();
				window.open(this.href,"player","resizable=0,scrollbars=0,height=10,width=350,status=1,menubar=0");
				return false;
			});
		}
	}
};
var relatedPosts = {
	init: function(){
		if($('#related_posts ul.results').length){
			$('#related_posts ul.results li:last-child').addClass('last');
		}
	}	
};

var homePageNav = {
	init: function(){
		if($('.homepage .wp-pagenavi a').length){
			var linkCount = $('.wp-pagenavi a').length;
			for (var i=0; i<linkCount; i++) {
				var link = $('.wp-pagenavi a:eq('+i+')')
				var hrefVal = link.attr('href');
				link.attr('href',hrefVal+'#content');
			};
			
			
		}
	}
};

var homePageSort = {
	init: function(){
		
	}
};

 $(document).ready(function(){
 	audioPopOut.init();
 	featuredCycle.init();
	scrollTos.init();
	fullList.init();
	relatedPosts.init();
	homePageNav.init();
	homePageSort.init();
	DOMUtilities.init();
 });
  
/**
 * DOM Utilies Object for handling basic DOM actions
 * @author					Dave Shepard
 * @version					1.0
 * @required libraries:		JQuery 1.3.2 or later
 * 
 * Usage:
 *     $(document).ready(function(){
 *     		DOMUtilities.init();
 *     });
 *     
 * Can be initialized via the init(); method to apply to entire <body> or
 * a scope can be passed to limit the initialization to the child elements
 * of a particular element. Individual methods can als be called and passed
 * a scope.
 */
var DOMUtilities = {
	targetBlank: function(locality){
		// XHTML 1.0 Strict work around for external links
		$(locality+' a[rel*="external"]').attr("target","_blank");
	},
	inputAutoClear: function(locality){
		$(locality+' input.clearField').focus(function(){
			if(this.defaultValue == this.value) this.value='';
		}).blur(function(){
			if(this.value == '') this.value = this.defaultValue;
		});
	},
	imgRollover: function(locality){
		// Image roll-over setup
		$(locality+' img.rollOver, '+locality+' input[type="image"].rollOver')
			.mouseover(function(){
				if (this.src.indexOf("_i.") != -1) {
					this.src = this.src.replace("_i.", "_o.");
				}
			}).mouseout(function(){
				if (this.src.indexOf("_o.") != -1) {
					this.src = this.src.replace("_o.", "_i.");
				}
				if(this.src.indexOf("_a.")) {
					this.src = this.src.replace("_a.","_i.");
				}
			}).filter("input").mousedown(function(){
				this.src = this.src.replace("_o.","_a.");
			}).mouseup(function(){
				this.src = this.src.replace("_a.","_i.");
			});
	},
	init: function(locality){
		if(locality == null) {
			locality = "body";
		}
		this.targetBlank(locality);
		this.inputAutoClear(locality);
		this.imgRollover(locality);
	}
}
