// JavaScript Document// JavaScript Document

// WTF Functions 
// Updated: 2011-12-26
												
							
$(document).ready(function() {

//Pop-Up
var profiles = { };
	 
 resize = function(sizeRatio, element) {
	  
	newWidth = 611;
	newHeight = Math.round(newWidth / sizeRatio);
	element
		.attr('width', newWidth)
		.attr('height', newHeight)
		.parent()
			.attr('width', newWidth)
			.attr('height', newHeight);
	 
 }

 $('embed[src*="youtube.com"], iframe[title*="YouTube"], iframe[src*="vimeo"]').each(function(){
	sizeRatio = $(this).attr('width') / $(this).attr('height');
	resize(sizeRatio, $(this)); 
	 
 });
 
 
 //Insurance Policy
  $('.entry img, .listing img').each(function(){
	sizeRatio = $(this).attr('width') / $(this).attr('height');	
	if( $(this).attr('width') > 611) {
	resize(sizeRatio, $(this)); 
	}
	 
 });
 
								
$('#comment-total li a, #facebook-options li a ').click(function(){		
		$.scrollTo( $('div#entry_comments'), 800, {offset:-50}	)
		return false;
	});
	
	
$('#social-nav ul li a[title]').qtip({
	
	position: {
   target: 'mouse',
   adjust: { mouse: true },
   corner: {
      target: 'bottomRight',
      tooltip: 'topLeft'
   }
},
show: { 
	effect: 'fade' 
	},
style: {  
	 background:'#09f',
	 padding:12,
	 color:'#222',
	 	border: {
		color:'#09f' 
		        },
      	tip: { 
         corner: 'topLeft',
         color: '#09f',
		 	size: {
          	  x: 10, 
          	  y : 11
        		 }
			  } 		 
    }
		 
	
});


$.each($("a.synopsis"), function(i,val){
    $.fn.qtip.styles.mystyle = { 
    background:'#09f',
	width:350,				   
	 padding:12,
	 color:'#222',
	 a: { display: 'none'},
	 	border: {
		color:'#09f' 
		        },
      	tip: { 
         corner: 'bottomLeft', 
         color: '#09f',
		 	size: {
          	  x: 10,
          	  y : 8 
        		 }
			  } 		 ,
                   tip: 'bottomLeft',
                   name: 'dark'
                }

                //var theContent = $('#table-synopsis' + $(this).attr("id").slice(8) + '').html()
				var theContent = $(this).siblings('div.table-synopsis').html(); 
				$(val).qtip({
                    content: theContent,
                     position: {
                              corner: {
                                 target: 'topRight',
                                 tooltip: 'bottomLeft'
                              }
                           },
                       style: 'mystyle'
                });
            });


$(".popupwindow").popupwindow(profiles);
	
$('ul.video-grid li a[title], .episode-header ul li a[title],.blog-entry-header ul li a[title], .episode-actions a[title], #marquee-actions a[title],  .archive-subscribe div a[title]').qtip({
	
position: {
   target: 'mouse',
   adjust: { mouse: true },
   corner: {
      target: 'bottomLeft',
      tooltip: 'bottomLeft'
   }
},
show: { 
	effect: 'fade' 
	},
style: {  
	 background:'#09f',
	 padding:12,
	 color:'#222',
	 	border: {
		color:'#09f' 
		        },
      	tip: { 
         corner: 'bottomLeft', 
         color: '#09f',
		 	size: {
          	  x: 10,
          	  y : 8 
        		 }
			  } 		 
    }
   
});



$("a[rel^='prettyPhoto']").prettyPhoto({
	animation_speed: 'fast',
	overlay_gallery: false, 
	theme: 'light_square'
	});
	



$('#banner-about-content .cycle').cycle({ 
	fx:'scrollDown',
	timeout:5000, 
	cleartypeNoBg: true,
	pager: '#aboutnav',
	 pagerAnchorBuilder: function(idx, slide) { 
		
		return '<li><a class="cyclenav"></a></li>'; 
    	
		} 	

});	


$("#twitter-feed div.tweet").tweet({
            username: "wtfpod",
            join_text: "auto",
            avatar_size: 0,
            count: 1,
            loading_text: "loading tweets...",
			auto_join_text_default: "",
			auto_join_text_ed: "", 
			auto_join_text_ing: "",
			auto_join_text_reply: "",
			auto_join_text_url: "",		
        });
		
$("#twitter-maron div.tweet").tweet({
            username: "marcmaron",
            join_text: "auto",
            avatar_size: 0,
            count: 1,
            loading_text: "loading tweets...",
			auto_join_text_default: "",
			auto_join_text_ed: "", 
			auto_join_text_ing: "",
			auto_join_text_reply: "",
			auto_join_text_url: "",		
        });


//When page loads...
	$(".tab_content").hide(); //Hide all content
	$("ul.tabs li:first").addClass("active").show(); //Activate first tab
	$(".tab_content:first").show(); //Show first tab content

	//On Click Event
	$("ul.tabs li").click(function() {

		$("ul.tabs li").removeClass("active"); //Remove any "active" class
		$(this).addClass("active"); //Add "active" class to selected tab
		$(".tab_content").hide(); //Hide all tab content
		
		//setVideoCycle();

		var activeTab = $(this).find("a").attr("href"); //Find the href attribute value to identify the active tab + content
		$(activeTab).fadeIn(); //Fade in the active ID content
		
		
		return false;
	});
   			
	  $('div.video-cycle').cycle({ 
		fx:'fade',
		timeout:0, 
		pager:  '#conan-nav', 
    	pagerAnchorBuilder: function(idx, slide) { 
   	     // return selector string for existing anchor 
        return '#conan-nav li:eq(' + idx + ') a'; 
    	} 
	
	});		
	
	
	$('#youtube-gallery ul li a').each(function(index, value){  
	  url = $.jYoutube($(this).attr('href'));
	  $(this).append($('<img src="'+url+'" />'));

	});	
	
	
	$('a.btn-guide').hover(
	  function(){
		$(this).fadeTo("fast", 0.70);
	  },
	  function(){
		   $(this).fadeTo("fast", 1);
	  }
	);
	
	
	
// repeatHeaders widget
$.tablesorter.addWidget({
			// give the widget a id
			id: "repeatHeaders",
			// format is called when the on init and when a sorting has finished
			format: function(table) {
				// cache and collect all TH headers
				if(!this.headers) {
					var h = this.headers = []; 
					$("thead th",table).each(function() {
						h.push(
							"<th>" + $(this).text() + "</th>"
						);
						
					});
				}
				
				// remove appended headers by classname.
				$("tr.repated-header",table).remove();
				
				// loop all tr elements and insert a copy of the "headers"	
				for(var i=1; i < table.tBodies[0].rows.length+1; i++) {
					// insert a copy of the table head every 10th row
					if((i%25) == 0) {
						$("tbody tr:eq(" + i + ")",table).before(
							$("<tr></tr>").addClass("repated-header").html(this.headers.join(""))
						
						);	
					}
				}
				
			}
		});
		
		
		
$('input#id_search').quicksearch('#download-guide table tbody tr',  {
		'onBefore': function () {
			$("tr.repated-header").hide();
			},
		'onAfter': function () {
			if ($('#id_search').val() == '') {
				$("tr.repated-header").show();
				}
			}
		});
	
		
		// call the tablesorter plugin and assign widgets with id "zebra" (Default widget in the core) and the newly created "repeatHeaders"
$("#download-guide table").tablesorter({
			widgets: ['zebra','repeatHeaders'],
			sortList: [[0,0]], 
			headers: { 
			 1: {  sorter: false },
			  2: {  sorter: false },
			   3: {  sorter: false },
			    4: {  sorter: false },
				 5: {  sorter: false },
				  6: {  sorter: false },
				   7: {  sorter: false },
				    8: {  sorter: false },
			 	}				 
			});


$('a[name=modal]').click(function(e) {
		//Cancel the link behavior
		e.preventDefault();
		
		//Get the A tag
		var id = $(this).attr('href');
	
		//Get the screen height and width
		var maskHeight = $(document).height();
		var maskWidth = $(window).width();
	
		//Set heigth and width to mask to fill up the whole screen
		$('#mask').css({'width':maskWidth,'height':maskHeight});
		
		//transition effect		
		$('#mask').fadeIn(1000);	
		$('#mask').fadeTo("slow",0.8);	
	
		$(id).centerInClient();

		//transition effect
		$(id).fadeIn(2000); 
	
	});
	
	//if close button is clicked
$('.window .close').click(function (e) {
		//Cancel the link behavior
		e.preventDefault();
		
		$('#mask').hide();
		$('.window').hide();
	});		
	
	//if mask is clicked
$('#mask').click(function () {
		$(this).hide();
		$('.window').hide();
	});			
	
	
				
$(function() {
     $("#SelectURL").change(function() {     
            if ($(this).val() != '----------' &&  $(this).val() != 'year') {
                window.open($(this).val());
            }
        });
    });


function isIOS(){
    return (
        (navigator.platform.indexOf("iPhone") != -1) ||
        (navigator.platform.indexOf("iPod") != -1) ||
        (navigator.platform.indexOf("iPad") != -1)
    	);
	}

if(isIOS()){
	var fallBack = '<a href="http://itunes.apple.com/us/album/this-has-to-be-funny/id452595590" class="fallBack"> </a>';
	$("#merchbox-album").html(fallBack);  
	}

});



