function createXhrObject() {
    if (window.XMLHttpRequest) 
        return new XMLHttpRequest();
    
    if (window.ActiveXObject) {
        var names = ["Msxml2.XMLHTTP.6.0", "Msxml2.XMLHTTP.3.0", "Msxml2.XMLHTTP", "Microsoft.XMLHTTP"];
        for (var i in names) {
            try {
                return new ActiveXObject(names[i]);
            } 
            catch (e) {
            }
        }
    }
    window.alert("Votre navigateur ne prend pas en charge l'objet XMLHTTPRequest.");
    return null; // non supporté
}

var donation_req = new createXhrObject();
var last_donation, donation;

function edit_donation(id) {
	
	donation = document.getElementById('donation_' + id);
	donation_req.open('GET', 'admin/donations.php?mode=edit&id=' + id, true);
	donation_req.onreadystatechange = donations;
	donation_req.send(null);
}

function donations() {
	
	if (donation_req.readyState == 4) {
		donation.innerHTML = donation_req.responseText;
	}
}

function usesShowAll() {
	document.getElementById('listing-use').style.overflow = "visible";
	document.getElementById('uses-showall').innerHTML = "";
}

function showPanel(){
    if (document.getElementById('imagePanel').style.display == 'none') {
        document.getElementById('imagePanel').style.display = 'block';
    }
    else {
        document.getElementById('imagePanel').style.display = 'none';
    }
}

function showLightbox(action){
    if (action == 'show') {
        document.getElementById('LightboxPanel').style.display = 'block';
    }
    else {
        document.getElementById('LightboxPanel').style.display = 'none';
    }
}

function showHuge(){
    if (document.getElementById('hugePanel').style.display == 'block') {
        document.getElementById('hugePanel').style.display = 'none';
    }
    else {
        document.getElementById('hugePanel').style.display = 'block';
    }
}

function searchOptions(){
    if (document.getElementById('search-options').style.display == 'none') {
        document.getElementById('search-options').style.display = 'block';
        document.getElementById('search-options-image').src = 'gfx/search_options_2.gif';
    }
    else {
        document.getElementById('search-options').style.display = 'none';
        document.getElementById('search-options-image').src = 'gfx/search_options.gif';
    }
}

function teampop(url) {
	
	var team_pop = document.getElementById('team_pop');
	
	team_pop.style.display = "block";
	team_pop.innerHTML = '<a href="javascript:closeTeampop();"><img src="' + url + '"></a>';
	
}

function closeTeampop() {
	document.getElementById('team_pop').style.display = "none";
}

function updateForm(){
    if (document.getElementById('action_selection').value == 'lightbox') {
        document.getElementById('admin-form-selection').action = 'index.php?a=lightbox';
    }
    else {
        document.getElementById('admin-form-selection').action = 'index.php?a=randomize';
    }
}

function showStock(x, y, z){
    if (document.getElementById('stockpanel').style.display == 'block') {
        document.getElementById('stockpanel').style.display = 'none';
    }
    else {
        document.getElementById('stockpanel').style.display = 'block';
    }
    
    document.getElementById('imageid').value = x;
    document.getElementById('istockid').value = y;
    document.getElementById('fotoliaid').value = z;
    
    document.getElementById('image_istock').src = 'galleries/image_v2.php?imgid=' + x;
    
}

function launchFree(action){
    var div = document.getElementById('free_downloads');
    if (action == "close") {
        div.style.display = "none";
    }
    else {
        div.style.display = "block";
    }
}

function newsAuteur(){
    var div = document.getElementById('auteur');
    if (div.value == "x" || div.value == "X") {
        div.value = "Xavier";
    }
    else 
        if (div.value == "g" || div.value == "G") {
            div.value = "Gabor";
        }
        
        else 
            if (div.value == "e" || div.value == "E") {
                div.value = "Eric";
            }
}

function toggle_visibility(id){
    var e = document.getElementById(id);
    if (e.style.display == 'block') 
        e.style.display = 'none';
    else 
        e.style.display = 'block';
}

var option_yourl = createXhrObject();

