function basename (filename)
{
	var i = filename.lastIndexOf("/");
	
	if (i == -1) {
		return filename;
	} else {
		return filename.slice(i+1);
	}
}

function dirname (filename)
{
	var i = filename.lastIndexOf("/");
	
	if (i == -1) {
		return filename;
	} else {
		return filename.slice(0,i);
	}
}

function open_popup (url, w, h)
{
	w = window.open(url, "popup","resizable=no,status=no,scrollbars=no,height=" + h + ",width=" + w);
	w.focus();
}

function preload_nodes ()
{
	var images = [ "nav_1.gif", "nav_2.gif", "nav_3.gif", "nav_4.gif", "nav_5.gif", 
				   "nav2_30.gif", "nav2_30_noline.gif", "nav2_31.gif", "nav2_32.gif", "nav2_33.gif", "nav2_34.gif", "nav2_35.gif", 
				   "nodes_10.jpg", "nodes_11.jpg", "nodes_12.jpg", "nodes_13.jpg", "nodes_14.jpg", "nodes_15.jpg",
				   "nodes_20.jpg", "nodes_21.jpg", "nodes_22.jpg", "nodes_23.jpg", "nodes_24.jpg",
 			   	   "nodes_40.jpg", "nodes_41.jpg", "nodes_42.jpg", "nodes_43.jpg", "nodes_44.jpg",
				   "nodes_50.jpg", "nodes_51.jpg", "nodes_52.jpg", "nodes_53.jpg"
				   ];
	var i;
	
	for (i = 0; i < images.length; i++) {
		$("#preload").append("<img src='" +  nk_theme_base + "/images/" +  images[i] + "' />");
	}
}

function get_section ()
{
	var body_class = document.body.getAttribute("class");
	if (body_class == null) body_class = document.body.getAttribute("className"); // i hate ie
	class_list = body_class.split(" ");
	
	for (var i = 0; i < class_list.length; i++) {
		if (class_list[i].charAt(0) == "S") return class_list[i].slice(2);
	}
	
	return null;
}

function get_node ()
{
	var body_class = document.body.getAttribute("class");
	if (body_class == null) body_class = document.body.getAttribute("className"); // i hate ie
	class_list = body_class.split(" ");
	
	for (var i = 0; i < class_list.length; i++) {
		if (class_list[i].charAt(0) == "N") return class_list[i].slice(2);
	}
	
	return null;
}

function get_tier ()
{
	var body_class = document.body.getAttribute("class");
	if (body_class == null) body_class = document.body.getAttribute("className"); // i hate ie
	class_list = body_class.split(" ");
	
	for (var i = 0; i < class_list.length; i++) {
		if (class_list[i].charAt(0) == "T") return class_list[i].slice(2);
	}
	
	return null;
}

function goto_page (url)
{
	nk_debug("goto_page()", url);
	
	window.location.href = url;
}

function open_project_popup (project)
{
	nk_debug("open_project_popup()", project);
}

function has_flash_player (required_version)
{
	var version = deconcept.SWFObjectUtil.getPlayerVersion();
	var rc;
	
	if (version != undefined && version != null) {
		if (version['major'] == 0) {
			rc = false;
		} else {
			if (version['major'] >= required_version) {
				rc = true;
			} else {
				rc = false;
			}
		}
	} else {
		rc = false;
	}

	return rc;			
}

