/* General */

/**
 * External link direction
 */
window.addEvent('domready', function() {
	$$('a[rel=external]').each(function(link) {
		link.addEvent('click', function(event){
			window.open(this.href);
			return false;
		});
	});	
});

// 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');
		});
	}
};


/* a */
window.addEvent('domready',function(){

	// blank
	$$('a[rel=blank]').each(function(el){
		el.setProperty('target','_blank');
	});

	// sbox
	sbox();

});

/* sbox */
function sbox(){
	$$('a[rel=sbox]').each(function(el){
		el.setProperty('href',el.getProperty('href')+'&template=iframe');
	});
	SqueezeBox.assign($$('a[rel=sbox]'),{
		handler:'iframe',
		size:{
			x:640,
			y:360
		}
	});
}

/*	// bg
	$('bg-right').setStyle('left',$('ad2').getPosition().x+'px');

	// foot
	$('foot').setStyle('width',$('ad2').getPosition().x+'px'); */


/* Poll handling */

// old stuff, we need to get rid of these

//AJAX

var bustcachevar=0; //bust potential caching of external pages after initial request? (1=yes, 0=no)
var loadstatustext_cool="<p style=\"font-family:verdana,arial,sans-serf;color:gray;margin-left:auto;margin-right:auto;padding:4px;\"><img src='http://images.coronaria.fi/?id=4188303' /> ladataan...</p>"
var loadstatustext="<br /><br /><p style=\"display:block;font-family:verdana,arial,sans-serf;color:#DDDDDD;text-align:center;\">lataa...</p>";

var siteUrl = 'http://'+window.location.host+'/';


////NO NEED TO EDIT BELOW////////////////////////
var loadedobjects=""
var defaultcontentarray=new Object()
var bustcacheparameter=""

function ajaxinit () {
	if (window.XMLHttpRequest) // if Mozilla, Safari etc
		return (new XMLHttpRequest());
	else if (window.ActiveXObject){ // if IE
		try {
			return ( new ActiveXObject("Msxml2.XMLHTTP"));
		} 
		catch (e){
			try{
				return ( new ActiveXObject("Microsoft.XMLHTTP"));
			}
			catch (e){}
		}
	}
	else {
		return false;
	}
}

function ajaxpage(url, containerid, targetobj){
	var page_request = ajaxinit();
	if (!page_request) {
		return false;
	}
	//var ullist=targetobj.parentNode.parentNode.getElementsByClass(document, 'tab', 'div')
	var ullist=targetobj.parentNode.parentNode.getElementsByTagName("li");

	for (var i=0; i<ullist.length; i++) {
		if (ullist[i].className == 'righttab_unactive' || ullist[i].className == 'righttab_active')
			ullist[i].className="righttab_unactive";  //deselect all tabs
		if (ullist[i].className == 'lefttab_unactive' || ullist[i].className == 'lefttab_active' )
			ullist[i].className="lefttab_unactive";
	}

	if (targetobj.parentNode.className == "righttab_unactive")
		targetobj.parentNode.className="righttab_active";  //highlight currently clicked on tab
	if (targetobj.parentNode.className == "lefttab_unactive")
		targetobj.parentNode.className="lefttab_active";  //highlight currently clicked on tab
		
	if (url.indexOf("#default")!=-1){ //if simply show default content within container (verus fetch it via ajax)
		document.getElementById(containerid).innerHTML=defaultcontentarray[containerid];
		return;
	}
	document.getElementById(containerid).innerHTML=loadstatustext;
	//fadeIn(containerid,0,5);
	page_request.onreadystatechange=function(){
		loadpage(page_request, containerid, 1);
	}
	if (bustcachevar) //if bust caching of external page
		bustcacheparameter=(url.indexOf("?")!=-1)? "&"+new Date().getTime() : "?"+new Date().getTime();
	page_request.open('GET', url+bustcacheparameter, true);
	page_request.send(null);
}

function loadpage(page_request, containerid, fade){
	if (page_request.readyState == 4 && (page_request.status==200 || window.location.href.indexOf("http")==-1)) {
		//document.getElementById(containerid).style.visibility = 'hidden';
		document.getElementById(containerid).innerHTML=page_request.responseText;
		if (fade == 1) {
			setOpacity(document.getElementById(containerid), 0);
			//document.getElementById(containerid).style.visibility = 'visible';
			fadeIn(containerid,0,10);
		} else {
			document.getElementById(containerid).style.visibility = 'visible';
		}
	}
}

