//DOM ready block starts
window.addEvent('domready', function() {
	
	/**
	 * External link direction
	 */
	$$('a[rel=external]').each(function(link) {
		link.addEvent('click', function(event){
			window.open(this.href);
			return false;
		});
	});
	
	/**
	 * Inputs with disappearing default values
	 */
	$$('input.defaultvalue').each(function(el) {
		el.addEvent('focus', function(event){
			this.set('value','');
			this.setStyle('color','#000');
			this.store('isChanged',true);
			this.removeEvents('focus');
		});
	});
	
	/**
	 * Disappearing values must be ignored if they aren't changed
	 */
	$$('form.defaultvalued').each(function(el) {
		el.addEvent('submit', function(event){
			els = this.getElements('input.defaultvalue');
			for (i=0; i < els.length; i++) {
				if ( els[i].retrieve('isChanged')!=true) {
					els[i].set('value','');
				}
			}
		});
	});
	
	// default squeezeBox
	var boxWidth = document.documentElement.clientWidth*0.8;
	var boxHeight = document.documentElement.clientHeight-60;

	if (boxWidth < 800) {
		boxWidth = document.documentElement.clientWidth*0.95;
	}

	SqueezeBox.assign($$('a[rel=admin]'), {
		handler:'iframe',
		size:{
			x:boxWidth,
			y:boxHeight
		},
		onClose: function() {
			window.location.reload();
		}
	});
});
//DOM ready block ends

// resize
function resize(method) {
	if(method=="larger") {
		$$('#cppfArticleContent').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent a').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent h1').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent h2').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent h3').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent h4').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
		$$('#cppfArticleContent p').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()+1)+'px');
		});
	}
	if(method=="smaller") {
		$$('#cppfArticleContent').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent a').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent h1').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent h2').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent h3').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent h4').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
		$$('#cppfArticleContent p').each(function(el) {
			el.setStyle('font-size',(el.getStyle('font-size').toInt()-1)+'px');
		});
	}
};

/**
 * Alert-like easy message box with SqueezeBox
 * @author TH
 * @extends SqueezeBox
 */ 
SqueezeBox.msg = 	 
	function(responseText) {
		 this.initialize({
			handler:'string',
		size:{
			x:300,
			y:75
		}}
	);
	this.setContent('string', '<div style=\"margin-top:22px;text-align:center\"><strong>'+responseText)+'</strong></div>';
	this.showContent();
};

/**
 * instance LoadBox
 * Lays loading msg box over given element
 * @author TH
 */
var LoadBox = {
	presets: {
		opacity: 0.4,
		loadingText: 'ladataan'
	},
	render: function(el, conf) {
		conf = (conf != undefined && typeof(conf)=='object')?$merge(this.presets,conf):this.presets;
		if (Browser.Engine.trident) {
			elb = el;
			for (var posX = 0, posY = 0; el; el = el.offsetParent ) {
			      posX += el.offsetLeft;
			      posY += el.offsetTop;
			}
			el = elb;
			posY = posY + (el.getSize().y/2-13);
			posX = posX + (el.getSize().x/2-39);
			//el.set('html','<div style="margin:8px;"><img src=\"http://images.coronaria.fi/?id=4188303\" alt=\"\" /> '+conf.loadingText+'</div>');
		} else {
			coords = el.getCoordinates(document.body);
			posY = coords.top + (coords.height/2-13);
			posX = coords.left + (coords.width/2-39);
		}
		//If argument conf is defined, we merge it with presets. If not, let's revert to presets
		el.getChildren().set('opacity',conf.opacity);
		
		box = new Element('div', {
			'class': 'load-box',
			'styles':{
				'top': posY,
				'left': posX 
			},
			'html':'<img src=\"http://images.coronaria.fi/?id=4188303\" alt=\"\" /> '+conf.loadingText
		});
		box.inject(el,'top');
		
	}
};
			
function selectImage(name) {				
	while( window.opener.document.getElementById('avatarImg').hasChildNodes() ) { 
		window.opener.document.getElementById('avatarImg').removeChild( window.opener.document.getElementById('avatarImg').firstChild ); 
	}
	
	img = document.createElement('img');
	var now = new Date();
	img.src = 'http://images.coronaria.fi/tmp/'+name+'?'+now.getTime();
	window.opener.document.getElementById('avatarImg').appendChild(img);
	window.opener.document.getElementById('avatarImg').appendChild(field);
	window.opener.document.getElementById('avatarLink').innerHTML = 'Vaihda avatar';
	
	//window.opener.reload();
	window.close();
}

function deleteImage() {
	if (confirm("Haluatko poistaa kuvan? Huomaa, että poisto tulee voimaan tallennuksen yhteydessä.")) {
		document.getElementById('image').value = '';
		document.getElementById('deletedImage').value = '1';
		document.getElementById('selectedImageHidden').value = 'http://images.coronaria.fi/4881014.png';
		document.getElementById('selectedImage').src = 'http://images.coronaria.fi/4881014.png';
	} else {
		return false;
	}
}
