/* numbers only */
function isNumberKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
	return true;
}

/* numbers and dots only */
function isFloatKey(evt) {
	var charCode = (evt.which) ? evt.which : event.keyCode
	if (charCode == 46) return true; //exception voor punt
	if (charCode > 31 && (charCode < 48 || charCode > 57)) return false;
	return true;
}

/* clear textfield */
function clearfield(el, defaultvalue) {
	var item = document.getElementById(el);
	if(item.value == defaultvalue) item.value = '';
	item.focus();
}

/* versnelde innerHTML funcie */
function replaceHtml(el, html) {
	var oldEl = (typeof el === "string" ? document.getElementById(el) : el);
	var newEl = document.createElement(oldEl.nodeName);
	newEl.id = oldEl.id;
	newEl.className = oldEl.className;
	newEl.innerHTML = html;
	oldEl.parentNode.replaceChild(newEl, oldEl);
	return newEl;
};

/* resize to fit */
function resizeImgOO(el, widthcontainer, heightcontainer, parent) { 
	function imgRatio() { 
		return (el.height / el.width); 
	} 
	function holderRatio() { 
		return (heightcontainer / widthcontainerh); 
	} 
	function fitToContainer() { 
		if(imgRatio>holderRatio)  { 
			if(el.height > heightcontainer) {
				el.height = heightcontainer; 
			}
			if(el.width > widthcontainer) {
				el.width = el.width*(widthcontainer/el.width);
				el.height = el.height*(widthcontainer/el.width);
			}
		} 
		else  { 
			if(el.height > widthcontainer) {
				el.width = widthcontainer;	
			}
			if(el.height > heightcontainer) {
				el.width = el.width*(heightcontainer/el.width);
				el.height = el.height*(heightcontainer/el.width);
			}
		} 
		parent.width = el.width;
	}
	this.imgRatio = imgRatio; 
	this.holderRatio = holderRatio; 
	this.resize = fitToContainer; 
}
					
