function str_replace(search, replace, subject) {
    var f = search, r = replace, s = subject;
    var ra = r instanceof Array, sa = s instanceof Array, f = [].concat(f), r = [].concat(r), i = (s = [].concat(s)).length;
 
    while (j = 0, i--) {
        if (s[i]) {
            while (s[i] = (s[i]+'').split(f[j]).join(ra ? r[j] || "" : r[0]), ++j in f){};
        }
    };
 
    return sa ? s : s[0];
}
function array_search(needle, haystack) {
    for(var key in haystack)
        if( haystack[key] == needle )
            return key;
    return false;
}

function onBeforeCycle(curr, next, opts, forward) {
    var index = [forward ? opts.currSlide+1 : opts.currSlide-1];
    $(opts.prev)[index == 0 ? 'hide' : 'show']();
    $(opts.next)[index == opts.slideCount - 1 ? 'hide' : 'show']();
}

function onClickCycle(isNext, zeroBasedSlideIndex, slideElement) {
	$el = $(slideElement);
	type = $el.parent().attr('rel');
	value = $el.attr('src').split('/');
	
	$('.' + type + 'Preview').cycle(zeroBasedSlideIndex);
	$('#' + type + 'Val').val(value[value.length-1]);
}

$(window).load(function(){
	$('.pieces, .previewPieces').css('overflow', 'hidden');
	
	var background = $('#backgroundVal').val();
	var mouth = $('#mouthVal').val();
	var eyes = $('#eyesVal').val();
	var accessories = $('#accessoriesVal').val();
	
	var htmlBackground = '';
	for(k in json.background)
		htmlBackground = htmlBackground + '<img src="/data/avatarsPieces/' + json.background[k] + '" />';
	
	$('.background .pieces').html(htmlBackground).cycle({ 
		startingSlide: array_search(background, json.background),
	    fx:     'scrollHorz', 
	    prev:   'input[name=backgroundBack]', 
	    next:   'input[name=backgroundNext]', 
	    before:   onBeforeCycle,
		prevNextClick: onClickCycle,
	    timeout: 0,
	    speed: 500,
	    nowrap: true
	});
	$('.backgroundPreview').html(htmlBackground).cycle({ 
		startingSlide: array_search(background, json.background),
	    fx:     'scrollHorz', 
	    timeout: 0,
	    speed: 500,
	    cssBefore: { zIndex: 50 },
	    cssAfter: { zIndex: 51 }
	});
	$('.eyesPreview img').css('z-index', 50);
	
	$('.tarsierPreview img').css('z-index', 52);
	
	var htmlEyes = '';
	for(k in json.eyes)
		htmlEyes = htmlEyes + '<img src="/data/avatarsPieces/' + json.eyes[k] + '" />';
	
	$('.eyes .pieces').html(htmlEyes).cycle({ 
		startingSlide: array_search(eyes, json.eyes),
	    fx:     'scrollHorz', 
	    prev:   'input[name=eyesBack]', 
	    next:   'input[name=eyesNext]', 
	    before:   onBeforeCycle,
		prevNextClick: onClickCycle,
	    timeout: 0,
	    speed: 500,
	    nowrap: true
	});
	$('.eyesPreview').html(htmlEyes).cycle({ 
		startingSlide: array_search(eyes, json.eyes),
	    fx:     'scrollHorz', 
	    timeout: 0,
	    speed: 500,
	    cssBefore: { zIndex: 54 },
	    cssAfter: { zIndex: 55 } 
	});
	
	$('.eyesPreview img').css('z-index', 54);
	
	var htmlMouth = '';
	for(k in json.mouth)
		htmlMouth = htmlMouth + '<img src="/data/avatarsPieces/' + json.mouth[k] + '" />';
	
	$('.mouth .pieces').html(htmlMouth).cycle({ 
		startingSlide: array_search(mouth, json.mouth),
	    fx:     'scrollHorz', 
	    prev:   'input[name=mouthBack]', 
	    next:   'input[name=mouthNext]', 
	    before:   onBeforeCycle,
		prevNextClick: onClickCycle,
	    timeout: 0,
	    speed: 500,
	    nowrap: true
	});
	$('.mouthPreview').html(htmlMouth).cycle({ 
		startingSlide: array_search(mouth, json.mouth),
	    fx:     'scrollHorz', 
	    timeout: 0,
	    speed: 500,
	    cssBefore: { zIndex: 56 },
	    cssAfter: { zIndex: 57 } 
	});
	
	$('.mouthPreview img').css('z-index', 56);
	
	var htmlAccessories = '';
	for(k in json.accessories)
		htmlAccessories = htmlAccessories + '<img src="/data/avatarsPieces/' + json.accessories[k] + '" />';
	
	$('.accessories .pieces').html(htmlAccessories).cycle({ 
		startingSlide: array_search(accessories, json.accessories),
	    fx:     'scrollHorz', 
	    prev:   'input[name=accessoriesBack]', 
	    next:   'input[name=accessoriesNext]', 
	    before:   onBeforeCycle,
		prevNextClick: onClickCycle,
	    timeout: 0,
	    speed: 500,
	    nowrap: true
	});
	$('.accessoriesPreview').html(htmlAccessories).cycle({ 
		startingSlide: array_search(accessories, json.accessories),
	    fx:     'scrollHorz', 
	    timeout: 0,
	    speed: 500,
	    cssBefore: { zIndex: 58 },
	    cssAfter: { zIndex: 59 } 
	});
	
	$('.preview .previewPieces').css('position', 'absolute');
	$('.accessoriesPreview img').css('z-index', 58);
	
	/**
	 *	Avatar aléatoire
	 */
	
});