function toggle_lightboxes() {
	var e = document.getElementById('lightbox_machine');
	if(e.style.display == 'block') {
		e.style.display = 'none';
		option_yourl.open('GET', 'lightbox/your.lightboxes.php?display=none', true);
		option_yourl.send(null);
		document.getElementById('your_lightboxes').style.backgroundImage = "url('gfx/your.gif')";
	}
	else {
		e.style.display = 'block';
		option_yourl.open('GET', 'lightbox/your.lightboxes.php?display=block', true);
		option_yourl.send(null);
		document.getElementById('your_lightboxes').style.backgroundImage = "url('gfx/your_2.gif')";
	}
}

var video_flux = createXhrObject();

function derouler(id){
    if (document.getElementById(id).style.display == "none") {
        document.getElementById(id).style.display = "block";
    }
    else {
        document.getElementById(id).style.display = "none";
    }
}

function in_array(recherche, tableau) {
	for(i = 0; i < tableau.length; i++) {
		if(tableau[i] == recherche) {
			return true;
		}
		else {
			return false;
		}
	}
}

function addComment() {
	
	var btn = document.getElementById('comment_button').style.backgroundImage;
	
	document.getElementById('button_container').innerHTML = '<input type="submit" value="" class="submit" id="comment_button">';
	document.getElementById('comment_button').style.backgroundImage = btn;
	document.getElementById('comment').style.display = "block";
}

var fondNoir, passFenetre;

function lostPassword() {
	
	fondNoir = document.createElement('div');
	fondNoir.style.opacity = 0.8;
	fondNoir.style.backgroundColor = "black";
	fondNoir.style.position = "fixed";
	fondNoir.style.top = "0px";
	fondNoir.style.left = "0px";
	fondNoir.style.width = window.innerWidth + "px";
	fondNoir.style.height = window.innerHeight + "px";
	
	passFenetre = document.createElement('div');
	passFenetre.style.backgroundColor = "white";
	passFenetre.style.position = "fixed";
	passFenetre.style.width = "640px";
	passFenetre.style.height = "180px";
	passFenetre.style.top = (window.innerHeight/2) - (parseInt(passFenetre.style.height.replace('px',''))/2) + "px";
	passFenetre.style.left = (window.innerWidth/2) - (parseInt(passFenetre.style.width.replace('px',''))/2) + "px";
	passFenetre.style.padding = "20px 0px 0 20px";
	
	passFenetre.innerHTML = "<h1>Password recovery</h1><p>Please enter your Email address : <input type=\"text\" id=\"lost_email\" value=\"\"> <input type=\"button\" value=\"Send\" onclick=\"lostPassSend();\"></p><p id=\"lost_status\">You will receive an email with a link to redefine a new password.</p><p><br><input type=\"button\" value=\"Close\" onclick=\"lostPassClose();\"></p>";
	
	document.body.appendChild(fondNoir);
	document.body.appendChild(passFenetre);
	
}

function lostPassSend() {
	
	var lost_email = document.getElementById('lost_email').value;
	
	var requete_lost = new createXhrObject();
	
	requete_lost.open('GET', 'password_recovery/password_recovery.php?lost=' + lost_email, true);
	requete_lost.onreadystatechange = function() {
		
		if(requete_lost.readyState == 4) {
			
			document.getElementById('lost_status').innerHTML = requete_lost.responseText;
			
		}
		
		else {
			
			document.getElementById('lost_status').innerHTML = '<img src="password_recovery/preloader.gif">';
			
		}
		
	}
	requete_lost.send(null);
	
}

function lostPassClose() {
	
	document.body.removeChild(fondNoir);
	document.body.removeChild(passFenetre);
	
}

function isIE() {
	return /msie/i.test(navigator.userAgent) && !/opera/i.test(navigator.userAgent);
}

function videos(id, url, ref){
	document.getElementById('videos').innerHTML = '<object width="425" height="350"><param name="movie" value="http://www.youtube.com/v/' + url + '" id="video_param"></param><embed src="http://www.youtube.com/v/' + url + '" id="video_embed" type="application/x-shockwave-flash" width="425" height="350"></embed></object>';	
	document.getElementById('urls').innerHTML = '<a href="index.php?search=' + ref + '&search_filter=refs&showmore=1"><img src="gfx/from_session.gif" border="0"></a>';
	document.getElementById('makingof_id').value = id;
}

