//Globals
function Globals() {
	//Show Loading Panel
	this.ajaxLoadingPanel = function(show) {
		var bodyH=$(window).height();
		if(!$.fdvars.preloaderLoaded) {
			var img='<div id="preloader_bar_img"><img src="'+$.fdvars.DIR_IMAGES+'/ajax/loading_bar.gif" /></div>';			
			$("body").append(img);			
			$.fdvars.preloaderLoaded = true;
			dimg = $('#preloader_bar_img');
			dimg.css({'position':'fixed','top':bodyH,'right':'30px'});
		}		
		if(show){$(dimg).animate({opacity: 1, top: bodyH-50}, 500);}
		else{$(dimg).animate({opacity: 0, top: bodyH}, 300);}
	};
	
	//Add ToolTip
	this.addGraphToolTip = function() {
		$("<div id='graph_tooltip'></div>").css( {
			position: 'absolute',
			display: 'none',
			padding: '4px 6px',
			color: '#FFF',
			border: '1px solid #0e5aa5', 							
			'background-color': '#0e5aa5', 							
			opacity: 0.80
		}).appendTo("body");
		return $("#graph_tooltip");
	};
	
	//Show ToolTip
	this.showGraphTooltip = function(graph_tooltip, x, y, txt) {	
		isVisible = true;		
		graph_tooltip.html(txt);
		graph_tooltip.css({left: x, top: y});
		graph_tooltip.show();
	}
	
	//Hide ToolTip
	this.hideGraphTooltip = function(graph_tooltip) {
		graph_tooltip.hide();						
	};
	
	//Unread Comments Count
	this.unreadComments = function(i) {
		if ($("b.count").length>0) {
			var countHolder = $("b.count");
			var count = parseInt(countHolder.text())-i;
			if (count>0) {
				countHolder.text(count.toString());
			} else {
				countHolder.fadeOut();
			}
		}
	};

	this.error = function(item) {
		item = $("#"+item);
		item.focus();
		item.select();
	};
}

var objGlobals = new Globals();

//Browser
var isIE = (navigator.appVersion.indexOf("MSIE") != -1) ? true : false;
var isWin = (navigator.appVersion.toLowerCase().indexOf("win") != -1) ? true : false;
var isOpera = (navigator.userAgent.indexOf("Opera") != -1) ? true : false;

//Item Preview
var ItemPreview = function(previewConfigs) {
	var preview = this;
    preview.config = previewConfigs;
    preview.previewTypes = {
		flash:function(config){
            var flashvars, params, attributes;
            flashvars = {};
			if (config.flashvars!=undefined) {
				flashvars = config.flashvars;
			}
            params = {
                base: config.base,
				allowFullScreen: "true",
                wmode: "opaque"
            };
            attributes = {};
            swfobject.embedSWF(config.url, config.container, config.width, config.height, "9.0.0", $.fdvars.DOMAIN+"flash/expressInstall.swf", flashvars, params, attributes);
        }
    };
    preview.client_has_flash = function () {
        var flashVersion, subFlashVersion, subFlashVersion2, detect;
        flashVersion = 9;
        subFlashVersion = 0;
        subFlashVersion2 = 0;
        detect = detectFlashVer(flashVersion, subFlashVersion);
        if (detect) {
            return true;
        } else {
            var err = $("<div>");
			err.html("<h2>Loading Item Preview...</h2><p><br />If you are reading this message you may not have Adobe Flash installed or you are required to upgrade your Flash player.</p><p>You can download a copy of Adobe Flash from <a href=\"http://get.adobe.com/flashplayer/\">here</a>.</p>");
            $("#large_item_preview_container").append(err);
			var previewHolderHeight = $("#preview").height();
			var previewHeight = err.height();
			err.css({marginTop:(previewHolderHeight-previewHeight)/2});
            return false;
        }
    };
    preview.initialize = function () {
        preview.hasFlash = preview.client_has_flash();
        if (preview.previewTypes[preview.config.type] === undefined) {
            preview.config.type = "unknown";
        }
    };
    preview.display = function () {		
        if (!preview.hasFlash) {
            return;
        }
        var type = preview.previewTypes[preview.config.type];
        if (typeof(type) !== "undefined") {
            type(preview.config);
        } else {
            alert("Item preview type not defined.");
        }
    };
    preview.initialize();
    return preview;
};