// send poll aswer
function sendPollAnswer(form) {
	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	form = document.getElementById('modpollform');
	quickpoll = form.quickpoll;
	questionid = form.questionid.value;
	
	page_request.onreadystatechange=function(){
		loadpage(page_request, 'ModPoll', 0);
	}

	//
	//alert(form);
	len = quickpoll.length;
	//quickpollValue='';
	for (i = 0; i < len; i++) {
		
		if (quickpoll[i].checked) {
				quickpollValue = quickpoll[i].value;
		}
    }
    
    if (quickpollValue == undefined) {
    	return false;
    }
  
	document.getElementById('ModPoll').innerHTML=loadstatustext_cool;
	
	parameters = 'quickpoll='+quickpollValue+'&questionid='+questionid;
	//alert(parameters);
	page_request.open('POST', siteUrl+'.?module=ModPoll', true);
	page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	page_request.setRequestHeader("Content-length", parameters.length);
	page_request.setRequestHeader("Connection", "close");
	page_request.send(parameters);
	
	if (ajaxinit()) {
		return false;
	} else {
		return true;
	}
			
}


// send article poll aswer
function sendArticlePollAnswer(form) {
	var page_request = ajaxinit();
	if (!page_request)
		return false;	
	form = document.getElementById('modarticlepollform');
	quickpoll = form.quickpoll;
	articlequestionid = form.articlequestionid.value;
	articlepoll = form.articlepoll.value;
	articlepollid = form.articlepollid.value;
	
	page_request.onreadystatechange=function(){
		loadpage(page_request, 'articlePollContent', 0);
	}

	//
	//alert(form);
	len = quickpoll.length;
	//quickpollValue='';
	for (i = 0; i < len; i++) {
		
		if (quickpoll[i].checked) {
				quickpollValue = quickpoll[i].value;
		}
    }
    
    if (quickpollValue == undefined) {
    	return false;
    }
  
	document.getElementById('articlePollContent').innerHTML=loadstatustext_cool;
	
	parameters = 'quickpoll='+quickpollValue+'&articlequestionid='+articlequestionid+'&articlepoll='+articlepoll+'&articlepollid='+articlepollid;
	//alert(parameters);
	page_request.open('POST', siteUrl+'.?module=ModPoll', true);
	page_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
	page_request.setRequestHeader("Content-length", parameters.length);
	page_request.setRequestHeader("Connection", "close");
	page_request.send(parameters);
	
	if (ajaxinit()) {
		return false;
	} else {
		return true;
	}
			
}


/**
 * Suggests URL in societies form, using value of field 'urlAlias'. May be useful in some other place too
 * @author TH
 * @return boolean
 */
function suggestUri (el) {
	tmpval = el.value;
	tmpval = tmpval.toLowerCase();
	tmpval = tmpval.replace(/ä/g,'a');
	tmpval = tmpval.replace(/å/g,'a');
	tmpval = tmpval.replace(/ö/g,'o');

	tmpval = tmpval.replace(/�/g,'a');
	tmpval = tmpval.replace(/�/g,'a');
	tmpval = tmpval.replace(/�/g,'o');
	
	tmpval = tmpval.replace(/ /g,'-');
	tmpval = tmpval.replace(/\(/g,'');
	tmpval = tmpval.replace(/\)/g,'');
	document.getElementById('urlAlias').value = tmpval;
}
 
 
 /**
  * Suggests URL in societies form, using value of field 'urlAlias'. May be useful in some other place too
  * @author TH
  * @return string
  */
 function cleanUri (txt) {
 	tmpval = txt;
 	tmpval = tmpval.toLowerCase();
	tmpval = tmpval.replace(/�/g,'a');
	tmpval = tmpval.replace(/�/g,'a');
	tmpval = tmpval.replace(/�/g,'o');
 	tmpval = tmpval.replace(/ /g,'-');
 	tmpval = tmpval.replace(/\(/g,'');
 	tmpval = tmpval.replace(/\)/g,'');
 	return tmpval;
 } 
 
  
/**
 * 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');
		
	}
};
	