jQuery(document).ready(function() {
	jQuery('#checkoutBlock, #previewBlock' ).hide();
	if(jQuery('#teaserBlock').length <= 0) {
		if (jQuery('#previewBlock').length > 0) {
			jQuery('#previewBlock').show();
		} else if (jQuery('#checkoutBlock').length > 0) {
			jQuery('#checkoutBlock').show();
		}
	}
	
	jQuery('#startButton, .startCoaching').click(function() {
		jQuery('#teaserBlock').hide();
		jQuery('#previewBlock').show();
	 });
	
	jQuery('#checkoutButton, .checkoutCoaching').click(function() {
		productAddToCartForm.submit(jQuery("#product_addtocart_form button")[0]);
	 });
});

baseUrls = {};

function setBaseUrls(config) {
        baseUrls.baseServiceUrl = config.baseServiceUrl;
        baseUrls.baseVideoUrl = config.baseVideoUrl;
        baseUrls.basePlayerUrl = config.basePlayerUrl;
        baseUrls.baseAssetUrl = config.baseAssetUrl;
}

function MotivadoPlayer(config) {
	if (config.product == undefined || config.baseServiceUrl == undefined) {
		return alert('Missing required parameters!');
	}

	setBaseUrls(config);
	
	var flashvars = config;
	flashvars.baseVideoUrl = config.baseVideoUrl || config.baseServiceUrl + 'videos/';
	flashvars.basePlayerUrl = config.basePlayerUrl || config.baseServiceUrl + 'player/';
	flashvars.baseAssetUrl = config.baseAssetUrl || config.baseServiceUrl + 'assets/';
	flashvars.skin = config.skin || undefined;
	flashvars.autoPlay = config.autoPlay || 'true';
	flashvars.debugMode = config.debugMode || 'false';
	
	jQuery(document).ready(function() {
		jQuery('#' + (config.element || 'MotivadoPlayer')).flash({
			swf: (config.basePlayerUrl || config.baseServiceUrl + 'player/') + (config.player || 'CoachingPlayer.swf'),
			width: config.width || 960,
			height: config.height || 350,
                        allowscriptaccess: 'always',
			flashvars: flashvars
		});
	});
        return true;
}

function VideoPlayer(config) {
	if (config.videoUrl == undefined || config.basePlayerUrl == undefined) {
		return alert('Missing required parameters!');
	}
	
	setBaseUrls(config);
	
	jQuery(document).ready(function() {
		jQuery('#' + (config.element || 'VideoPlayer')).flash({
			swf: config.basePlayerUrl + (config.player || 'VideoDisplay.swf'),
			width: config.width || 480,
			height: config.height || 350,
			wmode: config.wmode || 'transparent',
			flashvars: {
				singleVideo_url: config.videoUrl,//TODO: please rename to videoUrl
				autoPlay: config.autoPlay || 'false',
				debugMode: config.debugMode || 'false',
				coopid: config.id || null
			}
		})
	});
}

function buyCoaching(key, videoFileName) {
        //if we have a passed videoUrl reconfigure the video player to use it.
	baseUrls.baseVideoUrl = 'http://videos.motivado.de/'; //TODO: do not hard-code!
	if (videoFileName) {
		VideoPlayer({
			videoUrl: baseUrls.baseVideoUrl + videoFileName,
			basePlayerUrl: baseUrls.basePlayerUrl,
			element: "VideoPlayerCheckout",
            		autoPlay: "true"
		});
	}
	//TODO: Implement submit in buy content block. 
	//productAddToCartForm.submit(jQuery("#product_addtocart_form button")[0]);
	endCoaching();
	// TODO: replace video url in #checkoutBlock
	jQuery('#checkoutBlock').fadeIn(1000);
        // track display of checkout block
        pageTracker._trackPageview(document.location.href+'checkout-block');
}

function startCoaching(key) {
	// find coaching by key in list of purchased products on dashboard
	// if none is found, redirect to product page so the user can purchase it
	endCoaching();//TODO
}

function endCoaching() {
    
        $jQ.fn.exists = function(){return this.length>0;}
        // close pretty photo popup, if it is open
        if ($jQ('.pp_fade .pp_details a.pp_close').exists()) {
            $jQ.prettyPhoto.close();
        }
        
        // if not on customer dashboard, remove preview block
        if (window.location.pathname != '/coachings/') {
                jQuery('#previewBlock').remove();
        }
}

function callToAction() {
	$jQ('#callToAction').show(); //TODO: do not hard-code!
	$jQ('#MotivadoPlayer').hide(); //TODO: do not hard-code!
	return true;
}

