/**^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^document ready start**/$(document).ready(function(){

anchor=document.location.hash.substring(1);
universalcontrols();
tabs();
carousel();
videopage();
headline_scroller();


/**^^^^^^^^^^^Rollover Ads**/

$('#flashAd193').mouseenter(function(){
			$(this).css({'width': '500px','margin-left': '-200px'});
		}).mouseleave(function(){
			$(this).css({'width': '300px','margin-left': '0px'});
			so.write("flashAd193c");
		});
		
$('#flashAd291').mouseenter(function(){	
			$(this).css({'width': '500px','margin-left': '-200px'});
		}).mouseleave(function(){
			$(this).css({'width': '300px','margin-left': '0px'});
			so3.write("flashAd291c");
		});
				
$('#flashAd196').mouseenter(function(){
			$(this).css({'height': '179px'});
		}).mouseleave(function(){
			$(this).css({'height': '90px'});
			so3.write("flashAd196c");
		});
	
$('#flashAd197').mouseenter(function(){
			$(this).css({'width': '320px','margin-left': '-200px'});
		}).mouseleave(function(){
			$(this).css({'width': '160px','margin-left': '0px'});
			so2.write("flashAd197c");
		});
		
$('#widget_thumbs li.live').hover(function() {
		$(this).css('background-position', '0 -36px');
	}, function() {
		$(this).css('background-position', '0 0');
	});
	
$('#widget_thumbs li.live').click(function() {
	$('#widget_thumbs li.live').css('background-position', '0 0');
	$(this).siblings().removeClass('select').addClass('live');
	$(this).addClass('select').removeClass('live');
	var divid= $(this).attr('id');
	$('#' + divid + '_info').show().siblings().hide();
});

/*
var imgCount = $('.author_container img').length;
alert(imgCount);
*/

if ($('.author_container img').length == 0) {
	$('.blog_excerpt').css('width', '100%');
} else if ($('.author_container img').length == 1) {
	$('.blog_excerpt').css('width', '510px');
} else if ($('.author_container img').length == 2) {
	$('.blog_excerpt').css('width', '420px');
} else if ($('.author_container img').length == 3) {
	$('.blog_excerpt').css('width', '325px');
} else if ($('.author_container img').length >= 4) {
	$('.author_container img').css('width', '57px');
	$('.author_container').css('width', '248px');
	$('.blog_excerpt').css('width', '354px');
}
   

if ($.browser.msie && $.browser.version <= 8) {
	$('#channels li a').css('padding', '5px 10px');
	$('#channels').css('margin-left', '7px');
	$('#blogs_top #blog_search input[type="submit"]').css('padding', '4px 10px 3px 10px');
};

if ($.browser.msie && $.browser.version >= 9) {
	$('#blogs_top #blog_search input[type="submit"]').css('padding', '5px 10px 3px 10px');
};

if ($.browser.msie && $.browser.version == 8) {
	$('#blogs_top #blog_search input[type="submit"]').css('padding', '6px 10px 4px 10px');
};

if ($.browser.msie && $.browser.version <= 7) {
	$('#blogs_top #blog_search input[type="submit"]').css('padding', '4px 10px 3px 10px');
};


/**^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^document ready end**/});

$(window).unload(function(){

holderheight=$("div#video_holder").height();
$("#video_holder").html("").css("height",holderheight).css("background-color","#fff");
});


