


function popup($url, $width, $height, $scrolling)
{
	if ($scrolling == null) $scrolling = 0;

	$x = (screen.width / 2) - ($width / 2);
	$y = (screen.height / 2) - ($height / 2);
	$properties = "toolbar=0,location=0,directories=0,status=0,menubar=0,scrollbars=" + $scrolling + ",resizable=0,width=" + $width + ",height=" + $height + ",top=" + $y + ",left=" + $x;
	$popup = window.open($url, 'default', $properties);
	$popup.focus();
}


function videoPopup($url)
{
	popup($url, 643, 369);	
}


function preview(url, width, height, scrollbars)
{
	var x = (screen.width / 2) - (width / 2);
	var y = (screen.height / 2) - (height / 2);
	var attributes = "toolbar=0, location=0, directories=0, status=0, menubar=0, scrollbars=" + scrollbars + ", resizable=0, width=" + width + ", height=" + height + ", top=" + y + ", left=" + x;

	var popup;

	popup = window.open(url, Math.random(), attributes);
	
	if (popup == null)
	{
		alert('Please turn of your popup blocker.');
		return;	
	}

	popup.focus();
}


function popup(type, data)
{
	if (type.charAt != undefined)
	{
		preview.apply(this, arguments);
		return;
	}

	if (type === false)
	{
		document.getElementById('popup-container').innerHTML = "";
		document.getElementById('popup-overlay').style.display = 'none';
		
		return;
	}
	
	if (type == -1)
	{
		history.go(-1);
		return;
	}

	var templates = ["<div id=\"popup\">\n   <a href=\"javascript:void(0);\" onclick=\"popup(false);\" class=\"close\" title=\"Close\">Close<\/a>\n   <div class=\"content\">\n      <div class=\"teacher\">\n         <div class=\"title\">\n            <strong class=\"title\">{1}<\/strong>\n            <strong class=\"subtitle\">{2}<\/strong>\n         <\/div>\n         <div class=\"icon\" style=\"background-image: url({4});\"><\/div>\n         <div class=\"clear\"><\/div>\n         <p>\n         {3}\n         <\/p>\n      <\/div>\n      <div class=\"list\">\n         <div class=\"list-text\">\n            {5}\n            <div class=\"clear\"><\/div>\n         <\/div>\n      <\/div>\n\t{share}\n   <\/div>\n   <div class=\"footer\"><\/div>\n<\/div>","<div id=\"popup\">\n   <a href=\"javascript:void(0);\" onclick=\"popup(false);\" class=\"close\" title=\"Close\">Close<\/a>\n   <a href=\"javascript:void(0);\" onclick=\"popup(-1);\" class=\"back\" title=\"Back\">Go Back<\/a>\n   <div class=\"content\">\n      <div class=\"details\">\n         <div class=\"details-left\">\n            <strong class=\"title\">{1}<\/strong>\n            {2}\n            <span class=\"p\">{3}<\/span>\n         <\/div>\n         <div class=\"details-right\">\n            {4}\n            <div class=\"clear\"><\/div>\n         <\/div>\n         <div class=\"clear\"><\/div>\n      <\/div>\n      {5}\n\t{share}\n   <\/div>\n   <div class=\"footer\"><\/div>\n<\/div>","<div id=\"popup\">\n   <a href=\"javascript:void(0);\" onclick=\"popup(false);\" class=\"close\" title=\"Close\">Close<\/a>\n   <a href=\"javascript:void(0);\" onclick=\"popup(-1);\" class=\"back\" title=\"Back\">Go Back<\/a>\n   <div class=\"content\">\n      <strong class=\"title\">{1}<\/strong>\n      <strong class=\"subtitle\">{2}<\/strong>\n      <div class=\"image\" style=\"width: {6}px;\"><img src=\"{5}\" \/><\/div>\n      <div class=\"list\">\n         <strong class=\"subtitle\">{3}<\/strong>\n         <span class=\"p\">{4}<\/span>\n      <\/div>\n\t{share}\n   <\/div>\n   <div class=\"footer\"><\/div>\n<\/div>","<div id=\"popup\">\n   <a href=\"javascript:void(0);\" onclick=\"popup(false);\" class=\"close\" title=\"Close\">Close<\/a>\n   <a href=\"javascript:void(0);\" onclick=\"popup(-1);\" class=\"back\" title=\"Back\">Go Back<\/a>\n   <div class=\"content\">\n\t   <strong class=\"title\">{1}<\/strong>\n      <strong class=\"subtitle\">{2}<\/strong>\n      <div class=\"video\">\n         <div id=\"player-container\" style=\"width: 618px; height: 347px;\">\n           <div class=\"player\"><\/div>\n         <\/div>\n         <strong class=\"subtitle\">{3}<\/strong>\n         <span class=\"p\">{4}<\/span>\n      <\/div>\n      <div class=\"list\" style=\"display: none;\">\n         <strong class=\"subtitle\">Related Videos<\/strong>\n         <div class=\"images\">\n            {5}\n            <div class=\"clear\"><\/div>\n         <\/div>\n      <\/div>\n\t{share}\n   <\/div>\n   <div class=\"footer\"><\/div>\n<\/div>","<div id=\"popup\">\n   <a href=\"javascript:void(0);\" onclick=\"popup(false);\" class=\"close\" title=\"Close\">Close<\/a>\n   <div class=\"content\">\n      <div class=\"teacher\">\n         <div class=\"title\">\n            <strong class=\"title\">{1}<\/strong>\n            <strong class=\"subtitle\">{2}<\/strong>\n         <\/div>\n         <div class=\"icon\" style=\"background-image: url({4});\"><\/div>\n         <div class=\"clear\"><\/div>\n         <span class=\"p\">\n         {3}\n         <\/span>\n      <\/div>\n      {5}\n      {6}\n\t{share}\n   <\/div>\n   <div class=\"footer\"><\/div>\n<\/div>","<div id=\"popup\">\n   <a href=\"javascript:void(0);\" onclick=\"popup(false);\" class=\"close\" title=\"Close\">Close<\/a>\n   <div class=\"content\">\n      <div class=\"teacher\">\n         <div class=\"title\">\n            <strong class=\"title\">{1}<\/strong>\n            <strong class=\"subtitle\">{2}<\/strong>\n         <\/div>\n         <div class=\"icon\" style=\"background-image: url({4});\"><\/div>\n         <div class=\"clear\"><\/div>\n         <span class=\"p\">\n         {3}\n         <\/span>\n      <\/div>\n      <div class=\"list\">\n         <div class=\"list-text\">\n            <ul class=\"bullets\" style=\"width: 325px;\">\n               {5}\n            <\/ul>\n            <div class=\"stories\" id=\"stories\">\n              {6}\n            <\/div>\n            <div class=\"clear\"><\/div>\n         <\/div>\n      <\/div>\n\t{share}\n   <\/div>\n   <div class=\"footer\"><\/div>\n<\/div>"]	
	var share = "<div style=\"padding-top: 15px;\">\n<script type=\"text\/javascript\">var addthis_pub=\"flipvisual\";<\/script>\n<a rel=\"nofollow\" href=\"http:\/\/www.addthis.com\/bookmark.php\" onmouseover=\"return addthis_open(this, '', 'http:\/\/www.miamiadschool.com\/{1}', 'Miami Ad School - {2}')\" onmouseout=\"addthis_close()\" onclick=\"return addthis_sendto()\">\n\t<img src=\"http:\/\/s7.addthis.com\/static\/btn\/lg-share-en.gif\" width=\"125\" height=\"16\" border=\"0\" alt=\"Bookmark and Share\" style=\"border:0\"\/>\n<\/a>\n<script type=\"text\/javascript\" src=\"http:\/\/s7.addthis.com\/js\/152\/addthis_widget.js\"><\/script>\n<\/div>"
	var template;
	var element;

	switch (type)
	{
		case 1: template = [data.title, data.subtitle, data.description, data.icon, data.staff]; break;
		case 2: template = [data.title, data.subtitle, data.description, data.details, data.projects]; break;
		case 3: template = [data.h1, data.h2, data.title, data.description, data.file, data.file_width]; break;
		case 4: template = [data.h1, data.h2, data.title, data.description, data.player]; break;
		case 5: template = [data.title, data.subtitle, data.description, data.icon, data.images, data.videos]; break;
		case 6: template = [data.title, data.subtitle, data.description, data.icon, data.list, data.detail]; break;
	}

	if (isNaN(window.popupDepth)) window.popupDepth = 0;
	if (window.popupHistory == undefined) window.popupHistory = new Array();

	if (type > -1)
	{
		window.popupDepth++;
		window.popupData = snippet(templates[type - 1], template);
		window.popupData = window.popupData.split('{path}').join('http://www.miamiadschool.com/locations/');
		window.popupData = window.popupData.split('{share}').join(snippet(share, [data.permalink, data.title]));

		if (type == 6) window.story = 1;
	}
	else
	{
		window.popupDepth--;
		window.popupData = window.popupHistory[window.popupDepth][1];
		
		type = window.popupHistory[window.popupDepth][0];
	}

	window.popupHistory[window.popupDepth] = [type, window.popupData];

	document.getElementById('popup-container').innerHTML = window.popupData;
	document.getElementById('popup-overlay').style.display = 'block';

	popupSetup(type, data);

	updateFonts();

	window.scroll(0, 0);
}


