/**
* ActivEngine main JS file
*/

var root_url = 'http://www.gersaint.com';

// Array of file to include
var files = new Array(
	root_url + '/templates/default/js/tinymce/tiny_mce.js',
	root_url + '/templates/default/js/scriptaculous/lib/prototype.js',
	root_url + '/templates/default/js/scriptaculous/src/scriptaculous.js'

);

// inserting via DOM fails in Safari 2.0, so brute force approach
for ( i = 0; i < files.length; i++ ) {
    document.write('<script type="text/javascript" src="'+files[i]+'"></script>');
}

function loadPage(page)
{
  var content = $('content');
  new Ajax.Request(page, {
  		     method: 'get',
		     setRequestHeaders:['cache-control','no-store', 'no-cache', 'must-revalidate'],
		     onLoading: function() {
		       $('loader').style.display = 'block';
		     },
		     onLoaded: function() { $('loader').style.display = 'none'; },
  		     onSuccess: function(transport) {
  		       var response = transport.responseText;
  		       content.update(response);
		     },
		     onFailure: function() { alert('Something went wrong ...') }
		   });
		     }


function loadEditor(page)
{
	var content = $('content');

	new Ajax.Request(page, {
  		method: 'get',
		onLoading: function() {
			//content.update();
			$('loader').style.display = 'block';
		},
		onLoaded: function() { $('loader').style.display = 'none'; },
  		onSuccess: function(transport) {
			var response = transport.responseText;
  			content.update(response);

			tinyMCE.idCounter=0;
			tinyMCE.execCommand('mceAddControl', true, 'elm1');
		},
		onFailure: function() { alert('Something went wrong ...') }
	});
}

function loadAllCatalogue(page)
{
  var content = $('content-body');

  new Ajax.Request(page, {
  		     method: 'get',
		     setRequestHeaders:['cache-control','no-store', 'no-cache', 'must-revalidate'],
		       onLoading: function() {
			 $('loader').style.display = 'block';
		       },
		       onLoaded: function() { $('loader').style.display = 'none'; },
  		       onSuccess: function(transport) {
  			 var response = transport.responseText;
  			 content.update(response);
		       },
		       onFailure: function() { alert('Something went wrong ...') }
		   });
}

function loadCatalogue(page)
{
  var content = $('catalogue');
  javascript:scroll(0,0);

  new Ajax.Request(page, {
  		     method: 'get',
		     setRequestHeaders:['cache-control','no-store', 'no-cache', 'must-revalidate'],
		     onLoading: function() {
		       $('loader-catalog').style.display = 'block';
		     },
		     onLoaded: function() { $('loader-catalog').style.display = 'none'; },
  		     onSuccess: function(transport) {
  		       var response = transport.responseText;
  		       content.update(response);
		     },
		     onFailure: function() { alert('Something went wrong ...') }
		   });
}

function loadAuction(page)
{
	var content = $('objet-detail-blue');

	new Ajax.Request(page, {
  		method: 'get',
		setRequestHeaders:['cache-control','no-store', 'no-cache', 'must-revalidate'],
  		onSuccess: function(transport) {
  			var response = transport.responseText;
  			content.update(response);
		},
		onFailure: function() { alert('Something went wrong ...') }
	});
}

function submitForm(form, page)
{
	var content = $('content');

	new Ajax.Request(page, {
		method: 'post',
		parameters: $(form).serialize(),
    enctype: 'multipart/form-data',
		onLoading: function() {  $('locale_add').value = '...'; },
		//onLoaded: function() { $('locale_add').value = ''; },
  		onSuccess: function(transport) {
  			var response = transport.responseText;
  			content.update(response);
		},
		onFailure: function() { alert('Something went wrong ...') }
	});
}

function submitSearchCatalogue(form, page, content)
{
	var content = $(content);

	new Ajax.Request(page, {
		method: 'post',
		parameters: $(form).serialize(),
		onLoading: function() {  $('locale_add').value = '...'; },
  		onSuccess: function(transport) {
  			var response = transport.responseText;
  			content.update(response);
		},
		onFailure: function() { alert('Something went wrong ...') }
	});
}

function submitFormEditor(form, page)
{
	TinyMCE.prototype.triggerSave(true,true);
	var content = $('content');
	new Ajax.Request(page, {
		method: 'post',
		parameters: $(form).serialize(),
		onLoading: function() {  $('locale_add').value = '...'; },
		//onLoaded: function() { $('locale_add').value = ''; },

  		onSuccess: function(transport) {
  			var response = transport.responseText;
  			content.update(response);
		},
		onFailure: function() { alert('Something went wrong ...') }
	});
}

