var BDocument = new bannerDOM();

function showBorders() {
	BDocument.showBorders();
}

function hideBorders() {
	BDocument.hideBorders();
}

function showColors() {
	BDocument.showColors();
}

function hideColors() {
	BDocument.hideColors();
}

function objFocus(id) {
	BDocument.focus(id);
}

function objectURL(bannerOBJ) {
	return '/banner-maker/dumpResource.php?obj='+bannerOBJ.toURL();
}

var BackRequest = 1;

function updateBackground() {
	var bk = BDocument.getElementById('background');
	if (bk !== 0) {
		document.getElementById('banner').style.background = 'url('+objectURL(bk)+')';
	} else {
		alert('Error Updating Background: Background is not present in BDocument.');
	}
}

var lastw = '';
var lasth = '';

function backTimer() {
	bnr = document.getElementById('banner');
	//bnr.style.border=bnr.style.border !== ''?'':'1px solid black';
	if (!bnr) {
		alert('Banner DIV not present!');
		return 0;
	}
	if ((bnr.style.width != lastw) || (bnr.style.height != lasth)) {
		lastw = bnr.style.width;
		lasth = bnr.style.height;
		var backg = BDocument.getElementById('background');
		
		if (backg == 0) {
			alert('background not present in DOM!');
			return 0;
		}
		
		if (BackRequest == 1) {
		    BackRequest = 0;
		    updateBackground();
		}
		
		backg.addProperty('width' , parseInt(lastw));
		backg.addProperty('height', parseInt(lasth));
		
		//updateBackground();
	}
	setTimeout('backTimer();', 500);
}

function urlDecode(astr) {
    var bstr = astr;
    bstr = bstr.replace('&amp;', '&');
    bstr = bstr.replace('&quot;', '"');
    bstr = bstr.replace('&lt;', '<');
    bstr = bstr.replace('&gt;', '>');
    return bstr;
}

function htmlEntities(astr) {
	var bstr = astr;
	bstr = bstr.replace('&', '&amp;');
	bstr = bstr.replace('"', '&quot;');
	bstr = bstr.replace('<', '&lt;');
	bstr = bstr.replace('>', '&gt;');
	return bstr;
}

function onReceiveControl(msg) {
	if (msg == '') {
		alert('Server returned a error request');
		return 0;
	}
	var m = unescape(msg);
	var split = m.split('[<html>]');
	if (split.length != 3) {
		alert('Server returned a bad response');
		return 0;
	}
	document.getElementById(split[0]).innerHTML = split[2];
	document.getElementById(split[0]).style.display = '';
	initTextFormSliders(split[1]);
}

function initTextFormSliders(ID) {
	form_widget_amount_slider('slider_opacity'+ID,
						 document.getElementById('opacity'+ID),
						 '150',0,100, 'document.getElementById(\'opacity__' +ID + '\').innerHTML='+
						              'document.getElementById(\'opacity' +  ID + '\').value;');
	form_widget_amount_slider('slider_rotation'+ID,
						 document.getElementById('rotation'+ ID),
						 '150',0,72, 'document.getElementById(\'rotation__' +ID + '\').innerHTML='+
						              '(document.getElementById(\'rotation' +  ID + '\').value*5)+\"&deg;\";');
	form_widget_amount_slider('slider_distance'+ID,
						 document.getElementById('shadowdistance'+ID),
						 '150',0,10, 'document.getElementById(\'shadowdistance__' +ID + '\').innerHTML='+
						              'document.getElementById(\'shadowdistance' 
						              +  ID + '\').value;');
	form_widget_amount_slider('slider_shadowopacity'+ID,
						 document.getElementById('shadowopacity'+ID),
						 '150',0,100, 'document.getElementById(\'shadowopacity__' +ID + '\').innerHTML='+
						              'document.getElementById(\'shadowopacity' 
						              + ID + '\').value;');
}


