var oXmlHttp;
var currentUniqueId;
var nTimeout = -666;

window.onload = function(){
	if(typeof(document.all) == 'undefined') {
		document.captureEvents(Event.MOUSEMOVE);
	}
	document.onmousemove = GetMouseCoords;
	//setTimeout("PreviewSticker()", 1500);
	with(window.document){
		getElementById('error_width').style.display = 'none';
		for(var i=1 ; i<=3 ; i++){
			getElementById('sticker_model' + i).onmouseover = function(){
				OnOpenInfobox(this);
			}
			getElementById('sticker_model' + i).onmouseout = OnCloseInfobox;
			getElementById('sticker_model' + i).onmousemove = OnMoveInfobox;
			
			getElementById('line' + i + '_enabled').onmouseover = function(){
				OnOpenInfobox(this);
			}
			getElementById('line' + i + '_enabled').onmousemove = function(){
				OnMoveInfobox();
			}
			getElementById('line' + i + '_enabled').onmouseout = function(){
				OnCloseInfobox();
			}
			getElementById('line' + i + '_enabled').onclick = function(){
				OnLineStatus();
			};
			getElementById('line' + i + '_enabled').onchange = function(){
				//OnPreviewSticker();
			};
			getElementById('line' + i + '_enabled').checked = (i == 1);
			getElementById('line' + i + '_content').onblur = function(){
				this.className = 'textLine';
				//OnPreviewSticker();
			};
			getElementById('line' + i + '_content').onfocus = function(){
				this.className = 'textLine selected';
			};
			getElementById('line' + i + '_content').onkeyup = function(){
				//OnPreviewSticker();
			};
			
			getElementById('line' + i + '_special').onclick = function(){
				OnShowSpecial(this);
			}
		}
		var oRoot = getElementById('special');
	}
	
	var oLinks = oRoot.getElementsByTagName('a');
	for(i=0 ; i<oLinks.length ; i++){
		if(oLinks[i].name == 'sc'){
			oLinks[i].onmouseover =	function(){ this.className = 'selected'; }
			oLinks[i].onmouseout =	function(){ this.className = ''; }
		}
	}
	
	document.getElementById('uploadContainer').name = 'uploadContainer';
	
	OnLineStatus();
	PreviewSticker();
}

function OnCloseHyperbox(){
	document.getElementById('hyperBox').style.display = 'none';
}

var curInsertTarget;

function OnShowSpecial(oFrom){
	var nIndex = oFrom.id.charAt(4);
	curInsertTarget = 'line' + nIndex + '_content';
	if(!document.getElementById('line' + nIndex + '_enabled').checked){ return; }
	document.getElementById('special').style.display = 'block';
	document.getElementById('special').style.top = (curMouseY) + "px";
	document.getElementById('special').style.left = (curMouseX - curOffsetX) + "px";
}

function OnHideSpecial(){
	document.getElementById('special').style.display = 'none';
	document.getElementById(curInsertTarget).focus();
}

function setCursorPos( id, nPos){
	var oInput = document.getElementById(id);
	if(!oInput){
		return;
	}
	oInput.focus();
	if(typeof oInput.selectionStart != "undefined"){
		oInput.setSelectionRange( nPos, nPos);
	}else{
		var str = oInput.createTextRange();
		str.moveStart('character', nPos);
		str.collapse();
		str.select();
	}
	return true;
} 

function insertSpecial(cChar){
	if(curInsertTarget == '' || !curInsertTarget){
		return;
	}
	document.getElementById(curInsertTarget).value += cChar;
	document.getElementById(curInsertTarget).focus();
	setCursorPos(curInsertTarget, document.getElementById(curInsertTarget).value.length);
}

function OnLineStatus(){
	var strFilter = '';
	for(var i=1 ; i<=3 ; i++){
		document.getElementById('line' + i + '_content').disabled = !document.getElementById('line' + i + '_enabled').checked;
		if(document.getElementById('line' + i + '_enabled').checked){
			strFilter = i;
		}
	}
	document.getElementById('icons_tabs').src = 'composer_icon_display.php?filter=' + strFilter;
	OnDeleteLeftIconNoPreview();
	OnDeleteRightIconNoPreview();
}


function OnCloseInfobox(){
	document.getElementById('infoBox').style.display = 'none';
}

function OnMoveInfobox(){
	document.getElementById('infoBox').style.top = (curMouseY + curOffset) + "px";
	document.getElementById('infoBox').style.left = (curMouseX + curOffset - curOffsetX) + "px";	
}

function OnSpecialClick(){
	
}

