//
//	CALENDAR
//

function calendar_open(){
	$('calendar').setStyle('display', 'block');
}

function calendar_close(){
	$('calendar').setStyle('display', 'none');
}

//
//	VIDEO
//

function loadyouTubePlayer(in_player_id, in_div, in_video_id){	
	var player_params = { allowScriptAccess: "always", wmode: "transparent", bgcolor: "#ffffff" };
	var player_atts = { id: in_player_id };
	swfobject.embedSWF("http://www.youtube.com/v/"+in_video_id+"&enablejsapi=1&playerapiid="+in_player_id, in_div, "396", "301", "9", null, null, player_params, player_atts);
	window.addEvent('youtube'+in_player_id+'IsReadyEvent', function(){
		$(in_player_id).setSize("396", "301");
		$('youtubeembedcode').set('value', $(in_player_id).getVideoEmbedCode());
	});
}

function reloadyouTubePlayer(in_player_id, in_div, in_video_id){	
	$(in_div+'container').set('html', '<div id="'+in_div+'"></div>');
	loadyouTubePlayer(in_player_id, in_div, in_video_id);
}

function onYouTubePlayerReady(playerId) {
	window.fireEvent('youtube'+playerId+'IsReadyEvent');
}

//
//	GOOGLE MAPS
//
var map;
var marker;
var point;

function load_google_map(){
	if (GBrowserIsCompatible()) {
		//	create map
		map = new GMap2(document.getElementById("map"));
		//	set type
		map.setMapType(G_HYBRID_MAP);
		//	set default location
 		map.setCenter(new GLatLng(52.364892, 4.881835), 17);
 		//	add controls
 		map.addControl(new GMapTypeControl());
 		map.addControl(new GSmallMapControl());
 		//	place point
 		point = new GLatLng(52.364892, 4.881835); 
		marker = new GMarker(point,  G_DEFAULT_ICON, false);
		GEvent.addListener(marker, "click", function() {
			marker.openInfoWindowHtml("<span style='color: #000000;'><b>Sugar Factory</b><br>Lijnbaansgracht 238<br>1050 DT<br>Amsterdam</span>");
		});  
		map.addOverlay(marker);
	}
}

//
//	INTERFACE
//

function expand_item(in_type, in_id){
	var intro = $(in_type+'_intro_'+in_id);
	var body = $(in_type+'_body_'+in_id);
	new Fx.Morph(intro, {duration: 500}).start({
		'height': [intro.getSize().y, 0]
	});
	new Fx.Morph(body, {duration: 1000}).start({
		'height': [0, body.getScrollSize().y]
	});
}

function collapse_item(in_type, in_id){
	var intro = $(in_type+'_intro_'+in_id);
	var body = $(in_type+'_body_'+in_id);
	new Fx.Morph(intro, {duration: 1000, transition: Fx.Transitions.Sine.easeInOut}).start({
		'height': [0, intro.getScrollSize().y]
	});
	new Fx.Morph(body, {duration: 1000, transition: Fx.Transitions.Sine.easeInOut}).start({
		'height': [body.getSize().y, 0]
	});
}

function toggle(in_element_id, in_text_id){
	if($defined($(in_element_id))){
		var el = $(in_element_id);
		var cur_height = el.getSize().y;
		var con_height = el.getScrollSize().y;
		if(cur_height){
			if($(in_text_id).getProperty('closed_message')){
				$(in_text_id).set('html', $(in_text_id).getProperty('closed_message'));
			}
			new Fx.Tween(el, {onComplete: fix}).start('height', cur_height, 0);
		} else {
			if($(in_text_id).getProperty('open_message')){
				$(in_text_id).set('html', $(in_text_id).getProperty('open_message'));
			}
			new Fx.Tween(el, {onComplete: fix}).start('height', cur_height, con_height);
		}
	}
}

function copy(inElement) {
  if (inElement.createTextRange) {
    var range = inElement.createTextRange();
    if(range)
      range.execCommand('Copy');
  } else {
    var flashcopier = 'flashcopier';
    if(!document.getElementById(flashcopier)) {
      var divholder = document.createElement('div');
      divholder.id = flashcopier;
      document.body.appendChild(divholder);
    }
    document.getElementById(flashcopier).innerHTML = '';
    swfobject.embedSWF("http://sugarfactory.nl/swf/clipboard.swf?clipboard="+escape(inElement.value), flashcopier, "0", "0", "8", null, null, null, null);
  }
}

function subscribe(){
	new Request.HTML({   
        method: 'post',   
        url: 'http://sugarfactory.nl/page=site.subscribe',
        onComplete: function(){
			loadDiv('mailinglist/status=subscribed', 'rightcontent');
		}
	}).post({'email': $('input_email_subscribe').value}); 
}

function unsubscribe(){
	new Request.HTML({   
        method: 'post',   
        url: 'http://sugarfactory.nl/page=site.unsubscribe',
        onComplete: function(){
			loadDiv('mailinglist/status=unsubscribed', 'rightcontent');
		}
	}).post({'email': $('input_email_subscribe').value}); 
}

function email_this(balloon_id){
	new Request.HTML({   
        method: 'post',   
        url: 'http://sugarfactory.nl/page=site.email_this/balloon.html?'+$(balloon_id+'_form').toQueryString(),
        evalScripts: true
	}).post({'q': 'send'}); 
}

var oStatus = 1;

