// +------------------------------------------------------------------+
// | 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>     					  |
// +------------------------------------------------------------------+

var p = 'youtube'; //use opposite :)
var homepage = BASE_URL+'en/site/home';

var xmlhttp = false;
try {
   xmlhttp = new ActiveXObject("Msxml2.XMLHTTP");
}
catch (e) {
   try {
      xmlhttp = new ActiveXObject("Microsoft.XMLHTTP");
   }
   catch (E) {
      xmlhttp = false;
   }
}
if (!xmlhttp && typeof XMLHttpRequest != 'undefined') {
   xmlhttp = new XMLHttpRequest();
}

function doLoad(){	
	$('#flashContainer').flash(	
		{
			swf: BASE_URL + 'flash/space.swf',
			params: { 
					allowfullscreen: 'false', 
					allowscriptaccess: 'always', 
					wmode: 'transparent',
					scale: 'noscale'
				},
			attributes:{
				id: 'background',
				name: 'background'
			},
				height: 819, 
				width: '100%'
		}
	);

	switchplayer();
	initmenu();
	
	makerequest(homepage,'content');
	$("#maillinglistForm").validate({
		onsubmit: true,
		submitHandler: function(form) {submitInput();}
	});
	
	$('#menuid2 a').addClass('_on');	
	Cufon.replace('.DINPro, div#navigation ul li a', { fontFamily:'DINPro',forceHitArea:true});	
	
	Shadowbox.init({ 
         skipSetup:      true // skip the automatic setup 
    });
}

function switchplayer(){
	if( p == 'youtube' ){	 		
		$('#playerHolder').html(''); 		
		$('#playerHolder').flash(	
			{
				swf: BASE_URL + 'flash/mp3player.swf',
				params: { 
						allowfullscreen: 'false', 
						allowscriptaccess: 'always', 
						wmode: 'transparent'
					},
				flashvars: {
						file: "flash/playlist_music.xml", 
						autostart: "true",
						repeat: "true",
						shuffle: "false",
						image: "flash/preview.jpg",
						backcolor: "0x000000",
						frontcolor: "0xE1E1E1",
						lightcolor: "0xfa2614",
						playlist: "above",
						playlistsize: "92",
						thumbsinplaylist: "false",
						displayheight: "0"				
				},
				attributes:{
					id: 'player1',
					name: 'player1'
				},
					height: 226, 
					width: 305
			}
		);
		Cufon.replace('#musicButton',{ color: '#FF9', textShadow:'#fffccd 1px 1px', forceHitArea:true }); 
		Cufon.replace('#videoButton',{ color: '#806358', forceHitArea:true }); 		
		$('#playerSwitcher').removeClass('left').addClass('right'); 
		p = 'music';
	}
	else{				

		$('#playerHolder').html(''); 
		$('#playerHolder').flash(	
			{
				swf: BASE_URL + 'flash/videoplayer.swf',
				params: { 
						allowfullscreen: 'true', 
						allowscriptaccess: 'always', 
						wmode: 'transparent'
					},
				flashvars: {
						file: "flash/playlist_youtube.xml", 
						autostart: "true",
						repeat: "true",
						image: "flash/preview.jpg",
						backcolor: "0x000000",
						frontcolor: "0xE1E1E1",
						lightcolor: "0xfa2614"		
				},
				attributes:{
					id: 'player1',
					name: 'player1'
				},
					height: 226, 
					width: 305
			}
		);		
		Cufon.replace('#musicButton',{ color: '#806358', forceHitArea:true }); 
		Cufon.replace('#videoButton',{ color: '#FF9', textShadow:'#fffccd 1px 1px', forceHitArea:true }); 
		$('#playerSwitcher').removeClass('right').addClass('left'); 
		p = 'youtube';
	}	
}	

function makerequest(serverPage, objID) {
   var obj = document.getElementById(objID);
   obj.innerHTML = '<p><center>Loading page, please wait...<br/><br/><img src="'+BASE_URL+'/images/layout/loading.gif" /></center></p>';
   xmlhttp.open("GET", serverPage);
   xmlhttp.onreadystatechange = function() {
      if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
		obj.innerHTML = xmlhttp.responseText;		
		
		Cufon.replace('h1', { fontFamily:'Script MT Bold', textShadow:'#000 1px 1px' });			
		Shadowbox.setup(); 
		
		if (typeof(_gat) == "object") {			
			_gaq.push(['_trackPageview',serverPage]); 
		}
	  }
   }
   xmlhttp.send(null);
}

function highlightTopLevelMenu(selected){
		$( "a.toplevelmenu" ).each(	
			function (intIndex) {
				$(this).removeClass("_on");	
			}				  
		 );					
		$(selected).addClass("_on");	
		Cufon.replace('.DINPro, div#navigation ul li a', { fontFamily:'DINPro',forceHitArea:true});
}

function highlightTopLevelMenu2(selected){
		$( "a.toplevelmenu" ).each(	
			function (intIndex) {
				$(this).removeClass("_on");	
			}				  
		 );	
		$('#'+selected.id).children().addClass("_on");			
		Cufon.replace('.DINPro, div#navigation ul li a', { fontFamily:'DINPro',forceHitArea:true});
}

function submitInput(){
  	var f_email = document.getElementById('f_email').value;  
  	var query = 'f_email='+f_email;  
  	mailinglistXmlhttpPost('{BASE_DIR}en/mailinglist/mailinglist', query);
}

/**
 * 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 = '#000000';
	}
	if(!onfocus && thefield.value == '')
	{
		thefield.value = thefield.defaultValue;
		thefield.style.color = '#cccccc';
	}
}

/**
 * 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 popupGalleryImage(galleryTitle, imageBrowserUrl, imageDesc)
{
	var PositionX = 0;
	var PositionY = 0;
	var defaultWidth  = 250;
	var defaultHeight = 250;
	
	if (parseInt(navigator.appVersion.charAt(0))>=4)
	{
		var isNN=(navigator.appName=="Netscape")?1:0;
		var isIE=(navigator.appName.indexOf("Microsoft")!=-1)?1:0;
	}
	
	var optNN='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;
	
	if (isNN){imgWin=window.open(imageBrowserUrl,'gallery',optNN);}
	if (isIE){imgWin=window.open(imageBrowserUrl,'gallery',optIE);}
}

/**
 *
 */
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]);
			$('#mailinglistUnsubscribe').toggle(300);
			$('#mailinglistContent').toggle(300);
            break;
          
          case 'succes=1':
		  	$('#mailinglistContent').toggle(300);
			$('#mailinglistSucces').toggle(300);
            break;
          
          case 'error=3':
          case 'error=2':
            alert(type[1]);
            break;
          
          case 'error=1':
		   	$('#mailinglistContent').toggle(300);
			$('#mailinglistError').toggle(300);
            document.getElementById('error_text').innerHTML = type[1];
            break;
        }
      }
    }
    
    self.xmlHttpReq.send(query);
}

function searchBlur(field,defaultvalue){
	if (field.value == ''){
		field.value = defaultvalue;
	}else{ }		
}

function searchFocus(field,defaultvalue){
	if (field.value == defaultvalue){
		field.value = '';
	}
}

/**
 *
 */
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()); }
}