function videos_local(id, url, ref){
	document.getElementById('videos').innerHTML = '<object id="monFlash" type="application/x-shockwave-flash" data="videos/player_flv.swf" width="622" height="350"><param name="movie" value="videos/player_flv.swf" /><param name="FlashVars" value="flv=' + url + '&amp;width=622&amp;height=350&amp;showvolume=1&amp;autoplay=1&amp;srt=1" /></object>';
	document.getElementById('urls').innerHTML = '<a href="index.php?search=' + ref + '&search_filter=refs&showmore=1"><img src="gfx/from_session.gif" border="0"></a>';
	document.getElementById('makingof_id').value = id;
}


var niceView, niceImg, centrage, targetx, targety, targetw, targeth, targeto, sensito, niceLoader, niceDirection, niceImgId;

var niceListen = "none";

var niceImages = new Array();
var niceStartPos = 0;

function niceView2(el, id) {

	niceImages = document.getElementById('niceHistory').value.split(', ');
	
	niceImgId = id;
	
	niceView = document.createElement('div');
	niceView.style.backgroundColor = "black";
	niceView.style.opacity = 0;
	niceView.style.position = "absolute";
	niceView.style.top = 0;
	niceView.style.left = 0;
	niceView.style.width = window.innerWidth + "px";
	niceView.style.height = window.innerHeight + "px";
	niceView.id = "niceView"; 
	
	niceImg = document.createElement('img');
	niceImg.src = el.src;
	niceImg.style.position = "absolute";
	niceImg.style.top = el.offsetTop + "px";
	niceImg.style.left = el.offsetLeft + "px";
	niceImg.style.border = "1px solid #CCCCCC";
	niceImg.style.opacity = 1;
	
	niceImg.onclick = function() {
		
		niceClose();
	
	}
	
	targetx = window.innerWidth/2 - el.offsetWidth/2;
	targety = window.innerHeight/2 - el.offsetHeight/2;

	document.body.appendChild(niceView);
	niceView.appendChild(niceImg);
	
	targetw = niceImg.offsetWidth;
	targeth = niceImg.offsetHeight;
	
	targeto = 1;
	
	var fade = setInterval(function() {
		
		niceView.style.opacity = parseFloat(niceView.style.opacity) + 0.1;
		
		if(parseFloat(niceView.style.opacity) == 1.0) {			
			niceLoad2(id, niceImg);
			clearInterval(fade);
		}

	}, 20);

}

function niceLoad2(id, el) {
	
	document.getElementById('header').style.display = "none";
	document.getElementById('menu').style.display = "none";
	document.getElementById('new_search_zone').style.display = "none";
	document.getElementById('page').style.display = "none";
	
	sensito = document.createElement('div');
	sensito.style.position = "absolute";
	
	sensito.style.width = "560px";
	sensito.style.height = "30px";
	
	sensito.style.top = window.innerHeight - 30 + "px";
	sensito.style.left = (window.innerWidth/2) - (560/2) + "px";
	
	niceLoader = document.createElement('img');
	niceLoader.src = "gfx/loader_large.gif";
	
	niceLoader.style.position = "absolute";
	niceLoader.style.top = window.innerHeight - 30 - 12 + "px";
	niceLoader.style.left = window.innerWidth/2 - 50 + "px";
	
	niceView.appendChild(niceLoader);	
	niceView.appendChild(sensito);
	
	var gris = 0;
	
	
	for(x = 0; x < 16; x++) {
	
		var niceGris = document.createElement('div');
		niceGris.style.width = 30 + "px";
		niceGris.style.height = 30 + "px";
		niceGris.style.backgroundColor = 'rgb(' + gris + ',' + gris + ',' + gris + ')';
		niceGris.style.cssFloat = "left";
		niceGris.style.marginRight = "5px";
		
		niceGris.onmouseover = function() {
			
			niceView.style.backgroundColor = this.style.backgroundColor;
			
		}
		
		sensito.appendChild(niceGris);
		
		gris = gris + 17;
	
	}
	
	
	var niceReq = new createXhrObject();
	niceReq.open('GET', 'large.thumbs.php?id=' + id, true);
	niceReq.send(null);
	niceReq.onreadystatechange = function() {
		niceThumbs(niceReq, id);
	}
	
	centrage = setInterval(function() {
	
		var difx = targetx - el.offsetLeft;
		var dify = targety - el.offsetTop;
		
		var difw = targetw - el.offsetWidth;
		var difh = targeth - el.offsetHeight;
		
		var difo = targeto - parseFloat(niceImg.style.opacity);
		
		if(niceListen == "targeto") {
		
			if(Math.round(el.style.opacity) == 0) {
				el.style.opacity = 0;
				niceNextSuite();
				niceListen = "none";
			}
		
		}
		
		el.style.left = Math.round(el.offsetLeft + difx/10) + "px";
		el.style.top = Math.round(el.offsetTop + dify/10) + "px";
		
		el.style.width = Math.round(el.offsetWidth + difw/10) + "px";
		el.style.height = Math.round(el.offsetHeight + difh/10) + "px";
		
		el.style.opacity = parseFloat(el.style.opacity) + difo/5;
		
	
	}, 20);
	
}