function appear(in_text, in_width, in_height){
	if(oStatus == 0){
		
		if($defined(smartBox)){
			smartBox.closeBox();
		}
		
		$('overlay_content').set('html', '');
		$('overlay_container').setStyle('width', in_width+"px");
		$('overlay_container').setStyle('height', in_height+"px");
		$('overlay_container').setStyle('marginLeft', -1 * Math.round(in_width/2)+"px");
		$('overlay_container').setStyle('opacity', 0);
		$('overlay_container').setStyle('display', 'block');
		$('overlay_close').setStyle('marginLeft', (in_width)+"px");
		$('overlay_container').setStyle('margin-top', window.getScrollTop()+((document.body.clientHeight/2)-250)+"px");
		
		$('overlay').setStyle('opacity', 0);
		var oHeight = Math.max($('body').offsetHeight.toInt(), $('site').offsetHeight.toInt());
		$('overlay').setStyle('height', oHeight+"px");
		$('overlay').setStyle('display', 'block');
		oStatus = -1;
		new Fx.Tween($('overlay'), {duration: 500, onComplete: function(){
			oStatus = 1;
			$('overlay_content').set('html', in_text);
			new Fx.Tween($('overlay_container'), {duration: 500}).start('opacity', 0, 1);
		}}).start('opacity', 0, 0.8);
	}
}

function disappear(){
	if(oStatus == 1){
		oStatus = -1;
		
		new Fx.Tween($('overlay_container'), {duration: 500, onComplete: function(){
			$('overlay_container').setStyle('display', 'none');
			$('overlay_content').set('html', '');
		}}).start('opacity', 1, 0);
		
		new Fx.Tween($('overlay'), {duration: 500, onComplete: function(){
			$('overlay').setStyle('display', 'none');
			oStatus = 0;
		}}).start('opacity', 0.8, 0);
	}
}

//
//	INIT
//

function pngfix(){
	var arVersion = navigator.appVersion.split("MSIE");
	var version = parseFloat(arVersion[1]);
	if ((version >= 5.5 && version < 7) && (document.body.filters)){
		var png_images = $$('.pngfix');	
		png_images.each(function(element) {
			var img = $(element);
			if(!img.hasClass('pngfixed')){
				img.addClass('pngfixed');
				var imgID = (img.id) ? "id='" + img.id + "' " : ""
				var imgClass = (img.className) ? "class='" + img.className + "' " : ""
				var imgTitle = (img.title) ? "title='" + img.title + "' " : "title='" + img.alt + "' "
				var imgStyle = "display:inline-block;" + img.style.cssText 
				if (img.align == "left") imgStyle = "float:left;" + imgStyle
				if (img.align == "right") imgStyle = "float:right;" + imgStyle
				if (img.parentElement.href) imgStyle = "cursor:hand;" + imgStyle
				var strNewHTML = "<span " + imgID + imgClass + imgTitle
				+ " style=\"" + "width:" + img.width + "px; height:" + img.height + "px;" + imgStyle + ";"
				+ "filter:progid:DXImageTransform.Microsoft.AlphaImageLoader"
				+ "(src=\'" + img.src + "\', sizingMethod='image');\"></span>" 
				img.outerHTML = strNewHTML	
			}
		});
	}
}

function loadDiv(in_url, in_div, in_options){
	$(in_div).set('html','<center><br><br><br><br><br><br><img src="http://sugarfactory.nl/img/ajaxloader.gif"><br><br>Loading...<br><br><br><br><br><br></center>');
	new Fx.Scroll($('body'), {'duration': 1000}).start(0,0);
	new Request.HTML({   
        method: 'get',   
        url: 'http://sugarfactory.nl/page=site.'+in_url, 
        evalScripts: true, 
        headers: {'X-REQUEST': 'AJAX'},
        update: $(in_div),
        onComplete: function(){ 
			pageTracker._trackPageview("http://sugarfactory.nl/page=site."+in_url); 
			initDiv(in_div); 
		}
	}).send(in_options); 
}

function loadPic(in_url, in_div){
	new Fx.Tween($(in_div), {duration: 250, onComplete: function(){
		new Request.HTML({   
	        method: 'get',   
	        url: 'http://sugarfactory.nl/page=site.'+in_url, 
	        evalScripts: true, 
	        headers: {'X-REQUEST': 'AJAX'},
	        update: $(in_div),
	        onComplete: function(){ 
	        	pic_height = $('picture_image').getProperty('height').toInt();
	        	new Fx.Tween($(in_div), {onComplete: function(){ initDiv(in_div); }}).start('height', $(in_div).getSize().y, pic_height+20);
			}
		}).send(); 	
	}}).start('height', $(in_div).getSize().y, 270);
	$(in_div).set('html','<center><br><br><br><br><br><br><img src="http://sugarfactory.nl/img/ajaxloader.gif"><br><br>Loading...<br><br><br><br><br><br></center>');
}

function initDiv(in_div){
	initAjaxlinks();
	pngfix();
	fix();
	EB.reprocessAttributes();
	smartBox.initialize({
	    xOffset: 2,
	    lockX: 'right',
	    lockY: 'top',
	    smartBoxSuffix: '_hoverbox',
	    yOffset: 5
	});
}

function initAjaxlinks(){
	var xs = $$('a.xlink');
	for(i=0;i<xs.length;i++){
		xs[i].removeEvents('click');
		xs[i].addEvent('click', function(e) {
			if (e) new Event(e).stop();
			eval($(this).getProperty('xhref'));
		});		
	}
}

function fix(){
	var height = Math.max($('left').offsetHeight.toInt(), $('right').offsetHeight.toInt());
	$('column1').setStyle('height', height+"px");
	$('column2').setStyle('height', height+"px");
}
