var browser = checkBrowser();

var url = window.location.href;

var host = window.location.host;
var port = 80;
var baseUrl = "";

var useHttps = false;

var windowsVista = browser.indexOf("Windows NT 6.") != -1;
var ie7 = browser.indexOf("MSIE 7.") != -1;

if (url.indexOf("#") == -1) {
	var spliturl = new Array();
	spliturl = url.split("/");
	var flashnav = spliturl[4]
	var slide = spliturl[5]
	if (flashnav == null && flashnav == "")
		flashnav  = "start";
	if (slide != null && slide != "")
		flashnav  += "/"+slide;
	
	if (flashnav != null && flashnav != "") {
		if (url.indexOf(".html") || url.charAt(url.length - 1) == "/")    
			url += "#"+ flashnav;
		else
			url += "/#"+ flashnav;
		
		window.location.replace(url);
	}
}

function reload() {
	document.location.reload(true);
	return true;
}

function findUrl() {
	var url = window.location.href;

	if (url.indexOf("#") == -1) return "";

	return url.substring(url.indexOf("#") + 1, url.length);
}

function checkBrowser() {
	var value = navigator.userAgent;

	return value;
}

/* Create a sharelet that does not display a button, and will generate a popup on share. */

if (useHttps == false)
	var ShareObj = SHARETHIS.addEntry({}, {button: false, popup: true});

/* Functions called from mx:Script to update elements in the sharelet. */

function ShareThis_set(el, val) {
	ShareObj.properties[el] = val;
}

/* The share event executed from mx:Script.	*/

function ShareThis_share() {
	var browser = checkBrowser();

	if (browser.indexOf("Safari") > 1) {
		setTimeout(ShareObj.popup, 500);
	} else {
		//alert("Opening Share this popup = " + ShareObj);
		ShareObj.popup();
	}
}

function $$(elementId) {
	return document.getElementById(elementId);
}

function setVideoPosition(width, height) {
	var videoElement = $$("movie");

	videoElement.style.width = width + "px";
	videoElement.style.height = height + "px";
	videoElement.style.top = ((document.documentElement.clientHeight - height) / 2) + "px";
	videoElement.style.left = ((document.documentElement.clientWidth - width) / 2) + "px";
}

function openVideo(embedTag, width, height) {
	embedTag = embedTag.replace("<embed", "<param name='wmode' value='transparent'></param><embed wmode='transparent'");

	/*if (navigator.userAgent.indexOf("Firefox") != -1) {
		my_window= window.open ("", "mywindow1","toolbar=0,menubar=0,resizable=0,location=0,status=0,scrollbars=0,width=441,height=360");
		my_window.document.write(embedTag);
		var obj = my_window.document.getElementsByTagName("embed");
		my_window.innerWidth = parseInt(obj[0].width) + 15;
		my_window.innerHeight = parseInt(obj[0].height) + 15;
	} else {*/  
		var videoElement = $$("movie");
		var screenLockerElement = $$("screenLocker");

		setVideoPosition(width, height);

		$("#movieTarget").html(embedTag);

		videoElement.style.display = "block";
		screenLockerElement.style.display = "block";
	//}
}

function closeVideo() {
	var videoElement = $$("movie");
	var screenLockerElement = $$("screenLocker");

	videoElement.style.display = "none";

	$("#movieTarget").html("");

	screenLockerElement.style.display = "none";
}

var popup = null;

function openPopupLog() {
	popup = window.open("popup.html", "PopupLog", "width=500, height=300, resizable=yes");
}

function setPopupLogText(value) {
	popup.setTextareaValue(value);
}

function setMikiUserCookie(value) {
	//console.log("setMikiUserCookie = " + value);

	var expireDate = new Date();
	var days = 1;
	expireDate.setTime(expireDate.getTime() + (days * 24 * 60 * 60 * 1000));

	document.cookie = 'mikiuser=' + value + '; path=/';
}

function removeMikiUserCookie() {
	//console.log("removeMikiUserCookie");

	var expireDate = new Date();
	var days = -1;
	expireDate.setTime(expireDate.getTime() + (days * 24 * 60 * 60 * 1000));

	document.cookie = 'mikiuser= ; expires=' + expireDate + '; path=/';
}

function miki(width, height, container, variables) {
	var vista = navigator.userAgent.indexOf("Windows NT 6.") != -1;
	var ie7 = navigator.userAgent.indexOf("MSIE 7.") != -1;
	var ff = navigator.userAgent.indexOf("Firefox") != -1;

	var wmode = "transparent";

	variables.cookieKey = "";

	var cookieList = document.cookie.split("; ");
	for (var i = 0; i < cookieList.length; i++) {
		if (cookieList[i].split("=")[0] == "mikiuser") {
			var key = cookieList[i].split("=");
			key.shift();

			variables.cookieKey = key.join("=");
		}
	}

	//if (ff)
	  //  wmode = "window";

	if (variables.port == undefined) variables.port = 80;

	//if (variables.host == undefined) variables.host = "85.25.81.198";
	if (variables.host == undefined) variables.host = "my-miki.com";

	variables.externalHost = "my-miki.com";
	//variables.externalHost = "my-miki.com";

	if (variables.useHttps == undefined) variables.useHttps = useHttps;

	var baseUrl = "";
	if (variables.useHttps)
		baseUrl = "https://" + variables.host + "/";
	else
		baseUrl = "http://" + variables.host + "/";

	if (vista && ie7) {
		var html = "";
		var flashvars = "";

		for (var property in variables) {
			if (flashvars.length) flashvars += "&" + property + "=" + variables[property];
			else flashvars = property + "=" + variables[property];
		}

		html += '<object wmode="' + wmode + '" data="' + baseUrl + 'loader.swf" allowfullscreen="true" allowScriptAccess="always" allowNetworking="all" id="miki-client" type="application/x-shockwave-flash" height="' + height + '" width="' + width + '">';
		html += '	<param name="quality" value="high" />';
		html += '	<param name="movie" value="' + baseUrl + 'loader.swf" />';
		html += '	<param name="menu" value="false" />';

		html += '	<param name="base" value="' + baseUrl + '" />';

		html += '	<param name="wmode" value="' + wmode + '" />';
		html += '	<param name="bgcolor" value="#FFFFFF" />';
		html += '	<param name="flashvars" value="' + flashvars + '" />';

		html += '</object>';

		document.getElementById(container).innerHTML = html;

	} else {
		swfobject.embedSWF(baseUrl + 'loader.swf', container, width, height, '9.0.124.0', 'scripts/swfobject/expressinstall.swf', variables, {
			base: baseUrl,
			bgcolor: '#FFFFFF',
			menu: 'false',
			wmode: wmode
		},
		{
			id: 'miki-client',
			allowFullScreen: 'true',
			allowScriptAccess: 'always',
			allowNetworking: 'all'
		});
	}

	if (windowsVista && ie7)
		SWFAddress.addId("miki-client")
}