function do_ready ()
{
	var s = get_section();
	var n = get_node();
	var t = get_tier();

//	nk_debug_open();
	
	nk_debug("do_ready()", s, n, t);
	
	if (has_flash_player(8)) {
		replace_swfs();
	}
	
	$('div#nav2:not(:has(ul))').hide();
	
	$('div.t4box').addClass('clearfix');
	$('div.postbody p:has(img)').parents('div.postbody').addClass('clearfix');
	
	$(".comment-block").hide();
	$(".formbody").hide();
	
	$(".commentsbody h3 a").bind("click", function () {
		$(".comment-block").toggle();
		$(".formbody").toggle();
		return false;
	});
	
	$(".formbody #submit").bind("mouseover", function () {
		$(this).attr("src", nk_theme_base + "/images/submit_over.png");
	});

	$(".formbody #submit").bind("mouseout", function () {
		$(this).attr("src", nk_theme_base + "/images/submit_norm.png");
	});
	
	$("#toggle-admin").bind("click", function () {
		$(".admin").toggle();
		return false;
	});
	
	$("#addthis-link").bind("mouseover", function () {
		return addthis_open(this, '', '[URL]', '[TITLE]');
	});

	$("#addthis-link").bind("mouseout", function () {
		addthis_close();
	});

	$("#addthis-link").bind("clink", function () {
		return addthis_sendto();
	});
	
	if (s == 4)
	{
		// add a class on the pagebody div to override the width
		$('div.pagebody').addClass('studio-pagebody');	
	}
	
	if (s == 4 && n == 0 && t == 1) do_studio_ready();
	else if (s == 4 && (n == 1 || n == 2) && (t == 2 || t == 3)) // added 2/3/11
	{
		$("div.pagebody h2").addClass("left-header");
		
		$('div#nav2 ul.dropdownmenu').hide();
		do_studio_carousel_ready();
		
		var hash = window.location.hash.substring(1);
		
		if (hash && $("#" + hash))
		{
			$("#" + hash).replaceWith('<span id="' + hash + '" class="featured-thumb">' + $("#" + hash).text() + '</span>');
		} else
		{
			var first_item = $("ul#studio-carousel li:first").find("a");
			$(first_item).replaceWith('<span id="' + $(first_item).attr("id") + '" class="featured-thumb">' + $(first_item).text() + '</span>');
			
			do_studio_featured_ready();	
		}
		
		$("ul#studio-carousel li").click(function() {
			if ($(this).find("span.featured-thumb").length > 0)
			{
				return false;
			}
			
			$(".featured-thumb").replaceWith('<a id="' + $(".featured-thumb").attr("id") + '" href="' + window.location + '">' + $(".featured-thumb").text() + '</a>');
			$(this).find("a").replaceWith('<span id="' + $(this).find("a").attr("id") + '" class="featured-thumb">' + $(this).find("a").text() + '</span>');
			
			do_studio_featured_ready();
			return false;
		});
		
		do_studio_featured_ready();
	} else if (s == 4 && (n == 3 || n == 4) && (t == 2 || t == 3))
	{
		$("div.pagebody h2").addClass("right-header");
		
		$("div.studio-item-left, div.studio-item-right").each(function() {
			var item_link = $(this).find("p a").attr("href");
			var item_onclick = $(this).find("p a").attr("onclick");
			
			$(this).find("img, h4").wrap('<a href="' + item_link + '" title="View this project" />');			
			
			if (item_onclick !== null)
			{
				$(this).find("a:has(img), a:has(h4)").click(item_onclick);
			} else
			{
				$(this).find("a:has(img), a:has(h4)").attr("target", "_blank");
			}
			
		});
	}
}

function do_company_overview_ready ()
{
	if (! has_flash_player(9)) {
		var i = 1;
		var n = $("#non-flash a").length;
		
		$("#non-flash").data("i", i);
		$("#non-flash").data("n", n);
		
		setTimeout(do_company_overview_rotate, 5000);
	}
}

function do_company_overview_rotate ()
{
	var i = $("#non-flash").data("i");
	var n = $("#non-flash").data("n");
	
	if (++i > n) i = 1;
	$("#non-flash").data("i", i);
	
	$("#non-flash a").css("display", "none");
	$("#non-flash a#non-flash-0" + i).css("display", "block");
	
	setTimeout(do_company_overview_rotate, 5000);
}

function handle_studio_carousel_click (hash)
{
	$("#featured-thumb").replaceWith('<a id="' + hash + '" href="' + window.location + '">' + $("#featured-thumb").text() + '</a>');
	$("#th_" + hash).replaceWith('<span id="featured-thumb">' + $("#th_" + hash).text() + '</span>');	
}

