var DialogWindow = {blocker:null, currentPopup:null}
DialogWindow.ShowBlocker = function() {
	if (this.blocker == null) {
		this.blocker = document.createElement("div");
		this.blocker.style.cssText = "-moz-opacity:.50;filter:alpha(opacity=50); z-index:3000; position:absolute; top:0; left:0; background:#ffffff; width:100%; height:100%;";
		document.body.appendChild(this.blocker);
	}
	this.blocker.style.height = document.body.scrollHeight;
	this.blocker.style.display = "";
}
DialogWindow.HideBlocker = function() {
	this.blocker.style.display = "none";
}
DialogWindow.Show = function(popup) {
	this.ShowBlocker();
	document.getElementById("popupContainer").style.display = "";
	document.getElementById("popupContainer").appendChild(popup.parentNode.removeChild(popup));
	if (popup.style.position == "absolute") {
//	alert(document.body.scrollTop+"+("+document.body.clientHeight+"/2-parseInt("+popup.style.height+", 10)/2);");
		popup.style.top = document.body.scrollTop+(document.body.clientHeight/2-parseInt(popup.style.height, 10)/2);
		popup.style.left = document.body.offsetWidth/2-parseInt(popup.style.width, 10)/2;
	}
	popup.style.display = "";
	
	this.currentPopup = popup;
	
	var pos = Common.GetPosition(this.currentPopup);
	document.getElementById("popupClose").style.display = "";
	document.getElementById("popupClose").style.left = pos.x-15;
	document.getElementById("popupClose").style.top = pos.y-15;

}
DialogWindow.Hide = function() {
	document.getElementById("popupClose").style.display = "none";
	document.getElementById("popupContainer").style.display = "none";
	
	var popups = document.getElementById("popupContainer").getElementsByTagName("div");
	for (var i=popups.length-1;i>=0;i--) document.getElementById("popupCartridge").appendChild(document.getElementById("popupContainer").removeChild(popups[i]));
//	for (var i=0;i<popups.length;i++) popups[i].style.display = "none";

//	this.currentPopup.style.display = "none";
	this.HideBlocker();
}

var PleaseWait = {};
PleaseWait.Show = function() {
	if (!document.getElementById("pleasewaitmovie")) {
		document.getElementById("pleasewaitmovieContainer").innerHTML = '<object classid="clsid:d27cdb6e-ae6d-11cf-96b8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=9,0,0,0" width="100" height="100" id="pleasewaitmovie" align="middle">'+
										'<param name="movie" value="/images/loading.swf" />'+
										'<param name="quality" value="high" />'+
										'<param name="wmode" value="transparent" />'+
										'<embed src="/images/loading.swf" wmode="transparent" quality="high" bgcolor="#ffffff" width="100" height="100" name="pleasewaitmovie" type="application/x-shockwave-flash" pluginspage="http://www.macromedia.com/go/getflashplayer" />'+
										'</object>';
	}
	DialogWindow.ShowBlocker();
	document.getElementById("pleasewaitContainer").style.display = "";
}
PleaseWait.Hide = function() {
	DialogWindow.HideBlocker();
	document.getElementById("pleasewaitContainer").style.display = "none";
}

