
var DialogClass = Class.create();

DialogClass.prototype = {
	initialize: function(className, loaderText){
		this.ClassName = (className==null) ? "default" : className;
		this.LoaderText = (loaderText==null) ? "Loading" : loaderText;

		var objBody = document.getElementsByTagName("body").item(0);

		//Create Overlay
		var Overlay = document.createElement("div");
		Element.extend(Overlay);
		Overlay.id = "Overlay";
		Overlay.style.position = 'absolute';
		Overlay.style.top = '0';
		Overlay.style.left = '0';
		Overlay.style.zIndex = '1000';
		Overlay.style.height = getPageSize()[1] + "px";
		Overlay.style.width = getPageSize()[0] + "px";
		Overlay.hide();
		this.Overlay = Overlay;
		objBody.appendChild(this.Overlay);
		this.closeOverlay();

		//create loader
		var loader = document.createElement("div");
		Element.extend(loader);
		loader.id = "DialogLoader";
		loader.style.zIndex = 1001;
		loader.style.width = "100px";
		loader.style.position = "absolute";
		loader.update(this.LoaderText);
		loader.hide();
		this.loader = loader;
		objBody.appendChild(loader);
		this.makeCentered(this.loader);

		//create dialog container
		var container = document.createElement("div");
		Element.extend(container);
		container.id = "Dialog";
		container.style.zIndex = 1002;
		container.style.position = "absolute";
		container.addClassName("Block green");
		container.hide();

		var header = document.createElement("h3");
		Element.extend(header);
		this.Header = header;
		container.appendChild(this.Header);

		var content = document.createElement("div");
		Element.extend(content);
		content.addClassName("content");
		container.appendChild(content);

		this.Container = container;
		objBody.appendChild(this.Container);
	},

	showDropDowns:function(){
		dropdowns = document.getElementsByTagName("select");
		for(i=0;i<dropdowns.length;i++){
			el = dropdowns[i];
			Element.extend(el);
			el.show();
		}
	},

	hideDropDowns:function(dialog){
		dropdowns = document.getElementsByTagName("select");
		for(i=0;i<dropdowns.length;i++){
			el = dropdowns[i];
			Element.extend(el);
			el.hide();
		}
		if(dialog != null){
			opendropdowns = $(dialog).getElementsByTagName("select");
			for(i=0;i<opendropdowns.length;i++){
				el = opendropdowns[i];
				el.show();
			}
		}
	},

	closeOverlay:function(){
		var eff = new Effect.Opacity(this.Overlay,
			{ to: 0.0, duration: 0.3 ,
			transition: Effect.Transitions.linear,
			afterFinish:function(obj){
				obj.element.hide();
			}
		});
	},

	ajax:function(topic,url,options){
		this.Header.update(topic);
		if(options==null) options = {};
		if(options.width) this.Container.style.width = options.width + "px";

		this.hideDropDowns();
		this.Overlay.show();
		var eff = new Effect.Opacity('Overlay',
		{ to: 0.8, duration:0.3,
			transition: Effect.Transitions.linear,
			afterFinish:function(obj){
				Dialog.makeCentered(Dialog.loader);
				Dialog.loader.show();
			}
		});
		var req = new Ajax.Request(url, {
  			onSuccess: function(transport) {
      			Dialog.Container.down("div.content").update(transport.responseText);
      			Dialog.loader.hide();
      			Dialog.show(Dialog.Container);
  			}
		});
	},

	open:function(topic,el,options){
		this.Header.update(topic);
		if(options==null) options = {};
		if(options.width) this.Container.style.width = options.width + "px";

		this.hideDropDowns();
		this.Overlay.show();
		var eff = new Effect.Opacity('Overlay',
		{ to: 0.8, duration:0.3,
			transition: Effect.Transitions.linear,
			afterFinish:function(obj){
				Dialog.show(Dialog.Container);
				Dialog.Container.down("div.content").update($(el).innerHTML);
			}
		});
	},

	show:function(el){
		var dlg = $(el);
		Element.extend(dlg);
		Position.absolutize(dlg);
		dlg.style.zIndex = 1002;
		this.makeCentered(dlg);
		dlg.show();
	},

	close:function(){
		this.Container.hide();
		this.closeOverlay();
		this.loader.hide();
		this.showDropDowns();
	},

	makeCentered:function(el){
		var hg = document.viewport.getHeight();
		var wd = document.viewport.getWidth();
		scrpos = document.viewport.getScrollOffsets();
		$(el).style.left = String((wd / 2) - (parseInt($(el).getWidth()) / 2) + scrpos.left) + "px";
		$(el).style.top = String((hg / 2) - (parseInt($(el).getHeight()) / 2) + scrpos.top) + "px";
	}
};

