/*
	JQuery News - etbvanmeggelen.nl
	Yuen Li
	22-5-2009
	VPRI Internetdiensten - www.vpri.nl
*/

var $vm = jQuery.noConflict();
var news = new Array();
var secs = 8000;
var imgrot = 6000;
var timer = null;
var tmr_topimg = null;
var ckNews = getCookie('vm_newsstatus');
var ckBack = getCookie('vm_backstatus');

$vm(document).ready(function(){
	/* img base check */
	if ( typeof(BaseURL) == 'undefined' ) {
		BaseURL = 'img/';
	}
	/* xhtml strict external link replacement: optional */
	$vm('a[rel*=external]').click( function() {
		window.open(this.href);
		return false;
	});
	/* Search String */
	$vm('#srcwords').blur(function(){
		if ( $vm(this).val() == '' ) $vm(this).val( srcstr );
	}).focus(function(){
		if ( $vm(this).val() == srcstr ) $vm(this).val( '' );
	});
	/* footer img hover */
	$vm("#xhtml10").hover(
		function () {
			$vm(this).attr("src",BaseURL+"valid-xhtml10.png");
		}, 
		function () {
			$vm(this).attr("src",BaseURL+"valid-xhtml10_alpha.png");
		}
	);
	$vm("#vcss").hover(
		function () {
			$vm(this).attr("src",BaseURL+"vcss.png");
		}, 
		function () {
			$vm(this).attr("src",BaseURL+"vcss_alpha.png");
		}
	);
	/* custom news module */
	if ( $vm('#news').css("display") !== 'none' ) {
		$vm('div[id*=vm_news]').each(function(){
			news[news.length] = $vm(this).attr("id");
			if ( !$vm(this).hasClass('hideme') ) {
				current = news.length - 1;
			}
		});
		// no need to rotate only 1 news item
		if ( news.length > 1 ) {
			if ( ckNews == 'pause' ) {
				$vm('#btn_pause').css("display", "none");
				$vm('#btn_start').show();
			} else {
				timer = setTimeout('fadeEngine('+current+')',secs);
			}
		} else {
			$vm('#btn_pause').css("display", "none");
		}
	}
	/* Top Imgs */
	if ($vm('#vm_topimg').css("display") !== 'none') {
		if ( ckBack == 'pause' ) {
			$vm('#back_pause').css("display", "none");
			$vm('#back_start').show();
		} else {
			tmr_topimg = setTimeout('changeBack()', imgrot);
		}
	}
	/* button functions */
	$vm('#btn_pause').click(function(){
		setCookie('vm_newsstatus','pause');
		clearTimeout(timer);
		$vm(this).css("display", "none");
		$vm('#btn_start').show();
	});
	$vm('#btn_start').click(function(){
		setCookie('vm_newsstatus','start');
		timer = setTimeout('fadeEngine('+current+')',secs);
		$vm(this).css("display", "none");
		$vm('#btn_pause').show();
	});
	$vm('#back_pause').click(function(){
		setCookie('vm_backstatus','pause');
		clearTimeout(tmr_topimg);
		$vm(this).css("display", "none");
		$vm('#back_start').show();
	});
	$vm('#back_start').click(function(){
		setCookie('vm_backstatus','start');
		tmr_topimg = setTimeout('changeBack()', imgrot);
		$vm(this).css("display", "none");
		$vm('#back_pause').show();
	});
});
function getCookie(c_name){
	if (document.cookie.length > 0 ){
		c_start=document.cookie.indexOf(c_name + "=");
		if (c_start!=-1){
			c_start=c_start + c_name.length+1;
			c_end=document.cookie.indexOf(";",c_start);
			if (c_end==-1) c_end=document.cookie.length;
			return unescape(document.cookie.substring(c_start,c_end));
		}
	}
	return "";
}
function setCookie(name,val) {
	var exdate = new Date();
	exdate.setDate(exdate.getDate()+7);
	document.cookie=name+ "=" +escape(val)+";expires="+exdate.toGMTString();
}
function changeBack() {
	if ( typeof(topimgs) == 'undefined' ) return false;
	if ( topimgs.length == 0 ) return false;
	if ( currimgkey == (topimgs.length-1) ) {
		currimgkey = 0;
	} else {
		currimgkey++;
	}
	$vm('#vm_topimg').fadeTo(1200, 0, function(){
		$vm(this).css({backgroundImage:"url("+TopURL+topimgs[currimgkey]+")" }).fadeTo(1200, 1);
	});
	tmr_topimg = setTimeout('changeBack()', imgrot);
}
function fadeEngine( key ) {
	var total = news.length - 1;
	var new_key = key;
	if ( key == total ) {
		new_key = 0; 
	} else { 
		new_key++;
	}
	current = new_key;
	$vm("#"+news[key]).fadeOut(1200, function(){
		$vm("#"+news[new_key]).fadeIn(1200);
	});
	timer = setTimeout('fadeEngine('+new_key+')',secs);
}



