$(document).ready(function() {
	
	
	/* ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ
		Copy Box: Put tops and bottoms on them
	ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ  */
	var copyBox = $("div[class='copyBox']");
	
	//if (!$.browser.msie) {
	if (0==0) {
		//copyBox.before("<div class='copyBoxTop'></div>");
		copyBox.after("<div class='copyBoxBottom'></div>");
	}
	
	else {
		var cp = $("div[class='copyBox']");
		cp.before("<div class='copyBoxTop'><" + "/div>");
		cp.after("<div class='copyBoxBottom'><" + "/div>");
		

		var stuff = $("#masthead,#footer,div[class='contact_links'],div[class^='canvas'],div[class^='copyBox']");
		//stuff.css("border","1px solid red");
		
		stuff.wrap("<table cellspacing='0' cellpadding='0' width='100%' align='center' border='0'><tr><td align='center'></td></tr></table>");
		
		$("div[class='copyBox']").css("width","1000px");
		$("div.copyBox").css("width","1000px");
		$("div[class^='copyBo']").css("width","1000px");
		$("div[class^='canva']").css("width","1000px");
	}


	/* ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ
		Upper Masthead Links: Email Updates form, RSS, Twitter
	ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ  */
	//Handle the contact_links
	var d = $("div.contact_links");
	var e_link = d.find("#get_email_updates");
	e_link.bind("click",function() {
		//hide the first row
		var ths = $(this);
		ths.css("cursor","pointer");
		ths.removeAttr("href");
		$("table.links tr:first").hide();
		
		//show the get_email_udates
		$("table.links td.get_email_updates").show();
		//make sure the submit button is visible
		$("table.links td.get_email_updates input [type='submit']").show();		
		
		//Enable the click behavior for the email updates input
		$("table.links input[type='text']").one("click",function() {
		$(this).val("");
		});
	});
	

	/* ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ
		Email updates form: Hide signup btn when clicked
	ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ  */
	//var signup_btn = $("div.signup_for_email_updates input[type='submit'],#signup_for_email_updates_form_submit");
	
	var signup_btn = $("div.signup_for_email_updates input[type='submit']");
	signup_btn.bind("click",function() {
		var ths = $(this);

		//Submit, then trigger the cancel click
		//$("table.links a:contains('Cancel')").trigger("click");
	});
	
	function hideCancel() {
		$("table.links td.get_email_updates").hide();
		$("table.links tr:first").show();
		$("table.links input[type='text']").val("Enter your email address here");
	}
	
	
	/* ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ
		Email updates form: Cancel link
	ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ  */
	var c_link = $("table.links a:contains('Cancel')");
	c_link.bind("click",function() {
		var ths = $(this);
		ths.removeAttr("href");
		ths.css("cursor","pointer");
		hideCancel();
	});
	
	/* ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ
		Email updates form: Button and textfield behavior
	ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ  */
	var signup_txt = $("div.signup_for_email_updates input.signupText");
	signup_txt.bind("focus",function() {
		ths = $(this);
		ths.addClass("clicked");
	});
	
	signup_txt.bind("change",function() {
		ths = $(this);
		ths.removeClass("clicked");
	});
	
	

	/* ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ
		Highlight the current location in the masthead - if appropriate
	ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ  */
	var current_location = $("#masthead").attr("location");
	var current_link = $("#masthead div.links").find("a:contains('" + 
	current_location + "')");
	current_link.css("color","#FFFFFF");
		
	
		/* ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ
		Activate all div.copy screencast links (any tags). For example...
		<a youtube_id="some_youtube_id">Click here</a>
	ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ  */
	var youtube_links = $("*[youtube_id], #homepageCanvas [youtube_id]");
	youtube_links.each(function() {
		var ths = $(this);
		//Get the tagName and the youtube_id
		var tagName = ths.get(0).tagName;
		var id = ths.attr("youtube_id");
		
		//Set up some defaults
		var w = 640;
		var h = 400;
		var hd = false;
		
		//Now see if the tag wants to pass its own dimensions
		if (ths.attr("w")) {
			var temp_w = parseInt(ths.attr("w"));
			if (temp_w > 0) w = temp_w;
		}
		if (ths.attr("h")) {
			var temp_h = parseInt(ths.attr("h"));
			if (temp_h > 0) h = temp_h;
		}
		if (ths.attr("hd")) {
			hd = true;
		}
		
		
		//Ensure the css
		ths.css({"cursor":"pointer"});
		
		//Based on current location
		if ($("#masthead").attr("location") != "Tutorials") {
			ths.addClass("tutorial_link");
		}
		
		//Give the element a mouseover effect
		ths.bind("mouseenter",function() {
			$(this).addClass("clickable");
		});
		
		ths.bind("mouseleave",function() {
			$(this).removeClass("clickable");
		});
		
		
		ths.bind("click",function() {
			var ths = $(this);
			//remove any href it may have
			ths.removeAttr("href");
	
			//Also find anything else that may need that class
			mark_as_seen(id);
			drawFlash_youtube_single(id,w,h,hd);
			
		});
	});
	
	
	/* ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ
		Attach a click event to screencast thumbnails that
		TRIGGER the click event for the correct span 
	ﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐﾐ */
	var thumbs = $("td.thumbnail img");
	thumbs.wrap("<div class='thumbnail_outliner'></div>")
	//add the arrows
	thumbs.parent().prepend("<div class='thumbnail_arrow'></div>")
	
	//bind the arrows to the right trigger
	var arrows = $("div.thumbnail_arrow");
	
	//var thumbs_and_arrows = $(thumbs,arrows);
	thumbs.bind("click",function() {
		var target_link = $(this).parent().parent().next().find("span.title[youtube_id]");
		target_link.trigger("click");
	});
	arrows.bind("click",function() {
		var target_link = $(this).parent().parent().next().find("span.title[youtube_id]");
		target_link.trigger("click");
	});
	
	thumbs.bind("mouseenter",function() {
		var target_link = $(this).parent().parent().next().find("span.title[youtube_id]");
		target_link.trigger("mouseenter");			   
    });
	arrows.bind("mouseenter",function() {
		var target_link = $(this).parent().parent().next().find("span.title[youtube_id]");
		target_link.trigger("mouseenter");			   
    });
	
	thumbs.bind("mouseleave",function() {
		var target_link = $(this).parent().parent().next().find("span.title[youtube_id]");
		target_link.trigger("mouseleave");			   
    });
	arrows.bind("mouseleave",function() {
		var target_link = $(this).parent().parent().next().find("span.title[youtube_id]");
		target_link.trigger("mouseleave");			   
    });
	
	
	/*末末末末末末末末末末末末末末末末末末末末末末末末末末末
		Transform all link targets (for external links)
	末末末末末末末末末末末末末末末末末末末末末末末末末末末 */
	page_links = $("a[href^='http']");
	str = "";
	
	page_links.each(function() {
		l = jQuery.trim($(this).attr("href"));
		
		if (typeof(l) == "string") {
		
		  if (is_internal_link(l) == false) {
			  $(this).attr("target","_blank");
		  }
		}
	});
	
									 
	
});