var Dialog;
function initDialog() { Dialog = new DialogClass(); }
Event.observe(window, 'load', initDialog, false);

function getPageSize(){

	var xScroll, yScroll;

	if (window.innerHeight && window.scrollMaxY) {
		xScroll = document.body.scrollWidth;
		yScroll = window.innerHeight + window.scrollMaxY;
	} else if (document.body.scrollHeight > document.body.offsetHeight){ // all but Explorer Mac
		xScroll = document.body.scrollWidth;
		yScroll = document.body.scrollHeight;
	} else { // Explorer Mac...would also work in Explorer 6 Strict, Mozilla and Safari
		xScroll = document.body.offsetWidth;
		yScroll = document.body.offsetHeight;
	}

	var windowWidth, windowHeight;
	if (self.innerHeight) {	// all except Explorer
		windowWidth = self.innerWidth;
		windowHeight = self.innerHeight;
	} else if (document.documentElement && document.documentElement.clientHeight) { // Explorer 6 Strict Mode
		windowWidth = document.documentElement.clientWidth;
		windowHeight = document.documentElement.clientHeight;
	} else if (document.body) { // other Explorers
		windowWidth = document.body.clientWidth;
		windowHeight = document.body.clientHeight;
	}

	// for small pages with total height less then height of the viewport
	if(yScroll < windowHeight){
		pageHeight = windowHeight;
	} else {
		pageHeight = yScroll;
	}

	// for small pages with total width less then width of the viewport
	if(xScroll < windowWidth){
		pageWidth = windowWidth;
	} else {
		pageWidth = xScroll;
	}


	arrayPageSize = new Array(pageWidth,pageHeight,windowWidth,windowHeight)
	return arrayPageSize;
}

function ShowTooltip(event, source){

	ttSource = $(source);

	if($("IdeaToolTip") != null && $("IdeaToolTip").visible){
		tt = $("IdeaToolTip");
		css = tt.classNames();
		for(i=0;i<css.length;i++)
			tt.removeClassName(css[i]);
	}else{
		tt = document.createElement("div");
		Element.extend(tt);
		tt.id = "IdeaToolTip";
		tt.hide();
		document.body.appendChild(tt);
		Position.absolutize(tt);
	}

	tt.addClassName(ttSource.className);
	tt.update(ttSource.innerHTML);

	tt.setStyle({
		'width':ttSource.style.width,
		'height':ttSource.style.height,
		'display':'block',
		'z-index':1050,
		'text-align':'left'
	});

	tt.style.left = (Event.pointerX(event) + 15)  + "px";
	tt.style.top = (Event.pointerY(event) + 10)  + "px";
	tt.makePositioned();
	tt.show();
}

function HideTooltip(){
	$('IdeaToolTip').hide();
}

function confirmAndGo(url, msg){
	if(confirm(msg)){
		window.location.href = url;
	}
}

function translate(lang){

	if(document.location.search){
		var searchString = document.location.search;
		searchString = searchString.substring(1);

		var nvPairs = searchString.split("&");

		var siteurl = "";

		for (i = 0; i < nvPairs.length; i++)
		{
		     var nvPair = nvPairs[i].split("=");
		     var name = nvPair[0];
		     var value = nvPair[1];
		     if(nvPair[0] == "u"){
		     	siteurl = nvPair[1];
		     }
		}
	}else{
		siteurl = window.location.href;
	}

	if(lang == "en"){
		window.location = siteurl;
		return;
	}

	window.location = "http://64.233.179.104/translate_p?hl=en&langpair=en|"+lang+"&u=" + siteurl;

	//alert(window.self.location);
}