/**^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^start universal controls**/
function universalcontrols(){

		
	if($("#series_videos li").size()==1){ //if series videos has only one link, hide it and the counter
			$("#series_videos").hide();
			$("#video_series p.controls").hide();
	}
	$("body").prepend("<div id='popupholder'></div>");
	$("form.validate").validate();
	
	$("input.nospaces").live("keyup",function(){
			x=$(this).val().replace(/ /g,'');
			$(this).val(x);
	});
	
	$("#podcast select").change(function(){
		$(this).closest("form").submit();
	});
	
	$("input[type='checkbox']").addClass("checkbox");
	
	$("input[type='text'], input[type='password']").addClass("text");
	$("input[type='text'],input[type='password']").each(function(){ 
		$(this).addClass("text");
		if($(this).val()!=""){$("label[for="+$(this).attr('id')+"] span.direction").hide();}
	});

	$("form.ajax input[type='submit']").live("click",function(){
	    thisform=$(this).closest("form.ajax");
		if(thisform.valid()){
			thisform.validate();
			thisform.find("input, textarea, select").attr("readonly",true);
			$.post(thisform.attr("action"), thisform.serialize(), function(returnedhtml){
					thisform.after(returnedhtml);
					thisform.hide();
				});
		}
		return false;
	});


	
	$("#popupholder").prepend('<div id="popupform" style="display:none;"><a href="#" class="close">&#215;</a><div></div></div>');

	
	$("label span.direction").each(function(){
		$("#"+$(this).parent().attr("for"))
			.focus(function(){$("label[for="+$(this).attr("id")+"] span.direction").css("opacity","0")})
			.blur(function(){if($(this).val()==""){$("label[for="+$(this).attr("id")+"] span.direction").css("opacity","1")}});
	});
	
	$("a.disabled").click(function(){
		return false;
	});
	
	$("#footer div.bmc a").mouseover(
		function(){magazine_id=$(this).attr("rel");
		if($(this).parent().find("img.cover").length==0){
		currpub=$("#footer div.bmc a").index(this);
		$.get('/assets_tube/includes/footer_pub_popup.asp?magazine_id='+magazine_id, show_pub_cover);
	}})
	.mouseout(function(){$("#footer div.bmc img.cover").remove();});

	$("#user>ul li a").click(function(){
			$(this).parent().siblings().removeClass("active");
			$(this).parent().addClass("active");
			$(this).closest("div").find("form").removeClass("active");
			$(this).closest("div").find("form").eq($("#user li a").index(this)).addClass("active");
			return false;
	});

	if($("#remember_me").attr("checked")==true){
		$("#user label[for='username']").hide();	
		$("#user label[for='password']").hide();	
	}
	
	$("#user input[type='text'], #user input[type='password']")
		.focus(function(){	$(this).parent().find("label").hide();	})
		.blur(function(){if($(this).val()==""){$(this).parent().find("label").show();} }); 
	
	
		$("img").fixBroken();
		$(".thumbnail.hd img").after("<span></span>");
		$("div#skyscraper a, div#leaderboard a, a.newtab, a.newwindow, a[rel='newtab'], a[rel='newwindow']").live("click",function(){window.open(this.href);return false;});
		$("[title]").tooltip({delay:200,fade:250, showURL:false});

		
		$("a.button").live("mousedown", function(){$(this).addClass("active");})
					.live("mouseup", function(){$(this).removeClass("active");});
					
		$("a#closepopup").live("click",function(){ $("#popup").remove();return false;});
		
		$("input[type='submit']")
			.live("mousedown",function(){$(this).addClass("active");})
			.live("mouseup",function(){$(this).removeClass("active");})
			.live("mouseout",function(){$(this).removeClass("active");});
		
		
		/********** THUMBNAIL HOVER EFFECT**/
		$("ul.videos li a")
		.live("mouseover",function(){$(".thumbnail").removeClass("active");	$(this).closest("li").find(".thumbnail").addClass("active");})
		.live("mouseout",function(){$(".thumbnail").removeClass("active")});
		
		$("img.thumbnail")
		.live("mouseover",function(){$(".thumbnail").removeClass("active");$(this).addClass("thumbnail");})
		.live("mouseout",function(){$(".thumbnail").removeClass("active");});
		
		
		$("label.switch").click(function(){
			if($("input[type='checkbox']#"+$(this).attr("for")).is(':checked')){
				switchoff($(this));
			}
			else{
				switchon($(this));
			}
			return false;
		});
		

		
	/********* popup forms ************/
	$("a.popupform").live("click",function(){
		popupform($(this).attr("href"));
		return false;
	});
	
		
	$("#popupform form input[type='submit']").live("click",function(e){
			if($(this).hasClass("close")){
				closepopup();
				closepopupform();
				return false;
			}
			else{
				a=$(this).closest("form").attr("method");
				b=$(this).closest("form").attr("action");
				c=$(this).closest("form").serialize();
				d=$(this).closest("form").attr("enctype");
				$.ajax({
					type: a,
					url: b,
					enctype: d,
					data: c,
					cache:false,
					success: function(data){
							if(data.indexOf("<form")>=0){  //if the data contains a form tag somewhere
								$("#popupform div").html(data); //show form again (white background)
							}
							else{
								closepopupform(); //close form
								popup(data); //pop up green box
							}
					}
				});
				}

			if($(this).parents("form:first").hasClass("upload")){
					$("form.upload").attr("target","upload_target");
					$("#edit_public_profile img.profile_pic").hide();
					$("#edit_public_profile a.upload_new").hide();					
					$("#edit_public_profile a.upload_new").after("Your picture has been changed.");
					closepopupform();
			}
			else{
					return false;
			}
	});
	
	$("a.close").live("click",function(){
		closepopup();
		closepopupform();
		return false;
	});

	
	/********* popup submit ************/
	$("a.popupsubmit").click(function(){
		a=$(this).attr("href");
		$.get(a, popup);
		return false;
	});
	
	
	/********* popup file ************/
	$("a.popup").click(function(){
		a=$(this).attr("href");
		if($(this).hasClass("large")){
			$.get(a, popupfilelarge);
		}
		else{
			$.get(a, popupfile);
		}
		return false;
	});	
	
	
	$(".tree").click(function(){
		treeindex=$(".tree").index(this);
		if($(this).hasClass("expanded")){ //collapse it
			$(this).removeClass("expanded");
			$(this).addClass("collapsed");
			$(".branch").eq(treeindex).hide();
		}
		else{
			$(this).addClass("expanded");
			$(this).removeClass("collapsed");
			$(".branch").eq(treeindex).show();
		}
		return false;
	});
	
	$("#overlay").live("click",function(){
		closepopup();
		closepopupform();
	});
	
	$("a.remove").click(function(){
		removeindex=$("a.remove").index(this);
		if($(this).hasClass("popupform")){
		}
		else{		removefrompage();
		}
	});
	
	$('.blog_share li:first-child a').css('padding-left', '0');
	$('.blog_share li:last-child a').css('border-right', '0');
	
	

/**^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^end  universal controls**/}