function niceThumbs(req, id) {
	
	if(req.readyState == 4) {
			
			niceLoader.style.display = "none";
			
			niceImg.src = 'gfx/large_thumbs/' + id + '.jpg';
		
			var tailles = req.responseText;
			tailles = tailles.split('x');			
			
			targetx = window.innerWidth/2 - tailles[0]/2;
			targety = window.innerHeight/2 - tailles[1]/2;
			
			targetw = tailles[0];
			targeth = tailles[1];
			
			if(niceImages.length > 1) {
			
				setTimeout(function() {
					niceButtons();
				}, 1000);
			
			}

	}
	
}

var play_left, play_right, next_left, next_right, nicePlayAnim;

var nice_playing = 0;

function niceButtons() {

	next_left = document.createElement('img');
	next_left.src = "gfx/left_nice.png";
	
	next_left.style.position = "absolute";
	next_left.style.top = niceImg.offsetTop + niceImg.offsetHeight/2 - 26 + "px"
	next_left.style.left = niceImg.offsetLeft - 70 + "px";
	
	next_left.title = "Previous Picture";
	
	next_left.onclick = function() {
	
		niceNext('arriere');
	
	}
	
	play_left = document.createElement('img');
	play_left.src = "gfx/left_play_nice.png";
	
	play_left.style.position = "absolute";
	play_left.style.top = niceImg.offsetTop + niceImg.offsetHeight/2 - 43 + "px" 
	play_left.style.left = niceImg.offsetLeft - 140 + "px";
	
	play_left.title = "Diaporama Backwards";
	
	play_left.onclick = function() {
	
		play_left.src = "gfx/stop_nice.png";
		
		if(nice_playing == 0) {
			nicePlay('arriere');
		}
		else {
			niceStop();
			play_left.src = "gfx/left_play_nice.png";
		}
		
	}
	
	/////////
	
	next_right = document.createElement('img');
	next_right.src = "gfx/right_nice.png";
	
	next_right.onclick = function() {
		
		niceNext('avant');
	}
	
	next_right.style.position = "absolute";
	next_right.style.top = niceImg.offsetTop + niceImg.offsetHeight/2 - 26 + "px"
	next_right.style.left = niceImg.offsetLeft + niceImg.offsetWidth + 10 + "px";
	
	next_right.title = "Next picture";
	
	play_right = document.createElement('img');
	play_right.src = "gfx/right_play_nice.png";
	
	play_right.style.position = "absolute";
	play_right.style.top = niceImg.offsetTop + niceImg.offsetHeight/2 - 43 + "px" 
	play_right.style.left = niceImg.offsetLeft + niceImg.offsetWidth + 80 + "px";
	
	play_right.title = "Diaporama forwards";
	
	play_right.onclick = function() {
		
		play_right.src = "gfx/stop_nice.png";
		
		if(nice_playing == 0) {
			nicePlay('avant');
		}
		else {
			niceStop();
			play_right.src = "gfx/right_play_nice.png";
		}
		
	}
	
	niceView.appendChild(next_left);
	niceView.appendChild(next_right);
	
	niceView.appendChild(play_left);
	niceView.appendChild(play_right);

}

