
var Site = {

  jumpToDiscussion:'',

point_it: 	function(event){
		pos_x = event.offsetX?(event.offsetX):event.pageX-document.getElementById("pointer_div").offsetLeft;
		pos_y = event.offsetY?(event.offsetY):event.pageY-document.getElementById("pointer_div").offsetTop;
	},	
	
  load: function()
  {
	//alert('cruiser');
    this.popups();
    this.spotErrors();
		this.showMessage();
    
    if(this.jumpToDiscussion)
    {
			//alert('dreadnought');
      document.location.href = '#'+this.jumpToDiscussion;
    }
  },
  
  
  popups: function()
  {
		//alert('frigate');
    $('a[rel=popup]').each(function(index){
      
      $(this).click(function(e) {
				//alert('destroyer');
        Event.stop(e);
        Site.loadPopup(this.attr('href'));
      }, false);
      
    });
  },
  
  //cpo todo rationalise popups
  loadPopup: function(url,options)
  {
		//alert('tug');
    options = {};
    options.width = 700;
    options.height = 500;
    
    if(url.indexOf('?') !== -1)
    {
      url = url + '&mpopup=true';
    }
    else
    {
      url = url + '?mpopup=true';
    }
    
    window.open(url,'','width='+options.width+',height='+options.height+',toolbar=0,resizable=1,scrollbars=1');
  },

	popup: function(url) 
	{
			params  = 'width='+screen.width;
			params += ', height='+screen.height;
			params += ', top=0, left=0'
			params += ', fullscreen=yes';
		
			params += ', scrollbars=yes';
			params += ', toolbar=yes';
			params += ', toolbar=yes';
			params += ', location=yes';

			newwin=window.open(url,'windowname4', params);
			if (window.focus) {newwin.focus()}
			return false;
},
	
	spotErrors: function() 
	{
		
		//alert('joe');
		
		elements = $('div.error-message');

		hasError = false;
    elements.each(
									function(index) {
									//alert(index.text );
									hasError = true
									}
									);
		if (hasError){
			alert('Problem with form: Please see incorrect or missing field(s) shown highlighted in red');
		}
	},

	showMessage: function() 
	{
		elements = $('p.message');

		hasMessage = false;
    elements.each(
									function(index) {
									alert(index.innerHTML );
									hasMessage = true
									}
								);

		elements = $('div.message');

		hasMessage = false;
    elements.each(
									function(index) {
									alert(index.innerHTML );
									hasMessage = true
									}
								);



	},	
	
	backToSearch: function(url) 
	{
		
		//alert('joe');
		//alert(location.href);
//		history.go('dev.tgl.tv/search2');
//		 history.go('Search');
		 history.go(-2);
		//alert(location.href);
		
	},
	
	backToSearchStr: function(url) 
	{
		
		 history.go(url);
		return false;
		
	},

	launchLibrary: function(elem,desc,filename){
				//alert(filename);
				if(!document.getElementById(desc)){
					alert('Field not found');

					elem.checked = false;
					return false;
				}
				if(!document.getElementById(filename)){
					alert('Field not found');
					elem.checked = false;
					return false;
				}
	

				var newdesc = document.getElementById(desc).value;
				var newname = document.getElementById(filename).value;

				if((newdesc == '')||(newname == '')){
					alert('Please fill in the Description and select a file');
					elem.checked = false;
					return false;
				}
				//alert(newname);

				var newWin = window.open('/manage/libraries/add/photo&mpopup=true','popup', 'width=960,height=400,scrollbars,toolbar,menubar,location');

//				alert(newWin.document);
//				alert(newWin.document.getElementById("LibraryDescription"));

//				alert(newWin.document.getElementById("form_left"));

/*
				$(newWin).ready(function()
				{
						alert('newWin.document ready');
						newWin.document.getElementById("LibraryDescription").value = "demo pasted in";
				}
				);
*/
				//this only appears to trigger properly with raw JS not JQuery
				newWin.onload = function() { 

						if(newdesc != ''){
							newWin.document.getElementById("LibraryDescription").value = newdesc;
							newWin.document.getElementById("LibraryTitle").value = newname;
						}else{
							newWin.document.getElementById("LibraryDescription").value = "demo pasted in";
						}
				} 
/*
// possible onload chaining
if (window.attachEvent) {window.attachEvent('onload', your_function);}
else if (window.addEventListener) {window.addEventListener('load', your_function, false);}
else {document.addEventListener('load', your_function, false);} 
*/
			elem.checked = true;
			return true;

	}
		
	
	
}

$(document).ready(function()
{
	Site.load();
}
);