function do_studio_ready ()
{
	$f("t9video", "/_shared/flowplayer/flowplayer-3.2.6.swf", {
		// http://flowplayer.org/documentation/configuration/clips.html
		clip: {
			url: "http://media.whatscookin.com/website/studio/videos/INDI_11_square_cropped.m4v",
			//accelerated: true,
			autoBuffering: true,
			autoPlay: true,
			baseurl: "./",
			scaling: "fit",
			provider: "pseudostreaming"
		},

		plugins: {
			// http://flowplayer.org/plugins/flash/controlbar.html
			controls: {
				autoHide: true,
				playlist: false,
				time: false,
				volume: false,

				url: "/_shared/flowplayer/flowplayer.controls-3.2.4.swf"
				//url: "flowplayer.controls-air-3.2.4.swf"
				//url: "flowplayer.controls-skinless-3.2.4.swf"
				//url: "flowplayer.controls-tube-3.2.4.swf"
			},
		
			// http://flowplayer.org/plugins/streaming/pseudostreaming.html
			pseudostreaming: {
				url: "/_shared/flowplayer/flowplayer.pseudostreaming-3.2.6.swf"
			}
		}
	}).ipad({
		simulateiDevice: false,
		controls: true
	});
}

function do_studio_featured_ready ()
{
	var filename = $(".featured-thumb:first").attr("id");

	$f("featured-video", "/_shared/flowplayer/flowplayer-3.2.6.swf", {
		// http://flowplayer.org/documentation/configuration/clips.html
		clip: {
			url: "http://media.whatscookin.com/website/studio/videos/" + filename + ".mp4",
			//accelerated: true,
			autoBuffering: true,
			autoPlay: true,
			baseurl: "./",
			scaling: "fit",
			provider: "pseudostreaming"
		},

		plugins: {
			// http://flowplayer.org/plugins/flash/controlbar.html
			controls: {
				autoHide: true,
				playlist: false,
				time: false,
				volume: false,

				url: "/_shared/flowplayer/flowplayer.controls-3.2.4.swf"
				//url: "flowplayer.controls-air-3.2.4.swf"
				//url: "flowplayer.controls-skinless-3.2.4.swf"
				//url: "flowplayer.controls-tube-3.2.4.swf"
			},
		
			// http://flowplayer.org/plugins/streaming/pseudostreaming.html
			pseudostreaming: {
				url: "/_shared/flowplayer/flowplayer.pseudostreaming-3.2.6.swf"
			}
		}
	}).ipad({
		simulateiDevice: false,
		controls: true
	});

	var video_description = $(".featured-thumb:first").parents("li").find(".thumb-body").html();

	if (video_description != "")
	{
		$("#featured-video-desc").html(video_description);
		$("#featured-video-desc p:first").addClass("studio-project-title");
		$("#featured-video-desc p.studio-project-title").next().addClass("studio-project-client");
	}
}

function do_studio_carousel_ready ()
{
	$("#studio-carousel").jcarousel( {
		visible: 4,
		scroll: 4					
	});
}

function do_onload ()
{
	var s = get_section();
	var n = get_node();
	var t = get_tier();

//	nk_debug_open();
	
	nk_debug("do_onload()", s, n, t);

	if (s == 1 && n == 0 && t == 1) do_company_overview_ready();
}

function do_onunload ()
{
	nk_debug_close();
}

// JQuery URLVars function
// from http://jquery-howto.blogspot.com/2009/09/get-url-parameters-values-with-jquery.html
$.extend({
  getUrlVars: function(){
    var vars = [], hash;
    var hashes = window.location.href.slice(window.location.href.indexOf('?') + 1).split('&');
    for(var i = 0; i < hashes.length; i++)
    {
      hash = hashes[i].split('=');
      vars.push(hash[0]);
      vars[hash[0]] = hash[1];
    }
    return vars;
  },
  getUrlVar: function(name){
    return $.getUrlVars()[name];
  }
});