function carousel(){
	if($(".carousel>ul>li").length>=3){
	$(".carousel").before('<a href="#" class="prev button">&lt;</a><a href="#" class="next button">&gt;</a>');
    $(".carousel").jCarouselLite({btnNext: ".next",btnPrev: ".prev"});
	}
}
	

var currpub=0;
function show_pub_cover(data) {
		$("#footer div.bmc img.cover").remove();
		$("#footer div.bmc a").eq(currpub).after(data);
		$("#footer div.bmc img.cover").animate({top:"-220px"},{duration:400});
}
		  
function delhash(d){
	d=Right(d,d.length-1);
	return d;
}
		 
function removefrompage(){
	$("a.remove").eq(removeindex).parent().slideUp(300);
}

function popup(x){
	$("#popup").remove();
	$("#popupholder").prepend('<div id="popup"><p>'+x+'<br /><br /><a href="#" class="button" id="closepopup">OK</a></p></div>');
}
function closepopup(){
	hideoverlay();
	$("#popup").remove();
	$("#popupfile").remove();

}
function closepopupform(){
	hideoverlay();
	$("#popupform").hide();
}

function popupfile(x){
	showoverlay();
	$("#popupfile").remove();
		$("#popupholder").prepend('<div id="popupfile"><a href="#" class="close">&#215;</a>'+x+'</div>');
}

function popupfilelarge(x){
	showoverlay();
	$("#popupfile").remove();
	$("#popupholdre").prepend('<div id="popupfile" class="large"><a href="#" class="close">&#215;</a>'+x+'</div>');
}

function popupform(x,y){
			showoverlay();
			$("#popupform div").html("");		
			$("#popupform div").load(x); 
			
			if(y=="wide"){
				//$("#popupform").css("width","480px");
				//$("#popupform").css("margin-left","270px");
			}
			$("#popupform").show();
}

function showoverlay(){
	$("div#overlay").remove();
	$("#container").after('<div id="overlay"></div>');
}
function hideoverlay(){
	$("div#overlay").remove();
}


$.fn.fixBroken = function(){
	return this.each(function(){
	var tag = $(this);
	var alt_img = 'http://bmctoday.net/assets_tube/images/thumbs/sorry.jpg';
		tag.error(function() { // this adds the onerror event to images
		tag.attr("src",alt_img); // change the src attribute of the image
		return true;
		});
	});
};
	

function bookmarkPage(title, url) {
	if (window.sidebar) {
		window.sidebar.addPanel(title, url, "");
	} else if(window.opera && window.print) {
		var elem = document.createElement('a');
		elem.setAttribute('href',url);
		elem.setAttribute('title',title);
		elem.setAttribute('rel','sidebar');
		elem.click();
	} else if(document.all) {
		window.external.AddFavorite(url, title);
	}
};

