// implement JSON.stringify serialization

if(typeof(JSON) != 'object' || typeof(JSON.stringify) != 'function'){
JSON = {}
JSON.stringify = function (obj) {

	var t = typeof (obj);
	if (t != "object" || obj === null) {

		// simple data type
		if (t == "string") obj = '"'+obj+'"';
		return String(obj);

	}else {

		// recurse array or object
		var n, v, json = [], arr = (obj && obj.constructor == Array);

		for (n in obj) {
			v = obj[n]; t = typeof(v);

			if (t == "string") v = '"'+v+'"';
			else if (t == "object" && v !== null) v = JSON.stringify(v);

			json.push((arr ? "" : '"' + n + '":') + String(v));
		}

		return (arr ? "[" : "{") + String(json) + (arr ? "]" : "}");
	}
};

}

$(document).ready(function(){
	
	findFlashProducts();
	
	// Start: jQuery Fancybox for Contao //
	$('a[rel*=lightbox]').each(function(){
	var value = $(this).attr('rel');
	value = value.replace('[', '_');
	value = value.replace(']', '');
	$(this).addClass('lightbox');
	$(this).removeAttr('rel');
	$(this).attr('rel', value);
	});
	
	$('a.lightbox').fancybox({
	'hideOnContentClick': true,
	'overlayColor': '#000000',
	'overlayOpacity': 0.3,
	'titlePosition': 'over' // outside, inside, over
	});
	// End: jQuery Fancybox for Contao //	
	
	
	// Start: NIVO SLIDER //
	$('.image-slider').wrap('<div id="slider-wrapper">');
	
	var showNav = false;
	if($('.image-slider').children().length > 1){
		showNav = true;		
	};
	
	$('.image-slider').nivoSlider({
		effect: 'fade', // Specify sets like: 'fold,fade,sliceDown'
		slices: 15, // For slice animations
		boxCols: 8, // For box animations
		boxRows: 4, // For box animations
		animSpeed: 500, // Slide transition speed
		pauseTime: 5000, // How long each slide will show
		startSlide: 0, // Set starting Slide (0 index)
		directionNav: showNav, // Next & Prev navigation
		directionNavHide: false, // Only show on hover
		controlNav: showNav, // 1,2,3... navigation
		controlNavThumbs: false, // Use thumbnails for Control Nav
		controlNavThumbsFromRel: false, // Use image rel for thumbs
		controlNavThumbsSearch: '.jpg', // Replace this with...
		controlNavThumbsReplace: '_thumb.jpg', // ...this in thumb Image src
		keyboardNav: true, // Use left & right arrows
		pauseOnHover: true, // Stop animation while hovering
		manualAdvance: false, // Force manual transitions
		captionOpacity: 0.8, // Universal caption opacity
		prevText: 'Prev', // Prev directionNav text
		nextText: 'Next', // Next directionNav text
		beforeChange: function(){
		}, // Triggers before a slide transition
		afterChange: function(){
		}, // Triggers after a slide transition
		slideshowEnd: function(){
		}, // Triggers after all slides have been shown
		lastSlide: function(){
		}, // Triggers when last slide is shown
		afterLoad: function(){
		} // Triggers when slider has loaded
	});
	
	// End: NIVO SLIDER //
	
})


function findFlashProducts(){
	var $productItems = $('.products div.item');
	if($productItems.length > 0 ){
		var products = [];
	
		$productItems.each(function(){
			var $item = $(this);
			var obj = {
				name: $item.find('div.name div.value').text(),
				image: $item.find('div.image div.value img').attr('src'),
				swf: $item.find('div.swf div.value a').attr('href'),
				parts: $item.find('div.parts div.value').text(),
				applications: $item.find('div.applications div.value').text()
			}
			
			products.push( obj )
			
		});
		
		
		var flashvars = {};

		flashvars.category = 'knochenzement';
		flashvars.language = 'de';
		flashvars.path = 'http://www.medmix.ch/';
		flashvars.products =  JSON.stringify(products).replace(/"/g, '##quot##');
		var params = { scale: "noscale", wmode: "transparent", base: '/' };
		var attributes = { };
	
		swfobject.embedSWF("/tl_files/flash/MedmixProducts.swf", "headerFlash", "100%", "370", "9.0.0","/tl_files/flash/expressInstall.swf", flashvars, params, attributes);
		
		if(swfobject.hasFlashPlayerVersion('9.0.0') ){
			$('.products').addClass('noprint-hide')
		}
		
		//$('.products').remove()
		//$('.products').text(flashvars.products)
		
	
	}
}


