// +------------------------------------------------------------------+
// | JavaScript version 1.0                                           |
// +------------------------------------------------------------------+
// | general.js – Some standard functions used in the CMS cleint      |
// |              side. Script is loaded in template by parser.       |
// +------------------------------------------------------------------+
// | Copyright (c) 2008 MultiMove                                     |
// +------------------------------------------------------------------+
// | Authors: S.F.Beck <sander@multimove.nl>     					  |
// +------------------------------------------------------------------+

/**
 * The load function
 * 
 * @return void
 */ 
function doLoad( url ){
	try{
	mloadnow('m4');
	if(url.search(/inzet=/) == 0 ){
		document.getElementById('clientSideInclude').style.display='block';
		url = url.replace(/inzet=/,'');
		window.setTimeout('clientSideInclude("clientSideInclude",\''+url+'\')',0);
	}
	}catch(e){}
}


/**
 * Make pop-up windows
 * 
 * @return void
 */
function popup( url, width, height )
{
	var iLeft = (screen.width - width) / 2 ;
	var iTop  = (screen.height - height) / 2 ;

	var sOptions = "resizable=yes,dependent=yes,scrollbars=yes," ;
	sOptions += ",width=" + width ;
	sOptions += ",height=" + height ;
	sOptions += ",left=" + iLeft ;
	sOptions += ",top=" + iTop ;

	window.open( url, "FCKBrowseWindow", sOptions ) ;
}


/**
 * Clear textfields preset text and turn to black
 * 
 * @return void
 */
function clearText(thefield) {
	if (thefield.defaultValue == thefield.value) {
		thefield.value = "";
		thefield.style.color = "#b2b2b2";
	}
	else thefield.value = thefield.defaultValue;
}


/**
 * Detect the browser
 */
var browserDetect = {
	init: function() {
		this.browser = this.searchString(this.dataBrowser) || 'default';
	},
	searchString: function(data) {
		for(var i = 0; i < data.length; i++) {
			var dataString = navigator.userAgent;
			if (dataString) {
				if(dataString.indexOf(data[i].subString) != -1)
					return data[i].identity;
			}
		}
	},
	dataBrowser: [
		{
			subString: 'Chrome',
			identity: 'Chrome'
		},
		{
			subString: 'MSIE',
			identity: 'Explorer'
		},
		{
			subString: 'Firefox',
			identity: 'Firefox'
		},
		{
			subString: 'Safari',
			identity: 'Safari'
		}
	]
}


/**
 *
 */
function popImage(imageURL, imageTitle)
{
	browserDetect.init();
	
	var AutoClose			= true;
	var PositionX			= 0;
	var PositionY			= 0;
	var defaultWidth		= 250;
	var defaultHeight		= 75;
	
	var optFF = 'scrollbars=no,status=yes,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
	var optIE = 'scrollbars=no,status=yes,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
	var optChrome = 'scrollbars=no,status=yes,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
	var optSafari = 'scrollbars=no,status=yes,width='+defaultWidth+',height='+defaultHeight+',left='+PositionX+',top='+PositionY;
	
	switch(browserDetect.browser)
	{
		case 'Explorer': default: var isIE = true; break;
		case 'Firefox': var isFF = true; break;
		case 'Chrome': var isChrome = true; break;
		case 'Safari': var isSafari = true; break;
	}
	
	if(isFF) { imgWin = window.open('about:blank', '', optFF); }
	if(isIE) { imgWin = window.open('about:blank', '', optIE); }
	if(isChrome) { imgWin = window.open('about:blank', '', optChrome); }
	if(isSafari) { imgWin = window.open('about:blank', '', optSafari); }
	
	with(imgWin.document)
	{
		var image = new Image();
			image.onload = function() {
				
				switch(browserDetect.browser)
				{
					case 'Explorer': var width = this.width + 10; var height = this.height + 55; break;
					case 'Firefox': var width = this.width + 16; var height = this.height + 87; break;
					case 'Chrome': var width = this.width + 16; var height = this.height + 64; break;
					case 'Safari': var width = this.width; var height = this.height + 39; break;
				}
				
				if(width < 200) { width = 200; }
				if(height < 200) { height = 200; }
				
				getElementById('loading').style.display = 'none';
				
				getElementById('image').src = this.src;
				getElementById('image').style.width = this.width;
				getElementById('image').style.height = this.height;
				
				imgWin.resizeTo(width, height);
				imgWin.moveTo((screen.availWidth / 2) - (width / 2), (screen.availHeight / 2) - (height / 2));
				imgWin.document.title = imageTitle;
			};
			image.src = imageURL;
		
		writeln('<html><head><title>Loading..</title><style>body{margin: 0px; padding: 0px; font-family: Arial; font-size: 12px; color: #ffffff;}</style>');
		
		writeln('</head>');
		
		if(!AutoClose) writeln('<body bgcolor=000000 scroll="no" onload="resizeImage(); self.focus()">')
		else writeln('<body bgcolor=000000 scroll="no" onload="self.focus()" onblur="self.close()">');
		
		writeln('<div id="loading">Loading..</div>');
		writeln('<img id="image" onclick="window.close();" style="display: block; width: 0px; height: 0px; cursor: pointer;">');
		
		writeln('</body></html>');
		
		close();
	}
}