var Common = {};
Common.GetBrowserType = function() {
	if (navigator.userAgent.match(/Safari/i)) return "sf";
	else if (document.addEventListener) return "ff";
	else if (document.all && !window.opera) return "ie";
	else return "un";
}
Common.GetPosition = function(obj) {
	var pos = {x:0, y:0};
	while (obj != null && obj.nodeName.toLowerCase() != "body") {
		pos.x += parseInt(obj.offsetLeft, 10);
		pos.y += parseInt(obj.offsetTop, 10);
		obj = obj.offsetParent;
	}
	return pos;
}
Common.JumpTo = function(movieId) {
	document.location.href = "/watch/"+movieId;
}
Common.GetMovieId = function() { 
	var movieId;
	if (document.location.href.match(/\#m[0-9]*$/)) movieId = document.location.href.match(/\#m([0-9]*)/)[1];
	else movieId = document.location.href.match(/\/([0-9]*)[^\/]*$/)[1];
	return movieId;
}
Common.GetCategoryName = function() {
	if (document.location.href.match(/\#/)) return document.location.href.match(/\#(.*)/)[1].replace(/%20/g, " ");
	else return "";
}
Common.SetCategoryName = function(name) {
	document.location.href = document.location.href.replace(/\#.*$/, "")+"#"+name;
}
Common.GetCurrentTag = function() {
	if (document.location.href.match(/\#/)) return document.location.href.match(/\#(.*)/)[1];
	else return "";
}
Common.GetBroadcasterId = function() {
	if (document.getElementById("broadcasterId")) return document.getElementById("broadcasterId").value;
	else return 0;
}
Common.ShowDefaultImage = function() {
    this.src = "/images/defaultthumbnail.gif";
}

Common.DefaultBigAvatar = function() {
    this.src = "/images/users/defaultbig.gif";
}
Common.GetGroups = function() {
	var groups = new Array();
	if (typeof(this.Groups) == "undefined") this.Groups = document.getElementById("sharing_groups_list").getElementsByTagName("li");
	for (var i=0;i<this.Groups.length;i++) {
		groups.push({id:this.Groups[i].getAttribute("id"), name:this.Groups[i].getAttribute("name")});
	}
	return groups;
}
Common.GetGroupById = function(id) {
	if (typeof(this.Groups) == "undefined") this.Groups = document.getElementById("sharing_groups_list").getElementsByTagName("li");
	for (var i=0;i<this.Groups.length;i++) {
		if (this.Groups[i].getAttribute("id") == id) return this.Groups[i].getAttribute("name");
	}
	return "Unknown";
}
Common.ClearContainer = function(container, tagName) {
	var nodes = container.childNodes;
	for (var i=nodes.length-1;i>=0;i--) {
		if (nodes[i].tagName == tagName || tagName == "") container.removeChild(nodes[i]);
	}
}
Common.ShowErrorThumbnail = function(img, width, height) {
	img.src = "/images/defaultthumbnail.gif";
}
Common.FormatNumber = function(value) {
	value = value+"";
	if (value == "null") value = "0";
	var rvalue = value.split("").reverse().join("");
	rvalue = rvalue.replace(/([0-9]{3}(?=.))/g, "$1,"); 
	return rvalue.split("").reverse().join("");
}
Common.FormatSeconds = function(value) {
	value = parseInt(value);
	var minutes = Math.floor(value/60);
	var seconds = value % 60;
	return Common.lpad(minutes, "0", 2)+":"+Common.lpad(seconds, "0", 2);
}
Common.lpad = function(value, char, len) {
	value = value+"";
	for (var i=value.length;i<len;i++) {
		value = char+value;
	}
	return value;
}
Common.SetSelectBox = function(select, id) {
	var options = select.childNodes;
	for (var i=0;i<options.length;i++) {
		if (options[i].value == id) {
			select.selectedIndex = i;
		}
	}
}
Common.UpdateTopNavigation = function(section) {
	$("nav_home").className = (section == "home") ? "current" : "";
	$("nav_help").className = (section == "help") ? "current" : "";
	$("nav_categories").className = (section == "categories") ? "current" : "";
	$("nav_whatshot").className = (section == "whatshot") ? "current" : "";
}
Common.GetElementByName = function(xml, name) {
	var nodes = xml.getElementsByTagName(name);
	return (nodes.length > 0 && nodes[0].firstChild) ? nodes[0].firstChild.nodeValue : null;
}
Common.ParseXMLResult = function(xml) {
	var result = {errorCode:0, reason:""};
	
	var success = xml.getElementsByTagName("Success")[0].firstChild.nodeValue;

	if (success == "false") {
		var reason = xml.getElementsByTagName("FailText")[0].firstChild.nodeValue;
		result.errorCode = -1;
		result.reason = reason;
	}
	
	return result;
}
Common.GetBrandsAndModels = function(xml) {
	var brands = new Array();
	var manufactures = xml.getElementsByTagName("Manufacturer");
	for (var i=0;i<manufactures.length;i++) {
		var brandName = manufactures[i].getElementsByTagName("Name")[0].firstChild.nodeValue;
		var brandId = manufactures[i].getElementsByTagName("Id")[0].firstChild.nodeValue;
		var models = manufactures[i].getElementsByTagName("Model");
		brands[brandId] = {name:brandName, items:new Array()};
		for (var m=0;m<models.length;m++) {
			var modelId = models[m].getElementsByTagName("Id")[0].firstChild.nodeValue;
			var modelName = models[m].getElementsByTagName("Name")[0].firstChild.nodeValue;
			var modelImage = models[m].getElementsByTagName("ImagePath")[0].firstChild.nodeValue;
			brands[brandId].items.push({id:modelId, name:modelName, image:modelImage});
		}
	}
	return brands;
}
Common.GetViewsLabel = function(value) {
	return (parseInt(value, 10) == 1) ? "view" : "views"; 
}
Common.GetFansLabel = function(value) {
	return (parseInt(value, 10) == 1) ? "fan" : "fans"; 
}
Common.IsLoggedIn = function() {
	return $("LoggedIn") ? true : false;
}
Common.GetDomain = function() {
	return document.location.href.replace(/(http\:\/\/[^\/]*\/).*/, "$1");
}

function hoverImage(imgObj, imgChange, imgClick) {
	var imageType = imgObj.src.substring(imgObj.src.lastIndexOf("."));
	
	if (imgObj.src.indexOf(imgChange) == -1) {
		imgObj.src = imgObj.src.replace(imageType, imgChange+imageType);
	} else {
		imgObj.src = imgObj.src.replace(imgChange+imageType, imageType);
	}
}

function clickImage(action, imgObj, imgChange, imgClick) {
	var imageType = imgObj.src.substring(imgObj.src.lastIndexOf("."));
	
	if (action == 'down') {
		imgObj.src = imgObj.src.replace(imgChange, imgClick);
	} else {
		if (imgObj.src.indexOf(imgChange) == -1) {
			imgObj.src = imgObj.src.replace(imgClick+imageType, imageType);
		} else {
			imgObj.src = imgObj.src.replace(imgChange+imageType, imageType);
		}	
	}	
}

function checkChars(taObj, totalCharsCont, e) {
    if (document.all) {
        var keyNum = event.keyCode;
    } else {
        var keyNum = e.keyCode;
    }
	
	var totalChars = document.getElementById(totalCharsCont).attributes['originVal'].value;
	
    var charsLeft = totalChars-taObj.value.length;
    document.getElementById(totalCharsCont).innerHTML = charsLeft;
	
	if (taObj.value.length <= (totalChars-1)) {
		return true; 
	} else {
		if (keyNum == 8 || keyNum == 46) {
		} else {	
			return false;
		}	
	}
}

function checkCharsAfterDel(taObj, totalCharsCont, e) {
    if (document.all) {
        var keyNum = event.keyCode;
    } else {
        var keyNum = e.keyCode;
    }
	
	var totalChars = document.getElementById(totalCharsCont).attributes['originVal'].value;
	
    var charsLeft = totalChars-taObj.value.length;
    document.getElementById(totalCharsCont).innerHTML = charsLeft;
}

function copy(inElement) {
	if (inElement.createTextRange) {
		var range = inElement.createTextRange();
		if (range) range.execCommand('Copy');
	} else {
		var flashcopier = 'flashcopier';
		if(!document.getElementById(flashcopier)) {
			var divholder = document.createElement('div');
			divholder.id = flashcopier;
			document.body.appendChild(divholder);
		}
		document.getElementById(flashcopier).innerHTML = '';
		var divinfo = '<embed src="/images/_clipboard.swf" FlashVars="clipboard='+encodeURIComponent(inElement.value)+'" width="0" height="0" type="application/x-shockwave-flash"></embed>';
		document.getElementById(flashcopier).innerHTML = divinfo;
	}
}

function getElementPos(obj){
	try {
		var position = {x:0,y:0};

		do{
			position.x += obj.offsetLeft;
			position.y += obj.offsetTop;
			obj = obj.offsetParent;
		} while(obj)

		return position;
	} catch (e) {
		alert(e.description);
	}
}

function flixeeSelect(selectedObj) {
	for (var i=0; i<6; i++) {
		document.getElementById("flixeeColor"+i).className = 'flixeeColors';
	}
	
	selectedObj.className = 'flixeeColors selector';
}

function getModelInst(model, step) {
	if (model == 'nokia') {
		if (step == 'download') {
			return "By Opening the text message and pressing the link"
		} else if (step == 'install') {
			return "Press Yes to install the Flixwagon application"
		} else if (step == 'launch') {
			return "Go to the applications folder and launch Flixwagon. You're ready to broadcast! To view the video go to <a href='/mypage/index'>My Page.</a>"
		}
	} else {
		// Default Values
		if (step == 'download') {
			return "By Opening the text message and pressing the link"
		} else if (step == 'install') {
			return "Press Yes to install the Flixwagon application"
		} else if (step == 'launch') {
			return "Go to the aplications folder and launch Flixwagon. You're ready to broadcast! To view the video go to <a href='/mypage/index'>My Page.</a>"
		}
	}
}
