/**^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^document ready start**/$(document).ready(function(){

anchor=document.location.hash.substring(1);
universalcontrols();
tabs();
carousel();
videopage();
headline_scroller();



/**^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^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(){

	$("form.validate").validate();
	
	$("input.nospaces").live("keyup",function(){
			x=$(this).val().replace(/ /g,'');
			$(this).val(x);
	});
	$("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();}
	});


	
	$("#container").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").hide()})
			.blur(function(){if($(this).val()==""){$("label[for="+$(this).attr("id")+"] span.direction").show()}});
	});
	
	
	$("#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").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();
		}
	});
	

/**^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^end  universal controls**/}


function carousel(){
	if($(".carousel>ul>li").length>>2){
	$(".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();
	$("#container").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();
		$("#container").prepend('<div id="popupfile"><a href="#" class="close">&#215;</a>'+x+'</div>');
}

function popupfilelarge(x){
	showoverlay();
	$("#popupfile").remove();
	$("#container").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("left","270px");
			}
			$("#popupform").show();
}

function showoverlay(){
	$("div#overlay").remove();
	$("#footer").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());
	});
}

	
		