function mailinglistXmlhttpPost(url, query)
{
    var xmlHttpReq = false;
    var self = this;
    
    // Mozilla/Safari
    if (window.XMLHttpRequest)
    {
        self.xmlHttpReq = new XMLHttpRequest();
    }
    // IE
    else if (window.ActiveXObject)
    {
        self.xmlHttpReq = new ActiveXObject("Microsoft.XMLHTTP");
    }
    
    self.xmlHttpReq.open('POST', url, true);
    self.xmlHttpReq.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
    self.xmlHttpReq.onreadystatechange = function()
    {
      if(self.xmlHttpReq.readyState == 4)
      {
        var type = self.xmlHttpReq.responseText.split('|');
        
        switch(type[0])
        {
          default:
            alert(type[1]);
            break;
          
          case 'succes=2':
            alert(type[1]);
            document.getElementById('mailinglist_unsubscribe').style.display = 'none';
            document.getElementById('mailinglist_content').style.display = 'block';
            break;
          
          case 'succes=1':
            document.getElementById('mailinglist_content').style.display = 'none';
            document.getElementById('mailinglist_succes').style.display = 'block';
            break;
          
          case 'error=3':
          case 'error=2':
            alert(type[1]);
            break;
          
          case 'error=1':
            document.getElementById('mailinglist_content').style.display = 'none';
            document.getElementById('mailinglist_error').style.display = 'block';
            document.getElementById('error_text').innerHTML = type[1];
            break;
        }
      }
    }
    self.xmlHttpReq.send(query);
}

/**
 * AJAX clientside include
 * @return bool
 */
function clientSideInclude(id, url){
  var req = false;
  // For Safari, Firefox, and other non-MS browsers
  if (window.XMLHttpRequest) {
    try {
      req = new XMLHttpRequest();
    } catch (e) {
      req = false;
    }
  } else if (window.ActiveXObject) {
    // For Internet Explorer on Windows
    try {
      req = new ActiveXObject("Msxml2.XMLHTTP");
    } catch (e) {
      try {
        req = new ActiveXObject("Microsoft.XMLHTTP");
      } catch (e) {
        req = false;
      }
    }
  }
 var element = document.getElementById(id);
 if (!element) {
  alert("Bad id " + id +
   "passed to clientSideInclude." +
   "You need a div or span element " +
   "with this id in your page.");
  return;
 }
  if (req) {
    // Synchronous request, wait till we have it all
    req.open('GET', url, false);
    req.send(null);
    element.innerHTML = req.responseText;
  } else {
    element.innerHTML =
   "Sorry, your browser does not support " +
      "XMLHTTPRequest objects. This page requires " +
      "Internet Explorer 5 or better for Windows, " +
      "or Firefox for any system, or Safari. Other " +
      "compatible browsers may also exist.";
  }
}


function setCookie(sName, sValue){
	document.cookie = sName + '=' + escape(sValue) + '; expires=Fri, 31 Dec 2030 23:59:59 GMT; path=/';
}

function getCookie(sName){
  var aCookie = document.cookie.split('; '), i = aCookie.length, aCrumb;
  while (i--) {
    aCrumb = aCookie[i].split('=');
    if (sName == aCrumb[0]) {
      return typeof aCrumb[1] != 'undefined'? unescape(aCrumb[1]) : null;
    }
  }
  return null;
}


var ddopen = false;
var height = 0;
var heightMax = 145;
var speed = 12;
function mopen(ddmenuitem){	
	if ( height < heightMax-speed ){	
		height += speed; 
		$('#'+ddmenuitem).css({'height' : height+'px'});
		$('#'+ddmenuitem).css({'display' : 'block'});
		setTimeout('mopen("m4")', 5);
	}
	else{
		height = heightMax;
		$('#'+ddmenuitem).css({'height' : height+'px'});
		$('#'+ddmenuitem).css({'display' : 'block'});
		$('#submenu_down').css({'background' : 'url('+BASE_URL+'images/layout/close-up.gif)'});
		$("#submenu_down").bind("mouseenter",function(){$('#submenu_down').css({'background':'url('+BASE_URL+'images/layout/close-over.gif)'});});		
		$("#submenu_down").bind("mouseleave",function(){$('#submenu_down').css({'background':'url('+BASE_URL+'images/layout/close-up.gif)'});});	
		ddopen = true;
		setCookie('ddopen', ddopen); 	
	}
}

function mclose(ddmenuitem,redirect){	
	if ( height > speed ){
		height -= speed; 
		$('#'+ddmenuitem).css({'height' : height+'px'});
		window.setTimeout('mclose("m4",\''+redirect+'\')', 5);
	}
	else{	
		height = 0;
		$('#'+ddmenuitem).css({'height' : height+'px'});
		$('#'+ddmenuitem).css({'display' : 'none'});				
		$('#submenu_down').css({'background' : 'url('+BASE_URL+'images/layout/open-up.gif)'});	
		$("#submenu_down").bind("mouseenter",function(){$('#submenu_down').css({'background':'url('+BASE_URL+'images/layout/open-over.gif)'});});		
		$("#submenu_down").bind("mouseleave",function(){$('#submenu_down').css({'background':'url('+BASE_URL+'images/layout/open-up.gif)'});});	

		ddopen = false;		
		setCookie('ddopen', ddopen); 		
		if( redirect != '' ){
			window.location = redirect;
		}			
	}
}

function mopenclose(id){
	if(!ddopen){
		mopen(id);
	}
	else{
		mclose(id,'');	
	}
}

function mloadnow(id){	
	if( getCookie('ddopen') == 'true'){
		height = heightMax;
		mopen(id);			
	}
	else	
		mopen(id);
}