function PreviewSticker(){
	//clearTimeout(nTimeout);
	var oXmlHttp;
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	//alert('maj');
	with(window.document){
		//getElementById('error_width').style.display = 'none';
		var strData = 'cmd=basics';
		for(var i=1 ; i<=3 ; i++){
			if(getElementById('line' + i + '_enabled').checked){
				strData += '&line' + i + '_enabled=1';
				strData += '&line' + i + '_content=' + getElementById('line' + i + '_content').value;				
			}else{
				strData += '&line' + i + '_enabled=0';
			}
		}
		for(i=0 ; i<fBuilder.font.length ; i++){
			if(fBuilder.font[i].checked){
				strData += '&font=' + fBuilder.font[i].value;
				break;
			}
		}
		for(i=0 ; i<fBuilder.color.length ; i++){
			if(fBuilder.color[i].checked){
				strData += '&color=' + fBuilder.color[i].value;
				break;
			}
		}
		for(i=0 ; i<fBuilder.opacity.length ; i++){
			if(fBuilder.opacity[i].checked){
				strData += '&opacity=' + fBuilder.opacity[i].value;
				break;
			}
		}
	}
	oXmlHttp.setRequestHeader('Content-length', strData.length);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		var oResp = oXmlHttp.responseText.split(',');
		var flag = false;
		for(var i=0 ; i<oResp.length ; i++){
			if(oResp[i] == '1'){
				flag = true;
			}
		}
		
		document.getElementById('error_width').style.display = (!flag)? 'none' : 'block';
		
		//getUniqueId();
		document.getElementById('sticker_outline_img').src = '../scripts/sticker_manager.php?cmd=render&id=' + Math.random();
		//document.getElementById('sticker_outline').innerHTML = '<img src="../scripts/sticker_manager.php?cmd=render&id=' + currentUniqueId + '" border="0"/>';
		nTimeout = setTimeout(function(){PreviewSticker();}, 2000);
	}
	oXmlHttp.send(strData);
}

function OnPreviewSticker(){
	if(nTimeout == -666){
		nTimeout = setTimeout(function(){PreviewSticker();}, 2000);
	}
}


function getUniqueId(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('GET', '../scripts/getuniqueid.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		currentUniqueId = oXmlHttp.responseText;
	}
	oXmlHttp.send(null);
}

function OnDebug(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		alert('debug : ' + oXmlHttp.responseText);
	}
	oXmlHttp.send('cmd=debug');	
}

function OnUnset(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		document.getElementById('sticker_outline').innerHTML = '';
	}
	oXmlHttp.send('cmd=unset');	
}

function OnSetBackground(strFile){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		OnPreviewSticker();
	}
	oXmlHttp.send('cmd=setbackground&background=' + strFile);	
}

function OnSetType(strType){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
	}
	oXmlHttp.send('cmd=settype&type=' + strType);	
}

function OnSetIcon(strFile){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	with(window.document){
		for(i=0 ; i<fBuilder.direction.length ; i++){
			if(fBuilder.direction[i].checked){
				var strCommand = 'set' + fBuilder.direction[i].value + 'icon';
				break;
			}
		}
	}
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		document.location.href = '#outline';
		OnPreviewSticker();
	}
	oXmlHttp.send('cmd=' + strCommand + '&icon=' + strFile);
}

function OnFlipLeftIcon(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		OnPreviewSticker();
	}
	oXmlHttp.send('cmd=fliplefticon');
}

function OnFlipRightIcon(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		OnPreviewSticker();
	}
	oXmlHttp.send('cmd=fliprighticon');
}

function OnSwitchIcons(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		OnPreviewSticker();
	}
	oXmlHttp.send('cmd=switchicons');
}

function OnDeleteLeftIcon(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		OnPreviewSticker();
	}
	oXmlHttp.send('cmd=setlefticon&icon=');
}
function OnDeleteLeftIconNoPreview(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.send('cmd=setlefticon&icon=');
}

function OnDeleteRightIcon(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.onreadystatechange = function(){
		if(oXmlHttp.readyState != 4){return;}
		OnPreviewSticker();
	}
	oXmlHttp.send('cmd=setrighticon&icon=');
}
function OnDeleteRightIconNoPreview(){
	if(!(oXmlHttp = getXmlHttp())){ alert('Erreur.'); return; }
	oXmlHttp.open('POST', '../scripts/sticker_manager.php', true);
	oXmlHttp.setRequestHeader('User-Agent', 'XMLHTTP/1.0');
	oXmlHttp.setRequestHeader('Content-type', 'application/x-www-form-urlencoded');
	oXmlHttp.setRequestHeader('Content-length', 9);
	oXmlHttp.setRequestHeader('Connection', 'Close');
	oXmlHttp.send('cmd=setrighticon&icon=');
}


function OnTabClick(oFrom){
	var oDivs = document.getElementsByTagName('div');
	for(var i=0 ; i<oDivs.length ; i++) {
		if(oDivs[i].className == 'tab selected'){
			oDivs[i].className = 'tab';
		}
	}
	oFrom.className = 'tab selected';
	var nIndex = oFrom.id.substr(3, 4);
	
	for(var i=0 ; i<oDivs.length ; i++) {
		if(oDivs[i].id == 'iconsContainer' + nIndex){
			oDivs[i].style.display = 'block';
		}else if(oDivs[i].id.substr(0,14) == 'iconsContainer'){
			oDivs[i].style.display = 'none';
		}
	}
}


function OnPrintModel(nModel){
	for(var i=1 ; i<=3 ; i++){
		document.getElementById('print_model' + i).style.display = (i == nModel)? 'block' : 'none';
	}
	window.print();
}

var isFilter1Loaded = false;
var isFilter2Loaded = false;
var isFilter3Loaded = false;

function OnShowBuilder(){
	document.getElementById('builder_loading').style.display	= 'none';
	document.getElementById('builder_engine').style.display		= 'block';
}