/*
	BackOffice Function (application/views/scripts/backoffice/index.phtml)
*/
function backoffice_confirm_deletion(){
	var form = $('changeUserDetails');
	var input = form['accountAction'];

	if($(input).getValue() == 'delete'){
		var r = confirm("Do you really want to delete this account?")
		if (r==true){
			form.submit();
		}
	}
	else{
		form.submit();
	}
}

/*
	Friend List Functions (application/views/scripts/mysettings/index.phtml)
*/

function update_friends(url){

	if($('loadingdiv_friends').style.display == "none"){
		$('loadingdiv_friends').style.display = "block";
		$('orderedList').style.display = "none";
	};
	var liste = Sortable.serialize('orderedList');
	t_liste = liste.split("&");
	page = url.replace("valueToChange", t_liste);

	window.location.href = page;
}

function showdivSelection_friends(){
	Effect.toggle('divSelection_friends','blind');
	var liste = Sortable.serialize('orderedList');
	input = '<input type="hidden" name="listOrder" value="' + liste + '" id="listOrder">'
	$('additional').innerHTML = input;
}

function hidedivSelection_friends(){
	Effect.toggle('divSelection_friends','blind');
}

/*
	Account Creation Function (application/views/scripts/home/accountcreation.phtml)
*/

function checkavailability(name, url, language){
	page = url + '?subDomaine_name=' + name + language;
	new Ajax.Updater('availability', page, {asynchronous:true});
}

/*
	Link List Funxtions (application/views/scripts/mysettings/index.phtml)
*/

function update_links(url){
	if($('loadingdiv_links').style.display == "none"){
		$('loadingdiv_links').style.display = "block";
		$('orderedList_links').style.display = "none";
	};
	var liste = Sortable.serialize('orderedList_links');
	t_liste = liste.split("&");
	page = url.replace("valueToChange", t_liste);

	// page = baseUrl + '/mySettings/updateLinkOrder/listOrder/' + t_liste + '/type/link?name=' + username ;
	window.location.href = page;
}

function showdivSelection_links(){
	Effect.toggle('divSelection_links','blind');
	var liste = Sortable.serialize('orderedList_links');
	input = '<input type="hidden" name="listOrder" value="' + liste + '" id="listOrder">'
	$("additional_links").innerHTML = input;
}

function hidedivSelection_links(){
	Effect.toggle('divSelection_links','blind');
}

/*
	My Settings Function (application/views/scripts/mysettings/index.phtml)
*/
function disp_confirm(text){
	var r=confirm(text);
	if (r==true){
		document.deleteBannerForm.submit();
	}
}

/*
	On Home files functions (application/views/scripts/onHome_files.php)
*/

function update_files(url){
	if($('loadingdiv_files').style.display == "none"){
		$('loadingdiv_files').style.display = "block";
		$('orderedList_files').style.display = "none";
	};
	var liste = Sortable.serialize('orderedList_files');
	t_liste = liste.split("&");
	url = url.replace("valueToChange", t_liste);

	window.location.href = url;
}

function showdivSelection_files(){
	Effect.toggle('divSelection_files','blind');
	var liste = Sortable.serialize('orderedList_files');
	input = '<input type="hidden" name="listOrder" value="' + liste + '" id="listOrder">'
	$("additional").innerHTML = input;
}

function hidedivSelection_files(){
	Effect.toggle('divSelection_files','blind');
}

/*
	On Home Galleries Functions (application/views/scripts/mygalleries/ordergalleries.php)
*/

function update(url){
	if($('loadingdiv').style.display == "none"){
		$('loadingdiv').style.display = "block";
		$('orderedList').style.display = "none";
	};
	var liste = Sortable.serialize('orderedList');
	t_liste = liste.split("&");
	url = url.replace("valueToChange", t_liste);
	window.location.href = url;
}

function showDivSelection(){
	Effect.toggle('divSelection','blind');
	var liste = Sortable.serialize('orderedList');
	input = '<input type="hidden" name="listOrder" value="' + liste + '" id="listOrder">'
	$("additional_gallery").innerHTML = input;
}

function hideDivSelection(){
	Effect.toggle('divSelection','blind');
}

/*
	Add Photo to Gallery Function (application/controllers/GalleriesController.php / application/views/scripts/files/selected.phtml)
*/

function addPhotoToGallery(url, galleryId){
	page = url + '/' + galleryId;
	window.location.href = page;
}

function displayGalleryList(url){
	if ($('addToGalleryDiv').style.display == 'none') {
		$('loadingImg').style.display = 'block';

		new Ajax.Updater(	'addToGalleryDiv',
							url,
							{
								onComplete:function(){
									new Effect.toggle('addToGalleryDiv', 'appear');
									$('loadingImg').style.display = 'none';
								},
								asynchronous:true}
						);
	}
	else{
		new Effect.toggle('addToGalleryDiv', 'appear');
	};
}

Object.extend(Event, {
    _domReady : function() {
        if (arguments.callee.done) return;
        arguments.callee.done = true;

        if (this._timer)  clearInterval(this._timer);

        this._readyCallbacks.each(function(f) { f() });
        this._readyCallbacks = null;
        this._loaded = true;
    },

    onDOMReady : function(f) {
        if (this._loaded) {
            f();
            return ;
        }
        if (!this._readyCallbacks) {
            var domReady = this._domReady.bind(this);

            if (document.addEventListener)
                document.addEventListener("DOMContentLoaded", domReady, false);

            /*@cc_on @*/
            /*@if (@_win32)
              document.write("<script id=__ie_onload defer src=javascript:void(0)><\/script>");
              document.getElementById("__ie_onload").onreadystatechange = function() {
              if (this.readyState == "complete") domReady();
              };
              /*@end @*/

            if (/WebKit/i.test(navigator.userAgent)) {
                this._timer = setInterval(function() {
                    if (/loaded|complete/.test(document.readyState)) domReady();
                }, 10);
            }

            Event.observe(window, 'load', domReady);
            Event._readyCallbacks =  [];
        }
        Event._readyCallbacks.push(f);
    }
});