function nicePlay(direction) {

	nice_playing = 1;

	nicePlayAnim = setInterval(function() {
	
		niceNext(direction);
	
	}, 8000);

}

function niceStop() {

	clearInterval(nicePlayAnim);
	
	nice_playing = 0;

}

function niceNext(direction) {
	
	niceLoader.style.display = "block";
	
	niceDirection = direction;
	
	targeto = 0;
	niceListen = "targeto";

}

function niceNextSuite(direction) {

	if(niceDirection == "avant") {
			
		if(niceStartPos + 1 < niceImages.length) {
			niceStartPos++;
		}
		
		else {
			niceStartPos = 0;
		}
	
	}
	
	else {
		
		if(niceStartPos - 1 < 0) {
			niceStartPos = niceImages.length - 1;
		}
		
		else {
			niceStartPos--;
		}
				
	}
	
	niceImgId = niceImages[niceStartPos];

	var niceNextReq = new createXhrObject();
	niceNextReq.open('GET', 'large.thumbs.php?id=' + niceImages[niceStartPos], true);
	niceNextReq.send(null);
	niceNextReq.onreadystatechange = function() {
		
		if(niceNextReq.readyState == 4) {
						
			niceImg.src = 'gfx/large_thumbs/' + niceImages[niceStartPos] + '.jpg';

			var tailles = niceNextReq.responseText;
			tailles = tailles.split('x');			
			
			targetx = window.innerWidth/2 - tailles[0]/2;
			targety = window.innerHeight/2 - tailles[1]/2;
			
			targetw = tailles[0];
			targeth = tailles[1];
			
			setTimeout(function() {
				targeto = 1;
				
				next_right.style.left = niceImg.offsetLeft + niceImg.offsetWidth + 10 + "px";
				play_right.style.left = niceImg.offsetLeft + niceImg.offsetWidth + 80 + "px";
				
				next_left.style.left = niceImg.offsetLeft - 70 + "px";
				play_left.style.left = niceImg.offsetLeft - 140 + "px";
				
			}, 1000);
			
			niceLoader.style.display = "none";

		}
		
	}
	
}

function niceClose() {
	
	window.location = "index.php?a=showImage&imgid=" + niceImgId;

}

function envoiRaw() {
	
	if(document.getElementById('raison_requete').value != "") {
		
		var req = new createXhrObject();
		req.open('GET', 'rawVision/envoi.php?raison=' + $('#raison_requete').val(), true);
		req.send(null);
		req.onreadystatechange = function() {
			if(req.readyState == 4) {
				$('#pop_raw').html(req.responseText);
				$('#pop_raw').animate({
					left 		: $(window).width()/2 - $('#pop_raw').width()/2 + 'px',
					top			: $(window).height()/2 - $('#pop_raw').height()/2 + 'px'
				}, 500);
				setTimeout(function() {
					$('#pop_raw').animate({
						opacity : 0
					}, 500, function() {
						$('#pop_raw').remove();
					})
					
				}, 1500);			
			}		
		}
		
	}
	
	else {
		
		alert('Please complete the reason of your request. Veuillez compléter la raison de votre requête.');
		
	}
	
	
	
}

function optionTri() {


	document.getElementById('select_limit').onchange = function(){
		window.location = 'index.php' + $('#type_requete').val() + '&limit=' + $('#select_limit').val() + '&sort=' + $('#select_ordre').val();
	}
	
	document.getElementById('select_ordre').onchange = function(){
		window.location = 'index.php' + $('#type_requete').val() + '&limit=' + $('#select_limit').val() + '&sort=' + $(this).val();
	}
	
	$('#toggle_raws').click(
		function() {
			toggleRaw();
		}
	);
	
}

function toggleRaw() {
	
	var req = new createXhrObject();
	req.open('GET', 'rawVision/toggle.php', true);
	req.send(null);
	req.onreadystatechange = function() {
		if(req.readyState == 4) {
			window.location = window.location;
		}
	}
							
}



