// GEN basis
function popup(url,winName,W,H,X,Y) {
	if(W == undefined) W = 400;	//default width
	if(H == undefined) H = 300; //default height
	if(X == undefined) X = screen.width/2-W/2; //default Xpos (is now centered)
	if(Y == undefined) Y = screen.height/2-H/2; //default Ypos (is now centered)
	X = 10;
	Y = 10;
	winprops = 'height='+H+',width='+W+',top='+X+',left='+Y+',scrollbars=yes, resizable=yes';

	win = window.open(url, winName, winprops);
	if (parseInt(navigator.appVersion) >= 4) {
		win.window.focus();
	}
}

function $rgb(id)
{

	return document.getElementById(id);	
}
function hide(id)
{
		
	$rgb(id).style.display = 'none';
}
function show(id)
{
		
	$rgb(id).style.display = 'block';
}
function isHidden(id)
{
	if($rgb(id).style.display == 'none')
		return true;
	else
		return false;
}
function V_switch(id)
{
	if(isHidden(id))
		show(id);
	else
		hide(id);
}
// GEN basis
// AJAX basis
	
var parsing_xml = false;

function loadXMLFromText(xmlData)
{
	if (window.ActiveXObject) {
			//for IE
			xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
			xmlDoc.async="false";
			xmlDoc.loadXML(xmlData);
			return xmlDoc;
		} else if (document.implementation && document.implementation.createDocument) {
			//for Mozila
			parser=new DOMParser();
			xmlDoc=parser.parseFromString(xmlData,"text/xml");
			return xmlDoc;
		}

}
function replacewww(text) { 
	var start = (text.indexOf("www"))-7; 
	var match = text.substr(start,7); 
	if(match != "http://" || match != "ttps://") 
	{ 
	text = text.replace("www", "http://www"); 
	} 
	
	 
	return text; 
}
function replaceURLWithHTMLLinks(text) { 
	var exp = /(\b(https?|ftp|file):\/\/[-A-Z0-9+&@#\/%?=~_|!:,.;]*[-A-Z0-9+&@#\/%=~_|])/ig; 
	return text.replace(exp,"<a href='$1' >$1</a>"); 
}


function processXmlTwitter(response)
{
	parsing_xml =false; 
	xmlDoc=loadXMLFromText( response );
	var str = xmlDoc.getElementsByTagName("text")[0].childNodes[0].nodeValue; 
	 
	str = replacewww(str); 
	str = replaceURLWithHTMLLinks(str); 
	$rgb('twittertxt').innerHTML=  "<p>"+str+"</p>";
	show('twitter');
	
	
}
														
function processData(responseText, responseStatus)
	 { 
		
		//document.getElementById('loadingbar').style.display ='none';
		if (responseStatus==200) // succes
		{
			if(responseText == 'error')
							alert('Error updating data!'+responseText);
			else
			{
				if(parsing_xml)
					processXmlTwitter(responseText);
				else
				{
					
					eval(responseText);
				}
				
			}
		 }
		else if(responseStatus == 0)
		{
		
		}
		else 
		{ 
		   alert(responseStatus + ' -- Error Processing Request');
		}
	  }
function ajaxObject(url) {
		  var that=this;      
		   this.updating = false;
			  this.abort = function() {
					that.AJAX.onreadystatechange = function () {}
				  that.updating=false;
				  that.AJAX.abort();
				  that.AJAX=null;
				
			  }
		  this.update = function(form,postMethod) { 
			if(that.updating) that.abort();
			 that.AJAX = null;                      
			 var passData = getFormValues(form,"validate");
			 
			 
					
			if (window.XMLHttpRequest) {              
			  that.AJAX=new XMLHttpRequest();              
			} else {                                  
			  that.AJAX=new ActiveXObject("Microsoft.XMLHTTP");
			}                                             
			if (that.AJAX==null) {                             
			  return false;                               
			} else {
			
				
				  that.AJAX.onreadystatechange = function() {  
					if (that.AJAX.readyState==4) {             
					  that.updating=false;                
					  that.callback(that.AJAX.responseText,that.AJAX.status);        
					  that.AJAX=null;                                         
					} 
					}
																
			  that.updating = new Date();                              
			  if (/post/i.test(postMethod)) {
				var uri=urlCall+'?'+that.updating.getTime();
				that.AJAX.open("POST", uri, true);
				that.AJAX.setRequestHeader("Content-type", "application/x-www-form-urlencoded; charset=UTF-8");
				that.AJAX.setRequestHeader("Content-Length", passData.length);
				that.AJAX.send(passData);
			  } else {
				var uri=urlCall+'?'+passData+'&timestamp='+(that.updating.getTime()); 
				that.AJAX.open("GET", uri, true);                             
				that.AJAX.send(null);                                         
			  }              
			  return true;                                             
			}                                                                           
		  }
		  var urlCall = url;        
		  this.callback = processData;
		  }
		  
		
	
function getFormValues(fobj,valFunc, url) 			
	{ 
	
	   var str = ""; 
	
	   var valueArr = null; 
	
	   var val = ""; 
	
	   var cmd = ""; 
	
	   for(var i = 0;i < fobj.elements.length;i++) 
	
	   { 
	
		   switch(fobj.elements[i].type) 
	
		   { 
			case "checkbox":
					var myval = fobj.elements[i].checked;
					if(myval) 
						myval = 1;
					else
						myval = 0;								   
				   str += fobj.elements[i].name + 
	
					"=" + myval + "&"; 
	
					   break; 
			  case "textarea":
				 str += fobj.elements[i].name + 
	
						 "=" + escape(fobj.elements[i].value) + "&"; 
						 break; 
		   
				case "password":
					str += fobj.elements[i].name + 
	
						 "=" + escape(fobj.elements[i].value) + "&"; 
						  break; 
				case "hidden":
					str += fobj.elements[i].name + 

					 "=" + (fobj.elements[i].value) + "&"; 
					  break; 
			   case "text": 
	
							
					str += fobj.elements[i].name + 
	
					 "=" + escape(fobj.elements[i].value) + "&"; 
	
					 break; 
	
			   case "select-one": 
	
					str += fobj.elements[i].name + 
	
					"=" + fobj.elements[i].options[fobj.elements[i].selectedIndex].value + "&"; 
	
					break; 
	
		   } 
	
	   } 
							
				
					 
					 
	   str = str.substr(0,(str.length - 1)); 
	
	   return str; 
	
	}


// TWITTER

	
//  AJAX basis
//  AJAX basis

//  MYRGB basis
function addListener(element, type, expression)
{
if(window.addEventListener)	
{
 // Standard
 element.addEventListener(type, expression, false);
 return true;
 }
  else if(window.attachEvent)
   { // IE	
	element.attachEvent('on' + type, expression);	
	return true;	
}
 else return false;
}

addListener(document, "keyup", function (e) { if (!e) { e = event; } if (e.keyCode == 27) { document.location.href = "http://www.myrgb.be"; } } );
//  MYRGB basis


// SEARCH BAR


// BLOGARCHIVE TWEEN
// BLOGARCHIVE TWEEN

// ajax INIT
//var myRequest = new ajaxObject( 'http://www.rgbscape.be/'+'ajax/searchsuggest.php');

// Adwindow magic
var curx = 0;
var maxw = 0;
var active = 0;
var busy = false;
var timer = null;
function startAdwindow()
{
	var div = document.getElementById('adwindow_scroller');
	if(div == null) return;
	
	maxw = 960*$rgb('adwindow_scroller').childNodes.length;
	
	active = maxw/960-1;
	setActive(active);
	timer = setTimeout('timerAdwindow()', 10000);
	
}
function timerAdwindow()
{
	newx = curx - 960;
	if(-newx >= maxw) newx = 0;
	
	var div = $rgb('adwindow_scroller');
	var temp1 = curx;
	var temp2 = newx;
	
	active = maxw/960 + newx/960-1;
	
	setActive(active);
	
	
	t1 = new Tween( div.style,'left',Tween.regularEaseInOut,temp1 ,temp2 ,1.25,'px');
	t1.start();
	busy = true;
	curx = newx;
	t1.onMotionFinished = function(){ busy = false; clearTimeout(timer);  timer =   setTimeout('timerAdwindow()', 10000);     };
	
	
	
	
}
function setActive(id)
{
	for(var i = 0; i< maxw/960; i++)
		$rgb('controller_'+i).className = '';
	
	
	$rgb('controller_'+id).className = 'active';
		
}
function adwindowGoto(x)
{
	if(busy) return;
	clearTimeout(timer);
	
	// set classes
	setActive(x);
	
	newx = -maxw+960+x*960;
	var div = $rgb('adwindow_scroller');
	var temp1 = curx;
	var temp2 = newx;
	
	t1 = new Tween( div.style,'left',Tween.regularEaseInOut,temp1 ,temp2 ,1.25,'px');
	t1.start();
	busy = true;
	curx = newx;
	t1.onMotionFinished = function(){ busy = false;clearTimeout(timer);  timer =   setTimeout('timerAdwindow()', 10000);     };
	
	
}

// socialwidget magic
pic1= new Image(16,11); 
pic1.src="http://www.rgbscape.be/img/loading.gif"; 
function socialWidget(id, ajax)
{
	// set classes and  appear boxes
	for(var i = 0; i < 10; i++)
	{
		var div = document.getElementById('sw_'+i);
		if(div == null) break;
		
		div.className ="";
		
		$rgb('sc_'+i).style.display = 'none';
	}
	
	document.getElementById('sw_'+id).className = 'active';
	
	$rgb('sc_'+id).style.display='block';
	//
	if(ajax != "")
	{
		// add loading button
		$rgb('sc_'+id).innerHTML = "<img class='loading' src='http://www.rgbscape.be/img/loading.gif' />";
		var myRequest = new ajaxObject( 'http://www.rgbscape.be/'+'ajax/'+ajax+'.php');
		myRequest.update($rgb('socialform'), 'POST');

	}
	
	
	
}
function URLDecode( str)
{
   // Replace + with ' '
   // Replace %xx with equivalent character
   // Put [ERROR] in output if %xx is invalid.
   var HEXCHARS = "0123456789ABCDEFabcdef"; 
   var encoded = str;
   var plaintext = "";
   var i = 0;
   while (i < encoded.length) {
       var ch = encoded.charAt(i);
	   if (ch == "+") {
	       plaintext += " ";
		   i++;
	   } else if (ch == "%") {
			if (i < (encoded.length-2) 
					&& HEXCHARS.indexOf(encoded.charAt(i+1)) != -1 
					&& HEXCHARS.indexOf(encoded.charAt(i+2)) != -1 ) {
				plaintext += unescape( encoded.substr(i,3) );
				i += 3;
			} else {
				alert( 'Bad escape combination near ...' + encoded.substr(i) );
				plaintext += "%[ERROR]";
				i++;
			}
		} else {
		   plaintext += ch;
		   i++;
		}
	} // while
  return plaintext;
  
 
}

// JQUERY MAGIC
	var htmlholder = "";
	var htmlholder2 = "";
	$(document).ready(function() {
			
			
	// init fancybox on pics
	$("a.content-fotos").fancybox({
			'hideOnContentClick': false
		});
	// done
		
	// init fancybox on html content
	$("#correct_this_article").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false, 
		'hideOnContentClick' : false, 
		'callbackOnClose'		: function() {
			$rgb('correction_form').innerHTML = htmlholder;
			
			
		},
		'callbackOnShow'		: function() {
			htmlholder = $rgb('correction_form').innerHTML;
			$rgb('correction_form').innerHTML = "";
			
		}
		
	});
	
		
	// done
	
	
	
	//youtube magic
	$("#youtubelink").fancybox({
		'scrolling'		: 'no',
		'titleShow'		: false, 
		'hideOnContentClick' : false, 
		'frameWidth': 640,
		'frameHeight':360,
		'callbackOnClose'		: function() {
			
			
			
		},
		'callbackOnShow'		: function() {
			
			
		}
		
	});


	//done
	
	
	// work some external links
	$('a').filter(function() {
	 return this.hostname && this.hostname !== location.hostname && !this.href.match(/^javascript/) ; 
	}).addClass('extlink');

	// if on homepage, start adwindow
	startAdwindow();
	
	
	
	});
	