var active_object;
var selected_object;
var active_rear = 1;
var intval;
var timer = 5000;
function open_media(object) {	
	var dlink = $(object).closest('.mediaitem').data("link");
	var dwidth = $(object).closest('.mediaitem').data("width") || "";
	var dheight = $(object).closest('.mediaitem').data("height") || "";
	
	if(dlink) {
		Shadowbox.open({
			player:		"iframe",
			content:	dlink,
			title:		"",
			width:		dwidth,
			height:		dheight
		});
	}
}
function manualscroll_media(col,direction) {
	var column;
	var stepAnimate = 100;
	var animate = false;
	if(col == 1) column = $('#mediagridnew').find('.mediacontainer:first-child');
	if(col == 2) column = $('#mediagridnew').find('.mediacontainer:last-child');
	
	if(direction == 1 && ($(column).position().top*-1) > 0) {
		animate = true;
		if(($(column).position().top*-1) < stepAnimate) stepAnimate = ($(column).position().top*-1);
	}
	else if(direction == -1 && ($(column).position().top*-1) <= ($(column).height()-300 ) ) animate = true;
	
	if(animate) {
		scrollock = true;
		$(column).animate({
			top: ($(column).position().top+(stepAnimate*direction))
		}, {duration: 50, queue: false, complete: function() { able_manualscroll_media(); } } );
		
		clearInterval(intval); intval = setInterval('slide_media()', timer);
	}
}
function able_manualscroll_media() {
	if($('#mediagridnew').find('.mediacontainer').length > 1) {
		if(($('.mediacontainer:first-child').position().top*-1) <= 0) $('.mediascroller').find('div:nth-child(1)').fadeOut(50);
		else $('.mediascroller').find('div:nth-child(1)').fadeIn(50);
		if(($('.mediacontainer:last-child').position().top*-1) <= 0) $('.mediascroller').find('div:nth-child(2)').fadeOut(50);
		else $('.mediascroller').find('div:nth-child(2)').fadeIn(50);
		if(($('.mediacontainer:first-child').position().top*-1) >= ($('.mediacontainer:first-child').height()-300 )) $('.mediascroller').find('div:nth-child(3)').fadeOut(50);
		else $('.mediascroller').find('div:nth-child(3)').fadeIn(50);
		if(($('.mediacontainer:last-child').position().top*-1) >= ($('.mediacontainer:last-child').height()-300 )) $('.mediascroller').find('div:nth-child(4)').fadeOut(50);
		else $('.mediascroller').find('div:nth-child(4)').fadeIn(50);
	}
}
function scroll_media(object) {
	if($(object).position().top > 150) offsetAnimate = 140;
	else offsetAnimate = $(object).position().top;
	
	$(object).closest('.mediacontainer').animate({
		top: (-$(object).position().top+offsetAnimate)
	}, {duration: 500, queue: false, complete: function() { able_manualscroll_media(); } });
}
function select_media(object) {
	clearInterval(intval); intval = setInterval('slide_media()', (timer*3));
	activate_media(object);
}
function activate_media(object) {
	selected_object = $(object);
	if(!$(object).hasClass('active')) {
		
		$('.mediaitem.active').addClass('inactive');
		$('.mediaitem.active').removeClass('active');
		darken_media($('.mediaitem.inactive'));
		$('.mediaitem.inactive').find('.extension').fadeTo(600, 0, function() { $(this).remove(); } );
		$('.mediaitem.inactive').removeClass('inactive');
		
		highlight_media(object);
		$(object).addClass('active');
		$(object).append('<div class="extension" '+$(object).find('#getResp').html()+' onclick="open_media(this);"><table><tr><td>'+$(object).data("klik")+'</td></tr></table></div>');
		$(object).find('.extension').fadeTo(0,0);
		$(object).find('.extension').fadeTo(200, 0.8);
		scroll_media(object);
		update_background(object);
		getFeed($(object).data('mediaID'));
	}
}
function update_background(object) {
	$('<img/>').attr('src', 'content/mediagrid_backgrounds/'+$(object).data('mediaImageLink')+'.jpg').ready(function() {
		$('#mediagridnew').find('.mediarear div:nth-child('+active_rear+')').fadeTo(600, 0);
		$('#mediagridnew').find('.mediarear div:nth-child('+active_rear+')').css({"z-index":"1"});
		if(active_rear == 1) active_rear = 2;
		else active_rear = 1;
		$('#mediagridnew').find('.mediarear div:nth-child('+active_rear+')').css({'z-index':'2','background-image':'url(content/mediagrid_backgrounds/'+$(object).data('mediaImageLink')+'.jpg)'});
		$('#mediagridnew').find('.mediarear div:nth-child('+active_rear+')').fadeTo(600, 1);
	});
}
function highlight_media(object) {
	active_object = $(object);
	$('.mediaoverlay', object).stop(true, true).fadeTo(200, 0);
}
function darken_media(object) {
	if(!$(object).hasClass('active')) $('.mediaoverlay', object).stop(true, true).fadeTo(600, 0.7);
}
function slide_media() {
	clearInterval(intval); intval = setInterval('slide_media()', (timer));
	var object;
	var objectIndex;
	var column;
	if(selected_object) object = selected_object;
	else if(active_object) object = active_object;
	if(object) {
		objectIndex = $(object).closest('.mediacontainer').children('div').index(object);
		column = $(object).closest('.mediacontainer');
		if($('#mediagridnew').find('.mediacontainer').length > 1) {
			if($(column)[0] === $('#mediagridnew').find('.mediacontainer:first-child')[0]) {
				column = $('#mediagridnew').find('.mediacontainer:last-child');
				if(objectIndex > $(column).find('.mediaitem').length-1) {
					column = $('#mediagridnew').find('.mediacontainer:first-child');
					if(objectIndex >= $(column).find('.mediaitem').length-1) objectIndex = 0; 
				}
			}
			else {
				column = $('#mediagridnew').find('.mediacontainer:first-child');
				objectIndex++;
				if(objectIndex > $(column).find('.mediaitem').length-1) {
					column = $('#mediagridnew').find('.mediacontainer:last-child');
					if(objectIndex >= $(column).find('.mediaitem').length-1) {
						objectIndex = 0;
						column = $('#mediagridnew').find('.mediacontainer:first-child');
					}
				}
			}
		}
		else {
			objectIndex++;
			if(objectIndex > $(column).find('.mediaitem').length-1) objectIndex = 0;
		};
		object = $(column).find('.mediaitem').eq(objectIndex);
	}	
	else object = $('#mediagridnew').find('.mediacontainer:first-child').find('.mediaitem:first-child');
	
	activate_media(object);
}