function popupSetup(type, data)
{
	switch (type)
	{
		case 4:

			/*
			var movie;
			
			movie = new FlashMovie('http://www.miamiadschool.com/locations/assets/swf/player.swf', '100%', '100%', 8);
			movie.id = 'player';
			movie.setParameter('wmode', 'transparent');
			movie.setVariable('url', data.file);
			movie.create('player-container');
			*/
			
			var movie;

			movie = new SWFObject("http://www.miamiadschool.com/assets/swf/player.swf", 'player', '100%', '100%', '8', '#ffffff');
			movie.addParam('allowScriptAccess', 'always');
			movie.addParam('allowFullScreen', 'true');
			movie.addParam('wmode', 'transparent');
			movie.addVariable('data', data.player);
			movie.write('player-container');

			var player = document.getElementById('player');

			player.onmouseover = function() { player.showControl(); }
			player.onmouseout = function() { player.hideControl(); }

			break;
			
		case 6:

			element = document.getElementById('story-button' + window.story);

			if (element != null) element.onclick();

			break;
	}	
}


function videoPlay()
{
	var element;

	element = document.getElementById('player');
	element.start();
}


function videoStop()
{
	var element;

	element = document.getElementById('player');
	element.reset();
}


function videoFullscreen()
{
	var element;

	element = document.getElementById('player');
	element.fullscreen();
}