function ControlVersion() {
    var objFlash, version, ex;
    try {
        objFlash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.7");
        version = objFlash.GetVariable("$version");
    } catch (ex) {}
    if (!version) {
        try {
            objFlash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.6");
            version = "WIN 6,0,21,0";
            objFlash.AllowScriptAccess = "always";
            version = objFlash.GetVariable("$version");
        } catch (ex) {}
    }
    if (!version) {
        try {
            objFlash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
            version = objFlash.GetVariable("$version");
        } catch (ex) {}
    }
    if (!version) {
        try {
            objFlash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash.3");
            version = "WIN 3,0,18,0";
        } catch (ex) {}
    }
    if (!version) {
        try {
            objFlash = new ActiveXObject("ShockwaveFlash.ShockwaveFlash");
            version = "WIN 2,0,0,11";
        } catch (ex) {
            version = -1;
        }
    }
    return version;
}

function GetSwfVer() {
    var version = -1;
    if (navigator.plugins != null && navigator.plugins.length > 0) {
        if (navigator.plugins["Shockwave Flash 2.0"] || navigator.plugins["Shockwave Flash"]) {
            var plugin = navigator.plugins["Shockwave Flash 2.0"] ? " 2.0" : "";
            var description = navigator.plugins["Shockwave Flash" + plugin].description;
            var arrDescription = description.split(" ");
            var flashVersion = arrDescription[2].split(".");
            var ver = flashVersion[0];
            var subVer = flashVersion[1];
            var subVer2 = arrDescription[3];
            if (subVer2 == "") {
                subVer2 = arrDescription[4];
            }
            if (subVer2[0] == "d") {
                subVer2 = subVer2.substring(1);
            } else {
                if (subVer2[0] == "r") {
                    subVer2 = subVer2.substring(1);
                    if (subVer2.indexOf("d") > 0) {
                        subVer2 = subVer2.substring(0, subVer2.indexOf("d"));
                    }
                }
            }
            var version = ver + "." + subVer + "." + subVer2;
        }
    } else {
        if (navigator.userAgent.toLowerCase().indexOf("webtv/2.6") != -1) {
            version = 4;
        } else {
            if (navigator.userAgent.toLowerCase().indexOf("webtv/2.5") != -1) {
                version = 3;
            } else {
                if (navigator.userAgent.toLowerCase().indexOf("webtv") != -1) {
                    version = 2;
                } else {
                    if (isIE && isWin && !isOpera) {
                        version = ControlVersion();
                    }
                }
            }
        }
    }
    return version;
}

function detectFlashVer(flashVersion, subFlashVersion, subFlashVersion2) {
    versionStr = GetSwfVer();
	if (versionStr == -1) {
        return false;
    } else {
        if (versionStr != 0) {
            if (isIE && isWin && !isOpera) {
                tempArray = versionStr.split(" ");
                tempString = tempArray[1];
                versionArray = tempString.split(",");
            } else {
                versionArray = versionStr.split(".");
            }
            var ver = versionArray[0];
            var subVer = versionArray[1];
            var subVer2 = versionArray[2];
            if (ver > parseFloat(flashVersion)) {
                return true;
            } else {
                if (ver == parseFloat(flashVersion)) {
                    if (subVer > parseFloat(subFlashVersion)) {
                        return true;
                    } else {
                        if (subVer == parseFloat(subFlashVersion)) {
                            if (subVer2 >= parseFloat(subFlashVersion2)) {
                                return true;
                            }
                        }
                    }
                }
            }			
            return false;
        }
    }
}