function mark_as_seen(youtube_id) {
	//try to find anything with youtube_id = youtube_id whose class is not 
	var possible = $("*[youtube_id='" + youtube_id + "']:not(.seen)");
	//possible.css("border","1px solid red");
	
	//now get the tagname of each possible
	possible.each(function() {
		var ths = $(this);
		tagName = ths.get(0).tagName;
		p = ths.parent();
		grandParent = ths.parent().parent().parent().parent().parent();
		g_tagName = grandParent.get(0).tagName;
		g_class = grandParent.attr("class");
		
		if (tagName != "LI" && g_tagName == "TABLE" && g_class == "featured_tutorials") {			
			//parent = ths.parent();
			p.css("position","relative");
			var q = "<div class='featured_seen'></div>";
			p.prepend(q);
		}
		
		else if (tagName == "LI") {
			//Add the seen class
			$(this).addClass("seen");
		}
		else {
			//do nothing	
		}
		
   });
}


function is_internal_link(l) {
	switch (l) {
			case "features":
			case "../privatebeta":
			case "../privatebeta/":
			case "about":
			case "tutorials":
			case "tryit":
			case "terms":
			case "legal":
			case "privacy":
			case "thanks":
			case "announcement_thanks":
			case "":
				return true;
				break;
				
			default:
				var aliases = Array("../","#","http://www.shotgunsoftware.com","www.shotgunsoftware.com",
									"shotgunsoftware.com","https://shotgunsoftware.zendesk.com/forums/31277/posts.rss");
				var found = false;
				l = l.toLowerCase();
				for (var i=0; i<aliases.length;i++) {
					var c = aliases[i];
					d = l.indexOf(c);
					if (l.indexOf(c) == 0) {
						found = true;	
					}
				}
				
				return found;
				
				break;
		}	
}