function switchoff(thelabel){
				$("input#"+thelabel.attr('for')).attr("checked",false);
				thelabel.removeClass("active");
				thelabel.css("background","url(/assets_tube/images/forms.png) 0px -630px no-repeat");
				thelabel.find("span").html("Off");
}
		
function switchon(thelabel){
				$("input#"+thelabel.attr('for')).attr("checked",true);
				thelabel.addClass("active");
				thelabel.css("background","url(/assets_tube/images/forms.png) 0px -600px no-repeat");
				thelabel.find("span").html("On");
}

function tabs(){
	thehtml="";
	$(".tabs>div h2").each(function(){
		thehtml=thehtml+('<li><a href="#tab_'+$(this).parent().attr("id")+'">'+$(this).html()+'</a></li>');
		$(this).hide();
	});
	
	$("div.tabs").prepend('<ul class="tabs">'+thehtml+'</ul>');
	
	$(".tabs ul.tabs a").click(function(){
			pos=$(".tabs a").index(this);
			$(".tabs a").removeClass("active");
			$(this).addClass("active");
			$(".tabs>div").hide();
			$(".tabs>div").eq(pos).show();
			window.location=this.href;
			return false;
	});
		
	//on load, detect if anchor is a tab and show
	$(".tabs a").removeClass("active");

	$(".tabs>div").hide();
	anchoristab=false;		
	if(anchor!="" && anchor.substr(0,3)=="tab"){
		whichone=$(".tabs>div").index($("#"+anchor.substr(4,anchor.length)));
		anchoristab=true;
	}
	else{whichone=0;}
	
	$(".tabs>div").eq(whichone).show();
	$(".tabs ul.tabs a").eq(whichone).addClass("active");
	
}


function headline_scroller(){
	var headline_count = $("div.headline").size();
	var current_headline=0;
	$("div.headline:eq("+current_headline+")").css('top', '70px');
	setInterval(headline_rotate,4000); 
}
	
function headline_rotate() {
  old_headline = current_headline % headline_count;
  new_headline = ++current_headline % headline_count;
  $("div.headline:eq(" + new_headline + ")").fadeIn().animate({top: 70},1500);     
  $("div.headline:eq(" + old_headline + ")").fadeOut(1000,function(){css('top', '500px')});
}

function videopage(){
	//$("#video_info p.description").jTruncate({length: 200});

	$("#mixtape_videos .mixtapes")
	.mouseover(function(){$(this).find("ul").show();})
	.mouseout(function(){$(this).find("ul").hide();});
	
	$("#mixtape_videos .mixtapes li")
	.mouseover(function(){	$(this).addClass("active")})
	.mouseout(function(){	$(this).removeClass("active")});
	
	$("#mixtape_videos a").click(function(){
		$("#mixtape_videos h3").html($(this).html()+"&#9660;");
		$("#mixtape_videos .mixtapes").find("ul").hide();	
		$("#series_videos").load($(this).attr("href"));
		return false;
	});	
	
	$("#quicklink").click(function(){
		popup("Here's a QuickLink to this video:<br />"+$("#quicklink_url").val());
	});
}

	
function preroll_countdown(){
	if(preroll_seconds==2){ //if finishing up countdown 
		$("#ad_preroll p.status").slideUp(1000); //take away the "your video will start in..." text
			$("#ad_preroll div.collapsed").slideDown(1000,function(){		 //show the skinny ad
				$("#ad_preroll div.expanded").hide(); //hide the big ad
				collapseheight=$(this).height(); 
				$("#video_holder").css("padding-top",collapseheight); //make the video_holder div as big as the big ad
				so.write("video_holder"); //write the flash include
			});
	    clearInterval(preroll_countdown_interval);
	}
	else{
	if(preroll_seconds>=1){
		preroll_seconds--;
		preroll_seconds_word="seconds.";
		if(preroll_seconds==1){
			preroll_seconds_word="second.";
		}
		$("#ad_preroll p.status").html("Your video will start in "+preroll_seconds+" "+preroll_seconds_word);
	}
	}
}		

function preroll_start(secs){
	$("#video_holder").css("min-height","475px");
	preroll_seconds=secs+1;
	$("#ad_preroll p.status").html("Your video will start in "+secs+1+" seconds.");
	preroll_countdown_interval=setInterval(preroll_countdown,1000);
}