// search magic
function searchSite()
{
	$rgb('searchform').submit();
}
	
// contact magic
var myRequestContact = new ajaxObject( 'http://www.rgbscape.be/'+'ajax/contact.php');

function Contact()
{
	myRequestContact.update($rgb('contactform'), 'POST');
}
function contact_gt()
{
	window.location = "http://www.rgbscape.be/contact/bedankt";
}


// facebook magic
var myRequestFB = new ajaxObject( 'http://www.rgbscape.be/'+'ajax/facebook_blog_comments_update.php');

function updateFBComments(id)
{
	myRequestFB.update( $rgb('fb_blog_update'), 'POST');

}

// correction magic
var myRequestCorr = new ajaxObject( 'http://www.rgbscape.be/'+'ajax/correction.php');

function sendCorrection()
{
	myRequestCorr.update( $rgb('my_correction_form'), 'POST');
}
function correction_gt()
{
	window.location = "http://www.rgbscape.be/contact/bedankt-correctie";
}

// checkout magic

var myRequestCheckout = new ajaxObject( 'http://www.rgbscape.be/'+'ajax/checkout.php');

function goToCheckout(id)
{
	myRequestCheckout.update( $rgb('rgb_'+id), 'POST');
}
function ogone()
{
	$rgb("form_ogone").submit();
}
	