function updateFonts()
{
	sIFR.replaceElement('#popup strong.title', named({sFlashSrc: 'http://www.miamiadschool.com/locations/assets/swf/fonts/futura-extra-bold.swf', sColor: '#ec008c', sCase: 'upper', sWmode: 'transparent' }));
	sIFR.replaceElement('#popup strong.subtitle', named({sFlashSrc: 'http://www.miamiadschool.com/locations/assets/swf/fonts/helvetica-neue-medium-condensed.swf', sColor: '#333333', sCase: 'upper', sWmode: 'transparent' }));
	//sIFR.replaceElement('h5', named({sFlashSrc: 'http://www.miamiadschool.com/locations/assets/swf/fonts/myriad-pro.swf', sColor: '#333333', sCase: 'upper', sWmode: 'transparent' }));	
}


function snippet(html, data)
{
	for (var i = 1; i <= data.length; i++)
	{
		html = html.split('{' + i + '}').join(data[i - 1]);	
	}

	return html;
}


function updateHTML(id, html, index)
{
	document.getElementById(id).innerHTML = html;

	if (id == 'stories') window.story = index;
}


function toggle(id)
{
	document.getElementById(id).style.display != 'block' ? show(id) : hide(id);
}


function show(id, index, count, depth)
{
	if (isNaN(index))
	{
		document.getElementById(id).style.display = 'block';
	}
	else
	{
		for (var i = 1; i <= count; i++)
		{
			document.getElementById(id + i).style.display = (i == index ? 'block' : 'none');
			document.getElementById(id + i + '-button').style.zIndex = (i == index ? depth - 1 : depth + 1);
		}
	}

	var element = document.getElementById(isNaN(index) ? id : id + index);
	var target;

	document.onmousedown = function(event)
	{
		var valid = false;

		if (element.style.display == 'none') return;
		if (!event) event = window.event;
		if (event.target) target = event.target;
		else if (event.srcElement) target = event.srcElement;
		if (target.nodeType == 3) target = target.parentNode;

		if (target != element)
		{
			while ((target = target.parentNode) != null)
			{
				if (target == element)
				{
					valid = true;
					break;
				}
	
				i++;
			}

			if (!valid) hide(id, count);
		}
	}
}


function hide(id, count)
{
	var element;
	
	if (isNaN(count))
	{
		element = document.getElementById(id);
		
		if (element == null) return;

		element.style.display = 'none';
		return;
	}

	for (var i = 1; i <= count; i++)
	{
		document.getElementById(id + i).style.display = 'none';
	}
}