var BrowserDetect = {
	init: function () {
		this.browser = this.searchString(this.dataBrowser) || "An unknown browser";
		this.version = this.searchVersion(navigator.userAgent)
			|| this.searchVersion(navigator.appVersion)
			|| "an unknown version";
		this.OS = this.searchString(this.dataOS) || "an unknown OS";
		return this;
	},
	searchString: function (data) {
		for (var i=0;i<data.length;i++)	{
			var dataString = data[i].string;
			var dataProp = data[i].prop;
			this.versionSearchString = data[i].versionSearch || data[i].identity;
			if (dataString) {
				if (dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
			else if (dataProp)
				return data[i].identity;
		}
	},
	searchVersion: function (dataString) {
		var index = dataString.indexOf(this.versionSearchString);
		if (index == -1) return;
		return parseFloat(dataString.substring(index+this.versionSearchString.length+1));
	},
	dataBrowser: [
		{
			string: navigator.userAgent,
			subString: "Chrome",
			identity: "Chrome"
		},
		{ 	string: navigator.userAgent,
			subString: "OmniWeb",
			versionSearch: "OmniWeb/",
			identity: "OmniWeb"
		},
		{
			string: navigator.vendor,
			subString: "Apple",
			identity: "Safari",
			versionSearch: "Version"
		},
		{
			prop: window.opera,
			identity: "Opera"
		},
		{
			string: navigator.vendor,
			subString: "iCab",
			identity: "iCab"
		},
		{
			string: navigator.vendor,
			subString: "KDE",
			identity: "Konqueror"
		},
		{
			string: navigator.userAgent,
			subString: "Firefox",
			identity: "Firefox"
		},
		{
			string: navigator.vendor,
			subString: "Camino",
			identity: "Camino"
		},
		{		// for newer Netscapes (6+)
			string: navigator.userAgent,
			subString: "Netscape",
			identity: "Netscape"
		},
		{
			string: navigator.userAgent,
			subString: "MSIE",
			identity: "Explorer",
			versionSearch: "MSIE"
		},
		{
			string: navigator.userAgent,
			subString: "Gecko",
			identity: "Mozilla",
			versionSearch: "rv"
		},
		{ 		// for older Netscapes (4-)
			string: navigator.userAgent,
			subString: "Mozilla",
			identity: "Netscape",
			versionSearch: "Mozilla"
		}
	],
	dataOS : [
		{
			string: navigator.platform,
			subString: "Win",
			identity: "Windows"
		},
		{
			string: navigator.platform,
			subString: "Mac",
			identity: "Mac"
		},
		{
			   string: navigator.userAgent,
			   subString: "iPhone",
			   identity: "iPhone/iPod"
	    },
		{
			string: navigator.platform,
			subString: "Linux",
			identity: "Linux"
		}
	]

};

var browser = BrowserDetect.init();


function getWindowSize(){
	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 - 13;
		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;
	}
	
	var data_returned = new Array ();
  	data_returned[0] = pageWidth;
  	data_returned[1] = pageHeight;
  	data_returned[2] = windowWidth;
  	data_returned[3] = windowHeight;
  	return data_returned;
}


function JSConfirm( question, redirectUrl )
{
	if( confirm( question ) )
	{
		window.location = redirectUrl;
	}
}


function openPopup( url, width, height, scrollbars, resizeable )
{
    window.open( url, 'popup', 'width='+width+', height='+height+', scrollbars='+scrollbars+', resizeable='+resizeable+'' )
}


function showHideLanguages()
{
	if( $( 'lang_Drop' ).style.display == 'none' )
	{
		$( 'lang_Drop' ).style.display = 'block';
	}
	else
	{
		$( 'lang_Drop' ).style.display = 'none'
	}
}




var AJAX = Class.create({
	initialize: function( url, callback ) {
    	this.url 			= url;
    	ajax_calback 	= callback;
    },
	
    startWithJson: function()
    {
    	new Ajax.Request(this.url,
 		{
    		method:'get',
    		requestHeaders: {Accept: 'application/json'},
    		onSuccess: function(transport){
      			var data = transport.responseText.evalJSON(true);
      			
      			eval( ajax_calback + '(data)'  );
      			
    		},
    		onFailure: function(){ alert('AJAX FAIL');  }
  		});
    }
});


function changeFocusInputStyleTelAFriend( id, type, inpType )
{
	if( type )
	{
		if( inpType )
		{
			document.getElementById( id ).className = 'tf_input_parrent hovered_input';	
		}
		else
		{
			document.getElementById( id ).className = 'tf_tarea_parrent hovered_tarea';	
		}
	}
	else
	{
		if( inpType )
		{
			document.getElementById( id ).className = 'tf_input_parrent';	
		}
		else
		{
			document.getElementById( id ).className = 'tf_tarea_parrent';	
		}
	}
}


function changeFocusInputStyle( id, type, inpType )
{
	if( type )
	{
		if( inpType )
		{
			document.getElementById( id ).className = 'inputparrent_dark';	
		}
		else
		{
			document.getElementById( id ).className = 'textarea_field_hover';	
		}
	}
	else
	{
		if( inpType )
		{
			document.getElementById( id ).className = 'inputparrent';	
		}
		else
		{
			document.getElementById( id ).className = 'textarea_field';	
		}
	}
}


function addToFavorites( url, title )
{ 

	if ( window.sidebar )
	{
		// Mozilla Firefox Bookmark
		window.sidebar.addPanel(title, url,"");
	}
	else if( window.external )
	{
		// IE Favorite
		window.external.AddFavorite( url, title);
	}
	else if(window.opera && window.print)
	{
		alert('You are using a browser that does not support adding Favorites by script, please add it manually');
	}
	
}


function changeLanguageBackground( mode )
{
	if( !mode )
	{
		$( 'lang_main' ).className = 'language';
	}
	else
	{
		$( 'lang_main' ).className = 'language language_hover';
	}
}
