//-------------------------------------
// Mako "namespace"
//-------------------------------------

var Mako = {};

//-------------------------------------
// Mako global functions
//-------------------------------------

(function()
{
	/*
	* Toggles "hidden" div
	*
	* @param string - div ID
	* @return void
	* @requires jQuery
	*/

	Mako.toggle = function(id)
	{
		$("#"+id).slideToggle("fast");
	};

	/*
	* Opens links in a new window
	*
	* @return void
	*/

	Mako.externalLinks = function()
	{
		if (!document.getElementsByTagName)
		{
			return;
		}
		
		if(document.getElementsByTagName("a") != null)
		{
			var anchors = document.getElementsByTagName("a"); 

			for (var i=0; i<anchors.length; i++)
			{ 
				var anchor = anchors[i];

				if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external")
				{
					anchor.target = "_blank";
				}
			}
		}
	};

	$(window).load(Mako.externalLinks);

	/*
	* Opens "friends" pop-up window
	*
	* @return void
	*/

	Mako.myFriends = function()
	{
		var ipb_var_base_url = "http://www.squareinsider.com/forums/index.php?s=&";
		var url = ipb_var_base_url.replace('&amp;', '&') + "act=profile&CODE=friends_list_popup";

		window.open(url.replace('&amp;', '&'), "Friends", "width=450, height=400, scrollbars=yes", false);
	};

	/*
	* CSS Dropdown
	*
	* @return void
	*/

	Mako.startList = function()
	{
		if (document.all&&document.getElementById)
		{
			var navRoot = null;
			
			if(document.getElementById("nav") != null)
			{
				navRoot = document.getElementById("nav");
				
				for (i=0; i<navRoot.childNodes.length; i++)
				{
					node = navRoot.childNodes[i];

					if (node.nodeName=="LI")
					{
						node.onmouseover=function()
						{
							this.className+=" over";
						}

						node.onmouseout=function()
						{
								this.className=this.className.replace(" over", "");
						}
					}
				}
			}
		}
	};

	$(window).load(Mako.startList);

	/*
	* Resizes all linked images
	*
	* @return void
	*/

	Mako.resizeImages = function()
	{
		//--------------------------------------------
		// Set max width and get all images
		//--------------------------------------------

		var maxWidth = 550;
		var divPadding = 4;
		var images = document.getElementsByTagName("img");

		//--------------------------------------------
		// Loop through all the images
		//--------------------------------------------

		for(var i = 0; i < images.length; i++)
		{
			//---------------------------------------------
			// Check if image is linked and if its too big
			//---------------------------------------------

			if(images[i].id == "linked" && images[i].width > maxWidth)
			{
				// Get image info

				var src = images[i].src;
				var width = images[i].width;
				var height = images[i].height;

				// Calculate percentage and resize image

				var pct = Math.ceil(parseInt((maxWidth / width) * 100));

				images[i].width = maxWidth;
				images[i].className = "resize_image";

				// Create div

				var div = document.createElement("div");

				div._src = src;
				div._width = width;
				div._height = height;

				div.className = "resize_info";
				div.style.padding = divPadding + "px";
				div.style.width = (maxWidth - (divPadding * 2)) + "px";
				div.innerHTML = "Resized to " + pct + "% of original size [" + width + "x" + height + "] - <a href=\""+src+"\" rel=\"shadowbox\">Click here to view full image</a>.";

				// Insert div over the image

				images[i].parentNode.insertBefore(div, images[i]);
			}
		}
	};

	$(window).load(Mako.resizeImages);
	
	/*
	* Setup shadowbox
	*
	* @reuturn void
	* @requires shadhobox + jQuery
	*/

	Mako.initBox = function()
	{
		var o = {overlayOpacity: 0.75, animSequence: "sync"};

	    Shadowbox.init(o);
	};

	$(window).load(Mako.initBox);
	
	/*
	* Opens a modal window
	*
	* @param string - div ID
	* @param string - title
	* @param int - height
	* @param int - width
	* @return void
	* @requires shadowbox + jQuery
	*/
	
	Mako.openModal = function(divID, t, h, w)
	{
		var t = (t == null) ? null : t;
		var h = (h == null) ? 270 : h;
		var w = (w == null) ? 450 : w;

		Shadowbox.open({title: t, player: 'html', content: $("#"+divID).html(), height: h, width: w}, {enableKeys: false});
	};
	
	/**
	* Hides the address bar on iphone
	*/
	
	Mako.iphoneHide = function()
	{
		if (navigator.userAgent.indexOf('iPhone') != -1)
		{
			window.scrollTo(0, 1);
		}
	}
	
	$(window).load(Mako.iphoneHide);
	
})();