function getModificationsForTextObject(obj) {
	var id = obj.id;
	obj.addProperty('font', 			document.getElementById('font'+id).value	);
	obj.addProperty('size', 			document.getElementById('size'+id).value	);
	obj.addProperty('text', 			document.getElementById('text'+id).value	);
	obj.addProperty('color', 		document.getElementById('color'+id).value	);
	obj.addProperty('opacity', 		document.getElementById('opacity'+id).value	);
	obj.addProperty('rotation',		document.getElementById('rotation'+id).value	*5);
	obj.addProperty('shadowcolor', 	document.getElementById('shadowcolor'+id).value	);
	obj.addProperty('shadowpos', 		document.getElementById('shadowpos'+id).value	);
	obj.addProperty('shadowdistance',	document.getElementById('shadowdistance'+id).value);
	obj.addProperty('shadowopacity',	document.getElementById('shadowopacity'+id).value	);
	obj.addProperty('outline', 	 	document.getElementById('outline'+id).value		);
	obj.addProperty('outlinecolor',	document.getElementById('outlinecolor'+id).value	);
	//alert('ready');
	return obj;
}

function getModificationsForBackgroundObject(obj) {
	var id=obj.id;
	obj.addProperty('color1',		document.getElementById('bgcolor1').value	);
	obj.addProperty('color2',		document.getElementById('bgcolor2').value	);
	obj.addProperty('fillmethod',		document.getElementById('fillmethod').value);
	obj.addProperty('backgroundborder',document.getElementById('backgroundborder').value);
	obj.addProperty('bordercolor',     document.getElementById('bordercolor').value);
	return obj;
}

function getModificationsForImageObject(obj) {
	var id = obj.id;
	obj.addProperty('online', 		document.getElementById('imgurl'+id).value	);
	obj.addProperty('local', 		document.getElementById('imglocal'+id).value	);
	obj.addProperty('name', 			document.getElementById('imgname'+id).value	);
	return obj;
}


function showHide(divid) {
	var tdiv = document.getElementById(divid);
	tdiv.style.display = tdiv.style.display=='none'?'':'none';
}

function createTextElement(ID) {
	var myObj = new bannerObject;
	myObj.id 	= ID;
	myObj.type='text';
	myObj = getModificationsForTextObject(myObj);
	BDocument.insert(myObj);
	document.getElementById('data').value = BDocument.toBuffer();
	document.getElementById('dataForm').submit();
}

function createImageElement(ID) {
	var myObj = new bannerObject;
	myObj.id 	= ID;
	myObj.type='image';
	myObj = getModificationsForImageObject(myObj);
	
	if (myObj.getProperty('name') == '') {
		alert('Error adding image: No image source specified. Please click on a source and follow instructions.');
		return 0;
	}
	
	BDocument.insert(myObj);
	document.getElementById('data').value = BDocument.toBuffer();
	document.getElementById('dataForm').submit();
}

function cancelCreateElement(divID) {
	document.getElementById(divID).style.display = 'none';
}

function toggleTextOutline(id) {
	var row1 = document.getElementById('grp1Outline'+id);
	var row2 = document.getElementById('grp2Outline'+id);
	row1.style.display = row1.style.display == 'none'?'':'none';
	row2.style.display = row2.style.display == 'none'?'':'none';
}

function toggleShadowSettings(id) {
	var row1 = document.getElementById('grp1Shadow'+id);
	var row2 = document.getElementById('grp2Shadow'+id);
	row1.style.display = row1.style.display == 'none'?'':'none';
	row2.style.display = row2.style.display == 'none'?'':'none';
}

function DisplayFontPreview(id, selectobj) {
	var theimg  = document.getElementById(id);
	var thefont = selectobj.value;
	theimg.src  = '/banner-maker/fontImage.php?font='+thefont;
}

function defaultTextControlSet(bannerObject, placeInDiv) {
	ajax_mem_load('/banner-maker/dumpControlsForText.php?div='+placeInDiv+'&data='+bannerObject.toURL(), 'onReceiveControl');
}

function defaultImageControlSet(bannerObject, placeInDiv) {
	ajax_mem_load('/banner-maker/dumpControlsForImage.php?div='+placeInDiv+'&data='+bannerObject.toURL(), 'onReceiveControlIMG');
}

function randomID(prefix) {
	var d = new Date();
	return prefix+'_'+d.getTime();
}

