// site specific javascript

if(!jQuery.browser.safari)
{
	jQuery(document).ready(function(){
		behavior_binder();
	});
} else {
	jQuery(window).load(function(){
		behavior_binder();
	});
}


/*
Binds behaviors
** can be re-run whenever there are DOM changes
*/
var GENRE_ACCORION_HEIGHT=false;
function behavior_binder(){

/* DRAWS THE FONT IMAGE FOR THE TARGETED ELEMENT
	*/
	
	jQuery('.module_header h2').drawFont();
	jQuery('.module_title').drawFont();
	
/* TURNS ON INTERNAL TABS
	(requires jquery-ui-tabs.js)
	*/
	jQuery("ul.module_tabs").tabs();


	jQuery('ul.star_rating').snasRate();
	jQuery('div.module_article_comments').snasCommenting();
	jQuery('.comments_counter').numComments();
	jQuery('ul#video_category_menu .selected').each(function(){
		jQuery(this).parent().parent().css('display','block');
	});
	jQuery('ul#video_category_menu > li').each(function(){
		if(jQuery('ul',this).length){
			jQuery(this).bind('click',function(e){toggle_video_menu(e)});
		}
	});
	jQuery('div#scifi_daily div.scifi_daily_row').each(function(){by_genre_accordion_init(this);});
	fix_mt_pager();
	init_community_pager();
}
var COMMUNITY_PAGE_AT=0;
var COMMUNITY_PAGE_STEPS=4;
var COMMUNITY_PAGE_OFFSET=0;
function init_community_pager(){
	if(jQuery('#discuss_shows_list_container').length){
		var list_height=jQuery('#discuss_shows_list').height();
		var container_height=jQuery('#discuss_shows_list_container').height();
		if(list_height>container_height){
			COMMUNITY_PAGE_OFFSET=(list_height-container_height)/COMMUNITY_PAGE_STEPS;

			jQuery('#discuss_shows_list_controller #discuss_shows_list_down').bind('click',function(e){
				e.preventDefault();
				if(COMMUNITY_PAGE_AT<COMMUNITY_PAGE_STEPS){
					COMMUNITY_PAGE_AT++;
					var moveto=-(COMMUNITY_PAGE_AT*COMMUNITY_PAGE_OFFSET);
					jQuery('#discuss_shows_list').animate({marginTop:moveto});
				}
			});
			jQuery('#discuss_shows_list_controller #discuss_shows_list_up').bind('click',function(e){
				e.preventDefault();
				if(COMMUNITY_PAGE_AT>0){
					COMMUNITY_PAGE_AT--;
					var moveto=-(COMMUNITY_PAGE_AT*COMMUNITY_PAGE_OFFSET);
					jQuery('#discuss_shows_list').animate({marginTop:moveto});
				}
			});
		}
		else{
			jQuery('#discuss_shows_list_controller li').css('display','none');
		}

	}
}
function fix_mt_pager(){
	if($('ul.pagination li.mt_pager').length){
		var pages=[];
		var prev=false;
		var next=false;
		$('ul.pagination li.mt_pager a').each(function(){
			if($(this).attr('rel')!=''){
				if($(this).attr('rel')=='prev'){
					prev=$(this).attr('href');
				}
				else{
					next=$(this).attr('href');
				}
			}
			else{
				if(parseInt($(this).html())){
					pages[parseInt($(this).html())]=$(this).attr('href');
				}
				
			}
			
		});
		$('ul.pagination li.mt_pager b').each(function(){
			pages[parseInt($(this).html())]=false;
		});
		var list='';
		if(prev){
			list+='<li class="pagination_prev"><a href="'+prev+'"><span class="angled_quotes">&#171;</span> Prev</a></li>';
		}
		for(var i=0;i<pages.length;i++){
			if(pages[i]!==undefined){
				if(pages[i]===false){
					list+='<li class="selected"><a href="javascript:void();">'+i+'</a></li>';
				}
				else{
					list+='<li><a href="'+pages[i]+'">'+i+'</a></li>';
				}
			}
		}
		if(next){
			list+='<li class="pagination_next"><a href="'+next+'">Next <span class="angled_quotes">&#187;</span></a></li>';
		}
		$('ul.pagination').html(list);
	}
}
function by_genre_accordion_init(row){
	if(jQuery('li',row).length>9){
		jQuery(row).append('<p class="accordion_btn"><a href="javascript:void()" class="more_btn">more</a><p>');
		jQuery('p.accordion_btn a',row).toggle(function(e){expand_genre_accordion(e)},function(e){shrink_genre_accordion(e)});
		if(jQuery('li:gt(3)',row).is('.selected')){
			jQuery('p.accordion_btn a',row).click();
		}
	}
}
function expand_genre_accordion(e){
	var list=jQuery(e.target).parent().parent();
	if(GENRE_ACCORION_HEIGHT===false){
		GENRE_ACCORION_HEIGHT=jQuery(list).css('height');
	}
	jQuery(list).css({height:'auto'});
	jQuery('p.accordion_btn a',list).addClass('back_btn');
	jQuery('p.accordion_btn a',list).removeClass('more_btn');
}
function shrink_genre_accordion(e){
	var list=jQuery(e.target).parent().parent();
	jQuery(list).css({height:GENRE_ACCORION_HEIGHT});
	jQuery('p.accordion_btn a',list).addClass('more_btn');
	jQuery('p.accordion_btn a',list).removeClass('back_btn');
}
function toggle_video_menu(e){
	if(jQuery('ul',jQuery(e.target).parent()).css('display')=='none'){
		jQuery('ul',jQuery(e.target).parent()).slideDown();
	}
	else{
		jQuery('ul',jQuery(e.target).parent()).slideUp();
	}
}
function nav_select(this_section){
	jQuery('#navigation .selected').removeClass('selected');
	jQuery('#'+this_section).addClass('selected');
}

jQuery(document).ready(function(){
	jQuery('ul#video_category_menu li.expand').each(function() {	
		jQuery(this).children('a').attr({href: 'javascript: void(0);'});
		jQuery(this).children('a').click(function() {
			jQuery(this).parent().children('ul').toggle();
		});
	});
});
