// Core.js
// Loader and some website functions
$(document).ready(function() {
	$(document).pngFix(); 
	initHeadlines();
	
	$('.input').defaultInputValue();
	$('#paper_delivery_signup input').placeholder();
	$(".fancyBoxIframe").fancybox({
		'width'				: 700,
		'height'			: 400,
		'autoScale'			: false,
		'autoDimensions'	: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe'
	});  
	$(".fancyBoxIframeFlv").fancybox({
		'width'				: 506,
		'height'			: 400,
		'autoScale'			: false,
		'autoDimensions'	: false,
		'transitionIn'		: 'elastic',
		'transitionOut'		: 'elastic',
		'type'				: 'iframe'
	});  
	$('#carousel').jcarousel({
        visible: 1,
		scroll: 1,
		auto: 7,
		animation: 1000,
		wrap: 'both',
		itemFallbackDimension: 1920,	
		itemLoadCallback : {
			onBeforeAnimation: carousel_onBeforeAnimation,
 	 		onAfterAnimation: carousel_onAfterAnimation
		},
		initCallback: carousel_initCallback
    });	
	$('#paper_radio label').click(function() {
		var $for = $(this).attr('for');
		$('#' + $for).click();
		$('#paper_radio label[for="'+$('#paper_radio input:checked').attr('id')+'"]').addClass('checked').siblings('label').removeClass('checked');
	});
});

function carousel_onBeforeAnimation(){
	$('#carousel-next').hide();
	$('#carousel-prev').hide();
}

function carousel_onAfterAnimation(){
	$('#carousel-next').show();
	$('#carousel-prev').show();
}

//Callback init carousel
carousel_initCallback = function(carousel){
	$('#carousel-next').bind('click', function() {
        carousel.next();
        return false;
    });
 
    $('#carousel-prev').bind('click', function() {
        carousel.prev();
        return false;
    });
	
	// Pause autoscrolling if the user moves with the cursor over the clip.
    carousel.clip.hover(function() {
        carousel.stopAuto();
    }, function() {
        carousel.startAuto();
    });
}

//Init headlines
initHeadlines = function () {
	var i=1;
	$('div.newsHeadlines').children().each(function() { 
		$(this).attr("id",'newsHeadlineItem'+i);     
		$('#newsHeadlinesNav').append('<a href="javascript:toggleHeadline('+ i +');" id="newsHeadlineBul'+i+'"></a>');
		i++;
	});
	$('#newsHeadlineItem1').delay(300).show(800).addClass('show');
	$('#newsHeadlineBul1').addClass('show');
}


//Init rotator
initRotator = function () {
	$('div#cases div.slide:last').addClass('show');
	rotateNow('right');
}

//Headline functions
toggleHeadline = function(id){
	 $("div.newsHeadlines div.show").removeClass('show').hide(400,function(){	 
		$('#newsHeadlineItem'+id).show(400).addClass('show');
		$('#newsHeadlinesNav a.show').removeClass('show');
		$('#newsHeadlineBul'+id).addClass('show');
	})	 
}

/**
 * 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';
	}
}

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)
{
	text_close = 'Sluiten';
	try { Shadowbox.init(backcolor+';'+frontcolor+';'+lightcolor+';'+text_cancel+';'+text_previous+';'+text_next+';'+text_close+';'+text_of); }
	catch(e) { alert(e.toString()); }
}

function theRotator() {
	//Set the opacity of all images to 0
	$('div#rotator ul li').css({opacity: 0.0});
	
	//Get the first image and display it (gets set to full opacity)
	$('div#rotator ul li:first').css({opacity: 1.0});
		
	//Call the rotator function to run the slideshow, 6000 = change to next image after 6 seconds
	setInterval('rotate()',6000);
}

function rotate() {	
	//Get the first image
	var current = ($('div#rotator ul li.show')?  $('div#rotator ul li.show') : $('div#rotator ul li:first'));

	//Get next image, when it reaches the end, rotate it back to the first image
	var next = ((current.next().length) ? ((current.next().hasClass('show')) ? $('div#rotator ul li:first') :current.next()) : $('div#rotator ul li:first'));	
	
	//Set the fade in effect for the next image, the show class has higher z-index
	next.css({opacity: 0.0})
	.addClass('show')
	.animate({opacity: 1.0}, 1000);

	//Hide the current image
	current.animate({opacity: 0.0}, 1000)
	.removeClass('show');  
};	
