function str_replace (search, replace, subject, count) {
    f = [].concat(search),
    r = [].concat(replace),
    s = subject,
    ra = r instanceof Array, sa = s instanceof Array;    s = [].concat(s);
    if (count) 
    {
        this.window[count] = 0;
    }
    for (i=0, sl=s.length; i < sl; i++) 
    {
        if (s[i] === '') 
        {
            continue;
        }
        for (j=0, fl=f.length; j < fl; j++) 
        {            
            temp = s[i]+'';
            repl = ra ? (r[j] !== undefined ? r[j] : '') : r[0];
            s[i] = (temp).split(f[j]).join(repl);
            if (count && s[i] !== temp) 
            {
                this.window[count] += (temp.length-s[i].length)/f[j].length;
            }        
        }
    }
    return sa ? s : s[0];
}

$(document).ready(function()
{
    
    $('#campaign').click(function()
    {
        location.href = 'http://www.linc.nl';
    });
    $('#design').click(function()
    {
        location.href = 'http://www.linc.nl';
    });
    
    var input = document.getElementsByTagName('input');
    for (var a=input.length-1; a >= 0; a--) 
    {
        if (input[a].type != 'text' || input[a].className != 'custInput') { continue; } // skips non-text inputs
        input[a].mask = input[a].value;
        input[a].onfocus = function() {
            if (this.value == this.mask) { this.value = ''; }
            $(this).css('color', '#414141');
        };
        input[a].onblur = function() {
            if (this.value == '') { $(this).css('color', '#cbcbcb'); this.value = this.mask;  }
        };
    } 
    
    $('.custVerzenden').click(function()
    {
        
        $test = ValidateRequiredFields(this.id, 'naam,email','Naam,E-mailadres','De volgende velden zijn verplicht','E-mailadres is geen e-mail');
        if (!$test)
        {
            return false;
        }
        sendMail();        
        $('#lightboxCustomizer').click();
          
        return true;
    });
    
    $("a[rel='customizer']").colorbox({width:"413px", height:"350px", iframe:true});
    
    $("#homeLeftForm").validate({
        errorPlacement: function(error, element) {
			
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		},
        rules: 
        {
            contactpersoon: {
                NotEqual : '' 
            }
        }
    });
    
    $("#homeRightForm").validate({
        errorPlacement: function(error, element) {
			
		},
		// set this class to error-labels to indicate valid fields
		success: function(label) {
			// set &nbsp; as text for IE
			label.html("&nbsp;").addClass("checked");
		},
        rules: 
        {
            contactpersoon2: {
                NotEqual : '' 
            }
        }
    });
    
    $.validator.methods.NotEqual = function(value, element) {
        return value != element.defaultValue;
    };
    
    $(".clearInput").focus(function(srcc)
    {
        if ($(this).val() == this.defaultValue)
        {
            $(this).val("");
            $(this).css('color', '#797979');
        }
    });
    
    $(".clearInput").blur(function()
    {
        if ($(this).val() == "")
        {
            $(this).val(this.defaultValue);
        }
    });
    
    $(".clearInput").blur();
    
	$("a[rel='lightbox']").colorbox();
    
    $('.switchSite').mouseover(function()
    {
        $(this).attr('src', 'images/menuDesignOver.png');
    });
    
    $('.switchSite').mouseout(function()
    {
        $(this).attr('src', 'images/menuDesign.png');
    });
    
    $('#header > ul').innerfade({
		animationtype: 'fade',
		speed: 1000,
		timeout: 4000,
		type: 'sequence'
	});
    
    $('#headerTekst > ul').innerfade({
		animationtype: 'fade',
		speed: 1000,
		timeout: 4000,
		type: 'sequence'
	});
    
    $('.references > ul').innerfade({
		animationtype: 'fade',
		speed: 1000,
		timeout: 3500,
		type: 'sequence'
	});
    
    $(".menu").click(function()
    {
        $(this).children('img').attr('onmouseout', '');
        $oldSrc = $('.menuActive').children('img').attr('src');
        $newSrc = str_replace('Over', '', $oldSrc, 1);
        $('.menuActive').children('img').attr('src', $newSrc);
        
    });
    
    $('.newsList').click(function()
    {
        location.href = $(this).attr('title');
    });
    
    setTimeout(function()
    {
        $('#ballon').fadeIn();
    }, 5000);
    
    $(".colorAjax").colorbox(); 
    
    $('.sliderMedium').mouseover(function()
    {
        $(this).attr('style', "background: url('images/sliderHover.png');");
    });
    
    $('.sliderMedium').mouseout(function()
    {
        $(this).attr('style', "background: url('images/slider.png');");
    });
    
    $('.sliderSmall').mouseover(function()
    {
        $(this).attr('style', "background: url('images/sliderSmallHover.png');");
    });
    
    $('.sliderSmall').mouseout(function()
    {
        $(this).attr('style', "background: url('images/sliderSmall.png');");
    });
    
    $(".slide").slider({
        range: 'min',
		value:0,
		min: 0,
		max: 10,
		step: 1,
		slide: function(event, ui) {
            $(this).parent('div').next().html(ui.value);
            $id = this.id;
            $data = $('#data').html();
            $.get("includes/updateCustomizer.php", { name: $id, count: ui.value, data : $data },function(data)
            { 
                $('#data').html(data);
                setData(data);
            });
		}
	});
});

function changePortfolio(id)
{
    $('#portfolioContent').html('<img src="images/loading.gif" width="25" height="25" alt="Loading..." style="vertical-align:middle"/>').load('controllers/getPortfolio.php?idPortfolio=' + id);
    return false;
}

function mycarousel_itemVisibleInCallback(carousel, item, i, state, evt)
{
    // The index() method calculates the index from a
    // given index who is out of the actual item range.
    var idx = carousel.index(i, mycarousel_itemList.length);
    carousel.add(i, mycarousel_getItemHTML(mycarousel_itemList[idx - 1]));
    
};

function mycarousel_itemVisibleOutCallback(carousel, item, i, state, evt)
{
    carousel.remove(i);
};

/**
 * Item html creation helper.
 */
 

function mycarousel_getItemHTML(item)
{
    return '<a href="' + item.link + '" id="portfolio_' + item.pf_id + '" onclick="return changePortfolio(this.id);"><img src="' + item.url + '" width="76" height="76" alt="' + item.title + '" /></a>';
};


$(document).ready(function()
{
    $('.klantgroep').click(function()
    {
        location.href = $(this).children('a').attr('href');
    });
});

pic7= new Image(508,62); 
pic7.src="http://www.linc.nl/images/klantgroepLijstBackgroundHover.png";
pic8= new Image(508,62); 
pic8.src="http://www.linc.nl/images/klantgroepLijstBackground.png";

$(document).ready(function(){
	$("a[rel='login']").colorbox({width:"500px", height:"540px", iframe:true});
});

function setData(data) 
{
    flashMovie.setData(data);
}

function amChartInited(chart_id)
{   
    flashMovie = document.getElementById(chart_id);           
}

function amReturnImageData(chart_id, data)
{
    $smallData = $('#data').html(); 
    $naam = $('#naam').val();
    $email = $('#email').val();
    $notitie = $('#notitie').val();    
    $.post("includes/amline/export.php", { smallData: $smallData, id: chart_id , data: data, naam: $naam, email:$email, notitie:$notitie }, function(data)
    {
    });
}

function sendMail() {
    flashMovie.exportImage();
}
