﻿$(document).ready(function() {
	var params = {};
	params.wmode = "transparent";
	params.allowFullScreen = false;
	params.name = "tuvalu";
	var flashvars = {};
	flashvars.videoId = gup('videoId');
	try {
		swfobject.embedSWF("tuvalu.swf?ts=" + new Date().getTime(), "flash", "1007", "663", "9.0.0", null, flashvars, params);
	} catch (e) { }
	$("a#close").live("click", function() {
		if ($(this).attr("href") != "#") {
			/*if (location.href.indexOf("upload") != -1) {
			parent.openBox("/content/UploadMovie.aspx");
			return false;
			} else {
			parent.openBox($(this).attr("href"));
			return false;
			}*/
			history.go(-1);
		} else {
			parent.tb_remove();
			return false;
		}
	});
	$("input.debug").live("click", function() {
		parent.tb_remove();
	});
	$(".kindmovie input").live("click", function() {
		if ($(this).attr("value") == 2 && $("#makingOf").css("display") == "none") {
			$("#makingOf").slideDown("slow");
		} else if ($(this).attr("value") == 1 && $("#makingOf").css("display") == "block") {
			$("#makingOf").slideUp("fast");
		}
	});
	$("#checkboxes.login").live("click", function() {
		$(".fields .row").removeClass("error");
		// /userservice.svc/login?username="+ username.text + "&password="+password.text));
		$.ajax({
			url: "/Login.aspx",
			data: "username=" + $("#email").val() + "&password=" + $("#password").val(),
			type: "POST",
			dataType: "xml",
			contentTypt: "text/xml",
			success: function(data) {
				// tell flash we're logged in
				//if (window.console) { console.log('success'); console.log(data); }
				parent.thisMovie("flash").doLogin($(data).find("user").attr("email"), $(data).find("img").attr("src"));
				parent.tb_remove();
			},
			error: function(data) {
				//weird but works
				if (data.status == 200) {
					// tell flash we're logged in
					//if (window.console) { console.log('error but 200'); console.log($("#email").val()); }
					parent.thisMovie("flash").doLogin($("#email").val(), $("#password").val());
					parent.tb_remove();
				}
				else {
					$(".fields .row").addClass("error");
				}
			}
		});
	});
	$("#updateProfile").live("click", function() {
		parent.thisMovie("flash").doUpdate();
		parent.tb_remove();
		return false;
	});
	$("#view a").live("click", function() {
		parent.thisMovie("flash").doAnonymousLogin();
		parent.tb_remove();
		return false;
	});
	$("input#buttonSubmit").live("click", function() {
		var ytCode = $(this).parents("#TB_content").find("textarea#_tbBlooper").val();
		if (ytCode != "" && !isYtCode(ytCode)) {
			$(this).parents("#TB_content").find("textarea#_tbBlooper").parents(".row").addClass("error");
		}
	});
	if (location.href.indexOf("ResetPassword=true") != -1) {
		openBox("/content/ResetPassword.aspx?" + location.search.substr(location.search.indexOf("&") + 1));
	}
	if (location.href.indexOf("Activate=activate") != -1) {
		location.href = "/content/Activate.aspx?" + location.search.substr(location.search.indexOf("&") + 1);
		//openBox("/content/Activate.aspx?" + location.search.substr(location.search.indexOf("&")+1));
	}
});
function isYtCode(ytcode) {
	if (ytcode.toLowerCase().indexOf("youtube") != -1 && ytcode.toLowerCase().indexOf("watch?v=") != -1) return true;
	return false;
}
function getYTCode(urlString) {
	if (urlString && urlString.indexOf("watch?v=") != -1) {
		urlString = urlString.substr(urlString.indexOf('v=')+2, urlString.length);
		if (urlString.indexOf("&") != -1) {
			urlString = urlString.substr(0, urlString.indexOf('&'));
		}
	}
	return urlString;
}

function onLogin(data, status) {
	debug(data);
}

function debug(str) {
	if (window.console) console.log(str);
}

function openBox(str) {
	debug("incoming URL: " + str);
	var sep = "?";
	if (str.indexOf("?") != -1) sep = "&";
	str = str + sep + "width=434&amp;height=608&amp;TB_iframe=true";
	tb_show(null,str,null);
}


function thisMovie(movieName) {
     if (navigator.appName.indexOf("Microsoft") != -1) {
         return window[movieName];
     } else {
         return document[movieName];
     }
 }

function gup( name ){
  name = name.replace(/[\[]/,"\\\[").replace(/[\]]/,"\\\]");
  var regexS = "[\\?&]"+name+"=([^&#]*)";
  var regex = new RegExp( regexS );
  var results = regex.exec( window.location.href );
  if( results == null )
    return "";
  else
    return results[1];
}

function doVideoStat(videoId) {
    sitestat("http://nl.sitestat.com/klo/tros/s?teleukomwaartezijn.spel.video"+videoId+"&category=teleukomwaartezijn&thema=jeugd&ns_channel=entertainment");
}

function pausePlayback() {
}

function resumePlayback() {
}