function openPopup(url, w, h, x, y)
{
	var win = window.open(url, '_blank','width='+w+',height='+h+',left='+x+',top='+y+',directories=no,location=no,menubar=no,resizable=yes,scrollbars=no,status=no,toolbar=no');
	win.focus();
}

function openImagePopup(url)
{
  var navigatorname = navigator.appName;

  if ( navigatorname == 'Netscape' ) {
    openPopup(url, screen.width, screen.height, screen.width / 2, screen.height / 2);
  }
  else {
    var image = new Image();
    image.src = url;
    var width = image.width + 40;
    var height = image.height + 50;
    var x = (screen.width - width) / 2;
    var y = (screen.height - height) / 2;

    openPopup(url, width, height, x, y);
  }
}

function addFile(form, part)
{
	var hchars  = "1234567890";
	var hlength = 7;
	var hash = '';
	for ( i = 0; i < hlength; i++ ) {
		hash += hchars.charAt( Math.floor( Math.random() * hchars.length ) )
	}

	var html = '<p id="p_'+hash+'"><input type="file" id="file_new[]" name="file_new[]" size="40" /> ';
	html += '<a onClic="$(\'p_'+hash+'\').remove()">Supprimer</a></p>';

	new Insertion.Bottom(part, html);
}

function suggestPassword()
{
	var pwchars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
	var passwordlength = 8; // do we want that to be dynamic?  no, keep it simple :)
	var passwd = document.getElementById('user_password');
	passwd.value = '';

	for ( i = 0; i < passwordlength; i++ ) {
		passwd.value += pwchars.charAt( Math.floor( Math.random() * pwchars.length ) )
	}

	return passwd.value;

}

/* -- Tooltips functions -- */
// position of the tooltip relative to the mouse in pixel //
var offsetx = 12;
var offsety =  8;

function newelement(newid)
{
    if(document.createElement)
    {
        var el = document.createElement('div');
        el.id = newid;
        with(el.style)
        {
            display = 'none';
            position = 'absolute';
        }
        el.innerHTML = '&nbsp;';
        document.body.appendChild(el);
    }
}
var ie5 = (document.getElementById && document.all);
var ns6 = (document.getElementById && !document.all);
var ua = navigator.userAgent.toLowerCase();
var isapple = (ua.indexOf('applewebkit') != -1 ? 1 : 0);
function getmouseposition(e)
{
    if(document.getElementById)
    {
        var iebody=(document.compatMode &&
            document.compatMode != 'BackCompat') ?
                document.documentElement : document.body;
        pagex = (isapple == 1 ? 0:(ie5)?iebody.scrollLeft:window.pageXOffset);
        pagey = (isapple == 1 ? 0:(ie5)?iebody.scrollTop:window.pageYOffset);
        mousex = (ie5)?event.x:(ns6)?clientX = e.clientX:false;
        mousey = (ie5)?event.y:(ns6)?clientY = e.clientY:false;

        var lixlpixel_tooltip = document.getElementById('tooltip');
        lixlpixel_tooltip.style.left = (mousex+pagex+offsetx) + 'px';
        lixlpixel_tooltip.style.top = (mousey+pagey+offsety) + 'px';
    }
}
function tooltip(tip)
{
    if(!document.getElementById('tooltip')) newelement('tooltip');
    var lixlpixel_tooltip = document.getElementById('tooltip');
    lixlpixel_tooltip.innerHTML = tip;
    lixlpixel_tooltip.style.display = 'block';
    document.onmousemove = getmouseposition;
}
function exit()
{
    document.getElementById('tooltip').style.display = 'none';
}

function printsale(url,id)
{
  window.open(url+"/print/print_object.php?id="+id, '_blank','width=620,height=700,left=0,top=0,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
}
function printsalecustomer(url,id, customer)
{
  window.open(url+"/print/print_object.php?id="+id+"&customer="+customer, '_blank','width=620,height=750,left=0,top=0,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
}
function adminprintzone(url,id)
{
  window.open(url+"/account/print_user.php?id="+id, '_blank','width=620,height=700,left=0,top=0,directories=no,location=no,menubar=no,resizable=no,scrollbars=no,status=no,toolbar=no');
}
function printvaluation(url,id)
{
  window.open(url+"/account/valuation.php?type=print&valuation_id="+id, '_blank','width=620,height=700,left=0,top=0,directories=no,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no');
}
function displaycat(url)
{
  window.open(url+"/account/display_sub_cat.php", '_blank','width=620,height=700,left=0,top=0,directories=no,location=no,menubar=no,resizable=no,scrollbars=yes,status=no,toolbar=no');
}
