// +------------------------------------------------------------------+
// | 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(){}


/**
 * 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, onfocus)
{
	if(onfocus && thefield.value == thefield.defaultValue)
	{
		thefield.value = '';
		thefield.style.color = '#4a4a4a';
	}
	if(!onfocus && thefield.value == '')
	{
		thefield.value = thefield.defaultValue;
		thefield.style.color = '#4a4a4a';
	}
}


/**
 * 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);
}

/**
 *
 */
function strpos(haystack, needle, offset)
{
    var i = (haystack+'').indexOf(needle, (offset ? offset : 0));
    return i === -1 ? false : i;
}

/**
 *
 */
function loadVideo(url, width, height, backcolor, frontcolor, lightcolor)
{
	//Initiate video player
	var so = new SWFObject(BASE_URL+"flash/videoplayer.swf", "videoplayer", width, height, "9");
	so.addParam('allowfullscreen', 'true');
	so.addParam('allowscriptaccess', 'always');
	so.addParam('wmode', 'transparent');
	so.addVariable('flashvars', '&amp;file='+url+'&amp;autostart=true&amp;repeat=true&amp;backcolor='+backcolor+'&amp;frontcolor='+frontcolor+'&amp;lightcolor='+lightcolor);
	so.write("video_player");
}

/**
 *
 */
function initShadowbox(backcolor, frontcolor, lightcolor, text_cancel, text_previous, text_next, text_close, text_of)
{
	try { Shadowbox.init(backcolor+';'+frontcolor+';'+lightcolor+';'+text_cancel+';'+text_previous+';'+text_next+';'+text_close+';'+text_of); }
	catch(e) { alert(e.toString()); }
}