function showOutlineColor(id, dropdown) {
	if (dropdown.value.toString() === '0') {
		document.getElementById('v_'+id).style.display='none';
		document.getElementById(id).style.display='none';
	} else {
		document.getElementById('v_'+id).style.display='';
		document.getElementById(id).style.display='';
	}
}

function addText() {
	var myDom = new bannerObject();
	myDom.id = randomID('text');
	myDom.type = 'text';
	myDom.addProperty('font', 'arial.ttf');
	myDom.addProperty('size', '20');
	myDom.addProperty('text', 'Welcome to templatix.org banner maker');
	myDom.addProperty('color', '#000000');
	myDom.addProperty('opacity', '0');
	myDom.addProperty('rotation', '0');
	myDom.addProperty('shadow', '0');
	myDom.addProperty('shadowcolor','#DDFFDD');
	myDom.addProperty('shadowpos','tl');
	myDom.addProperty('shadowdistance', '0');
	myDom.addProperty('shadowopacity', '0');
	myDom.addProperty('outline','0');
	myDom.addProperty('outlinecolor', '#FFFFFF');
	defaultTextControlSet(myDom, 'addnew');
}

function addImage() {
	var myDom = new bannerObject();
	myDom.id = randomID('image');
	myDom.type = 'image';
	myDom.addProperty('online', '');
	myDom.addProperty('local',  '');
	myDom.addProperty('name',   '');
	defaultImageControlSet(myDom, 'addnew');
}

function onReceiveControlIMG(msg) {
	if (msg == '') {
		alert('Server returned a error request\n'+unescape(msg));
		return 0;
	}
	var m = unescape(msg);
	var split = m.split('[<html>]');
	if (split.length != 3) {
		alert('Server returned a bad response\n'+unescape(msg));
		return 0;
	}
	document.getElementById(split[0]).innerHTML = split[2];
	document.getElementById(split[0]).style.display = '';
}

function removeElement(elementID) {
	if (!confirm('Are you sure you want to remove this element?')) {
		return 0;
	}
	BDocument.remove(elementID);
	updateBanner();
}

function resetElement(elementID) {
	var doc = BDocument.getElementById(elementID);
	doc.addProperty('x', '0');
	doc.addProperty('y', '0');
	updateBanner();
}

function updateBanner() {
	var i=0;
	for (i=0; i<BDocument.count(); i++) {
		dm = BDocument.objects[i];
		
		if (dm.type == 'text') {
			dm = getModificationsForTextObject(dm);
		} else 
		if (dm.type == 'image') {
			//alert('loadimage');
			dm = getModificationsForImageObject(dm);
		} else
		if (dm.type == 'background') {
			dm = getModificationsForBackgroundObject(dm);
		}
	}
	document.getElementById('data').value = BDocument.toBuffer();
	document.getElementById('dataForm').submit();
}

function IMG_UPDATE_NAME(id, url, local) {
	document.getElementById('imgname'+id).value = id;
	document.getElementById('imgurl'+id).value = url;
	document.getElementById('imglocal'+id).value = local;
}

function openWindow(url, name, width, height)
{
    var str = "height=" + height + ",innerHeight=" + height;
    str += ",width=" + width + ",innerWidth=" + width;
    if (window.screen)
    {
        var ah = screen.availHeight - 30;
        var aw = screen.availWidth - 10;

        var xc = (aw - width) / 2;
        var yc = (ah - height) / 2;

        str = str + ",left=" + xc + ",screenX=" + xc;
        str = str + ",top=" + yc + ",screenY=" + yc;
    }
    str = str + ",scrollbars=yes,toolbar=no,location=no,directories=no,status=no,menubar=no, resizable=no";

    var thewindow = window.open(url, name, str);
    
    thewindow.focus();
}

function downloadImageForObject(objid) {
	openWindow('/banner-maker/downloadImage.php?id='+objid,'dlimage', 300, 150);
}

function uploadImageForObject(objid) {
	openWindow('/banner-maker/uploadImage.php?id='+objid,'uplimage', 300, 150);
}

function downloadBanner() {
    document.getElementById('dldbanner_frm').submit();
}